File indexing completed on 2025-01-26 05:24:12

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "readonlycontroller.hpp"
0010 
0011 // Kasten core
0012 #include <Kasten/AbstractDocument>
0013 // KF
0014 #include <KXMLGUIClient>
0015 #include <KLocalizedString>
0016 #include <KActionCollection>
0017 #include <KToggleAction>
0018 #include <KGuiItem>
0019 
0020 namespace Kasten {
0021 
0022 ReadOnlyController::ReadOnlyController(KXMLGUIClient* guiClient)
0023 {
0024     mSetReadOnlyAction = new KToggleAction(QIcon::fromTheme(QStringLiteral("object-unlocked")),
0025                                            i18nc("@option:check set the document to read-only", "Set Read-only"),
0026                                            this);
0027     const QString checkedText = i18nc("@option:check set the document to read-write", "Set Read-write");
0028     const KGuiItem checkedState(checkedText, QIcon::fromTheme(QStringLiteral("object-locked")));
0029     mSetReadOnlyAction->setCheckedState(checkedState);
0030     connect(mSetReadOnlyAction, &QAction::triggered,
0031             this, &ReadOnlyController::setReadOnly);
0032 
0033     guiClient->actionCollection()->addAction(QStringLiteral("isreadonly"), mSetReadOnlyAction);
0034 
0035     setTargetModel(nullptr);
0036 }
0037 
0038 void ReadOnlyController::setTargetModel(AbstractModel* model)
0039 {
0040     if (mDocument) {
0041         mDocument->disconnect(mSetReadOnlyAction);
0042     }
0043 
0044     mDocument = model ? model->findBaseModel<AbstractDocument*>() : nullptr;
0045 
0046     if (mDocument) {
0047         mSetReadOnlyAction->setChecked(mDocument->isReadOnly());
0048 
0049         connect(mDocument, &AbstractModel::readOnlyChanged,
0050                 mSetReadOnlyAction, &QAction::setChecked);
0051         connect(mDocument, &AbstractModel::modifiableChanged,
0052                 mSetReadOnlyAction, &QAction::setEnabled);
0053     }
0054 
0055     mSetReadOnlyAction->setEnabled(mDocument ? mDocument->isModifiable() : false);
0056 }
0057 
0058 void ReadOnlyController::setReadOnly(bool isReadOnly)
0059 {
0060     mDocument->setReadOnly(isReadOnly);
0061 }
0062 
0063 }
0064 
0065 #include "moc_readonlycontroller.cpp"