36 #include "simplestringlisteditor.h"
38 #include <kinputdialog.h>
39 #include <kiconloader.h>
40 #include <tdelocale.h>
42 #include <kpushbutton.h>
52 static inline TQListBoxItem * findSelectedItem( TQListBox * lb ) {
53 TQListBoxItem * item = 0;
54 for ( item = lb->firstItem() ; item && !item->isSelected() ;
55 item = item->next() ) ;
59 SimpleStringListEditor::SimpleStringListEditor( TQWidget * parent,
62 const TQString & addLabel,
63 const TQString & removeLabel,
64 const TQString & modifyLabel,
65 const TQString & addDialogLabel )
66 : TQWidget( parent, name ),
67 mAddButton(0), mRemoveButton(0), mModifyButton(0),
68 mUpButton(0), mDownButton(0),
69 mAddDialogLabel( addDialogLabel.isEmpty() ?
70 i18n(
"New entry:") : addDialogLabel )
72 TQHBoxLayout * hlay =
new TQHBoxLayout(
this, 0, KDialog::spacingHint() );
74 mListBox =
new TQListBox(
this );
75 hlay->addWidget( mListBox, 1 );
77 if ( buttons == None )
78 kdDebug(5006) <<
"SimpleStringListBox called with no buttons. "
79 "Consider using a plain TQListBox instead!" << endl;
81 TQVBoxLayout * vlay =
new TQVBoxLayout( hlay );
83 if ( buttons & Add ) {
84 if ( addLabel.isEmpty() )
85 mAddButton =
new TQPushButton( i18n(
"&Add..."),
this );
87 mAddButton =
new TQPushButton( addLabel,
this );
88 mAddButton->setAutoDefault(
false );
89 vlay->addWidget( mAddButton );
90 connect( mAddButton, TQ_SIGNAL(clicked()),
91 this, TQ_SLOT(slotAdd()) );
94 if ( buttons & Remove ) {
95 if ( removeLabel.isEmpty() )
96 mRemoveButton =
new TQPushButton( i18n(
"&Remove"),
this );
98 mRemoveButton =
new TQPushButton( removeLabel,
this );
99 mRemoveButton->setAutoDefault(
false );
100 mRemoveButton->setEnabled(
false );
101 vlay->addWidget( mRemoveButton );
102 connect( mRemoveButton, TQ_SIGNAL(clicked()),
103 this, TQ_SLOT(slotRemove()) );
106 if ( buttons & Modify ) {
107 if ( modifyLabel.isEmpty() )
108 mModifyButton =
new TQPushButton( i18n(
"&Modify..."),
this );
110 mModifyButton =
new TQPushButton( modifyLabel,
this );
111 mModifyButton->setAutoDefault(
false );
112 mModifyButton->setEnabled(
false );
113 vlay->addWidget( mModifyButton );
114 connect( mModifyButton, TQ_SIGNAL(clicked()),
115 this, TQ_SLOT(slotModify()) );
116 connect( mListBox, TQ_SIGNAL( doubleClicked( TQListBoxItem* ) ),
117 this, TQ_SLOT( slotModify() ) );
120 if ( buttons & Up ) {
121 if ( !(buttons & Down) )
122 kdDebug(5006) <<
"Are you sure you want to use an Up button "
123 "without a Down button??" << endl;
124 mUpButton =
new KPushButton( TQString(),
this );
125 mUpButton->setIconSet( BarIconSet(
"go-up", TDEIcon::SizeSmall ) );
126 mUpButton->setAutoDefault(
false );
127 mUpButton->setEnabled(
false );
128 vlay->addWidget( mUpButton );
129 connect( mUpButton, TQ_SIGNAL(clicked()),
130 this, TQ_SLOT(slotUp()) );
133 if ( buttons & Down ) {
134 if ( !(buttons & Up) )
135 kdDebug(5006) <<
"Are you sure you want to use a Down button "
136 "without an Up button??" << endl;
137 mDownButton =
new KPushButton( TQString(),
this );
138 mDownButton->setIconSet( BarIconSet(
"go-down", TDEIcon::SizeSmall ) );
139 mDownButton->setAutoDefault(
false );
140 mDownButton->setEnabled(
false );
141 vlay->addWidget( mDownButton );
142 connect( mDownButton, TQ_SIGNAL(clicked()),
143 this, TQ_SLOT(slotDown()) );
146 vlay->addStretch( 1 );
148 connect( mListBox, TQ_SIGNAL(selectionChanged()),
149 this, TQ_SLOT(slotSelectionChanged()) );
152 void SimpleStringListEditor::setStringList(
const TQStringList & strings ) {
154 mListBox->insertStringList( strings );
157 void SimpleStringListEditor::appendStringList(
const TQStringList & strings ) {
158 mListBox->insertStringList( strings );
161 TQStringList SimpleStringListEditor::stringList()
const {
163 for ( TQListBoxItem * item = mListBox->firstItem() ;
164 item ; item = item->next() )
165 result << item->text();
169 bool SimpleStringListEditor::containsString(
const TQString & str ) {
170 for ( TQListBoxItem * item = mListBox->firstItem() ;
171 item ; item = item->next() ) {
172 if ( item->text() == str )
178 void SimpleStringListEditor::setButtonText( ButtonCode button,
179 const TQString & text ) {
182 if ( !mAddButton )
break;
183 mAddButton->setText( text );
186 if ( !mRemoveButton )
break;
187 mRemoveButton->setText( text );
190 if ( !mModifyButton )
break;
191 mModifyButton->setText( text );
195 kdDebug(5006) <<
"SimpleStringListEditor: Cannot change text of "
196 "Up and Down buttons: they don't contains text!" << endl;
200 kdDebug(5006) <<
"SimpleStringListEditor::setButtonText: No such button!"
203 kdDebug(5006) <<
"SimpleStringListEditor::setButtonText: Can only set "
204 "text for one button at a time!" << endl;
208 kdDebug(5006) <<
"SimpleStringListEditor::setButtonText: the requested "
209 "button has not been created!" << endl;
212 void SimpleStringListEditor::slotAdd() {
214 TQString newEntry = KInputDialog::getText( i18n(
"New Value"),
215 mAddDialogLabel, TQString(),
218 emit aboutToAdd( newEntry );
219 if ( ok && !newEntry.isEmpty() && !containsString( newEntry )) {
220 mListBox->insertItem( newEntry );
225 void SimpleStringListEditor::slotRemove() {
226 delete findSelectedItem( mListBox );
230 void SimpleStringListEditor::slotModify() {
231 TQListBoxItem * item = findSelectedItem( mListBox );
235 TQString newText = KInputDialog::getText( i18n(
"Change Value"),
236 mAddDialogLabel, item->text(),
238 emit aboutToAdd( newText );
239 if ( !ok || newText.isEmpty() || newText == item->text() )
return;
241 int index = mListBox->index( item );
243 mListBox->insertItem( newText, index );
244 mListBox->setCurrentItem( index );
248 void SimpleStringListEditor::slotUp() {
249 TQListBoxItem * item = findSelectedItem( mListBox );
250 if ( !item || !item->prev() )
return;
253 TQListBoxItem * pprev = item->prev()->prev();
255 mListBox->takeItem( item );
257 mListBox->insertItem( item, pprev );
259 mListBox->setCurrentItem( item );
262 mRemoveButton->setEnabled(
true );
264 mModifyButton->setEnabled(
true );
266 mUpButton->setEnabled( item->prev() );
268 mDownButton->setEnabled(
true );
272 void SimpleStringListEditor::slotDown() {
273 TQListBoxItem * item = findSelectedItem( mListBox );
274 if ( !item || !item->next() )
return;
277 TQListBoxItem * next = item->next();
279 mListBox->takeItem( item );
282 mListBox->insertItem( item, next );
284 mListBox->insertItem( item );
286 mListBox->setCurrentItem( item );
289 mRemoveButton->setEnabled(
true );
291 mModifyButton->setEnabled(
true );
293 mUpButton->setEnabled(
true );
295 mDownButton->setEnabled( item->next() );
299 void SimpleStringListEditor::slotSelectionChanged() {
301 TQListBoxItem * item = findSelectedItem( mListBox );
306 mRemoveButton->setEnabled( item );
308 mModifyButton->setEnabled( item );
310 mUpButton->setEnabled( item && item->prev() );
312 mDownButton->setEnabled( item && item->next() );
317 #include "simplestringlisteditor.moc"