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