00001
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
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
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
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
00086
00087 assert( (
int)
mWidgetList.count() <
mMaxWidgets );
00088
00089
addWidgetAtEnd();
00090
00091 enableControls();
00092 }
00093
00094 void KWidgetLister::slotFewer()
00095 {
00096
00097
00098 assert( (
int)
mWidgetList.count() >
mMinWidgets );
00099
00100
removeLastWidget();
00101
00102 enableControls();
00103 }
00104
00105 void KWidgetLister::slotClear()
00106 {
00107
setNumberOfShownWidgetsTo(
mMinWidgets );
00108
00109
00110
QPtrListIterator<QWidget> it(
mWidgetList );
00111
for ( it.toFirst() ; it.current() ; ++it )
00112
clearWidget( (*it) );
00113
00114
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
00134
00135
mWidgetList.removeLast();
00136 enableControls();
00137 emit
widgetRemoved();
00138 }
00139
00140 void KWidgetLister::clearWidget( QWidget* )
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
00155
for ( ; superfluousWidgets ; superfluousWidgets-- )
00156
removeLastWidget();
00157
00158
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"