00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 enum SettingLevel { Level_KDEDefault, Level_AppXML, Level_UserSettings,
00086 NSettingLevels };
00087 enum { Unset = -1 };
00088
00089 class KToolBar::Private
00090 {
00091 public:
00092 Private(KToolBar *qq)
00093 : q(qq),
00094 isMainToolBar(false),
00095 enableContext(true),
00096 unlockedMovable(true),
00097 xmlguiClient(0),
00098 contextLockAction(0),
00099 dropIndicatorAction(0),
00100 context(0),
00101 dragAction(0)
00102 {
00103 }
00104
00105 void slotAppearanceChanged();
00106 void slotContextAboutToShow();
00107 void slotContextAboutToHide();
00108 void slotContextLeft();
00109 void slotContextRight();
00110 void slotContextTop();
00111 void slotContextBottom();
00112 void slotContextIcons();
00113 void slotContextText();
00114 void slotContextTextRight();
00115 void slotContextTextUnder();
00116 void slotContextIconSize();
00117 void slotLockToolBars(bool lock);
00118
00119 void init(bool readConfig = true, bool isMainToolBar = false);
00120 QString getPositionAsString() const;
00121 KMenu *contextMenu();
00122 void setLocked(bool locked);
00123 void adjustSeparatorVisibility();
00124 void loadKDESettings();
00125 void applyCurrentSettings();
00126
00127 static Qt::ToolButtonStyle toolButtonStyleFromString(const QString& style);
00128 static QString toolButtonStyleToString(Qt::ToolButtonStyle);
00129 static Qt::ToolBarArea positionFromString(const QString& position);
00130
00131 KToolBar *q;
00132 bool isMainToolBar : 1;
00133 bool enableContext : 1;
00134 bool unlockedMovable : 1;
00135 static bool s_editable;
00136 static bool s_locked;
00137
00138 KXMLGUIClient *xmlguiClient;
00139
00140 QMenu* contextOrient;
00141 QMenu* contextMode;
00142 QMenu* contextSize;
00143
00144 QAction* contextTop;
00145 QAction* contextLeft;
00146 QAction* contextRight;
00147 QAction* contextBottom;
00148 QAction* contextIcons;
00149 QAction* contextTextRight;
00150 QAction* contextText;
00151 QAction* contextTextUnder;
00152 KToggleAction* contextLockAction;
00153 QMap<QAction*,int> contextIconSizes;
00154
00155 class IntSetting
00156 {
00157 public:
00158 IntSetting() {
00159 for (int level = 0; level < NSettingLevels; ++level) {
00160 values[level] = Unset;
00161 }
00162 }
00163 int currentValue() const {
00164 int val = Unset;
00165 for (int level = 0; level < NSettingLevels; ++level) {
00166 if (values[level] != Unset)
00167 val = values[level];
00168 }
00169 return val;
00170 }
00171
00172
00173 int defaultValue() const {
00174 int val = Unset;
00175 for (int level = 0; level < Level_UserSettings; ++level) {
00176 if (values[level] != Unset)
00177 val = values[level];
00178 }
00179 return val;
00180 }
00181 QString toString() const {
00182 QString str;
00183 for (int level = 0; level < NSettingLevels; ++level) {
00184 str += QString::number(values[level]) + ' ';
00185 }
00186 return str;
00187 }
00188 int& operator[](int index) { return values[index]; }
00189 private:
00190 int values[NSettingLevels];
00191 };
00192 IntSetting iconSizeSettings;
00193 IntSetting toolButtonStyleSettings;
00194
00195 QList<QAction*> actionsBeingDragged;
00196 QAction* dropIndicatorAction;
00197
00198 KMenu* context;
00199 KAction* dragAction;
00200 QPoint dragStartPosition;
00201 };
00202
00203 bool KToolBar::Private::s_editable = false;
00204 bool KToolBar::Private::s_locked = false;
00205
00206 void KToolBar::Private::init(bool readConfig, bool _isMainToolBar)
00207 {
00208 isMainToolBar = _isMainToolBar;
00209 loadKDESettings();
00210
00211
00212
00213 if (readConfig) {
00214 KConfigGroup cg(KGlobal::config(), QString());
00215 q->applySettings(cg);
00216 }
00217
00218 if (q->mainWindow()) {
00219
00220 connect(q, SIGNAL(allowedAreasChanged(Qt::ToolBarAreas)),
00221 q->mainWindow(), SLOT(setSettingsDirty()));
00222 connect(q, SIGNAL(iconSizeChanged(const QSize&)),
00223 q->mainWindow(), SLOT(setSettingsDirty()));
00224 connect(q, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
00225 q->mainWindow(), SLOT(setSettingsDirty()));
00226 connect(q, SIGNAL(movableChanged(bool)),
00227 q->mainWindow(), SLOT(setSettingsDirty()));
00228 connect(q, SIGNAL(orientationChanged(Qt::Orientation)),
00229 q->mainWindow(), SLOT(setSettingsDirty()));
00230 }
00231
00232 if (!KAuthorized::authorize("movable_toolbars"))
00233 q->setMovable(false);
00234 else
00235 q->setMovable(!KToolBar::toolBarsLocked());
00236
00237 connect(q, SIGNAL(movableChanged(bool)),
00238 q, SLOT(slotMovableChanged(bool)));
00239
00240 q->setAcceptDrops(true);
00241
00242 connect(KGlobalSettings::self(), SIGNAL(toolbarAppearanceChanged(int)),
00243 q, SLOT(slotAppearanceChanged()));
00244 connect(KIconLoader::global(), SIGNAL(iconLoaderSettingsChanged()),
00245 q, SLOT(slotAppearanceChanged()));
00246 }
00247
00248 QString KToolBar::Private::getPositionAsString() const
00249 {
00250
00251 switch (q->mainWindow()->toolBarArea(const_cast<KToolBar*>(q))) {
00252 case Qt::BottomToolBarArea:
00253 return "Bottom";
00254 case Qt::LeftToolBarArea:
00255 return "Left";
00256 case Qt::RightToolBarArea:
00257 return "Right";
00258 case Qt::TopToolBarArea:
00259 default:
00260 return "Top";
00261 }
00262 }
00263
00264 KMenu *KToolBar::Private::contextMenu()
00265 {
00266 if (!context) {
00267 context = new KMenu(q);
00268 context->addTitle(i18n("Toolbar Menu"));
00269
00270 contextOrient = new KMenu(i18n("Orientation"), context);
00271 context->addMenu(contextOrient);
00272
00273 contextTop = contextOrient->addAction(i18nc("toolbar position string", "Top"), q, SLOT(slotContextTop()));
00274 contextTop->setChecked(true);
00275 contextLeft = contextOrient->addAction(i18nc("toolbar position string", "Left"), q, SLOT(slotContextLeft()));
00276 contextRight = contextOrient->addAction(i18nc("toolbar position string", "Right"), q, SLOT(slotContextRight()));
00277 contextBottom = contextOrient->addAction(i18nc("toolbar position string", "Bottom"), q, SLOT(slotContextBottom()));
00278
00279 QActionGroup* positionGroup = new QActionGroup(contextOrient);
00280 foreach (QAction* action, contextOrient->actions()) {
00281 action->setActionGroup(positionGroup);
00282 action->setCheckable(true);
00283 }
00284
00285 contextMode = new KMenu(i18n("Text Position"), context);
00286 context->addMenu(contextMode);
00287
00288 contextIcons = contextMode->addAction(i18n("Icons Only"), q, SLOT(slotContextIcons()));
00289 contextText = contextMode->addAction(i18n("Text Only"), q, SLOT(slotContextText()));
00290 contextTextRight = contextMode->addAction(i18n("Text Alongside Icons"), q, SLOT(slotContextTextRight()));
00291 contextTextUnder = contextMode->addAction(i18n("Text Under Icons"), q, SLOT(slotContextTextUnder()));
00292
00293 QActionGroup* textGroup = new QActionGroup(contextMode);
00294 foreach (QAction* action, contextMode->actions()) {
00295 action->setActionGroup(textGroup);
00296 action->setCheckable(true);
00297 }
00298
00299 contextSize = new KMenu(i18n("Icon Size"), context);
00300 context->addMenu(contextSize);
00301
00302 contextIconSizes.insert(contextSize->addAction(i18nc("@item:inmenu Icon size", "Default"), q, SLOT(slotContextIconSize())),
00303 iconSizeSettings.defaultValue());
00304
00305
00306 KIconTheme *theme = KIconLoader::global()->theme();
00307 QList<int> avSizes;
00308 if (theme) {
00309 avSizes = theme->querySizes(isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
00310 }
00311
00312 qSort(avSizes);
00313
00314 if (avSizes.count() < 10) {
00315
00316 foreach (int it, avSizes) {
00317 QString text;
00318 if (it < 19)
00319 text = i18n("Small (%1x%2)", it, it);
00320 else if (it < 25)
00321 text = i18n("Medium (%1x%2)", it, it);
00322 else if (it < 35)
00323 text = i18n("Large (%1x%2)", it, it);
00324 else
00325 text = i18n("Huge (%1x%2)", it, it);
00326
00327
00328 contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
00329 }
00330 } else {
00331
00332 const int progression[] = { 16, 22, 32, 48, 64, 96, 128, 192, 256 };
00333
00334 for (uint i = 0; i < 9; i++) {
00335 foreach (int it, avSizes) {
00336 if (it >= progression[ i ]) {
00337 QString text;
00338 if (it < 19)
00339 text = i18n("Small (%1x%2)", it, it);
00340 else if (it < 25)
00341 text = i18n("Medium (%1x%2)", it, it);
00342 else if (it < 35)
00343 text = i18n("Large (%1x%2)", it, it);
00344 else
00345 text = i18n("Huge (%1x%2)", it, it);
00346
00347
00348 contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
00349 break;
00350 }
00351 }
00352 }
00353 }
00354
00355 QActionGroup* sizeGroup = new QActionGroup(contextSize);
00356 foreach (QAction* action, contextSize->actions()) {
00357 action->setActionGroup(sizeGroup);
00358 action->setCheckable(true);
00359 }
00360
00361 if (!q->toolBarsLocked() && !q->isMovable())
00362 unlockedMovable = false;
00363
00364 delete contextLockAction;
00365 contextLockAction = new KToggleAction(KIcon("system-lock-screen"), (q->toolBarsLocked())?i18n("Unlock Toolbars"):i18n("Lock Toolbars"), q);
00366 context->addAction(contextLockAction);
00367 contextLockAction->setChecked(q->toolBarsLocked());
00368 connect(contextLockAction, SIGNAL(toggled(bool)), q, SLOT(slotLockToolBars(bool)));
00369
00370 connect(context, SIGNAL(aboutToShow()), q, SLOT(slotContextAboutToShow()));
00371 }
00372
00373 contextOrient->menuAction()->setVisible(!q->toolBarsLocked());
00374 contextMode->menuAction()->setVisible(!q->toolBarsLocked());
00375 contextSize->menuAction()->setVisible(!q->toolBarsLocked());
00376
00377
00378
00379
00380
00381 return context;
00382 }
00383
00384 void KToolBar::Private::setLocked(bool locked)
00385 {
00386 if (unlockedMovable)
00387 q->setMovable(!locked);
00388 }
00389
00390 void KToolBar::Private::adjustSeparatorVisibility()
00391 {
00392 bool visibleNonSeparator = false;
00393 int separatorToShow = -1;
00394
00395 for (int index = 0; index < q->actions().count(); ++index) {
00396 QAction* action = q->actions()[ index ];
00397 if (action->isSeparator()) {
00398 if (visibleNonSeparator) {
00399 separatorToShow = index;
00400 visibleNonSeparator = false;
00401 } else {
00402 action->setVisible(false);
00403 }
00404 } else if (!visibleNonSeparator) {
00405 if (action->isVisible()) {
00406 visibleNonSeparator = true;
00407 if (separatorToShow != -1) {
00408 q->actions()[ separatorToShow ]->setVisible(true);
00409 separatorToShow = -1;
00410 }
00411 }
00412 }
00413 }
00414
00415 if (separatorToShow != -1)
00416 q->actions()[ separatorToShow ]->setVisible(false);
00417 }
00418
00419 Qt::ToolButtonStyle KToolBar::Private::toolButtonStyleFromString(const QString & _style)
00420 {
00421 QString style = _style.toLower();
00422 if (style == "textbesideicon" || style == "icontextright")
00423 return Qt::ToolButtonTextBesideIcon;
00424 else if (style == "textundericon" || style == "icontextbottom")
00425 return Qt::ToolButtonTextUnderIcon;
00426 else if (style == "textonly")
00427 return Qt::ToolButtonTextOnly;
00428 else
00429 return Qt::ToolButtonIconOnly;
00430 }
00431
00432 QString KToolBar::Private::toolButtonStyleToString(Qt::ToolButtonStyle style)
00433 {
00434 switch(style)
00435 {
00436 case Qt::ToolButtonIconOnly:
00437 default:
00438 return "IconOnly";
00439 case Qt::ToolButtonTextBesideIcon:
00440 return "TextBesideIcon";
00441 case Qt::ToolButtonTextOnly:
00442 return "TextOnly";
00443 case Qt::ToolButtonTextUnderIcon:
00444 return "TextUnderIcon";
00445 }
00446 }
00447
00448 Qt::ToolBarArea KToolBar::Private::positionFromString(const QString& position)
00449 {
00450 Qt::ToolBarArea newposition = Qt::TopToolBarArea;
00451 if (position == QLatin1String("left")) {
00452 newposition = Qt::LeftToolBarArea;
00453 } else if (position == QLatin1String("bottom")) {
00454 newposition = Qt::BottomToolBarArea;
00455 } else if (position == QLatin1String("right")) {
00456 newposition = Qt::RightToolBarArea;
00457 }
00458 return newposition;
00459 }
00460
00461
00462 void KToolBar::Private::slotAppearanceChanged()
00463 {
00464 loadKDESettings();
00465 applyCurrentSettings();
00466 }
00467
00468 void KToolBar::Private::loadKDESettings()
00469 {
00470 iconSizeSettings[Level_KDEDefault] = q->iconSizeDefault();
00471
00472 if (isMainToolBar) {
00473 toolButtonStyleSettings[Level_KDEDefault] = q->toolButtonStyleSetting();
00474 } else {
00475 const QString fallBack = "TextBesideIcon";
00491 KConfigGroup group(KGlobal::config(), "Toolbar style");
00492 const QString value = group.readEntry("ToolButtonStyleOtherToolbars", fallBack);
00493 toolButtonStyleSettings[Level_KDEDefault] = KToolBar::Private::toolButtonStyleFromString(value);
00494 }
00495 }
00496
00497
00498 void KToolBar::Private::applyCurrentSettings()
00499 {
00500
00501 const int currentIconSize = iconSizeSettings.currentValue();
00502 q->setIconSize(QSize(currentIconSize, currentIconSize));
00503
00504 q->setToolButtonStyle(static_cast<Qt::ToolButtonStyle>(toolButtonStyleSettings.currentValue()));
00505
00506
00507 KMainWindow *kmw = q->mainWindow();
00508 if (kmw)
00509 kmw->setSettingsDirty();
00510 }
00511
00512 void KToolBar::Private::slotContextAboutToShow()
00513 {
00521 KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
00522 if (kmw) {
00523 kmw->setupToolbarMenuActions();
00524
00525 QAction *tbAction = kmw->toolBarMenuAction();
00526 if (!q->toolBarsLocked() && tbAction && tbAction->associatedWidgets().count() > 0)
00527 contextMenu()->addAction(tbAction);
00528 }
00529
00530
00531 QAction *configureAction = 0;
00532 const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
00533 if (xmlguiClient)
00534 configureAction = xmlguiClient->actionCollection()->action(actionName);
00535
00536 if (!configureAction && kmw)
00537 configureAction = kmw->actionCollection()->action(actionName);
00538
00539 if (configureAction)
00540 context->addAction(configureAction);
00541
00542 KEditToolBar::setGlobalDefaultToolBar(q->QObject::objectName().toLatin1().constData());
00543
00544
00545 switch (q->toolButtonStyle()) {
00546 case Qt::ToolButtonIconOnly:
00547 default:
00548 contextIcons->setChecked(true);
00549 break;
00550 case Qt::ToolButtonTextBesideIcon:
00551 contextTextRight->setChecked(true);
00552 break;
00553 case Qt::ToolButtonTextOnly:
00554 contextText->setChecked(true);
00555 break;
00556 case Qt::ToolButtonTextUnderIcon:
00557 contextTextUnder->setChecked(true);
00558 break;
00559 }
00560
00561 QMapIterator< QAction*, int > it = contextIconSizes;
00562 while (it.hasNext()) {
00563 it.next();
00564 if (it.value() == q->iconSize().width()) {
00565 it.key()->setChecked(true);
00566 break;
00567 }
00568 }
00569
00570 switch (q->mainWindow()->toolBarArea(q)) {
00571 case Qt::BottomToolBarArea:
00572 contextBottom->setChecked(true);
00573 break;
00574 case Qt::LeftToolBarArea:
00575 contextLeft->setChecked(true);
00576 break;
00577 case Qt::RightToolBarArea:
00578 contextRight->setChecked(true);
00579 break;
00580 default:
00581 case Qt::TopToolBarArea:
00582 contextTop->setChecked(true);
00583 break;
00584 }
00585 }
00586
00587 void KToolBar::Private::slotContextAboutToHide()
00588 {
00589
00590
00591 KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
00592 if (kmw && kmw->toolBarMenuAction())
00593 if (kmw->toolBarMenuAction()->associatedWidgets().count() > 1)
00594 contextMenu()->removeAction(kmw->toolBarMenuAction());
00595
00596
00597 QAction *configureAction = 0;
00598 const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
00599 if (xmlguiClient)
00600 configureAction = xmlguiClient->actionCollection()->action(actionName);
00601
00602 if (!configureAction && kmw)
00603 configureAction = kmw->actionCollection()->action(actionName);
00604
00605 if (configureAction)
00606 context->removeAction(configureAction);
00607 }
00608
00609 void KToolBar::Private::slotContextLeft()
00610 {
00611 q->mainWindow()->addToolBar(Qt::LeftToolBarArea, q);
00612 }
00613
00614 void KToolBar::Private::slotContextRight()
00615 {
00616 q->mainWindow()->addToolBar(Qt::RightToolBarArea, q);
00617 }
00618
00619 void KToolBar::Private::slotContextTop()
00620 {
00621 q->mainWindow()->addToolBar(Qt::TopToolBarArea, q);
00622 }
00623
00624 void KToolBar::Private::slotContextBottom()
00625 {
00626 q->mainWindow()->addToolBar(Qt::BottomToolBarArea, q);
00627 }
00628
00629 void KToolBar::Private::slotContextIcons()
00630 {
00631 q->setToolButtonStyle(Qt::ToolButtonIconOnly);
00632 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
00633 }
00634
00635 void KToolBar::Private::slotContextText()
00636 {
00637 q->setToolButtonStyle(Qt::ToolButtonTextOnly);
00638 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
00639 }
00640
00641 void KToolBar::Private::slotContextTextUnder()
00642 {
00643 q->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
00644 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
00645 }
00646
00647 void KToolBar::Private::slotContextTextRight()
00648 {
00649 q->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00650 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
00651 }
00652
00653 void KToolBar::Private::slotContextIconSize()
00654 {
00655 QAction* action = qobject_cast<QAction*>(q->sender());
00656 if (action && contextIconSizes.contains(action)) {
00657 const int iconSize = contextIconSizes.value(action);
00658 q->setIconDimensions(iconSize);
00659 }
00660 }
00661
00662 void KToolBar::Private::slotLockToolBars(bool lock)
00663 {
00664 q->setToolBarsLocked(lock);
00665 }
00666
00667
00668
00669 KToolBar::KToolBar(QWidget *parent, bool isMainToolBar, bool readConfig)
00670 : QToolBar(parent),
00671 d(new Private(this))
00672 {
00673 d->init(readConfig, isMainToolBar);
00674
00675
00676 if (QMainWindow* mw = qobject_cast<QMainWindow*>(parent))
00677 mw->addToolBar(this);
00678 }
00679
00680 KToolBar::KToolBar(const QString& objectName, QWidget *parent, bool readConfig)
00681 : QToolBar(parent),
00682 d(new Private(this))
00683 {
00684 setObjectName(objectName);
00685
00686
00687 d->init(readConfig, objectName == "mainToolBar");
00688
00689
00690 if (QMainWindow* mw = qobject_cast<QMainWindow*>(parent))
00691 mw->addToolBar(this);
00692 }
00693
00694 KToolBar::KToolBar(const QString& objectName, QMainWindow* parent, Qt::ToolBarArea area,
00695 bool newLine, bool isMainToolBar, bool readConfig)
00696 : QToolBar(parent),
00697 d(new Private(this))
00698 {
00699 setObjectName(objectName);
00700 d->init(readConfig, isMainToolBar);
00701
00702 if (newLine)
00703 mainWindow()->addToolBarBreak(area);
00704
00705 mainWindow()->addToolBar(area, this);
00706
00707 if (newLine)
00708 mainWindow()->addToolBarBreak(area);
00709 }
00710
00711 KToolBar::~KToolBar()
00712 {
00713 delete d->contextLockAction;
00714 delete d;
00715 }
00716
00717 void KToolBar::setContextMenuEnabled(bool enable)
00718 {
00719 d->enableContext = enable;
00720 }
00721
00722 bool KToolBar::contextMenuEnabled() const
00723 {
00724 return d->enableContext;
00725 }
00726
00727 void KToolBar::saveSettings(KConfigGroup &cg)
00728 {
00729 Q_ASSERT(!cg.name().isEmpty());
00730
00731 cg.deleteEntry("Hidden");
00732
00733 const int currentIconSize = iconSize().width();
00734
00735 if (!cg.hasDefault("IconSize") && currentIconSize == d->iconSizeSettings.defaultValue()) {
00736 cg.revertToDefault("IconSize");
00737 d->iconSizeSettings[Level_UserSettings] = Unset;
00738 } else {
00739 cg.writeEntry("IconSize", currentIconSize);
00740 d->iconSizeSettings[Level_UserSettings] = currentIconSize;
00741 }
00742
00743 const Qt::ToolButtonStyle currentToolButtonStyle = toolButtonStyle();
00744 if (!cg.hasDefault("ToolButtonStyle") && currentToolButtonStyle == d->toolButtonStyleSettings.defaultValue()) {
00745 cg.revertToDefault("ToolButtonStyle");
00746 d->toolButtonStyleSettings[Level_UserSettings] = Unset;
00747 } else {
00748 cg.writeEntry("ToolButtonStyle", d->toolButtonStyleToString(currentToolButtonStyle));
00749 d->toolButtonStyleSettings[Level_UserSettings] = currentToolButtonStyle;
00750 }
00751
00752 }
00753
00754 void KToolBar::setXMLGUIClient(KXMLGUIClient *client)
00755 {
00756 d->xmlguiClient = client;
00757 }
00758
00759 void KToolBar::contextMenuEvent(QContextMenuEvent* event)
00760 {
00761 if (mainWindow() && d->enableContext) {
00762 QPointer<KToolBar> guard(this);
00763 d->contextMenu()->exec(event->globalPos());
00764
00765
00766 if (guard)
00767 d->slotContextAboutToHide();
00768 return;
00769 }
00770
00771 QToolBar::contextMenuEvent(event);
00772 }
00773
00774 Qt::ToolButtonStyle KToolBar::toolButtonStyleSetting()
00775 {
00776 KConfigGroup group(KGlobal::config(), "Toolbar style");
00777 return KToolBar::Private::toolButtonStyleFromString(group.readEntry("ToolButtonStyle", "TextUnderIcon"));
00778 }
00779
00780 void KToolBar::loadState(const QDomElement &element)
00781 {
00782 QMainWindow *mw = mainWindow();
00783 if (!mw)
00784 return;
00785
00786 {
00787 QDomNode textNode = element.namedItem("text");
00788 QByteArray text;
00789 QByteArray context;
00790 if (textNode.isElement())
00791 {
00792 QDomElement textElement = textNode.toElement();
00793 text = textElement.text().toUtf8();
00794 context = textElement.attribute("context").toUtf8();
00795 }
00796 else
00797 {
00798 textNode = element.namedItem("Text");
00799 if (textNode.isElement())
00800 {
00801 QDomElement textElement = textNode.toElement();
00802 text = textElement.text().toUtf8();
00803 context = textElement.attribute("context").toUtf8();
00804 }
00805 }
00806
00807 QString i18nText;
00808 if (!text.isEmpty() && !context.isEmpty())
00809 i18nText = i18nc(context, text);
00810 else if (!text.isEmpty())
00811 i18nText = i18n(text);
00812
00813 if (!i18nText.isEmpty())
00814 setWindowTitle(i18nText);
00815 }
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 bool loadingAppDefaults = true;
00830 if (element.hasAttribute("tempXml")) {
00831
00832 loadingAppDefaults = false;
00833 const QString iconSizeDefault = element.attribute("iconSizeDefault");
00834 if (!iconSizeDefault.isEmpty()) {
00835 d->iconSizeSettings[Level_AppXML] = iconSizeDefault.toInt();
00836 }
00837 const QString toolButtonStyleDefault = element.attribute("toolButtonStyleDefault");
00838 if (!toolButtonStyleDefault.isEmpty()) {
00839 d->toolButtonStyleSettings[Level_AppXML] = d->toolButtonStyleFromString(toolButtonStyleDefault);
00840 }
00841 } else {
00842
00843 bool newLine = false;
00844 QString attrNewLine = element.attribute("newline").toLower();
00845 if (!attrNewLine.isEmpty())
00846 newLine = attrNewLine == "true";
00847 if (newLine && mw)
00848 mw->insertToolBarBreak(this);
00849 }
00850
00851 int newIconSize = -1;
00852 if (element.hasAttribute("iconSize")) {
00853 bool ok;
00854 newIconSize = element.attribute("iconSize").trimmed().toInt(&ok);
00855 if (!ok)
00856 newIconSize = -1;
00857 }
00858 if (newIconSize != -1)
00859 d->iconSizeSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = newIconSize;
00860
00861 const QString newToolButtonStyle = element.attribute("iconText");
00862 if (!newToolButtonStyle.isEmpty())
00863 d->toolButtonStyleSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = d->toolButtonStyleFromString(newToolButtonStyle);
00864
00865 bool hidden = false;
00866 {
00867 QString attrHidden = element.attribute("hidden").toLower();
00868 if (!attrHidden.isEmpty())
00869 hidden = attrHidden == "true";
00870 }
00871
00872 Qt::ToolBarArea pos = Qt::NoToolBarArea;
00873 {
00874 QString attrPosition = element.attribute("position").toLower();
00875 if (!attrPosition.isEmpty())
00876 pos = KToolBar::Private::positionFromString(attrPosition);
00877 }
00878 if (pos != Qt::NoToolBarArea)
00879 mw->addToolBar(pos, this);
00880
00881 if (hidden)
00882 hide();
00883 else
00884 show();
00885
00886 d->applyCurrentSettings();
00887 }
00888
00889
00890
00891 void KToolBar::saveState(QDomElement ¤t) const
00892 {
00893 Q_ASSERT(!current.isNull());
00894
00895 current.setAttribute("tempXml", "true");
00896
00897 current.setAttribute("noMerge", "1");
00898 current.setAttribute("position", d->getPositionAsString().toLower());
00899 current.setAttribute("hidden", isHidden() ? "true" : "false");
00900
00901 const int currentIconSize = iconSize().width();
00902 if (currentIconSize == d->iconSizeSettings.defaultValue())
00903 current.removeAttribute("iconSize");
00904 else
00905 current.setAttribute("iconSize", iconSize().width());
00906
00907 if (toolButtonStyle() == d->toolButtonStyleSettings.defaultValue())
00908 current.removeAttribute("iconText");
00909 else
00910 current.setAttribute("iconText", d->toolButtonStyleToString(toolButtonStyle()));
00911
00912
00913
00914 if (d->iconSizeSettings[Level_AppXML] != Unset) {
00915 current.setAttribute("iconSizeDefault", d->iconSizeSettings[Level_AppXML]);
00916 }
00917 if (d->toolButtonStyleSettings[Level_AppXML] != Unset) {
00918 const Qt::ToolButtonStyle bs = static_cast<Qt::ToolButtonStyle>(d->toolButtonStyleSettings[Level_AppXML]);
00919 current.setAttribute("toolButtonStyleDefault", d->toolButtonStyleToString(bs));
00920 }
00921 }
00922
00923
00924 void KToolBar::applySettings(const KConfigGroup &cg, bool forceGlobal)
00925 {
00926 Q_ASSERT(!cg.name().isEmpty());
00927 Q_UNUSED(forceGlobal);
00928
00929
00930
00931
00932 if (cg.hasKey("Hidden")) {
00933 const bool hidden = cg.readEntry("Hidden", false);
00934 if (hidden)
00935 hide();
00936 else {
00937 show();
00938 }
00939 }
00940
00941 if (cg.hasKey("IconSize")) {
00942 d->iconSizeSettings[Level_UserSettings] = cg.readEntry("IconSize", 0);
00943 }
00944 if (cg.hasKey("ToolButtonStyle")) {
00945 d->toolButtonStyleSettings[Level_UserSettings] = d->toolButtonStyleFromString(cg.readEntry("ToolButtonStyle", QString()));
00946 }
00947
00948 d->applyCurrentSettings();
00949 }
00950
00951 KMainWindow * KToolBar::mainWindow() const
00952 {
00953 return qobject_cast<KMainWindow*>(const_cast<QObject*>(parent()));
00954 }
00955
00956 void KToolBar::setIconDimensions(int size)
00957 {
00958 QToolBar::setIconSize(QSize(size, size));
00959 d->iconSizeSettings[Level_UserSettings] = size;
00960 }
00961
00962 int KToolBar::iconSizeDefault() const
00963 {
00964 return KIconLoader::global()->currentSize(d->isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
00965 }
00966
00967 void KToolBar::slotMovableChanged(bool movable)
00968 {
00969 if (movable && !KAuthorized::authorize("movable_toolbars"))
00970 setMovable(false);
00971 }
00972
00973 void KToolBar::dragEnterEvent(QDragEnterEvent *event)
00974 {
00975 if (toolBarsEditable() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction) &&
00976 event->mimeData()->hasFormat("application/x-kde-action-list")) {
00977 QByteArray data = event->mimeData()->data("application/x-kde-action-list");
00978
00979 QDataStream stream(data);
00980
00981 QStringList actionNames;
00982
00983 stream >> actionNames;
00984
00985 foreach (const QString& actionName, actionNames) {
00986 foreach (KActionCollection* ac, KActionCollection::allCollections()) {
00987 QAction* newAction = ac->action(actionName.toAscii().constData());
00988 if (newAction) {
00989 d->actionsBeingDragged.append(newAction);
00990 break;
00991 }
00992 }
00993 }
00994
00995 if (d->actionsBeingDragged.count()) {
00996 QAction* overAction = actionAt(event->pos());
00997
00998 QFrame* dropIndicatorWidget = new QFrame(this);
00999 dropIndicatorWidget->resize(8, height() - 4);
01000 dropIndicatorWidget->setFrameShape(QFrame::VLine);
01001 dropIndicatorWidget->setLineWidth(3);
01002
01003 d->dropIndicatorAction = insertWidget(overAction, dropIndicatorWidget);
01004
01005 insertAction(overAction, d->dropIndicatorAction);
01006
01007 event->acceptProposedAction();
01008 return;
01009 }
01010 }
01011
01012 QToolBar::dragEnterEvent(event);
01013 }
01014
01015 void KToolBar::dragMoveEvent(QDragMoveEvent *event)
01016 {
01017 if (toolBarsEditable())
01018 forever {
01019 if (d->dropIndicatorAction) {
01020 QAction* overAction = 0L;
01021 foreach (QAction* action, actions()) {
01022
01023 QWidget* widget = widgetForAction(action);
01024 if (event->pos().x() < widget->pos().x() + (widget->width() / 2)) {
01025 overAction = action;
01026 break;
01027 }
01028 }
01029
01030 if (overAction != d->dropIndicatorAction) {
01031
01032 int dropIndicatorIndex = actions().indexOf(d->dropIndicatorAction);
01033 if (dropIndicatorIndex + 1 < actions().count()) {
01034 if (actions()[ dropIndicatorIndex + 1 ] == overAction)
01035 break;
01036 } else if (!overAction) {
01037 break;
01038 }
01039
01040 insertAction(overAction, d->dropIndicatorAction);
01041 }
01042
01043 event->accept();
01044 return;
01045 }
01046 break;
01047 }
01048
01049 QToolBar::dragMoveEvent(event);
01050 }
01051
01052 void KToolBar::dragLeaveEvent(QDragLeaveEvent *event)
01053 {
01054
01055 delete d->dropIndicatorAction;
01056 d->dropIndicatorAction = 0L;
01057 d->actionsBeingDragged.clear();
01058
01059 if (toolBarsEditable()) {
01060 event->accept();
01061 return;
01062 }
01063
01064 QToolBar::dragLeaveEvent(event);
01065 }
01066
01067 void KToolBar::dropEvent(QDropEvent *event)
01068 {
01069 if (toolBarsEditable()) {
01070 foreach (QAction* action, d->actionsBeingDragged) {
01071 if (actions().contains(action))
01072 removeAction(action);
01073 insertAction(d->dropIndicatorAction, action);
01074 }
01075 }
01076
01077
01078 delete d->dropIndicatorAction;
01079 d->dropIndicatorAction = 0L;
01080 d->actionsBeingDragged.clear();
01081
01082 if (toolBarsEditable()) {
01083 event->accept();
01084 return;
01085 }
01086
01087 QToolBar::dropEvent(event);
01088 }
01089
01090 void KToolBar::mousePressEvent(QMouseEvent *event)
01091 {
01092 if (toolBarsEditable() && event->button() == Qt::LeftButton) {
01093 if (KAction* action = qobject_cast<KAction*>(actionAt(event->pos()))) {
01094 d->dragAction = action;
01095 d->dragStartPosition = event->pos();
01096 event->accept();
01097 return;
01098 }
01099 }
01100
01101 QToolBar::mousePressEvent(event);
01102 }
01103
01104 void KToolBar::mouseMoveEvent(QMouseEvent *event)
01105 {
01106 if (!toolBarsEditable() || !d->dragAction)
01107 return QToolBar::mouseMoveEvent(event);
01108
01109 if ((event->pos() - d->dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
01110 event->accept();
01111 return;
01112 }
01113
01114 QDrag *drag = new QDrag(this);
01115 QMimeData *mimeData = new QMimeData;
01116
01117 QByteArray data;
01118 {
01119 QDataStream stream(&data, QIODevice::WriteOnly);
01120
01121 QStringList actionNames;
01122 actionNames << d->dragAction->objectName();
01123
01124 stream << actionNames;
01125 }
01126
01127 mimeData->setData("application/x-kde-action-list", data);
01128
01129 drag->setMimeData(mimeData);
01130
01131 Qt::DropAction dropAction = drag->start(Qt::MoveAction);
01132
01133 if (dropAction == Qt::MoveAction)
01134
01135
01136 if (drag->target() != this)
01137 removeAction(d->dragAction);
01138
01139 d->dragAction = 0L;
01140 event->accept();
01141 }
01142
01143 void KToolBar::mouseReleaseEvent(QMouseEvent *event)
01144 {
01145
01146 if (d->dragAction) {
01147 d->dragAction = 0L;
01148 event->accept();
01149 return;
01150 }
01151
01152 QToolBar::mouseReleaseEvent(event);
01153 }
01154
01155 bool KToolBar::eventFilter(QObject * watched, QEvent * event)
01156 {
01157
01158 if (event->type() == QEvent::MouseButtonPress) {
01159 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01160 if (me->buttons() & Qt::RightButton)
01161 if (QWidget* ww = qobject_cast<QWidget*>(watched))
01162 if (ww->parent() == this && !ww->isEnabled())
01163 QCoreApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, me->pos(), me->globalPos()));
01164
01165 } else if (event->type() == QEvent::ParentChange) {
01166
01167
01168 if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
01169 if (!this->isAncestorOf(ww)) {
01170
01171 ww->removeEventFilter(this);
01172 foreach (QWidget* child, ww->findChildren<QWidget*>())
01173 child->removeEventFilter(this);
01174 }
01175 }
01176 }
01177
01178 QToolButton* tb;
01179 if ((tb = qobject_cast<QToolButton*>(watched)) && !tb->actions().isEmpty()) {
01180
01181 QAction* act = tb->actions().first();
01182 if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
01183 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01184 if (me->button() == Qt::MidButton
01185 ) {
01186 if (me->type() == QEvent::MouseButtonPress)
01187 tb->setDown(true);
01188 else {
01189 tb->setDown(false);
01190 QMetaObject::invokeMethod(act, "triggered", Qt::DirectConnection,
01191 Q_ARG(Qt::MouseButtons, me->button()),
01192 Q_ARG(Qt::KeyboardModifiers, QApplication::keyboardModifiers()));
01193 }
01194 }
01195 }
01196
01197
01198
01199
01200
01201 if (event->type() == QEvent::Show || event->type() == QEvent::Paint || event->type() == QEvent::EnabledChange) {
01202 act = tb->defaultAction();
01203 if (act) {
01204 const QString text = KGlobal::locale()->removeAcceleratorMarker(act->iconText().isEmpty() ? act->text() : act->iconText());
01205 const QString toolTip = KGlobal::locale()->removeAcceleratorMarker(act->toolTip());
01206
01207 tb->setText(i18nc("@action:intoolbar Text label of toolbar button", "%1", text));
01208 tb->setToolTip(i18nc("@info:tooltip Tooltip of toolbar button", "%1", toolTip));
01209 }
01210 }
01211 }
01212
01213
01214 if (toolBarsEditable()) {
01215 if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
01216 switch (event->type()) {
01217 case QEvent::MouseButtonPress: {
01218 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01219 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01220 me->button(), me->buttons(), me->modifiers());
01221 mousePressEvent(&newEvent);
01222 return true;
01223 }
01224 case QEvent::MouseMove: {
01225 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01226 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01227 me->button(), me->buttons(), me->modifiers());
01228 mouseMoveEvent(&newEvent);
01229 return true;
01230 }
01231 case QEvent::MouseButtonRelease: {
01232 QMouseEvent* me = static_cast<QMouseEvent*>(event);
01233 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
01234 me->button(), me->buttons(), me->modifiers());
01235 mouseReleaseEvent(&newEvent);
01236 return true;
01237 }
01238 default:
01239 break;
01240 }
01241 }
01242 }
01243
01244 return QToolBar::eventFilter(watched, event);
01245 }
01246
01247 void KToolBar::actionEvent(QActionEvent * event)
01248 {
01249 if (event->type() == QEvent::ActionRemoved) {
01250 QWidget* widget = widgetForAction(event->action());
01251 if (widget) {
01252 widget->removeEventFilter(this);
01253
01254 foreach (QWidget* child, widget->findChildren<QWidget*>())
01255 child->removeEventFilter(this);
01256 }
01257 }
01258
01259 QToolBar::actionEvent(event);
01260
01261 if (event->type() == QEvent::ActionAdded) {
01262 QWidget* widget = widgetForAction(event->action());
01263 if (widget) {
01264 widget->installEventFilter(this);
01265
01266 foreach (QWidget* child, widget->findChildren<QWidget*>())
01267 child->installEventFilter(this);
01268 }
01269 }
01270
01271 d->adjustSeparatorVisibility();
01272 }
01273
01274 bool KToolBar::toolBarsEditable()
01275 {
01276 return KToolBar::Private::s_editable;
01277 }
01278
01279 void KToolBar::setToolBarsEditable(bool editable)
01280 {
01281 if (KToolBar::Private::s_editable != editable)
01282 KToolBar::Private::s_editable = editable;
01283 }
01284
01285 void KToolBar::setToolBarsLocked(bool locked)
01286 {
01287 if (KToolBar::Private::s_locked != locked) {
01288 KToolBar::Private::s_locked = locked;
01289
01290 foreach (KMainWindow* mw, KMainWindow::memberList())
01291 foreach (KToolBar* toolbar, mw->findChildren<KToolBar*>()) {
01292 toolbar->d->setLocked(locked);
01293 if (toolbar->d->contextLockAction)
01294 toolbar->d->contextLockAction->setText(locked ? i18n("Unlock Toolbars") : i18n("Lock Toolbars"));
01295 }
01296
01297 }
01298 }
01299
01300 bool KToolBar::toolBarsLocked()
01301 {
01302 return KToolBar::Private::s_locked;
01303 }
01304
01305 #include "ktoolbar.moc"