kmail

simplestringlisteditor.cpp
1 /*
2  simplestringlisteditor.cpp
3 
4  This file is part of KMail, the KDE mail client.
5  Copyright (c) 2001 Marc Mutz <mutz@kde.org>
6 
7  KMail is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License, version 2, as
9  published by the Free Software Foundation.
10 
11  KMail is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this program with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include "simplestringlisteditor.h"
37 
38 #include <kinputdialog.h>
39 #include <kiconloader.h>
40 #include <tdelocale.h>
41 #include <kdebug.h>
42 #include <kpushbutton.h>
43 
44 #include <tqlayout.h>
45 
46 
47 //********************************************************
48 // SimpleStringListEditor
49 //********************************************************
50 
51 // small helper function:
52 static inline TQListBoxItem * findSelectedItem( TQListBox * lb ) {
53  TQListBoxItem * item = 0;
54  for ( item = lb->firstItem() ; item && !item->isSelected() ;
55  item = item->next() ) ;
56  return item;
57 }
58 
59 SimpleStringListEditor::SimpleStringListEditor( TQWidget * parent,
60  const char * name,
61  ButtonCode buttons,
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 )
71 {
72  TQHBoxLayout * hlay = new TQHBoxLayout( this, 0, KDialog::spacingHint() );
73 
74  mListBox = new TQListBox( this );
75  hlay->addWidget( mListBox, 1 );
76 
77  if ( buttons == None )
78  kdDebug(5006) << "SimpleStringListBox called with no buttons. "
79  "Consider using a plain TQListBox instead!" << endl;
80 
81  TQVBoxLayout * vlay = new TQVBoxLayout( hlay ); // inherits spacing
82 
83  if ( buttons & Add ) {
84  if ( addLabel.isEmpty() )
85  mAddButton = new TQPushButton( i18n("&Add..."), this );
86  else
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()) );
92  }
93 
94  if ( buttons & Remove ) {
95  if ( removeLabel.isEmpty() )
96  mRemoveButton = new TQPushButton( i18n("&Remove"), this );
97  else
98  mRemoveButton = new TQPushButton( removeLabel, this );
99  mRemoveButton->setAutoDefault( false );
100  mRemoveButton->setEnabled( false ); // no selection yet
101  vlay->addWidget( mRemoveButton );
102  connect( mRemoveButton, TQ_SIGNAL(clicked()),
103  this, TQ_SLOT(slotRemove()) );
104  }
105 
106  if ( buttons & Modify ) {
107  if ( modifyLabel.isEmpty() )
108  mModifyButton = new TQPushButton( i18n("&Modify..."), this );
109  else
110  mModifyButton = new TQPushButton( modifyLabel, this );
111  mModifyButton->setAutoDefault( false );
112  mModifyButton->setEnabled( false ); // no selection yet
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() ) );
118  }
119 
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 ); // no selection yet
128  vlay->addWidget( mUpButton );
129  connect( mUpButton, TQ_SIGNAL(clicked()),
130  this, TQ_SLOT(slotUp()) );
131  }
132 
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 ); // no selection yet
141  vlay->addWidget( mDownButton );
142  connect( mDownButton, TQ_SIGNAL(clicked()),
143  this, TQ_SLOT(slotDown()) );
144  }
145 
146  vlay->addStretch( 1 ); // spacer
147 
148  connect( mListBox, TQ_SIGNAL(selectionChanged()),
149  this, TQ_SLOT(slotSelectionChanged()) );
150 }
151 
152 void SimpleStringListEditor::setStringList( const TQStringList & strings ) {
153  mListBox->clear();
154  mListBox->insertStringList( strings );
155 }
156 
157 void SimpleStringListEditor::appendStringList( const TQStringList & strings ) {
158  mListBox->insertStringList( strings );
159 }
160 
161 TQStringList SimpleStringListEditor::stringList() const {
162  TQStringList result;
163  for ( TQListBoxItem * item = mListBox->firstItem() ;
164  item ; item = item->next() )
165  result << item->text();
166  return result;
167 }
168 
169 bool SimpleStringListEditor::containsString( const TQString & str ) {
170  for ( TQListBoxItem * item = mListBox->firstItem() ;
171  item ; item = item->next() ) {
172  if ( item->text() == str )
173  return true;
174  }
175  return false;
176 }
177 
178 void SimpleStringListEditor::setButtonText( ButtonCode button,
179  const TQString & text ) {
180  switch ( button ) {
181  case Add:
182  if ( !mAddButton ) break;
183  mAddButton->setText( text );
184  return;
185  case Remove:
186  if ( !mRemoveButton ) break;
187  mRemoveButton->setText( text );
188  return;
189  case Modify:
190  if ( !mModifyButton ) break;
191  mModifyButton->setText( text );
192  return;
193  case Up:
194  case Down:
195  kdDebug(5006) << "SimpleStringListEditor: Cannot change text of "
196  "Up and Down buttons: they don't contains text!" << endl;
197  return;
198  default:
199  if ( button & All )
200  kdDebug(5006) << "SimpleStringListEditor::setButtonText: No such button!"
201  << endl;
202  else
203  kdDebug(5006) << "SimpleStringListEditor::setButtonText: Can only set "
204  "text for one button at a time!" << endl;
205  return;
206  }
207 
208  kdDebug(5006) << "SimpleStringListEditor::setButtonText: the requested "
209  "button has not been created!" << endl;
210 }
211 
212 void SimpleStringListEditor::slotAdd() {
213  bool ok = false;
214  TQString newEntry = KInputDialog::getText( i18n("New Value"),
215  mAddDialogLabel, TQString(),
216  &ok, this );
217  // let the user verify the string before adding
218  emit aboutToAdd( newEntry );
219  if ( ok && !newEntry.isEmpty() && !containsString( newEntry )) {
220  mListBox->insertItem( newEntry );
221  emit changed();
222  }
223 }
224 
225 void SimpleStringListEditor::slotRemove() {
226  delete findSelectedItem( mListBox ); // delete 0 is well-behaved...
227  emit changed();
228 }
229 
230 void SimpleStringListEditor::slotModify() {
231  TQListBoxItem * item = findSelectedItem( mListBox );
232  if ( !item ) return;
233 
234  bool ok = false;
235  TQString newText = KInputDialog::getText( i18n("Change Value"),
236  mAddDialogLabel, item->text(),
237  &ok, this );
238  emit aboutToAdd( newText );
239  if ( !ok || newText.isEmpty() || newText == item->text() ) return;
240 
241  int index = mListBox->index( item );
242  delete item;
243  mListBox->insertItem( newText, index );
244  mListBox->setCurrentItem( index );
245  emit changed();
246 }
247 
248 void SimpleStringListEditor::slotUp() {
249  TQListBoxItem * item = findSelectedItem( mListBox );
250  if ( !item || !item->prev() ) return;
251 
252  // find the item that we want to insert after:
253  TQListBoxItem * pprev = item->prev()->prev();
254  // take the item from it's current position...
255  mListBox->takeItem( item );
256  // ...and insert it after the above mentioned item:
257  mListBox->insertItem( item, pprev );
258  // make sure the old item is still the current one:
259  mListBox->setCurrentItem( item );
260  // enable and disable controls:
261  if ( mRemoveButton )
262  mRemoveButton->setEnabled( true );
263  if ( mModifyButton )
264  mModifyButton->setEnabled( true );
265  if ( mUpButton )
266  mUpButton->setEnabled( item->prev() );
267  if ( mDownButton )
268  mDownButton->setEnabled( true );
269  emit changed();
270 }
271 
272 void SimpleStringListEditor::slotDown() {
273  TQListBoxItem * item = findSelectedItem( mListBox );
274  if ( !item || !item->next() ) return;
275 
276  // find the item that we want to insert after:
277  TQListBoxItem * next = item->next();
278  // take the item from it's current position...
279  mListBox->takeItem( item );
280  // ...and insert it after the above mentioned item:
281  if ( next )
282  mListBox->insertItem( item, next );
283  else
284  mListBox->insertItem( item );
285  // make sure the old item is still the current one:
286  mListBox->setCurrentItem( item );
287  // enable and disable controls:
288  if ( mRemoveButton )
289  mRemoveButton->setEnabled( true );
290  if ( mModifyButton )
291  mModifyButton->setEnabled( true );
292  if ( mUpButton )
293  mUpButton->setEnabled( true );
294  if ( mDownButton )
295  mDownButton->setEnabled( item->next() );
296  emit changed();
297 }
298 
299 void SimpleStringListEditor::slotSelectionChanged() {
300  // try to find a selected item:
301  TQListBoxItem * item = findSelectedItem( mListBox );
302 
303  // if there is one, item will be non-null (ie. true), else 0
304  // (ie. false):
305  if ( mRemoveButton )
306  mRemoveButton->setEnabled( item );
307  if ( mModifyButton )
308  mModifyButton->setEnabled( item );
309  if ( mUpButton )
310  mUpButton->setEnabled( item && item->prev() );
311  if ( mDownButton )
312  mDownButton->setEnabled( item && item->next() );
313 }
314 
315 
316 
317 #include "simplestringlisteditor.moc"