File indexing completed on 2024-09-29 09:33:16

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
0004     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0005     SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
0006     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0007     SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
0008     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
0009     SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
0010     SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
0011     SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
0012     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0013 
0014     SPDX-License-Identifier: LGPL-2.0-only
0015 */
0016 
0017 #include "ktoggletoolbaraction.h"
0018 
0019 #include <QByteArray>
0020 #include <QEvent>
0021 #include <QPointer>
0022 
0023 #include <KAuthorized>
0024 
0025 #include "kmainwindow.h"
0026 #include "ktoolbar.h"
0027 
0028 class KToggleToolBarActionPrivate
0029 {
0030 public:
0031     KToggleToolBarActionPrivate(KToggleToolBarAction *q)
0032         : toolBarName(nullptr)
0033         , toolBar(nullptr)
0034         , beingToggled(false)
0035     {
0036         const bool authorized = KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar"));
0037         q->setEnabled(authorized);
0038         q->setVisible(authorized);
0039     }
0040 
0041     QByteArray toolBarName;
0042     QPointer<KToolBar> toolBar;
0043     bool beingToggled;
0044 };
0045 
0046 KToggleToolBarAction::KToggleToolBarAction(const char *toolBarName, const QString &text, QObject *parent)
0047     : KToggleAction(text, parent)
0048     , d(new KToggleToolBarActionPrivate(this))
0049 {
0050     d->toolBarName = toolBarName;
0051 }
0052 
0053 KToggleToolBarAction::KToggleToolBarAction(KToolBar *toolBar, const QString &text, QObject *parent)
0054     : KToggleAction(text, parent)
0055     , d(new KToggleToolBarActionPrivate(this))
0056 {
0057     d->toolBar = toolBar;
0058     d->toolBar->installEventFilter(this);
0059 
0060     d->beingToggled = true;
0061     setChecked(d->toolBar->isVisible());
0062     d->beingToggled = false;
0063 }
0064 
0065 KToggleToolBarAction::~KToggleToolBarAction() = default;
0066 
0067 bool KToggleToolBarAction::eventFilter(QObject *watched, QEvent *event)
0068 {
0069     if (d->beingToggled) {
0070         return false;
0071     }
0072 
0073     d->beingToggled = true;
0074 
0075     if (watched == d->toolBar) {
0076         switch (event->type()) {
0077         case QEvent::Hide:
0078             if (isChecked()) {
0079                 setChecked(false);
0080             }
0081             break;
0082 
0083         case QEvent::Show:
0084             if (!isChecked()) {
0085                 setChecked(true);
0086             }
0087             break;
0088 
0089         default:
0090             break;
0091         }
0092     }
0093 
0094     d->beingToggled = false;
0095 
0096     return false;
0097 }
0098 
0099 KToolBar *KToggleToolBarAction::toolBar()
0100 {
0101     return d->toolBar;
0102 }
0103 
0104 void KToggleToolBarAction::slotToggled(bool checked)
0105 {
0106     if (!d->beingToggled && d->toolBar && checked != d->toolBar->isVisible()) {
0107         d->beingToggled = true;
0108         d->toolBar->setVisible(checked);
0109         d->beingToggled = false;
0110 
0111         QMainWindow *mw = d->toolBar->mainWindow();
0112         if (mw && qobject_cast<KMainWindow *>(mw)) {
0113             static_cast<KMainWindow *>(mw)->setSettingsDirty();
0114         }
0115     }
0116 
0117     KToggleAction::slotToggled(checked);
0118 }
0119 
0120 #include "moc_ktoggletoolbaraction.cpp"