20 #include "tdeactionselector.h"
23 #include <kiconloader.h>
26 #include <tqapplication.h>
27 #include <tqlistbox.h>
28 #include <tqtoolbutton.h>
32 #include <tqwhatsthis.h>
34 class TDEActionSelectorPrivate {
36 TQListBox *availableListBox, *selectedListBox;
37 TQToolButton *btnAdd, *btnRemove, *btnUp, *btnDown;
38 TQLabel *lAvailable, *lSelected;
39 bool moveOnDoubleClick, keyboardEnabled;
41 TQString addIcon, removeIcon, upIcon, downIcon;
43 bool showUpDownButtons;
48 TDEActionSelector::TDEActionSelector( TQWidget *parent,
const char *name )
49 : TQWidget( parent,
name )
51 d =
new TDEActionSelectorPrivate();
52 d->moveOnDoubleClick =
true;
53 d->keyboardEnabled =
true;
54 d->iconSize = SmallIcon;
55 d->addIcon = TQApplication::reverseLayout()?
"back" :
"forward";
56 d->removeIcon = TQApplication::reverseLayout()?
"forward" :
"back";
58 d->downIcon =
"go-down";
59 d->availableInsertionPolicy = Sorted;
60 d->selectedInsertionPolicy = BelowCurrent;
61 d->showUpDownButtons =
true;
65 TQHBoxLayout *lo =
new TQHBoxLayout(
this );
68 TQVBoxLayout *loAv =
new TQVBoxLayout( lo );
69 d->lAvailable =
new TQLabel( i18n(
"&Available:"),
this );
70 loAv->addWidget( d->lAvailable );
71 d->availableListBox =
new TQListBox(
this );
72 loAv->addWidget( d->availableListBox );
73 d->lAvailable->setBuddy( d->availableListBox );
75 TQVBoxLayout *loHBtns =
new TQVBoxLayout( lo );
76 loHBtns->addStretch( 1 );
77 d->btnAdd =
new TQToolButton(
this );
78 loHBtns->addWidget( d->btnAdd );
79 d->btnRemove =
new TQToolButton(
this );
80 loHBtns->addWidget( d->btnRemove );
81 loHBtns->addStretch( 1 );
83 TQVBoxLayout *loS =
new TQVBoxLayout( lo );
84 d->lSelected =
new TQLabel( i18n(
"&Selected:"),
this );
85 loS->addWidget( d->lSelected );
86 d->selectedListBox =
new TQListBox(
this );
87 loS->addWidget( d->selectedListBox );
88 d->lSelected->setBuddy( d->selectedListBox );
90 TQVBoxLayout *loVBtns =
new TQVBoxLayout( lo );
91 loVBtns->addStretch( 1 );
92 d->btnUp =
new TQToolButton(
this );
93 d->btnUp->setAutoRepeat(
true );
94 loVBtns->addWidget( d->btnUp );
95 d->btnDown =
new TQToolButton(
this );
96 d->btnDown->setAutoRepeat(
true );
97 loVBtns->addWidget( d->btnDown );
98 loVBtns->addStretch( 1 );
102 connect( d->btnAdd, TQ_SIGNAL(clicked()),
this, TQ_SLOT(buttonAddClicked()) );
103 connect( d->btnRemove, TQ_SIGNAL(clicked()),
this, TQ_SLOT(buttonRemoveClicked()) );
104 connect( d->btnUp, TQ_SIGNAL(clicked()),
this, TQ_SLOT(buttonUpClicked()) );
105 connect( d->btnDown, TQ_SIGNAL(clicked()),
this, TQ_SLOT(buttonDownClicked()) );
106 connect( d->availableListBox, TQ_SIGNAL(doubleClicked(TQListBoxItem*)),
107 this, TQ_SLOT(itemDoubleClicked(TQListBoxItem*)) );
108 connect( d->selectedListBox, TQ_SIGNAL(doubleClicked(TQListBoxItem*)),
109 this, TQ_SLOT(itemDoubleClicked(TQListBoxItem*)) );
110 connect( d->availableListBox, TQ_SIGNAL(currentChanged(TQListBoxItem*)),
111 this, TQ_SLOT(slotCurrentChanged(TQListBoxItem *)) );
112 connect( d->selectedListBox, TQ_SIGNAL(currentChanged(TQListBoxItem*)),
113 this, TQ_SLOT(slotCurrentChanged(TQListBoxItem *)) );
115 d->availableListBox->installEventFilter(
this );
116 d->selectedListBox->installEventFilter(
this );
119 TDEActionSelector::~TDEActionSelector()
130 return d->availableListBox;
135 return d->selectedListBox;
144 d->btnAdd->setIconSet( SmallIconSet( icon, d->iconSize ) );
147 d->removeIcon = icon;
148 d->btnRemove->setIconSet( SmallIconSet( icon, d->iconSize ) );
152 d->btnUp->setIconSet( SmallIconSet( icon, d->iconSize ) );
156 d->btnDown->setIconSet( SmallIconSet( icon, d->iconSize ) );
159 kdDebug(13001)<<
"TDEActionSelector::setButtonIcon: DAINBREAD!"<<
endl;
168 d->btnAdd->setIconSet( iconset );
171 d->btnRemove->setIconSet( iconset );
174 d->btnUp->setIconSet( iconset );
177 d->btnDown->setIconSet( iconset );
180 kdDebug(13001)<<
"TDEActionSelector::setButtonIconSet: DAINBREAD!"<<
endl;
189 d->btnAdd->setTextLabel( tip );
192 d->btnRemove->setTextLabel( tip );
195 d->btnUp->setTextLabel( tip );
198 d->btnDown->setTextLabel( tip );
201 kdDebug(13001)<<
"TDEActionSelector::setButtonToolTip: DAINBREAD!"<<
endl;
210 TQWhatsThis::add( d->btnAdd, text );
213 TQWhatsThis::add( d->btnRemove, text );
216 TQWhatsThis::add( d->btnUp, text );
219 TQWhatsThis::add( d->btnDown, text );
222 kdDebug(13001)<<
"TDEActionSelector::setButtonWhatsThis: DAINBREAD!"<<
endl;
228 d->btnAdd->setEnabled( d->availableListBox->currentItem() > -1 );
229 d->btnRemove->setEnabled( d->selectedListBox->currentItem() > -1 );
230 d->btnUp->setEnabled( d->selectedListBox->currentItem() > 0 );
231 d->btnDown->setEnabled( d->selectedListBox->currentItem() > -1 &&
232 d->selectedListBox->currentItem() < (
int)d->selectedListBox->count() - 1 );
241 return d->moveOnDoubleClick;
246 d->moveOnDoubleClick = b;
251 return d->keyboardEnabled;
256 d->keyboardEnabled = b;
261 return d->lAvailable->text();
266 d->lAvailable->setText( text );
271 return d->lSelected->text();
276 d->lSelected->setText( text );
293 return d->availableInsertionPolicy;
298 d->availableInsertionPolicy = p;
303 return d->selectedInsertionPolicy;
308 d->selectedInsertionPolicy = p;
313 return d->showUpDownButtons;
318 d->showUpDownButtons = show;
345 if ( ! d->keyboardEnabled )
return;
346 if ( (e->state() & TQt::ControlButton) )
354 buttonRemoveClicked();
371 if ( d->keyboardEnabled && e->type() == TQEvent::KeyPress )
373 if ( (((TQKeyEvent*)e)->state() & TQt::ControlButton) )
375 switch ( ((TQKeyEvent*)e)->key() )
381 buttonRemoveClicked();
390 return TQWidget::eventFilter( o, e );
395 else if ( o->inherits(
"TQListBox" ) )
397 switch ( ((TQKeyEvent*)e)->key() )
401 TQListBox *lb = (TQListBox*)o;
402 int index = lb->currentItem();
403 if ( index < 0 )
break;
404 moveItem( lb->item( index ) );
409 return TQWidget::eventFilter( o, e );
416 void TDEActionSelector::buttonAddClicked()
419 TQListBoxItem *item = d->availableListBox->firstItem();
421 if ( item->isSelected() ) {
422 d->availableListBox->takeItem( item );
423 d->selectedListBox->insertItem( item, insertionIndex( d->selectedListBox, d->selectedInsertionPolicy ) );
424 d->selectedListBox->setCurrentItem( item );
426 item = d->availableListBox->firstItem();
430 if ( d->selectedInsertionPolicy == Sorted )
431 d->selectedListBox->sort();
432 d->selectedListBox->setFocus();
435 void TDEActionSelector::buttonRemoveClicked()
438 TQListBoxItem *item = d->selectedListBox->firstItem();
440 if ( item->isSelected() ) {
441 d->selectedListBox->takeItem( item );
442 d->availableListBox->insertItem( item, insertionIndex( d->availableListBox, d->availableInsertionPolicy ) );
443 d->availableListBox->setCurrentItem( item );
445 item = d->selectedListBox->firstItem();
449 if ( d->availableInsertionPolicy == Sorted )
450 d->availableListBox->sort();
451 d->availableListBox->setFocus();
454 void TDEActionSelector::buttonUpClicked()
456 int c = d->selectedListBox->currentItem();
458 TQListBoxItem *item = d->selectedListBox->item( c );
459 d->selectedListBox->takeItem( item );
460 d->selectedListBox->insertItem( item, c-1 );
461 d->selectedListBox->setCurrentItem( item );
465 void TDEActionSelector::buttonDownClicked()
467 int c = d->selectedListBox->currentItem();
468 if ( c < 0 || c ==
int( d->selectedListBox->count() ) - 1 )
return;
469 TQListBoxItem *item = d->selectedListBox->item( c );
470 d->selectedListBox->takeItem( item );
471 d->selectedListBox->insertItem( item, c+1 );
472 d->selectedListBox->setCurrentItem( item );
476 void TDEActionSelector::itemDoubleClicked( TQListBoxItem *item )
478 if ( d->moveOnDoubleClick )
486 void TDEActionSelector::loadIcons()
488 d->btnAdd->setIconSet( SmallIconSet( d->addIcon, d->iconSize ) );
489 d->btnRemove->setIconSet( SmallIconSet( d->removeIcon, d->iconSize ) );
490 d->btnUp->setIconSet( SmallIconSet( d->upIcon, d->iconSize ) );
491 d->btnDown->setIconSet( SmallIconSet( d->downIcon, d->iconSize ) );
494 void TDEActionSelector::moveItem( TQListBoxItem *item )
496 TQListBox *lbFrom = item->listBox();
498 if ( lbFrom == d->availableListBox )
499 lbTo = d->selectedListBox;
500 else if ( lbFrom == d->selectedListBox )
501 lbTo = d->availableListBox;
506 d->availableInsertionPolicy : d->selectedInsertionPolicy;
508 lbFrom->takeItem( item );
509 lbTo->insertItem( item, insertionIndex( lbTo, p ) );
511 lbTo->setCurrentItem( item );
515 if ( lbTo == d->selectedListBox )
521 int TDEActionSelector::insertionIndex( TQListBox *lb, InsertionPolicy policy )
527 index = lb->currentItem();
528 if ( index > -1 ) index += 1;
540 #include "tdeactionselector.moc"