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 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035
00036 #include "simplestringlisteditor.h"
00037
00038 #include <kinputdialog.h>
00039 #include <kiconloader.h>
00040 #include <klocale.h>
00041 #include <kdebug.h>
00042 #include <kpushbutton.h>
00043
00044 #include <qlayout.h>
00045
00046
00047
00048
00049
00050
00051
00052 static inline QListBoxItem * findSelectedItem( QListBox * lb ) {
00053 QListBoxItem * item = 0;
00054 for ( item = lb->firstItem() ; item && !item->isSelected() ;
00055 item = item->next() ) ;
00056 return item;
00057 }
00058
00059 SimpleStringListEditor::SimpleStringListEditor( QWidget * parent,
00060 const char * name,
00061 ButtonCode buttons,
00062 const QString & addLabel,
00063 const QString & removeLabel,
00064 const QString & modifyLabel,
00065 const QString & addDialogLabel )
00066 : QWidget( parent, name ),
00067 mAddButton(0), mRemoveButton(0), mModifyButton(0),
00068 mUpButton(0), mDownButton(0),
00069 mAddDialogLabel( addDialogLabel.isEmpty() ?
00070 i18n("New entry:") : addDialogLabel )
00071 {
00072 QHBoxLayout * hlay = new QHBoxLayout( this, 0, KDialog::spacingHint() );
00073
00074 mListBox = new QListBox( this );
00075 hlay->addWidget( mListBox, 1 );
00076
00077 if ( buttons == None )
00078 kdDebug(5006) << "SimpleStringListBox called with no buttons. "
00079 "Consider using a plain QListBox instead!" << endl;
00080
00081 QVBoxLayout * vlay = new QVBoxLayout( hlay );
00082
00083 if ( buttons & Add ) {
00084 if ( addLabel.isEmpty() )
00085 mAddButton = new QPushButton( i18n("&Add..."), this );
00086 else
00087 mAddButton = new QPushButton( addLabel, this );
00088 mAddButton->setAutoDefault( false );
00089 vlay->addWidget( mAddButton );
00090 connect( mAddButton, SIGNAL(clicked()),
00091 this, SLOT(slotAdd()) );
00092 }
00093
00094 if ( buttons & Remove ) {
00095 if ( removeLabel.isEmpty() )
00096 mRemoveButton = new QPushButton( i18n("&Remove"), this );
00097 else
00098 mRemoveButton = new QPushButton( removeLabel, this );
00099 mRemoveButton->setAutoDefault( false );
00100 mRemoveButton->setEnabled( false );
00101 vlay->addWidget( mRemoveButton );
00102 connect( mRemoveButton, SIGNAL(clicked()),
00103 this, SLOT(slotRemove()) );
00104 }
00105
00106 if ( buttons & Modify ) {
00107 if ( modifyLabel.isEmpty() )
00108 mModifyButton = new QPushButton( i18n("&Modify..."), this );
00109 else
00110 mModifyButton = new QPushButton( modifyLabel, this );
00111 mModifyButton->setAutoDefault( false );
00112 mModifyButton->setEnabled( false );
00113 vlay->addWidget( mModifyButton );
00114 connect( mModifyButton, SIGNAL(clicked()),
00115 this, SLOT(slotModify()) );
00116 connect( mListBox, SIGNAL( doubleClicked( QListBoxItem* ) ),
00117 this, SLOT( slotModify() ) );
00118 }
00119
00120 if ( buttons & Up ) {
00121 if ( !(buttons & Down) )
00122 kdDebug(5006) << "Are you sure you want to use an Up button "
00123 "without a Down button??" << endl;
00124 mUpButton = new KPushButton( QString::null, this );
00125 mUpButton->setIconSet( BarIconSet( "up", KIcon::SizeSmall ) );
00126 mUpButton->setAutoDefault( false );
00127 mUpButton->setEnabled( false );
00128 vlay->addWidget( mUpButton );
00129 connect( mUpButton, SIGNAL(clicked()),
00130 this, SLOT(slotUp()) );
00131 }
00132
00133 if ( buttons & Down ) {
00134 if ( !(buttons & Up) )
00135 kdDebug(5006) << "Are you sure you want to use a Down button "
00136 "without an Up button??" << endl;
00137 mDownButton = new KPushButton( QString::null, this );
00138 mDownButton->setIconSet( BarIconSet( "down", KIcon::SizeSmall ) );
00139 mDownButton->setAutoDefault( false );
00140 mDownButton->setEnabled( false );
00141 vlay->addWidget( mDownButton );
00142 connect( mDownButton, SIGNAL(clicked()),
00143 this, SLOT(slotDown()) );
00144 }
00145
00146 vlay->addStretch( 1 );
00147
00148 connect( mListBox, SIGNAL(selectionChanged()),
00149 this, SLOT(slotSelectionChanged()) );
00150 }
00151
00152 void SimpleStringListEditor::setStringList( const QStringList & strings ) {
00153 mListBox->clear();
00154 mListBox->insertStringList( strings );
00155 }
00156
00157 void SimpleStringListEditor::appendStringList( const QStringList & strings ) {
00158 mListBox->insertStringList( strings );
00159 }
00160
00161 QStringList SimpleStringListEditor::stringList() const {
00162 QStringList result;
00163 for ( QListBoxItem * item = mListBox->firstItem() ;
00164 item ; item = item->next() )
00165 result << item->text();
00166 return result;
00167 }
00168
00169 void SimpleStringListEditor::setButtonText( ButtonCode button,
00170 const QString & text ) {
00171 switch ( button ) {
00172 case Add:
00173 if ( !mAddButton ) break;
00174 mAddButton->setText( text );
00175 return;
00176 case Remove:
00177 if ( !mRemoveButton ) break;
00178 mRemoveButton->setText( text );
00179 return;
00180 case Modify:
00181 if ( !mModifyButton ) break;
00182 mModifyButton->setText( text );
00183 return;
00184 case Up:
00185 case Down:
00186 kdDebug(5006) << "SimpleStringListEditor: Cannot change text of "
00187 "Up and Down buttons: they don't contains text!" << endl;
00188 return;
00189 default:
00190 if ( button & All )
00191 kdDebug(5006) << "SimpleStringListEditor::setButtonText: No such button!"
00192 << endl;
00193 else
00194 kdDebug(5006) << "SimpleStringListEditor::setButtonText: Can only set "
00195 "text for one button at a time!" << endl;
00196 return;
00197 }
00198
00199 kdDebug(5006) << "SimpleStringListEditor::setButtonText: the requested "
00200 "button has not been created!" << endl;
00201 }
00202
00203 void SimpleStringListEditor::slotAdd() {
00204 bool ok = false;
00205 QString newEntry = KInputDialog::getText( i18n("New Value"),
00206 mAddDialogLabel, QString::null,
00207 &ok, this );
00208
00209 emit aboutToAdd( newEntry );
00210 if ( ok && !newEntry.isEmpty() )
00211 mListBox->insertItem( newEntry );
00212 emit changed();
00213 }
00214
00215 void SimpleStringListEditor::slotRemove() {
00216 delete findSelectedItem( mListBox );
00217 emit changed();
00218 }
00219
00220 void SimpleStringListEditor::slotModify() {
00221 QListBoxItem * item = findSelectedItem( mListBox );
00222 if ( !item ) return;
00223
00224 bool ok = false;
00225 QString newText = KInputDialog::getText( i18n("Change Value"),
00226 mAddDialogLabel, item->text(),
00227 &ok, this );
00228 emit aboutToAdd( newText );
00229 if ( !ok || newText.isEmpty() || newText == item->text() ) return;
00230
00231 int index = mListBox->index( item );
00232 delete item;
00233 mListBox->insertItem( newText, index );
00234 mListBox->setCurrentItem( index );
00235 emit changed();
00236 }
00237
00238 void SimpleStringListEditor::slotUp() {
00239 QListBoxItem * item = findSelectedItem( mListBox );
00240 if ( !item || !item->prev() ) return;
00241
00242
00243 QListBoxItem * pprev = item->prev()->prev();
00244
00245 mListBox->takeItem( item );
00246
00247 mListBox->insertItem( item, pprev );
00248
00249 mListBox->setCurrentItem( item );
00250
00251 if ( mRemoveButton )
00252 mRemoveButton->setEnabled( true );
00253 if ( mModifyButton )
00254 mModifyButton->setEnabled( true );
00255 if ( mUpButton )
00256 mUpButton->setEnabled( item->prev() );
00257 if ( mDownButton )
00258 mDownButton->setEnabled( true );
00259 emit changed();
00260 }
00261
00262 void SimpleStringListEditor::slotDown() {
00263 QListBoxItem * item = findSelectedItem( mListBox );
00264 if ( !item || !item->next() ) return;
00265
00266
00267 QListBoxItem * next = item->next();
00268
00269 mListBox->takeItem( item );
00270
00271 if ( next )
00272 mListBox->insertItem( item, next );
00273 else
00274 mListBox->insertItem( item );
00275
00276 mListBox->setCurrentItem( item );
00277
00278 if ( mRemoveButton )
00279 mRemoveButton->setEnabled( true );
00280 if ( mModifyButton )
00281 mModifyButton->setEnabled( true );
00282 if ( mUpButton )
00283 mUpButton->setEnabled( true );
00284 if ( mDownButton )
00285 mDownButton->setEnabled( item->next() );
00286 emit changed();
00287 }
00288
00289 void SimpleStringListEditor::slotSelectionChanged() {
00290
00291 QListBoxItem * item = findSelectedItem( mListBox );
00292
00293
00294
00295 if ( mRemoveButton )
00296 mRemoveButton->setEnabled( item );
00297 if ( mModifyButton )
00298 mModifyButton->setEnabled( item );
00299 if ( mUpButton )
00300 mUpButton->setEnabled( item && item->prev() );
00301 if ( mDownButton )
00302 mDownButton->setEnabled( item && item->next() );
00303 }
00304
00305
00306
00307 #include "simplestringlisteditor.moc"