23 #include <QtCore/QStringList>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QLabel>
26 #include <QtGui/QLayout>
27 #include <QtGui/QListView>
39 class KEditListBoxPrivate
57 KEditListBox::Buttons buttons;
60 QWidget *representationWidget = 0 );
62 void updateButtonState();
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));
87 listView->setModel(model);
92 mainLayout->insertLayout(1, subLayout);
94 setEditor( lineEdit, representationWidget );
97 q->setButtons( newButtons );
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 )
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);
139 if (servRemoveButton) {
141 w = servRemoveButton;
147 if (servDownButton) {
154 void KEditListBoxPrivate::updateButtonState()
158 servUpButton->setEnabled(index.
isValid());
160 if (servDownButton) {
161 servDownButton->setEnabled(index.
isValid());
163 if (servRemoveButton) {
164 servRemoveButton->setEnabled(index.
isValid());
172 if ( !selectedIndexes.isEmpty() && selectedIndexes[0].isValid() )
173 return selectedIndexes[0];
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;
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();
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();
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();
356 d->servDownButton->setEnabled(
false);
357 d->servDownButton->show();
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 );
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())
433 if (index.
row() == 0) {
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())
461 if (index.
row() == d->model->rowCount() - 1) {
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() )
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();
537 return selectedIndex.
row();
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 ];
651 return d->model->stringList();
656 d->model->setStringList(items);
666 d->updateButtonState();
670 d->lineEdit->setFocus( Qt::OtherFocusReason );
676 if (o == d->lineEdit && e->
type() == QEvent::KeyPress ) {
678 if (keyEvent->
key() == Qt::Key_Down ||
679 keyEvent->
key() == Qt::Key_Up) {
687 #include "keditlistbox.moc"
QString i18n(const char *text)
int currentItem() const
See Q3ListBox::currentItem()
void setLineEdit(KLineEdit *edit)
void clicked(bool checked)
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
bool contains(const QString &str, Qt::CaseSensitivity cs) 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()
int count(const T &value) const
void append(const T &value)
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()
void setObjectName(const QString &name)
QModelIndexList selectedIndexes() const
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.
QLineEdit * lineEdit() const
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...
void addStretch(int stretch)
KLineEdit * lineEdit() const
Return a pointer to the embedded KLineEdit.
void insert(int i, const T &value)
void setRepresentationWidget(QWidget *repWidget)
void setItems(const QStringList &items)
Clears the listbox and sets the contents to items.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Buttons buttons() const
Returns which buttons are visible.
friend class KEditListBoxPrivate
virtual bool event(QEvent *e)
QPushButton * downButton() const
Return a pointer to the Down button.
void addLayout(QLayout *layout, int stretch)
void typedSomething(const QString &text)
QListView * listView() const
Return a pointer to the embedded QListView.