libkdenetwork Library API Documentation

kwidgetlister.cpp

00001 /*  -*- c++ -*-
00002     kwidgetlister.cpp
00003 
00004     This file is part of libkdenetwork.
00005     Copyright (c) 2001 Marc Mutz <mutz@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License,
00009     version 2, as published by the Free Software Foundation.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this library; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this library with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #include "kwidgetlister.h"
00033 
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 
00037 #include <qpushbutton.h>
00038 #include <qlayout.h>
00039 #include <qhbox.h>
00040 
00041 #include <assert.h>
00042 
00043 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, QWidget *parent, const char* name )
00044   : QWidget( parent, name )
00045 {
00046   mWidgetList.setAutoDelete(TRUE);
00047 
00048   mMinWidgets = QMAX( minWidgets, 1 );
00049   mMaxWidgets = QMAX( maxWidgets, mMinWidgets + 1 );
00050 
00051   //--------- the button box
00052   mLayout = new QVBoxLayout(this, 0, 4);
00053   mButtonBox = new QHBox(this);
00054   mLayout->addWidget( mButtonBox );
00055 
00056   mBtnMore = new QPushButton( i18n("more widgets","More"), mButtonBox );
00057   mButtonBox->setStretchFactor( mBtnMore, 0 );
00058 
00059   mBtnFewer = new QPushButton( i18n("fewer widgets","Fewer"), mButtonBox );
00060   mButtonBox->setStretchFactor( mBtnFewer, 0 );
00061 
00062   QWidget *spacer = new QWidget( mButtonBox );
00063   mButtonBox->setStretchFactor( spacer, 1 );
00064 
00065   mBtnClear = new QPushButton( i18n("clear widgets","Clear"), mButtonBox );
00066   mButtonBox->setStretchFactor( mBtnClear, 0 );
00067 
00068   //---------- connect everything
00069   connect( mBtnMore, SIGNAL(clicked()),
00070        this, SLOT(slotMore()) );
00071   connect( mBtnFewer, SIGNAL(clicked()),
00072        this, SLOT(slotFewer()) );
00073   connect( mBtnClear, SIGNAL(clicked()),
00074        this, SLOT(slotClear()) );
00075 
00076   enableControls();
00077 }
00078 
00079 KWidgetLister::~KWidgetLister()
00080 {
00081 }
00082 
00083 void KWidgetLister::slotMore()
00084 {
00085   // the class should make certain that slotMore can't
00086   // be called when mMaxWidgets are on screen.
00087   assert( (int)mWidgetList.count() < mMaxWidgets );
00088 
00089   addWidgetAtEnd();
00090   //  adjustSize();
00091   enableControls();
00092 }
00093 
00094 void KWidgetLister::slotFewer()
00095 {
00096   // the class should make certain that slotFewer can't
00097   // be called when mMinWidgets are on screen.
00098   assert( (int)mWidgetList.count() > mMinWidgets );
00099 
00100   removeLastWidget();
00101   //  adjustSize();
00102   enableControls();
00103 }
00104 
00105 void KWidgetLister::slotClear()
00106 {
00107   setNumberOfShownWidgetsTo( mMinWidgets );
00108 
00109   // clear remaining widgets
00110   QPtrListIterator<QWidget> it( mWidgetList );
00111   for ( it.toFirst() ; it.current() ; ++it )
00112     clearWidget( (*it) );
00113 
00114   //  adjustSize();
00115   enableControls();
00116   emit clearWidgets();
00117 }
00118 
00119 void KWidgetLister::addWidgetAtEnd(QWidget *w)
00120 {
00121   if (!w) w = this->createWidget(this);
00122 
00123   mLayout->insertWidget( mLayout->findWidget( mButtonBox ), w );
00124   mWidgetList.append( w );
00125   w->show();
00126   enableControls();
00127   emit widgetAdded();
00128   emit widgetAdded(w);
00129 }
00130 
00131 void KWidgetLister::removeLastWidget()
00132 {
00133   // The layout will take care that the
00134   // widget is removed from screen, too.
00135   mWidgetList.removeLast();
00136   enableControls();
00137   emit widgetRemoved();
00138 }
00139 
00140 void KWidgetLister::clearWidget( QWidget* /*aWidget*/ )
00141 {
00142 }
00143 
00144 QWidget* KWidgetLister::createWidget( QWidget* parent )
00145 {
00146   return new QWidget( parent );
00147 }
00148 
00149 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum )
00150 {
00151   int superfluousWidgets = QMAX( (int)mWidgetList.count() - aNum, 0 );
00152   int missingWidgets     = QMAX( aNum - (int)mWidgetList.count(), 0 );
00153 
00154   // remove superfluous widgets
00155   for ( ; superfluousWidgets ; superfluousWidgets-- )
00156     removeLastWidget();
00157 
00158   // add missing widgets
00159   for ( ; missingWidgets ; missingWidgets-- )
00160     addWidgetAtEnd();
00161 }
00162 
00163 void KWidgetLister::enableControls()
00164 {
00165   int count = mWidgetList.count();
00166   bool isMaxWidgets = ( count >= mMaxWidgets );
00167   bool isMinWidgets = ( count <= mMinWidgets );
00168 
00169   mBtnMore->setEnabled( !isMaxWidgets );
00170   mBtnFewer->setEnabled( !isMinWidgets );
00171 }
00172 
00173 #include "kwidgetlister.moc"
KDE Logo
This file is part of the documentation for libkdenetwork Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:16:59 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003