• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
ktoolbar.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright
3 (C) 2000 Reginald Stadlbauer (reggie@kde.org)
4 (C) 1997, 1998 Stephan Kulow (coolo@kde.org)
5 (C) 1997, 1998 Mark Donohoe (donohoe@kde.org)
6 (C) 1997, 1998 Sven Radej (radej@kde.org)
7 (C) 1997, 1998 Matthias Ettrich (ettrich@kde.org)
8 (C) 1999 Chris Schlaeger (cs@kde.org)
9 (C) 1999 Kurt Granroth (granroth@kde.org)
10 (C) 2005-2006 Hamish Rodda (rodda@kde.org)
11
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public
14 License version 2 as published by the Free Software Foundation.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Library General Public License for more details.
20
21 You should have received a copy of the GNU Library General Public License
22 along with this library; see the file COPYING.LIB. If not, write to
23 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA.
25*/
26
27#include "ktoolbar.h"
28
29#include <config.h>
30
31#include <QtCore/QPointer>
32#include <QtGui/QDesktopWidget>
33#include <QtGui/QFrame>
34#include <QtGui/QLayout>
35#include <QtGui/QMouseEvent>
36#include <QtGui/QToolButton>
37#include <QtXml/QDomElement>
38
39#include <kaction.h>
40#include <kactioncollection.h>
41#include <kapplication.h>
42#include <kauthorized.h>
43#include <kconfig.h>
44#include <kdebug.h>
45#include <kedittoolbar.h>
46#include <kglobalsettings.h>
47#include <kguiitem.h>
48#include <kicon.h>
49#include <kiconloader.h>
50#include <klocale.h>
51#include <kxmlguiwindow.h>
52#include <kmenu.h>
53#include <kstandardaction.h>
54#include <ktoggleaction.h>
55#include <kxmlguifactory.h>
56
57#include <kconfiggroup.h>
58
59/*
60 Toolbar settings (e.g. icon size or toolButtonStyle)
61 =====================================================
62
63 We have the following stack of settings (in order of priority) :
64 - user-specified settings (loaded/saved in KConfig)
65 - developer-specified settings in the XMLGUI file (if using xmlgui) (cannot change at runtime)
66 - KDE-global default (user-configurable; can change at runtime)
67 and when switching between kparts, they are saved as xml in memory,
68 which, in the unlikely case of no-kmainwindow-autosaving, could be
69 different from the user-specified settings saved in KConfig and would have
70 priority over it.
71
72 So, in summary, without XML:
73 Global config / User settings (loaded/saved in kconfig)
74 and with XML:
75 Global config / App-XML attributes / User settings (loaded/saved in kconfig)
76
77 And all those settings (except the KDE-global defaults) have to be stored in memory
78 since we cannot retrieve them at random points in time, not knowing the xml document
79 nor config file that holds these settings. Hence the iconSizeSettings and toolButtonStyleSettings arrays.
80
81 For instance, if you change the KDE-global default, whether this makes a change
82 on a given toolbar depends on whether there are settings at Level_AppXML or Level_UserSettings.
83 Only if there are no settings at those levels, should the change of KDEDefault make a difference.
84*/
85enum SettingLevel { Level_KDEDefault, Level_AppXML, Level_UserSettings,
86 NSettingLevels };
87enum { Unset = -1 };
88
89class KToolBar::Private
90{
91 public:
92 Private(KToolBar *qq)
93 : q(qq),
94 isMainToolBar(false),
95#ifndef KDE_NO_DEPRECATED
96 enableContext(true),
97#endif
98 unlockedMovable(true),
99 contextOrient(0),
100 contextMode(0),
101 contextSize(0),
102 contextButtonTitle(0),
103 contextShowText(0),
104 contextButtonAction(0),
105 contextTop(0),
106 contextLeft(0),
107 contextRight(0),
108 contextBottom(0),
109 contextIcons(0),
110 contextTextRight(0),
111 contextText(0),
112 contextTextUnder(0),
113 contextLockAction(0),
114 dropIndicatorAction(0),
115 context(0),
116 dragAction(0)
117 {
118 }
119
120 void slotAppearanceChanged();
121 void slotContextAboutToShow();
122 void slotContextAboutToHide();
123 void slotContextLeft();
124 void slotContextRight();
125 void slotContextShowText();
126 void slotContextTop();
127 void slotContextBottom();
128 void slotContextIcons();
129 void slotContextText();
130 void slotContextTextRight();
131 void slotContextTextUnder();
132 void slotContextIconSize();
133 void slotLockToolBars(bool lock);
134
135 void init(bool readConfig = true, bool isMainToolBar = false);
136 QString getPositionAsString() const;
137 KMenu *contextMenu(const QPoint &globalPos);
138 void setLocked(bool locked);
139 void adjustSeparatorVisibility();
140 void loadKDESettings();
141 void applyCurrentSettings();
142
143 QAction *findAction(const QString &actionName, KXMLGUIClient **client = 0) const;
144
145 static Qt::ToolButtonStyle toolButtonStyleFromString(const QString& style);
146 static QString toolButtonStyleToString(Qt::ToolButtonStyle);
147 static Qt::ToolBarArea positionFromString(const QString& position);
148
149 KToolBar *q;
150 bool isMainToolBar : 1;
151#ifndef KDE_NO_DEPRECATED
152 bool enableContext : 1;
153#endif
154 bool unlockedMovable : 1;
155 static bool s_editable;
156 static bool s_locked;
157
158 QSet<KXMLGUIClient *> xmlguiClients;
159
160 QMenu* contextOrient;
161 QMenu* contextMode;
162 QMenu* contextSize;
163
164 QAction* contextButtonTitle;
165 QAction* contextShowText;
166 QAction* contextButtonAction;
167 QAction* contextTop;
168 QAction* contextLeft;
169 QAction* contextRight;
170 QAction* contextBottom;
171 QAction* contextIcons;
172 QAction* contextTextRight;
173 QAction* contextText;
174 QAction* contextTextUnder;
175 KToggleAction* contextLockAction;
176 QMap<QAction*,int> contextIconSizes;
177
178 class IntSetting
179 {
180 public:
181 IntSetting() {
182 for (int level = 0; level < NSettingLevels; ++level) {
183 values[level] = Unset;
184 }
185 }
186 int currentValue() const {
187 int val = Unset;
188 for (int level = 0; level < NSettingLevels; ++level) {
189 if (values[level] != Unset)
190 val = values[level];
191 }
192 return val;
193 }
194 // Default value as far as the user is concerned is kde-global + app-xml.
195 // If currentValue()==defaultValue() then nothing to write into kconfig.
196 int defaultValue() const {
197 int val = Unset;
198 for (int level = 0; level < Level_UserSettings; ++level) {
199 if (values[level] != Unset)
200 val = values[level];
201 }
202 return val;
203 }
204 QString toString() const {
205 QString str;
206 for (int level = 0; level < NSettingLevels; ++level) {
207 str += QString::number(values[level]) + ' ';
208 }
209 return str;
210 }
211 int& operator[](int index) { return values[index]; }
212 private:
213 int values[NSettingLevels];
214 };
215 IntSetting iconSizeSettings;
216 IntSetting toolButtonStyleSettings; // either Qt::ToolButtonStyle or -1, hence "int".
217
218 QList<QAction*> actionsBeingDragged;
219 QAction* dropIndicatorAction;
220
221 KMenu* context;
222 KAction* dragAction;
223 QPoint dragStartPosition;
224};
225
226bool KToolBar::Private::s_editable = false;
227bool KToolBar::Private::s_locked = true;
228
229void KToolBar::Private::init(bool readConfig, bool _isMainToolBar)
230{
231 isMainToolBar = _isMainToolBar;
232 loadKDESettings();
233
234 // also read in our configurable settings (for non-xmlgui toolbars)
235 // KDE5: we can probably remove this, if people save settings then they load them too, e.g. using KMainWindow's autosave.
236 if (readConfig) {
237 KConfigGroup cg(KGlobal::config(), QString());
238 q->applySettings(cg);
239 }
240
241 if (q->mainWindow()) {
242 // Get notified when settings change
243 connect(q, SIGNAL(allowedAreasChanged(Qt::ToolBarAreas)),
244 q->mainWindow(), SLOT(setSettingsDirty()));
245 connect(q, SIGNAL(iconSizeChanged(QSize)),
246 q->mainWindow(), SLOT(setSettingsDirty()));
247 connect(q, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
248 q->mainWindow(), SLOT(setSettingsDirty()));
249 connect(q, SIGNAL(movableChanged(bool)),
250 q->mainWindow(), SLOT(setSettingsDirty()));
251 connect(q, SIGNAL(orientationChanged(Qt::Orientation)),
252 q->mainWindow(), SLOT(setSettingsDirty()));
253 }
254
255 if (!KAuthorized::authorize("movable_toolbars"))
256 q->setMovable(false);
257 else
258 q->setMovable(!KToolBar::toolBarsLocked());
259
260 connect(q, SIGNAL(movableChanged(bool)),
261 q, SLOT(slotMovableChanged(bool)));
262
263 q->setAcceptDrops(true);
264
265 connect(KGlobalSettings::self(), SIGNAL(toolbarAppearanceChanged(int)),
266 q, SLOT(slotAppearanceChanged()));
267 connect(KIconLoader::global(), SIGNAL(iconLoaderSettingsChanged()),
268 q, SLOT(slotAppearanceChanged()));
269}
270
271QString KToolBar::Private::getPositionAsString() const
272{
273 // get all of the stuff to save
274 switch (q->mainWindow()->toolBarArea(const_cast<KToolBar*>(q))) {
275 case Qt::BottomToolBarArea:
276 return "Bottom";
277 case Qt::LeftToolBarArea:
278 return "Left";
279 case Qt::RightToolBarArea:
280 return "Right";
281 case Qt::TopToolBarArea:
282 default:
283 return "Top";
284 }
285}
286
287KMenu *KToolBar::Private::contextMenu(const QPoint &globalPos)
288{
289 if (!context) {
290 context = new KMenu(q);
291
292 contextButtonTitle = context->addTitle(i18nc("@title:menu", "Show Text"));
293 contextShowText = context->addAction(QString(), q, SLOT(slotContextShowText()));
294
295 context->addTitle(i18nc("@title:menu", "Toolbar Settings"));
296
297 contextOrient = new KMenu(i18nc("Toolbar orientation", "Orientation"), context);
298
299 contextTop = contextOrient->addAction(i18nc("toolbar position string", "Top"), q, SLOT(slotContextTop()));
300 contextTop->setChecked(true);
301 contextLeft = contextOrient->addAction(i18nc("toolbar position string", "Left"), q, SLOT(slotContextLeft()));
302 contextRight = contextOrient->addAction(i18nc("toolbar position string", "Right"), q, SLOT(slotContextRight()));
303 contextBottom = contextOrient->addAction(i18nc("toolbar position string", "Bottom"), q, SLOT(slotContextBottom()));
304
305 QActionGroup* positionGroup = new QActionGroup(contextOrient);
306 foreach (QAction* action, contextOrient->actions()) {
307 action->setActionGroup(positionGroup);
308 action->setCheckable(true);
309 }
310
311 contextMode = new KMenu(i18n("Text Position"), context);
312
313 contextIcons = contextMode->addAction(i18n("Icons Only"), q, SLOT(slotContextIcons()));
314 contextText = contextMode->addAction(i18n("Text Only"), q, SLOT(slotContextText()));
315 contextTextRight = contextMode->addAction(i18n("Text Alongside Icons"), q, SLOT(slotContextTextRight()));
316 contextTextUnder = contextMode->addAction(i18n("Text Under Icons"), q, SLOT(slotContextTextUnder()));
317
318 QActionGroup* textGroup = new QActionGroup(contextMode);
319 foreach (QAction* action, contextMode->actions()) {
320 action->setActionGroup(textGroup);
321 action->setCheckable(true);
322 }
323
324 contextSize = new KMenu(i18n("Icon Size"), context);
325
326 contextIconSizes.insert(contextSize->addAction(i18nc("@item:inmenu Icon size", "Default"), q, SLOT(slotContextIconSize())),
327 iconSizeSettings.defaultValue());
328
329 // Query the current theme for available sizes
330 KIconTheme *theme = KIconLoader::global()->theme();
331 QList<int> avSizes;
332 if (theme) {
333 avSizes = theme->querySizes(isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
334 }
335
336 qSort(avSizes);
337
338 if (avSizes.count() < 10) {
339 // Fixed or threshold type icons
340 foreach (int it, avSizes) {
341 QString text;
342 if (it < 19)
343 text = i18n("Small (%1x%2)", it, it);
344 else if (it < 25)
345 text = i18n("Medium (%1x%2)", it, it);
346 else if (it < 35)
347 text = i18n("Large (%1x%2)", it, it);
348 else
349 text = i18n("Huge (%1x%2)", it, it);
350
351 // save the size in the contextIconSizes map
352 contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
353 }
354 } else {
355 // Scalable icons.
356 const int progression[] = { 16, 22, 32, 48, 64, 96, 128, 192, 256 };
357
358 for (uint i = 0; i < 9; i++) {
359 foreach (int it, avSizes) {
360 if (it >= progression[ i ]) {
361 QString text;
362 if (it < 19)
363 text = i18n("Small (%1x%2)", it, it);
364 else if (it < 25)
365 text = i18n("Medium (%1x%2)", it, it);
366 else if (it < 35)
367 text = i18n("Large (%1x%2)", it, it);
368 else
369 text = i18n("Huge (%1x%2)", it, it);
370
371 // save the size in the contextIconSizes map
372 contextIconSizes.insert(contextSize->addAction(text, q, SLOT(slotContextIconSize())), it);
373 break;
374 }
375 }
376 }
377 }
378
379 QActionGroup* sizeGroup = new QActionGroup(contextSize);
380 foreach (QAction* action, contextSize->actions()) {
381 action->setActionGroup(sizeGroup);
382 action->setCheckable(true);
383 }
384
385 if (!q->toolBarsLocked() && !q->isMovable())
386 unlockedMovable = false;
387
388 delete contextLockAction;
389 contextLockAction = new KToggleAction(KIcon("system-lock-screen"), i18n("Lock Toolbar Positions"), q);
390 contextLockAction->setChecked(q->toolBarsLocked());
391 connect(contextLockAction, SIGNAL(toggled(bool)), q, SLOT(slotLockToolBars(bool)));
392
393 // Now add the actions to the menu
394 context->addMenu(contextMode);
395 context->addMenu(contextSize);
396 context->addMenu(contextOrient);
397 context->addSeparator();
398
399 connect(context, SIGNAL(aboutToShow()), q, SLOT(slotContextAboutToShow()));
400 }
401
402 contextButtonAction = q->actionAt(q->mapFromGlobal(globalPos));
403 if (contextButtonAction) {
404 contextShowText->setText(contextButtonAction->text());
405 contextShowText->setIcon(contextButtonAction->icon());
406 contextShowText->setCheckable(true);
407 }
408
409 contextOrient->menuAction()->setVisible(!q->toolBarsLocked());
410 // Unplugging a submenu from abouttohide leads to the popupmenu floating around
411 // So better simply call that code from after exec() returns (DF)
412 //connect(context, SIGNAL(aboutToHide()), this, SLOT(slotContextAboutToHide()));
413
414 return context;
415}
416
417void KToolBar::Private::setLocked(bool locked)
418{
419 if (unlockedMovable)
420 q->setMovable(!locked);
421}
422
423void KToolBar::Private::adjustSeparatorVisibility()
424{
425 bool visibleNonSeparator = false;
426 int separatorToShow = -1;
427
428 for (int index = 0; index < q->actions().count(); ++index) {
429 QAction* action = q->actions()[ index ];
430 if (action->isSeparator()) {
431 if (visibleNonSeparator) {
432 separatorToShow = index;
433 visibleNonSeparator = false;
434 } else {
435 action->setVisible(false);
436 }
437 } else if (!visibleNonSeparator) {
438 if (action->isVisible()) {
439 visibleNonSeparator = true;
440 if (separatorToShow != -1) {
441 q->actions()[ separatorToShow ]->setVisible(true);
442 separatorToShow = -1;
443 }
444 }
445 }
446 }
447
448 if (separatorToShow != -1)
449 q->actions()[ separatorToShow ]->setVisible(false);
450}
451
452Qt::ToolButtonStyle KToolBar::Private::toolButtonStyleFromString(const QString & _style)
453{
454 QString style = _style.toLower();
455 if (style == "textbesideicon" || style == "icontextright")
456 return Qt::ToolButtonTextBesideIcon;
457 else if (style == "textundericon" || style == "icontextbottom")
458 return Qt::ToolButtonTextUnderIcon;
459 else if (style == "textonly")
460 return Qt::ToolButtonTextOnly;
461 else
462 return Qt::ToolButtonIconOnly;
463}
464
465QString KToolBar::Private::toolButtonStyleToString(Qt::ToolButtonStyle style)
466{
467 switch(style)
468 {
469 case Qt::ToolButtonIconOnly:
470 default:
471 return "IconOnly";
472 case Qt::ToolButtonTextBesideIcon:
473 return "TextBesideIcon";
474 case Qt::ToolButtonTextOnly:
475 return "TextOnly";
476 case Qt::ToolButtonTextUnderIcon:
477 return "TextUnderIcon";
478 }
479}
480
481Qt::ToolBarArea KToolBar::Private::positionFromString(const QString& position)
482{
483 Qt::ToolBarArea newposition = Qt::TopToolBarArea;
484 if (position == QLatin1String("left")) {
485 newposition = Qt::LeftToolBarArea;
486 } else if (position == QLatin1String("bottom")) {
487 newposition = Qt::BottomToolBarArea;
488 } else if (position == QLatin1String("right")) {
489 newposition = Qt::RightToolBarArea;
490 }
491 return newposition;
492}
493
494// Global setting was changed
495void KToolBar::Private::slotAppearanceChanged()
496{
497 loadKDESettings();
498 applyCurrentSettings();
499}
500
501void KToolBar::Private::loadKDESettings()
502{
503 iconSizeSettings[Level_KDEDefault] = q->iconSizeDefault();
504
505 if (isMainToolBar) {
506 toolButtonStyleSettings[Level_KDEDefault] = q->toolButtonStyleSetting();
507 } else {
508 const QString fallBack = toolButtonStyleToString(Qt::ToolButtonTextBesideIcon);
524 KConfigGroup group(KGlobal::config(), "Toolbar style");
525 const QString value = group.readEntry("ToolButtonStyleOtherToolbars", fallBack);
526 toolButtonStyleSettings[Level_KDEDefault] = KToolBar::Private::toolButtonStyleFromString(value);
527 }
528}
529
530// Call this after changing something in d->iconSizeSettings or d->toolButtonStyleSettings
531void KToolBar::Private::applyCurrentSettings()
532{
533 //kDebug() << q->objectName() << "iconSizeSettings:" << iconSizeSettings.toString() << "->" << iconSizeSettings.currentValue();
534 const int currentIconSize = iconSizeSettings.currentValue();
535 q->setIconSize(QSize(currentIconSize, currentIconSize));
536 //kDebug() << q->objectName() << "toolButtonStyleSettings:" << toolButtonStyleSettings.toString() << "->" << toolButtonStyleSettings.currentValue();
537 q->setToolButtonStyle(static_cast<Qt::ToolButtonStyle>(toolButtonStyleSettings.currentValue()));
538
539 // And remember to save the new look later
540 KMainWindow *kmw = q->mainWindow();
541 if (kmw)
542 kmw->setSettingsDirty();
543}
544
545QAction *KToolBar::Private::findAction(const QString &actionName, KXMLGUIClient **clientOut) const
546{
547 foreach (KXMLGUIClient* client, xmlguiClients) {
548 QAction* action = client->actionCollection()->action(actionName);
549 if (action) {
550 if (clientOut) {
551 *clientOut = client;
552 }
553 return action;
554 }
555 }
556 return 0;
557}
558
559void KToolBar::Private::slotContextAboutToShow()
560{
569 KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
570
571 // try to find "configure toolbars" action
572 QAction *configureAction = 0;
573 const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
574 configureAction = findAction(actionName);
575
576 if (!configureAction && kmw) {
577 configureAction = kmw->actionCollection()->action(actionName);
578 }
579
580 if (configureAction) {
581 context->addAction(configureAction);
582 }
583
584 context->addAction(contextLockAction);
585
586 if (kmw) {
587 kmw->setupToolbarMenuActions();
588 // Only allow hiding a toolbar if the action is also plugged somewhere else (e.g. menubar)
589 QAction *tbAction = kmw->toolBarMenuAction();
590 if (!q->toolBarsLocked() && tbAction && tbAction->associatedWidgets().count() > 0)
591 context->addAction(tbAction);
592 }
593
594 KEditToolBar::setGlobalDefaultToolBar(q->QObject::objectName().toLatin1().constData());
595
596 // Check the actions that should be checked
597 switch (q->toolButtonStyle()) {
598 case Qt::ToolButtonIconOnly:
599 default:
600 contextIcons->setChecked(true);
601 break;
602 case Qt::ToolButtonTextBesideIcon:
603 contextTextRight->setChecked(true);
604 break;
605 case Qt::ToolButtonTextOnly:
606 contextText->setChecked(true);
607 break;
608 case Qt::ToolButtonTextUnderIcon:
609 contextTextUnder->setChecked(true);
610 break;
611 }
612
613 QMapIterator< QAction*, int > it = contextIconSizes;
614 while (it.hasNext()) {
615 it.next();
616 if (it.value() == q->iconSize().width()) {
617 it.key()->setChecked(true);
618 break;
619 }
620 }
621
622 switch (q->mainWindow()->toolBarArea(q)) {
623 case Qt::BottomToolBarArea:
624 contextBottom->setChecked(true);
625 break;
626 case Qt::LeftToolBarArea:
627 contextLeft->setChecked(true);
628 break;
629 case Qt::RightToolBarArea:
630 contextRight->setChecked(true);
631 break;
632 default:
633 case Qt::TopToolBarArea:
634 contextTop->setChecked(true);
635 break;
636 }
637
638 const bool showButtonSettings = contextButtonAction
639 && !contextShowText->text().isEmpty()
640 && contextTextRight->isChecked();
641 contextButtonTitle->setVisible(showButtonSettings);
642 contextShowText->setVisible(showButtonSettings);
643 if (showButtonSettings) {
644 contextShowText->setChecked(contextButtonAction->priority() >= QAction::NormalPriority);
645 }
646}
647
648void KToolBar::Private::slotContextAboutToHide()
649{
650 // We have to unplug whatever slotContextAboutToShow plugged into the menu.
651 // Unplug the toolbar menu action
652 KXmlGuiWindow *kmw = qobject_cast<KXmlGuiWindow *>(q->mainWindow());
653 if (kmw && kmw->toolBarMenuAction()) {
654 if (kmw->toolBarMenuAction()->associatedWidgets().count() > 1) {
655 context->removeAction(kmw->toolBarMenuAction());
656 }
657 }
658
659 // Unplug the configure toolbars action too, since it's afterwards anyway
660 QAction *configureAction = 0;
661 const char* actionName = KStandardAction::name(KStandardAction::ConfigureToolbars);
662 configureAction = findAction(actionName);
663
664 if (!configureAction && kmw) {
665 configureAction = kmw->actionCollection()->action(actionName);
666 }
667
668 if (configureAction) {
669 context->removeAction(configureAction);
670 }
671
672 context->removeAction(contextLockAction);
673}
674
675void KToolBar::Private::slotContextLeft()
676{
677 q->mainWindow()->addToolBar(Qt::LeftToolBarArea, q);
678}
679
680void KToolBar::Private::slotContextRight()
681{
682 q->mainWindow()->addToolBar(Qt::RightToolBarArea, q);
683}
684
685void KToolBar::Private::slotContextShowText()
686{
687 Q_ASSERT(contextButtonAction);
688 const QAction::Priority priority = contextShowText->isChecked()
689 ? QAction::NormalPriority : QAction::LowPriority;
690 contextButtonAction->setPriority(priority);
691
692 // Find to which xml file and componentData the action belongs to
693 KComponentData componentData;
694 QString filename;
695 KXMLGUIClient *client;
696 if (findAction(contextButtonAction->objectName(), &client)) {
697 componentData = client->componentData();
698 filename = client->xmlFile();
699 }
700 if (filename.isEmpty()) {
701 componentData = KGlobal::mainComponent();
702 filename = componentData.componentName() + "ui.rc";
703 }
704
705 // Save the priority state of the action
706 const QString configFile = KXMLGUIFactory::readConfigFile(filename, componentData);
707
708 QDomDocument document;
709 document.setContent(configFile);
710 QDomElement elem = KXMLGUIFactory::actionPropertiesElement(document);
711 QDomElement actionElem = KXMLGUIFactory::findActionByName(elem, contextButtonAction->objectName(), true);
712 actionElem.setAttribute("priority", priority);
713 KXMLGUIFactory::saveConfigFile(document, filename, componentData);
714}
715
716void KToolBar::Private::slotContextTop()
717{
718 q->mainWindow()->addToolBar(Qt::TopToolBarArea, q);
719}
720
721void KToolBar::Private::slotContextBottom()
722{
723 q->mainWindow()->addToolBar(Qt::BottomToolBarArea, q);
724}
725
726void KToolBar::Private::slotContextIcons()
727{
728 q->setToolButtonStyle(Qt::ToolButtonIconOnly);
729 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
730}
731
732void KToolBar::Private::slotContextText()
733{
734 q->setToolButtonStyle(Qt::ToolButtonTextOnly);
735 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
736}
737
738void KToolBar::Private::slotContextTextUnder()
739{
740 q->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
741 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
742}
743
744void KToolBar::Private::slotContextTextRight()
745{
746 q->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
747 toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
748}
749
750void KToolBar::Private::slotContextIconSize()
751{
752 QAction* action = qobject_cast<QAction*>(q->sender());
753 if (action && contextIconSizes.contains(action)) {
754 const int iconSize = contextIconSizes.value(action);
755 q->setIconDimensions(iconSize);
756 }
757}
758
759void KToolBar::Private::slotLockToolBars(bool lock)
760{
761 q->setToolBarsLocked(lock);
762}
763
764
765
766KToolBar::KToolBar(QWidget *parent, bool isMainToolBar, bool readConfig)
767 : QToolBar(parent),
768 d(new Private(this))
769{
770 d->init(readConfig, isMainToolBar);
771
772 // KToolBar is auto-added to the top area of the main window if parent is a QMainWindow
773 if (QMainWindow* mw = qobject_cast<QMainWindow*>(parent))
774 mw->addToolBar(this);
775}
776
777KToolBar::KToolBar(const QString& objectName, QWidget *parent, bool readConfig)
778 : QToolBar(parent),
779 d(new Private(this))
780{
781 setObjectName(objectName);
782 // mainToolBar -> isMainToolBar = true -> buttonStyle is configurable
783 // others -> isMainToolBar = false -> ### hardcoded default for buttonStyle !!! should be configurable? -> hidden key added
784 d->init(readConfig, objectName == "mainToolBar");
785
786 // KToolBar is auto-added to the top area of the main window if parent is a QMainWindow
787 if (QMainWindow* mw = qobject_cast<QMainWindow*>(parent))
788 mw->addToolBar(this);
789}
790
791KToolBar::KToolBar(const QString& objectName, QMainWindow* parent, Qt::ToolBarArea area,
792 bool newLine, bool isMainToolBar, bool readConfig)
793 : QToolBar(parent),
794 d(new Private(this))
795{
796 setObjectName(objectName);
797 d->init(readConfig, isMainToolBar);
798
799 if (newLine)
800 mainWindow()->addToolBarBreak(area);
801
802 mainWindow()->addToolBar(area, this);
803
804 if (newLine)
805 mainWindow()->addToolBarBreak(area);
806}
807
808KToolBar::~KToolBar()
809{
810 delete d->contextLockAction;
811 delete d;
812}
813
814#ifndef KDE_NO_DEPRECATED
815void KToolBar::setContextMenuEnabled(bool enable)
816{
817 d->enableContext = enable;
818}
819#endif
820
821#ifndef KDE_NO_DEPRECATED
822bool KToolBar::contextMenuEnabled() const
823{
824 return d->enableContext;
825}
826#endif
827
828void KToolBar::saveSettings(KConfigGroup &cg)
829{
830 Q_ASSERT(!cg.name().isEmpty());
831
832 if (cg.hasKey("Hidden")) {
833 cg.deleteEntry("Hidden"); // remove old key to avoid bugs from the compat code in applySettings. KDE5: remove.
834 }
835
836 const int currentIconSize = iconSize().width();
837 //kDebug() << objectName() << currentIconSize << d->iconSizeSettings.toString() << "defaultValue=" << d->iconSizeSettings.defaultValue();
838 if (!cg.hasDefault("IconSize") && currentIconSize == d->iconSizeSettings.defaultValue()) {
839 cg.revertToDefault("IconSize");
840 d->iconSizeSettings[Level_UserSettings] = Unset;
841 } else {
842 cg.writeEntry("IconSize", currentIconSize);
843 d->iconSizeSettings[Level_UserSettings] = currentIconSize;
844 }
845
846 const Qt::ToolButtonStyle currentToolButtonStyle = toolButtonStyle();
847 if (!cg.hasDefault("ToolButtonStyle") && currentToolButtonStyle == d->toolButtonStyleSettings.defaultValue()) {
848 cg.revertToDefault("ToolButtonStyle");
849 d->toolButtonStyleSettings[Level_UserSettings] = Unset;
850 } else {
851 cg.writeEntry("ToolButtonStyle", d->toolButtonStyleToString(currentToolButtonStyle));
852 d->toolButtonStyleSettings[Level_UserSettings] = currentToolButtonStyle;
853 }
854}
855
856#ifndef KDE_NO_DEPRECATED
857void KToolBar::setXMLGUIClient(KXMLGUIClient *client)
858{
859 d->xmlguiClients.clear();
860 d->xmlguiClients << client;
861}
862#endif
863
864void KToolBar::addXMLGUIClient( KXMLGUIClient *client )
865{
866 d->xmlguiClients << client;
867}
868
869void KToolBar::removeXMLGUIClient( KXMLGUIClient *client )
870{
871 d->xmlguiClients.remove(client);
872}
873
874void KToolBar::contextMenuEvent(QContextMenuEvent* event)
875{
876#ifndef KDE_NO_DEPRECATED
877 if (mainWindow() && d->enableContext) {
878 QPointer<KToolBar> guard(this);
879 const QPoint globalPos = event->globalPos();
880 d->contextMenu(globalPos)->exec(globalPos);
881
882 // "Configure Toolbars" recreates toolbars, so we might not exist anymore.
883 if (guard) {
884 d->slotContextAboutToHide();
885 }
886 return;
887 }
888#endif
889
890 QToolBar::contextMenuEvent(event);
891}
892
893Qt::ToolButtonStyle KToolBar::toolButtonStyleSetting()
894{
895 KConfigGroup group(KGlobal::config(), "Toolbar style");
896 const QString fallback = Private::toolButtonStyleToString(Qt::ToolButtonTextBesideIcon);
897 return KToolBar::Private::toolButtonStyleFromString(group.readEntry("ToolButtonStyle", fallback));
898}
899
900void KToolBar::loadState(const QDomElement &element)
901{
902 QMainWindow *mw = mainWindow();
903 if (!mw)
904 return;
905
906 {
907 QDomNode textNode = element.namedItem("text");
908 QByteArray text;
909 QByteArray context;
910 if (textNode.isElement())
911 {
912 QDomElement textElement = textNode.toElement();
913 text = textElement.text().toUtf8();
914 context = textElement.attribute("context").toUtf8();
915 }
916 else
917 {
918 textNode = element.namedItem("Text");
919 if (textNode.isElement())
920 {
921 QDomElement textElement = textNode.toElement();
922 text = textElement.text().toUtf8();
923 context = textElement.attribute("context").toUtf8();
924 }
925 }
926
927 QString i18nText;
928 if (!text.isEmpty() && !context.isEmpty())
929 i18nText = i18nc(context, text);
930 else if (!text.isEmpty())
931 i18nText = i18n(text);
932
933 if (!i18nText.isEmpty())
934 setWindowTitle(i18nText);
935 }
936
937 /*
938 This method is called in order to load toolbar settings from XML.
939 However this can be used in two rather different cases:
940 - for the initial loading of the app's XML. In that case the settings
941 are only the defaults (Level_AppXML), the user's KConfig settings will override them
942
943 - for later re-loading when switching between parts in KXMLGUIFactory.
944 In that case the XML contains the final settings, not the defaults.
945 We do need the defaults, and the toolbar might have been completely
946 deleted and recreated meanwhile. So we store the app-default settings
947 into the XML.
948 */
949 bool loadingAppDefaults = true;
950 if (element.hasAttribute("tempXml")) {
951 // this isn't the first time, so the app-xml defaults have been saved into the (in-memory) XML
952 loadingAppDefaults = false;
953 const QString iconSizeDefault = element.attribute("iconSizeDefault");
954 if (!iconSizeDefault.isEmpty()) {
955 d->iconSizeSettings[Level_AppXML] = iconSizeDefault.toInt();
956 }
957 const QString toolButtonStyleDefault = element.attribute("toolButtonStyleDefault");
958 if (!toolButtonStyleDefault.isEmpty()) {
959 d->toolButtonStyleSettings[Level_AppXML] = d->toolButtonStyleFromString(toolButtonStyleDefault);
960 }
961 } else {
962 // loading app defaults
963 bool newLine = false;
964 QString attrNewLine = element.attribute("newline").toLower();
965 if (!attrNewLine.isEmpty())
966 newLine = attrNewLine == "true";
967 if (newLine && mw)
968 mw->insertToolBarBreak(this);
969 }
970
971 int newIconSize = -1;
972 if (element.hasAttribute("iconSize")) {
973 bool ok;
974 newIconSize = element.attribute("iconSize").trimmed().toInt(&ok);
975 if (!ok)
976 newIconSize = -1;
977 }
978 if (newIconSize != -1)
979 d->iconSizeSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = newIconSize;
980
981 const QString newToolButtonStyle = element.attribute("iconText");
982 if (!newToolButtonStyle.isEmpty())
983 d->toolButtonStyleSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = d->toolButtonStyleFromString(newToolButtonStyle);
984
985 bool hidden = false;
986 {
987 QString attrHidden = element.attribute("hidden").toLower();
988 if (!attrHidden.isEmpty())
989 hidden = attrHidden == "true";
990 }
991
992 Qt::ToolBarArea pos = Qt::NoToolBarArea;
993 {
994 QString attrPosition = element.attribute("position").toLower();
995 if (!attrPosition.isEmpty())
996 pos = KToolBar::Private::positionFromString(attrPosition);
997 }
998 if (pos != Qt::NoToolBarArea)
999 mw->addToolBar(pos, this);
1000
1001 setVisible(!hidden);
1002
1003 d->applyCurrentSettings();
1004}
1005
1006// Called when switching between xmlgui clients, in order to find any unsaved settings
1007// again when switching back to the current xmlgui client.
1008void KToolBar::saveState(QDomElement &current) const
1009{
1010 Q_ASSERT(!current.isNull());
1011
1012 current.setAttribute("tempXml", "true");
1013
1014 current.setAttribute("noMerge", "1");
1015 current.setAttribute("position", d->getPositionAsString().toLower());
1016 current.setAttribute("hidden", isHidden() ? "true" : "false");
1017
1018 const int currentIconSize = iconSize().width();
1019 if (currentIconSize == d->iconSizeSettings.defaultValue())
1020 current.removeAttribute("iconSize");
1021 else
1022 current.setAttribute("iconSize", iconSize().width());
1023
1024 if (toolButtonStyle() == d->toolButtonStyleSettings.defaultValue())
1025 current.removeAttribute("iconText");
1026 else
1027 current.setAttribute("iconText", d->toolButtonStyleToString(toolButtonStyle()));
1028
1029 // Note: if this method is used by more than KXMLGUIBuilder, e.g. to save XML settings to *disk*,
1030 // then the stuff below shouldn't always be done. This is not the case currently though.
1031 if (d->iconSizeSettings[Level_AppXML] != Unset) {
1032 current.setAttribute("iconSizeDefault", d->iconSizeSettings[Level_AppXML]);
1033 }
1034 if (d->toolButtonStyleSettings[Level_AppXML] != Unset) {
1035 const Qt::ToolButtonStyle bs = static_cast<Qt::ToolButtonStyle>(d->toolButtonStyleSettings[Level_AppXML]);
1036 current.setAttribute("toolButtonStyleDefault", d->toolButtonStyleToString(bs));
1037 }
1038}
1039
1040// called by KMainWindow::applyMainWindowSettings to read from the user settings
1041void KToolBar::applySettings(const KConfigGroup &cg, bool forceGlobal)
1042{
1043 Q_ASSERT(!cg.name().isEmpty());
1044 Q_UNUSED(forceGlobal); // KDE5: remove
1045
1046 // a small leftover from kde3: separate bool for hidden/shown. But it's also part of saveMainWindowSettings,
1047 // it is not really useful anymore, except in the unlikely case where someone would call this by hand.
1048 // KDE5: remove the block below
1049 if (cg.hasKey("Hidden")) {
1050 const bool hidden = cg.readEntry("Hidden", false);
1051 if (hidden)
1052 hide();
1053 else {
1054 show();
1055 }
1056 }
1057
1058 if (cg.hasKey("IconSize")) {
1059 d->iconSizeSettings[Level_UserSettings] = cg.readEntry("IconSize", 0);
1060 }
1061 if (cg.hasKey("ToolButtonStyle")) {
1062 d->toolButtonStyleSettings[Level_UserSettings] = d->toolButtonStyleFromString(cg.readEntry("ToolButtonStyle", QString()));
1063 }
1064
1065 d->applyCurrentSettings();
1066}
1067
1068KMainWindow * KToolBar::mainWindow() const
1069{
1070 return qobject_cast<KMainWindow*>(const_cast<QObject*>(parent()));
1071}
1072
1073void KToolBar::setIconDimensions(int size)
1074{
1075 QToolBar::setIconSize(QSize(size, size));
1076 d->iconSizeSettings[Level_UserSettings] = size;
1077}
1078
1079int KToolBar::iconSizeDefault() const
1080{
1081 return KIconLoader::global()->currentSize(d->isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
1082}
1083
1084void KToolBar::slotMovableChanged(bool movable)
1085{
1086 if (movable && !KAuthorized::authorize("movable_toolbars"))
1087 setMovable(false);
1088}
1089
1090void KToolBar::dragEnterEvent(QDragEnterEvent *event)
1091{
1092 if (toolBarsEditable() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction) &&
1093 event->mimeData()->hasFormat("application/x-kde-action-list")) {
1094 QByteArray data = event->mimeData()->data("application/x-kde-action-list");
1095
1096 QDataStream stream(data);
1097
1098 QStringList actionNames;
1099
1100 stream >> actionNames;
1101
1102 foreach (const QString& actionName, actionNames) {
1103 foreach (KActionCollection* ac, KActionCollection::allCollections()) {
1104 QAction* newAction = ac->action(actionName.toLatin1().constData());
1105 if (newAction) {
1106 d->actionsBeingDragged.append(newAction);
1107 break;
1108 }
1109 }
1110 }
1111
1112 if (d->actionsBeingDragged.count()) {
1113 QAction* overAction = actionAt(event->pos());
1114
1115 QFrame* dropIndicatorWidget = new QFrame(this);
1116 dropIndicatorWidget->resize(8, height() - 4);
1117 dropIndicatorWidget->setFrameShape(QFrame::VLine);
1118 dropIndicatorWidget->setLineWidth(3);
1119
1120 d->dropIndicatorAction = insertWidget(overAction, dropIndicatorWidget);
1121
1122 insertAction(overAction, d->dropIndicatorAction);
1123
1124 event->acceptProposedAction();
1125 return;
1126 }
1127 }
1128
1129 QToolBar::dragEnterEvent(event);
1130}
1131
1132void KToolBar::dragMoveEvent(QDragMoveEvent *event)
1133{
1134 if (toolBarsEditable())
1135 forever {
1136 if (d->dropIndicatorAction) {
1137 QAction* overAction = 0L;
1138 foreach (QAction* action, actions()) {
1139 // want to make it feel that half way across an action you're dropping on the other side of it
1140 QWidget* widget = widgetForAction(action);
1141 if (event->pos().x() < widget->pos().x() + (widget->width() / 2)) {
1142 overAction = action;
1143 break;
1144 }
1145 }
1146
1147 if (overAction != d->dropIndicatorAction) {
1148 // Check to see if the indicator is already in the right spot
1149 int dropIndicatorIndex = actions().indexOf(d->dropIndicatorAction);
1150 if (dropIndicatorIndex + 1 < actions().count()) {
1151 if (actions()[ dropIndicatorIndex + 1 ] == overAction)
1152 break;
1153 } else if (!overAction) {
1154 break;
1155 }
1156
1157 insertAction(overAction, d->dropIndicatorAction);
1158 }
1159
1160 event->accept();
1161 return;
1162 }
1163 break;
1164 }
1165
1166 QToolBar::dragMoveEvent(event);
1167}
1168
1169void KToolBar::dragLeaveEvent(QDragLeaveEvent *event)
1170{
1171 // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1172 delete d->dropIndicatorAction;
1173 d->dropIndicatorAction = 0L;
1174 d->actionsBeingDragged.clear();
1175
1176 if (toolBarsEditable()) {
1177 event->accept();
1178 return;
1179 }
1180
1181 QToolBar::dragLeaveEvent(event);
1182}
1183
1184void KToolBar::dropEvent(QDropEvent *event)
1185{
1186 if (toolBarsEditable()) {
1187 foreach (QAction* action, d->actionsBeingDragged) {
1188 if (actions().contains(action))
1189 removeAction(action);
1190 insertAction(d->dropIndicatorAction, action);
1191 }
1192 }
1193
1194 // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1195 delete d->dropIndicatorAction;
1196 d->dropIndicatorAction = 0L;
1197 d->actionsBeingDragged.clear();
1198
1199 if (toolBarsEditable()) {
1200 event->accept();
1201 return;
1202 }
1203
1204 QToolBar::dropEvent(event);
1205}
1206
1207void KToolBar::mousePressEvent(QMouseEvent *event)
1208{
1209 if (toolBarsEditable() && event->button() == Qt::LeftButton) {
1210 if (KAction* action = qobject_cast<KAction*>(actionAt(event->pos()))) {
1211 d->dragAction = action;
1212 d->dragStartPosition = event->pos();
1213 event->accept();
1214 return;
1215 }
1216 }
1217
1218 QToolBar::mousePressEvent(event);
1219}
1220
1221void KToolBar::mouseMoveEvent(QMouseEvent *event)
1222{
1223 if (!toolBarsEditable() || !d->dragAction)
1224 return QToolBar::mouseMoveEvent(event);
1225
1226 if ((event->pos() - d->dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
1227 event->accept();
1228 return;
1229 }
1230
1231 QDrag *drag = new QDrag(this);
1232 QMimeData *mimeData = new QMimeData;
1233
1234 QByteArray data;
1235 {
1236 QDataStream stream(&data, QIODevice::WriteOnly);
1237
1238 QStringList actionNames;
1239 actionNames << d->dragAction->objectName();
1240
1241 stream << actionNames;
1242 }
1243
1244 mimeData->setData("application/x-kde-action-list", data);
1245
1246 drag->setMimeData(mimeData);
1247
1248 Qt::DropAction dropAction = drag->start(Qt::MoveAction);
1249
1250 if (dropAction == Qt::MoveAction)
1251 // Only remove from this toolbar if it was moved to another toolbar
1252 // Otherwise the receiver moves it.
1253 if (drag->target() != this)
1254 removeAction(d->dragAction);
1255
1256 d->dragAction = 0L;
1257 event->accept();
1258}
1259
1260void KToolBar::mouseReleaseEvent(QMouseEvent *event)
1261{
1262 // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1263 if (d->dragAction) {
1264 d->dragAction = 0L;
1265 event->accept();
1266 return;
1267 }
1268
1269 QToolBar::mouseReleaseEvent(event);
1270}
1271
1272bool KToolBar::eventFilter(QObject * watched, QEvent * event)
1273{
1274 // Generate context menu events for disabled buttons too...
1275 if (event->type() == QEvent::MouseButtonPress) {
1276 QMouseEvent* me = static_cast<QMouseEvent*>(event);
1277 if (me->buttons() & Qt::RightButton)
1278 if (QWidget* ww = qobject_cast<QWidget*>(watched))
1279 if (ww->parent() == this && !ww->isEnabled())
1280 QCoreApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, me->pos(), me->globalPos()));
1281
1282 } else if (event->type() == QEvent::ParentChange) {
1283 // Make sure we're not leaving stale event filters around,
1284 // when a child is reparented somewhere else
1285 if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
1286 if (!this->isAncestorOf(ww)) {
1287 // New parent is not a subwidget - remove event filter
1288 ww->removeEventFilter(this);
1289 foreach (QWidget* child, ww->findChildren<QWidget*>())
1290 child->removeEventFilter(this);
1291 }
1292 }
1293 }
1294
1295 QToolButton* tb;
1296 if ((tb = qobject_cast<QToolButton*>(watched))) {
1297 const QList<QAction*> tbActions = tb->actions();
1298 if (!tbActions.isEmpty()) {
1299 // Handle MMB on toolbar buttons
1300 if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
1301 QMouseEvent* me = static_cast<QMouseEvent*>(event);
1302 if (me->button() == Qt::MidButton /*&&
1303 act->receivers(SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)))*/) {
1304 QAction* act = tbActions.first();
1305 if (me->type() == QEvent::MouseButtonPress)
1306 tb->setDown(act->isEnabled());
1307 else {
1308 tb->setDown(false);
1309 if (act->isEnabled()) {
1310 QMetaObject::invokeMethod(act, "triggered", Qt::DirectConnection,
1311 Q_ARG(Qt::MouseButtons, me->button()),
1312 Q_ARG(Qt::KeyboardModifiers, QApplication::keyboardModifiers()));
1313 }
1314 }
1315 }
1316 }
1317
1318 // CJK languages use more verbose accelerator marker: they add a Latin
1319 // letter in parenthesis, and put accelerator on that. Hence, the default
1320 // removal of ampersand only may not be enough there, instead the whole
1321 // parenthesis construct should be removed. Use KLocale's method to do this.
1322 if (event->type() == QEvent::Show || event->type() == QEvent::Paint || event->type() == QEvent::EnabledChange) {
1323 QAction *act = tb->defaultAction();
1324 if (act) {
1325 const QString text = KGlobal::locale()->removeAcceleratorMarker(act->iconText().isEmpty() ? act->text() : act->iconText());
1326 const QString toolTip = KGlobal::locale()->removeAcceleratorMarker(act->toolTip());
1327 // Filtering messages requested by translators (scripting).
1328 tb->setText(i18nc("@action:intoolbar Text label of toolbar button", "%1", text));
1329 tb->setToolTip(i18nc("@info:tooltip Tooltip of toolbar button", "%1", toolTip));
1330 }
1331 }
1332 }
1333 }
1334
1335 // Redirect mouse events to the toolbar when drag + drop editing is enabled
1336 if (toolBarsEditable()) {
1337 if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
1338 switch (event->type()) {
1339 case QEvent::MouseButtonPress: {
1340 QMouseEvent* me = static_cast<QMouseEvent*>(event);
1341 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1342 me->button(), me->buttons(), me->modifiers());
1343 mousePressEvent(&newEvent);
1344 return true;
1345 }
1346 case QEvent::MouseMove: {
1347 QMouseEvent* me = static_cast<QMouseEvent*>(event);
1348 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1349 me->button(), me->buttons(), me->modifiers());
1350 mouseMoveEvent(&newEvent);
1351 return true;
1352 }
1353 case QEvent::MouseButtonRelease: {
1354 QMouseEvent* me = static_cast<QMouseEvent*>(event);
1355 QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1356 me->button(), me->buttons(), me->modifiers());
1357 mouseReleaseEvent(&newEvent);
1358 return true;
1359 }
1360 default:
1361 break;
1362 }
1363 }
1364 }
1365
1366 return QToolBar::eventFilter(watched, event);
1367}
1368
1369void KToolBar::actionEvent(QActionEvent * event)
1370{
1371 if (event->type() == QEvent::ActionRemoved) {
1372 QWidget* widget = widgetForAction(event->action());
1373 if (widget) {
1374 widget->removeEventFilter(this);
1375
1376 foreach (QWidget* child, widget->findChildren<QWidget*>())
1377 child->removeEventFilter(this);
1378 }
1379 }
1380
1381 QToolBar::actionEvent(event);
1382
1383 if (event->type() == QEvent::ActionAdded) {
1384 QWidget* widget = widgetForAction(event->action());
1385 if (widget) {
1386 widget->installEventFilter(this);
1387
1388 foreach (QWidget* child, widget->findChildren<QWidget*>())
1389 child->installEventFilter(this);
1390 // Center widgets that do not have any use for more space. See bug 165274
1391 if (!(widget->sizePolicy().horizontalPolicy() & QSizePolicy::GrowFlag)
1392 // ... but do not center when using text besides icon in vertical toolbar. See bug 243196
1393 && !(orientation() == Qt::Vertical && toolButtonStyle() == Qt::ToolButtonTextBesideIcon)) {
1394 const int index = layout()->indexOf(widget);
1395 if (index != -1) {
1396 layout()->itemAt(index)->setAlignment(Qt::AlignJustify);
1397 }
1398 }
1399 }
1400 }
1401
1402 d->adjustSeparatorVisibility();
1403}
1404
1405bool KToolBar::toolBarsEditable()
1406{
1407 return KToolBar::Private::s_editable;
1408}
1409
1410void KToolBar::setToolBarsEditable(bool editable)
1411{
1412 if (KToolBar::Private::s_editable != editable) {
1413 KToolBar::Private::s_editable = editable;
1414 }
1415}
1416
1417void KToolBar::setToolBarsLocked(bool locked)
1418{
1419 if (KToolBar::Private::s_locked != locked) {
1420 KToolBar::Private::s_locked = locked;
1421
1422 foreach (KMainWindow* mw, KMainWindow::memberList()) {
1423 foreach (KToolBar* toolbar, mw->findChildren<KToolBar*>()) {
1424 toolbar->d->setLocked(locked);
1425 }
1426 }
1427 }
1428}
1429
1430bool KToolBar::toolBarsLocked()
1431{
1432 return KToolBar::Private::s_locked;
1433}
1434
1435#include "ktoolbar.moc"
KActionCollection
A container for a set of QAction objects.
Definition: kactioncollection.h:57
KActionCollection::allCollections
static const QList< KActionCollection * > & allCollections()
Access the list of all action collections in existence for this app.
Definition: kactioncollection.cpp:695
KActionCollection::action
QAction * action(int index) const
Return the QAction* at position "index" in the action collection.
Definition: kactioncollection.cpp:141
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:217
KComponentData
KComponentData::componentName
QString componentName() const
KConfigGroup
KConfigGroup::name
QString name() const
KConfigGroup::deleteEntry
void deleteEntry(const char *pKey, WriteConfigFlags pFlags=Normal)
KConfigGroup::hasDefault
bool hasDefault(const char *key) const
KConfigGroup::hasKey
bool hasKey(const char *key) const
KConfigGroup::writeEntry
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
KConfigGroup::readEntry
QString readEntry(const char *key, const char *aDefault=0) const
KConfigGroup::revertToDefault
void revertToDefault(const char *key)
KEditToolBar::setGlobalDefaultToolBar
static void setGlobalDefaultToolBar(const char *toolBarName)
Sets the default toolbar which will be auto-selected for all KEditToolBar instances.
Definition: kedittoolbar.cpp:689
KGlobalSettings::self
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
Definition: kglobalsettings.cpp:188
KIconLoader::MainToolbar
@ MainToolbar
Main toolbar icons.
Definition: kiconloader.h:137
KIconLoader::Toolbar
@ Toolbar
Toolbar icons.
Definition: kiconloader.h:135
KIconLoader::global
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
KIconLoader::currentSize
int currentSize(KIconLoader::Group group) const
Returns the current size of the icon group.
Definition: kiconloader.cpp:1370
KIconLoader::theme
KIconTheme * theme() const
Returns a pointer to the current theme.
Definition: kiconloader.cpp:1363
KIconTheme
Definition: kicontheme.h:47
KIconTheme::querySizes
QList< int > querySizes(KIconLoader::Group group) const
Query available sizes for a group.
Definition: kicontheme.cpp:330
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KLocale::removeAcceleratorMarker
QString removeAcceleratorMarker(const QString &label) const
KMainWindow
KDE top level main window
Definition: kmainwindow.h:107
KMainWindow::setSettingsDirty
void setSettingsDirty()
Tell the main window that it should save its settings when being closed.
Definition: kmainwindow.cpp:991
KMainWindow::memberList
static QList< KMainWindow * > memberList()
List of members of KMainWindow class.
Definition: kmainwindow.cpp:1218
KMenu
A menu with keyboard searching.
Definition: kmenu.h:42
KToggleAction
Checkbox like action.
Definition: ktoggleaction.h:41
KToolBar
Floatable toolbar with auto resize.
Definition: ktoolbar.h:54
KToolBar::applySettings
void applySettings(const KConfigGroup &cg, bool forceGlobal=false)
Read the toolbar settings from group configGroup in config and apply them.
Definition: ktoolbar.cpp:1041
KToolBar::setContextMenuEnabled
void setContextMenuEnabled(bool enable=true)
This allows you to enable or disable the context menu.
Definition: ktoolbar.cpp:815
KToolBar::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *)
Definition: ktoolbar.cpp:1090
KToolBar::contextMenuEnabled
bool contextMenuEnabled() const
Returns the context menu enabled flag.
Definition: ktoolbar.cpp:822
KToolBar::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *)
Definition: ktoolbar.cpp:874
KToolBar::mousePressEvent
virtual void mousePressEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1207
KToolBar::addXMLGUIClient
void addXMLGUIClient(KXMLGUIClient *client)
Adds an XML gui client that uses this toolbar.
Definition: ktoolbar.cpp:864
KToolBar::dropEvent
virtual void dropEvent(QDropEvent *)
Definition: ktoolbar.cpp:1184
KToolBar::toolBarsEditable
static bool toolBarsEditable()
Returns whether the toolbars are currently editable (drag & drop of actions).
Definition: ktoolbar.cpp:1405
KToolBar::~KToolBar
virtual ~KToolBar()
Destroys the toolbar.
Definition: ktoolbar.cpp:808
KToolBar::toolBarsLocked
static bool toolBarsLocked()
Returns whether the toolbars are locked (i.e., moving of the toobars disallowed).
Definition: ktoolbar.cpp:1430
KToolBar::setToolBarsEditable
static void setToolBarsEditable(bool editable)
Enable or disable toolbar editing via drag & drop of actions.
Definition: ktoolbar.cpp:1410
KToolBar::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *)
Definition: ktoolbar.cpp:1132
KToolBar::mainWindow
KMainWindow * mainWindow() const
Returns the main window that this toolbar is docked with.
Definition: ktoolbar.cpp:1068
KToolBar::loadState
void loadState(const QDomElement &element)
Load state from an XML.
Definition: ktoolbar.cpp:900
KToolBar::slotMovableChanged
virtual void slotMovableChanged(bool movable)
Definition: ktoolbar.cpp:1084
KToolBar::setXMLGUIClient
void setXMLGUIClient(KXMLGUIClient *client)
Sets the XML gui client.
Definition: ktoolbar.cpp:857
KToolBar::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1221
KToolBar::eventFilter
bool eventFilter(QObject *watched, QEvent *event)
Reimplemented to support context menu activation on disabled tool buttons.
Definition: ktoolbar.cpp:1272
KToolBar::setToolBarsLocked
static void setToolBarsLocked(bool locked)
Allows you to lock and unlock all toolbars (i.e., disallow/allow moving of the toobars).
Definition: ktoolbar.cpp:1417
KToolBar::saveState
void saveState(QDomElement &element) const
Save state into an XML.
Definition: ktoolbar.cpp:1008
KToolBar::KToolBar
KToolBar(QWidget *parent, bool isMainToolBar=false, bool readConfig=true)
Constructor.
Definition: ktoolbar.cpp:766
KToolBar::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *)
Definition: ktoolbar.cpp:1169
KToolBar::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1260
KToolBar::removeXMLGUIClient
void removeXMLGUIClient(KXMLGUIClient *client)
Removes an XML gui client that uses this toolbar.
Definition: ktoolbar.cpp:869
KToolBar::setIconDimensions
void setIconDimensions(int size)
Convenience function to set icon size.
Definition: ktoolbar.cpp:1073
KToolBar::saveSettings
void saveSettings(KConfigGroup &cg)
Save the toolbar settings to group configGroup in config.
Definition: ktoolbar.cpp:828
KToolBar::actionEvent
virtual void actionEvent(QActionEvent *)
Definition: ktoolbar.cpp:1369
KToolBar::iconSizeDefault
int iconSizeDefault() const
Returns the default size for this type of toolbar.
Definition: ktoolbar.cpp:1079
KToolBar::toolButtonStyleSetting
static Qt::ToolButtonStyle toolButtonStyleSetting()
Returns the global setting for "Icon Text" for the main toolbar.
Definition: ktoolbar.cpp:893
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
Definition: kxmlguiclient.h:47
KXMLGUIClient::xmlFile
virtual QString xmlFile() const
This will return the name of the XML file as set by setXMLFile().
Definition: kxmlguiclient.cpp:154
KXMLGUIClient::actionCollection
virtual KActionCollection * actionCollection() const
Retrieves the entire action collection for the GUI client.
Definition: kxmlguiclient.cpp:128
KXMLGUIClient::componentData
virtual KComponentData componentData() const
Definition: kxmlguiclient.cpp:144
KXMLGUIFactory::readConfigFile
static QString readConfigFile(const QString &filename, const KComponentData &componentData=KComponentData())
Definition: kxmlguifactory.cpp:117
KXMLGUIFactory::actionPropertiesElement
static QDomElement actionPropertiesElement(QDomDocument &doc)
Definition: kxmlguifactory.cpp:769
KXMLGUIFactory::findActionByName
static QDomElement findActionByName(QDomElement &elem, const QString &sName, bool create)
Definition: kxmlguifactory.cpp:783
KXMLGUIFactory::saveConfigFile
static bool saveConfigFile(const QDomDocument &doc, const QString &filename, const KComponentData &componentData=KComponentData())
Definition: kxmlguifactory.cpp:142
KXmlGuiWindow
KDE top level main window with predefined action layout
Definition: kxmlguiwindow.h:62
KXmlGuiWindow::toolBarMenuAction
QAction * toolBarMenuAction()
Returns a pointer to the mainwindows action responsible for the toolbars menu.
Definition: kxmlguiwindow.cpp:100
KXmlGuiWindow::setupToolbarMenuActions
void setupToolbarMenuActions()
Definition: kxmlguiwindow.cpp:110
QAction
QFrame
QList
QMainWindow
QMap
QMenu
QObject
QSet
QToolBar
QToolButton
QWidget
kaction.h
kactioncollection.h
kapplication.h
kauthorized.h
kconfig.h
defaultValue
QString defaultValue(const QString &t)
kconfiggroup.h
kdebug.h
kedittoolbar.h
kglobalsettings.h
kguiitem.h
kicon.h
kiconloader.h
klocale.h
i18n
QString i18n(const char *text)
i18nc
QString i18nc(const char *ctxt, const char *text)
kmenu.h
kstandardaction.h
ktoggleaction.h
Unset
@ Unset
Definition: ktoolbar.cpp:87
SettingLevel
SettingLevel
Definition: ktoolbar.cpp:85
Level_AppXML
@ Level_AppXML
Definition: ktoolbar.cpp:85
Level_UserSettings
@ Level_UserSettings
Definition: ktoolbar.cpp:85
NSettingLevels
@ NSettingLevels
Definition: ktoolbar.cpp:86
Level_KDEDefault
@ Level_KDEDefault
Definition: ktoolbar.cpp:85
ktoolbar.h
readConfig
TsConfig readConfig(const QString &fname)
kxmlguifactory.h
kxmlguiwindow.h
KAuthorized::authorize
bool authorize(const QString &genericAction)
KGlobal::mainComponent
const KComponentData & mainComponent()
KGlobal::locale
KLocale * locale()
KGlobal::config
KSharedConfigPtr config()
group
group
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
KStandardAction::ConfigureToolbars
@ ConfigureToolbars
Definition: kstandardaction.h:153
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal