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

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2021 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 "viewareacontextmenucontroller.hpp"
0010 
0011 // Kasten gui
0012 #include <Kasten/MultiViewAreas>
0013 #include <Kasten/AbstractView>
0014 // Kasten core
0015 #include <Kasten/DocumentSyncManager>
0016 #include <Kasten/AbstractModelFileSystemSynchronizer>
0017 // KF
0018 #include <KXMLGUIClient>
0019 #include <KXMLGUIFactory>
0020 #include <KActionCollection>
0021 #include <KStandardAction>
0022 #include <KLocalizedString>
0023 // Qt
0024 #include <QMenu>
0025 
0026 namespace Kasten {
0027 
0028 ViewAreaContextMenuController::ViewAreaContextMenuController(MultiViewAreas* multiViewAreas,
0029                                                              DocumentSyncManager* syncManager,
0030                                                              KXMLGUIClient* guiClient)
0031     : mGuiClient(guiClient)
0032     , mMultiViewAreas(multiViewAreas)
0033     , mSyncManager(syncManager)
0034 {
0035     KActionCollection* actionCollection = guiClient->actionCollection();
0036 
0037     mReloadAction = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")),
0038                                 i18nc("@action:inmenu", "Reloa&d"), this);
0039     mReloadAction->setObjectName(QStringLiteral("viewarea_reload"));
0040     connect(mReloadAction, &QAction::triggered,
0041             this, &ViewAreaContextMenuController::reload);
0042     mReloadAction->setEnabled(false);
0043 
0044     actionCollection->addAction(mReloadAction->objectName(), mReloadAction);
0045 
0046     const QIcon documentCloseIcon = QIcon::fromTheme(QStringLiteral("document-close"));
0047     mCloseAction = KStandardAction::close(this, &ViewAreaContextMenuController::close, this);
0048     mCloseAction->setObjectName(QStringLiteral("viewarea_close"));
0049     mCloseAction->setProperty("defaultShortcuts", QVariant());
0050     mCloseAction->setShortcuts({});
0051     mCloseAction->setEnabled(false);
0052 
0053     actionCollection->addAction(mCloseAction->objectName(), mCloseAction);
0054 
0055     mCloseAllAction = new QAction(documentCloseIcon,
0056                                     i18nc("@action:inmenu", "Close All"), this);
0057     mCloseAllAction->setObjectName(QStringLiteral("viewarea_close_all"));
0058     connect(mCloseAllAction, &QAction::triggered,
0059             this, &ViewAreaContextMenuController::closeAll);
0060     mCloseAllAction->setEnabled(false);
0061 
0062     mCloseAllOtherAction = new QAction(documentCloseIcon,
0063                                         i18nc("@action:inmenu", "Close All Other"), this);
0064     mCloseAllOtherAction->setObjectName(QStringLiteral("viewarea_close_all_other"));
0065     connect(mCloseAllOtherAction, &QAction::triggered,
0066             this, &ViewAreaContextMenuController::closeAllOther);
0067     mCloseAllOtherAction->setEnabled(false);
0068 
0069     actionCollection->addAction(mCloseAllAction->objectName(), mCloseAllAction);
0070     actionCollection->addAction(mCloseAllOtherAction->objectName(), mCloseAllOtherAction);
0071 
0072     connect(mMultiViewAreas, &MultiViewAreas::contextMenuRequested,
0073             this, &ViewAreaContextMenuController::showContextMenu);
0074 }
0075 
0076 void ViewAreaContextMenuController::setTargetModel(AbstractModel* model)
0077 {
0078     Q_UNUSED(model)
0079 }
0080 
0081 void ViewAreaContextMenuController::showContextMenu(AbstractGroupedViews* viewArea, AbstractView* view, QPoint pos)
0082 {
0083     mViewArea = viewArea;
0084     mView = view;
0085     mDocument = mView ? mView->findBaseModel<AbstractDocument*>() : nullptr;
0086     auto* synchronizer = mDocument ? qobject_cast<AbstractModelFileSystemSynchronizer*>(mDocument->synchronizer()) : nullptr;
0087 
0088     const bool isForView = (mView != nullptr);
0089     const auto views = mViewArea->viewList();
0090     bool canSync = false;
0091     if (synchronizer) {
0092         const LocalSyncState localSyncState = synchronizer->localSyncState();
0093         const RemoteSyncState remoteSyncState = synchronizer->remoteSyncState();
0094         canSync = (localSyncState == LocalHasChanges)
0095                   || (remoteSyncState == RemoteHasChanges)
0096                   || (remoteSyncState == RemoteUnknown);
0097     }
0098     mReloadAction->setEnabled(canSync);
0099 
0100     mCloseAction->setEnabled(isForView);
0101     mCloseAllOtherAction->setEnabled(isForView && views.size() > 1);
0102     mCloseAllAction->setEnabled(!views.isEmpty());
0103 
0104     QWidget* w = mGuiClient->factory()->container(QStringLiteral("viewAreaContextMenu"), mGuiClient);
0105     auto* popup = static_cast<QMenu*>(w);
0106 
0107     const auto popupPoint = mMultiViewAreas->widget()->mapToGlobal(pos);
0108 
0109     popup->popup(popupPoint);
0110 }
0111 
0112 void ViewAreaContextMenuController::reload()
0113 {
0114     mSyncManager->reload(mDocument);
0115 }
0116 
0117 void ViewAreaContextMenuController::close()
0118 {
0119     Q_EMIT mMultiViewAreas->closeRequest({mView});
0120 }
0121 
0122 void ViewAreaContextMenuController::closeAll()
0123 {
0124     Q_EMIT mMultiViewAreas->closeRequest(mViewArea->viewList());
0125 }
0126 
0127 void ViewAreaContextMenuController::closeAllOther()
0128 {
0129     auto views = mViewArea->viewList();
0130     views.removeOne(mView);
0131     Q_EMIT mMultiViewAreas->closeRequest(views);
0132 }
0133 
0134 }
0135 
0136 #include "moc_viewareacontextmenucontroller.cpp"