• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDE3Support

  • kde3support
  • kdeui
k3buttonbox.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Mario Weilguni (mweilguni@sime.com)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20/*
21 * K3ButtonBox class
22 *
23 * A container widget for buttons. Uses Qt layout control to place the
24 * buttons, can handle both vertical and horizontal button placement.
25*
26 * HISTORY
27 *
28 * 05/11/2004 Andrew Coles <andrew_coles@yahoo.co.uk>
29 * Now uses QPtrListIterators instead of indexing through data->buttons
30 * Item.button and data are now const pointers, set in the relevant constructors
31 *
32 * 03/08/2000 Mario Weilguni <mweilguni@kde.org>
33 * Removed all those long outdated Motif stuff
34 * Improved and clarified some if conditions (easier to understand)
35 *
36 * 11/13/98 Reginald Stadlbauer <reggie@kde.org>
37 * Now in Qt 1.4x motif default buttons have no extra width/height anymore.
38 * So the K3ButtonBox doesn't add this width/height to default buttons anymore
39 * which makes the buttons look better.
40 *
41 * 01/17/98 Mario Weilguni <mweilguni@sime.com>
42 * Fixed a bug in sizeHint()
43 * Improved the handling of Motif default buttons
44 *
45 * 01/09/98 Mario Weilguni <mweilguni@sime.com>
46 * The last button was to far right away from the right/bottom border.
47 * Fixed this. Removed old code. Buttons get now a minimum width.
48 * Programmer may now override minimum width and height of a button.
49 *
50 */
51
52#include "k3buttonbox.moc"
53#include <kglobalsettings.h>
54#include <kguiitem.h>
55#include <kpushbutton.h>
56#include <QList>
57#include <assert.h>
58
59#define minButtonWidth 50
60
61class K3ButtonBox::Item {
62public:
63 KPushButton* const button;
64 bool noexpand;
65 unsigned short stretch;
66 unsigned short actual_size;
67
68 Item(KPushButton* const _button) : button(_button) {}
69};
70
71template class QList<K3ButtonBox::Item *>;
72
73class K3ButtonBoxPrivate {
74public:
75 unsigned short border;
76 unsigned short autoborder;
77 unsigned short orientation;
78 bool activated;
79 QList<K3ButtonBox::Item *> buttons;
80};
81
82K3ButtonBox::K3ButtonBox(QWidget *parent, Qt::Orientation _orientation,
83 int border, int autoborder)
84 : QWidget(parent), data(new K3ButtonBoxPrivate)
85{
86 assert(data);
87
88 data->orientation = _orientation;
89 data->border = border;
90 data->autoborder = autoborder < 0 ? border : autoborder;
91}
92
93K3ButtonBox::~K3ButtonBox() {
94 while(!data->buttons.isEmpty())
95 delete data->buttons.takeFirst();
96
97 delete data;
98}
99
100QPushButton *K3ButtonBox::addButton(const QString& text, bool noexpand) {
101 Item* const item = new Item(new KPushButton(text, this));
102
103 item->noexpand = noexpand;
104 data->buttons.append(item);
105 item->button->adjustSize();
106
107 updateGeometry();
108
109 return item->button;
110}
111
112QPushButton *K3ButtonBox::addButton(const KGuiItem& guiitem, bool noexpand) {
113 Item* const item = new Item(new KPushButton(guiitem, this));
114
115 item->noexpand = noexpand;
116 data->buttons.append(item);
117 item->button->adjustSize();
118
119 updateGeometry();
120
121 return item->button;
122}
123
124 QPushButton *
125K3ButtonBox::addButton(
126 const QString & text,
127 QObject * receiver,
128 const char * slot,
129 bool noexpand
130)
131{
132 QPushButton * pb = addButton(text, noexpand);
133
134 if ((0 != receiver) && (0 != slot))
135 QObject::connect(pb, SIGNAL(clicked()), receiver, slot);
136
137 return pb;
138}
139
140 QPushButton *
141K3ButtonBox::addButton(
142 const KGuiItem& guiitem,
143 QObject * receiver,
144 const char * slot,
145 bool noexpand
146)
147{
148 QPushButton * pb = addButton(guiitem, noexpand);
149
150 if ((0 != receiver) && (0 != slot))
151 QObject::connect(pb, SIGNAL(clicked()), receiver, slot);
152
153 return pb;
154}
155
156void K3ButtonBox::addStretch(int scale) {
157 if(scale > 0) {
158 Item* const item = new Item(0);
159 item->noexpand = false;
160 item->stretch = scale;
161 data->buttons.append(item);
162 }
163}
164
165void K3ButtonBox::layout() {
166 // resize all buttons
167 const QSize bs = bestButtonSize();
168
169 foreach(Item *item, data->buttons) {
170 QPushButton* const b = item->button;
171 if(b) {
172 if(item->noexpand)
173 b->setFixedSize(buttonSizeHint(b));
174 else
175 b->setFixedSize(bs);
176 }
177 }
178
179 setMinimumSize(sizeHint());
180}
181
182void K3ButtonBox::placeButtons() {
183
184 if(data->orientation == Qt::Horizontal) {
185 // calculate free size and stretches
186 int fs = width() - 2 * data->border;
187 int stretch = 0;
188 {
189 foreach(Item *item, data->buttons) {
190 QPushButton* const b = item->button;
191 if(b) {
192 fs -= b->width();
193
194 // Last button?
195 if(!(item == data->buttons.last()))
196 fs -= data->autoborder;
197 } else {
198 stretch +=item->stretch;
199 }
200
201 }
202 }
203
204 // distribute buttons
205 int x_pos = data->border;
206 {
207 foreach(Item *item, data->buttons) {
208
209 QPushButton* const b = item->button;
210 if(b) {
211 b->move(x_pos, (height() - b->height()) / 2);
212
213 x_pos += b->width() + data->autoborder;
214 } else {
215 x_pos += (int)((((double)fs) * item->stretch) / stretch);
216 }
217
218 }
219 }
220
221 } else { // VERTICAL
222 // calcualte free size and stretches
223 int fs = height() - 2 * data->border;
224 int stretch = 0;
225 {
226 foreach(Item *item, data->buttons) {
227
228 QPushButton* const b = item->button;
229 if(b)
230 fs -= b->height() + data->autoborder;
231 else
232 stretch +=item->stretch;
233
234 }
235
236 }
237
238 // distribute buttons
239 int y_pos = data->border;
240 {
241 foreach(Item *item, data->buttons) {
242
243 QPushButton* const b = item->button;
244 if(b) {
245 b->move((width() - b->width()) / 2, y_pos);
246
247 y_pos += b->height() + data->autoborder;
248 } else {
249 y_pos += (int)((((double)fs) * item->stretch) / stretch);
250 }
251
252 }
253 }
254 }
255}
256
257void K3ButtonBox::resizeEvent(QResizeEvent *) {
258 placeButtons();
259}
260
261QSize K3ButtonBox::bestButtonSize() const {
262 QSize s(0, 0);
263
264 // calculate optimal size
265 foreach(Item *item, data->buttons) {
266
267 QPushButton* const b = item->button;
268
269 if(b && !item->noexpand) {
270 const QSize bs = buttonSizeHint(b);
271
272 const int bsWidth = bs.width();
273 const int bsHeight = bs.height();
274
275 if(bsWidth > s.width())
276 s.setWidth(bsWidth);
277 if(bsHeight > s.height())
278 s.setHeight(bsHeight);
279 }
280 }
281
282 return s;
283}
284
285QSize K3ButtonBox::sizeHint() const {
286 unsigned int dw;
287
288 if(data->buttons.isEmpty())
289 return QSize(0, 0);
290 else {
291 dw = 2 * data->border;
292
293 const QSize bs = bestButtonSize();
294
295 foreach(Item *item, data->buttons) {
296
297 QPushButton* const b = item->button;
298
299 if(b) {
300 QSize s;
301 if(item->noexpand)
302 s = buttonSizeHint(b);
303 else
304 s = bs;
305
306 if(data->orientation == Qt::Horizontal)
307 dw += s.width();
308 else
309 dw += s.height();
310
311 if( !(item == data->buttons.last()) )
312 dw += data->autoborder;
313 }
314
315 }
316
317 if(data->orientation == Qt::Horizontal)
318 return QSize(dw, bs.height() + 2 * data->border);
319 else
320 return QSize(bs.width() + 2 * data->border, dw);
321 }
322}
323
324QSizePolicy K3ButtonBox::sizePolicy() const
325{
326 return data->orientation == Qt::Horizontal?
327 QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) :
328 QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Minimum );
329}
330
331/*
332 * Returns the best size for a button. If a button is less than
333 * minButtonWidth pixels wide, return minButtonWidth pixels
334 * as minimum width
335 */
336QSize K3ButtonBox::buttonSizeHint(QPushButton *b) const {
337 QSize s = b->sizeHint();
338 const QSize ms = b->minimumSize();
339 if(s.width() < minButtonWidth)
340 s.setWidth(minButtonWidth);
341
342 // allows the programmer to override the settings
343 const int msWidth = ms.width();
344 const int msHeight = ms.height();
345
346 if(msWidth > s.width())
347 s.setWidth(msWidth);
348 if(msHeight > s.height())
349 s.setHeight(msHeight);
350
351 return s;
352}
K3ButtonBox::addButton
QPushButton * addButton(const QString &text, bool noexpand=false)
Add a new QPushButton.
Definition: k3buttonbox.cpp:100
K3ButtonBox::layout
void layout()
This function must be called once after all buttons have been inserted.
Definition: k3buttonbox.cpp:165
K3ButtonBox::resizeEvent
virtual void resizeEvent(QResizeEvent *)
Definition: k3buttonbox.cpp:257
K3ButtonBox::buttonSizeHint
QSize buttonSizeHint(QPushButton *) const
Definition: k3buttonbox.cpp:336
K3ButtonBox::bestButtonSize
QSize bestButtonSize() const
Definition: k3buttonbox.cpp:261
K3ButtonBox::sizeHint
virtual QSize sizeHint() const
Definition: k3buttonbox.cpp:285
K3ButtonBox::sizePolicy
virtual QSizePolicy sizePolicy() const
Definition: k3buttonbox.cpp:324
K3ButtonBox::~K3ButtonBox
~K3ButtonBox()
Free private data field.
Definition: k3buttonbox.cpp:93
K3ButtonBox::K3ButtonBox
K3ButtonBox(QWidget *parent, Qt::Orientation _orientation=Qt::Horizontal, int border=0, int _autoborder=6)
Create an empty container for buttons.
Definition: k3buttonbox.cpp:82
K3ButtonBox::addStretch
void addStretch(int scale=1)
Add a stretch to the buttonbox.
Definition: k3buttonbox.cpp:156
K3ButtonBox::placeButtons
void placeButtons()
Definition: k3buttonbox.cpp:182
KGuiItem
KPushButton
QList
QObject
QPushButton
QWidget
minButtonWidth
#define minButtonWidth
Definition: k3buttonbox.cpp:59
kglobalsettings.h
kguiitem.h
kpushbutton.h
Item
Item
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

Skip menu "KDE3Support"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal