1
/* This file is part of the KDE project
2
Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16
Boston, MA 02111-1307, USA.
20
#include "kactionselector.h"
23
#include <kiconloader.h>
24
#include <kdialog.h> // for spacingHint()
26
#include <qapplication.h>
29
#include <qtoolbutton.h>
33
#include <qwhatsthis.h>
35
class KActionSelectorPrivate {
37
QListBox *availableListBox, *selectedListBox;
38
QToolButton *btnAdd, *btnRemove, *btnUp, *btnDown;
39
QLabel *lAvailable, *lSelected;
40
bool moveOnDoubleClick, keyboardEnabled;
41
KActionSelector::ButtonIconSize iconSize;
42
QString addIcon, removeIcon, upIcon, downIcon;
43
KActionSelector::InsertionPolicy availableInsertionPolicy, selectedInsertionPolicy;
44
bool showUpDownButtons;
47
//BEGIN Constructor/destructor
49
KActionSelector::KActionSelector( QWidget *parent, const char *name )
50
: QWidget( parent, name )
52
d = new KActionSelectorPrivate();
53
d->moveOnDoubleClick = true;
54
d->keyboardEnabled = true;
55
d->iconSize = SmallIcon;
56
d->addIcon = QApplication::reverseLayout() ? "back" : "forward";
57
d->removeIcon = QApplication::reverseLayout() ? "forward" : "back";
60
d->availableInsertionPolicy = Sorted;
61
d->selectedInsertionPolicy = BelowCurrent;
62
d->showUpDownButtons = true;
64
//int isz = IconSize( KIcon::Small );
66
QHBoxLayout *lo = new QHBoxLayout( this );
67
lo->setSpacing( KDialog::spacingHint() );
69
QVBoxLayout *loAv = new QVBoxLayout( lo );
70
d->lAvailable = new QLabel( i18n("&Available:"), this );
71
loAv->addWidget( d->lAvailable );
72
d->availableListBox = new QListBox( this );
73
loAv->addWidget( d->availableListBox );
74
d->lAvailable->setBuddy( d->availableListBox );
76
QVBoxLayout *loHBtns = new QVBoxLayout( lo );
77
loHBtns->addStretch( 1 );
78
d->btnAdd = new QToolButton( this );
79
loHBtns->addWidget( d->btnAdd );
80
d->btnRemove = new QToolButton( this );
81
loHBtns->addWidget( d->btnRemove );
82
loHBtns->addStretch( 1 );
84
QVBoxLayout *loS = new QVBoxLayout( lo );
85
d->lSelected = new QLabel( i18n("&Selected:"), this );
86
loS->addWidget( d->lSelected );
87
d->selectedListBox = new QListBox( this );
88
loS->addWidget( d->selectedListBox );
89
d->lSelected->setBuddy( d->selectedListBox );
91
QVBoxLayout *loVBtns = new QVBoxLayout( lo );
92
loVBtns->addStretch( 1 );
93
d->btnUp = new QToolButton( this );
94
loVBtns->addWidget( d->btnUp );
95
d->btnDown = new QToolButton( this );
96
loVBtns->addWidget( d->btnDown );
97
loVBtns->addStretch( 1 );
101
connect( d->btnAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()) );
102
connect( d->btnRemove, SIGNAL(clicked()), this, SLOT(buttonRemoveClicked()) );
103
connect( d->btnUp, SIGNAL(clicked()), this, SLOT(buttonUpClicked()) );
104
connect( d->btnDown, SIGNAL(clicked()), this, SLOT(buttonDownClicked()) );
105
connect( d->availableListBox, SIGNAL(doubleClicked(QListBoxItem*)),
106
this, SLOT(itemDoubleClicked(QListBoxItem*)) );
107
connect( d->selectedListBox, SIGNAL(doubleClicked(QListBoxItem*)),
108
this, SLOT(itemDoubleClicked(QListBoxItem*)) );
109
connect( d->availableListBox, SIGNAL(currentChanged(QListBoxItem*)),
110
this, SLOT(slotCurrentChanged(QListBoxItem *)) );
111
connect( d->selectedListBox, SIGNAL(currentChanged(QListBoxItem*)),
112
this, SLOT(slotCurrentChanged(QListBoxItem *)) );
114
d->availableListBox->installEventFilter( this );
115
d->selectedListBox->installEventFilter( this );
118
KActionSelector::~KActionSelector()
123
//END Constructor/destroctor
125
//BEGIN Public Methods
127
QListBox *KActionSelector::availableListBox()
129
return d->availableListBox;
132
QListBox *KActionSelector::selectedListBox()
134
return d->selectedListBox;
137
void KActionSelector::setButtonIcon( const QString &icon, MoveButton button )
143
d->btnAdd->setIconSet( SmallIconSet( icon, d->iconSize ) );
146
d->removeIcon = icon;
147
d->btnRemove->setIconSet( SmallIconSet( icon, d->iconSize ) );
151
d->btnUp->setIconSet( SmallIconSet( icon, d->iconSize ) );
155
d->btnDown->setIconSet( SmallIconSet( icon, d->iconSize ) );
158
kdDebug()<<"KActionSelector::setButtonIcon: DAINBREAD!"<<endl;
162
void KActionSelector::setButtonIconSet( const QIconSet &iconset, MoveButton button )
167
d->btnAdd->setIconSet( iconset );
170
d->btnRemove->setIconSet( iconset );
173
d->btnUp->setIconSet( iconset );
176
d->btnDown->setIconSet( iconset );
179
kdDebug()<<"KActionSelector::setButtonIconSet: DAINBREAD!"<<endl;
183
void KActionSelector::setButtonTooltip( const QString &tip, MoveButton button )
188
d->btnAdd->setTextLabel( tip );
191
d->btnRemove->setTextLabel( tip );
194
d->btnUp->setTextLabel( tip );
197
d->btnDown->setTextLabel( tip );
200
kdDebug()<<"KActionSelector::setButtonToolTip: DAINBREAD!"<<endl;
204
void KActionSelector::setButtonWhatsThis( const QString &text, MoveButton button )
209
QWhatsThis::add( d->btnAdd, text );
212
QWhatsThis::add( d->btnRemove, text );
215
QWhatsThis::add( d->btnUp, text );
218
QWhatsThis::add( d->btnDown, text );
221
kdDebug()<<"KActionSelector::setButtonWhatsThis: DAINBREAD!"<<endl;
225
void KActionSelector::setButtonsEnabled()
227
d->btnAdd->setEnabled( d->availableListBox->currentItem() > -1 );
228
d->btnRemove->setEnabled( d->selectedListBox->currentItem() > -1 );
229
d->btnUp->setEnabled( d->selectedListBox->currentItem() > 0 );
230
d->btnDown->setEnabled( d->selectedListBox->currentItem() > -1 &&
231
d->selectedListBox->currentItem() < (int)d->selectedListBox->count() - 1 );
238
bool KActionSelector::moveOnDoubleClick() const
240
return d->moveOnDoubleClick;
243
void KActionSelector::setMoveOnDoubleClick( bool b )
245
d->moveOnDoubleClick = b;
248
bool KActionSelector::keyboardEnabled() const
250
return d->keyboardEnabled;
253
void KActionSelector::setKeyboardEnabled( bool b )
255
d->keyboardEnabled = b;
258
QString KActionSelector::availableLabel() const
260
return d->lAvailable->text();
263
void KActionSelector::setAvailableLabel( const QString &text )
265
d->lAvailable->setText( text );
268
QString KActionSelector::selectedLabel() const
270
return d->lSelected->text();
273
void KActionSelector::setSelectedLabel( const QString &text )
275
d->lSelected->setText( text );
278
KActionSelector::ButtonIconSize KActionSelector::buttonIconSize() const
283
void KActionSelector::setButtonIconSize( ButtonIconSize size )
290
KActionSelector::InsertionPolicy KActionSelector::availableInsertionPolicy() const
292
return d->availableInsertionPolicy;
295
void KActionSelector::setAvailableInsertionPolicy( InsertionPolicy p )
297
d->availableInsertionPolicy = p;
300
KActionSelector::InsertionPolicy KActionSelector::selectedInsertionPolicy() const
302
return d->selectedInsertionPolicy;
305
void KActionSelector::setSelectedInsertionPolicy( InsertionPolicy p )
307
d->selectedInsertionPolicy = p;
310
bool KActionSelector::showUpDownButtons() const
312
return d->showUpDownButtons;
315
void KActionSelector::setShowUpDownButtons( bool show )
317
d->showUpDownButtons = show;
334
void KActionSelector::polish()
342
void KActionSelector::keyPressEvent( QKeyEvent *e )
344
if ( ! d->keyboardEnabled ) return;
345
if ( (e->state() & Qt::ControlButton) )
353
buttonRemoveClicked();
368
bool KActionSelector::eventFilter( QObject *o, QEvent *e )
370
if ( d->keyboardEnabled && e->type() == QEvent::KeyPress )
372
if ( (((QKeyEvent*)e)->state() & Qt::ControlButton) )
374
switch ( ((QKeyEvent*)e)->key() )
380
buttonRemoveClicked();
389
return QWidget::eventFilter( o, e );
394
else if ( o->inherits( "QListBox" ) )
396
switch ( ((QKeyEvent*)e)->key() )
400
QListBox *lb = (QListBox*)o;
401
int index = lb->currentItem();
402
if ( index < 0 ) break;
403
moveItem( lb->item( index ) );
408
return QWidget::eventFilter( o, e );
413
//BEGIN Private Slots
415
void KActionSelector::buttonAddClicked()
417
// move all selected items from available to selected listbox
418
QListBoxItem *item = d->availableListBox->firstItem();
420
if ( item->isSelected() ) {
421
d->availableListBox->takeItem( item );
422
d->selectedListBox->insertItem( item, insertionIndex( d->selectedListBox, d->selectedInsertionPolicy ) );
423
d->selectedListBox->setCurrentItem( item );
428
if ( d->selectedInsertionPolicy == Sorted )
429
d->selectedListBox->sort();
430
d->selectedListBox->setFocus();
433
void KActionSelector::buttonRemoveClicked()
435
// move all selected items from selected to available listbox
436
QListBoxItem *item = d->selectedListBox->firstItem();
438
if ( item->isSelected() ) {
439
d->selectedListBox->takeItem( item );
440
d->availableListBox->insertItem( item, insertionIndex( d->availableListBox, d->availableInsertionPolicy ) );
441
d->availableListBox->setCurrentItem( item );
442
emit removed( item );
446
if ( d->availableInsertionPolicy == Sorted )
447
d->availableListBox->sort();
448
d->availableListBox->setFocus();
451
void KActionSelector::buttonUpClicked()
453
int c = d->selectedListBox->currentItem();
455
QListBoxItem *item = d->selectedListBox->item( c );
456
d->selectedListBox->takeItem( item );
457
d->selectedListBox->insertItem( item, c-1 );
458
d->selectedListBox->setCurrentItem( item );
459
emit movedUp( item );
462
void KActionSelector::buttonDownClicked()
464
int c = d->selectedListBox->currentItem();
466
QListBoxItem *item = d->selectedListBox->item( c );
467
d->selectedListBox->takeItem( item );
468
d->selectedListBox->insertItem( item, c+1 );
469
d->selectedListBox->setCurrentItem( item );
470
emit movedDown( item );
473
void KActionSelector::itemDoubleClicked( QListBoxItem *item )
475
if ( d->moveOnDoubleClick )
481
//BEGIN Private Methods
483
void KActionSelector::loadIcons()
485
d->btnAdd->setIconSet( SmallIconSet( d->addIcon, d->iconSize ) );
486
d->btnRemove->setIconSet( SmallIconSet( d->removeIcon, d->iconSize ) );
487
d->btnUp->setIconSet( SmallIconSet( d->upIcon, d->iconSize ) );
488
d->btnDown->setIconSet( SmallIconSet( d->downIcon, d->iconSize ) );
491
void KActionSelector::moveItem( QListBoxItem *item )
493
QListBox *lbFrom = item->listBox();
495
if ( lbFrom == d->availableListBox )
496
lbTo = d->selectedListBox;
497
else if ( lbFrom == d->selectedListBox )
498
lbTo = d->availableListBox;
499
else //?! somewhat unlikely...
502
InsertionPolicy p = ( lbTo == d->availableListBox ) ?
503
d->availableInsertionPolicy : d->selectedInsertionPolicy;
505
lbFrom->takeItem( item );
506
lbTo->insertItem( item, insertionIndex( lbTo, p ) );
508
lbTo->setCurrentItem( item );
512
if ( lbTo == d->selectedListBox )
515
emit removed( item );
518
int KActionSelector::insertionIndex( QListBox *lb, InsertionPolicy policy )
524
index = lb->currentItem();
525
if ( index > -1 ) index += 1;
536
//END Private Methods
537
#include "kactionselector.moc"