• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • widgets
kwidgetlister.cpp
Go to the documentation of this file.
1 /* -*- c++ -*-
2 
3  kwidgetlister.cpp
4 
5  This file is part of libkdepim.
6  Copyright (c) 2001 Marc Mutz <mutz@kde.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License,
10  version 2, as published by the Free Software Foundation.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this library with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include "kwidgetlister.h"
34 
35 #include <KDebug>
36 #include <KDialog>
37 #include <KLocale>
38 #include <KGuiItem>
39 #include <KHBox>
40 #include <KPushButton>
41 
42 #include <QPushButton>
43 #include <QLayout>
44 #include <QVBoxLayout>
45 
46 #include <assert.h>
47 
48 using namespace KPIM;
49 
50 class KWidgetLister::Private
51 {
52  public:
53  Private( KWidgetLister *qq )
54  : q( qq ),
55  mBtnMore( 0 ),
56  mBtnFewer( 0 ),
57  mBtnClear( 0 ),
58  mLayout( 0 ),
59  mButtonBox( 0 ),
60  mMinWidgets( 0 ),
61  mMaxWidgets( 0 )
62 
63 
64  {
65  }
66 
67  ~Private()
68  {
69  qDeleteAll( mWidgetList );
70  mWidgetList.clear();
71  }
72 
73  void enableControls();
74 
75  KWidgetLister *q;
76  QPushButton *mBtnMore, *mBtnFewer, *mBtnClear;
77  QVBoxLayout *mLayout;
78  KHBox *mButtonBox;
79  QList<QWidget*> mWidgetList;
80  int mMinWidgets;
81  int mMaxWidgets;
82 };
83 
84 void KWidgetLister::Private::enableControls()
85 {
86  const int count = mWidgetList.count();
87  const bool isMaxWidgets = ( count >= mMaxWidgets );
88  const bool isMinWidgets = ( count <= mMinWidgets );
89  if ( mBtnMore )
90  mBtnMore->setEnabled( !isMaxWidgets );
91  if ( mBtnFewer )
92  mBtnFewer->setEnabled( !isMinWidgets );
93 }
94 
95 KWidgetLister::KWidgetLister( bool fewerMoreButton, int minWidgets, int maxWidgets, QWidget *parent )
96  : QWidget( parent ), d( new Private( this ) )
97 {
98  d->mMinWidgets = qMax( minWidgets, 1 );
99  d->mMaxWidgets = qMax( maxWidgets, d->mMinWidgets + 1 );
100  init( fewerMoreButton );
101 }
102 
103 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, QWidget *parent )
104  : QWidget( parent ), d( new Private( this ) )
105 {
106  d->mMinWidgets = qMax( minWidgets, 1 );
107  d->mMaxWidgets = qMax( maxWidgets, d->mMinWidgets + 1 );
108  init();
109 }
110 
111 KWidgetLister::~KWidgetLister()
112 {
113  delete d;
114 }
115 
116 void KWidgetLister::init( bool fewerMoreButton )
117 {
118  //--------- the button box
119  d->mLayout = new QVBoxLayout( this );
120  d->mLayout->setMargin( 0 );
121  d->mLayout->setSpacing( 4 );
122 
123  d->mButtonBox = new KHBox( this );
124  d->mButtonBox->setSpacing( KDialog::spacingHint() );
125  d->mLayout->addWidget( d->mButtonBox );
126 
127  if ( fewerMoreButton )
128  {
129  d->mBtnMore = new KPushButton( KGuiItem( i18nc( "more widgets", "More" ),
130  QLatin1String("list-add") ), d->mButtonBox );
131  d->mButtonBox->setStretchFactor( d->mBtnMore, 0 );
132 
133  d->mBtnFewer = new KPushButton( KGuiItem( i18nc( "fewer widgets", "Fewer" ),
134  QLatin1String("list-remove") ), d->mButtonBox );
135  d->mButtonBox->setStretchFactor( d->mBtnFewer, 0 );
136  }
137  QWidget *spacer = new QWidget( d->mButtonBox );
138  d->mButtonBox->setStretchFactor( spacer, 1 );
139 
140  d->mBtnClear = new KPushButton( KStandardGuiItem::clear(), d->mButtonBox );
141  // FIXME a useful whats this. KStandardGuiItem::clear() returns a text with an edit box
142  d->mBtnClear->setWhatsThis( QString() );
143  d->mButtonBox->setStretchFactor( d->mBtnClear, 0 );
144 
145  //---------- connect everything
146  if ( fewerMoreButton )
147  {
148  connect( d->mBtnMore, SIGNAL(clicked()),
149  this, SLOT(slotMore()) );
150  connect( d->mBtnFewer, SIGNAL(clicked()),
151  this, SLOT(slotFewer()) );
152  }
153 
154  connect( d->mBtnClear, SIGNAL(clicked()),
155  this, SLOT(slotClear()) );
156 
157  d->enableControls();
158 
159 }
160 
161 void KWidgetLister::slotMore()
162 {
163  // the class should make certain that slotMore can't
164  // be called when mMaxWidgets are on screen.
165  assert( (int)d->mWidgetList.count() < d->mMaxWidgets );
166 
167  addWidgetAtEnd();
168  // adjustSize();
169  d->enableControls();
170 }
171 
172 void KWidgetLister::slotFewer()
173 {
174  // the class should make certain that slotFewer can't
175  // be called when mMinWidgets are on screen.
176  assert( (int)d->mWidgetList.count() > d->mMinWidgets );
177 
178  removeLastWidget();
179  // adjustSize();
180  d->enableControls();
181 }
182 
183 void KWidgetLister::slotClear()
184 {
185  setNumberOfShownWidgetsTo( d->mMinWidgets );
186 
187  // clear remaining widgets
188  foreach ( QWidget *widget, d->mWidgetList )
189  clearWidget( widget );
190 
191  // adjustSize();
192  d->enableControls();
193  emit clearWidgets();
194 }
195 
196 void KWidgetLister::addWidgetAtEnd( QWidget *widget )
197 {
198  if ( !widget )
199  widget = this->createWidget( this );
200 
201  d->mLayout->insertWidget( d->mLayout->indexOf( d->mButtonBox ), widget );
202  d->mWidgetList.append( widget );
203  widget->show();
204 
205  d->enableControls();
206  emit widgetAdded();
207  emit widgetAdded( widget );
208 }
209 
210 void KWidgetLister::removeLastWidget()
211 {
212  // The layout will take care that the
213  // widget is removed from screen, too.
214  delete d->mWidgetList.takeLast();
215  d->enableControls();
216  emit widgetRemoved();
217 }
218 
219 void KWidgetLister::clearWidget( QWidget *widget )
220 {
221  Q_UNUSED( widget );
222 }
223 
224 QWidget *KWidgetLister::createWidget( QWidget *parent )
225 {
226  return new QWidget( parent );
227 }
228 
229 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum )
230 {
231  int superfluousWidgets = qMax( (int)d->mWidgetList.count() - aNum, 0 );
232  int missingWidgets = qMax( aNum - (int)d->mWidgetList.count(), 0 );
233 
234  // remove superfluous widgets
235  for ( ; superfluousWidgets ; superfluousWidgets-- ) {
236  removeLastWidget();
237  }
238 
239  // add missing widgets
240  for ( ; missingWidgets ; missingWidgets-- ) {
241  addWidgetAtEnd();
242  }
243 }
244 
245 QList<QWidget*> KWidgetLister::widgets() const
246 {
247  return d->mWidgetList;
248 }
249 
250 int KWidgetLister::widgetsMinimum() const
251 {
252  return d->mMinWidgets;
253 }
254 
255 int KWidgetLister::widgetsMaximum() const
256 {
257  return d->mMaxWidgets;
258 }
259 
260 void KWidgetLister::removeWidget(QWidget*widget)
261 {
262  // The layout will take care that the
263  // widget is removed from screen, too.
264 
265  if ( d->mWidgetList.count() <= widgetsMinimum() )
266  return;
267 
268  const int index = d->mWidgetList.indexOf( widget );
269  QWidget* w = d->mWidgetList.takeAt(index);
270  w->deleteLater();
271  w = 0;
272  d->enableControls();
273  emit widgetRemoved( widget );
274  emit widgetRemoved();
275 
276 }
277 
278 void KWidgetLister::addWidgetAfterThisWidget(QWidget*currentWidget, QWidget* widget)
279 {
280  if ( !widget )
281  widget = this->createWidget( this );
282 
283  int index = d->mLayout->indexOf( currentWidget ? currentWidget : d->mButtonBox )+1;
284  d->mLayout->insertWidget( index, widget );
285  index = 0;
286  if (currentWidget) {
287  index = d->mWidgetList.indexOf(currentWidget);
288  d->mWidgetList.insert(index+1, widget);
289  } else {
290  d->mWidgetList.append(widget);
291  }
292  widget->show();
293 
294  d->enableControls();
295  emit widgetAdded();
296  emit widgetAdded( widget );
297 }
298 
299 
300 #include "kwidgetlister.moc"
kwidgetlister.h
KPIM::KWidgetLister::createWidget
virtual QWidget * createWidget(QWidget *parent)
Returns a new widget that shall be added to the lister.
Definition: kwidgetlister.cpp:224
KPIM::KWidgetLister::slotClear
virtual void slotClear()
Called whenever the user clicks on the 'clear' button.
Definition: kwidgetlister.cpp:183
QWidget
KPIM::KWidgetLister::clearWidgets
void clearWidgets()
This signal is emitted whenever the clear button is clicked.
KPIM::KWidgetLister
Widget that manages a list of other widgets (incl.
Definition: kwidgetlister.h:63
KPIM::KWidgetLister::widgetsMinimum
int widgetsMinimum() const
The minimum number of widgets that are to stay on screen.
Definition: kwidgetlister.cpp:250
KPIM::KWidgetLister::widgets
QList< QWidget * > widgets() const
Returns the list of widgets.
Definition: kwidgetlister.cpp:245
KPIM::KWidgetLister::widgetsMaximum
int widgetsMaximum() const
The maximum number of widgets that are to be shown on screen.
Definition: kwidgetlister.cpp:255
KPIM::KWidgetLister::KWidgetLister
KWidgetLister(int minWidgets=1, int maxWidgets=8, QWidget *parent=0)
Creates a new widget lister.
Definition: kwidgetlister.cpp:103
KPIM::KWidgetLister::addWidgetAtEnd
virtual void addWidgetAtEnd(QWidget *widget=0)
Adds a single widget.
Definition: kwidgetlister.cpp:196
KPIM::KWidgetLister::addWidgetAfterThisWidget
virtual void addWidgetAfterThisWidget(QWidget *currentWidget, QWidget *widget=0)
Add widget after specific widget.
Definition: kwidgetlister.cpp:278
KPIM::KWidgetLister::widgetRemoved
void widgetRemoved()
This signal is emitted whenever a widget was removed.
KPIM::KWidgetLister::slotMore
virtual void slotMore()
Called whenever the user clicks on the 'more' button.
Definition: kwidgetlister.cpp:161
KPIM::KWidgetLister::clearWidget
virtual void clearWidget(QWidget *w)
Called to clear a given widget.
Definition: kwidgetlister.cpp:219
KPIM::KWidgetLister::~KWidgetLister
virtual ~KWidgetLister()
Destroys the widget lister.
Definition: kwidgetlister.cpp:111
KPIM::KWidgetLister::setNumberOfShownWidgetsTo
virtual void setNumberOfShownWidgetsTo(int count)
Sets the number of widgets on scrren to exactly count.
Definition: kwidgetlister.cpp:229
KPIM::KWidgetLister::widgetAdded
void widgetAdded()
This signal is emitted whenever a widget was added.
KHBox
KPIM::KWidgetLister::slotFewer
virtual void slotFewer()
Called whenever the user clicks on the 'fewer' button.
Definition: kwidgetlister.cpp:172
KPIM::KWidgetLister::removeWidget
virtual void removeWidget(QWidget *widget)
Remove specific widget.
Definition: kwidgetlister.cpp:260
KPIM::KWidgetLister::removeLastWidget
virtual void removeLastWidget()
Removes a single (always the last) widget.
Definition: kwidgetlister.cpp:210
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



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