23 #include <QtCore/QStringList>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QLabel>
26 #include <QtGui/QLayout>
27 #include <QtGui/QListView>
39 class KEditListBoxPrivate
52 QVBoxLayout* mainLayout;
53 QVBoxLayout* btnsLayout;
54 QStringListModel *model;
57 KEditListBox::Buttons buttons;
60 QWidget *representationWidget = 0 );
62 void updateButtonState();
63 QModelIndex selectedIndex();
70 void KEditListBoxPrivate::init(
bool check, KEditListBox::Buttons newButtons,
73 checkAtEntering = check;
75 servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
76 q->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,
77 QSizePolicy::Preferred));
79 mainLayout =
new QVBoxLayout(q);
81 QHBoxLayout* subLayout =
new QHBoxLayout;
82 btnsLayout =
new QVBoxLayout;
83 btnsLayout->addStretch();
85 model =
new QStringListModel();
87 listView->setModel(model);
89 subLayout->addWidget(listView);
90 subLayout->addLayout(btnsLayout);
92 mainLayout->insertLayout(1, subLayout);
94 setEditor( lineEdit, representationWidget );
97 q->setButtons( newButtons );
99 q->connect(listView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
100 SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));
104 void KEditListBoxPrivate::setEditor(
KLineEdit* newLineEdit,
QWidget* representationWidget )
106 if (editingWidget != lineEdit &&
107 editingWidget != representationWidget) {
108 delete editingWidget;
110 if (lineEdit != newLineEdit) {
113 lineEdit = newLineEdit ? newLineEdit :
new KLineEdit(q);
114 editingWidget = representationWidget ?
115 representationWidget : lineEdit;
117 if ( representationWidget )
118 representationWidget->setParent(q);
120 mainLayout->insertWidget(0,editingWidget);
122 lineEdit->setTrapReturnKey(
true);
123 lineEdit->installEventFilter(q);
125 q->connect(lineEdit,SIGNAL(textChanged(
QString)),SLOT(typedSomething(
QString)));
126 q->connect(lineEdit,SIGNAL(returnPressed()),SLOT(addItem()));
129 q->typedSomething( lineEdit->text() );
133 q->setTabOrder(editingWidget, listView);
136 q->setTabOrder(w,servNewButton);
139 if (servRemoveButton) {
140 q->setTabOrder(w,servRemoveButton);
141 w = servRemoveButton;
144 q->setTabOrder(w,servUpButton);
147 if (servDownButton) {
148 q->setTabOrder(w,servDownButton);
154 void KEditListBoxPrivate::updateButtonState()
156 QModelIndex index = selectedIndex();
158 servUpButton->setEnabled(index.isValid());
160 if (servDownButton) {
161 servDownButton->setEnabled(index.isValid());
163 if (servRemoveButton) {
164 servRemoveButton->setEnabled(index.isValid());
168 QModelIndex KEditListBoxPrivate::selectedIndex()
171 const QModelIndexList selectedIndexes = selection->selectedIndexes();
172 if ( !selectedIndexes.isEmpty() && selectedIndexes[0].isValid() )
173 return selectedIndexes[0];
175 return QModelIndex();
179 class KEditListBox::CustomEditorPrivate
184 representationWidget(0),
193 : d(new CustomEditorPrivate(this))
198 : d(new CustomEditorPrivate(this))
200 d->representationWidget = repWidget;
205 : d(new CustomEditorPrivate(this))
207 d->representationWidget = combo;
208 d->lineEdit = qobject_cast<
KLineEdit*>( combo->lineEdit() );
209 Q_ASSERT( d->lineEdit );
219 d->representationWidget = repWidget;
229 return d->representationWidget;
244 :
QGroupBox(title, parent), d(new KEditListBoxPrivate(this))
250 bool checkAtEntering, Buttons buttons )
251 :
QGroupBox(parent ), d(new KEditListBoxPrivate(this))
254 d->init( checkAtEntering, buttons );
258 const char *
name,
bool checkAtEntering, Buttons buttons)
259 :
QGroupBox(title, parent ), d(new KEditListBoxPrivate(this))
262 d->init( checkAtEntering, buttons );
267 bool checkAtEntering, Buttons buttons)
268 :
QGroupBox(title, parent), d(new KEditListBoxPrivate(this))
297 return d->servNewButton;
302 return d->servRemoveButton;
307 return d->servUpButton;
312 return d->servDownButton;
317 return int(d->model->rowCount());
322 if ( d->buttons == buttons )
325 if ( ( buttons &
Add ) && !d->servNewButton ) {
327 d->servNewButton->setEnabled(
false);
328 d->servNewButton->show();
329 connect(d->servNewButton, SIGNAL(clicked()), SLOT(
addItem()));
331 d->btnsLayout->insertWidget(0, d->servNewButton);
332 }
else if ( ( buttons & Add ) == 0 && d->servNewButton ) {
333 delete d->servNewButton;
334 d->servNewButton = 0;
337 if ( ( buttons &
Remove ) && !d->servRemoveButton ) {
339 d->servRemoveButton->setEnabled(
false);
340 d->servRemoveButton->show();
341 connect(d->servRemoveButton, SIGNAL(clicked()), SLOT(
removeItem()));
343 d->btnsLayout->insertWidget(1, d->servRemoveButton);
344 }
else if ( ( buttons & Remove ) == 0 && d->servRemoveButton ) {
345 delete d->servRemoveButton;
346 d->servRemoveButton = 0;
349 if ( ( buttons &
UpDown ) && !d->servUpButton ) {
351 d->servUpButton->setEnabled(
false);
352 d->servUpButton->show();
353 connect(d->servUpButton, SIGNAL(clicked()), SLOT(
moveItemUp()));
356 d->servDownButton->setEnabled(
false);
357 d->servDownButton->show();
358 connect(d->servDownButton, SIGNAL(clicked()), SLOT(
moveItemDown()));
360 d->btnsLayout->insertWidget(2, d->servUpButton);
361 d->btnsLayout->insertWidget(3, d->servDownButton);
362 }
else if ( ( buttons & UpDown ) == 0 && d->servUpButton ) {
363 delete d->servUpButton; d->servUpButton = 0;
364 delete d->servDownButton; d->servDownButton = 0;
372 d->checkAtEntering = check;
377 return d->checkAtEntering;
388 bool block = d->listView->signalsBlocked();
389 d->listView->blockSignals(
true );
390 QModelIndex currentIndex = d->selectedIndex();
391 if ( currentIndex.isValid() )
392 d->model->setData(currentIndex,text);
393 d->listView->blockSignals( block );
398 if ( !d->servNewButton )
401 if ( !d->lineEdit->hasAcceptableInput() ) {
402 d->servNewButton->setEnabled(
false);
406 if (!d->checkAtEntering)
407 d->servNewButton->setEnabled(!text.isEmpty());
412 d->servNewButton->setEnabled(
false);
417 bool enable = !list.contains( text, Qt::CaseSensitive );
418 d->servNewButton->setEnabled( enable );
425 if (!d->listView->isEnabled())
431 QModelIndex index = d->selectedIndex();
432 if ( index.isValid() ) {
433 if (index.row() == 0) {
438 QModelIndex aboveIndex = d->model->index( index.row() - 1, index.column() );
440 QString tmp = d->model->data( aboveIndex, Qt::DisplayRole ).toString();
441 d->model->setData( aboveIndex, d->model->data( index, Qt::DisplayRole ) );
442 d->model->setData( index, tmp );
445 d->listView->selectionModel()->select(aboveIndex, QItemSelectionModel::Select);
453 if (!d->listView->isEnabled())
459 QModelIndex index = d->selectedIndex();
460 if ( index.isValid() ) {
461 if (index.row() == d->model->rowCount() - 1) {
466 QModelIndex belowIndex = d->model->index( index.row() + 1, index.column() );
468 QString tmp = d->model->data( belowIndex, Qt::DisplayRole ).toString();
469 d->model->setData( belowIndex, d->model->data( index, Qt::DisplayRole ) );
470 d->model->setData( index, tmp );
473 d->listView->selectionModel()->select(belowIndex, QItemSelectionModel::Select);
484 if ( !d->servNewButton || !d->servNewButton->isEnabled() )
487 QModelIndex currentIndex = d->selectedIndex();
489 const QString& currentTextLE=d->lineEdit->text();
490 bool alreadyInList(
false);
492 if (!d->checkAtEntering)
495 if ( currentIndex.isValid() ) {
496 if ( d->model->data( currentIndex, Qt::DisplayRole ).toString() == currentTextLE )
497 alreadyInList =
true;
501 alreadyInList = d->model->stringList().contains( currentTextLE, Qt::CaseSensitive );
504 if ( d->servNewButton )
505 d->servNewButton->setEnabled(
false);
507 bool block = d->lineEdit->signalsBlocked();
508 d->lineEdit->blockSignals(
true);
509 d->lineEdit->clear();
510 d->lineEdit->blockSignals(block);
516 block = d->listView->signalsBlocked();
518 if ( currentIndex.isValid() ) {
519 d->model->setData(currentIndex, currentTextLE );
523 lst<<d->model->stringList();
524 d->model->setStringList(lst);
527 emit
added( currentTextLE );
530 d->updateButtonState();
535 QModelIndex selectedIndex = d->selectedIndex();
536 if ( selectedIndex.isValid() )
537 return selectedIndex.row();
544 QModelIndex currentIndex = d->selectedIndex();
545 if ( !currentIndex.isValid() )
548 if ( currentIndex.row() >= 0 )
550 QString removedText = d->model->data( currentIndex, Qt::DisplayRole ).toString();
552 d->model->removeRows( currentIndex.row(), 1 );
554 d->listView->selectionModel()->clear();
561 d->updateButtonState();
566 int index = newIndex.row();
572 bool moveEnabled = d->servUpButton && d->servDownButton;
576 if (d->model->rowCount() <= 1)
578 d->servUpButton->setEnabled(
false);
579 d->servDownButton->setEnabled(
false);
581 else if (index == (d->model->rowCount() - 1))
583 d->servUpButton->setEnabled(
true);
584 d->servDownButton->setEnabled(
false);
588 d->servUpButton->setEnabled(
false);
589 d->servDownButton->setEnabled(
true);
593 d->servUpButton->setEnabled(
true);
594 d->servDownButton->setEnabled(
true);
598 if ( d->servRemoveButton )
599 d->servRemoveButton->setEnabled(
true);
604 d->lineEdit->clear();
615 for (
int i = 0, j = index; i < list.count(); ++i, ++j )
616 content.insert( j, list[ i ] );
618 d->model->setStringList( content );
628 list.insert( index, text );
630 d->model->setStringList(list);
637 return list[ index ];
642 QModelIndex index = d->selectedIndex();
643 if ( !index.isValid() )
646 return text( index.row() );
651 return d->model->stringList();
656 d->model->setStringList(items);
664 void KEditListBox::slotSelectionChanged(
const QItemSelection&,
const QItemSelection& )
666 d->updateButtonState();
667 QModelIndex index = d->selectedIndex();
669 if (index.isValid()) {
670 d->lineEdit->setFocus( Qt::OtherFocusReason );
676 if (o == d->lineEdit && e->type() == QEvent::KeyPress ) {
677 QKeyEvent* keyEvent = (QKeyEvent*)e;
678 if (keyEvent->key() == Qt::Key_Down ||
679 keyEvent->key() == Qt::Key_Up) {
680 return ((
QObject*)d->listView)->event(e);
687 #include "keditlistbox.moc"
QString i18n(const char *text)
int currentItem() const
See Q3ListBox::currentItem()
void setLineEdit(KLineEdit *edit)
void added(const QString &text)
This signal is emitted when the user adds a new string to the list, the parameter is the added string...
virtual KLineEdit * lineEdit() const
KEditListBox(QWidget *parent=0)
Create an editable listbox.
const char * name(StandardAction id)
This will return the internal name of a given standard action.
QPushButton * upButton() const
Return a pointer to the Up button.
virtual QWidget * representationWidget() const
QString currentText() const
See Q3ListBox::currentText()
QString text(int index) const
See Q3ListBox::text()
QPushButton * addButton() const
Return a pointer to the Add button.
void setCustomEditor(const CustomEditor &editor)
Allows to use a custom editing widget instead of the standard KLineEdit widget.
void insertStringList(const QStringList &list, int index=-1)
See Q3ListBox::insertStringList()
void setCheckAtEntering(bool check)
If check is true, after every character you type in the line edit KEditListBox will enable or disable...
bool eventFilter(QObject *o, QEvent *e)
Reimplented for interal reasons.
int count() const
See Q3ListBox::count()
QPushButton * removeButton() const
Return a pointer to the Remove button.
A wrapper around QIcon that provides KDE icon features.
void setButtons(Buttons buttons)
Specifies which buttons should be visible.
static void beep(const QString &reason=QString(), QWidget *widget=0L)
This is a simple substitution for QApplication::beep()
void insertItem(const QString &text, int index=-1)
See Q3ListBox::insertItem()
void enableMoveButtons(const QModelIndex &, const QModelIndex &)
bool checkAtEntering()
Returns true if check at entering is enabled.
QStringList items() const
An enhanced QLineEdit widget for inputting text.
void clear()
Clears both the listbox and the line edit.
void removed(const QString &text)
This signal is emitted when the user removes a string from the list, the parameter is the removed str...
KLineEdit * lineEdit() const
Return a pointer to the embedded KLineEdit.
void setRepresentationWidget(QWidget *repWidget)
void setItems(const QStringList &items)
Clears the listbox and sets the contents to items.
Buttons buttons() const
Returns which buttons are visible.
friend class KEditListBoxPrivate
QPushButton * downButton() const
Return a pointer to the Down button.
void typedSomething(const QString &text)
QListView * listView() const
Return a pointer to the embedded QListView.