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