summaryrefslogtreecommitdiffabout
path: root/microkde/kdeui/kbuttonbox.cpp
Unidiff
Diffstat (limited to 'microkde/kdeui/kbuttonbox.cpp') (more/less context) (show whitespace changes)
-rw-r--r--microkde/kdeui/kbuttonbox.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/microkde/kdeui/kbuttonbox.cpp b/microkde/kdeui/kbuttonbox.cpp
index 83d622a..3ea6703 100644
--- a/microkde/kdeui/kbuttonbox.cpp
+++ b/microkde/kdeui/kbuttonbox.cpp
@@ -4,197 +4,199 @@
4 This library is free software; you can redistribute it and/or 4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public 5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either 6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version. 7 version 2 of the License, or (at your option) any later version.
8 8
9 This library is distributed in the hope that it will be useful, 9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details. 12 Library General Public License for more details.
13 13
14 You should have received a copy of the GNU Library General Public License 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 15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. 17 Boston, MA 02111-1307, USA.
18*/ 18*/
19 19
20/* 20/*
21 * KButtonBox class 21 * KButtonBox class
22 * 22 *
23 * A container widget for buttons. Uses Qt layout control to place the 23 * A container widget for buttons. Uses Qt layout control to place the
24 * buttons, can handle both vertical and horizontal button placement. 24 * buttons, can handle both vertical and horizontal button placement.
25* 25*
26 * HISTORY 26 * HISTORY
27 * 27 *
28 * 03/08/2000 Mario Weilguni <mweilguni@kde.org> 28 * 03/08/2000 Mario Weilguni <mweilguni@kde.org>
29 * Removed all those long outdated Motif stuff 29 * Removed all those long outdated Motif stuff
30 * Improved and clarified some if conditions (easier to understand) 30 * Improved and clarified some if conditions (easier to understand)
31 * 31 *
32 * 11/13/98 Reginald Stadlbauer <reggie@kde.org> 32 * 11/13/98 Reginald Stadlbauer <reggie@kde.org>
33 * Now in Qt 1.4x motif default buttons have no extra width/height anymore. 33 * Now in Qt 1.4x motif default buttons have no extra width/height anymore.
34 * So the KButtonBox doesn't add this width/height to default buttons anymore 34 * So the KButtonBox doesn't add this width/height to default buttons anymore
35 * which makes the buttons look better. 35 * which makes the buttons look better.
36 * 36 *
37 * 01/17/98 Mario Weilguni <mweilguni@sime.com> 37 * 01/17/98 Mario Weilguni <mweilguni@sime.com>
38 * Fixed a bug in sizeHint() 38 * Fixed a bug in sizeHint()
39 * Improved the handling of Motif default buttons 39 * Improved the handling of Motif default buttons
40 * 40 *
41 * 01/09/98 Mario Weilguni <mweilguni@sime.com> 41 * 01/09/98 Mario Weilguni <mweilguni@sime.com>
42 * The last button was to far right away from the right/bottom border. 42 * The last button was to far right away from the right/bottom border.
43 * Fixed this. Removed old code. Buttons get now a minimum width. 43 * Fixed this. Removed old code. Buttons get now a minimum width.
44 * Programmer may now override minimum width and height of a button. 44 * Programmer may now override minimum width and height of a button.
45 * 45 *
46 */ 46 */
47 47
48//US #include "kbuttonbox.moc" 48//US #include "kbuttonbox.moc"
49 49
50#include <kbuttonbox.h> 50#include <kbuttonbox.h>
51#include <qpushbutton.h> 51#include <qpushbutton.h>
52#include <qptrlist.h> 52#include <q3ptrlist.h>
53//Added by qt3to4:
54#include <QResizeEvent>
53#include <assert.h> 55#include <assert.h>
54 56
55#define minButtonWidth 50 57#define minButtonWidth 50
56 58
57class KButtonBox::Item { 59class KButtonBox::Item {
58public: 60public:
59 QPushButton *button; 61 QPushButton *button;
60 bool noexpand; 62 bool noexpand;
61 unsigned short stretch; 63 unsigned short stretch;
62 unsigned short actual_size; 64 unsigned short actual_size;
63}; 65};
64 66
65template class QPtrList<KButtonBox::Item>; 67template class Q3PtrList<KButtonBox::Item>;
66 68
67class KButtonBoxPrivate { 69class KButtonBoxPrivate {
68public: 70public:
69 unsigned short border; 71 unsigned short border;
70 unsigned short autoborder; 72 unsigned short autoborder;
71 unsigned short orientation; 73 unsigned short orientation;
72 bool activated; 74 bool activated;
73 QPtrList<KButtonBox::Item> buttons; 75 Q3PtrList<KButtonBox::Item> buttons;
74}; 76};
75 77
76KButtonBox::KButtonBox(QWidget *parent, Orientation _orientation, 78KButtonBox::KButtonBox(QWidget *parent, Qt::Orientation _orientation,
77 int border, int autoborder) 79 int border, int autoborder)
78 : QWidget(parent) 80 : QWidget(parent)
79{ 81{
80 data = new KButtonBoxPrivate; 82 data = new KButtonBoxPrivate;
81 assert(data != 0); 83 assert(data != 0);
82 84
83 data->orientation = _orientation; 85 data->orientation = _orientation;
84 data->border = border; 86 data->border = border;
85 data->autoborder = autoborder < 0 ? border : autoborder; 87 data->autoborder = autoborder < 0 ? border : autoborder;
86 data->buttons.setAutoDelete(TRUE); 88 data->buttons.setAutoDelete(TRUE);
87} 89}
88 90
89KButtonBox::~KButtonBox() { 91KButtonBox::~KButtonBox() {
90 delete data; 92 delete data;
91} 93}
92 94
93QPushButton *KButtonBox::addButton(const QString& text, bool noexpand) { 95QPushButton *KButtonBox::addButton(const QString& text, bool noexpand) {
94 Item *item = new Item; 96 Item *item = new Item;
95 97
96 item->button = new QPushButton(text, this); 98 item->button = new QPushButton(text, this);
97 item->noexpand = noexpand; 99 item->noexpand = noexpand;
98 data->buttons.append(item); 100 data->buttons.append(item);
99 item->button->adjustSize(); 101 item->button->adjustSize();
100 102
101 return item->button; 103 return item->button;
102} 104}
103 105
104 QPushButton * 106 QPushButton *
105KButtonBox::addButton( 107KButtonBox::addButton(
106 const QString & text, 108 const QString & text,
107 QObject * receiver, 109 QObject * receiver,
108 const char * slot, 110 const char * slot,
109 bool noexpand 111 bool noexpand
110) 112)
111{ 113{
112 QPushButton * pb = addButton(text, noexpand); 114 QPushButton * pb = addButton(text, noexpand);
113 115
114 if ((0 != receiver) && (0 != slot)) 116 if ((0 != receiver) && (0 != slot))
115 QObject::connect(pb, SIGNAL(clicked()), receiver, slot); 117 QObject::connect(pb, SIGNAL(clicked()), receiver, slot);
116 118
117 return pb; 119 return pb;
118} 120}
119 121
120 122
121void KButtonBox::addStretch(int scale) { 123void KButtonBox::addStretch(int scale) {
122 if(scale > 0) { 124 if(scale > 0) {
123 Item *item = new Item; 125 Item *item = new Item;
124 item->button = 0; 126 item->button = 0;
125 item->noexpand = FALSE; 127 item->noexpand = FALSE;
126 item->stretch = scale; 128 item->stretch = scale;
127 data->buttons.append(item); 129 data->buttons.append(item);
128 } 130 }
129} 131}
130 132
131void KButtonBox::layout() { 133void KButtonBox::layout() {
132 // resize all buttons 134 // resize all buttons
133 QSize bs = bestButtonSize(); 135 QSize bs = bestButtonSize();
134 136
135 for(unsigned int i = 0; i < data->buttons.count(); i++) { 137 for(unsigned int i = 0; i < data->buttons.count(); i++) {
136 Item *item = data->buttons.at(i); 138 Item *item = data->buttons.at(i);
137 QPushButton *b = item->button; 139 QPushButton *b = item->button;
138 if(b != 0) { 140 if(b != 0) {
139 if(item->noexpand) 141 if(item->noexpand)
140 b->setFixedSize(buttonSizeHint(b)); 142 b->setFixedSize(buttonSizeHint(b));
141 else 143 else
142 b->setFixedSize(bs); 144 b->setFixedSize(bs);
143 } 145 }
144 } 146 }
145 147
146 setMinimumSize(sizeHint()); 148 setMinimumSize(sizeHint());
147} 149}
148 150
149void KButtonBox::placeButtons() { 151void KButtonBox::placeButtons() {
150 unsigned int i; 152 unsigned int i;
151 153
152 if(data->orientation == Horizontal) { 154 if(data->orientation == Qt::Horizontal) {
153 // calculate free size and stretches 155 // calculate free size and stretches
154 int fs = width() - 2 * data->border; 156 int fs = width() - 2 * data->border;
155 int stretch = 0; 157 int stretch = 0;
156 for(i = 0; i < data->buttons.count(); i++) { 158 for(i = 0; i < data->buttons.count(); i++) {
157 Item *item = data->buttons.at(i); 159 Item *item = data->buttons.at(i);
158 if(item->button != 0) { 160 if(item->button != 0) {
159 fs -= item->button->width(); 161 fs -= item->button->width();
160 162
161 // Last button? 163 // Last button?
162 if(i != data->buttons.count() - 1) 164 if(i != data->buttons.count() - 1)
163 fs -= data->autoborder; 165 fs -= data->autoborder;
164 } else 166 } else
165 stretch +=item->stretch; 167 stretch +=item->stretch;
166 } 168 }
167 169
168 // distribute buttons 170 // distribute buttons
169 int x_pos = data->border; 171 int x_pos = data->border;
170 for(i = 0; i < data->buttons.count(); i++) { 172 for(i = 0; i < data->buttons.count(); i++) {
171 Item *item = data->buttons.at(i); 173 Item *item = data->buttons.at(i);
172 if(item->button != 0) { 174 if(item->button != 0) {
173 QPushButton *b = item->button; 175 QPushButton *b = item->button;
174 b->move(x_pos, (height() - b->height()) / 2); 176 b->move(x_pos, (height() - b->height()) / 2);
175 177
176 x_pos += b->width() + data->autoborder; 178 x_pos += b->width() + data->autoborder;
177 } else 179 } else
178 x_pos += (int)((((double)fs) * item->stretch) / stretch); 180 x_pos += (int)((((double)fs) * item->stretch) / stretch);
179 } 181 }
180 } else { // VERTICAL 182 } else { // VERTICAL
181 // calcualte free size and stretches 183 // calcualte free size and stretches
182 int fs = height() - 2 * data->border; 184 int fs = height() - 2 * data->border;
183 int stretch = 0; 185 int stretch = 0;
184 for(i = 0; i < data->buttons.count(); i++) { 186 for(i = 0; i < data->buttons.count(); i++) {
185 Item *item = data->buttons.at(i); 187 Item *item = data->buttons.at(i);
186 if(item->button != 0) 188 if(item->button != 0)
187 fs -= item->button->height() + data->autoborder; 189 fs -= item->button->height() + data->autoborder;
188 else 190 else
189 stretch +=item->stretch; 191 stretch +=item->stretch;
190 } 192 }
191 193
192 // distribute buttons 194 // distribute buttons
193 int y_pos = data->border; 195 int y_pos = data->border;
194 for(i = 0; i < data->buttons.count(); i++) { 196 for(i = 0; i < data->buttons.count(); i++) {
195 Item *item = data->buttons.at(i); 197 Item *item = data->buttons.at(i);
196 if(item->button != 0) { 198 if(item->button != 0) {
197 QPushButton *b = item->button; 199 QPushButton *b = item->button;
198 b->move((width() - b->width()) / 2, y_pos); 200 b->move((width() - b->width()) / 2, y_pos);
199 201
200 y_pos += b->height() + data->autoborder; 202 y_pos += b->height() + data->autoborder;
@@ -206,95 +208,95 @@ void KButtonBox::placeButtons() {
206 208
207void KButtonBox::resizeEvent(QResizeEvent *) { 209void KButtonBox::resizeEvent(QResizeEvent *) {
208 placeButtons(); 210 placeButtons();
209} 211}
210 212
211QSize KButtonBox::bestButtonSize() const { 213QSize KButtonBox::bestButtonSize() const {
212 QSize s(0, 0); 214 QSize s(0, 0);
213 unsigned int i; 215 unsigned int i;
214 216
215 // calculate optimal size 217 // calculate optimal size
216 for(i = 0; i < data->buttons.count(); i++) { 218 for(i = 0; i < data->buttons.count(); i++) {
217 KButtonBox *that = (KButtonBox*)this; // to remove the const ;( 219 KButtonBox *that = (KButtonBox*)this; // to remove the const ;(
218 Item *item = that->data->buttons.at(i); 220 Item *item = that->data->buttons.at(i);
219 QPushButton *b = item->button; 221 QPushButton *b = item->button;
220 222
221 if(b != 0 && !item->noexpand) { 223 if(b != 0 && !item->noexpand) {
222 QSize bs = buttonSizeHint(b); 224 QSize bs = buttonSizeHint(b);
223 225
224 if(bs.width() > s.width()) 226 if(bs.width() > s.width())
225 s.setWidth(bs.width()); 227 s.setWidth(bs.width());
226 if(bs.height() > s.height()) 228 if(bs.height() > s.height())
227 s.setHeight(bs.height()); 229 s.setHeight(bs.height());
228 } 230 }
229 } 231 }
230 232
231 return s; 233 return s;
232} 234}
233 235
234QSize KButtonBox::sizeHint() const { 236QSize KButtonBox::sizeHint() const {
235 unsigned int i, dw; 237 unsigned int i, dw;
236 238
237 if(data->buttons.count() == 0) 239 if(data->buttons.count() == 0)
238 return QSize(0, 0); 240 return QSize(0, 0);
239 else { 241 else {
240 dw = 2 * data->border; 242 dw = 2 * data->border;
241 243
242 QSize bs = bestButtonSize(); 244 QSize bs = bestButtonSize();
243 for(i = 0; i < data->buttons.count(); i++) { 245 for(i = 0; i < data->buttons.count(); i++) {
244 KButtonBox *that = (KButtonBox*)this; 246 KButtonBox *that = (KButtonBox*)this;
245 Item *item = that->data->buttons.at(i); 247 Item *item = that->data->buttons.at(i);
246 QPushButton *b = item->button; 248 QPushButton *b = item->button;
247 if(b != 0) { 249 if(b != 0) {
248 QSize s; 250 QSize s;
249 if(item->noexpand) 251 if(item->noexpand)
250 s = that->buttonSizeHint(b); 252 s = that->buttonSizeHint(b);
251 else 253 else
252 s = bs; 254 s = bs;
253 255
254 if(data->orientation == Horizontal) 256 if(data->orientation == Qt::Horizontal)
255 dw += s.width(); 257 dw += s.width();
256 else 258 else
257 dw += s.height(); 259 dw += s.height();
258 260
259 if( i != data->buttons.count() - 1 ) 261 if( i != data->buttons.count() - 1 )
260 dw += data->autoborder; 262 dw += data->autoborder;
261 } 263 }
262 } 264 }
263 265
264 if(data->orientation == Horizontal) 266 if(data->orientation == Qt::Horizontal)
265 return QSize(dw, bs.height() + 2 * data->border); 267 return QSize(dw, bs.height() + 2 * data->border);
266 else 268 else
267 return QSize(bs.width() + 2 * data->border, dw); 269 return QSize(bs.width() + 2 * data->border, dw);
268 } 270 }
269} 271}
270 272
271QSizePolicy KButtonBox::sizePolicy() const 273QSizePolicy KButtonBox::sizePolicy() const
272{ 274{
273 return data->orientation == Horizontal? 275 return data->orientation == Qt::Horizontal?
274 QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) : 276 QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) :
275 QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Minimum ); 277 QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Minimum );
276} 278}
277 279
278/* 280/*
279 * Returns the best size for a button. If a button is less than 281 * Returns the best size for a button. If a button is less than
280 * minButtonWidth pixels wide, return minButtonWidth pixels 282 * minButtonWidth pixels wide, return minButtonWidth pixels
281 * as minimum width 283 * as minimum width
282 */ 284 */
283QSize KButtonBox::buttonSizeHint(QPushButton *b) const { 285QSize KButtonBox::buttonSizeHint(QPushButton *b) const {
284 QSize s = b->sizeHint(); 286 QSize s = b->sizeHint();
285 QSize ms = b->minimumSize(); 287 QSize ms = b->minimumSize();
286 if(s.width() < minButtonWidth) 288 if(s.width() < minButtonWidth)
287 s.setWidth(minButtonWidth); 289 s.setWidth(minButtonWidth);
288 290
289 // allows the programmer to override the settings 291 // allows the programmer to override the settings
290 if(ms.width() > s.width()) 292 if(ms.width() > s.width())
291 s.setWidth(ms.width()); 293 s.setWidth(ms.width());
292 if(ms.height() > s.height()) 294 if(ms.height() > s.height())
293 s.setHeight(ms.height()); 295 s.setHeight(ms.height());
294 296
295 return s; 297 return s;
296} 298}
297 299
298void KButtonBox::virtual_hook( int, void* ) 300void KButtonBox::virtual_hook( int, void* )
299{ /*BASE::virtual_hook( id, data );*/ } 301{ /*BASE::virtual_hook( id, data );*/ }
300 302