kdeui Library API Documentation

kactionselector.cpp

00001 /* This file is part of the KDE project 00002 Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 00019 00020 #include "kactionselector.h" 00021 00022 #include <klocale.h> 00023 #include <kiconloader.h> 00024 #include <kdialog.h> // for spacingHint() 00025 #include <kdebug.h> 00026 #include <qapplication.h> 00027 #include <qlistbox.h> 00028 #include <qtoolbutton.h> 00029 #include <qlabel.h> 00030 #include <qlayout.h> 00031 #include <qevent.h> 00032 #include <qwhatsthis.h> 00033 00034 class KActionSelectorPrivate { 00035 public: 00036 QListBox *availableListBox, *selectedListBox; 00037 QToolButton *btnAdd, *btnRemove, *btnUp, *btnDown; 00038 QLabel *lAvailable, *lSelected; 00039 bool moveOnDoubleClick, keyboardEnabled; 00040 KActionSelector::ButtonIconSize iconSize; 00041 QString addIcon, removeIcon, upIcon, downIcon; 00042 KActionSelector::InsertionPolicy availableInsertionPolicy, selectedInsertionPolicy; 00043 bool showUpDownButtons; 00044 }; 00045 00046 //BEGIN Constructor/destructor 00047 00048 KActionSelector::KActionSelector( QWidget *parent, const char *name ) 00049 : QWidget( parent, name ) 00050 { 00051 d = new KActionSelectorPrivate(); 00052 d->moveOnDoubleClick = true; 00053 d->keyboardEnabled = true; 00054 d->iconSize = SmallIcon; 00055 d->addIcon = QApplication::reverseLayout()? "back" : "forward"; 00056 d->removeIcon = QApplication::reverseLayout()? "forward" : "back"; 00057 d->upIcon = "up"; 00058 d->downIcon = "down"; 00059 d->availableInsertionPolicy = Sorted; 00060 d->selectedInsertionPolicy = BelowCurrent; 00061 d->showUpDownButtons = true; 00062 00063 //int isz = IconSize( KIcon::Small ); 00064 00065 QHBoxLayout *lo = new QHBoxLayout( this ); 00066 lo->setSpacing( KDialog::spacingHint() ); 00067 00068 QVBoxLayout *loAv = new QVBoxLayout( lo ); 00069 d->lAvailable = new QLabel( i18n("&Available:"), this ); 00070 loAv->addWidget( d->lAvailable ); 00071 d->availableListBox = new QListBox( this ); 00072 loAv->addWidget( d->availableListBox ); 00073 d->lAvailable->setBuddy( d->availableListBox ); 00074 00075 QVBoxLayout *loHBtns = new QVBoxLayout( lo ); 00076 loHBtns->addStretch( 1 ); 00077 d->btnAdd = new QToolButton( this ); 00078 loHBtns->addWidget( d->btnAdd ); 00079 d->btnRemove = new QToolButton( this ); 00080 loHBtns->addWidget( d->btnRemove ); 00081 loHBtns->addStretch( 1 ); 00082 00083 QVBoxLayout *loS = new QVBoxLayout( lo ); 00084 d->lSelected = new QLabel( i18n("&Selected:"), this ); 00085 loS->addWidget( d->lSelected ); 00086 d->selectedListBox = new QListBox( this ); 00087 loS->addWidget( d->selectedListBox ); 00088 d->lSelected->setBuddy( d->selectedListBox ); 00089 00090 QVBoxLayout *loVBtns = new QVBoxLayout( lo ); 00091 loVBtns->addStretch( 1 ); 00092 d->btnUp = new QToolButton( this ); 00093 d->btnUp->setAutoRepeat( true ); 00094 loVBtns->addWidget( d->btnUp ); 00095 d->btnDown = new QToolButton( this ); 00096 d->btnDown->setAutoRepeat( true ); 00097 loVBtns->addWidget( d->btnDown ); 00098 loVBtns->addStretch( 1 ); 00099 00100 loadIcons(); 00101 00102 connect( d->btnAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()) ); 00103 connect( d->btnRemove, SIGNAL(clicked()), this, SLOT(buttonRemoveClicked()) ); 00104 connect( d->btnUp, SIGNAL(clicked()), this, SLOT(buttonUpClicked()) ); 00105 connect( d->btnDown, SIGNAL(clicked()), this, SLOT(buttonDownClicked()) ); 00106 connect( d->availableListBox, SIGNAL(doubleClicked(QListBoxItem*)), 00107 this, SLOT(itemDoubleClicked(QListBoxItem*)) ); 00108 connect( d->selectedListBox, SIGNAL(doubleClicked(QListBoxItem*)), 00109 this, SLOT(itemDoubleClicked(QListBoxItem*)) ); 00110 connect( d->availableListBox, SIGNAL(currentChanged(QListBoxItem*)), 00111 this, SLOT(slotCurrentChanged(QListBoxItem *)) ); 00112 connect( d->selectedListBox, SIGNAL(currentChanged(QListBoxItem*)), 00113 this, SLOT(slotCurrentChanged(QListBoxItem *)) ); 00114 00115 d->availableListBox->installEventFilter( this ); 00116 d->selectedListBox->installEventFilter( this ); 00117 } 00118 00119 KActionSelector::~KActionSelector() 00120 { 00121 } 00122 00123 //END Constructor/destroctor 00124 00125 //BEGIN Public Methods 00126 00127 QListBox *KActionSelector::availableListBox() const 00128 { 00129 return d->availableListBox; 00130 } 00131 00132 QListBox *KActionSelector::selectedListBox() const 00133 { 00134 return d->selectedListBox; 00135 } 00136 00137 void KActionSelector::setButtonIcon( const QString &icon, MoveButton button ) 00138 { 00139 switch ( button ) 00140 { 00141 case ButtonAdd: 00142 d->addIcon = icon; 00143 d->btnAdd->setIconSet( SmallIconSet( icon, d->iconSize ) ); 00144 break; 00145 case ButtonRemove: 00146 d->removeIcon = icon; 00147 d->btnRemove->setIconSet( SmallIconSet( icon, d->iconSize ) ); 00148 break; 00149 case ButtonUp: 00150 d->upIcon = icon; 00151 d->btnUp->setIconSet( SmallIconSet( icon, d->iconSize ) ); 00152 break; 00153 case ButtonDown: 00154 d->downIcon = icon; 00155 d->btnDown->setIconSet( SmallIconSet( icon, d->iconSize ) ); 00156 break; 00157 default: 00158 kdDebug(13001)<<"KActionSelector::setButtonIcon: DAINBREAD!"<<endl; 00159 } 00160 } 00161 00162 void KActionSelector::setButtonIconSet( const QIconSet &iconset, MoveButton button ) 00163 { 00164 switch ( button ) 00165 { 00166 case ButtonAdd: 00167 d->btnAdd->setIconSet( iconset ); 00168 break; 00169 case ButtonRemove: 00170 d->btnRemove->setIconSet( iconset ); 00171 break; 00172 case ButtonUp: 00173 d->btnUp->setIconSet( iconset ); 00174 break; 00175 case ButtonDown: 00176 d->btnDown->setIconSet( iconset ); 00177 break; 00178 default: 00179 kdDebug(13001)<<"KActionSelector::setButtonIconSet: DAINBREAD!"<<endl; 00180 } 00181 } 00182 00183 void KActionSelector::setButtonTooltip( const QString &tip, MoveButton button ) 00184 { 00185 switch ( button ) 00186 { 00187 case ButtonAdd: 00188 d->btnAdd->setTextLabel( tip ); 00189 break; 00190 case ButtonRemove: 00191 d->btnRemove->setTextLabel( tip ); 00192 break; 00193 case ButtonUp: 00194 d->btnUp->setTextLabel( tip ); 00195 break; 00196 case ButtonDown: 00197 d->btnDown->setTextLabel( tip ); 00198 break; 00199 default: 00200 kdDebug(13001)<<"KActionSelector::setButtonToolTip: DAINBREAD!"<<endl; 00201 } 00202 } 00203 00204 void KActionSelector::setButtonWhatsThis( const QString &text, MoveButton button ) 00205 { 00206 switch ( button ) 00207 { 00208 case ButtonAdd: 00209 QWhatsThis::add( d->btnAdd, text ); 00210 break; 00211 case ButtonRemove: 00212 QWhatsThis::add( d->btnRemove, text ); 00213 break; 00214 case ButtonUp: 00215 QWhatsThis::add( d->btnUp, text ); 00216 break; 00217 case ButtonDown: 00218 QWhatsThis::add( d->btnDown, text ); 00219 break; 00220 default: 00221 kdDebug(13001)<<"KActionSelector::setButtonWhatsThis: DAINBREAD!"<<endl; 00222 } 00223 } 00224 00225 void KActionSelector::setButtonsEnabled() 00226 { 00227 d->btnAdd->setEnabled( d->availableListBox->currentItem() > -1 ); 00228 d->btnRemove->setEnabled( d->selectedListBox->currentItem() > -1 ); 00229 d->btnUp->setEnabled( d->selectedListBox->currentItem() > 0 ); 00230 d->btnDown->setEnabled( d->selectedListBox->currentItem() > -1 && 00231 d->selectedListBox->currentItem() < (int)d->selectedListBox->count() - 1 ); 00232 } 00233 00234 //END Public Methods 00235 00236 //BEGIN Properties 00237 00238 bool KActionSelector::moveOnDoubleClick() const 00239 { 00240 return d->moveOnDoubleClick; 00241 } 00242 00243 void KActionSelector::setMoveOnDoubleClick( bool b ) 00244 { 00245 d->moveOnDoubleClick = b; 00246 } 00247 00248 bool KActionSelector::keyboardEnabled() const 00249 { 00250 return d->keyboardEnabled; 00251 } 00252 00253 void KActionSelector::setKeyboardEnabled( bool b ) 00254 { 00255 d->keyboardEnabled = b; 00256 } 00257 00258 QString KActionSelector::availableLabel() const 00259 { 00260 return d->lAvailable->text(); 00261 } 00262 00263 void KActionSelector::setAvailableLabel( const QString &text ) 00264 { 00265 d->lAvailable->setText( text ); 00266 } 00267 00268 QString KActionSelector::selectedLabel() const 00269 { 00270 return d->lSelected->text(); 00271 } 00272 00273 void KActionSelector::setSelectedLabel( const QString &text ) 00274 { 00275 d->lSelected->setText( text ); 00276 } 00277 00278 KActionSelector::ButtonIconSize KActionSelector::buttonIconSize() const 00279 { 00280 return d->iconSize; 00281 } 00282 00283 void KActionSelector::setButtonIconSize( ButtonIconSize size ) 00284 { 00285 d->iconSize = size; 00286 // reload icons 00287 loadIcons(); 00288 } 00289 00290 KActionSelector::InsertionPolicy KActionSelector::availableInsertionPolicy() const 00291 { 00292 return d->availableInsertionPolicy; 00293 } 00294 00295 void KActionSelector::setAvailableInsertionPolicy( InsertionPolicy p ) 00296 { 00297 d->availableInsertionPolicy = p; 00298 } 00299 00300 KActionSelector::InsertionPolicy KActionSelector::selectedInsertionPolicy() const 00301 { 00302 return d->selectedInsertionPolicy; 00303 } 00304 00305 void KActionSelector::setSelectedInsertionPolicy( InsertionPolicy p ) 00306 { 00307 d->selectedInsertionPolicy = p; 00308 } 00309 00310 bool KActionSelector::showUpDownButtons() const 00311 { 00312 return d->showUpDownButtons; 00313 } 00314 00315 void KActionSelector::setShowUpDownButtons( bool show ) 00316 { 00317 d->showUpDownButtons = show; 00318 if ( show ) 00319 { 00320 d->btnUp->show(); 00321 d->btnDown->show(); 00322 } 00323 else 00324 { 00325 d->btnUp->hide(); 00326 d->btnDown->hide(); 00327 } 00328 } 00329 00330 //END Properties 00331 00332 //BEGIN Public Slots 00333 00334 void KActionSelector::polish() 00335 { 00336 setButtonsEnabled(); 00337 } 00338 00339 //END Public Slots 00340 00341 //BEGIN Protected 00342 void KActionSelector::keyPressEvent( QKeyEvent *e ) 00343 { 00344 if ( ! d->keyboardEnabled ) return; 00345 if ( (e->state() & Qt::ControlButton) ) 00346 { 00347 switch ( e->key() ) 00348 { 00349 case Key_Right: 00350 buttonAddClicked(); 00351 break; 00352 case Key_Left: 00353 buttonRemoveClicked(); 00354 break; 00355 case Key_Up: 00356 buttonUpClicked(); 00357 break; 00358 case Key_Down: 00359 buttonDownClicked(); 00360 break; 00361 default: 00362 e->ignore(); 00363 return; 00364 } 00365 } 00366 } 00367 00368 bool KActionSelector::eventFilter( QObject *o, QEvent *e ) 00369 { 00370 if ( d->keyboardEnabled && e->type() == QEvent::KeyPress ) 00371 { 00372 if ( (((QKeyEvent*)e)->state() & Qt::ControlButton) ) 00373 { 00374 switch ( ((QKeyEvent*)e)->key() ) 00375 { 00376 case Key_Right: 00377 buttonAddClicked(); 00378 break; 00379 case Key_Left: 00380 buttonRemoveClicked(); 00381 break; 00382 case Key_Up: 00383 buttonUpClicked(); 00384 break; 00385 case Key_Down: 00386 buttonDownClicked(); 00387 break; 00388 default: 00389 return QWidget::eventFilter( o, e ); 00390 break; 00391 } 00392 return true; 00393 } 00394 else if ( o->inherits( "QListBox" ) ) 00395 { 00396 switch ( ((QKeyEvent*)e)->key() ) 00397 { 00398 case Key_Return: 00399 case Key_Enter: 00400 QListBox *lb = (QListBox*)o; 00401 int index = lb->currentItem(); 00402 if ( index < 0 ) break; 00403 moveItem( lb->item( index ) ); 00404 return true; 00405 } 00406 } 00407 } 00408 return QWidget::eventFilter( o, e ); 00409 } 00410 00411 //END Protected 00412 00413 //BEGIN Private Slots 00414 00415 void KActionSelector::buttonAddClicked() 00416 { 00417 // move all selected items from available to selected listbox 00418 QListBoxItem *item = d->availableListBox->firstItem(); 00419 while ( item ) { 00420 if ( item->isSelected() ) { 00421 d->availableListBox->takeItem( item ); 00422 d->selectedListBox->insertItem( item, insertionIndex( d->selectedListBox, d->selectedInsertionPolicy ) ); 00423 d->selectedListBox->setCurrentItem( item ); 00424 emit added( item ); 00425 } 00426 item = item->next(); 00427 } 00428 if ( d->selectedInsertionPolicy == Sorted ) 00429 d->selectedListBox->sort(); 00430 d->selectedListBox->setFocus(); 00431 } 00432 00433 void KActionSelector::buttonRemoveClicked() 00434 { 00435 // move all selected items from selected to available listbox 00436 QListBoxItem *item = d->selectedListBox->firstItem(); 00437 while ( item ) { 00438 if ( item->isSelected() ) { 00439 d->selectedListBox->takeItem( item ); 00440 d->availableListBox->insertItem( item, insertionIndex( d->availableListBox, d->availableInsertionPolicy ) ); 00441 d->availableListBox->setCurrentItem( item ); 00442 emit removed( item ); 00443 } 00444 item = item->next(); 00445 } 00446 if ( d->availableInsertionPolicy == Sorted ) 00447 d->availableListBox->sort(); 00448 d->availableListBox->setFocus(); 00449 } 00450 00451 void KActionSelector::buttonUpClicked() 00452 { 00453 int c = d->selectedListBox->currentItem(); 00454 if ( c < 1 ) return; 00455 QListBoxItem *item = d->selectedListBox->item( c ); 00456 d->selectedListBox->takeItem( item ); 00457 d->selectedListBox->insertItem( item, c-1 ); 00458 d->selectedListBox->setCurrentItem( item ); 00459 emit movedUp( item ); 00460 } 00461 00462 void KActionSelector::buttonDownClicked() 00463 { 00464 int c = d->selectedListBox->currentItem(); 00465 if ( c < 0 || c == int( d->selectedListBox->count() ) - 1 ) return; 00466 QListBoxItem *item = d->selectedListBox->item( c ); 00467 d->selectedListBox->takeItem( item ); 00468 d->selectedListBox->insertItem( item, c+1 ); 00469 d->selectedListBox->setCurrentItem( item ); 00470 emit movedDown( item ); 00471 } 00472 00473 void KActionSelector::itemDoubleClicked( QListBoxItem *item ) 00474 { 00475 if ( d->moveOnDoubleClick ) 00476 moveItem( item ); 00477 } 00478 00479 //END Private Slots 00480 00481 //BEGIN Private Methods 00482 00483 void KActionSelector::loadIcons() 00484 { 00485 d->btnAdd->setIconSet( SmallIconSet( d->addIcon, d->iconSize ) ); 00486 d->btnRemove->setIconSet( SmallIconSet( d->removeIcon, d->iconSize ) ); 00487 d->btnUp->setIconSet( SmallIconSet( d->upIcon, d->iconSize ) ); 00488 d->btnDown->setIconSet( SmallIconSet( d->downIcon, d->iconSize ) ); 00489 } 00490 00491 void KActionSelector::moveItem( QListBoxItem *item ) 00492 { 00493 QListBox *lbFrom = item->listBox(); 00494 QListBox *lbTo; 00495 if ( lbFrom == d->availableListBox ) 00496 lbTo = d->selectedListBox; 00497 else if ( lbFrom == d->selectedListBox ) 00498 lbTo = d->availableListBox; 00499 else //?! somewhat unlikely... 00500 return; 00501 00502 InsertionPolicy p = ( lbTo == d->availableListBox ) ? 00503 d->availableInsertionPolicy : d->selectedInsertionPolicy; 00504 00505 lbFrom->takeItem( item ); 00506 lbTo->insertItem( item, insertionIndex( lbTo, p ) ); 00507 lbTo->setFocus(); 00508 lbTo->setCurrentItem( item ); 00509 00510 if ( p == Sorted ) 00511 lbTo->sort(); 00512 if ( lbTo == d->selectedListBox ) 00513 emit added( item ); 00514 else 00515 emit removed( item ); 00516 } 00517 00518 int KActionSelector::insertionIndex( QListBox *lb, InsertionPolicy policy ) 00519 { 00520 int index; 00521 switch ( policy ) 00522 { 00523 case BelowCurrent: 00524 index = lb->currentItem(); 00525 if ( index > -1 ) index += 1; 00526 break; 00527 case AtTop: 00528 index = 0; 00529 break; 00530 default: 00531 index = -1; 00532 } 00533 return index; 00534 } 00535 00536 //END Private Methods 00537 #include "kactionselector.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:40:31 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003