• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
keditlistbox.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 David Faure <faure@kde.org>, Alexander Neundorf <neundorf@kde.org>
3  2000, 2002 Carsten Pfeiffer <pfeiffer@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <tqstringlist.h>
22 #include <tqpushbutton.h>
23 #include <tqlayout.h>
24 #include <tqgroupbox.h>
25 #include <tqlistbox.h>
26 #include <tqwhatsthis.h>
27 #include <tqlabel.h>
28 
29 #include <kcombobox.h>
30 #include <kdebug.h>
31 #include <kdialog.h>
32 #include <klineedit.h>
33 #include <tdelocale.h>
34 #include <tdeapplication.h>
35 #include <knotifyclient.h>
36 
37 #include "keditlistbox.h"
38 
39 #include <assert.h>
40 
41 class KEditListBoxPrivate
42 {
43 public:
44  bool m_checkAtEntering;
45  uint buttons;
46 };
47 
48 KEditListBox::KEditListBox(TQWidget *parent, const char *name,
49  bool checkAtEntering, int buttons )
50  :TQGroupBox(parent, name ), d(new KEditListBoxPrivate)
51 {
52  init( checkAtEntering, buttons );
53 }
54 
55 KEditListBox::KEditListBox(const TQString& title, TQWidget *parent,
56  const char *name, bool checkAtEntering, int buttons)
57  :TQGroupBox(title, parent, name ), d(new KEditListBoxPrivate)
58 {
59  init( checkAtEntering, buttons );
60 }
61 
62 KEditListBox::KEditListBox(const TQString& title, const CustomEditor& custom,
63  TQWidget *parent, const char *name,
64  bool checkAtEntering, int buttons)
65  :TQGroupBox(title, parent, name ), d(new KEditListBoxPrivate)
66 {
67  m_lineEdit = custom.lineEdit();
68  init( checkAtEntering, buttons, custom.representationWidget() );
69 }
70 
71 KEditListBox::~KEditListBox()
72 {
73  delete d;
74 }
75 
76 void KEditListBox::init( bool checkAtEntering, int buttons,
77  TQWidget *representationWidget )
78 {
79  d->m_checkAtEntering = checkAtEntering;
80 
81  servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
82  setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding,
83  TQSizePolicy::MinimumExpanding));
84 
85  TQGridLayout * grid = new TQGridLayout(this, 7, 2,
86  KDialog::marginHint(),
87  KDialog::spacingHint());
88  grid->addRowSpacing(0, fontMetrics().lineSpacing());
89  grid->setRowStretch( 6, 1 );
90 
91  grid->setMargin(15);
92 
93  if ( representationWidget )
94  representationWidget->reparent( this, TQPoint(0,0) );
95  else
96  m_lineEdit=new KLineEdit(this);
97 
98  m_listBox = new TQListBox(this);
99 
100  TQWidget *editingWidget = representationWidget ?
101  representationWidget : m_lineEdit;
102  grid->addMultiCellWidget(editingWidget,1,1,0,1);
103  grid->addMultiCellWidget(m_listBox, 2, 6, 0, 0);
104 
105  d->buttons = 0;
106  setButtons( buttons );
107 
108  connect(m_lineEdit,TQ_SIGNAL(textChanged(const TQString&)),this,TQ_SLOT(typedSomething(const TQString&)));
109  m_lineEdit->setTrapReturnKey(true);
110  connect(m_lineEdit,TQ_SIGNAL(returnPressed()),this,TQ_SLOT(addItem()));
111  connect(m_listBox, TQ_SIGNAL(highlighted(int)), TQ_SLOT(enableMoveButtons(int)));
112 
113  // maybe supplied lineedit has some text already
114  typedSomething( m_lineEdit->text() );
115 }
116 
117 void KEditListBox::setButtons( uint buttons )
118 {
119  if ( d->buttons == buttons )
120  return;
121 
122  TQGridLayout* grid = static_cast<TQGridLayout *>( layout() );
123  if ( ( buttons & Add ) && !servNewButton ) {
124  servNewButton = new TQPushButton(i18n("&Add"), this);
125  servNewButton->setEnabled(false);
126  servNewButton->show();
127  connect(servNewButton, TQ_SIGNAL(clicked()), TQ_SLOT(addItem()));
128 
129  grid->addWidget(servNewButton, 2, 1);
130  } else if ( ( buttons & Add ) == 0 && servNewButton ) {
131  delete servNewButton;
132  servNewButton = 0;
133  }
134 
135  if ( ( buttons & Remove ) && !servRemoveButton ) {
136  servRemoveButton = new TQPushButton(i18n("&Remove"), this);
137  servRemoveButton->setEnabled(false);
138  servRemoveButton->show();
139  connect(servRemoveButton, TQ_SIGNAL(clicked()), TQ_SLOT(removeItem()));
140 
141  grid->addWidget(servRemoveButton, 3, 1);
142  } else if ( ( buttons & Remove ) == 0 && servRemoveButton ) {
143  delete servRemoveButton;
144  servRemoveButton = 0;
145  }
146 
147  if ( ( buttons & UpDown ) && !servUpButton ) {
148  servUpButton = new TQPushButton(i18n("Move &Up"), this);
149  servUpButton->setEnabled(false);
150  servUpButton->show();
151  connect(servUpButton, TQ_SIGNAL(clicked()), TQ_SLOT(moveItemUp()));
152 
153  servDownButton = new TQPushButton(i18n("Move &Down"), this);
154  servDownButton->setEnabled(false);
155  servDownButton->show();
156  connect(servDownButton, TQ_SIGNAL(clicked()), TQ_SLOT(moveItemDown()));
157 
158  grid->addWidget(servUpButton, 4, 1);
159  grid->addWidget(servDownButton, 5, 1);
160  } else if ( ( buttons & UpDown ) == 0 && servUpButton ) {
161  delete servUpButton; servUpButton = 0;
162  delete servDownButton; servDownButton = 0;
163  }
164 
165  d->buttons = buttons;
166 }
167 
168 void KEditListBox::typedSomething(const TQString& text)
169 {
170  if(currentItem() >= 0) {
171  if(currentText() != m_lineEdit->text())
172  {
173  // IMHO changeItem() shouldn't do anything with the value
174  // of currentItem() ... like changing it or emitting signals ...
175  // but TT disagree with me on this one (it's been that way since ages ... grrr)
176  bool block = m_listBox->signalsBlocked();
177  const TQString& oldText = currentText();
178  int item = currentItem();
179  m_listBox->blockSignals( true );
180  m_listBox->changeItem(text, item);
181  m_listBox->blockSignals( block );
182  emit changed();
183  emit renamed(oldText, text);
184  emit renamed(item, oldText, text);
185  }
186  }
187 
188  if ( !servNewButton )
189  return;
190 
191  if (!d->m_checkAtEntering)
192  servNewButton->setEnabled(!text.isEmpty());
193  else
194  {
195  if (text.isEmpty())
196  {
197  servNewButton->setEnabled(false);
198  }
199  else
200  {
201  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
202  bool enable = (!m_listBox->findItem( text, mode ));
203  servNewButton->setEnabled( enable );
204  }
205  }
206 }
207 
208 void KEditListBox::moveItemUp()
209 {
210  if (!m_listBox->isEnabled())
211  {
212  KNotifyClient::beep();
213  return;
214  }
215 
216  const unsigned int selIndex = m_listBox->currentItem();
217  if (selIndex == 0)
218  {
219  KNotifyClient::beep();
220  return;
221  }
222 
223  TQListBoxItem *selItem = m_listBox->item(selIndex);
224  m_listBox->takeItem(selItem);
225  m_listBox->insertItem(selItem, selIndex-1);
226  m_listBox->setCurrentItem(selIndex - 1);
227 
228  emit changed();
229 }
230 
231 void KEditListBox::moveItemDown()
232 {
233  if (!m_listBox->isEnabled())
234  {
235  KNotifyClient::beep();
236  return;
237  }
238 
239  unsigned int selIndex = m_listBox->currentItem();
240  if (selIndex == m_listBox->count() - 1)
241  {
242  KNotifyClient::beep();
243  return;
244  }
245 
246  TQListBoxItem *selItem = m_listBox->item(selIndex);
247  m_listBox->takeItem(selItem);
248  m_listBox->insertItem(selItem, selIndex+1);
249  m_listBox->setCurrentItem(selIndex + 1);
250 
251  emit changed();
252 }
253 
254 void KEditListBox::addItem()
255 {
256  // when m_checkAtEntering is true, the add-button is disabled, but this
257  // slot can still be called through Key_Return/Key_Enter. So we guard
258  // against this.
259  if ( !servNewButton || !servNewButton->isEnabled() )
260  return;
261 
262  const TQString& currentTextLE=m_lineEdit->text();
263  bool alreadyInList(false);
264  //if we didn't check for dupes at the inserting we have to do it now
265  if (!d->m_checkAtEntering)
266  {
267  // first check current item instead of dumb iterating the entire list
268  if ( m_listBox->currentText() == currentTextLE )
269  alreadyInList = true;
270  else
271  {
272  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
273  alreadyInList =(m_listBox->findItem(currentTextLE, mode) );
274  }
275  }
276 
277  if ( servNewButton )
278  servNewButton->setEnabled(false);
279 
280  bool block = m_lineEdit->signalsBlocked();
281  m_lineEdit->blockSignals(true);
282  m_lineEdit->clear();
283  m_lineEdit->blockSignals(block);
284 
285  int item = currentItem();
286  m_listBox->setSelected(item, false);
287 
288  if (!alreadyInList)
289  {
290  block = m_listBox->signalsBlocked();
291  m_listBox->blockSignals( true );
292  m_listBox->insertItem(currentTextLE);
293  m_listBox->blockSignals( block );
294  emit changed();
295  emit added( currentTextLE );
296  emit added( item, currentTextLE );
297  }
298 }
299 
300 int KEditListBox::currentItem() const
301 {
302  int nr = m_listBox->currentItem();
303  if(nr >= 0 && !m_listBox->item(nr)->isSelected()) return -1;
304  return nr;
305 }
306 
307 void KEditListBox::removeItem()
308 {
309  int item = m_listBox->currentItem();
310 
311  if ( item >= 0 )
312  {
313  TQString removedText = m_listBox->currentText();
314 
315  m_listBox->removeItem( item );
316  if ( count() > 0 )
317  m_listBox->setSelected( TQMIN( item, count() - 1 ), true );
318 
319  emit changed();
320  emit removed( removedText );
321  emit removed( item, removedText );
322  }
323 
324  if ( servRemoveButton && m_listBox->currentItem() == -1 )
325  servRemoveButton->setEnabled(false);
326 }
327 
328 void KEditListBox::enableMoveButtons(int index)
329 {
330  // Update the lineEdit when we select a different line.
331  if(currentText() != m_lineEdit->text())
332  m_lineEdit->setText(currentText());
333 
334  bool moveEnabled = servUpButton && servDownButton;
335 
336  if (moveEnabled )
337  {
338  if (m_listBox->count() <= 1)
339  {
340  servUpButton->setEnabled(false);
341  servDownButton->setEnabled(false);
342  }
343  else if ((uint) index == (m_listBox->count() - 1))
344  {
345  servUpButton->setEnabled(true);
346  servDownButton->setEnabled(false);
347  }
348  else if (index == 0)
349  {
350  servUpButton->setEnabled(false);
351  servDownButton->setEnabled(true);
352  }
353  else
354  {
355  servUpButton->setEnabled(true);
356  servDownButton->setEnabled(true);
357  }
358  }
359 
360  if ( servRemoveButton )
361  servRemoveButton->setEnabled(true);
362 }
363 
364 void KEditListBox::clear()
365 {
366  m_lineEdit->clear();
367  m_listBox->clear();
368  emit changed();
369 }
370 
371 void KEditListBox::insertStringList(const TQStringList& list, int index)
372 {
373  m_listBox->insertStringList(list,index);
374 }
375 
376 void KEditListBox::insertStrList(const TQStrList* list, int index)
377 {
378  m_listBox->insertStrList(list,index);
379 }
380 
381 void KEditListBox::insertStrList(const TQStrList& list, int index)
382 {
383  m_listBox->insertStrList(list,index);
384 }
385 
386 void KEditListBox::insertStrList(const char ** list, int numStrings, int index)
387 {
388  m_listBox->insertStrList(list,numStrings,index);
389 }
390 
391 TQStringList KEditListBox::items() const
392 {
393  TQStringList list;
394  for (TQListBoxItem const * i = m_listBox->firstItem(); i != 0; i = i->next() )
395  list.append( i->text());
396 
397  return list;
398 }
399 
400 void KEditListBox::setItems(const TQStringList& items)
401 {
402  m_listBox->clear();
403  m_listBox->insertStringList(items, 0);
404 }
405 
406 int KEditListBox::buttons() const
407 {
408  return d->buttons;
409 }
410 
411 void KEditListBox::virtual_hook( int, void* )
412 { /*BASE::virtual_hook( id, data );*/ }
413 
414 
417 
418 KEditListBox::CustomEditor::CustomEditor( KComboBox *combo )
419 {
420  m_representationWidget = combo;
421  m_lineEdit = dynamic_cast<KLineEdit*>( combo->lineEdit() );
422  assert( m_lineEdit );
423 }
424 
425 #include "keditlistbox.moc"
KComboBox
An enhanced combo box.
Definition: kcombobox.h:152
KDialog::marginHint
static int marginHint()
Return the number of pixels you shall use between a dialog edge and the outermost widget(s) according...
Definition: kdialog.cpp:104
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:110
KEditListBox::CustomEditor
Custom editor class.
Definition: keditlistbox.h:287
KEditListBox::KEditListBox
KEditListBox(TQWidget *parent=0, const char *name=0, bool checkAtEntering=false, int buttons=All)
Create an editable listbox.
Definition: keditlistbox.cpp:48
KEditListBox::setItems
void setItems(const TQStringList &items)
Clears the listbox and sets the contents to items.
Definition: keditlistbox.cpp:400
KEditListBox::count
int count() const
See TQListBox::count()
Definition: keditlistbox.h:136
KEditListBox::currentText
TQString currentText() const
See TQListBox::currentText()
Definition: keditlistbox.h:172
KEditListBox::renamed
void renamed(const TQString &from, const TQString &to)
This signal is emitted when the user renames a list item.
KEditListBox::currentItem
int currentItem() const
See TQListBox::currentItem()
Definition: keditlistbox.cpp:300
KEditListBox::buttons
int buttons() const
Returns which buttons are visible.
Definition: keditlistbox.cpp:406
KEditListBox::removed
void removed(const TQString &text)
This signal is emitted when the user removes a string from the list.
KEditListBox::added
void added(const TQString &text)
This signal is emitted when the user adds a new string to the list.
KEditListBox::insertStringList
void insertStringList(const TQStringList &list, int index=-1)
See TQListBox::insertStringList()
Definition: keditlistbox.cpp:371
KEditListBox::items
TQStringList items() const
Definition: keditlistbox.cpp:391
KEditListBox::setButtons
void setButtons(uint buttons)
Specifies which buttons should be visible.
Definition: keditlistbox.cpp:117
KEditListBox::text
TQString text(int index) const
See TQListBox::text()
Definition: keditlistbox.h:164
KEditListBox::clear
void clear()
Clears both the listbox and the line edit.
Definition: keditlistbox.cpp:364
KEditListBox::insertStrList
void insertStrList(const TQStrList *list, int index=-1)
See TQListBox::insertStringList()
Definition: keditlistbox.cpp:376
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:146
KLineEdit::setTrapReturnKey
void setTrapReturnKey(bool trap)
By default, KLineEdit recognizes Key_Return and Key_Enter and emits the returnPressed() signals,...
Definition: klineedit.cpp:1068
KLineEdit::setText
virtual void setText(const TQString &)
Re-implemented to enable text squeezing.
Definition: klineedit.cpp:310
KLineEdit::clear
virtual void clear()
Reimplemented to workaround a buggy TQLineEdit::clear() (changing the clipboard to the text we just h...
Definition: klineedit.cpp:1320
KNotifyClient::beep
void beep(const TQString &reason=TQString::null)
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.1
This website is maintained by Timothy Pearson.