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