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 );
98 QVBoxLayout *loAv =
new QVBoxLayout();
99 lo->addLayout( loAv );
100 d->lAvailable =
new QLabel(
i18n(
"&Available:"),
this );
101 loAv->addWidget( d->lAvailable );
103 loAv->addWidget( d->availableListWidget );
104 d->lAvailable->setBuddy( d->availableListWidget );
106 QVBoxLayout *loHBtns =
new QVBoxLayout();
107 lo->addLayout( loHBtns );
108 loHBtns->addStretch( 1 );
110 loHBtns->addWidget( d->btnAdd );
112 loHBtns->addWidget( d->btnRemove );
113 loHBtns->addStretch( 1 );
115 QVBoxLayout *loS =
new QVBoxLayout();
116 lo->addLayout( loS );
117 d->lSelected =
new QLabel(
i18n(
"&Selected:"),
this );
118 loS->addWidget( d->lSelected );
120 loS->addWidget( d->selectedListWidget );
121 d->lSelected->setBuddy( d->selectedListWidget );
123 QVBoxLayout *loVBtns =
new QVBoxLayout();
124 lo->addLayout( loVBtns );
125 loVBtns->addStretch( 1 );
127 d->btnUp->setAutoRepeat(
true );
128 loVBtns->addWidget( d->btnUp );
130 d->btnDown->setAutoRepeat(
true );
131 loVBtns->addWidget( d->btnDown );
132 loVBtns->addStretch( 1 );
136 connect( d->btnAdd, SIGNAL(clicked()),
this, SLOT(buttonAddClicked()) );
137 connect( d->btnRemove, SIGNAL(clicked()),
this, SLOT(buttonRemoveClicked()) );
138 connect( d->btnUp, SIGNAL(clicked()),
this, SLOT(buttonUpClicked()) );
139 connect( d->btnDown, SIGNAL(clicked()),
this, SLOT(buttonDownClicked()) );
140 connect( d->availableListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
141 this, SLOT(itemDoubleClicked(QListWidgetItem*)) );
142 connect( d->selectedListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
143 this, SLOT(itemDoubleClicked(QListWidgetItem*)) );
144 connect( d->availableListWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(
polish()) );
145 connect( d->selectedListWidget, SIGNAL(itemSelectionChanged()),
this, SLOT(
polish()) );
147 d->availableListWidget->installEventFilter(
this );
148 d->selectedListWidget->installEventFilter(
this );
163 return d->availableListWidget;
168 return d->selectedListWidget;
177 d->btnAdd->setIcon(
KIcon( icon ) );
180 d->removeIcon = icon;
181 d->btnRemove->setIcon(
KIcon( icon ) );
185 d->btnUp->setIcon(
KIcon( icon ) );
189 d->btnDown->setIcon(
KIcon( icon ) );
192 kDebug(13001)<<
"KActionSelector::setButtonIcon: DAINBREAD!";
201 d->btnAdd->setIcon( iconset );
204 d->btnRemove->setIcon( iconset );
207 d->btnUp->setIcon( iconset );
210 d->btnDown->setIcon( iconset );
213 kDebug(13001)<<
"KActionSelector::setButtonIconSet: DAINBREAD!";
222 d->btnAdd->setText( tip );
223 d->btnAdd->setToolTip( tip );
226 d->btnRemove->setText( tip );
227 d->btnRemove->setToolTip( tip );
230 d->btnUp->setText( tip );
231 d->btnUp->setToolTip( tip );
234 d->btnDown->setText( tip );
235 d->btnDown->setToolTip( tip );
238 kDebug(13001)<<
"KActionSelector::setButtonToolTip: DAINBREAD!";
247 d->btnAdd->setWhatsThis(text );
250 d->btnRemove->setWhatsThis(text );
253 d->btnUp->setWhatsThis(text );
256 d->btnDown->setWhatsThis(text );
259 kDebug(13001)<<
"KActionSelector::setButtonWhatsThis: DAINBREAD!";
265 d->btnAdd->setEnabled( d->selectedRowIndex(d->availableListWidget) > -1 );
266 d->btnRemove->setEnabled( d->selectedRowIndex(d->selectedListWidget) > -1 );
267 d->btnUp->setEnabled( d->selectedRowIndex(d->selectedListWidget) > 0 );
268 d->btnDown->setEnabled( d->selectedRowIndex(d->selectedListWidget) > -1 &&
269 d->selectedRowIndex(d->selectedListWidget) < d->selectedListWidget->count() - 1 );
278 return d->moveOnDoubleClick;
283 d->moveOnDoubleClick = b;
288 return d->keyboardEnabled;
293 d->keyboardEnabled = b;
298 return d->lAvailable->text();
303 d->lAvailable->setText( text );
308 return d->lSelected->text();
313 d->lSelected->setText( text );
318 return d->availableInsertionPolicy;
323 d->availableInsertionPolicy = p;
328 return d->selectedInsertionPolicy;
333 d->selectedInsertionPolicy = p;
338 return d->showUpDownButtons;
343 d->showUpDownButtons = show;
370 if ( ! d->keyboardEnabled )
return;
371 if ( (e->modifiers() & Qt::ControlModifier) )
376 d->buttonAddClicked();
379 d->buttonRemoveClicked();
382 d->buttonUpClicked();
385 d->buttonDownClicked();
396 if ( d->keyboardEnabled && e->type() == QEvent::KeyPress )
398 if ( (((QKeyEvent*)e)->modifiers() & Qt::ControlModifier) )
400 switch ( ((QKeyEvent*)e)->key() )
403 d->buttonAddClicked();
406 d->buttonRemoveClicked();
409 d->buttonUpClicked();
412 d->buttonDownClicked();
415 return QWidget::eventFilter( o, e );
420 else if (
QListWidget *lb = qobject_cast<QListWidget*>(o) )
422 switch ( ((QKeyEvent*)e)->key() )
426 int index = lb->currentRow();
427 if ( index < 0 )
break;
428 d->moveItem( lb->item( index ) );
433 return QWidget::eventFilter( o, e );
440 void KActionSelectorPrivate::buttonAddClicked()
444 foreach (QListWidgetItem* item, list) {
445 availableListWidget->takeItem( availableListWidget->row( item ) );
446 selectedListWidget->insertItem( insertionIndex( selectedListWidget, selectedInsertionPolicy ), item );
447 selectedListWidget->setCurrentItem( item );
448 emit q->added( item );
451 selectedListWidget->sortItems();
452 selectedListWidget->setFocus();
455 void KActionSelectorPrivate::buttonRemoveClicked()
459 foreach (QListWidgetItem* item, list) {
460 selectedListWidget->takeItem( selectedListWidget->row( item ) );
461 availableListWidget->insertItem( insertionIndex( availableListWidget, availableInsertionPolicy ), item );
462 availableListWidget->setCurrentItem( item );
463 emit q->removed( item );
466 availableListWidget->sortItems();
467 availableListWidget->setFocus();
470 void KActionSelectorPrivate::buttonUpClicked()
472 int c = selectedRowIndex(selectedListWidget);
474 QListWidgetItem *item = selectedListWidget->item( c );
475 selectedListWidget->takeItem( c );
476 selectedListWidget->insertItem( c-1, item );
477 selectedListWidget->setCurrentItem( item );
478 emit q->movedUp( item );
481 void KActionSelectorPrivate::buttonDownClicked()
483 int c = selectedRowIndex(selectedListWidget);
484 if ( c < 0 || c == selectedListWidget->count() - 1 )
return;
485 QListWidgetItem *item = selectedListWidget->item( c );
486 selectedListWidget->takeItem( c );
487 selectedListWidget->insertItem( c+1, item );
488 selectedListWidget->setCurrentItem( item );
489 emit q->movedDown( item );
492 void KActionSelectorPrivate::itemDoubleClicked( QListWidgetItem *item )
494 if ( moveOnDoubleClick )
502 void KActionSelectorPrivate::loadIcons()
504 btnAdd->setIcon(
KIcon( addIcon ) );
505 btnRemove->setIcon(
KIcon( removeIcon ) );
506 btnUp->setIcon(
KIcon( upIcon ) );
507 btnDown->setIcon(
KIcon( downIcon ) );
510 void KActionSelectorPrivate::moveItem( QListWidgetItem *item )
514 if ( lbFrom == availableListWidget )
515 lbTo = selectedListWidget;
516 else if ( lbFrom == selectedListWidget )
517 lbTo = availableListWidget;
522 availableInsertionPolicy : selectedInsertionPolicy;
524 lbFrom->takeItem( lbFrom->row( item ) );
525 lbTo->insertItem( insertionIndex( lbTo, p ), item );
527 lbTo->setCurrentItem( item );
531 if ( lbTo == selectedListWidget )
532 emit q->added( item );
534 emit q->removed( item );
543 index = lb->currentRow();
544 if ( index > -1 ) index += 1;
555 int KActionSelectorPrivate::selectedRowIndex(
QListWidget *lb )
558 if (list.isEmpty()) {
561 return lb->row(list.at(0));
565 #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.