KDevelop API Documentation

parts/fileselector/kactionselector.cpp

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