KXmlGui

ktoggletoolbaraction.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <[email protected]>
4  SPDX-FileCopyrightText: 1999 Simon Hausmann <[email protected]>
5  SPDX-FileCopyrightText: 2000 Nicolas Hadacek <[email protected]>
6  SPDX-FileCopyrightText: 2000 Kurt Granroth <[email protected]>
7  SPDX-FileCopyrightText: 2000 Michael Koch <[email protected]>
8  SPDX-FileCopyrightText: 2001 Holger Freyther <[email protected]>
9  SPDX-FileCopyrightText: 2002 Ellis Whitehead <[email protected]>
10  SPDX-FileCopyrightText: 2002 Joseph Wenninger <[email protected]>
11  SPDX-FileCopyrightText: 2003 Andras Mantia <[email protected]>
12  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
13 
14  SPDX-License-Identifier: LGPL-2.0-only
15 */
16 
17 #include "ktoggletoolbaraction.h"
18 
19 #include <QByteArray>
20 #include <QEvent>
21 #include <QPointer>
22 
23 #include <KAuthorized>
24 
25 #include <klocalizedstring.h>
26 
27 #include "kmainwindow.h"
28 #include "ktoolbar.h"
29 
30 class Q_DECL_HIDDEN KToggleToolBarAction::Private
31 {
32 public:
33  Private(KToggleToolBarAction *q)
34  : toolBarName(nullptr), toolBar(nullptr), beingToggled(false)
35  {
36  const bool authorized = KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar"));
37  q->setEnabled(authorized);
38  q->setVisible(authorized);
39  }
40 
41  QByteArray toolBarName;
42  QPointer<KToolBar> toolBar;
43  bool beingToggled;
44 };
45 
46 KToggleToolBarAction::KToggleToolBarAction(const char *toolBarName, const QString &text, QObject *parent)
47  : KToggleAction(text, parent),
48  d(new Private(this))
49 {
50  d->toolBarName = toolBarName;
51 }
52 
54  : KToggleAction(text, parent),
55  d(new Private(this))
56 {
57  d->toolBar = toolBar;
58  d->toolBar->installEventFilter(this);
59 
60  d->beingToggled = true;
61  setChecked(d->toolBar->isVisible());
62  d->beingToggled = false;
63 }
64 
66 {
67  delete d;
68 }
69 
71 {
72  if (d->beingToggled) {
73  return false;
74  }
75 
76  d->beingToggled = true;
77 
78  if (watched == d->toolBar) {
79  switch (event->type()) {
80  case QEvent::Hide:
81  if (isChecked()) {
82  setChecked(false);
83  }
84  break;
85 
86  case QEvent::Show:
87  if (!isChecked()) {
88  setChecked(true);
89  }
90  break;
91 
92  default:
93  break;
94  }
95  }
96 
97  d->beingToggled = false;
98 
99  return false;
100 }
101 
103 {
104  return d->toolBar;
105 }
106 
107 void KToggleToolBarAction::slotToggled(bool checked)
108 {
109  if (!d->beingToggled && d->toolBar && checked != d->toolBar->isVisible()) {
110  d->beingToggled = true;
111  d->toolBar->setVisible(checked);
112  d->beingToggled = false;
113 
114  QMainWindow *mw = d->toolBar->mainWindow();
115  if (mw && qobject_cast<KMainWindow *>(mw)) {
116  static_cast<KMainWindow *>(mw)->setSettingsDirty();
117  }
118  }
119 
120  KToggleAction::slotToggled(checked);
121 }
122 
QString text() const const
KToggleToolBarAction(const char *toolBarName, const QString &text, QObject *parent)
Create a KToggleToolBarAction that manages the toolbar named toolBarName.
QEvent::Type type() const const
void setChecked(bool)
void setVisible(bool)
virtual bool event(QEvent *e) override
An action that takes care of everything associated with showing or hiding a toolbar by a menu action...
Top level main window.
Definition: kmainwindow.h:96
Floatable toolbar with auto resize.
Definition: ktoolbar.h:59
~KToggleToolBarAction() override
Destroys toggle toolbar action.
KToolBar * toolBar()
Returns a pointer to the tool bar it manages.
bool eventFilter(QObject *watched, QEvent *event) override
Reimplemented from QObject.
KCONFIGCORE_EXPORT bool authorizeAction(const QString &action)
QObject * parent() const const
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Oct 27 2020 22:55:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.