KXmlGui

ktoggletoolbaraction.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
4 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
5 SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
6 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
7 SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
8 SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
9 SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
10 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
11 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
12 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
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 "kmainwindow.h"
26#include "ktoolbar.h"
27
28class KToggleToolBarActionPrivate
29{
30public:
31 KToggleToolBarActionPrivate(KToggleToolBarAction *q)
32 : toolBar(nullptr)
33 , beingToggled(false)
34 {
35 const bool authorized = KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar"));
36 q->setEnabled(authorized);
37 q->setVisible(authorized);
38 }
39
40 QPointer<KToolBar> toolBar;
41 bool beingToggled;
42};
43
45 : KToggleAction(text, parent)
46 , d(new KToggleToolBarActionPrivate(this))
47{
48 d->toolBar = toolBar;
49 d->toolBar->installEventFilter(this);
50
51 d->beingToggled = true;
52 setChecked(d->toolBar->isVisible());
53 d->beingToggled = false;
54}
55
57
59{
60 if (d->beingToggled) {
61 return false;
62 }
63
64 d->beingToggled = true;
65
66 if (watched == d->toolBar) {
67 switch (event->type()) {
68 case QEvent::Hide:
69 if (isChecked()) {
70 setChecked(false);
71 }
72 break;
73
74 case QEvent::Show:
75 if (!isChecked()) {
76 setChecked(true);
77 }
78 break;
79
80 default:
81 break;
82 }
83 }
84
85 d->beingToggled = false;
86
87 return false;
88}
89
91{
92 return d->toolBar;
93}
94
95void KToggleToolBarAction::slotToggled(bool checked)
96{
97 if (!d->beingToggled && d->toolBar && checked != d->toolBar->isVisible()) {
98 d->beingToggled = true;
99 d->toolBar->setVisible(checked);
100 d->beingToggled = false;
101
102 QMainWindow *mw = d->toolBar->mainWindow();
104 static_cast<KMainWindow *>(mw)->setSettingsDirty();
105 }
106 }
107
108 KToggleAction::slotToggled(checked);
109}
110
111#include "moc_ktoggletoolbaraction.cpp"
static Q_INVOKABLE bool authorizeAction(const QString &action)
KMainWindow represents a top-level main window.
Definition kmainwindow.h:60
An action that takes care of everything associated with showing or hiding a toolbar by a menu action.
KToggleToolBarAction(KToolBar *toolBar, const QString &text, QObject *parent)
Create a KToggleToolBarAction that manages the toolBar.
~KToggleToolBarAction() override
Destroys toggle toolbar action.
bool eventFilter(QObject *watched, QEvent *event) override
Reimplemented from QObject.
KToolBar * toolBar()
Returns a pointer to the tool bar it manages.
Floatable toolbar with auto resize.
Definition ktoolbar.h:68
void setChecked(bool)
void setEnabled(bool)
virtual bool event(QEvent *e) override
void setVisible(bool)
QList< T > findChildren(Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:55:55 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.