25 #include <QtGui/QApplication>
26 #include <QtGui/QToolButton>
27 #include <QtGui/QLabel>
28 #include <QtGui/QLayout>
29 #include <QtGui/QActionEvent>
30 #include <QListWidget>
32 class KActionSelectorPrivate {
37 QListWidget *availableListWidget, *selectedListWidget;
39 QLabel *lAvailable, *lSelected;
40 bool moveOnDoubleClick : 1;
41 bool keyboardEnabled : 1;
42 bool showUpDownButtons : 1;
43 QString addIcon, removeIcon, upIcon, downIcon;
49 void moveItem( QListWidgetItem *item );
71 void buttonAddClicked();
72 void buttonRemoveClicked();
73 void buttonUpClicked();
74 void buttonDownClicked();
75 void itemDoubleClicked( QListWidgetItem *item );
76 void slotCurrentChanged( QListWidgetItem * )
77 { q->setButtonsEnabled(); }
84 , d( new KActionSelectorPrivate(this) )
86 d->moveOnDoubleClick =
true;
87 d->keyboardEnabled =
true;
88 d->addIcon = QApplication::isRightToLeft()?
"go-previous" :
"go-next";
89 d->removeIcon = QApplication::isRightToLeft()?
"go-next" :
"go-previous";
91 d->downIcon =
"go-down";
92 d->availableInsertionPolicy =
Sorted;
94 d->showUpDownButtons =
true;
96 QHBoxLayout *lo =
new QHBoxLayout(
this );
99 QVBoxLayout *loAv =
new QVBoxLayout();
100 lo->addLayout( loAv );
101 d->lAvailable =
new QLabel(
i18n(
"&Available:"),
this );
102 loAv->addWidget( d->lAvailable );
104 loAv->addWidget( d->availableListWidget );
105 d->lAvailable->setBuddy( d->availableListWidget );
107 QVBoxLayout *loHBtns =
new QVBoxLayout();
108 lo->addLayout( loHBtns );
109 loHBtns->addStretch( 1 );
111 loHBtns->addWidget( d->btnAdd );
113 loHBtns->addWidget( d->btnRemove );
114 loHBtns->addStretch( 1 );
116 QVBoxLayout *loS =
new QVBoxLayout();
117 lo->addLayout( loS );
118 d->lSelected =
new QLabel(
i18n(
"&Selected:"),
this );
119 loS->addWidget( d->lSelected );
121 loS->addWidget( d->selectedListWidget );
122 d->lSelected->setBuddy( d->selectedListWidget );
124 QVBoxLayout *loVBtns =
new QVBoxLayout();
125 lo->addLayout( loVBtns );
126 loVBtns->addStretch( 1 );
128 d->btnUp->setAutoRepeat(
true );
129 loVBtns->addWidget( d->btnUp );
131 d->btnDown->setAutoRepeat(
true );
132 loVBtns->addWidget( d->btnDown );
133 loVBtns->addStretch( 1 );
137 connect( d->btnAdd, SIGNAL(clicked()),
this, SLOT(buttonAddClicked()) );
138 connect( d->btnRemove, SIGNAL(clicked()),
this, SLOT(buttonRemoveClicked()) );
139 connect( d->btnUp, SIGNAL(clicked()),
this, SLOT(buttonUpClicked()) );
140 connect( d->btnDown, SIGNAL(clicked()),
this, SLOT(buttonDownClicked()) );
141 connect( d->availableListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
142 this, SLOT(itemDoubleClicked(QListWidgetItem*)) );
143 connect( d->selectedListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
144 this, SLOT(itemDoubleClicked(QListWidgetItem*)) );
145 connect( d->availableListWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(
polish()) );
146 connect( d->selectedListWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(
polish()) );
148 d->availableListWidget->installEventFilter(
this );
149 d->selectedListWidget->installEventFilter(
this );
164 return d->availableListWidget;
169 return d->selectedListWidget;
178 d->btnAdd->setIcon(
KIcon( icon ) );
181 d->removeIcon = icon;
182 d->btnRemove->setIcon(
KIcon( icon ) );
186 d->btnUp->setIcon(
KIcon( icon ) );
190 d->btnDown->setIcon(
KIcon( icon ) );
193 kDebug(13001)<<
"KActionSelector::setButtonIcon: DAINBREAD!";
202 d->btnAdd->setIcon( iconset );
205 d->btnRemove->setIcon( iconset );
208 d->btnUp->setIcon( iconset );
211 d->btnDown->setIcon( iconset );
214 kDebug(13001)<<
"KActionSelector::setButtonIconSet: DAINBREAD!";
223 d->btnAdd->setText( tip );
224 d->btnAdd->setToolTip( tip );
227 d->btnRemove->setText( tip );
228 d->btnRemove->setToolTip( tip );
231 d->btnUp->setText( tip );
232 d->btnUp->setToolTip( tip );
235 d->btnDown->setText( tip );
236 d->btnDown->setToolTip( tip );
239 kDebug(13001)<<
"KActionSelector::setButtonToolTip: DAINBREAD!";
248 d->btnAdd->setWhatsThis(text );
251 d->btnRemove->setWhatsThis(text );
254 d->btnUp->setWhatsThis(text );
257 d->btnDown->setWhatsThis(text );
260 kDebug(13001)<<
"KActionSelector::setButtonWhatsThis: DAINBREAD!";
266 d->btnAdd->setEnabled( d->selectedRowIndex(d->availableListWidget) > -1 );
267 d->btnRemove->setEnabled( d->selectedRowIndex(d->selectedListWidget) > -1 );
268 d->btnUp->setEnabled( d->selectedRowIndex(d->selectedListWidget) > 0 );
269 d->btnDown->setEnabled( d->selectedRowIndex(d->selectedListWidget) > -1 &&
270 d->selectedRowIndex(d->selectedListWidget) < d->selectedListWidget->count() - 1 );
279 return d->moveOnDoubleClick;
284 d->moveOnDoubleClick = b;
289 return d->keyboardEnabled;
294 d->keyboardEnabled = b;
299 return d->lAvailable->text();
304 d->lAvailable->setText( text );
309 return d->lSelected->text();
314 d->lSelected->setText( text );
319 return d->availableInsertionPolicy;
324 d->availableInsertionPolicy = p;
329 return d->selectedInsertionPolicy;
334 d->selectedInsertionPolicy = p;
339 return d->showUpDownButtons;
344 d->showUpDownButtons = show;
371 if ( ! d->keyboardEnabled )
return;
372 if ( (e->modifiers() & Qt::ControlModifier) )
377 d->buttonAddClicked();
380 d->buttonRemoveClicked();
383 d->buttonUpClicked();
386 d->buttonDownClicked();
397 if ( d->keyboardEnabled && e->type() == QEvent::KeyPress )
399 if ( (((QKeyEvent*)e)->modifiers() & Qt::ControlModifier) )
401 switch ( ((QKeyEvent*)e)->key() )
404 d->buttonAddClicked();
407 d->buttonRemoveClicked();
410 d->buttonUpClicked();
413 d->buttonDownClicked();
416 return QWidget::eventFilter( o, e );
421 else if (
QListWidget *lb = qobject_cast<QListWidget*>(o) )
423 switch ( ((QKeyEvent*)e)->key() )
427 int index = lb->currentRow();
428 if ( index < 0 )
break;
429 d->moveItem( lb->item( index ) );
434 return QWidget::eventFilter( o, e );
441 void KActionSelectorPrivate::buttonAddClicked()
445 foreach (QListWidgetItem* item, list) {
446 availableListWidget->takeItem( availableListWidget->row( item ) );
447 selectedListWidget->insertItem( insertionIndex( selectedListWidget, selectedInsertionPolicy ), item );
448 selectedListWidget->setCurrentItem( item );
449 emit q->added( item );
452 selectedListWidget->sortItems();
453 selectedListWidget->setFocus();
456 void KActionSelectorPrivate::buttonRemoveClicked()
460 foreach (QListWidgetItem* item, list) {
461 selectedListWidget->takeItem( selectedListWidget->row( item ) );
462 availableListWidget->insertItem( insertionIndex( availableListWidget, availableInsertionPolicy ), item );
463 availableListWidget->setCurrentItem( item );
464 emit q->removed( item );
467 availableListWidget->sortItems();
468 availableListWidget->setFocus();
471 void KActionSelectorPrivate::buttonUpClicked()
473 int c = selectedRowIndex(selectedListWidget);
475 QListWidgetItem *item = selectedListWidget->item( c );
476 selectedListWidget->takeItem( c );
477 selectedListWidget->insertItem( c-1, item );
478 selectedListWidget->setCurrentItem( item );
479 emit q->movedUp( item );
482 void KActionSelectorPrivate::buttonDownClicked()
484 int c = selectedRowIndex(selectedListWidget);
485 if ( c < 0 || c == selectedListWidget->count() - 1 )
return;
486 QListWidgetItem *item = selectedListWidget->item( c );
487 selectedListWidget->takeItem( c );
488 selectedListWidget->insertItem( c+1, item );
489 selectedListWidget->setCurrentItem( item );
490 emit q->movedDown( item );
493 void KActionSelectorPrivate::itemDoubleClicked( QListWidgetItem *item )
495 if ( moveOnDoubleClick )
503 void KActionSelectorPrivate::loadIcons()
505 btnAdd->setIcon(
KIcon( addIcon ) );
506 btnRemove->setIcon(
KIcon( removeIcon ) );
507 btnUp->setIcon(
KIcon( upIcon ) );
508 btnDown->setIcon(
KIcon( downIcon ) );
511 void KActionSelectorPrivate::moveItem( QListWidgetItem *item )
515 if ( lbFrom == availableListWidget )
516 lbTo = selectedListWidget;
517 else if ( lbFrom == selectedListWidget )
518 lbTo = availableListWidget;
523 availableInsertionPolicy : selectedInsertionPolicy;
525 lbFrom->takeItem( lbFrom->row( item ) );
526 lbTo->insertItem( insertionIndex( lbTo, p ), item );
528 lbTo->setCurrentItem( item );
532 if ( lbTo == selectedListWidget )
533 emit q->added( item );
535 emit q->removed( item );
544 index = lb->currentRow();
545 if ( index > -1 ) index += 1;
556 int KActionSelectorPrivate::selectedRowIndex(
QListWidget *lb )
559 if (list.isEmpty()) {
562 return lb->row(list.at(0));
566 #include "kactionselector.moc"
QString i18n(const char *text)
void setButtonWhatsThis(const QString &text, MoveButton button)
Sets the whatsthis help for button button to text.
void setButtonsEnabled()
Sets the enabled state of all moving buttons to reflect the current options.
void keyPressEvent(QKeyEvent *)
Reimplamented for internal reasons.
KActionSelector(QWidget *parent=0)
void setAvailableInsertionPolicy(InsertionPolicy policy)
Sets the insertion policy for the available listbox.
InsertionPolicy
This enum defines policies for where to insert moved items in a listbox.
void polish()
Emitted when an item is moved to the "selected" listbox.
bool moveOnDoubleClick() const
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
void setButtonIconSet(const QIcon &iconset, MoveButton button)
Sets the iconset for button button to iconset.
void setAvailableLabel(const QString &text)
Sets the label for the available items listbox to text.
QListWidget * availableListWidget() const
void setMoveOnDoubleClick(bool enable)
Sets moveOnDoubleClick to enable.
void setKeyboardEnabled(bool enable)
Sets the keyboard enabled depending on enable.
void setSelectedInsertionPolicy(InsertionPolicy policy)
Sets the insertion policy for the selected listbox.
void setButtonIcon(const QString &icon, MoveButton button)
Sets the pixmap of the button button to icon.
void setShowUpDownButtons(bool show)
Sets whether the Up and Down buttons should be displayed according to show.
A wrapper around QIcon that provides KDE icon features.
InsertionPolicy selectedInsertionPolicy() const
void setButtonTooltip(const QString &tip, MoveButton button)
Sets the tooltip for the button button to tip.
InsertionPolicy availableInsertionPolicy() const
QString selectedLabel() const
bool keyboardEnabled() const
QListWidget * selectedListWidget() const
bool eventFilter(QObject *, QEvent *)
Reimplemented for internal reasons.
bool showUpDownButtons() const
QString availableLabel() const
A widget for selecting and arranging actions/objects.
void setSelectedLabel(const QString &text)
Sets the label for the selected items listbox to text.
MoveButton
This enum indentifies the moving buttons.