KXmlGui

ktoggletoolbaraction.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Reginald Stadlbauer <[email protected]>
3  (C) 1999 Simon Hausmann <[email protected]>
4  (C) 2000 Nicolas Hadacek <[email protected]>
5  (C) 2000 Kurt Granroth <[email protected]>
6  (C) 2000 Michael Koch <[email protected]>
7  (C) 2001 Holger Freyther <[email protected]>
8  (C) 2002 Ellis Whitehead <[email protected]>
9  (C) 2002 Joseph Wenninger <[email protected]>
10  (C) 2003 Andras Mantia <[email protected]>
11  (C) 2005-2006 Hamish Rodda <[email protected]>
12 
13  This library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU Library General Public
15  License version 2 as published by the Free Software Foundation.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Library General Public License for more details.
21 
22  You should have received a copy of the GNU Library General Public License
23  along with this library; see the file COPYING.LIB. If not, write to
24  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  Boston, MA 02110-1301, USA.
26 */
27 
28 #include "ktoggletoolbaraction.h"
29 
30 #include <QByteArray>
31 #include <QEvent>
32 #include <QPointer>
33 
34 #include <KAuthorized>
35 
36 #include <klocalizedstring.h>
37 
38 #include "kmainwindow.h"
39 #include "ktoolbar.h"
40 
41 class Q_DECL_HIDDEN KToggleToolBarAction::Private
42 {
43 public:
44  Private(KToggleToolBarAction *q)
45  : toolBarName(nullptr), toolBar(nullptr), beingToggled(false)
46  {
47  const bool authorized = KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar"));
48  q->setEnabled(authorized);
49  q->setVisible(authorized);
50  }
51 
52  QByteArray toolBarName;
53  QPointer<KToolBar> toolBar;
54  bool beingToggled;
55 };
56 
57 KToggleToolBarAction::KToggleToolBarAction(const char *toolBarName, const QString &text, QObject *parent)
58  : KToggleAction(text, parent),
59  d(new Private(this))
60 {
61  d->toolBarName = toolBarName;
62 }
63 
65  : KToggleAction(text, parent),
66  d(new Private(this))
67 {
68  d->toolBar = toolBar;
69  d->toolBar->installEventFilter(this);
70 
71  d->beingToggled = true;
72  setChecked(d->toolBar->isVisible());
73  d->beingToggled = false;
74 }
75 
77 {
78  delete d;
79 }
80 
82 {
83  if (d->beingToggled) {
84  return false;
85  }
86 
87  d->beingToggled = true;
88 
89  if (watched == d->toolBar) {
90  switch (event->type()) {
91  case QEvent::Hide:
92  if (isChecked()) {
93  setChecked(false);
94  }
95  break;
96 
97  case QEvent::Show:
98  if (!isChecked()) {
99  setChecked(true);
100  }
101  break;
102 
103  default:
104  break;
105  }
106  }
107 
108  d->beingToggled = false;
109 
110  return false;
111 }
112 
114 {
115  return d->toolBar;
116 }
117 
118 void KToggleToolBarAction::slotToggled(bool checked)
119 {
120  if (!d->beingToggled && d->toolBar && checked != d->toolBar->isVisible()) {
121  d->beingToggled = true;
122  d->toolBar->setVisible(checked);
123  d->beingToggled = false;
124 
125  QMainWindow *mw = d->toolBar->mainWindow();
126  if (mw && qobject_cast<KMainWindow *>(mw)) {
127  static_cast<KMainWindow *>(mw)->setSettingsDirty();
128  }
129  }
130 
131  KToggleAction::slotToggled(checked);
132 }
133 
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:110
Floatable toolbar with auto resize.
Definition: ktoolbar.h:70
~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 Thu Aug 6 2020 22:49:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.