1 /*
2 This file is part of the Kasten Framework, made within the KDE community.
3
4 SPDX-FileCopyrightText: 2006-2009, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9 #include "closecontroller.hpp"
10
11 // Kasten gui
12 #include <Kasten/AbstractDocumentStrategy>
13 // Kasten core
14 #include <Kasten/AbstractDocument>
15 // KF
16 #include <KActionCollection>
17 #include <KStandardAction>
18 #include <KXMLGUIClient>
19 #include <KLocalizedString>
20 // Qt
21 #include <QAction>
22
23 namespace Kasten {
24
CloseController(AbstractDocumentStrategy * documentStrategy,KXMLGUIClient * guiClient,bool supportMultiple)25 CloseController::CloseController(AbstractDocumentStrategy* documentStrategy,
26 KXMLGUIClient* guiClient,
27 bool supportMultiple)
28 : mDocumentStrategy(documentStrategy)
29 {
30 KActionCollection* actionCollection = guiClient->actionCollection();
31
32 const QIcon documentCloseIcon = QIcon::fromTheme(QStringLiteral("document-close"));
33 mCloseAction = KStandardAction::close(this, &CloseController::close, this);
34 mCloseAction->setEnabled(false);
35
36 actionCollection->addAction(mCloseAction->objectName(), mCloseAction);
37
38 if (supportMultiple) {
39 mCloseAllAction = new QAction(documentCloseIcon,
40 i18nc("@action:inmenu", "Close All"), this);
41 mCloseAllAction->setObjectName(QStringLiteral("file_close_all"));
42 connect(mCloseAllAction, &QAction::triggered,
43 this, &CloseController::closeAll);
44 mCloseAllAction->setEnabled(false);
45
46 mCloseAllOtherAction = new QAction(documentCloseIcon,
47 i18nc("@action:inmenu", "Close All Other"), this);
48 mCloseAllOtherAction->setObjectName(QStringLiteral("file_close_all_other"));
49 connect(mCloseAllOtherAction, &QAction::triggered,
50 this, &CloseController::closeAllOther);
51 mCloseAllOtherAction->setEnabled(false);
52
53 actionCollection->addAction(mCloseAllAction->objectName(), mCloseAllAction);
54 actionCollection->addAction(mCloseAllOtherAction->objectName(), mCloseAllOtherAction);
55
56 connect(mDocumentStrategy, &Kasten::AbstractDocumentStrategy::added,
57 this, &CloseController::onDocumentsChanged);
58 connect(mDocumentStrategy, &Kasten::AbstractDocumentStrategy::closing,
59 this, &CloseController::onDocumentsChanged);
60 }
61 }
62
63 CloseController::~CloseController() = default;
64
setTargetModel(AbstractModel * model)65 void CloseController::setTargetModel(AbstractModel* model)
66 {
67 mDocument = model ? model->findBaseModel<AbstractDocument*>() : nullptr;
68 const bool hasDocument = (mDocument != nullptr);
69
70 mCloseAction->setEnabled(hasDocument);
71 }
72
close()73 void CloseController::close()
74 {
75 if (mDocumentStrategy->canClose(mDocument)) {
76 mDocumentStrategy->closeDocument(mDocument);
77 }
78 }
79
closeAll()80 void CloseController::closeAll()
81 {
82 if (mDocumentStrategy->canCloseAll()) {
83 mDocumentStrategy->closeAll();
84 }
85 }
86
closeAllOther()87 void CloseController::closeAllOther()
88 {
89 if (mDocumentStrategy->canCloseAllOther(mDocument)) {
90 mDocumentStrategy->closeAllOther(mDocument);
91 }
92 }
93
onDocumentsChanged()94 void CloseController::onDocumentsChanged()
95 {
96 const QVector<AbstractDocument*> documents = mDocumentStrategy->documents();
97
98 const bool hasDocuments = !documents.isEmpty();
99 // TODO: there could be just one, but not set for this tool?
100 const bool hasOtherDocuments = (documents.size() > 1);
101
102 mCloseAllAction->setEnabled(hasDocuments);
103 mCloseAllOtherAction->setEnabled(hasOtherDocuments);
104 }
105
106 }
107