• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Property Editing Library
 

KDevelop Property Editing Library

  • lib
  • widgets
  • propeditor
qeditlistbox.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 #include "qeditlistbox.h"
21 
22 #include "compat_tools.h"
23 
24 #include <klineedit.h>
25 
26 #include <tqpushbutton.h>
27 #include <tqlayout.h>
28 #include <tqgroupbox.h>
29 #include <tqlistbox.h>
30 #include <tqwhatsthis.h>
31 #include <tqlabel.h>
32 #include <tqcombobox.h>
33 #include <tqapplication.h>
34 #include <tqstringlist.h>
35 
36 #include <assert.h>
37 
38 //same as kdialog.cpp ones
39 #define MarginSize 11
40 #define SpazingSize 6
41 
42 class QEditListBoxPrivate
43 {
44 public:
45  bool m_checkAtEntering;
46  int buttons;
47 };
48 
49 QEditListBox::QEditListBox(TQWidget *parent, const char *name,
50  bool checkAtEntering, int buttons )
51  :TQGroupBox(parent, name )
52 {
53  init( checkAtEntering, buttons );
54 }
55 
56 QEditListBox::QEditListBox(const TQString& title, TQWidget *parent,
57  const char *name, bool checkAtEntering, int buttons)
58  :TQGroupBox(title, parent, name )
59 {
60  init( checkAtEntering, buttons );
61 }
62 
63 QEditListBox::QEditListBox(const TQString& title, const CustomEditor& custom,
64  TQWidget *parent, const char *name,
65  bool checkAtEntering, int buttons)
66  :TQGroupBox(title, parent, name )
67 {
68  m_lineEdit = custom.lineEdit();
69  init( checkAtEntering, buttons, custom.representationWidget() );
70 }
71 
72 QEditListBox::~QEditListBox()
73 {
74  delete d;
75  d=0;
76 }
77 
78 void QEditListBox::init( bool checkAtEntering, int buttons,
79  TQWidget *representationWidget )
80 {
81  d=new QEditListBoxPrivate;
82  d->m_checkAtEntering=checkAtEntering;
83  d->buttons = buttons;
84 
85  int lostButtons = 0;
86  if ( (buttons & Add) == 0 )
87  lostButtons++;
88  if ( (buttons & Remove) == 0 )
89  lostButtons++;
90  if ( (buttons & UpDown) == 0 )
91  lostButtons += 2;
92 
93 
94  servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
95  setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding,
96  TQSizePolicy::MinimumExpanding));
97 
98  TQWidget * gb = this;
99  TQGridLayout * grid = new TQGridLayout(gb, 7 - lostButtons, 2,
100  MarginSize,
101  SpazingSize);
102  grid->addRowSpacing(0, fontMetrics().lineSpacing());
103  for ( int i = 1; i < 7 - lostButtons; i++ )
104  grid->setRowStretch(i, 1);
105 
106  grid->setMargin(15);
107 
108  if ( representationWidget )
109  representationWidget->reparent( gb, TQPoint(0,0) );
110  else
111  m_lineEdit=new KLineEdit(gb);
112 
113  m_listBox = new TQListBox(gb);
114 
115  TQWidget *editingWidget = representationWidget ?
116  representationWidget : m_lineEdit;
117  grid->addMultiCellWidget(editingWidget,1,1,0,1);
118  grid->addMultiCellWidget(m_listBox, 2, 6 - lostButtons, 0, 0);
119  int row = 2;
120  if ( buttons & Add ) {
121  servNewButton = new TQPushButton(i18n("&Add"), gb);
122  servNewButton->setEnabled(false);
123  connect(servNewButton, TQT_SIGNAL(clicked()), TQT_SLOT(addItem()));
124 
125  grid->addWidget(servNewButton, row++, 1);
126  }
127 
128  if ( buttons & Remove ) {
129  servRemoveButton = new TQPushButton(i18n("&Remove"), gb);
130  servRemoveButton->setEnabled(false);
131  connect(servRemoveButton, TQT_SIGNAL(clicked()), TQT_SLOT(removeItem()));
132 
133  grid->addWidget(servRemoveButton, row++, 1);
134  }
135 
136  if ( buttons & UpDown ) {
137  servUpButton = new TQPushButton(i18n("Move &Up"), gb);
138  servUpButton->setEnabled(false);
139  connect(servUpButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemUp()));
140 
141  servDownButton = new TQPushButton(i18n("Move &Down"), gb);
142  servDownButton->setEnabled(false);
143  connect(servDownButton, TQT_SIGNAL(clicked()), TQT_SLOT(moveItemDown()));
144 
145  grid->addWidget(servUpButton, row++, 1);
146  grid->addWidget(servDownButton, row++, 1);
147  }
148 
149  connect(m_lineEdit,TQT_SIGNAL(textChanged(const TQString&)),this,TQT_SLOT(typedSomething(const TQString&)));
150 
151  connect(m_lineEdit,TQT_SIGNAL(returnPressed()),this,TQT_SLOT(addItem()));
152  connect(m_listBox, TQT_SIGNAL(highlighted(int)), TQT_SLOT(enableMoveButtons(int)));
153 
154  // maybe supplied lineedit has some text already
155  typedSomething( m_lineEdit->text() );
156 }
157 
158 void QEditListBox::typedSomething(const TQString& text)
159 {
160  if(currentItem() >= 0) {
161  if(currentText() != m_lineEdit->text())
162  {
163  // IMHO changeItem() shouldn't do anything with the value
164  // of currentItem() ... like changing it or emitting signals ...
165  // but TT disagree with me on this one (it's been that way since ages ... grrr)
166  bool block = m_listBox->signalsBlocked();
167  m_listBox->blockSignals( true );
168  m_listBox->changeItem(text, currentItem());
169  m_listBox->blockSignals( block );
170  emit changed();
171  }
172  }
173 
174  if ( !servNewButton )
175  return;
176 
177  if (!d->m_checkAtEntering)
178  servNewButton->setEnabled(!text.isEmpty());
179  else
180  {
181  if (text.isEmpty())
182  {
183  servNewButton->setEnabled(false);
184  }
185  else
186  {
187  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
188  bool enable = (m_listBox->findItem( text, mode ) == 0L);
189  servNewButton->setEnabled( enable );
190  }
191  }
192 }
193 
194 void QEditListBox::moveItemUp()
195 {
196  if (!m_listBox->isEnabled())
197  {
198  tqDebug("beep");
199  return;
200  }
201 
202  unsigned int selIndex = m_listBox->currentItem();
203  if (selIndex == 0)
204  {
205  tqDebug("beep");
206  return;
207  }
208 
209  TQListBoxItem *selItem = m_listBox->item(selIndex);
210  m_listBox->takeItem(selItem);
211  m_listBox->insertItem(selItem, selIndex-1);
212  m_listBox->setCurrentItem(selIndex - 1);
213 
214  emit changed();
215 }
216 
217 void QEditListBox::moveItemDown()
218 {
219  if (!m_listBox->isEnabled())
220  {
221  tqDebug("beep");
222  return;
223  }
224 
225  unsigned int selIndex = m_listBox->currentItem();
226  if (selIndex == m_listBox->count() - 1)
227  {
228  tqDebug("beep");
229  return;
230  }
231 
232  TQListBoxItem *selItem = m_listBox->item(selIndex);
233  m_listBox->takeItem(selItem);
234  m_listBox->insertItem(selItem, selIndex+1);
235  m_listBox->setCurrentItem(selIndex + 1);
236 
237  emit changed();
238 }
239 
240 void QEditListBox::addItem()
241 {
242  // when m_checkAtEntering is true, the add-button is disabled, but this
243  // slot can still be called through Key_Return/Key_Enter. So we guard
244  // against this.
245  if ( !servNewButton || !servNewButton->isEnabled() )
246  return;
247 
248  const TQString& currentTextLE=m_lineEdit->text();
249  bool alreadyInList(false);
250  //if we didn't check for dupes at the inserting we have to do it now
251  if (!d->m_checkAtEntering)
252  {
253  // first check current item instead of dumb iterating the entire list
254  if ( m_listBox->currentText() == currentTextLE )
255  alreadyInList = true;
256  else
257  {
258  StringComparisonMode mode = (StringComparisonMode) (ExactMatch | CaseSensitive );
259  alreadyInList =(m_listBox->findItem(currentTextLE, mode) != 0);
260  }
261  }
262 
263  if ( servNewButton )
264  servNewButton->setEnabled(false);
265 
266  bool block = m_lineEdit->signalsBlocked();
267  m_lineEdit->blockSignals(true);
268  m_lineEdit->clear();
269  m_lineEdit->blockSignals(block);
270 
271  m_listBox->setSelected(currentItem(), false);
272 
273  if (!alreadyInList)
274  {
275  block = m_listBox->signalsBlocked();
276  m_listBox->blockSignals( true );
277  m_listBox->insertItem(currentTextLE);
278  m_listBox->blockSignals( block );
279  emit changed();
280  emit added( currentTextLE );
281  }
282 }
283 
284 int QEditListBox::currentItem() const
285 {
286  int nr = m_listBox->currentItem();
287  if(nr >= 0 && !m_listBox->item(nr)->isSelected()) return -1;
288  return nr;
289 }
290 
291 void QEditListBox::removeItem()
292 {
293  int selected = m_listBox->currentItem();
294 
295  if ( selected >= 0 )
296  {
297  TQString removedText = m_listBox->currentText();
298 
299  m_listBox->removeItem( selected );
300  if ( count() > 0 )
301  m_listBox->setSelected( TQMIN( selected, count() - 1 ), true );
302 
303  emit changed();
304  emit removed( removedText );
305  }
306 
307  if ( servRemoveButton && m_listBox->currentItem() == -1 )
308  servRemoveButton->setEnabled(false);
309 }
310 
311 void QEditListBox::enableMoveButtons(int index)
312 {
313  // Update the lineEdit when we select a different line.
314  if(currentText() != m_lineEdit->text())
315  m_lineEdit->setText(currentText());
316 
317  bool moveEnabled = servUpButton && servDownButton;
318 
319  if (moveEnabled )
320  {
321  if (m_listBox->count() <= 1)
322  {
323  servUpButton->setEnabled(false);
324  servDownButton->setEnabled(false);
325  }
326  else if ((uint) index == (m_listBox->count() - 1))
327  {
328  servUpButton->setEnabled(true);
329  servDownButton->setEnabled(false);
330  }
331  else if (index == 0)
332  {
333  servUpButton->setEnabled(false);
334  servDownButton->setEnabled(true);
335  }
336  else
337  {
338  servUpButton->setEnabled(true);
339  servDownButton->setEnabled(true);
340  }
341  }
342 
343  if ( servRemoveButton )
344  servRemoveButton->setEnabled(true);
345 }
346 
347 void QEditListBox::clear()
348 {
349  m_lineEdit->clear();
350  m_listBox->clear();
351  emit changed();
352 }
353 
354 void QEditListBox::insertStringList(const TQStringList& list, int index)
355 {
356  m_listBox->insertStringList(list,index);
357 }
358 
359 void QEditListBox::insertStrList(const TQStrList* list, int index)
360 {
361  m_listBox->insertStrList(list,index);
362 }
363 
364 void QEditListBox::insertStrList(const TQStrList& list, int index)
365 {
366  m_listBox->insertStrList(list,index);
367 }
368 
369 void QEditListBox::insertStrList(const char ** list, int numStrings, int index)
370 {
371  m_listBox->insertStrList(list,numStrings,index);
372 }
373 
374 TQStringList QEditListBox::items() const
375 {
376  TQStringList list;
377  for ( uint i = 0; i < m_listBox->count(); i++ )
378  list.append( m_listBox->text( i ));
379 
380  return list;
381 }
382 
383 void QEditListBox::setItems(const TQStringList& items)
384 {
385  m_listBox->clear();
386  m_listBox->insertStringList(items, 0);
387 }
388 
389 void QEditListBox::virtual_hook( int, void* )
390 { /*BASE::virtual_hook( id, data );*/ }
391 
392 
395 
396 QEditListBox::CustomEditor::CustomEditor( TQComboBox *combo )
397 {
398  m_representationWidget = combo;
399  m_lineEdit = dynamic_cast<KLineEdit*>( combo->lineEdit() );
400  assert( m_lineEdit );
401 }
402 
403 #include "qeditlistbox.moc"

KDevelop Property Editing Library

Skip menu "KDevelop Property Editing Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDevelop Property Editing Library

Skip menu "KDevelop Property Editing Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Property Editing Library by doxygen 1.8.13
This website is maintained by Timothy Pearson.