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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 TQt library by Trolltech AS, Norway (or with modified versions 00023 of TQt that use the same license as TQt), 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 TQt. 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 <tqpushbutton.h> 00038 #include <tqlayout.h> 00039 #include <tqhbox.h> 00040 00041 #include <assert.h> 00042 #include <kguiitem.h> 00043 #include <kpushbutton.h> 00044 #include <kdialog.h> 00045 00046 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, TQWidget *parent, const char* name ) 00047 : TQWidget( parent, name ) 00048 { 00049 mWidgetList.setAutoDelete(TRUE); 00050 00051 mMinWidgets = TQMAX( minWidgets, 1 ); 00052 mMaxWidgets = TQMAX( maxWidgets, mMinWidgets + 1 ); 00053 00054 //--------- the button box 00055 mLayout = new TQVBoxLayout(this, 0, 4); 00056 mButtonBox = new TQHBox(this); 00057 mButtonBox->setSpacing( KDialog::spacingHint() ); 00058 mLayout->addWidget( mButtonBox ); 00059 00060 mBtnMore = new KPushButton( KGuiItem( i18n( "more widgets", "More" ), "button_more" ), mButtonBox ); 00061 mButtonBox->setStretchFactor( mBtnMore, 0 ); 00062 00063 mBtnFewer = new KPushButton( KGuiItem( i18n( "fewer widgets", "Fewer" ), "button_fewer" ), mButtonBox ); 00064 mButtonBox->setStretchFactor( mBtnFewer, 0 ); 00065 00066 TQWidget *spacer = new TQWidget( mButtonBox ); 00067 mButtonBox->setStretchFactor( spacer, 1 ); 00068 00069 // FIXME: We need a KStdGuiItem::clear here and in other locations to be automagically RTL aware - Martijn 00070 mBtnClear = new KPushButton( KGuiItem( i18n( "clear widgets", "Clear" ), "locationbar_erase" ), mButtonBox ); 00071 mButtonBox->setStretchFactor( mBtnClear, 0 ); 00072 00073 //---------- connect everything 00074 connect( mBtnMore, TQT_SIGNAL(clicked()), 00075 this, TQT_SLOT(slotMore()) ); 00076 connect( mBtnFewer, TQT_SIGNAL(clicked()), 00077 this, TQT_SLOT(slotFewer()) ); 00078 connect( mBtnClear, TQT_SIGNAL(clicked()), 00079 this, TQT_SLOT(slotClear()) ); 00080 00081 enableControls(); 00082 } 00083 00084 KWidgetLister::~KWidgetLister() 00085 { 00086 } 00087 00088 void KWidgetLister::slotMore() 00089 { 00090 // the class should make certain that slotMore can't 00091 // be called when mMaxWidgets are on screen. 00092 assert( (int)mWidgetList.count() < mMaxWidgets ); 00093 00094 addWidgetAtEnd(); 00095 // adjustSize(); 00096 enableControls(); 00097 } 00098 00099 void KWidgetLister::slotFewer() 00100 { 00101 // the class should make certain that slotFewer can't 00102 // be called when mMinWidgets are on screen. 00103 assert( (int)mWidgetList.count() > mMinWidgets ); 00104 00105 removeLastWidget(); 00106 // adjustSize(); 00107 enableControls(); 00108 } 00109 00110 void KWidgetLister::slotClear() 00111 { 00112 setNumberOfShownWidgetsTo( mMinWidgets ); 00113 00114 // clear remaining widgets 00115 TQPtrListIterator<TQWidget> it( mWidgetList ); 00116 for ( it.toFirst() ; it.current() ; ++it ) 00117 clearWidget( (*it) ); 00118 00119 // adjustSize(); 00120 enableControls(); 00121 emit clearWidgets(); 00122 } 00123 00124 void KWidgetLister::addWidgetAtEnd(TQWidget *w) 00125 { 00126 if (!w) w = this->createWidget(this); 00127 00128 mLayout->insertWidget( mLayout->findWidget( mButtonBox ), w ); 00129 mWidgetList.append( w ); 00130 w->show(); 00131 enableControls(); 00132 emit widgetAdded(); 00133 emit widgetAdded(w); 00134 } 00135 00136 void KWidgetLister::removeLastWidget() 00137 { 00138 // The layout will take care that the 00139 // widget is removed from screen, too. 00140 mWidgetList.removeLast(); 00141 enableControls(); 00142 emit widgetRemoved(); 00143 } 00144 00145 void KWidgetLister::clearWidget( TQWidget* /*aWidget*/ ) 00146 { 00147 } 00148 00149 TQWidget* KWidgetLister::createWidget( TQWidget* parent ) 00150 { 00151 return new TQWidget( parent ); 00152 } 00153 00154 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum ) 00155 { 00156 int superfluousWidgets = TQMAX( (int)mWidgetList.count() - aNum, 0 ); 00157 int missingWidgets = TQMAX( aNum - (int)mWidgetList.count(), 0 ); 00158 00159 // remove superfluous widgets 00160 for ( ; superfluousWidgets ; superfluousWidgets-- ) 00161 removeLastWidget(); 00162 00163 // add missing widgets 00164 for ( ; missingWidgets ; missingWidgets-- ) 00165 addWidgetAtEnd(); 00166 } 00167 00168 void KWidgetLister::enableControls() 00169 { 00170 int count = mWidgetList.count(); 00171 bool isMaxWidgets = ( count >= mMaxWidgets ); 00172 bool isMinWidgets = ( count <= mMinWidgets ); 00173 00174 mBtnMore->setEnabled( !isMaxWidgets ); 00175 mBtnFewer->setEnabled( !isMinWidgets ); 00176 } 00177 00178 #include "kwidgetlister.moc"