File indexing completed on 2024-09-29 06:35:52

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         : toolBar(nullptr)
0033         , beingToggled(false)
0034     {
0035         const bool authorized = KAuthorized::authorizeAction(QStringLiteral("options_show_toolbar"));
0036         q->setEnabled(authorized);
0037         q->setVisible(authorized);
0038     }
0039 
0040     QPointer<KToolBar> toolBar;
0041     bool beingToggled;
0042 };
0043 
0044 KToggleToolBarAction::KToggleToolBarAction(KToolBar *toolBar, const QString &text, QObject *parent)
0045     : KToggleAction(text, parent)
0046     , d(new KToggleToolBarActionPrivate(this))
0047 {
0048     d->toolBar = toolBar;
0049     d->toolBar->installEventFilter(this);
0050 
0051     d->beingToggled = true;
0052     setChecked(d->toolBar->isVisible());
0053     d->beingToggled = false;
0054 }
0055 
0056 KToggleToolBarAction::~KToggleToolBarAction() = default;
0057 
0058 bool KToggleToolBarAction::eventFilter(QObject *watched, QEvent *event)
0059 {
0060     if (d->beingToggled) {
0061         return false;
0062     }
0063 
0064     d->beingToggled = true;
0065 
0066     if (watched == d->toolBar) {
0067         switch (event->type()) {
0068         case QEvent::Hide:
0069             if (isChecked()) {
0070                 setChecked(false);
0071             }
0072             break;
0073 
0074         case QEvent::Show:
0075             if (!isChecked()) {
0076                 setChecked(true);
0077             }
0078             break;
0079 
0080         default:
0081             break;
0082         }
0083     }
0084 
0085     d->beingToggled = false;
0086 
0087     return false;
0088 }
0089 
0090 KToolBar *KToggleToolBarAction::toolBar()
0091 {
0092     return d->toolBar;
0093 }
0094 
0095 void KToggleToolBarAction::slotToggled(bool checked)
0096 {
0097     if (!d->beingToggled && d->toolBar && checked != d->toolBar->isVisible()) {
0098         d->beingToggled = true;
0099         d->toolBar->setVisible(checked);
0100         d->beingToggled = false;
0101 
0102         QMainWindow *mw = d->toolBar->mainWindow();
0103         if (mw && qobject_cast<KMainWindow *>(mw)) {
0104             static_cast<KMainWindow *>(mw)->setSettingsDirty();
0105         }
0106     }
0107 
0108     KToggleAction::slotToggled(checked);
0109 }
0110 
0111 #include "moc_ktoggletoolbaraction.cpp"