libtdepim

kwidgetlister.cpp
1 /*
2  kwidgetlister.cpp
3 
4  This file is part of libtdenetwork.
5  Copyright (c) 2001 Marc Mutz <mutz@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License,
9  version 2, as published by the Free Software Foundation.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this library with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #include "kwidgetlister.h"
33 
34 #include <tdelocale.h>
35 #include <kdebug.h>
36 
37 #include <tqpushbutton.h>
38 #include <tqlayout.h>
39 #include <tqhbox.h>
40 
41 #include <assert.h>
42 #include <kguiitem.h>
43 #include <kpushbutton.h>
44 #include <kdialog.h>
45 
46 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, TQWidget *parent, const char* name )
47  : TQWidget( parent, name )
48 {
49  mWidgetList.setAutoDelete(TRUE);
50 
51  mMinWidgets = TQMAX( minWidgets, 1 );
52  mMaxWidgets = TQMAX( maxWidgets, mMinWidgets + 1 );
53 
54  //--------- the button box
55  mLayout = new TQVBoxLayout(this, 0, 4);
56  mButtonBox = new TQHBox(this);
57  mButtonBox->setSpacing( KDialog::spacingHint() );
58  mLayout->addWidget( mButtonBox );
59 
60  mBtnMore = new KPushButton( KGuiItem( i18n( "more widgets", "More" ), "button_more" ), mButtonBox );
61  mButtonBox->setStretchFactor( mBtnMore, 0 );
62 
63  mBtnFewer = new KPushButton( KGuiItem( i18n( "fewer widgets", "Fewer" ), "button_fewer" ), mButtonBox );
64  mButtonBox->setStretchFactor( mBtnFewer, 0 );
65 
66  TQWidget *spacer = new TQWidget( mButtonBox );
67  mButtonBox->setStretchFactor( spacer, 1 );
68 
69  // FIXME: We need a KStdGuiItem::clear here and in other locations to be automagically RTL aware - Martijn
70  mBtnClear = new KPushButton( KGuiItem( i18n( "clear widgets", "Clear" ), "locationbar_erase" ), mButtonBox );
71  mButtonBox->setStretchFactor( mBtnClear, 0 );
72 
73  //---------- connect everything
74  connect( mBtnMore, TQ_SIGNAL(clicked()),
75  this, TQ_SLOT(slotMore()) );
76  connect( mBtnFewer, TQ_SIGNAL(clicked()),
77  this, TQ_SLOT(slotFewer()) );
78  connect( mBtnClear, TQ_SIGNAL(clicked()),
79  this, TQ_SLOT(slotClear()) );
80 
81  enableControls();
82 }
83 
84 KWidgetLister::~KWidgetLister()
85 {
86 }
87 
89 {
90  // the class should make certain that slotMore can't
91  // be called when mMaxWidgets are on screen.
92  assert( (int)mWidgetList.count() < mMaxWidgets );
93 
95  // adjustSize();
96  enableControls();
97 }
98 
100 {
101  // the class should make certain that slotFewer can't
102  // be called when mMinWidgets are on screen.
103  assert( (int)mWidgetList.count() > mMinWidgets );
104 
106  // adjustSize();
107  enableControls();
108 }
109 
111 {
113 
114  // clear remaining widgets
115  TQPtrListIterator<TQWidget> it( mWidgetList );
116  for ( it.toFirst() ; it.current() ; ++it )
117  clearWidget( (*it) );
118 
119  // adjustSize();
120  enableControls();
121  emit clearWidgets();
122 }
123 
125 {
126  if (!w) w = this->createWidget(this);
127 
128  mLayout->insertWidget( mLayout->findWidget( mButtonBox ), w );
129  mWidgetList.append( w );
130  w->show();
131  enableControls();
132  emit widgetAdded();
133  emit widgetAdded(w);
134 }
135 
137 {
138  // The layout will take care that the
139  // widget is removed from screen, too.
140  mWidgetList.removeLast();
141  enableControls();
142  emit widgetRemoved();
143 }
144 
145 void KWidgetLister::clearWidget( TQWidget* /*aWidget*/ )
146 {
147 }
148 
149 TQWidget* KWidgetLister::createWidget( TQWidget* parent )
150 {
151  return new TQWidget( parent );
152 }
153 
155 {
156  int superfluousWidgets = TQMAX( (int)mWidgetList.count() - aNum, 0 );
157  int missingWidgets = TQMAX( aNum - (int)mWidgetList.count(), 0 );
158 
159  // remove superfluous widgets
160  for ( ; superfluousWidgets ; superfluousWidgets-- )
162 
163  // add missing widgets
164  for ( ; missingWidgets ; missingWidgets-- )
165  addWidgetAtEnd();
166 }
167 
168 void KWidgetLister::enableControls()
169 {
170  int count = mWidgetList.count();
171  bool isMaxWidgets = ( count >= mMaxWidgets );
172  bool isMinWidgets = ( count <= mMinWidgets );
173 
174  mBtnMore->setEnabled( !isMaxWidgets );
175  mBtnFewer->setEnabled( !isMinWidgets );
176 }
177 
178 #include "kwidgetlister.moc"
virtual void slotMore()
Called whenever the user clicks on the 'more' button.
int mMaxWidgets
The maximum number of widgets that are to be shown on screen.
virtual void removeLastWidget()
Removes a single (always the last) widget.
void clearWidgets()
This signal is emitted whenever the clear button is clicked.
void widgetAdded()
This signal is emitted whenever a widget was added.
virtual void setNumberOfShownWidgetsTo(int aNum)
Sets the number of widgets on scrren to exactly aNum.
virtual void slotClear()
Called whenever the user clicks on the 'clear' button.
virtual TQWidget * createWidget(TQWidget *parent)
Because QT 2.x does not support signals/slots in template classes, we are forced to emulate this by f...
void widgetRemoved()
This signal is emitted whenever a widget was removed.
TQPtrList< TQWidget > mWidgetList
The list of widgets.
virtual void clearWidget(TQWidget *)
Called to clear a given widget.
int mMinWidgets
The minimum number of widgets that are to stay on screen.
virtual void addWidgetAtEnd(TQWidget *w=0)
Adds a single widget.
virtual void slotFewer()
Called whenever the user clicks on the 'fewer' button.