libkdepim
kwidgetlister.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 #include <kguiitem.h>
00043 #include <kpushbutton.h>
00044 #include <kdialog.h>
00045
00046 KWidgetLister::KWidgetLister( int minWidgets, int maxWidgets, QWidget *parent, const char* name )
00047 : QWidget( parent, name )
00048 {
00049 mWidgetList.setAutoDelete(TRUE);
00050
00051 mMinWidgets = QMAX( minWidgets, 1 );
00052 mMaxWidgets = QMAX( maxWidgets, mMinWidgets + 1 );
00053
00054
00055 mLayout = new QVBoxLayout(this, 0, 4);
00056 mButtonBox = new QHBox(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 QWidget *spacer = new QWidget( mButtonBox );
00067 mButtonBox->setStretchFactor( spacer, 1 );
00068
00069
00070 mBtnClear = new KPushButton( KGuiItem( i18n( "clear widgets", "Clear" ), "locationbar_erase" ), mButtonBox );
00071 mButtonBox->setStretchFactor( mBtnClear, 0 );
00072
00073
00074 connect( mBtnMore, SIGNAL(clicked()),
00075 this, SLOT(slotMore()) );
00076 connect( mBtnFewer, SIGNAL(clicked()),
00077 this, SLOT(slotFewer()) );
00078 connect( mBtnClear, SIGNAL(clicked()),
00079 this, SLOT(slotClear()) );
00080
00081 enableControls();
00082 }
00083
00084 KWidgetLister::~KWidgetLister()
00085 {
00086 }
00087
00088 void KWidgetLister::slotMore()
00089 {
00090
00091
00092 assert( (int)mWidgetList.count() < mMaxWidgets );
00093
00094 addWidgetAtEnd();
00095
00096 enableControls();
00097 }
00098
00099 void KWidgetLister::slotFewer()
00100 {
00101
00102
00103 assert( (int)mWidgetList.count() > mMinWidgets );
00104
00105 removeLastWidget();
00106
00107 enableControls();
00108 }
00109
00110 void KWidgetLister::slotClear()
00111 {
00112 setNumberOfShownWidgetsTo( mMinWidgets );
00113
00114
00115 QPtrListIterator<QWidget> it( mWidgetList );
00116 for ( it.toFirst() ; it.current() ; ++it )
00117 clearWidget( (*it) );
00118
00119
00120 enableControls();
00121 emit clearWidgets();
00122 }
00123
00124 void KWidgetLister::addWidgetAtEnd(QWidget *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
00139
00140 mWidgetList.removeLast();
00141 enableControls();
00142 emit widgetRemoved();
00143 }
00144
00145 void KWidgetLister::clearWidget( QWidget* )
00146 {
00147 }
00148
00149 QWidget* KWidgetLister::createWidget( QWidget* parent )
00150 {
00151 return new QWidget( parent );
00152 }
00153
00154 void KWidgetLister::setNumberOfShownWidgetsTo( int aNum )
00155 {
00156 int superfluousWidgets = QMAX( (int)mWidgetList.count() - aNum, 0 );
00157 int missingWidgets = QMAX( aNum - (int)mWidgetList.count(), 0 );
00158
00159
00160 for ( ; superfluousWidgets ; superfluousWidgets-- )
00161 removeLastWidget();
00162
00163
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"
|