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

KDEUI

ktoolbar.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright
00003     (C) 2000 Reginald Stadlbauer (reggie@kde.org)
00004     (C) 1997, 1998 Stephan Kulow (coolo@kde.org)
00005     (C) 1997, 1998 Mark Donohoe (donohoe@kde.org)
00006     (C) 1997, 1998 Sven Radej (radej@kde.org)
00007     (C) 1997, 1998 Matthias Ettrich (ettrich@kde.org)
00008     (C) 1999 Chris Schlaeger (cs@kde.org)
00009     (C) 1999 Kurt Granroth (granroth@kde.org)
00010     (C) 2005-2006 Hamish Rodda (rodda@kde.org)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Library General Public
00014     License version 2 as published by the Free Software Foundation.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Library General Public License for more details.
00020 
00021     You should have received a copy of the GNU Library General Public License
00022     along with this library; see the file COPYING.LIB.  If not, write to
00023     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00024     Boston, MA 02110-1301, USA.
00025 */
00026 
00027 #include "ktoolbar.h"
00028 
00029 #include <config.h>
00030 
00031 #include <QtCore/QPointer>
00032 #include <QtGui/QDesktopWidget>
00033 #include <QtGui/QFrame>
00034 #include <QtGui/QLayout>
00035 #include <QtGui/QMouseEvent>
00036 #include <QtGui/QToolButton>
00037 #include <QtXml/QDomElement>
00038 
00039 #include <kaction.h>
00040 #include <kactioncollection.h>
00041 #include <kapplication.h>
00042 #include <kauthorized.h>
00043 #include <kconfig.h>
00044 #include <kdebug.h>
00045 #include <kedittoolbar.h>
00046 #include <kglobalsettings.h>
00047 #include <kguiitem.h>
00048 #include <kicon.h>
00049 #include <kiconloader.h>
00050 #include <klocale.h>
00051 #include <kxmlguiwindow.h>
00052 #include <kmenu.h>
00053 #include <kstandardaction.h>
00054 #include <ktoggleaction.h>
00055 #include <kxmlguifactory.h>
00056 
00057 #include <kconfiggroup.h>
00058 
00059 class KToolBar::Private
00060 {
00061   public:
00062     Private(KToolBar *qq)
00063       : q(qq),
00064         honorStyle(false),
00065         enableContext(true),
00066         modified(false),
00067         unlockedMovable(true),
00068         xmlguiClient(0),
00069         contextLockAction(0),
00070         IconSizeDefault(22),
00071         ToolButtonStyleDefault(Qt::ToolButtonTextBesideIcon),
00072         HiddenDefault(false),
00073         NewLineDefault(false),
00074         PositionDefault("Top"),
00075         dropIndicatorAction(0),
00076         context(0),
00077         dragAction(0)
00078     {
00093     }
00094 
00095     void slotReadConfig();
00096     void slotAppearanceChanged();
00097     void slotContextAboutToShow();
00098     void slotContextAboutToHide();
00099     void slotContextLeft();
00100     void slotContextRight();
00101     void slotContextTop();
00102     void slotContextBottom();
00103     void slotContextIcons();
00104     void slotContextText();
00105     void slotContextTextRight();
00106     void slotContextTextUnder();
00107     void slotContextIconSize();
00108     void slotLockToolBars(bool lock);
00109 
00110     void init(bool readConfig = true, bool honorStyle = false);
00111     void getAttributes(QString &position, Qt::ToolButtonStyle &toolButtonStyle, int &index) const;
00112     int dockWindowIndex() const;
00113     KMenu *contextMenu();
00114     bool isMainToolBar() const;
00115     void setLocked(bool locked);
00116     void adjustSeparatorVisibility();
00117 
00118     static Qt::ToolButtonStyle toolButtonStyleFromString(const QString& style);
00119     static QString toolButtonStyleToString(Qt::ToolButtonStyle);
00120     static Qt::ToolBarArea positionFromString(const QString& position);
00121 
00122 
00123     KToolBar *q;
00124     bool honorStyle : 1;
00125     bool enableContext : 1;
00126     bool modified : 1;
00127     bool unlockedMovable : 1;
00128     static bool s_editable;
00129     static bool s_locked;
00130 
00131     KXMLGUIClient *xmlguiClient;
00132 
00133     QList<int> iconSizes;
00134 
00135     QMenu* contextOrient;
00136     QMenu* contextMode;
00137     QMenu* contextSize;
00138 
00139     QAction* contextTop;
00140     QAction* contextLeft;
00141     QAction* contextRight;
00142     QAction* contextBottom;
00143     QAction* contextIcons;
00144     QAction* contextTextRight;
00145     QAction* contextText;
00146     QAction* contextTextUnder;
00147     KToggleAction* contextLockAction;
00148     QMap<QAction*,int> contextIconSizes;
00149 
00150     // Default Values.
00151     int IconSizeDefault;
00152     Qt::ToolButtonStyle ToolButtonStyleDefault;
00153     bool HiddenDefault : 1;
00154     bool NewLineDefault : 1;
00155     QString PositionDefault;
00156 
00157     QList<QAction*> actionsBeingDragged;
00158     QAction* dropIndicatorAction;
00159 
00160     KMenu* context;
00161     KAction* dragAction;
00162     QPoint dragStartPosition;
00163 };
00164 
00165 bool KToolBar::Private::s_editable = false;
00166 bool KToolBar::Private::s_locked = false;
00167 
00168 void KToolBar::Private::init(bool readConfig, bool _honorStyle)
00169 {
00170   honorStyle = _honorStyle;
00171 
00172   // finally, read in our configurable settings
00173   if (readConfig)
00174     slotReadConfig();
00175 
00176   if (q->mainWindow()) {
00177     // Get notified when settings change
00178     connect(q, SIGNAL(allowedAreasChanged(Qt::ToolBarAreas)),
00179              q->mainWindow(), SLOT(setSettingsDirty()));
00180     connect(q, SIGNAL(iconSizeChanged(const QSize&)),
00181              q->mainWindow(), SLOT(setSettingsDirty()));
00182     connect(q, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
00183              q->mainWindow(), SLOT(setSettingsDirty()));
00184     connect(q, SIGNAL(movableChanged(bool)),
00185              q->mainWindow(), SLOT(setSettingsDirty()));
00186     connect(q, SIGNAL(orientationChanged(Qt::Orientation)),
00187              q->mainWindow(), SLOT(setSettingsDirty()));
00188   }
00189 
00190   if (!KAuthorized::authorize("movable_toolbars"))
00191     q->setMovable(false);
00192   else
00193     q->setMovable(!KToolBar::toolBarsLocked());
00194 
00195   connect(q, SIGNAL(movableChanged(bool)),
00196            q, SLOT(slotMovableChanged(bool)));
00197 
00198   q->setAcceptDrops(true);
00199 }
00200 
00201 void KToolBar::Private::getAttributes(QString &position, Qt::ToolButtonStyle &toolButtonStyle, int &index) const
00202 {
00203   // get all of the stuff to save
00204   switch (q->mainWindow()->toolBarArea(const_cast<KToolBar*>(q))) {
00205     case Qt::BottomToolBarArea:
00206       position = "Bottom";
00207       break;
00208     case Qt::LeftToolBarArea:
00209       position = "Left";
00210       break;
00211     case Qt::RightToolBarArea:
00212       position = "Right";
00213       break;
00214     case Qt::TopToolBarArea:
00215     default:
00216       position = "Top";
00217       break;
00218   }
00219 
00220   toolButtonStyle = q->toolButtonStyle();
00221 
00222   index = dockWindowIndex();
00223 }
00224 
00225 int KToolBar::Private::dockWindowIndex() const
00226 {
00227   Q_ASSERT(q->mainWindow());
00228 
00229   return q->mainWindow()->layout()->indexOf(const_cast<KToolBar*>(q));
00230 }
00231 
00232 KMenu *KToolBar::Private::contextMenu()
00233 {
00234   if (!context) {
00235     context = new KMenu(q);
00236     context->addTitle(i18n("Toolbar Menu"));
00237 
00238     contextOrient = new KMenu(i18n("Orientation"), context);
00239     context->addMenu(contextOrient);
00240 
00241     contextTop = contextOrient->addAction(i18nc("toolbar position string", "Top"), q, SLOT(slotContextTop()));
00242     contextTop->setChecked(true);
00243     contextLeft = contextOrient->addAction(i18nc("toolbar position string", "Left"), q, SLOT(slotContextLeft()));
00244     contextRight = contextOrient->addAction(i18nc("toolbar position string", "Right"), q, SLOT(slotContextRight()));
00245     contextBottom = contextOrient->addAction(i18nc("toolbar position string", "Bottom"), q, SLOT(slotContextBottom()));
00246 
00247     QActionGroup* positionGroup = new QActionGroup(contextOrient);
00248     foreach (QAction* action, contextOrient->actions()) {
00249       action->setActionGroup(positionGroup);
00250       action->setCheckable(true);
00251     }
00252 
00253     contextMode = new KMenu(i18n("Text Position"), context);
00254     context->addMenu(contextMode);
00255 
00256     contextIcons = contextMode->addAction(i18n("Icons Only"), q, SLOT(slotContextIcons()));
00257     contextIcons->setChecked(true);
00258     contextText = contextMode->addAction(i18n("Text Only"), q, SLOT(slotContextText()));
00259     contextTextRight = contextMode->addAction(i18n("Text Alongside Icons"), q, SLOT(slotContextTextRight()));
00260     contextTextUnder = contextMode->addAction(i18n("Text Under Icons"), q, SLOT(slotContextTextUnder()));
00261 
00262     QActionGroup* textGroup = new QActionGroup(contextMode);
00263     foreach (QAction* action, contextMode->actions()) {
00264       action->setActionGroup(textGroup);
00265       action->setCheckable(true);
00266     }
00267 
00268     contextSize = new KMenu(i18n("Icon Size"), context);
00269     context->addMenu(contextSize);
00270 
00271     contextIconSizes.insert(contextSize->addAction(i18nc("@item:inmenu Icon size", "Default"), q, SLOT(slotContextIconSize())), IconSizeDefault);
00272 
00273     // Query the current theme for available sizes
00274     KIconTheme *theme = KIconLoader::global()->theme();
00275     QList<int> avSizes;
00276     if (theme) {
00277       if (isMainToolBar())
00278         avSizes = theme->querySizes(KIconLoader::MainToolbar);
00279       else
00280         avSizes = theme->querySizes(KIconLoader::Toolbar);
00281     }
00282 
00283     iconSizes = avSizes;
00284     qSort(avSizes);
00285 
00286     if (avSizes.count() < 10) {
00287       // Fixed or threshold type icons
00288       foreach (int it, avSizes) {
00289         QString text;
00290         if (it < 19)
00291           text = i18n("Small (%1x%2)", it, it);
00292         else if (it < 25)
00293           text = i18n("Medium (%1x%2)", it, it);
00294         else if (it < 35)
00295           text = i18n("Large (%1x%2)", it, it);
00296         else
00297           text = i18n("Huge (%1x%2)", it, it);
00298 
00299         // save the size in the contextIconSizes map
00300         contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
00301       }
00302     } else {
00303       // Scalable icons.
00304       const int progression[] = { 16, 22, 32, 48, 64, 96, 128, 192, 256 };
00305 
00306       for (uint i = 0; i < 9; i++) {
00307         foreach (int it, avSizes) {
00308           if (it >= progression[ i ]) {
00309             QString text;
00310             if (it < 19)
00311               text = i18n("Small (%1x%2)", it, it);
00312             else if (it < 25)
00313               text = i18n("Medium (%1x%2)", it, it);
00314             else if (it < 35)
00315               text = i18n("Large (%1x%2)", it, it);
00316             else
00317               text = i18n("Huge (%1x%2)", it, it);
00318 
00319             // save the size in the contextIconSizes map
00320             contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
00321             break;
00322           }
00323         }
00324       }
00325     }
00326 
00327     QActionGroup* sizeGroup = new QActionGroup(contextSize);
00328     foreach (QAction* action, contextSize->actions()) {
00329       action->setActionGroup(sizeGroup);
00330       action->setCheckable(true);
00331     }
00332 
00333     if (!q->toolBarsLocked() && !q->isMovable())
00334       unlockedMovable = false;
00335 
00336     delete contextLockAction;
00337     contextLockAction = new KToggleAction(KIcon("system-lock-screen"), (q->toolBarsLocked())?i18n("Unlock Toolbars"):i18n("Lock Toolbars"), q);
00338     context->addAction(contextLockAction);
00339     contextLockAction->setChecked(q->toolBarsLocked());
00340     connect(contextLockAction, SIGNAL(toggled(bool)), q, SLOT(slotLockToolBars(bool)));
00341 
00342     connect(context, SIGNAL(aboutToShow()), q, SLOT(slotContextAboutToShow()));
00343   }
00344 
00345   contextOrient->menuAction()->setVisible(!q->toolBarsLocked());
00346   contextMode->menuAction()->setVisible(!q->toolBarsLocked());
00347   contextSize->menuAction()->setVisible(!q->toolBarsLocked());
00348 
00349   // Unplugging a submenu from abouttohide leads to the popupmenu floating around
00350   // So better simply call that code from after exec() returns (DF)
00351   //connect(context, SIGNAL(aboutToHide()), this, SLOT(slotContextAboutToHide()));
00352 
00353   return context;
00354 }
00355 
00356 bool KToolBar::Private::isMainToolBar() const
00357 {
00358   return q->objectName() == QLatin1String("mainToolBar");
00359 }
00360 
00361 void KToolBar::Private::setLocked(bool locked)
00362 {
00363   if (unlockedMovable)
00364     q->setMovable(!locked);
00365 }
00366 
00367 void KToolBar::Private::adjustSeparatorVisibility()
00368 {
00369   bool visibleNonSeparator = false;
00370   int separatorToShow = -1;
00371 
00372   for (int index = 0; index < q->actions().count(); ++index) {
00373     QAction* action = q->actions()[ index ];
00374     if (action->isSeparator()) {
00375       if (visibleNonSeparator) {
00376         separatorToShow = index;
00377         visibleNonSeparator = false;
00378       } else {
00379         action->setVisible(false);
00380       }
00381     } else if (!visibleNonSeparator) {
00382       if (action->isVisible()) {
00383         visibleNonSeparator = true;
00384         if (separatorToShow != -1) {
00385           q->actions()[ separatorToShow ]->setVisible(true);
00386           separatorToShow = -1;
00387         }
00388       }
00389     }
00390   }
00391 
00392   if (separatorToShow != -1)
00393     q->actions()[ separatorToShow ]->setVisible(false);
00394 }
00395 
00396 Qt::ToolButtonStyle KToolBar::Private::toolButtonStyleFromString(const QString & _style)
00397 {
00398   QString style = _style.toLower();
00399   if (style == "textbesideicon" || style == "icontextright")
00400     return Qt::ToolButtonTextBesideIcon;
00401   else if (style == "textundericon" || style == "icontextbottom")
00402     return Qt::ToolButtonTextUnderIcon;
00403   else if (style == "textonly")
00404     return Qt::ToolButtonTextOnly;
00405   else
00406     return Qt::ToolButtonIconOnly;
00407 }
00408 
00409 QString KToolBar::Private::toolButtonStyleToString(Qt::ToolButtonStyle style)
00410 {
00411   switch(style)
00412   {
00413     case Qt::ToolButtonIconOnly:
00414     default:
00415       return "IconOnly";
00416     case Qt::ToolButtonTextBesideIcon:
00417       return "TextBesideIcon";
00418     case Qt::ToolButtonTextOnly:
00419       return "TextOnly";
00420     case Qt::ToolButtonTextUnderIcon:
00421       return "TextUnderIcon";
00422   }
00423 }
00424 
00425 Qt::ToolBarArea KToolBar::Private::positionFromString(const QString& position)
00426 {
00427     Qt::ToolBarArea newposition = Qt::TopToolBarArea;
00428     if (position == QLatin1String("left")) {
00429         newposition = Qt::LeftToolBarArea;
00430     } else if (position == QLatin1String("bottom")) {
00431         newposition = Qt::BottomToolBarArea;
00432     } else if (position == QLatin1String("right")) {
00433         newposition = Qt::RightToolBarArea;
00434     }
00435     return newposition;
00436 }
00437 
00438 void KToolBar::Private::slotReadConfig()
00439 {
00445   KConfigGroup cg(KGlobal::config(), QString());
00446   q->applyAppearanceSettings(cg);
00447 }
00448 
00449 void KToolBar::Private::slotAppearanceChanged()
00450 {
00451   // Read appearance settings from global file.
00452   KConfigGroup cg(KGlobal::config(), QString());
00453   q->applyAppearanceSettings(cg , true /* lose local settings */);
00454 
00455   // And remember to save the new look later
00456   KMainWindow *kmw = qobject_cast<KMainWindow *>(q->mainWindow());
00457   if (kmw)
00458     kmw->setSettingsDirty();
00459 }
00460 
00461 void KToolBar::Private::slotContextAboutToShow()
00462 {
00470   KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
00471   if (kmw) {
00472     kmw->setupToolbarMenuActions();
00473     // Only allow hiding a toolbar if the action is also plugged somewhere else (e.g. menubar)
00474     QAction *tbAction = kmw->toolBarMenuAction();
00475     if (!q->toolBarsLocked() && tbAction && tbAction->associatedWidgets().count() > 0)
00476       contextMenu()->addAction(tbAction);
00477   }
00478 
00479   // try to find "configure toolbars" action
00480   QAction *configureAction = 0;
00481   const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
00482   if (xmlguiClient)
00483     configureAction = xmlguiClient->actionCollection()->action(actionName);
00484 
00485   if (!configureAction && kmw)
00486     configureAction = kmw->actionCollection()->action(actionName);
00487 
00488   if (configureAction)
00489     context->addAction(configureAction);
00490 
00491   KEditToolBar::setGlobalDefaultToolBar(q->QObject::objectName().toLatin1().constData());
00492 
00493   // Check the actions that should be checked
00494   switch (q->toolButtonStyle()) {
00495     case Qt::ToolButtonIconOnly:
00496     default:
00497       contextIcons->setChecked(true);
00498       break;
00499     case Qt::ToolButtonTextBesideIcon:
00500       contextTextRight->setChecked(true);
00501       break;
00502     case Qt::ToolButtonTextOnly:
00503       contextText->setChecked(true);
00504       break;
00505     case Qt::ToolButtonTextUnderIcon:
00506       contextTextUnder->setChecked(true);
00507       break;
00508   }
00509 
00510   QMapIterator< QAction*, int > it = contextIconSizes;
00511   while (it.hasNext()) {
00512     it.next();
00513     if (it.value() == q->iconSize().width()) {
00514       it.key()->setChecked(true);
00515       break;
00516     }
00517   }
00518 
00519   switch (q->mainWindow()->toolBarArea(q)) {
00520     case Qt::BottomToolBarArea:
00521       contextBottom->setChecked(true);
00522       break;
00523     case Qt::LeftToolBarArea:
00524       contextLeft->setChecked(true);
00525       break;
00526     case Qt::RightToolBarArea:
00527       contextRight->setChecked(true);
00528       break;
00529     default:
00530     case Qt::TopToolBarArea:
00531       contextTop->setChecked(true);
00532       break;
00533   }
00534 }
00535 
00536 void KToolBar::Private::slotContextAboutToHide()
00537 {
00538   // We have to unplug whatever slotContextAboutToShow plugged into the menu.
00539   // Unplug the toolbar menu action
00540   KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
00541   if (kmw && kmw->toolBarMenuAction())
00542     if (kmw->toolBarMenuAction()->associatedWidgets().count() > 1)
00543       contextMenu()->removeAction(kmw->toolBarMenuAction());
00544 
00545   // Unplug the configure toolbars action too, since it's afterwards anyway
00546   QAction *configureAction = 0;
00547   const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
00548   if (xmlguiClient)
00549     configureAction = xmlguiClient->actionCollection()->action(actionName);
00550 
00551   if (!configureAction && kmw)
00552     configureAction = kmw->actionCollection()->action(actionName);
00553 
00554   if (configureAction)
00555     context->removeAction(configureAction);
00556 }
00557 
00558 void KToolBar::Private::slotContextLeft()
00559 {
00560   q->mainWindow()->addToolBar(Qt::LeftToolBarArea, q);
00561 }
00562 
00563 void KToolBar::Private::slotContextRight()
00564 {
00565   q->mainWindow()->addToolBar(Qt::RightToolBarArea, q);
00566 }
00567 
00568 void KToolBar::Private::slotContextTop()
00569 {
00570   q->mainWindow()->addToolBar(Qt::TopToolBarArea, q);
00571 }
00572 
00573 void KToolBar::Private::slotContextBottom()
00574 {
00575   q->mainWindow()->addToolBar(Qt::BottomToolBarArea, q);
00576 }
00577 
00578 void KToolBar::Private::slotContextIcons()
00579 {
00580   q->setToolButtonStyle(Qt::ToolButtonIconOnly);
00581 }
00582 
00583 void KToolBar::Private::slotContextText()
00584 {
00585   q->setToolButtonStyle(Qt::ToolButtonTextOnly);
00586 }
00587 
00588 void KToolBar::Private::slotContextTextUnder()
00589 {
00590   q->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
00591 }
00592 
00593 void KToolBar::Private::slotContextTextRight()
00594 {
00595   q->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00596 }
00597 
00598 void KToolBar::Private::slotContextIconSize()
00599 {
00600   QAction* action = qobject_cast<QAction*>(q->sender());
00601   if (action && contextIconSizes.contains(action)) {
00602     q->setIconDimensions(contextIconSizes.value(action));
00603   }
00604 }
00605 
00606 void KToolBar::Private::slotLockToolBars(bool lock)
00607 {
00608   q->setToolBarsLocked(lock);
00609 }
00610 
00611 
00612 
00613 KToolBar::KToolBar(QWidget *parent, bool honorStyle, bool readConfig)
00614   : QToolBar(parent),
00615     d(new Private(this))
00616 {
00617   d->init(readConfig, honorStyle);
00618 
00619   // KToolBar is auto-added to the top area of the main window if parent is a QMainWindow
00620   if (QMainWindow* mw = qobject_cast<QMainWindow*>(parent))
00621     mw->addToolBar(this);
00622 }
00623 
00624 KToolBar::KToolBar(const QString& objectName, QMainWindow* parent, Qt::ToolBarArea area,
00625                     bool newLine, bool honorStyle, bool readConfig)
00626   : QToolBar(parent),
00627     d(new Private(this))
00628 {
00629   setObjectName(objectName);
00630   d->init(readConfig, honorStyle);
00631 
00632   if (newLine)
00633     mainWindow()->addToolBarBreak(area);
00634 
00635   mainWindow()->addToolBar(area, this);
00636 
00637   if (newLine)
00638     mainWindow()->addToolBarBreak(area);
00639 }
00640 
00641 KToolBar::~KToolBar()
00642 {
00643   delete d->contextLockAction;
00644   delete d;
00645 }
00646 
00647 void KToolBar::setContextMenuEnabled(bool enable)
00648 {
00649   d->enableContext = enable;
00650 }
00651 
00652 bool KToolBar::contextMenuEnabled() const
00653 {
00654   return d->enableContext;
00655 }
00656 
00657 void KToolBar::saveSettings(KConfigGroup &cg)
00658 {
00659     Q_ASSERT(!cg.name().isEmpty());
00660 
00661   QString position;
00662   Qt::ToolButtonStyle ToolButtonStyle;
00663   int index;
00664   d->getAttributes(position, ToolButtonStyle, index);
00665 
00666   if (!cg.hasDefault("Position") && position == d->PositionDefault)
00667     cg.revertToDefault("Position");
00668   else
00669     cg.writeEntry("Position", position);
00670 
00671   if (d->honorStyle && ToolButtonStyle == d->ToolButtonStyleDefault && !cg.hasDefault("ToolButtonStyle"))
00672     cg.revertToDefault("ToolButtonStyle");
00673   else
00674     cg.writeEntry("ToolButtonStyle", d->toolButtonStyleToString(ToolButtonStyle));
00675 
00676   if (!cg.hasDefault("IconSize") && iconSize().width() == iconSizeDefault())
00677     cg.revertToDefault("IconSize");
00678   else
00679     cg.writeEntry("IconSize", iconSize().width());
00680 
00681   if (!cg.hasDefault("Hidden") && isHidden() == d->HiddenDefault)
00682     cg.revertToDefault("Hidden");
00683   else
00684     cg.writeEntry("Hidden", isHidden());
00685 
00686     // Note that index, unlike the other settings, depends on the other toolbars
00687     // So on the first run with a clean local config file, even the usual
00688     // hasDefault/==IndexDefault test would save the toolbar indexes
00689     // (IndexDefault was 0, whereas index is the real index in the GUI)
00690     //
00691     // Saving the whole set of indexes is necessary though. When moving only
00692     // one toolbar, if we only saved the changed indexes, the toolbars wouldn't
00693     // reappear at the same position the next time.
00694     // The whole set of indexes has to be saved.
00695     //kDebug(220) << name() << "                writing index " << index;
00696 
00697     KMainWindow* kmw = mainWindow();
00698     QList<KToolBar*> toolbarList = kmw->findChildren<KToolBar*>();
00699 
00700     // don't save if there's only one toolbar
00701     if (!kmw || toolbarList.count() > 1)
00702         cg.writeEntry("Index", index);
00703     else
00704         cg.revertToDefault("Index");
00705 
00706     /* FIXME KToolBar port - need to implement
00707     if(!cg.hasDefault("NewLine") && newLine() == d->NewLineDefault)
00708       cg.revertToDefault("NewLine");
00709     else
00710       cg.writeEntry("NewLine", newLine());*/
00711 }
00712 
00713 void KToolBar::setXMLGUIClient(KXMLGUIClient *client)
00714 {
00715   d->xmlguiClient = client;
00716 }
00717 
00718 void KToolBar::contextMenuEvent(QContextMenuEvent* event)
00719 {
00720   if (mainWindow() && d->enableContext) {
00721     QPointer<KToolBar> guard(this);
00722     d->contextMenu()->exec(event->globalPos());
00723 
00724     // "Configure Toolbars" recreates toolbars, so we might not exist anymore.
00725     if (guard)
00726       d->slotContextAboutToHide();
00727     return;
00728   }
00729 
00730   QToolBar::contextMenuEvent(event);
00731 }
00732 
00733 Qt::ToolButtonStyle KToolBar::toolButtonStyleSetting()
00734 {
00735   KConfigGroup saver(KGlobal::config(), "Toolbar style");
00736 
00737   return KToolBar::Private::toolButtonStyleFromString(saver.readEntry("ToolButtonStyle", "TextUnderIcon"));
00738 }
00739 
00740 void KToolBar::loadState(const QDomElement &element)
00741 {
00742   QMainWindow *mw = mainWindow();
00743   if (!mw)
00744     return;
00745 
00746   {
00747     QDomNode textNode = element.namedItem("text");
00748     QByteArray text;
00749     QByteArray context;
00750     if (textNode.isElement())
00751     {
00752       QDomElement textElement = textNode.toElement();
00753       text = textElement.text().toUtf8();
00754       context = textElement.attribute("context").toUtf8();
00755     }
00756     else
00757     {
00758       textNode = element.namedItem("Text");
00759       if (textNode.isElement())
00760       {
00761         QDomElement textElement = textNode.toElement();
00762         text = textElement.text().toUtf8();
00763         context = textElement.attribute("context").toUtf8();
00764       }
00765     }
00766 
00767     QString i18nText;
00768     if (!text.isEmpty() && !context.isEmpty())
00769       i18nText = i18nc(context, text);
00770     else if (!text.isEmpty())
00771       i18nText = i18n(text);
00772 
00773     if (!i18nText.isEmpty())
00774       setWindowTitle(i18nText);
00775   }
00776 
00777   /*
00778     This method is called in order to load toolbar settings from XML.
00779     However this can be used in two rather different cases:
00780     - for the initial loading of the app's XML. In that case the settings
00781       are only the defaults, the user's KConfig settings will override them
00782       (KDE4 TODO: how about saving those user settings into the local XML file instead?
00783       Then this whole thing would be simpler, no KConfig settings to apply afterwards.
00784       OTOH we'd have to migrate those settings when the .rc version increases,
00785       like we do for shortcuts)
00786 
00787     - for later re-loading when switching between parts in KXMLGUIFactory.
00788       In that case the XML contains the final settings, not the defaults.
00789       We do need the defaults, and the toolbar might have been completely
00790       deleted and recreated meanwhile. So we store the app-default settings
00791       into the XML.
00792    */
00793   bool loadingAppDefaults = true;
00794   if (element.hasAttribute("newlineDefault")) {
00795     // this isn't the first time, so the defaults have been saved into the (in-memory) XML
00796     loadingAppDefaults = false;
00797     d->NewLineDefault = element.attribute("newlineDefault") == "true";
00798     d->HiddenDefault = element.attribute("hiddenDefault") == "true";
00799     d->IconSizeDefault = element.attribute("iconSizeDefault").toInt();
00800     d->PositionDefault = element.attribute("positionDefault");
00801     d->ToolButtonStyleDefault = d->toolButtonStyleFromString(element.attribute("toolButtonStyleDefault"));
00802   }
00803 
00804   {
00805     QString attrIconText = element.attribute("iconText").toLower().toLatin1();
00806     if (!attrIconText.isEmpty()) {
00807       setToolButtonStyle(d->toolButtonStyleFromString(attrIconText));
00808     } else {
00809       if (d->honorStyle)
00810         setToolButtonStyle(toolButtonStyleSetting());
00811       else
00812         setToolButtonStyle(d->ToolButtonStyleDefault);
00813     }
00814   }
00815 
00816   QString attrIconSize = element.attribute("iconSize").toLower().trimmed();
00817   int iconSize = d->IconSizeDefault;
00818 
00819   {
00820     bool ok;
00821     int newIconSize = attrIconSize.toInt(&ok);
00822     if (ok)
00823       iconSize = newIconSize;
00824   }
00825 
00826   setIconDimensions(iconSize);
00827 
00828   int index = -1; // append by default. This is very important, otherwise
00829 
00830   // with all 0 indexes, we keep reversing the toolbars.
00831   {
00832     QString attrIndex = element.attribute("index").toLower();
00833     if (!attrIndex.isEmpty())
00834       index = attrIndex.toInt();
00835   }
00836 
00837   bool newLine = d->NewLineDefault;
00838   bool hidden = d->HiddenDefault;
00839 
00840   {
00841     QString attrNewLine = element.attribute("newline").toLower();
00842     if (!attrNewLine.isEmpty())
00843       newLine = attrNewLine == "true";
00844 
00845     if (newLine && mw)
00846       mw->insertToolBarBreak(this);
00847   }
00848 
00849   {
00850     QString attrHidden = element.attribute("hidden").toLower();
00851     if (!attrHidden.isEmpty())
00852       hidden = attrHidden  == "true";
00853   }
00854 
00855   Qt::ToolBarArea pos = Qt::NoToolBarArea;
00856   {
00857     QString attrPosition = element.attribute("position").toLower();
00858     if (!attrPosition.isEmpty())
00859       pos = KToolBar::Private::positionFromString(attrPosition);
00860   }
00861   if (pos != Qt::NoToolBarArea)
00862     mw->addToolBar(pos, this);
00863 
00864   if (hidden)
00865     hide();
00866   else
00867     show();
00868 
00869   if (loadingAppDefaults) {
00870     d->getAttributes(d->PositionDefault, d->ToolButtonStyleDefault, index);
00871 
00872     d->NewLineDefault = newLine;
00873     d->HiddenDefault = hidden;
00874     d->IconSizeDefault = iconSize;
00875   }
00876 }
00877 
00878 void KToolBar::saveState(QDomElement &current) const
00879 {
00880   Q_ASSERT(!current.isNull());
00881 
00882   QString position;
00883   Qt::ToolButtonStyle ToolButtonStyle;
00884   int index = -1;
00885   d->getAttributes(position, ToolButtonStyle, index);
00886   position = position.toLower();
00887 
00888   current.setAttribute("noMerge", "1");
00889   current.setAttribute("position", position);
00890   current.setAttribute("toolButtonStyle", d->toolButtonStyleToString(ToolButtonStyle));
00891   current.setAttribute("index", index);
00892   // FIXME KAction port
00893   //current.setAttribute("newline", newLine());
00894   if (isHidden())
00895     current.setAttribute("hidden", "true");
00896   d->modified = true;
00897 
00898   // TODO if this method is used by more than KXMLGUIBuilder, e.g. to save XML settings to *disk*,
00899   // then the stuff below shouldn't always be done.
00900   current.setAttribute("newlineDefault", d->NewLineDefault);
00901   current.setAttribute("hiddenDefault", d->HiddenDefault ? "true" : "false");
00902   current.setAttribute("iconSizeDefault", d->IconSizeDefault);
00903   current.setAttribute("positionDefault", d->PositionDefault);
00904   current.setAttribute("toolButtonStyleDefault", d->toolButtonStyleToString(d->ToolButtonStyleDefault));
00905 }
00906 
00907 void KToolBar::applySettings(const KConfigGroup &cg, bool force)
00908 {
00909     Q_ASSERT(!cg.name().isEmpty());
00910 
00911   /*
00912     Let's explain this a bit more in details.
00913     The order in which we apply settings is :
00914      Global config / <appnamerc> user settings                        if no XMLGUI is used
00915      Global config / App-XML attributes / <appnamerc> user settings   if XMLGUI is used
00916 
00917     So in the first case, we simply read everything from KConfig as below,
00918     but in the second case we don't do anything here if there is no app-specific config,
00919     and the XMLGUI-related code (loadState()) uses the static methods of this class
00920     to get the global defaults.
00921 
00922     Global config doesn't include position (index, newline and hidden/shown).
00923   */
00924 
00925   // First the appearance stuff - the one which has a global config
00926   applyAppearanceSettings(cg);
00927 
00928   // ...and now the position stuff
00929   if (cg.exists() || force) {
00930 #if 0 // currently unused
00931     QString position = cg.readEntry("Position", d->PositionDefault);
00932     int index = cg.readEntry("Index", int(-1));
00933     bool newLine = cg.readEntry("NewLine", d->NewLineDefault);
00934 
00935     Qt::ToolBarArea pos = Qt::TopToolBarArea;
00936     if (position == "Top")
00937       pos = Qt::TopToolBarArea;
00938     else if (position == "Bottom")
00939       pos = Qt::BottomToolBarArea;
00940     else if (position == "Left")
00941       pos = Qt::LeftToolBarArea;
00942     else if (position == "Right")
00943       pos = Qt::RightToolBarArea;
00944 #endif
00945 
00946     const bool hidden = cg.readEntry("Hidden", d->HiddenDefault);
00947     if (hidden)
00948       hide();
00949     else
00950       show();
00951   }
00952 }
00953 
00954 void KToolBar::applyAppearanceSettings(const KConfigGroup &cg, bool forceGlobal)
00955 {
00956   Q_ASSERT(! cg.name().isEmpty());
00957 
00958   // If we have application-specific settings in the XML file,
00959   // and nothing in the application's config file, then
00960   // we don't apply the global defaults, the XML ones are preferred
00961   // (see applySettings for a full explanation)
00962   // This is the reason for the xmlgui tests below.
00963   const bool xmlgui = d->xmlguiClient && !d->xmlguiClient->xmlFile().isEmpty();
00964 
00965   KSharedConfig::Ptr gconfig = KGlobal::config();
00966 
00967   // we actually do this in two steps.
00968   // First, we read in the global styles [Toolbar style] (from the KControl module).
00969   // Then, if the toolbar is NOT 'mainToolBar', we will also try to read in [barname Toolbar style]
00970   bool applyToolButtonStyle = !xmlgui; // if xmlgui is used, global defaults won't apply
00971   bool applyIconSize = !xmlgui;
00972 
00973   int iconSize = d->IconSizeDefault;
00974   Qt::ToolButtonStyle ToolButtonStyle = d->ToolButtonStyleDefault;
00975 
00976   // this is the first iteration
00977 
00978   { // start block for KConfigGroup
00979     KConfigGroup globals(gconfig, "Toolbar style");
00980 
00981     if (applyToolButtonStyle)
00982     {
00983       // we read in the ToolButtonStyle property *only* if we intend on actually
00984       // honoring it
00985       if (d->honorStyle)
00986       d->ToolButtonStyleDefault = d->toolButtonStyleFromString(globals.readEntry("ToolButtonStyle",
00987                                           d->toolButtonStyleToString(d->ToolButtonStyleDefault)));
00988       else
00989       d->ToolButtonStyleDefault = Qt::ToolButtonTextUnderIcon;
00990 
00991       // Use the default icon size for toolbar icons.
00992       d->IconSizeDefault = globals.readEntry("IconSize", int(d->IconSizeDefault));
00993     }
00994 
00995     iconSize = d->IconSizeDefault;
00996     ToolButtonStyle = d->ToolButtonStyleDefault;
00997 
00998     if (!forceGlobal && cg.exists()) {
00999 
01000       // read in the ToolButtonStyle property
01001       if (cg.hasKey("ToolButtonStyle")) {
01002           ToolButtonStyle = d->toolButtonStyleFromString(cg.readEntry("ToolButtonStyle", QString()));
01003           applyToolButtonStyle = true;
01004       }
01005 
01006       // now get the size
01007       if (cg.hasKey("IconSize")) {
01008           iconSize = cg.readEntry("IconSize", 0);
01009           applyIconSize = true;
01010       }
01011     }
01012   } // end block for KConfigGroup
01013 
01014   // check if the icon/text has changed
01015   if (ToolButtonStyle != toolButtonStyle() && applyToolButtonStyle)
01016     setToolButtonStyle(ToolButtonStyle);
01017 
01018   // ...and check if the icon size has changed
01019   if (iconSize != KToolBar::iconSize().width() && applyIconSize)
01020     setIconDimensions(iconSize);
01021 }
01022 
01023 KMainWindow * KToolBar::mainWindow() const
01024 {
01025   return qobject_cast<KMainWindow*>(const_cast<QObject*>(parent()));
01026 }
01027 
01028 void KToolBar::setIconDimensions(int size)
01029 {
01030   QToolBar::setIconSize(QSize(size, size));
01031 }
01032 
01033 int KToolBar::iconSizeDefault() const
01034 {
01035   if (QObject::objectName() == "mainToolBar")
01036     return KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
01037 
01038   return KIconLoader::global()->currentSize(KIconLoader::Toolbar);
01039 }
01040 
01041 void KToolBar::slotMovableChanged(bool movable)
01042 {
01043   if (movable && !KAuthorized::authorize("movable_toolbars"))
01044     setMovable(false);
01045 }
01046 
01047 void KToolBar::dragEnterEvent(QDragEnterEvent *event)
01048 {
01049   if (toolBarsEditable() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction) &&
01050        event->mimeData()->hasFormat("application/x-kde-action-list")) {
01051     QByteArray data = event->mimeData()->data("application/x-kde-action-list");
01052 
01053     QDataStream stream(data);
01054 
01055     QStringList actionNames;
01056 
01057     stream >> actionNames;
01058 
01059     foreach (const QString& actionName, actionNames) {
01060       foreach (KActionCollection* ac, KActionCollection::allCollections()) {
01061         QAction* newAction = ac->action(actionName.toAscii().constData());
01062         if (newAction) {
01063           d->actionsBeingDragged.append(newAction);
01064           break;
01065         }
01066       }
01067     }
01068 
01069     if (d->actionsBeingDragged.count()) {
01070       QAction* overAction = actionAt(event->pos());
01071 
01072       QFrame* dropIndicatorWidget = new QFrame(this);
01073       dropIndicatorWidget->resize(8, height() - 4);
01074       dropIndicatorWidget->setFrameShape(QFrame::VLine);
01075       dropIndicatorWidget->setLineWidth(3);
01076 
01077       d->dropIndicatorAction = insertWidget(overAction, dropIndicatorWidget);
01078 
01079       insertAction(overAction, d->dropIndicatorAction);
01080 
01081       event->acceptProposedAction();
01082       return;
01083     }
01084   }
01085 
01086   QToolBar::dragEnterEvent(event);
01087 }
01088 
01089 void KToolBar::dragMoveEvent(QDragMoveEvent *event)
01090 {
01091   if (toolBarsEditable())
01092     forever {
01093       if (d->dropIndicatorAction) {
01094         QAction* overAction = 0L;
01095         foreach (QAction* action, actions()) {
01096           // want to make it feel that half way across an action you're dropping on the other side of it
01097           QWidget* widget = widgetForAction(action);
01098           if (event->pos().x() < widget->pos().x() + (widget->width() / 2)) {
01099             overAction = action;
01100             break;
01101           }
01102         }
01103 
01104         if (overAction != d->dropIndicatorAction) {
01105           // Check to see if the indicator is already in the right spot
01106           int dropIndicatorIndex = actions().indexOf(d->dropIndicatorAction);
01107           if (dropIndicatorIndex + 1 < actions().count()) {
01108             if (actions()[ dropIndicatorIndex + 1 ] == overAction)
01109               break;
01110           } else if (!overAction) {
01111             break;
01112           }
01113 
01114           insertAction(overAction, d->dropIndicatorAction);
01115         }
01116 
01117         event->accept();
01118         return;
01119       }
01120       break;
01121     }
01122 
01123   QToolBar::dragMoveEvent(event);
01124 }
01125 
01126 void KToolBar::dragLeaveEvent(QDragLeaveEvent *event)
01127 {
01128   // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
01129   delete d->dropIndicatorAction;
01130   d->dropIndicatorAction = 0L;
01131   d->actionsBeingDragged.clear();
01132 
01133   if (toolBarsEditable()) {
01134     event->accept();
01135     return;
01136   }
01137 
01138   QToolBar::dragLeaveEvent(event);
01139 }
01140 
01141 void KToolBar::dropEvent(QDropEvent *event)
01142 {
01143   if (toolBarsEditable()) {
01144     foreach (QAction* action, d->actionsBeingDragged) {
01145       if (actions().contains(action))
01146         removeAction(action);
01147       insertAction(d->dropIndicatorAction, action);
01148     }
01149   }
01150 
01151   // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
01152   delete d->dropIndicatorAction;
01153   d->dropIndicatorAction = 0L;
01154   d->actionsBeingDragged.clear();
01155 
01156   if (toolBarsEditable()) {
01157     event->accept();
01158     return;
01159   }
01160 
01161   QToolBar::dropEvent(event);
01162 }
01163 
01164 void KToolBar::mousePressEvent(QMouseEvent *event)
01165 {
01166   if (toolBarsEditable() && event->button() == Qt::LeftButton) {
01167     if (KAction* action = qobject_cast<KAction*>(actionAt(event->pos()))) {
01168       d->dragAction = action;
01169       d->dragStartPosition = event->pos();
01170       event->accept();
01171       return;
01172     }
01173   }
01174 
01175   QToolBar::mousePressEvent(event);
01176 }
01177 
01178 void KToolBar::mouseMoveEvent(QMouseEvent *event)
01179 {
01180   if (!toolBarsEditable() || !d->dragAction)
01181     return QToolBar::mouseMoveEvent(event);
01182 
01183   if ((event->pos() - d->dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
01184     event->accept();
01185     return;
01186   }
01187 
01188   QDrag *drag = new QDrag(this);
01189   QMimeData *mimeData = new QMimeData;
01190 
01191   QByteArray data;
01192   {
01193     QDataStream stream(&data, QIODevice::WriteOnly);
01194 
01195     QStringList actionNames;
01196     actionNames << d->dragAction->objectName();
01197 
01198     stream << actionNames;
01199   }
01200 
01201   mimeData->setData("application/x-kde-action-list", data);
01202 
01203   drag->setMimeData(mimeData);
01204 
01205   Qt::DropAction dropAction = drag->start(Qt::MoveAction);
01206 
01207   if (dropAction == Qt::MoveAction)
01208     // Only remove from this toolbar if it was moved to another toolbar
01209     // Otherwise the receiver moves it.
01210     if (drag->target() != this)
01211       removeAction(d->dragAction);
01212 
01213   d->dragAction = 0L;
01214   event->accept();
01215 }
01216 
01217 void KToolBar::mouseReleaseEvent(QMouseEvent *event)
01218 {
01219   // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
01220   if (d->dragAction) {
01221     d->dragAction = 0L;
01222     event->accept();
01223     return;
01224   }
01225 
01226   QToolBar::mouseReleaseEvent(event);
01227 }
01228 
01229 bool KToolBar::eventFilter(QObject * watched, QEvent * event)
01230 {
01231     // Generate context menu events for disabled buttons too...
01232     if (event->type() == QEvent::MouseButtonPress) {
01233         QMouseEvent* me = static_cast<QMouseEvent*>(event);
01234         if (me->buttons() & Qt::RightButton)
01235             if (QWidget* ww = qobject_cast<QWidget*>(watched))
01236                 if (ww->parent() == this && !ww->isEnabled())
01237                     QCoreApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, me->pos(), me->globalPos()));
01238 
01239     } else if (event->type() == QEvent::ParentChange) {
01240         // Make sure we're not leaving stale event filters around,
01241         // when a child is reparented somewhere else
01242         if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
01243             if (!this->isAncestorOf(ww)) {
01244                 // New parent is not a subwidget - remove event filter
01245                 ww->removeEventFilter(this);
01246                 foreach (QWidget* child, ww->findChildren<QWidget*>())
01247                     child->removeEventFilter(this);
01248             }
01249         }
01250     }
01251 
01252     QToolButton* tb;
01253     if ((tb = qobject_cast<QToolButton*>(watched)) && !tb->actions().isEmpty()) {
01254         // Handle MMB on toolbar buttons
01255         QAction* act = tb->actions().first();
01256         if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
01257             QMouseEvent* me = static_cast<QMouseEvent*>(event);
01258             if (me->button() == Qt::MidButton /*&&
01259                                                  act->receivers(SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)))*/) {
01260                 if (me->type() == QEvent::MouseButtonPress)
01261                     tb->setDown(true);
01262                 else {
01263                     tb->setDown(false);
01264                     QMetaObject::invokeMethod(act, "triggered", Qt::DirectConnection,
01265                                               Q_ARG(Qt::MouseButtons, me->button()),
01266                                               Q_ARG(Qt::KeyboardModifiers, QApplication::keyboardModifiers()));
01267                 }
01268             }
01269         }
01270 
01271         // CJK languages use more verbose accelerator marker: they add a Latin
01272         // letter in parenthesis, and put accelerator on that. Hence, the default
01273         // removal of ampersand only may not be enough there, instead the whole
01274         // parenthesis construct should be removed. Use KLocale's method to do this.
01275         if (event->type() == QEvent::Show || event->type() == QEvent::Paint || event->type() == QEvent::EnabledChange) {
01276             act = tb->defaultAction();
01277             if (act) {
01278                 const QString text = KGlobal::locale()->removeAcceleratorMarker(act->iconText().isEmpty() ? act->text() : act->iconText());
01279                 const QString toolTip = KGlobal::locale()->removeAcceleratorMarker(act->toolTip());
01280                 // Filtering messages requested by translators (scripting).
01281                 tb->setText(i18nc("@action:intoolbar Text label of toolbar button", "%1", text));
01282                 tb->setToolTip(i18nc("@info:tooltip Tooltip of toolbar button", "%1", toolTip));
01283             }
01284         }
01285     }
01286 
01287     // Redirect mouse events to the toolbar when drag + drop editing is enabled
01288     if (toolBarsEditable()) {
01289         if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
01290             switch (event->type()) {
01291             case QEvent::MouseButtonPress: {
01292                 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01293                 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01294                                       me->button(), me->buttons(), me->modifiers());
01295                 mousePressEvent(&newEvent);
01296                 return true;
01297             }
01298             case QEvent::MouseMove: {
01299                 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01300                 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01301                                       me->button(), me->buttons(), me->modifiers());
01302                 mouseMoveEvent(&newEvent);
01303                 return true;
01304             }
01305             case QEvent::MouseButtonRelease: {
01306                 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01307                 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01308                                       me->button(), me->buttons(), me->modifiers());
01309                 mouseReleaseEvent(&newEvent);
01310                 return true;
01311             }
01312             default:
01313                 break;
01314             }
01315         }
01316     }
01317 
01318     return QToolBar::eventFilter(watched, event);
01319 }
01320 
01321 void KToolBar::actionEvent(QActionEvent * event)
01322 {
01323   if (event->type() == QEvent::ActionRemoved) {
01324     QWidget* widget = widgetForAction(event->action());
01325     if (widget) {
01326         widget->removeEventFilter(this);
01327 
01328         foreach (QWidget* child, widget->findChildren<QWidget*>())
01329             child->removeEventFilter(this);
01330     }
01331   }
01332 
01333   QToolBar::actionEvent(event);
01334 
01335   if (event->type() == QEvent::ActionAdded) {
01336     QWidget* widget = widgetForAction(event->action());
01337     if (widget) {
01338         widget->installEventFilter(this);
01339 
01340         foreach (QWidget* child, widget->findChildren<QWidget*>())
01341             child->installEventFilter(this);
01342     }
01343   }
01344 
01345   d->adjustSeparatorVisibility();
01346 }
01347 
01348 bool KToolBar::toolBarsEditable()
01349 {
01350   return KToolBar::Private::s_editable;
01351 }
01352 
01353 void KToolBar::setToolBarsEditable(bool editable)
01354 {
01355   if (KToolBar::Private::s_editable != editable)
01356     KToolBar::Private::s_editable = editable;
01357 }
01358 
01359 void KToolBar::setToolBarsLocked(bool locked)
01360 {
01361   if (KToolBar::Private::s_locked != locked) {
01362     KToolBar::Private::s_locked = locked;
01363 
01364     foreach (KMainWindow* mw, KMainWindow::memberList())
01365       foreach (KToolBar* toolbar, mw->findChildren<KToolBar*>()) {
01366         toolbar->d->setLocked(locked);
01367         if (toolbar->d->contextLockAction)
01368             toolbar->d->contextLockAction->setText(locked ? i18n("Unlock Toolbars") : i18n("Lock Toolbars"));
01369       }
01370 
01371   }
01372 }
01373 
01374 bool KToolBar::toolBarsLocked()
01375 {
01376   return KToolBar::Private::s_locked;
01377 }
01378 
01379 #include "ktoolbar.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