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

KDEUI

kselectaction.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
00003               (C) 1999 Simon Hausmann <hausmann@kde.org>
00004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
00005               (C) 2000 Kurt Granroth <granroth@kde.org>
00006               (C) 2000 Michael Koch <koch@kde.org>
00007               (C) 2001 Holger Freyther <freyther@kde.org>
00008               (C) 2002 Ellis Whitehead <ellis@kde.org>
00009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
00010               (C) 2003 Andras Mantia <amantia@kde.org>
00011               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
00012               (C) 2006 Albert Astals Cid <aacid@kde.org>
00013               (C) 2006 Clarence Dang <dang@kde.org>
00014               (C) 2006 Michel Hermier <michel.hermier@gmail.com>
00015               (C) 2007 Nick Shaforostoff <shafff@ukr.net>
00016 
00017     This library is free software; you can redistribute it and/or
00018     modify it under the terms of the GNU Library General Public
00019     License version 2 as published by the Free Software Foundation.
00020 
00021     This library is distributed in the hope that it will be useful,
00022     but WITHOUT ANY WARRANTY; without even the implied warranty of
00023     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024     Library General Public License for more details.
00025 
00026     You should have received a copy of the GNU Library General Public License
00027     along with this library; see the file COPYING.LIB.  If not, write to
00028     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00029     Boston, MA 02110-1301, USA.
00030 */
00031 
00032 #include "kselectaction.h"
00033 #include "kselectaction_p.h"
00034 
00035 #include <QActionEvent>
00036 #include <QEvent>
00037 #include <QToolButton>
00038 #include <QToolBar>
00039 #include <kicon.h>
00040 #include <kdebug.h>
00041 
00042 #include "kcombobox.h"
00043 #include "kmenu.h"
00044 
00045 // QAction::setText("Hi") and then KPopupAccelManager exec'ing, causes
00046 // QAction::text() to return "&Hi" :(  Comboboxes don't have accels and
00047 // display ampersands literally.
00048 static QString DropAmpersands(const QString &text)
00049 {
00050     QString result;
00051     for (int i = 0; i < text.count(); ++i) {
00052         if (text.at(i) == '&') {
00053             if (i + 1 < text.count() && text.at(i + 1) == '&')
00054                 result.append(text.at(i++)); // "&&" -> '&'
00055             // else eat the ampersand
00056         }
00057         else
00058             result.append(text.at(i));
00059     }
00060     return result;
00061 }
00062 
00063 
00064 KSelectAction::KSelectAction(QObject *parent)
00065   : KAction(parent)
00066   , d_ptr(new KSelectActionPrivate())
00067 {
00068   Q_D(KSelectAction);
00069   d->init(this);
00070 }
00071 
00072 KSelectAction::KSelectAction(const QString &text, QObject *parent)
00073   : KAction(parent)
00074   , d_ptr(new KSelectActionPrivate())
00075 {
00076   Q_D(KSelectAction);
00077   d->init(this);
00078   setText(text);
00079 }
00080 
00081 KSelectAction::KSelectAction(const KIcon & icon, const QString &text, QObject *parent)
00082   : KAction(icon, text, parent)
00083   , d_ptr(new KSelectActionPrivate())
00084 {
00085   Q_D(KSelectAction);
00086   d->init(this);
00087 }
00088 
00089 KSelectAction::KSelectAction(KSelectActionPrivate &dd, QObject *parent)
00090   : KAction(parent)
00091   , d_ptr(&dd)
00092 {
00093   Q_D(KSelectAction);
00094   d->init(this);
00095 }
00096 
00097 KSelectAction::~KSelectAction()
00098 {
00099   delete d_ptr;
00100   delete menu();
00101 }
00102 
00103 void KSelectActionPrivate::init(KSelectAction *q)
00104 {
00105   q_ptr = q;
00106   QObject::connect(q_ptr->selectableActionGroup(), SIGNAL(triggered(QAction*)), q_ptr, SLOT(actionTriggered(QAction*)));
00107   QObject::connect(q_ptr, SIGNAL(toggled(bool)), q_ptr, SLOT(slotToggled(bool)));
00108   q_ptr->setMenu(new KMenu());
00109 }
00110 
00111 QActionGroup * KSelectAction::selectableActionGroup( ) const
00112 {
00113   Q_D(const KSelectAction);
00114   return d->m_actionGroup;
00115 }
00116 
00117 QList<QAction*> KSelectAction::actions( ) const
00118 {
00119   return selectableActionGroup()->actions();
00120 }
00121 
00122 QAction* KSelectAction::currentAction() const
00123 {
00124   return selectableActionGroup()->checkedAction();
00125 }
00126 
00127 int KSelectAction::currentItem() const
00128 {
00129   return selectableActionGroup()->actions().indexOf(currentAction());
00130 }
00131 
00132 QString KSelectAction::currentText( ) const
00133 {
00134   if (QAction* a = currentAction())
00135     return ::DropAmpersands(a->text());
00136 
00137   return QString();
00138 }
00139 
00140 bool KSelectAction::setCurrentAction(QAction* action)
00141 {
00142   //kDebug (129) << "KSelectAction::setCurrentAction(" << action << ")";
00143   if (action) {
00144     if (actions().contains(action)) {
00145       if (action->isVisible() && action->isEnabled() && action->isCheckable()) {
00146         action->setChecked(true);
00147         if (isCheckable())
00148             setChecked(true);
00149         return true;
00150       } else
00151         kWarning (129) << "Action does not have the correct properties to be current:" << action->text();
00152     } else
00153       kWarning (129) << "Action does not belong to group:" << action->text();
00154     return false;
00155   }
00156 
00157   if (currentAction())
00158     currentAction()->setChecked(false);
00159 
00160   return false;
00161 }
00162 
00163 bool KSelectAction::setCurrentItem( int index )
00164 {
00165   //kDebug (129) << "KSelectAction::setCurrentIndex(" << index << ")";
00166   return setCurrentAction(action(index));
00167 }
00168 
00169 QAction * KSelectAction::action( int index ) const
00170 {
00171   if (index >= 0 && index < selectableActionGroup()->actions().count())
00172     return selectableActionGroup()->actions().at(index);
00173 
00174   return 0L;
00175 }
00176 
00177 QAction * KSelectAction::action( const QString & text, Qt::CaseSensitivity cs ) const
00178 {
00179   QString compare;
00180   if (cs == Qt::CaseSensitive)
00181     compare = text;
00182   else
00183     compare = text.toLower();
00184 
00185   foreach (QAction* action, selectableActionGroup()->actions()) {
00186     const QString text = ::DropAmpersands(action->text());
00187     if (cs == Qt::CaseSensitive) {
00188       if (text == compare) {
00189         return action;
00190       }
00191 
00192     } else if (cs == Qt::CaseInsensitive) {
00193       if (text.toLower() == compare) {
00194         return action;
00195       }
00196     }
00197   }
00198 
00199   return 0L;
00200 }
00201 
00202 bool KSelectAction::setCurrentAction( const QString & text, Qt::CaseSensitivity cs)
00203 {
00204   //kDebug (129) << "KSelectAction::setCurrentAction(" << text << ",cs=" << cs << ")";
00205   return setCurrentAction(action(text, cs));
00206 }
00207 
00208 void KSelectAction::setComboWidth( int width )
00209 {
00210   Q_D(KSelectAction);
00211   if ( width < 0 )
00212     return;
00213 
00214   d->m_comboWidth = width;
00215 
00216   foreach (KComboBox* box, d->m_comboBoxes)
00217     box->setMaximumWidth(d->m_comboWidth);
00218 
00219   emit changed();
00220 }
00221 
00222 void KSelectAction::setMaxComboViewCount( int n )
00223 {
00224   Q_D(KSelectAction);
00225   d->m_maxComboViewCount = n;
00226 
00227   foreach (KComboBox* box, d->m_comboBoxes)
00228     if ( d->m_maxComboViewCount != -1 )
00229       box->setMaxVisibleItems(d->m_maxComboViewCount);
00230     else
00231       // hardcoded qt default
00232       box->setMaxVisibleItems(10);
00233 
00234   emit changed();
00235 }
00236 
00237 void KSelectAction::addAction(QAction* action)
00238 {
00239   Q_D(KSelectAction);
00240   //kDebug (129) << "KSelectAction::addAction(" << action << ")";
00241 
00242   action->setActionGroup(selectableActionGroup());
00243 
00244   // Keep in sync with createToolBarWidget()
00245   foreach (QToolButton* button, d->m_buttons)
00246     button->addAction(action);
00247 
00248   foreach (KComboBox* comboBox, d->m_comboBoxes)
00249     comboBox->addAction(action);
00250 
00251   menu()->addAction(action);
00252 }
00253 
00254 KAction* KSelectAction::addAction(const QString &text)
00255 {
00256   Q_D(KSelectAction);
00257   KAction* newAction = new KAction(parent());
00258   newAction->setText(text);
00259   newAction->setCheckable( true );
00260   newAction->setShortcutConfigurable(false);
00261 
00262   if (!d->m_menuAccelsEnabled) {
00263     newAction->setText(text);
00264     newAction->setShortcut(QKeySequence());
00265   }
00266 
00267   addAction(newAction);
00268   return newAction;
00269 }
00270 
00271 KAction* KSelectAction::addAction(const KIcon& icon, const QString& text)
00272 {
00273   KAction* newAction = addAction(text);
00274   newAction->setIcon(icon);
00275   return newAction;
00276 }
00277 
00278 QAction* KSelectAction::removeAction(QAction* action)
00279 {
00280   Q_D(KSelectAction);
00281   //kDebug (129) << "KSelectAction::removeAction(" << action << ")";
00282   //int index = selectableActionGroup()->actions().indexOf(action);
00283   //kDebug (129) << "\tindex=" << index;
00284 
00285   // Removes the action from the group and sets its parent to null.
00286   d->m_actionGroup->removeAction(action);
00287 
00288   foreach (QToolButton* button, d->m_buttons)
00289     button->removeAction(action);
00290 
00291   foreach (KComboBox* comboBox, d->m_comboBoxes)
00292     comboBox->removeAction(action);
00293 
00294   menu()->removeAction(action);
00295 
00296   return action;
00297 }
00298 
00299 void KSelectAction::actionTriggered(QAction* action)
00300 {
00301   // cache values so we don't need access to members in the action
00302   // after we've done an emit()
00303   const QString text = ::DropAmpersands(action->text());
00304   const int index = selectableActionGroup()->actions().indexOf(action);
00305   //kDebug (129) << "KSelectAction::actionTriggered(" << action << ") text=" << text
00306   //          << " index=" << index  << " emitting triggered()" << endl;
00307 
00308   if (isCheckable()) // if this is subsidiary of other KSelectAction-derived class
00309     trigger();       // then imitate usual QAction behaviour so that other submenus (and their items) become unchecked
00310 
00311   emit triggered(action);
00312   emit triggered(index);
00313   emit triggered(text);
00314 }
00315 
00316 QStringList KSelectAction::items() const
00317 {
00318   Q_D(const KSelectAction);
00319   QStringList ret;
00320 
00321   foreach (QAction* action, d->m_actionGroup->actions())
00322     ret << ::DropAmpersands(action->text());
00323 
00324   return ret;
00325 }
00326 
00327 void KSelectAction::changeItem( int index, const QString& text )
00328 {
00329   Q_D(KSelectAction);
00330   if ( index < 0 || index >= actions().count() )
00331   {
00332     kWarning() << "KSelectAction::changeItem Index out of scope";
00333     return;
00334   }
00335 
00336   actions()[index]->setText( d->makeMenuText( text ) );
00337 }
00338 
00339 void KSelectAction::setItems( const QStringList &lst )
00340 {
00341   Q_D(KSelectAction);
00342   //kDebug (129) << "KSelectAction::setItems(" << lst << ")";
00343 
00344   clear();
00345 
00346   foreach (const QString& string, lst) {
00347     if ( !string.isEmpty() ) {
00348       addAction(string);
00349     } else {
00350       QAction* action = new QAction(this);
00351       action->setSeparator(true);
00352       addAction(action);
00353     }
00354   }
00355 
00356   // Disable if empty and not editable
00357   setEnabled( lst.count() > 0 || d->m_edit );
00358 }
00359 
00360 int KSelectAction::comboWidth() const
00361 {
00362   Q_D(const KSelectAction);
00363   return d->m_comboWidth;
00364 }
00365 
00366 void KSelectAction::clear()
00367 {
00368   Q_D(KSelectAction);
00369   //kDebug (129) << "KSelectAction::clear()";
00370 
00371   // we need to delete the actions later since we may get a call to clear()
00372   // from a method called due to a triggered(...) signal
00373   const QList<QAction*> actions = d->m_actionGroup->actions();
00374   for (int i = 0; i < actions.count(); ++i)
00375   {
00376     // deleteLater() only removes us from the actions() list (among
00377     // other things) on the next entry into the event loop.  Until then,
00378     // e.g. action() and setCurrentItem() will be working on items
00379     // that are supposed to have been deleted.  So detach the action to
00380     // prevent this from happening.
00381     removeAction(actions[i]);
00382 
00383     actions[i]->deleteLater();
00384   }
00385 }
00386 
00387 void KSelectAction::removeAllActions( )
00388 {
00389   Q_D(KSelectAction);
00390   while (d->m_actionGroup->actions().count())
00391     removeAction(d->m_actionGroup->actions().first());
00392 }
00393 
00394 void KSelectAction::setEditable( bool edit )
00395 {
00396   Q_D(KSelectAction);
00397   d->m_edit = edit;
00398 
00399   foreach (KComboBox* comboBox, d->m_comboBoxes)
00400     comboBox->setEditable(edit);
00401 
00402   emit changed();
00403 }
00404 
00405 bool KSelectAction::isEditable() const
00406 {
00407   Q_D(const KSelectAction);
00408   return d->m_edit;
00409 }
00410 
00411 void KSelectAction::slotToggled(bool checked)
00412 {
00413     //if (checked && selectableActionGroup()->checkedAction())
00414     if (!checked && currentAction()) // other's submenu item has been selected
00415         currentAction()->setChecked(false);
00416 }
00417 
00418 KSelectAction::ToolBarMode KSelectAction::toolBarMode() const
00419 {
00420   Q_D(const KSelectAction);
00421   return d->m_toolBarMode;
00422 }
00423 
00424 void KSelectAction::setToolBarMode( ToolBarMode mode )
00425 {
00426   Q_D(KSelectAction);
00427   d->m_toolBarMode = mode;
00428 }
00429 
00430 QToolButton::ToolButtonPopupMode KSelectAction::toolButtonPopupMode( ) const
00431 {
00432   Q_D(const KSelectAction);
00433   return d->m_toolButtonPopupMode;
00434 }
00435 
00436 void KSelectAction::setToolButtonPopupMode( QToolButton::ToolButtonPopupMode mode )
00437 {
00438   Q_D(KSelectAction);
00439   d->m_toolButtonPopupMode = mode;
00440 }
00441 
00442 void KSelectActionPrivate::_k_comboBoxDeleted(QObject* object)
00443 {
00444   foreach (KComboBox* comboBox, m_comboBoxes)
00445     if (object == comboBox) {
00446       m_comboBoxes.removeAll(static_cast<KComboBox*>(object));
00447       break;
00448     }
00449 }
00450 
00451 void KSelectActionPrivate::_k_comboBoxCurrentIndexChanged(int index)
00452 {
00453     Q_Q(KSelectAction);
00454   //kDebug (129) << "KSelectActionPrivate::_k_comboBoxCurrentIndexChanged(" << index << ")";
00455 
00456   KComboBox *triggeringCombo = qobject_cast <KComboBox *> (q->sender ());
00457 
00458   QAction *a = q->action(index);
00459   //kDebug (129) << "\ta=" << a;
00460   if (a) {
00461     //kDebug (129) << "\t\tsetting as current action";
00462     a->trigger();
00463 
00464   } else if (q->isEditable () &&
00465     triggeringCombo && triggeringCombo->count () > 0 &&
00466     index == triggeringCombo->count () - 1) {
00467 
00468     // User must have added a new item by typing and pressing enter.
00469     const QString newItemText = triggeringCombo->currentText ();
00470     //kDebug (129) << "\t\tuser typed new item '" << newItemText << "'";
00471 
00472     // Only 1 combobox contains this and it's not a proper action.
00473     bool blocked = triggeringCombo->blockSignals (true);
00474     triggeringCombo->removeItem (index);
00475     triggeringCombo->blockSignals (blocked);
00476 
00477     KAction *newAction = q->addAction (newItemText);
00478 
00479     newAction->trigger();
00480   } else {
00481     if (q->selectableActionGroup()->checkedAction())
00482       q->selectableActionGroup()->checkedAction()->setChecked(false);
00483   }
00484 }
00485 
00486 // TODO: DropAmpersands() certainly makes sure this doesn't work.  But I don't
00487 // think it did anyway esp. in the presence KCheckAccelerator - Clarence.
00488 void KSelectAction::setMenuAccelsEnabled( bool b )
00489 {
00490   Q_D(KSelectAction);
00491   d->m_menuAccelsEnabled = b;
00492 }
00493 
00494 bool KSelectAction::menuAccelsEnabled() const
00495 {
00496   Q_D(const KSelectAction);
00497   return d->m_menuAccelsEnabled;
00498 }
00499 
00500 QWidget * KSelectAction::createWidget( QWidget * parent )
00501 {
00502   Q_D(KSelectAction);
00503     QMenu *menu = qobject_cast<QMenu *>(parent);
00504     if (menu) // If used in a menu want to return 0 and use only the text, not a widget
00505         return 0;
00506     ToolBarMode mode = toolBarMode();
00507     QToolBar *toolBar = qobject_cast<QToolBar *>(parent);
00508     if (!toolBar && mode != ComboBoxMode) { // we can return a combobox just fine.
00509         return 0;
00510     }
00511     switch (mode) {
00512     case MenuMode: {
00513       QToolButton* button = new QToolButton(toolBar);
00514       button->setAutoRaise(true);
00515       button->setFocusPolicy(Qt::NoFocus);
00516       button->setIconSize(toolBar->iconSize());
00517       button->setToolButtonStyle(toolBar->toolButtonStyle());
00518       QObject::connect(toolBar, SIGNAL(iconSizeChanged(const QSize&)),
00519                        button, SLOT(setIconSize(const QSize&)));
00520       QObject::connect(toolBar, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
00521                        button, SLOT(setToolButtonStyle(Qt::ToolButtonStyle)));
00522       button->setDefaultAction(this);
00523       QObject::connect(button, SIGNAL(triggered(QAction*)), toolBar, SIGNAL(actionTriggered(QAction*)));
00524 
00525       button->setPopupMode(toolButtonPopupMode());
00526 
00527       button->addActions(selectableActionGroup()->actions());
00528 
00529       d->m_buttons.append(button);
00530       return button;
00531     }
00532 
00533     case ComboBoxMode: {
00534         KComboBox* comboBox = new KComboBox(parent);
00535       comboBox->installEventFilter (this);
00536       // hack for the fact that QWidgetAction does not sync all its created widgets
00537       // to its enabled state, just QToolButtons (Qt 4.4.3)
00538       installEventFilter( comboBox );
00539 
00540       if ( d->m_maxComboViewCount != -1 )
00541         comboBox->setMaxVisibleItems( d->m_maxComboViewCount );
00542 
00543       if ( d->m_comboWidth > 0 )
00544         comboBox->setMaximumWidth( d->m_comboWidth );
00545 
00546       comboBox->setEditable(isEditable());
00547 
00548       foreach (QAction* action, selectableActionGroup()->actions())
00549         comboBox->addAction(action);
00550 
00551       connect(comboBox, SIGNAL(destroyed(QObject*)), SLOT(_k_comboBoxDeleted(QObject*)));
00552       connect(comboBox, SIGNAL(currentIndexChanged(int)), SLOT(_k_comboBoxCurrentIndexChanged(int)));
00553       d->m_comboBoxes.append(comboBox);
00554 
00555       return comboBox;
00556     }
00557   }
00558 
00559   return 0L;
00560 }
00561 
00562 void KSelectAction::deleteWidget(QWidget *widget)
00563 {
00564     Q_D(KSelectAction);
00565     if (QToolButton *toolButton = qobject_cast<QToolButton *>(widget))
00566         d->m_buttons.removeAll(toolButton);
00567     else if (KComboBox *comboBox = qobject_cast<KComboBox *>(widget))
00568         d->m_comboBoxes.removeAll(comboBox);
00569     KAction::deleteWidget(widget);
00570 }
00571 
00572 // KSelectAction::eventFilter() is called before action->setChecked()
00573 // invokes the signal to update QActionGroup so KSelectAction::currentItem()
00574 // returns an old value.  There are 3 possibilities, where n actions will
00575 // report QAction::isChecked() where n is:
00576 //
00577 // 0: the checked action was unchecked
00578 // 1: the checked action did not change
00579 // 2: another action was checked but QActionGroup has not been invoked yet
00580 //    to uncheck the one that was checked before
00581 //
00582 // TODO: we might want to cache this since QEvent::ActionChanged is fired
00583 //       often.
00584 static int TrueCurrentItem (KSelectAction *sa)
00585 {
00586   QAction *curAction = sa->currentAction ();
00587   //kDebug (129) << "\tTrueCurrentItem(" << sa << ") curAction=" << curAction;
00588 
00589   foreach (QAction *action, sa->actions ())
00590   {
00591     if (action->isChecked ())
00592     {
00593        //kDebug (129) << "\t\taction " << action << " (text=" << action->text () << ") isChecked";
00594 
00595        // 2 actions checked case?
00596        if (action != curAction)
00597        {
00598          //kDebug (129) << "\t\t\tmust be newly selected one";
00599          return sa->actions ().indexOf (action);
00600        }
00601     }
00602   }
00603 
00604   //kDebug (129) << "\t\tcurrent action still selected? " << (curAction && curAction->isChecked ());
00605   // 1 or 0 actions checked case (in that order)?
00606   return (curAction && curAction->isChecked ()) ? sa->actions ().indexOf (curAction) : -1;
00607 }
00608 
00609 // A plain "QVariant (action)" results in a bool being stored.  So all
00610 // QAction pointers become stored as "true".  This hacks around it.
00611 static QVariant QVariantFromQAction (QAction *action)
00612 {
00613     // "(void *)" or "long" would be smaller than "qlonglong"
00614     // but neither is supported by QVariant :(
00615     return qVariantFromValue ((qlonglong) action);
00616 }
00617 
00618 bool KSelectAction::eventFilter (QObject *watched, QEvent *event)
00619 {
00620   KComboBox *comboBox = qobject_cast <KComboBox *> (watched);
00621   if (!comboBox)
00622     return false/*propagate event*/;
00623 
00624 
00625   // If focus is lost, replace any edited text with the currently selected
00626   // item.
00627   if (event->type () == QEvent::FocusOut) {
00628     QFocusEvent * const e = static_cast <QFocusEvent *> (event);
00629     //kDebug (129) << "KSelectAction::eventFilter(FocusOut)"
00630     //  << "    comboBox: ptr=" << comboBox
00631     //  << " reason=" << e->reason ()
00632     //  << endl;
00633 
00634     if (e->reason () != Qt::ActiveWindowFocusReason/*switch window*/ &&
00635         e->reason () != Qt::PopupFocusReason/*menu*/ &&
00636         e->reason () != Qt::OtherFocusReason/*inconsistently reproduceable actions...*/) {
00637 
00638       //kDebug (129) << "\tkilling text";
00639       comboBox->setEditText (comboBox->itemText (comboBox->currentIndex ()));
00640     }
00641 
00642     return false/*propagate event*/;
00643   }
00644 
00645 
00646   bool blocked = comboBox->blockSignals (true);
00647 
00648   if (event->type () == QEvent::ActionAdded)
00649   {
00650     QActionEvent * const e = static_cast <QActionEvent *> (event);
00651 
00652     const int index = e->before () ?
00653       comboBox->findData (QVariantFromQAction (e->before ())) :
00654       comboBox->count ();
00655     const int newItem = ::TrueCurrentItem (this);
00656     //kDebug (129) << "KSelectAction::eventFilter(ActionAdded)"
00657     //          << "    comboBox: ptr=" << comboBox
00658     //          << " currentItem=" << comboBox->currentIndex ()
00659     //          << "    add index=" << index
00660     //          << "    action new: e->before=" << e->before ()
00661     //          << " ptr=" << e->action ()
00662     //          << " icon=" << e->action ()->icon ()
00663     //          << " text=" << e->action ()->text ()
00664     //          << " currentItem=" << newItem
00665     //          << endl;
00666     comboBox->insertItem (index,
00667       e->action()->icon(),
00668       ::DropAmpersands (e->action()->text()),
00669       QVariantFromQAction (e->action ()));
00670 
00671     // Inserting an item into a combobox can change the current item so
00672     // make sure the item corresponding to the checked action is selected.
00673     comboBox->setCurrentIndex (newItem);
00674   }
00675   else if (event->type () == QEvent::ActionChanged)
00676   {
00677     QActionEvent * const e = static_cast <QActionEvent *> (event);
00678 
00679     const int index = comboBox->findData (QVariantFromQAction (e->action ()));
00680     const int newItem = ::TrueCurrentItem (this);
00681     //kDebug (129) << "KSelectAction::eventFilter(ActionChanged)"
00682     //          << "    comboBox: ptr=" << comboBox
00683     //          << " currentItem=" << comboBox->currentIndex ()
00684     //          << "    changed action's index=" << index
00685     //          << "    action new: ptr=" << e->action ()
00686     //          << " icon=" << e->action ()->icon ()
00687     //          << " text=" << e->action ()->text ()
00688     //          << " currentItem=" << newItem
00689     //          << endl;
00690     comboBox->setItemIcon (index, e->action ()->icon ());
00691     comboBox->setItemText (index, ::DropAmpersands (e->action ()->text ()));
00692 
00693     // The checked action may have become unchecked so
00694     // make sure the item corresponding to the checked action is selected.
00695     comboBox->setCurrentIndex (newItem);
00696   }
00697   else if (event->type () == QEvent::ActionRemoved)
00698   {
00699     QActionEvent * const e = static_cast <QActionEvent *> (event);
00700 
00701     const int index = comboBox->findData (QVariantFromQAction (e->action ()));
00702     const int newItem = ::TrueCurrentItem (this);
00703     //kDebug (129) << "KSelectAction::eventFilter(ActionRemoved)"
00704     //          << "    comboBox: ptr=" << comboBox
00705     //          << " currentItem=" << comboBox->currentIndex ()
00706     //          << "    delete action index=" << index
00707     //          << "    new: currentItem=" << newItem
00708     //          << endl;
00709     comboBox->removeItem (index);
00710 
00711     // Removing an item from a combobox can change the current item so
00712     // make sure the item corresponding to the checked action is selected.
00713     comboBox->setCurrentIndex (newItem);
00714   }
00715 
00716   comboBox->blockSignals (blocked);
00717 
00718   return false/*propagate event*/;
00719 }
00720 
00721 // END
00722 
00723 /* vim: et sw=2 ts=2
00724  */
00725 
00726 #include "kselectaction.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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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