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