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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • 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 */
85 enum SettingLevel { Level_KDEDefault, Level_AppXML, Level_UserSettings,
86  NSettingLevels };
87 enum { Unset = -1 };
88 
89 class 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 
226 bool KToolBar::Private::s_editable = false;
227 bool KToolBar::Private::s_locked = true;
228 
229 void 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 
271 QString 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 
287 KMenu *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 
417 void KToolBar::Private::setLocked(bool locked)
418 {
419  if (unlockedMovable)
420  q->setMovable(!locked);
421 }
422 
423 void 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 
452 Qt::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 
465 QString 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 
481 Qt::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
495 void KToolBar::Private::slotAppearanceChanged()
496 {
497  loadKDESettings();
498  applyCurrentSettings();
499 }
500 
501 void 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
531 void 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 
545 QAction *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 
559 void 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 
648 void 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 
675 void KToolBar::Private::slotContextLeft()
676 {
677  q->mainWindow()->addToolBar(Qt::LeftToolBarArea, q);
678 }
679 
680 void KToolBar::Private::slotContextRight()
681 {
682  q->mainWindow()->addToolBar(Qt::RightToolBarArea, q);
683 }
684 
685 void 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 
716 void KToolBar::Private::slotContextTop()
717 {
718  q->mainWindow()->addToolBar(Qt::TopToolBarArea, q);
719 }
720 
721 void KToolBar::Private::slotContextBottom()
722 {
723  q->mainWindow()->addToolBar(Qt::BottomToolBarArea, q);
724 }
725 
726 void KToolBar::Private::slotContextIcons()
727 {
728  q->setToolButtonStyle(Qt::ToolButtonIconOnly);
729  toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
730 }
731 
732 void KToolBar::Private::slotContextText()
733 {
734  q->setToolButtonStyle(Qt::ToolButtonTextOnly);
735  toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
736 }
737 
738 void KToolBar::Private::slotContextTextUnder()
739 {
740  q->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
741  toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
742 }
743 
744 void KToolBar::Private::slotContextTextRight()
745 {
746  q->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
747  toolButtonStyleSettings[Level_UserSettings] = q->toolButtonStyle();
748 }
749 
750 void 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 
759 void KToolBar::Private::slotLockToolBars(bool lock)
760 {
761  q->setToolBarsLocked(lock);
762 }
763 
764 
765 
766 KToolBar::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 
777 KToolBar::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 
791 KToolBar::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 
808 KToolBar::~KToolBar()
809 {
810  delete d->contextLockAction;
811  delete d;
812 }
813 
814 #ifndef KDE_NO_DEPRECATED
815 void KToolBar::setContextMenuEnabled(bool enable)
816 {
817  d->enableContext = enable;
818 }
819 #endif
820 
821 #ifndef KDE_NO_DEPRECATED
822 bool KToolBar::contextMenuEnabled() const
823 {
824  return d->enableContext;
825 }
826 #endif
827 
828 void KToolBar::saveSettings(KConfigGroup &cg)
829 {
830  Q_ASSERT(!cg.name().isEmpty());
831 
832  cg.deleteEntry("Hidden"); // remove old key to avoid bugs from the compat code in applySettings. KDE5: remove.
833 
834  const int currentIconSize = iconSize().width();
835  //kDebug() << objectName() << currentIconSize << d->iconSizeSettings.toString() << "defaultValue=" << d->iconSizeSettings.defaultValue();
836  if (!cg.hasDefault("IconSize") && currentIconSize == d->iconSizeSettings.defaultValue()) {
837  cg.revertToDefault("IconSize");
838  d->iconSizeSettings[Level_UserSettings] = Unset;
839  } else {
840  cg.writeEntry("IconSize", currentIconSize);
841  d->iconSizeSettings[Level_UserSettings] = currentIconSize;
842  }
843 
844  const Qt::ToolButtonStyle currentToolButtonStyle = toolButtonStyle();
845  if (!cg.hasDefault("ToolButtonStyle") && currentToolButtonStyle == d->toolButtonStyleSettings.defaultValue()) {
846  cg.revertToDefault("ToolButtonStyle");
847  d->toolButtonStyleSettings[Level_UserSettings] = Unset;
848  } else {
849  cg.writeEntry("ToolButtonStyle", d->toolButtonStyleToString(currentToolButtonStyle));
850  d->toolButtonStyleSettings[Level_UserSettings] = currentToolButtonStyle;
851  }
852 }
853 
854 #ifndef KDE_NO_DEPRECATED
855 void KToolBar::setXMLGUIClient(KXMLGUIClient *client)
856 {
857  d->xmlguiClients.clear();
858  d->xmlguiClients << client;
859 }
860 #endif
861 
862 void KToolBar::addXMLGUIClient( KXMLGUIClient *client )
863 {
864  d->xmlguiClients << client;
865 }
866 
867 void KToolBar::removeXMLGUIClient( KXMLGUIClient *client )
868 {
869  d->xmlguiClients.remove(client);
870 }
871 
872 void KToolBar::contextMenuEvent(QContextMenuEvent* event)
873 {
874 #ifndef KDE_NO_DEPRECATED
875  if (mainWindow() && d->enableContext) {
876  QPointer<KToolBar> guard(this);
877  const QPoint globalPos = event->globalPos();
878  d->contextMenu(globalPos)->exec(globalPos);
879 
880  // "Configure Toolbars" recreates toolbars, so we might not exist anymore.
881  if (guard) {
882  d->slotContextAboutToHide();
883  }
884  return;
885  }
886 #endif
887 
888  QToolBar::contextMenuEvent(event);
889 }
890 
891 Qt::ToolButtonStyle KToolBar::toolButtonStyleSetting()
892 {
893  KConfigGroup group(KGlobal::config(), "Toolbar style");
894  const QString fallback = Private::toolButtonStyleToString(Qt::ToolButtonTextBesideIcon);
895  return KToolBar::Private::toolButtonStyleFromString(group.readEntry("ToolButtonStyle", fallback));
896 }
897 
898 void KToolBar::loadState(const QDomElement &element)
899 {
900  QMainWindow *mw = mainWindow();
901  if (!mw)
902  return;
903 
904  {
905  QDomNode textNode = element.namedItem("text");
906  QByteArray text;
907  QByteArray context;
908  if (textNode.isElement())
909  {
910  QDomElement textElement = textNode.toElement();
911  text = textElement.text().toUtf8();
912  context = textElement.attribute("context").toUtf8();
913  }
914  else
915  {
916  textNode = element.namedItem("Text");
917  if (textNode.isElement())
918  {
919  QDomElement textElement = textNode.toElement();
920  text = textElement.text().toUtf8();
921  context = textElement.attribute("context").toUtf8();
922  }
923  }
924 
925  QString i18nText;
926  if (!text.isEmpty() && !context.isEmpty())
927  i18nText = i18nc(context, text);
928  else if (!text.isEmpty())
929  i18nText = i18n(text);
930 
931  if (!i18nText.isEmpty())
932  setWindowTitle(i18nText);
933  }
934 
935  /*
936  This method is called in order to load toolbar settings from XML.
937  However this can be used in two rather different cases:
938  - for the initial loading of the app's XML. In that case the settings
939  are only the defaults (Level_AppXML), the user's KConfig settings will override them
940 
941  - for later re-loading when switching between parts in KXMLGUIFactory.
942  In that case the XML contains the final settings, not the defaults.
943  We do need the defaults, and the toolbar might have been completely
944  deleted and recreated meanwhile. So we store the app-default settings
945  into the XML.
946  */
947  bool loadingAppDefaults = true;
948  if (element.hasAttribute("tempXml")) {
949  // this isn't the first time, so the app-xml defaults have been saved into the (in-memory) XML
950  loadingAppDefaults = false;
951  const QString iconSizeDefault = element.attribute("iconSizeDefault");
952  if (!iconSizeDefault.isEmpty()) {
953  d->iconSizeSettings[Level_AppXML] = iconSizeDefault.toInt();
954  }
955  const QString toolButtonStyleDefault = element.attribute("toolButtonStyleDefault");
956  if (!toolButtonStyleDefault.isEmpty()) {
957  d->toolButtonStyleSettings[Level_AppXML] = d->toolButtonStyleFromString(toolButtonStyleDefault);
958  }
959  } else {
960  // loading app defaults
961  bool newLine = false;
962  QString attrNewLine = element.attribute("newline").toLower();
963  if (!attrNewLine.isEmpty())
964  newLine = attrNewLine == "true";
965  if (newLine && mw)
966  mw->insertToolBarBreak(this);
967  }
968 
969  int newIconSize = -1;
970  if (element.hasAttribute("iconSize")) {
971  bool ok;
972  newIconSize = element.attribute("iconSize").trimmed().toInt(&ok);
973  if (!ok)
974  newIconSize = -1;
975  }
976  if (newIconSize != -1)
977  d->iconSizeSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = newIconSize;
978 
979  const QString newToolButtonStyle = element.attribute("iconText");
980  if (!newToolButtonStyle.isEmpty())
981  d->toolButtonStyleSettings[loadingAppDefaults ? Level_AppXML : Level_UserSettings] = d->toolButtonStyleFromString(newToolButtonStyle);
982 
983  bool hidden = false;
984  {
985  QString attrHidden = element.attribute("hidden").toLower();
986  if (!attrHidden.isEmpty())
987  hidden = attrHidden == "true";
988  }
989 
990  Qt::ToolBarArea pos = Qt::NoToolBarArea;
991  {
992  QString attrPosition = element.attribute("position").toLower();
993  if (!attrPosition.isEmpty())
994  pos = KToolBar::Private::positionFromString(attrPosition);
995  }
996  if (pos != Qt::NoToolBarArea)
997  mw->addToolBar(pos, this);
998 
999  setVisible(!hidden);
1000 
1001  d->applyCurrentSettings();
1002 }
1003 
1004 // Called when switching between xmlgui clients, in order to find any unsaved settings
1005 // again when switching back to the current xmlgui client.
1006 void KToolBar::saveState(QDomElement &current) const
1007 {
1008  Q_ASSERT(!current.isNull());
1009 
1010  current.setAttribute("tempXml", "true");
1011 
1012  current.setAttribute("noMerge", "1");
1013  current.setAttribute("position", d->getPositionAsString().toLower());
1014  current.setAttribute("hidden", isHidden() ? "true" : "false");
1015 
1016  const int currentIconSize = iconSize().width();
1017  if (currentIconSize == d->iconSizeSettings.defaultValue())
1018  current.removeAttribute("iconSize");
1019  else
1020  current.setAttribute("iconSize", iconSize().width());
1021 
1022  if (toolButtonStyle() == d->toolButtonStyleSettings.defaultValue())
1023  current.removeAttribute("iconText");
1024  else
1025  current.setAttribute("iconText", d->toolButtonStyleToString(toolButtonStyle()));
1026 
1027  // Note: if this method is used by more than KXMLGUIBuilder, e.g. to save XML settings to *disk*,
1028  // then the stuff below shouldn't always be done. This is not the case currently though.
1029  if (d->iconSizeSettings[Level_AppXML] != Unset) {
1030  current.setAttribute("iconSizeDefault", d->iconSizeSettings[Level_AppXML]);
1031  }
1032  if (d->toolButtonStyleSettings[Level_AppXML] != Unset) {
1033  const Qt::ToolButtonStyle bs = static_cast<Qt::ToolButtonStyle>(d->toolButtonStyleSettings[Level_AppXML]);
1034  current.setAttribute("toolButtonStyleDefault", d->toolButtonStyleToString(bs));
1035  }
1036 }
1037 
1038 // called by KMainWindow::applyMainWindowSettings to read from the user settings
1039 void KToolBar::applySettings(const KConfigGroup &cg, bool forceGlobal)
1040 {
1041  Q_ASSERT(!cg.name().isEmpty());
1042  Q_UNUSED(forceGlobal); // KDE5: remove
1043 
1044  // a small leftover from kde3: separate bool for hidden/shown. But it's also part of saveMainWindowSettings,
1045  // it is not really useful anymore, except in the unlikely case where someone would call this by hand.
1046  // KDE5: remove the block below
1047  if (cg.hasKey("Hidden")) {
1048  const bool hidden = cg.readEntry("Hidden", false);
1049  if (hidden)
1050  hide();
1051  else {
1052  show();
1053  }
1054  }
1055 
1056  if (cg.hasKey("IconSize")) {
1057  d->iconSizeSettings[Level_UserSettings] = cg.readEntry("IconSize", 0);
1058  }
1059  if (cg.hasKey("ToolButtonStyle")) {
1060  d->toolButtonStyleSettings[Level_UserSettings] = d->toolButtonStyleFromString(cg.readEntry("ToolButtonStyle", QString()));
1061  }
1062 
1063  d->applyCurrentSettings();
1064 }
1065 
1066 KMainWindow * KToolBar::mainWindow() const
1067 {
1068  return qobject_cast<KMainWindow*>(const_cast<QObject*>(parent()));
1069 }
1070 
1071 void KToolBar::setIconDimensions(int size)
1072 {
1073  QToolBar::setIconSize(QSize(size, size));
1074  d->iconSizeSettings[Level_UserSettings] = size;
1075 }
1076 
1077 int KToolBar::iconSizeDefault() const
1078 {
1079  return KIconLoader::global()->currentSize(d->isMainToolBar ? KIconLoader::MainToolbar : KIconLoader::Toolbar);
1080 }
1081 
1082 void KToolBar::slotMovableChanged(bool movable)
1083 {
1084  if (movable && !KAuthorized::authorize("movable_toolbars"))
1085  setMovable(false);
1086 }
1087 
1088 void KToolBar::dragEnterEvent(QDragEnterEvent *event)
1089 {
1090  if (toolBarsEditable() && event->proposedAction() & (Qt::CopyAction | Qt::MoveAction) &&
1091  event->mimeData()->hasFormat("application/x-kde-action-list")) {
1092  QByteArray data = event->mimeData()->data("application/x-kde-action-list");
1093 
1094  QDataStream stream(data);
1095 
1096  QStringList actionNames;
1097 
1098  stream >> actionNames;
1099 
1100  foreach (const QString& actionName, actionNames) {
1101  foreach (KActionCollection* ac, KActionCollection::allCollections()) {
1102  QAction* newAction = ac->action(actionName.toLatin1().constData());
1103  if (newAction) {
1104  d->actionsBeingDragged.append(newAction);
1105  break;
1106  }
1107  }
1108  }
1109 
1110  if (d->actionsBeingDragged.count()) {
1111  QAction* overAction = actionAt(event->pos());
1112 
1113  QFrame* dropIndicatorWidget = new QFrame(this);
1114  dropIndicatorWidget->resize(8, height() - 4);
1115  dropIndicatorWidget->setFrameShape(QFrame::VLine);
1116  dropIndicatorWidget->setLineWidth(3);
1117 
1118  d->dropIndicatorAction = insertWidget(overAction, dropIndicatorWidget);
1119 
1120  insertAction(overAction, d->dropIndicatorAction);
1121 
1122  event->acceptProposedAction();
1123  return;
1124  }
1125  }
1126 
1127  QToolBar::dragEnterEvent(event);
1128 }
1129 
1130 void KToolBar::dragMoveEvent(QDragMoveEvent *event)
1131 {
1132  if (toolBarsEditable())
1133  forever {
1134  if (d->dropIndicatorAction) {
1135  QAction* overAction = 0L;
1136  foreach (QAction* action, actions()) {
1137  // want to make it feel that half way across an action you're dropping on the other side of it
1138  QWidget* widget = widgetForAction(action);
1139  if (event->pos().x() < widget->pos().x() + (widget->width() / 2)) {
1140  overAction = action;
1141  break;
1142  }
1143  }
1144 
1145  if (overAction != d->dropIndicatorAction) {
1146  // Check to see if the indicator is already in the right spot
1147  int dropIndicatorIndex = actions().indexOf(d->dropIndicatorAction);
1148  if (dropIndicatorIndex + 1 < actions().count()) {
1149  if (actions()[ dropIndicatorIndex + 1 ] == overAction)
1150  break;
1151  } else if (!overAction) {
1152  break;
1153  }
1154 
1155  insertAction(overAction, d->dropIndicatorAction);
1156  }
1157 
1158  event->accept();
1159  return;
1160  }
1161  break;
1162  }
1163 
1164  QToolBar::dragMoveEvent(event);
1165 }
1166 
1167 void KToolBar::dragLeaveEvent(QDragLeaveEvent *event)
1168 {
1169  // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1170  delete d->dropIndicatorAction;
1171  d->dropIndicatorAction = 0L;
1172  d->actionsBeingDragged.clear();
1173 
1174  if (toolBarsEditable()) {
1175  event->accept();
1176  return;
1177  }
1178 
1179  QToolBar::dragLeaveEvent(event);
1180 }
1181 
1182 void KToolBar::dropEvent(QDropEvent *event)
1183 {
1184  if (toolBarsEditable()) {
1185  foreach (QAction* action, d->actionsBeingDragged) {
1186  if (actions().contains(action))
1187  removeAction(action);
1188  insertAction(d->dropIndicatorAction, action);
1189  }
1190  }
1191 
1192  // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1193  delete d->dropIndicatorAction;
1194  d->dropIndicatorAction = 0L;
1195  d->actionsBeingDragged.clear();
1196 
1197  if (toolBarsEditable()) {
1198  event->accept();
1199  return;
1200  }
1201 
1202  QToolBar::dropEvent(event);
1203 }
1204 
1205 void KToolBar::mousePressEvent(QMouseEvent *event)
1206 {
1207  if (toolBarsEditable() && event->button() == Qt::LeftButton) {
1208  if (KAction* action = qobject_cast<KAction*>(actionAt(event->pos()))) {
1209  d->dragAction = action;
1210  d->dragStartPosition = event->pos();
1211  event->accept();
1212  return;
1213  }
1214  }
1215 
1216  QToolBar::mousePressEvent(event);
1217 }
1218 
1219 void KToolBar::mouseMoveEvent(QMouseEvent *event)
1220 {
1221  if (!toolBarsEditable() || !d->dragAction)
1222  return QToolBar::mouseMoveEvent(event);
1223 
1224  if ((event->pos() - d->dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
1225  event->accept();
1226  return;
1227  }
1228 
1229  QDrag *drag = new QDrag(this);
1230  QMimeData *mimeData = new QMimeData;
1231 
1232  QByteArray data;
1233  {
1234  QDataStream stream(&data, QIODevice::WriteOnly);
1235 
1236  QStringList actionNames;
1237  actionNames << d->dragAction->objectName();
1238 
1239  stream << actionNames;
1240  }
1241 
1242  mimeData->setData("application/x-kde-action-list", data);
1243 
1244  drag->setMimeData(mimeData);
1245 
1246  Qt::DropAction dropAction = drag->start(Qt::MoveAction);
1247 
1248  if (dropAction == Qt::MoveAction)
1249  // Only remove from this toolbar if it was moved to another toolbar
1250  // Otherwise the receiver moves it.
1251  if (drag->target() != this)
1252  removeAction(d->dragAction);
1253 
1254  d->dragAction = 0L;
1255  event->accept();
1256 }
1257 
1258 void KToolBar::mouseReleaseEvent(QMouseEvent *event)
1259 {
1260  // Want to clear this even if toolBarsEditable was changed mid-drag (unlikey)
1261  if (d->dragAction) {
1262  d->dragAction = 0L;
1263  event->accept();
1264  return;
1265  }
1266 
1267  QToolBar::mouseReleaseEvent(event);
1268 }
1269 
1270 bool KToolBar::eventFilter(QObject * watched, QEvent * event)
1271 {
1272  // Generate context menu events for disabled buttons too...
1273  if (event->type() == QEvent::MouseButtonPress) {
1274  QMouseEvent* me = static_cast<QMouseEvent*>(event);
1275  if (me->buttons() & Qt::RightButton)
1276  if (QWidget* ww = qobject_cast<QWidget*>(watched))
1277  if (ww->parent() == this && !ww->isEnabled())
1278  QCoreApplication::postEvent(this, new QContextMenuEvent(QContextMenuEvent::Mouse, me->pos(), me->globalPos()));
1279 
1280  } else if (event->type() == QEvent::ParentChange) {
1281  // Make sure we're not leaving stale event filters around,
1282  // when a child is reparented somewhere else
1283  if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
1284  if (!this->isAncestorOf(ww)) {
1285  // New parent is not a subwidget - remove event filter
1286  ww->removeEventFilter(this);
1287  foreach (QWidget* child, ww->findChildren<QWidget*>())
1288  child->removeEventFilter(this);
1289  }
1290  }
1291  }
1292 
1293  QToolButton* tb;
1294  if ((tb = qobject_cast<QToolButton*>(watched))) {
1295  const QList<QAction*> tbActions = tb->actions();
1296  if (!tbActions.isEmpty()) {
1297  // Handle MMB on toolbar buttons
1298  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
1299  QMouseEvent* me = static_cast<QMouseEvent*>(event);
1300  if (me->button() == Qt::MidButton /*&&
1301  act->receivers(SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)))*/) {
1302  QAction* act = tbActions.first();
1303  if (me->type() == QEvent::MouseButtonPress)
1304  tb->setDown(act->isEnabled());
1305  else {
1306  tb->setDown(false);
1307  if (act->isEnabled()) {
1308  QMetaObject::invokeMethod(act, "triggered", Qt::DirectConnection,
1309  Q_ARG(Qt::MouseButtons, me->button()),
1310  Q_ARG(Qt::KeyboardModifiers, QApplication::keyboardModifiers()));
1311  }
1312  }
1313  }
1314  }
1315 
1316  // CJK languages use more verbose accelerator marker: they add a Latin
1317  // letter in parenthesis, and put accelerator on that. Hence, the default
1318  // removal of ampersand only may not be enough there, instead the whole
1319  // parenthesis construct should be removed. Use KLocale's method to do this.
1320  if (event->type() == QEvent::Show || event->type() == QEvent::Paint || event->type() == QEvent::EnabledChange) {
1321  QAction *act = tb->defaultAction();
1322  if (act) {
1323  const QString text = KGlobal::locale()->removeAcceleratorMarker(act->iconText().isEmpty() ? act->text() : act->iconText());
1324  const QString toolTip = KGlobal::locale()->removeAcceleratorMarker(act->toolTip());
1325  // Filtering messages requested by translators (scripting).
1326  tb->setText(i18nc("@action:intoolbar Text label of toolbar button", "%1", text));
1327  tb->setToolTip(i18nc("@info:tooltip Tooltip of toolbar button", "%1", toolTip));
1328  }
1329  }
1330  }
1331  }
1332 
1333  // Redirect mouse events to the toolbar when drag + drop editing is enabled
1334  if (toolBarsEditable()) {
1335  if (QWidget* ww = qobject_cast<QWidget*>(watched)) {
1336  switch (event->type()) {
1337  case QEvent::MouseButtonPress: {
1338  QMouseEvent* me = static_cast<QMouseEvent*>(event);
1339  QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1340  me->button(), me->buttons(), me->modifiers());
1341  mousePressEvent(&newEvent);
1342  return true;
1343  }
1344  case QEvent::MouseMove: {
1345  QMouseEvent* me = static_cast<QMouseEvent*>(event);
1346  QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1347  me->button(), me->buttons(), me->modifiers());
1348  mouseMoveEvent(&newEvent);
1349  return true;
1350  }
1351  case QEvent::MouseButtonRelease: {
1352  QMouseEvent* me = static_cast<QMouseEvent*>(event);
1353  QMouseEvent newEvent(me->type(), mapFromGlobal(ww->mapToGlobal(me->pos())), me->globalPos(),
1354  me->button(), me->buttons(), me->modifiers());
1355  mouseReleaseEvent(&newEvent);
1356  return true;
1357  }
1358  default:
1359  break;
1360  }
1361  }
1362  }
1363 
1364  return QToolBar::eventFilter(watched, event);
1365 }
1366 
1367 void KToolBar::actionEvent(QActionEvent * event)
1368 {
1369  if (event->type() == QEvent::ActionRemoved) {
1370  QWidget* widget = widgetForAction(event->action());
1371  if (widget) {
1372  widget->removeEventFilter(this);
1373 
1374  foreach (QWidget* child, widget->findChildren<QWidget*>())
1375  child->removeEventFilter(this);
1376  }
1377  }
1378 
1379  QToolBar::actionEvent(event);
1380 
1381  if (event->type() == QEvent::ActionAdded) {
1382  QWidget* widget = widgetForAction(event->action());
1383  if (widget) {
1384  widget->installEventFilter(this);
1385 
1386  foreach (QWidget* child, widget->findChildren<QWidget*>())
1387  child->installEventFilter(this);
1388  // Center widgets that do not have any use for more space. See bug 165274
1389  if (!(widget->sizePolicy().horizontalPolicy() & QSizePolicy::GrowFlag)
1390  // ... but do not center when using text besides icon in vertical toolbar. See bug 243196
1391  && !(orientation() == Qt::Vertical && toolButtonStyle() == Qt::ToolButtonTextBesideIcon)) {
1392  const int index = layout()->indexOf(widget);
1393  if (index != -1) {
1394  layout()->itemAt(index)->setAlignment(Qt::AlignJustify);
1395  }
1396  }
1397  }
1398  }
1399 
1400  d->adjustSeparatorVisibility();
1401 }
1402 
1403 bool KToolBar::toolBarsEditable()
1404 {
1405  return KToolBar::Private::s_editable;
1406 }
1407 
1408 void KToolBar::setToolBarsEditable(bool editable)
1409 {
1410  if (KToolBar::Private::s_editable != editable) {
1411  KToolBar::Private::s_editable = editable;
1412  }
1413 }
1414 
1415 void KToolBar::setToolBarsLocked(bool locked)
1416 {
1417  if (KToolBar::Private::s_locked != locked) {
1418  KToolBar::Private::s_locked = locked;
1419 
1420  foreach (KMainWindow* mw, KMainWindow::memberList()) {
1421  foreach (KToolBar* toolbar, mw->findChildren<KToolBar*>()) {
1422  toolbar->d->setLocked(locked);
1423  }
1424  }
1425  }
1426 }
1427 
1428 bool KToolBar::toolBarsLocked()
1429 {
1430  return KToolBar::Private::s_locked;
1431 }
1432 
1433 #include "ktoolbar.moc"
KXmlGuiWindow::toolBarMenuAction
QAction * toolBarMenuAction()
Returns a pointer to the mainwindows action responsible for the toolbars menu.
Definition: kxmlguiwindow.cpp:100
i18n
QString i18n(const char *text)
KActionCollection
A container for a set of QAction objects.
Definition: kactioncollection.h:56
KToolBar::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1258
Level_KDEDefault
Definition: ktoolbar.cpp:85
KIconLoader::currentSize
int currentSize(KIconLoader::Group group) const
Returns the current size of the icon group.
Definition: kiconloader.cpp:1370
KToolBar::~KToolBar
virtual ~KToolBar()
Destroys the toolbar.
Definition: ktoolbar.cpp:808
KMainWindow::setSettingsDirty
void setSettingsDirty()
Tell the main window that it should save its settings when being closed.
Definition: kmainwindow.cpp:949
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:1415
KXMLGUIClient::actionCollection
virtual KActionCollection * actionCollection() const
Retrieves the entire action collection for the GUI client.
Definition: kxmlguiclient.cpp:128
kdebug.h
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document...
Definition: kxmlguiclient.h:46
kapplication.h
KIconLoader::global
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
group
kglobalsettings.h
KToolBar::saveSettings
void saveSettings(KConfigGroup &cg)
Save the toolbar settings to group configGroup in config.
Definition: ktoolbar.cpp:828
kauthorized.h
KConfigGroup::hasDefault
bool hasDefault(const QString &key) const
kactioncollection.h
Level_AppXML
Definition: ktoolbar.cpp:85
KMenu
A menu with keyboard searching.
Definition: kmenu.h:42
Level_UserSettings
Definition: ktoolbar.cpp:85
kconfig.h
KActionCollection::allCollections
static const QList< KActionCollection * > & allCollections()
Access the list of all action collections in existence for this app.
Definition: kactioncollection.cpp:695
KToolBar::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *)
Definition: ktoolbar.cpp:1130
QWidget
KDE_NO_DEPRECATED
#define KDE_NO_DEPRECATED
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
kxmlguifactory.h
KConfigGroup::writeEntry
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
KStandardAction::ConfigureToolbars
Definition: kstandardaction.h:153
kiconloader.h
KToolBar::slotMovableChanged
virtual void slotMovableChanged(bool movable)
Definition: ktoolbar.cpp:1082
KXMLGUIClient::xmlFile
virtual QString xmlFile() const
This will return the name of the XML file as set by setXMLFile().
Definition: kxmlguiclient.cpp:154
QString
KToolBar::iconSizeDefault
int iconSizeDefault() const
Returns the default size for this type of toolbar.
Definition: ktoolbar.cpp:1077
KGlobalSettings::self
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
Definition: kglobalsettings.cpp:188
KToolBar::actionEvent
virtual void actionEvent(QActionEvent *)
Definition: ktoolbar.cpp:1367
KToolBar::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *)
Definition: ktoolbar.cpp:1088
QObject
klocale.h
KToolBar::setToolBarsEditable
static void setToolBarsEditable(bool editable)
Enable or disable toolbar editing via drag & drop of actions.
Definition: ktoolbar.cpp:1408
kxmlguiwindow.h
i18nc
QString i18nc(const char *ctxt, const char *text)
KGlobal::config
KSharedConfigPtr config()
KIconTheme::querySizes
QList< int > querySizes(KIconLoader::Group group) const
Query available sizes for a group.
Definition: kicontheme.cpp:330
KToolBar::removeXMLGUIClient
void removeXMLGUIClient(KXMLGUIClient *client)
Removes an XML gui client that uses this toolbar.
Definition: ktoolbar.cpp:867
KToolBar::mousePressEvent
virtual void mousePressEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1205
KToolBar::eventFilter
bool eventFilter(QObject *watched, QEvent *event)
Reimplemented to support context menu activation on disabled tool buttons.
Definition: ktoolbar.cpp:1270
KConfigGroup::deleteEntry
void deleteEntry(const QString &pKey, WriteConfigFlags pFlags=Normal)
KToolBar::dropEvent
virtual void dropEvent(QDropEvent *)
Definition: ktoolbar.cpp:1182
KXmlGuiWindow
KDE top level main window with predefined action layout
Definition: kxmlguiwindow.h:61
KMainWindow
KDE top level main window
Definition: kmainwindow.h:106
kmenu.h
KXMLGUIClient::componentData
virtual KComponentData componentData() const
Definition: kxmlguiclient.cpp:144
QToolBar
KXmlGuiWindow::setupToolbarMenuActions
void setupToolbarMenuActions()
Definition: kxmlguiwindow.cpp:110
NSettingLevels
Definition: ktoolbar.cpp:86
QStringList
KXMLGUIFactory::readConfigFile
static QString readConfigFile(const QString &filename, const KComponentData &componentData=KComponentData())
Definition: kxmlguifactory.cpp:117
KToolBar::setIconDimensions
void setIconDimensions(int size)
Convenience function to set icon size.
Definition: ktoolbar.cpp:1071
KToolBar::setContextMenuEnabled
void setContextMenuEnabled(bool enable=true)
This allows you to enable or disable the context menu.
Definition: ktoolbar.cpp:815
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
KComponentData::componentName
QString componentName() const
kstandardaction.h
kedittoolbar.h
KIconLoader::theme
KIconTheme * theme() const
Returns a pointer to the current theme.
Definition: kiconloader.cpp:1363
kaction.h
QSet< KXMLGUIClient * >
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:1039
KIconLoader::MainToolbar
Main toolbar icons.
Definition: kiconloader.h:137
KConfigGroup::name
QString name() const
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
KConfigGroup::hasKey
bool hasKey(const QString &key) const
KToolBar::addXMLGUIClient
void addXMLGUIClient(KXMLGUIClient *client)
Adds an XML gui client that uses this toolbar.
Definition: ktoolbar.cpp:862
KGlobal::locale
KLocale * locale()
KToolBar::KToolBar
KToolBar(QWidget *parent, bool isMainToolBar=false, bool readConfig=true)
Constructor.
Definition: ktoolbar.cpp:766
QMenu
KLocale::removeAcceleratorMarker
QString removeAcceleratorMarker(const QString &label) const
KConfigGroup
KToolBar::toolButtonStyleSetting
static Qt::ToolButtonStyle toolButtonStyleSetting()
Returns the global setting for "Icon Text" for the main toolbar.
Definition: ktoolbar.cpp:891
ktoolbar.h
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
KIconLoader::Toolbar
Toolbar icons.
Definition: kiconloader.h:135
QPoint
QMainWindow
KToolBar::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *)
Definition: ktoolbar.cpp:1167
KToolBar
Floatable toolbar with auto resize.
Definition: ktoolbar.h:53
KToolBar::contextMenuEnabled
bool contextMenuEnabled() const
Returns the context menu enabled flag.
Definition: ktoolbar.cpp:822
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:216
KGlobal::mainComponent
const KComponentData & mainComponent()
KToolBar::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *)
Definition: ktoolbar.cpp:1219
KAuthorized::authorize
bool authorize(const QString &genericAction)
ktoggleaction.h
KToolBar::loadState
void loadState(const QDomElement &element)
Load state from an XML.
Definition: ktoolbar.cpp:898
KActionCollection::action
QAction * action(int index) const
Return the QAction* at position "index" in the action collection.
Definition: kactioncollection.cpp:141
QSize
KToggleAction
Checkbox like action.
Definition: ktoggleaction.h:40
KToolBar::toolBarsEditable
static bool toolBarsEditable()
Returns whether the toolbars are currently editable (drag & drop of actions).
Definition: ktoolbar.cpp:1403
KToolBar::mainWindow
KMainWindow * mainWindow() const
Returns the main window that this toolbar is docked with.
Definition: ktoolbar.cpp:1066
QToolButton
kicon.h
kguiitem.h
QFrame
Unset
Definition: ktoolbar.cpp:87
SettingLevel
SettingLevel
Definition: ktoolbar.cpp:85
KXMLGUIFactory::saveConfigFile
static bool saveConfigFile(const QDomDocument &doc, const QString &filename, const KComponentData &componentData=KComponentData())
Definition: kxmlguifactory.cpp:142
KIconTheme
Definition: kicontheme.h:46
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KConfigGroup::revertToDefault
void revertToDefault(const QString &key)
KXMLGUIFactory::actionPropertiesElement
static QDomElement actionPropertiesElement(QDomDocument &doc)
Definition: kxmlguifactory.cpp:769
KComponentData
QAction
QMap< QAction *, int >
KXMLGUIFactory::findActionByName
static QDomElement findActionByName(QDomElement &elem, const QString &sName, bool create)
Definition: kxmlguifactory.cpp:783
KToolBar::saveState
void saveState(QDomElement &element) const
Save state into an XML.
Definition: ktoolbar.cpp:1006
KMainWindow::memberList
static QList< KMainWindow * > memberList()
List of members of KMainWindow class.
Definition: kmainwindow.cpp:1176
kconfiggroup.h
KToolBar::toolBarsLocked
static bool toolBarsLocked()
Returns whether the toolbars are locked (i.e., moving of the toobars disallowed). ...
Definition: ktoolbar.cpp:1428
KToolBar::setXMLGUIClient
void setXMLGUIClient(KXMLGUIClient *client)
Sets the XML gui client.
Definition: ktoolbar.cpp:855
QList< QAction * >
KToolBar::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *)
Definition: ktoolbar.cpp:872
defaultValue
QString defaultValue(const QString &t)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:16 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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