• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kbuttonbox.cpp
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  * KButtonBox 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 KButtonBox 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 "kbuttonbox.moc"
53 #include <tdeglobalsettings.h>
54 #include <kguiitem.h>
55 #include <kpushbutton.h>
56 #include <tqptrlist.h>
57 #include <assert.h>
58 
59 #define minButtonWidth 50
60 
61 class KButtonBox::Item {
62 public:
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 
71 template class TQPtrList<KButtonBox::Item>;
72 
73 class KButtonBoxPrivate {
74 public:
75  unsigned short border;
76  unsigned short autoborder;
77  unsigned short orientation;
78  bool activated;
79  TQPtrList<KButtonBox::Item> buttons;
80 };
81 
82 KButtonBox::KButtonBox(TQWidget *parent, Orientation _orientation,
83  int border, int autoborder)
84  : TQWidget(parent), data(new KButtonBoxPrivate)
85 {
86  assert(data);
87 
88  data->orientation = _orientation;
89  data->border = border;
90  data->autoborder = autoborder < 0 ? border : autoborder;
91  data->buttons.setAutoDelete(true);
92 }
93 
94 KButtonBox::~KButtonBox() {
95  delete data;
96 }
97 
98 TQPushButton *KButtonBox::addButton(const TQString& text, bool noexpand) {
99  Item* const item = new Item(new KPushButton(text, this));
100 
101  item->noexpand = noexpand;
102  data->buttons.append(item);
103  item->button->adjustSize();
104 
105  this->updateGeometry();
106 
107  return item->button;
108 }
109 
110 TQPushButton *KButtonBox::addButton(const KGuiItem& guiitem, bool noexpand) {
111  Item* const item = new Item(new KPushButton(guiitem, this));
112 
113  item->noexpand = noexpand;
114  data->buttons.append(item);
115  item->button->adjustSize();
116 
117  this->updateGeometry();
118 
119  return item->button;
120 }
121 
122  TQPushButton *
123 KButtonBox::addButton(
124  const TQString & text,
125  TQObject * receiver,
126  const char * slot,
127  bool noexpand
128 )
129 {
130  TQPushButton * pb = addButton(text, noexpand);
131 
132  if ((0 != receiver) && (0 != slot))
133  TQObject::connect(pb, TQ_SIGNAL(clicked()), receiver, slot);
134 
135  return pb;
136 }
137 
138  TQPushButton *
139 KButtonBox::addButton(
140  const KGuiItem& guiitem,
141  TQObject * receiver,
142  const char * slot,
143  bool noexpand
144 )
145 {
146  TQPushButton * pb = addButton(guiitem, noexpand);
147 
148  if ((0 != receiver) && (0 != slot))
149  TQObject::connect(pb, TQ_SIGNAL(clicked()), receiver, slot);
150 
151  return pb;
152 }
153 
154 void KButtonBox::addStretch(int scale) {
155  if(scale > 0) {
156  Item* const item = new Item(0);
157  item->noexpand = false;
158  item->stretch = scale;
159  data->buttons.append(item);
160  }
161 }
162 
163 void KButtonBox::layout() {
164  // resize all buttons
165  const TQSize bs = bestButtonSize();
166 
167  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
168  Item* item;
169 
170  while ( (item = itr.current()) != 0 ) {
171  TQPushButton* const b = item->button;
172  if(b) {
173  if(item->noexpand)
174  b->setFixedSize(buttonSizeHint(b));
175  else
176  b->setFixedSize(bs);
177  }
178  ++itr;
179  }
180 
181  setMinimumSize(sizeHint());
182 }
183 
184 void KButtonBox::placeButtons() {
185 
186  if(data->orientation == TQt::Horizontal) {
187  // calculate free size and stretches
188  int fs = width() - 2 * data->border;
189  int stretch = 0;
190  {
191  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
192  Item *item;
193 
194  while ( (item = itr.current()) != 0 ) {
195  TQPushButton* const b = item->button;
196  if(b) {
197  fs -= b->width();
198 
199  // Last button?
200  if(!itr.atLast())
201  fs -= data->autoborder;
202  } else {
203  stretch +=item->stretch;
204  }
205 
206  ++itr;
207  }
208  }
209 
210  // distribute buttons
211  int x_pos = data->border;
212  {
213  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
214  Item *item;
215 
216  while ( (item = itr.current()) != 0 ) {
217  TQPushButton* const b = item->button;
218  if(b) {
219  b->move(x_pos, (height() - b->height()) / 2);
220 
221  x_pos += b->width() + data->autoborder;
222  } else {
223  x_pos += (int)((((double)fs) * item->stretch) / stretch);
224  }
225 
226  ++itr;
227  }
228  }
229 
230  } else { // VERTICAL
231  // calcualte free size and stretches
232  int fs = height() - 2 * data->border;
233  int stretch = 0;
234  {
235  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
236  Item *item;
237 
238  while ( (item = itr.current()) != 0 ) {
239  TQPushButton* const b = item->button;
240  if(b)
241  fs -= b->height() + data->autoborder;
242  else
243  stretch +=item->stretch;
244 
245  ++itr;
246  }
247 
248  }
249 
250  // distribute buttons
251  int y_pos = data->border;
252  {
253  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
254  Item *item;
255 
256  while ( (item = itr.current()) != 0 ) {
257  TQPushButton* const b = item->button;
258  if(b) {
259  b->move((width() - b->width()) / 2, y_pos);
260 
261  y_pos += b->height() + data->autoborder;
262  } else {
263  y_pos += (int)((((double)fs) * item->stretch) / stretch);
264  }
265 
266  ++itr;
267  }
268  }
269  }
270 }
271 
272 void KButtonBox::resizeEvent(TQResizeEvent *) {
273  placeButtons();
274 }
275 
276 TQSize KButtonBox::bestButtonSize() const {
277  TQSize s(0, 0);
278 
279  // calculate optimal size
280  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
281  Item *item;
282 
283  while ( (item = itr.current()) != 0 ) {
284  TQPushButton* const b = item->button;
285 
286  if(b && !item->noexpand) {
287  const TQSize bs = buttonSizeHint(b);
288 
289  const int bsWidth = bs.width();
290  const int bsHeight = bs.height();
291 
292  if(bsWidth > s.width())
293  s.setWidth(bsWidth);
294  if(bsHeight > s.height())
295  s.setHeight(bsHeight);
296  }
297  ++itr;
298  }
299 
300  return s;
301 }
302 
303 TQSize KButtonBox::sizeHint() const {
304  unsigned int dw;
305 
306  if(data->buttons.isEmpty())
307  return TQSize(0, 0);
308  else {
309  dw = 2 * data->border;
310 
311  const TQSize bs = bestButtonSize();
312 
313  TQPtrListIterator<KButtonBox::Item> itr(data->buttons);
314  Item *item;
315 
316  while ( (item = itr.current()) != 0 ) {
317  TQPushButton* const b = item->button;
318 
319  if(b) {
320  TQSize s;
321  if(item->noexpand)
322  s = buttonSizeHint(b);
323  else
324  s = bs;
325 
326  if(data->orientation == TQt::Horizontal)
327  dw += s.width();
328  else
329  dw += s.height();
330 
331  if( !itr.atLast() )
332  dw += data->autoborder;
333  }
334 
335  ++itr;
336  }
337 
338  if(data->orientation == TQt::Horizontal)
339  return TQSize(dw, bs.height() + 2 * data->border);
340  else
341  return TQSize(bs.width() + 2 * data->border, dw);
342  }
343 }
344 
345 TQSizePolicy KButtonBox::sizePolicy() const
346 {
347  return data->orientation == TQt::Horizontal?
348  TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Fixed ) :
349  TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Minimum );
350 }
351 
352 /*
353  * Returns the best size for a button. If a button is less than
354  * minButtonWidth pixels wide, return minButtonWidth pixels
355  * as minimum width
356  */
357 TQSize KButtonBox::buttonSizeHint(TQPushButton *b) const {
358  TQSize s = b->sizeHint();
359  const TQSize ms = b->minimumSize();
360  if(s.width() < minButtonWidth)
361  s.setWidth(minButtonWidth);
362 
363  // allows the programmer to override the settings
364  const int msWidth = ms.width();
365  const int msHeight = ms.height();
366 
367  if(msWidth > s.width())
368  s.setWidth(msWidth);
369  if(msHeight > s.height())
370  s.setHeight(msHeight);
371 
372  return s;
373 }
374 
375 void KButtonBox::virtual_hook( int, void* )
376 { /*BASE::virtual_hook( id, data );*/ }
377 
KButtonBox::bestButtonSize
TQSize bestButtonSize() const
Definition: kbuttonbox.cpp:276
KButtonBox::addButton
TQPushButton * addButton(const TQString &text, bool noexpand=false)
Add a new TQPushButton.
Definition: kbuttonbox.cpp:98
KButtonBox::KButtonBox
KButtonBox(TQWidget *parent, Orientation _orientation=TQt::Horizontal, int border=0, int _autoborder=6)
Create an empty container for buttons.
Definition: kbuttonbox.cpp:82
KButtonBox::addStretch
void addStretch(int scale=1)
Add a stretch to the buttonbox.
Definition: kbuttonbox.cpp:154
KButtonBox::layout
void layout()
This function must be called once after all buttons have been inserted.
Definition: kbuttonbox.cpp:163
KButtonBox::sizeHint
virtual TQSize sizeHint() const
Definition: kbuttonbox.cpp:303
KButtonBox::~KButtonBox
~KButtonBox()
Free private data field.
Definition: kbuttonbox.cpp:94
KGuiItem
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:39
KPushButton
This is nothing but a TQPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:38

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.1
This website is maintained by Timothy Pearson.