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
44KToggleToolBarAction::KToggleToolBarAction(KToolBar *toolBar, const QString &text, QObject *parent)
45 : KToggleAction(text, parent)
46 , d(new KToggleToolBarActionPrivate(this))
47{
48 d->toolBar = toolBar;
49 d->toolBar->installEventFilter(filterObj: this);
50
51 d->beingToggled = true;
52 setChecked(d->toolBar->isVisible());
53 d->beingToggled = false;
54}
55
56KToggleToolBarAction::~KToggleToolBarAction() = default;
57
58bool KToggleToolBarAction::eventFilter(QObject *watched, QEvent *event)
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
90KToolBar *KToggleToolBarAction::toolBar()
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();
103 if (mw && qobject_cast<KMainWindow *>(object: mw)) {
104 static_cast<KMainWindow *>(mw)->setSettingsDirty();
105 }
106 }
107
108 KToggleAction::slotToggled(checked);
109}
110
111#include "moc_ktoggletoolbaraction.cpp"
112

source code of kxmlgui/src/ktoggletoolbaraction.cpp