• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

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

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal