From b5f102abbde7d6d63a0d26923ac0722609bd2585 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 23 Jun 2024 19:37:42 +0200 Subject: [PATCH] Editor plugin: Disable editation when signature plugin is active and vice versa --- .../EditorPlugin/editorplugin.cpp | 16 +++++++- .../EditorPlugin/editorplugin.h | 1 + .../SignaturePlugin/signatureplugin.cpp | 18 +++++++++ .../SignaturePlugin/signatureplugin.h | 1 + Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp | 37 +++++++++++++++++++ Pdf4QtLibWidgets/sources/pdfdrawwidget.h | 7 ++++ .../sources/pdfpagecontentelements.cpp | 1 + .../sources/pdfpagecontentelements.h | 2 + 8 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.cpp b/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.cpp index df8fac9..a451465 100644 --- a/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.cpp +++ b/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.cpp @@ -158,6 +158,7 @@ void EditorPlugin::setWidget(pdf::PDFWidget* widget) connect(clearAction, &QAction::triggered, &m_scene, &pdf::PDFPageContentScene::clear); connect(activateAction, &QAction::triggered, this, &EditorPlugin::onSetActive); connect(m_widget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::drawSpaceChanged, this, &EditorPlugin::onDrawSpaceChanged); + connect(m_widget, &pdf::PDFWidget::sceneActivityChanged, this, &EditorPlugin::onSceneActivityChanged); updateActions(); } @@ -426,6 +427,11 @@ bool EditorPlugin::save() return true; } +void EditorPlugin::onSceneActivityChanged() +{ + updateActions(); +} + void EditorPlugin::onSceneChanged(bool graphicsOnly) { if (!graphicsOnly) @@ -591,12 +597,19 @@ void EditorPlugin::setActive(bool active) m_actions[Activate]->setChecked(active); updateActions(); + + // If editor is not active, remove the widget + if (m_editorWidget && !active) + { + delete m_editorWidget; + m_editorWidget = nullptr; + } } } void EditorPlugin::onSetActive(bool active) { - if (!active && !save()) + if (m_scene.isActive() && !active && !save()) { updateActions(); m_actions[Activate]->setChecked(true); @@ -617,6 +630,7 @@ void EditorPlugin::updateActions() { if (action == m_actions[Activate]) { + action->setEnabled(m_widget && !m_widget->isAnySceneActive(&m_scene)); continue; } diff --git a/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.h b/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.h index d6c6fdb..4632458 100644 --- a/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.h +++ b/Pdf4QtEditorPlugins/EditorPlugin/editorplugin.h @@ -52,6 +52,7 @@ public: bool save(); private: + void onSceneActivityChanged(); void onSceneChanged(bool graphicsOnly); void onSceneSelectionChanged(); void onWidgetSelectionChanged(); diff --git a/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.cpp b/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.cpp index b37a9e5..ead6585 100644 --- a/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.cpp +++ b/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.cpp @@ -162,6 +162,7 @@ void SignaturePlugin::setWidget(pdf::PDFWidget* widget) connect(signElectronicallyAction, &QAction::triggered, this, &SignaturePlugin::onSignElectronically); connect(signDigitallyAction, &QAction::triggered, this, &SignaturePlugin::onSignDigitally); connect(certificatesAction, &QAction::triggered, this, &SignaturePlugin::onOpenCertificatesManager); + connect(m_widget, &pdf::PDFWidget::sceneActivityChanged, this, &SignaturePlugin::onSceneActivityChanged); updateActions(); } @@ -508,6 +509,11 @@ void SignaturePlugin::onOpenCertificatesManager() dialog.exec(); } +void SignaturePlugin::onSceneActivityChanged() +{ + updateActions(); +} + void SignaturePlugin::onPenChanged(const QPen& pen) { if (pdf::PDFCreatePCElementTool* activeTool = qobject_cast(getActiveTool())) @@ -577,6 +583,13 @@ void SignaturePlugin::setActive(bool active) m_actions[Activate]->setChecked(active); updateActions(); + + // If editor is not active, remove the widget + if (m_editorWidget && !active) + { + delete m_editorWidget; + m_editorWidget = nullptr; + } } } @@ -589,6 +602,11 @@ void SignaturePlugin::updateActions() // Inactive scene - disable all except activate action and certificates for (QAction* action : m_actions) { + if (action == m_actions[Activate]) + { + action->setEnabled(m_widget && !m_widget->isAnySceneActive(&m_scene)); + } + if (action == m_actions[Activate] || action == m_actions[Certificates]) { diff --git a/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.h b/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.h index 6afe41d..45167c3 100644 --- a/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.h +++ b/Pdf4QtEditorPlugins/SignaturePlugin/signatureplugin.h @@ -57,6 +57,7 @@ private: void onSignElectronically(); void onSignDigitally(); void onOpenCertificatesManager(); + void onSceneActivityChanged(); void onPenChanged(const QPen& pen); void onBrushChanged(const QBrush& brush); diff --git a/Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp b/Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp index bddd386..186f7dc 100644 --- a/Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp +++ b/Pdf4QtLibWidgets/sources/pdfdrawwidget.cpp @@ -23,6 +23,7 @@ #include "pdfwidgetannotation.h" #include "pdfwidgetformmanager.h" #include "pdfblpainter.h" +#include "pdfpagecontentelements.h" #include #include @@ -145,6 +146,11 @@ void PDFWidget::onPageImageChanged(bool all, const std::vector& page } } +void PDFWidget::onSceneActiveStateChanged(bool) +{ + Q_EMIT sceneActivityChanged(); +} + void PDFWidget::removeInputInterface(IDrawWidgetInputInterface* inputInterface) { auto it = std::find(m_inputInterfaces.begin(), m_inputInterfaces.end(), inputInterface); @@ -152,6 +158,17 @@ void PDFWidget::removeInputInterface(IDrawWidgetInputInterface* inputInterface) { m_inputInterfaces.erase(it); } + + PDFPageContentScene* scene = dynamic_cast(inputInterface); + if (scene) + { + auto itScene = std::find(m_scenes.begin(), m_scenes.end(), inputInterface); + if (itScene != m_scenes.end()) + { + m_scenes.erase(itScene); + disconnect(scene, &PDFPageContentScene::sceneActiveStateChanged, this, &PDFWidget::onSceneActiveStateChanged); + } + } } void PDFWidget::addInputInterface(IDrawWidgetInputInterface* inputInterface) @@ -160,9 +177,29 @@ void PDFWidget::addInputInterface(IDrawWidgetInputInterface* inputInterface) { m_inputInterfaces.push_back(inputInterface); std::sort(m_inputInterfaces.begin(), m_inputInterfaces.end(), IDrawWidgetInputInterface::Comparator()); + + PDFPageContentScene* scene = dynamic_cast(inputInterface); + if (scene) + { + m_scenes.push_back(scene); + connect(scene, &PDFPageContentScene::sceneActiveStateChanged, this, &PDFWidget::onSceneActiveStateChanged); + } } } +bool PDFWidget::isAnySceneActive(PDFPageContentScene* sceneToSkip) const +{ + for (PDFPageContentScene* scene : m_scenes) + { + if (scene->isActive() && scene != sceneToSkip) + { + return true; + } + } + + return false; +} + PDFWidgetFormManager* PDFWidget::getFormManager() const { return m_formManager; diff --git a/Pdf4QtLibWidgets/sources/pdfdrawwidget.h b/Pdf4QtLibWidgets/sources/pdfdrawwidget.h index da864bd..d27eacb 100644 --- a/Pdf4QtLibWidgets/sources/pdfdrawwidget.h +++ b/Pdf4QtLibWidgets/sources/pdfdrawwidget.h @@ -38,6 +38,7 @@ class PDFDrawWidgetProxy; class PDFModifiedDocument; class PDFWidgetAnnotationManager; class IDrawWidgetInputInterface; +class PDFPageContentScene; class IDrawWidget { @@ -105,12 +106,17 @@ public: void removeInputInterface(IDrawWidgetInputInterface* inputInterface); void addInputInterface(IDrawWidgetInputInterface* inputInterface); + /// Returns true, if any scene is active + bool isAnySceneActive(PDFPageContentScene* sceneToSkip) const; + signals: + void sceneActivityChanged(); void pageRenderingErrorsChanged(pdf::PDFInteger pageIndex, int errorsCount); private: void onRenderingError(PDFInteger pageIndex, const QList& errors); void onPageImageChanged(bool all, const std::vector& pages); + void onSceneActiveStateChanged(bool); const PDFCMSManager* m_cmsManager; PDFToolManager* m_toolManager; @@ -122,6 +128,7 @@ private: PDFDrawWidgetProxy* m_proxy; PageRenderingErrors m_pageRenderingErrors; std::vector m_inputInterfaces; + std::vector m_scenes; RendererEngine m_rendererEngine; }; diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp index 197229e..40c56c0 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp +++ b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp @@ -870,6 +870,7 @@ void PDFPageContentScene::setActive(bool newIsActive) } Q_EMIT sceneChanged(false); + Q_EMIT sceneActiveStateChanged(newIsActive); } } diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h index 532c432..281c91a 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h +++ b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.h @@ -643,6 +643,8 @@ signals: /// Request to edit the elements void editElementRequest(const std::set& elements); + void sceneActiveStateChanged(bool activated); + private: struct MouseEventInfo