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

libkdepim

  • sources
  • kde-4.14
  • 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 <QVBoxLayout>
44 
45 #include <assert.h>
46 
47 using namespace KPIM;
48 
49 class KWidgetLister::Private
50 {
51 public:
52  Private( KWidgetLister *qq )
53  : q( qq ),
54  mBtnMore( 0 ),
55  mBtnFewer( 0 ),
56  mBtnClear( 0 ),
57  mLayout( 0 ),
58  mButtonBox( 0 ),
59  mMinWidgets( 0 ),
60  mMaxWidgets( 0 )
61 
62 
63  {
64  }
65 
66  ~Private()
67  {
68  qDeleteAll( mWidgetList );
69  mWidgetList.clear();
70  }
71 
72  void enableControls();
73 
74  KWidgetLister *q;
75  QPushButton *mBtnMore, *mBtnFewer, *mBtnClear;
76  QVBoxLayout *mLayout;
77  KHBox *mButtonBox;
78  QList<QWidget*> mWidgetList;
79  int mMinWidgets;
80  int mMaxWidgets;
81 };
82 
83 void KWidgetLister::Private::enableControls()
84 {
85  const int count = mWidgetList.count();
86  const bool isMaxWidgets = ( count >= mMaxWidgets );
87  const bool isMinWidgets = ( count <= mMinWidgets );
88  if ( mBtnMore )
89  mBtnMore->setEnabled( !isMaxWidgets );
90  if ( mBtnFewer )
91  mBtnFewer->setEnabled( !isMinWidgets );
92 }
93 
94 KWidgetLister::KWidgetLister( bool fewerMoreButton, int minWidgets, int maxWidgets, QWidget *parent )
95  : QWidget( parent ), d( new Private( this ) )
96 {
97  d->mMinWidgets = qMax( minWidgets, 1 );
98  d->mMaxWidgets = qMax( maxWidgets, d->mMinWidgets + 1 );
99  init( fewerMoreButton );
100 }
101 
102 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, QWidget *parent )
103  : QWidget( parent ), d( new Private( this ) )
104 {
105  d->mMinWidgets = qMax( minWidgets, 1 );
106  d->mMaxWidgets = qMax( maxWidgets, d->mMinWidgets + 1 );
107  init();
108 }
109 
110 KWidgetLister::~KWidgetLister()
111 {
112  delete d;
113 }
114 
115 void KWidgetLister::init( bool fewerMoreButton )
116 {
117  //--------- the button box
118  d->mLayout = new QVBoxLayout( this );
119  d->mLayout->setMargin( 0 );
120  d->mLayout->setSpacing( 4 );
121 
122  d->mButtonBox = new KHBox( this );
123  d->mButtonBox->setSpacing( KDialog::spacingHint() );
124  d->mLayout->addWidget( d->mButtonBox );
125 
126  if ( fewerMoreButton )
127  {
128  d->mBtnMore = new KPushButton( KGuiItem( i18nc( "more widgets", "More" ),
129  QLatin1String("list-add") ), d->mButtonBox );
130  d->mButtonBox->setStretchFactor( d->mBtnMore, 0 );
131 
132  d->mBtnFewer = new KPushButton( KGuiItem( i18nc( "fewer widgets", "Fewer" ),
133  QLatin1String("list-remove") ), d->mButtonBox );
134  d->mButtonBox->setStretchFactor( d->mBtnFewer, 0 );
135  }
136  QWidget *spacer = new QWidget( d->mButtonBox );
137  d->mButtonBox->setStretchFactor( spacer, 1 );
138 
139  d->mBtnClear = new KPushButton( KStandardGuiItem::clear(), d->mButtonBox );
140  // FIXME a useful whats this. KStandardGuiItem::clear() returns a text with an edit box
141  d->mBtnClear->setWhatsThis( QString() );
142  d->mButtonBox->setStretchFactor( d->mBtnClear, 0 );
143 
144  //---------- connect everything
145  if ( fewerMoreButton )
146  {
147  connect( d->mBtnMore, SIGNAL(clicked()),
148  this, SLOT(slotMore()) );
149  connect( d->mBtnFewer, SIGNAL(clicked()),
150  this, SLOT(slotFewer()) );
151  }
152 
153  connect( d->mBtnClear, SIGNAL(clicked()),
154  this, SLOT(slotClear()) );
155 
156  d->enableControls();
157 
158 }
159 
160 void KWidgetLister::slotMore()
161 {
162  // the class should make certain that slotMore can't
163  // be called when mMaxWidgets are on screen.
164  assert( (int)d->mWidgetList.count() < d->mMaxWidgets );
165 
166  addWidgetAtEnd();
167  // adjustSize();
168  d->enableControls();
169 }
170 
171 void KWidgetLister::slotFewer()
172 {
173  // the class should make certain that slotFewer can't
174  // be called when mMinWidgets are on screen.
175  assert( (int)d->mWidgetList.count() > d->mMinWidgets );
176 
177  removeLastWidget();
178  // adjustSize();
179  d->enableControls();
180 }
181 
182 void KWidgetLister::slotClear()
183 {
184  setNumberOfShownWidgetsTo( d->mMinWidgets );
185 
186  // clear remaining widgets
187  foreach ( QWidget *widget, d->mWidgetList )
188  clearWidget( widget );
189 
190  // adjustSize();
191  d->enableControls();
192  emit clearWidgets();
193 }
194 
195 void KWidgetLister::addWidgetAtEnd( QWidget *widget )
196 {
197  if ( !widget )
198  widget = this->createWidget( this );
199 
200  d->mLayout->insertWidget( d->mLayout->indexOf( d->mButtonBox ), widget );
201  d->mWidgetList.append( widget );
202  widget->show();
203 
204  d->enableControls();
205  emit widgetAdded();
206  emit widgetAdded( widget );
207 }
208 
209 void KWidgetLister::removeLastWidget()
210 {
211  // The layout will take care that the
212  // widget is removed from screen, too.
213  delete d->mWidgetList.takeLast();
214  d->enableControls();
215  emit widgetRemoved();
216 }
217 
218 void KWidgetLister::clearWidget( QWidget *widget )
219 {
220  Q_UNUSED( widget );
221 }
222 
223 QWidget *KWidgetLister::createWidget( QWidget *parent )
224 {
225  return new QWidget( parent );
226 }
227 
228 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum )
229 {
230  int superfluousWidgets = qMax( (int)d->mWidgetList.count() - aNum, 0 );
231  int missingWidgets = qMax( aNum - (int)d->mWidgetList.count(), 0 );
232 
233  // remove superfluous widgets
234  for ( ; superfluousWidgets ; superfluousWidgets-- ) {
235  removeLastWidget();
236  }
237 
238  // add missing widgets
239  for ( ; missingWidgets ; missingWidgets-- ) {
240  addWidgetAtEnd();
241  }
242 }
243 
244 QList<QWidget*> KWidgetLister::widgets() const
245 {
246  return d->mWidgetList;
247 }
248 
249 int KWidgetLister::widgetsMinimum() const
250 {
251  return d->mMinWidgets;
252 }
253 
254 int KWidgetLister::widgetsMaximum() const
255 {
256  return d->mMaxWidgets;
257 }
258 
259 void KWidgetLister::removeWidget(QWidget*widget)
260 {
261  // The layout will take care that the
262  // widget is removed from screen, too.
263 
264  if ( d->mWidgetList.count() <= widgetsMinimum() )
265  return;
266 
267  const int index = d->mWidgetList.indexOf( widget );
268  QWidget* w = d->mWidgetList.takeAt(index);
269  w->deleteLater();
270  w = 0;
271  d->enableControls();
272  emit widgetRemoved( widget );
273  emit widgetRemoved();
274 
275 }
276 
277 void KWidgetLister::addWidgetAfterThisWidget(QWidget*currentWidget, QWidget* widget)
278 {
279  if ( !widget )
280  widget = this->createWidget( this );
281 
282  int index = d->mLayout->indexOf( currentWidget ? currentWidget : d->mButtonBox )+1;
283  d->mLayout->insertWidget( index, widget );
284  if (currentWidget) {
285  index = d->mWidgetList.indexOf(currentWidget);
286  d->mWidgetList.insert(index+1, widget);
287  } else {
288  d->mWidgetList.append(widget);
289  }
290  widget->show();
291 
292  d->enableControls();
293  emit widgetAdded();
294  emit widgetAdded( widget );
295 }
296 
297 
kwidgetlister.h
QWidget
KPIM::KWidgetLister::createWidget
virtual QWidget * createWidget(QWidget *parent)
Returns a new widget that shall be added to the lister.
Definition: kwidgetlister.cpp:223
KPIM::KWidgetLister::slotClear
virtual void slotClear()
Called whenever the user clicks on the 'clear' button.
Definition: kwidgetlister.cpp:182
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:249
KPIM::KWidgetLister::widgets
QList< QWidget * > widgets() const
Returns the list of widgets.
Definition: kwidgetlister.cpp:244
KPIM::KWidgetLister::widgetsMaximum
int widgetsMaximum() const
The maximum number of widgets that are to be shown on screen.
Definition: kwidgetlister.cpp:254
KPIM::KWidgetLister::KWidgetLister
KWidgetLister(int minWidgets=1, int maxWidgets=8, QWidget *parent=0)
Creates a new widget lister.
Definition: kwidgetlister.cpp:102
KPIM::KWidgetLister::addWidgetAtEnd
virtual void addWidgetAtEnd(QWidget *widget=0)
Adds a single widget.
Definition: kwidgetlister.cpp:195
QList::count
int count(const T &value) const
KPIM::KWidgetLister::addWidgetAfterThisWidget
virtual void addWidgetAfterThisWidget(QWidget *currentWidget, QWidget *widget=0)
Add widget after specific widget.
Definition: kwidgetlister.cpp:277
QVBoxLayout
QObject::deleteLater
void deleteLater()
QString
QList< QWidget * >
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:160
KPIM::KWidgetLister::clearWidget
virtual void clearWidget(QWidget *w)
Called to clear a given widget.
Definition: kwidgetlister.cpp:218
KPIM::KWidgetLister::~KWidgetLister
virtual ~KWidgetLister()
Destroys the widget lister.
Definition: kwidgetlister.cpp:110
KPIM::KWidgetLister::setNumberOfShownWidgetsTo
virtual void setNumberOfShownWidgetsTo(int count)
Sets the number of widgets on scrren to exactly count.
Definition: kwidgetlister.cpp:228
QLatin1String
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
KPIM::KWidgetLister::widgetAdded
void widgetAdded()
This signal is emitted whenever a widget was added.
QPushButton
KHBox
QWidget::show
void show()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KPIM::KWidgetLister::slotFewer
virtual void slotFewer()
Called whenever the user clicks on the 'fewer' button.
Definition: kwidgetlister.cpp:171
KPIM::KWidgetLister::removeWidget
virtual void removeWidget(QWidget *widget)
Remove specific widget.
Definition: kwidgetlister.cpp:259
KPIM::KWidgetLister::removeLastWidget
virtual void removeLastWidget()
Removes a single (always the last) widget.
Definition: kwidgetlister.cpp:209
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:50 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
  • pimprint

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