• 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,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(typedSomething(const TQString&)));
109  m_lineEdit->setTrapReturnKey(true);
110  connect(m_lineEdit,TQT_SIGNAL(returnPressed()),this,TQT_SLOT(addItem()));
111  connect(m_listBox, TQT_SIGNAL(highlighted(int)), TQT_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, TQT_SIGNAL(clicked()), TQT_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, TQT_SIGNAL(clicked()), TQT_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, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemUp()));
152 
153  servDownButton = new TQPushButton(i18n("Move &Down"), this);
154  servDownButton->setEnabled(false);
155  servDownButton->show();
156  connect(servDownButton, TQT_SIGNAL(clicked()), TQT_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  m_listBox->blockSignals( true );
178  m_listBox->changeItem(text, currentItem());
179  m_listBox->blockSignals( block );
180  emit changed();
181  }
182  }
183 
184  if ( !servNewButton )
185  return;
186 
187  if (!d->m_checkAtEntering)
188  servNewButton->setEnabled(!text.isEmpty());
189  else
190  {
191  if (text.isEmpty())
192  {
193  servNewButton->setEnabled(false);
194  }
195  else
196  {
197  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
198  bool enable = (!m_listBox->findItem( text, mode ));
199  servNewButton->setEnabled( enable );
200  }
201  }
202 }
203 
204 void KEditListBox::moveItemUp()
205 {
206  if (!m_listBox->isEnabled())
207  {
208  KNotifyClient::beep();
209  return;
210  }
211 
212  const unsigned int selIndex = m_listBox->currentItem();
213  if (selIndex == 0)
214  {
215  KNotifyClient::beep();
216  return;
217  }
218 
219  TQListBoxItem *selItem = m_listBox->item(selIndex);
220  m_listBox->takeItem(selItem);
221  m_listBox->insertItem(selItem, selIndex-1);
222  m_listBox->setCurrentItem(selIndex - 1);
223 
224  emit changed();
225 }
226 
227 void KEditListBox::moveItemDown()
228 {
229  if (!m_listBox->isEnabled())
230  {
231  KNotifyClient::beep();
232  return;
233  }
234 
235  unsigned int selIndex = m_listBox->currentItem();
236  if (selIndex == m_listBox->count() - 1)
237  {
238  KNotifyClient::beep();
239  return;
240  }
241 
242  TQListBoxItem *selItem = m_listBox->item(selIndex);
243  m_listBox->takeItem(selItem);
244  m_listBox->insertItem(selItem, selIndex+1);
245  m_listBox->setCurrentItem(selIndex + 1);
246 
247  emit changed();
248 }
249 
250 void KEditListBox::addItem()
251 {
252  // when m_checkAtEntering is true, the add-button is disabled, but this
253  // slot can still be called through Key_Return/Key_Enter. So we guard
254  // against this.
255  if ( !servNewButton || !servNewButton->isEnabled() )
256  return;
257 
258  const TQString& currentTextLE=m_lineEdit->text();
259  bool alreadyInList(false);
260  //if we didn't check for dupes at the inserting we have to do it now
261  if (!d->m_checkAtEntering)
262  {
263  // first check current item instead of dumb iterating the entire list
264  if ( m_listBox->currentText() == currentTextLE )
265  alreadyInList = true;
266  else
267  {
268  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
269  alreadyInList =(m_listBox->findItem(currentTextLE, mode) );
270  }
271  }
272 
273  if ( servNewButton )
274  servNewButton->setEnabled(false);
275 
276  bool block = m_lineEdit->signalsBlocked();
277  m_lineEdit->blockSignals(true);
278  m_lineEdit->clear();
279  m_lineEdit->blockSignals(block);
280 
281  m_listBox->setSelected(currentItem(), false);
282 
283  if (!alreadyInList)
284  {
285  block = m_listBox->signalsBlocked();
286  m_listBox->blockSignals( true );
287  m_listBox->insertItem(currentTextLE);
288  m_listBox->blockSignals( block );
289  emit changed();
290  emit added( currentTextLE );
291  }
292 }
293 
294 int KEditListBox::currentItem() const
295 {
296  int nr = m_listBox->currentItem();
297  if(nr >= 0 && !m_listBox->item(nr)->isSelected()) return -1;
298  return nr;
299 }
300 
301 void KEditListBox::removeItem()
302 {
303  int selected = m_listBox->currentItem();
304 
305  if ( selected >= 0 )
306  {
307  TQString removedText = m_listBox->currentText();
308 
309  m_listBox->removeItem( selected );
310  if ( count() > 0 )
311  m_listBox->setSelected( TQMIN( selected, count() - 1 ), true );
312 
313  emit changed();
314  emit removed( removedText );
315  }
316 
317  if ( servRemoveButton && m_listBox->currentItem() == -1 )
318  servRemoveButton->setEnabled(false);
319 }
320 
321 void KEditListBox::enableMoveButtons(int index)
322 {
323  // Update the lineEdit when we select a different line.
324  if(currentText() != m_lineEdit->text())
325  m_lineEdit->setText(currentText());
326 
327  bool moveEnabled = servUpButton && servDownButton;
328 
329  if (moveEnabled )
330  {
331  if (m_listBox->count() <= 1)
332  {
333  servUpButton->setEnabled(false);
334  servDownButton->setEnabled(false);
335  }
336  else if ((uint) index == (m_listBox->count() - 1))
337  {
338  servUpButton->setEnabled(true);
339  servDownButton->setEnabled(false);
340  }
341  else if (index == 0)
342  {
343  servUpButton->setEnabled(false);
344  servDownButton->setEnabled(true);
345  }
346  else
347  {
348  servUpButton->setEnabled(true);
349  servDownButton->setEnabled(true);
350  }
351  }
352 
353  if ( servRemoveButton )
354  servRemoveButton->setEnabled(true);
355 }
356 
357 void KEditListBox::clear()
358 {
359  m_lineEdit->clear();
360  m_listBox->clear();
361  emit changed();
362 }
363 
364 void KEditListBox::insertStringList(const TQStringList& list, int index)
365 {
366  m_listBox->insertStringList(list,index);
367 }
368 
369 void KEditListBox::insertStrList(const TQStrList* list, int index)
370 {
371  m_listBox->insertStrList(list,index);
372 }
373 
374 void KEditListBox::insertStrList(const TQStrList& list, int index)
375 {
376  m_listBox->insertStrList(list,index);
377 }
378 
379 void KEditListBox::insertStrList(const char ** list, int numStrings, int index)
380 {
381  m_listBox->insertStrList(list,numStrings,index);
382 }
383 
384 TQStringList KEditListBox::items() const
385 {
386  TQStringList list;
387  for (TQListBoxItem const * i = m_listBox->firstItem(); i != 0; i = i->next() )
388  list.append( i->text());
389 
390  return list;
391 }
392 
393 void KEditListBox::setItems(const TQStringList& items)
394 {
395  m_listBox->clear();
396  m_listBox->insertStringList(items, 0);
397 }
398 
399 int KEditListBox::buttons() const
400 {
401  return d->buttons;
402 }
403 
404 void KEditListBox::virtual_hook( int, void* )
405 { /*BASE::virtual_hook( id, data );*/ }
406 
407 
410 
411 KEditListBox::CustomEditor::CustomEditor( KComboBox *combo )
412 {
413  m_representationWidget = combo;
414  m_lineEdit = tqt_dynamic_cast<KLineEdit*>( combo->lineEdit() );
415  assert( m_lineEdit );
416 }
417 
418 #include "keditlistbox.moc"
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
KEditListBox::currentItem
int currentItem() const
See TQListBox::currentItem()
Definition: keditlistbox.cpp:294
KNotifyClient::beep
void beep(const TQString &reason=TQString::null)
KLineEdit::clear
virtual void clear()
Reimplemented to workaround a buggy TQLineEdit::clear() (changing the clipboard to the text we just h...
Definition: klineedit.cpp:1322
KEditListBox::insertStringList
void insertStringList(const TQStringList &list, int index=-1)
See TQListBox::insertStringList()
Definition: keditlistbox.cpp:364
KEditListBox::CustomEditor
Custom editor class.
Definition: keditlistbox.h:247
KEditListBox::currentText
TQString currentText() const
See TQListBox::currentText()
Definition: keditlistbox.h:172
KEditListBox::removed
void removed(const TQString &text)
This signal is emitted when the user removes a string from the list, the parameter is the removed str...
tdelocale.h
KEditListBox::added
void added(const TQString &text)
This signal is emitted when the user adds a new string to the list, the parameter is the added string...
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::count
int count() const
See TQListBox::count()
Definition: keditlistbox.h:136
KEditListBox::setButtons
void setButtons(uint buttons)
Specifies which buttons should be visible.
Definition: keditlistbox.cpp:117
KEditListBox::insertStrList
void insertStrList(const TQStrList *list, int index=-1)
See TQListBox::insertStringList()
Definition: keditlistbox.cpp:369
KLineEdit::setTrapReturnKey
void setTrapReturnKey(bool trap)
By default, KLineEdit recognizes Key_Return and Key_Enter and emits the returnPressed() signals...
Definition: klineedit.cpp:1068
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::buttons
int buttons() const
Returns which buttons are visible.
Definition: keditlistbox.cpp:399
KEditListBox::setItems
void setItems(const TQStringList &items)
Clears the listbox and sets the contents to items.
Definition: keditlistbox.cpp:393
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:145
KEditListBox::clear
void clear()
Clears both the listbox and the line edit.
Definition: keditlistbox.cpp:357
CaseSensitive
KComboBox
An enhanced combo box.
Definition: kcombobox.h:151
KEditListBox::items
TQStringList items() const
Definition: keditlistbox.cpp:384
KLineEdit::setText
virtual void setText(const TQString &)
Re-implemented to enable text squeezing.
Definition: klineedit.cpp:310

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.8.8
This website is maintained by Timothy Pearson.