24 #include <QtCore/QStringList>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QLabel>
27 #include <QtGui/QLayout>
28 #include <QtGui/QListView>
40 class KEditListWidgetPrivate
53 QVBoxLayout* mainLayout;
54 QVBoxLayout* btnsLayout;
55 QStringListModel *model;
58 KEditListWidget::Buttons buttons;
61 QWidget *representationWidget = 0 );
63 void updateButtonState();
64 QModelIndex selectedIndex();
71 void KEditListWidgetPrivate::init(
bool check, KEditListWidget::Buttons newButtons,
74 checkAtEntering = check;
76 servNewButton = servRemoveButton = servUpButton = servDownButton = 0L;
77 q->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,
78 QSizePolicy::Preferred));
80 mainLayout =
new QVBoxLayout(q);
82 QHBoxLayout* subLayout =
new QHBoxLayout;
83 btnsLayout =
new QVBoxLayout;
84 btnsLayout->addStretch();
86 model =
new QStringListModel();
88 listView->setModel(model);
90 subLayout->addWidget(listView);
91 subLayout->addLayout(btnsLayout);
93 mainLayout->insertLayout(1, subLayout);
95 setEditor( lineEdit, representationWidget );
98 q->setButtons( newButtons );
100 q->connect(listView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
101 SLOT(slotSelectionChanged(QItemSelection,QItemSelection)));
105 void KEditListWidgetPrivate::setEditor(
KLineEdit* newLineEdit,
QWidget* representationWidget )
107 if (editingWidget != lineEdit &&
108 editingWidget != representationWidget) {
109 delete editingWidget;
111 if (lineEdit != newLineEdit) {
114 lineEdit = newLineEdit ? newLineEdit :
new KLineEdit(q);
115 editingWidget = representationWidget ?
116 representationWidget : lineEdit;
118 if ( representationWidget )
119 representationWidget->setParent(q);
121 mainLayout->insertWidget(0,editingWidget);
123 lineEdit->setTrapReturnKey(
true);
124 lineEdit->installEventFilter(q);
126 q->connect(lineEdit,SIGNAL(textChanged(
QString)),SLOT(typedSomething(
QString)));
127 q->connect(lineEdit,SIGNAL(returnPressed()),SLOT(addItem()));
130 q->typedSomething( lineEdit->text() );
134 q->setTabOrder(editingWidget, listView);
137 q->setTabOrder(w,servNewButton);
140 if (servRemoveButton) {
141 q->setTabOrder(w,servRemoveButton);
142 w = servRemoveButton;
145 q->setTabOrder(w,servUpButton);
148 if (servDownButton) {
149 q->setTabOrder(w,servDownButton);
155 void KEditListWidgetPrivate::updateButtonState()
157 QModelIndex index = selectedIndex();
159 servUpButton->setEnabled(index.isValid());
161 if (servDownButton) {
162 servDownButton->setEnabled(index.isValid());
164 if (servRemoveButton) {
165 servRemoveButton->setEnabled(index.isValid());
169 QModelIndex KEditListWidgetPrivate::selectedIndex()
172 const QModelIndexList selectedIndexes = selection->selectedIndexes();
173 if ( !selectedIndexes.isEmpty() && selectedIndexes[0].isValid() )
174 return selectedIndexes[0];
176 return QModelIndex();
180 class KEditListWidget::CustomEditorPrivate
185 representationWidget(0),
194 : d(new CustomEditorPrivate(this))
199 : d(new CustomEditorPrivate(this))
201 d->representationWidget = repWidget;
206 : d(new CustomEditorPrivate(this))
208 d->representationWidget = combo;
209 d->lineEdit = qobject_cast<
KLineEdit*>( combo->lineEdit() );
210 Q_ASSERT( d->lineEdit );
220 d->representationWidget = repWidget;
230 return d->representationWidget;
246 bool checkAtEntering,
248 :
QWidget(parent), d(new KEditListWidgetPrivate(this))
276 return d->servNewButton;
281 return d->servRemoveButton;
286 return d->servUpButton;
291 return d->servDownButton;
296 return int(d->model->rowCount());
301 if ( d->buttons == buttons )
304 if ( ( buttons &
Add ) && !d->servNewButton ) {
306 d->servNewButton->setEnabled(
false);
307 d->servNewButton->show();
308 connect(d->servNewButton, SIGNAL(clicked()), SLOT(addItem()));
310 d->btnsLayout->insertWidget(0, d->servNewButton);
311 }
else if ( ( buttons & Add ) == 0 && d->servNewButton ) {
312 delete d->servNewButton;
313 d->servNewButton = 0;
316 if ( ( buttons &
Remove ) && !d->servRemoveButton ) {
318 d->servRemoveButton->setEnabled(
false);
319 d->servRemoveButton->show();
320 connect(d->servRemoveButton, SIGNAL(clicked()), SLOT(removeItem()));
322 d->btnsLayout->insertWidget(1, d->servRemoveButton);
323 }
else if ( ( buttons & Remove ) == 0 && d->servRemoveButton ) {
324 delete d->servRemoveButton;
325 d->servRemoveButton = 0;
328 if ( ( buttons &
UpDown ) && !d->servUpButton ) {
330 d->servUpButton->setEnabled(
false);
331 d->servUpButton->show();
332 connect(d->servUpButton, SIGNAL(clicked()), SLOT(moveItemUp()));
335 d->servDownButton->setEnabled(
false);
336 d->servDownButton->show();
337 connect(d->servDownButton, SIGNAL(clicked()), SLOT(moveItemDown()));
339 d->btnsLayout->insertWidget(2, d->servUpButton);
340 d->btnsLayout->insertWidget(3, d->servDownButton);
341 }
else if ( ( buttons & UpDown ) == 0 && d->servUpButton ) {
342 delete d->servUpButton; d->servUpButton = 0;
343 delete d->servDownButton; d->servDownButton = 0;
351 d->checkAtEntering = check;
356 return d->checkAtEntering;
359 void KEditListWidget::typedSomething(
const QString& text)
367 bool block = d->listView->signalsBlocked();
368 d->listView->blockSignals(
true );
369 QModelIndex currentIndex = d->selectedIndex();
370 if ( currentIndex.isValid() )
371 d->model->setData(currentIndex,text);
372 d->listView->blockSignals( block );
377 if ( !d->servNewButton )
380 if ( !d->lineEdit->hasAcceptableInput() ) {
381 d->servNewButton->setEnabled(
false);
385 if (!d->checkAtEntering)
386 d->servNewButton->setEnabled(!text.isEmpty());
391 d->servNewButton->setEnabled(
false);
396 bool enable = !list.contains( text, Qt::CaseSensitive );
397 d->servNewButton->setEnabled( enable );
402 void KEditListWidget::moveItemUp()
404 if (!d->listView->isEnabled())
410 QModelIndex index = d->selectedIndex();
411 if ( index.isValid() ) {
412 if (index.row() == 0) {
417 QModelIndex aboveIndex = d->model->index( index.row() - 1, index.column() );
419 QString tmp = d->model->data( aboveIndex, Qt::DisplayRole ).toString();
420 d->model->setData( aboveIndex, d->model->data( index, Qt::DisplayRole ) );
421 d->model->setData( index, tmp );
424 d->listView->selectionModel()->select(aboveIndex, QItemSelectionModel::Select);
430 void KEditListWidget::moveItemDown()
432 if (!d->listView->isEnabled())
438 QModelIndex index = d->selectedIndex();
439 if ( index.isValid() ) {
440 if (index.row() == d->model->rowCount() - 1) {
445 QModelIndex belowIndex = d->model->index( index.row() + 1, index.column() );
447 QString tmp = d->model->data( belowIndex, Qt::DisplayRole ).toString();
448 d->model->setData( belowIndex, d->model->data( index, Qt::DisplayRole ) );
449 d->model->setData( index, tmp );
452 d->listView->selectionModel()->select(belowIndex, QItemSelectionModel::Select);
458 void KEditListWidget::addItem()
463 if ( !d->servNewButton || !d->servNewButton->isEnabled() )
466 QModelIndex currentIndex = d->selectedIndex();
468 const QString& currentTextLE=d->lineEdit->text();
469 bool alreadyInList(
false);
471 if (!d->checkAtEntering)
474 if ( currentIndex.isValid() ) {
475 if ( d->model->data( currentIndex, Qt::DisplayRole ).toString() == currentTextLE )
476 alreadyInList =
true;
480 alreadyInList = d->model->stringList().contains( currentTextLE, Qt::CaseSensitive );
483 if ( d->servNewButton )
484 d->servNewButton->setEnabled(
false);
486 bool block = d->lineEdit->signalsBlocked();
487 d->lineEdit->blockSignals(
true);
488 d->lineEdit->clear();
489 d->lineEdit->blockSignals(block);
495 block = d->listView->signalsBlocked();
497 if ( currentIndex.isValid() ) {
498 d->model->setData(currentIndex, currentTextLE );
502 lst<<d->model->stringList();
503 d->model->setStringList(lst);
506 emit
added( currentTextLE );
509 d->updateButtonState();
514 QModelIndex selectedIndex = d->selectedIndex();
515 if ( selectedIndex.isValid() )
516 return selectedIndex.row();
521 void KEditListWidget::removeItem()
523 QModelIndex currentIndex = d->selectedIndex();
524 if ( !currentIndex.isValid() )
527 if ( currentIndex.row() >= 0 )
529 QString removedText = d->model->data( currentIndex, Qt::DisplayRole ).toString();
531 d->model->removeRows( currentIndex.row(), 1 );
533 d->listView->selectionModel()->clear();
540 d->updateButtonState();
543 void KEditListWidget::enableMoveButtons(
const QModelIndex &newIndex,
const QModelIndex&)
545 int index = newIndex.row();
551 bool moveEnabled = d->servUpButton && d->servDownButton;
555 if (d->model->rowCount() <= 1)
557 d->servUpButton->setEnabled(
false);
558 d->servDownButton->setEnabled(
false);
560 else if (index == (d->model->rowCount() - 1))
562 d->servUpButton->setEnabled(
true);
563 d->servDownButton->setEnabled(
false);
567 d->servUpButton->setEnabled(
false);
568 d->servDownButton->setEnabled(
true);
572 d->servUpButton->setEnabled(
true);
573 d->servDownButton->setEnabled(
true);
577 if ( d->servRemoveButton )
578 d->servRemoveButton->setEnabled(
true);
583 d->lineEdit->clear();
594 for (
int i = 0, j = index; i < list.count(); ++i, ++j )
595 content.insert( j, list[ i ] );
597 d->model->setStringList( content );
607 list.insert( index, text );
609 d->model->setStringList(list);
616 return list[ index ];
621 QModelIndex index = d->selectedIndex();
622 if ( !index.isValid() )
625 return text( index.row() );
630 return d->model->stringList();
635 d->model->setStringList(items);
643 void KEditListWidget::slotSelectionChanged(
const QItemSelection&,
const QItemSelection& )
645 d->updateButtonState();
646 QModelIndex index = d->selectedIndex();
647 enableMoveButtons(index, QModelIndex());
648 if (index.isValid()) {
649 d->lineEdit->setFocus( Qt::OtherFocusReason );
655 if (o == d->lineEdit && e->type() == QEvent::KeyPress ) {
656 QKeyEvent* keyEvent = (QKeyEvent*)e;
657 if (keyEvent->key() == Qt::Key_Down ||
658 keyEvent->key() == Qt::Key_Up) {
659 return ((
QObject*)d->listView)->event(e);
666 #include "keditlistwidget.moc"
QString i18n(const char *text)
A wrapper around QIcon that provides KDE icon features.
static void beep(const QString &reason=QString(), QWidget *widget=0L)
This is a simple substitution for QApplication::beep()
An enhanced QLineEdit widget for inputting text.