File indexing completed on 2024-09-22 05:16:05

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2009, 2011 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 "closecontroller.hpp"
0010 
0011 // Kasten gui
0012 #include <Kasten/AbstractDocumentStrategy>
0013 // Kasten core
0014 #include <Kasten/AbstractDocument>
0015 // KF
0016 #include <KActionCollection>
0017 #include <KStandardAction>
0018 #include <KXMLGUIClient>
0019 #include <KLocalizedString>
0020 // Qt
0021 #include <QAction>
0022 
0023 namespace Kasten {
0024 
0025 CloseController::CloseController(AbstractDocumentStrategy* documentStrategy,
0026                                  KXMLGUIClient* guiClient,
0027                                  bool supportMultiple)
0028     : mDocumentStrategy(documentStrategy)
0029 {
0030     KActionCollection* actionCollection = guiClient->actionCollection();
0031 
0032     const QIcon documentCloseIcon = QIcon::fromTheme(QStringLiteral("document-close"));
0033     mCloseAction  = KStandardAction::close(this, &CloseController::close, this);
0034     mCloseAction->setEnabled(false);
0035 
0036     actionCollection->addAction(mCloseAction->objectName(), mCloseAction);
0037 
0038     if (supportMultiple) {
0039         mCloseAllAction = new QAction(documentCloseIcon,
0040                                       i18nc("@action:inmenu", "Close All"), this);
0041         mCloseAllAction->setObjectName(QStringLiteral("file_close_all"));
0042         connect(mCloseAllAction, &QAction::triggered,
0043                 this, &CloseController::closeAll);
0044         mCloseAllAction->setEnabled(false);
0045 
0046         mCloseAllOtherAction = new QAction(documentCloseIcon,
0047                                            i18nc("@action:inmenu", "Close All Other"), this);
0048         mCloseAllOtherAction->setObjectName(QStringLiteral("file_close_all_other"));
0049         connect(mCloseAllOtherAction, &QAction::triggered,
0050                 this, &CloseController::closeAllOther);
0051         mCloseAllOtherAction->setEnabled(false);
0052 
0053         actionCollection->addAction(mCloseAllAction->objectName(), mCloseAllAction);
0054         actionCollection->addAction(mCloseAllOtherAction->objectName(), mCloseAllOtherAction);
0055 
0056         connect(mDocumentStrategy, &Kasten::AbstractDocumentStrategy::added,
0057                 this, &CloseController::onDocumentsChanged);
0058         connect(mDocumentStrategy, &Kasten::AbstractDocumentStrategy::closing,
0059                 this, &CloseController::onDocumentsChanged);
0060     }
0061 }
0062 
0063 CloseController::~CloseController() = default;
0064 
0065 void CloseController::setTargetModel(AbstractModel* model)
0066 {
0067     mDocument = model ? model->findBaseModel<AbstractDocument*>() : nullptr;
0068     const bool hasDocument = (mDocument != nullptr);
0069 
0070     mCloseAction->setEnabled(hasDocument);
0071 }
0072 
0073 void CloseController::close()
0074 {
0075     if (mDocumentStrategy->canClose(mDocument)) {
0076         mDocumentStrategy->closeDocument(mDocument);
0077     }
0078 }
0079 
0080 void CloseController::closeAll()
0081 {
0082     if (mDocumentStrategy->canCloseAll()) {
0083         mDocumentStrategy->closeAll();
0084     }
0085 }
0086 
0087 void CloseController::closeAllOther()
0088 {
0089     if (mDocumentStrategy->canCloseAllOther(mDocument)) {
0090         mDocumentStrategy->closeAllOther(mDocument);
0091     }
0092 }
0093 
0094 void CloseController::onDocumentsChanged()
0095 {
0096     const QVector<AbstractDocument*> documents = mDocumentStrategy->documents();
0097 
0098     const bool hasDocuments = !documents.isEmpty();
0099     // TODO: there could be just one, but not set for this tool?
0100     const bool hasOtherDocuments = (documents.size() > 1);
0101 
0102     mCloseAllAction->setEnabled(hasDocuments);
0103     mCloseAllOtherAction->setEnabled(hasOtherDocuments);
0104 }
0105 
0106 }
0107 
0108 #include "moc_closecontroller.cpp"