From 1446c0780690e04124f31b5191514f251c998178 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 9 Sep 2023 14:02:57 +0200 Subject: [PATCH 1/2] Gui: fixes #10617: Ctrl+C only stop python running in console not from macros This commit handles the execution from the macro dialog --- src/Gui/DlgMacroExecuteImp.cpp | 5 +++++ src/Gui/DlgMacroExecuteImp.h | 2 ++ src/Gui/PythonTracing.cpp | 37 ++++++++++++++++++++++++++++++++++ src/Gui/PythonTracing.h | 34 +++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/src/Gui/DlgMacroExecuteImp.cpp b/src/Gui/DlgMacroExecuteImp.cpp index 640addab09..3c4abe69c5 100644 --- a/src/Gui/DlgMacroExecuteImp.cpp +++ b/src/Gui/DlgMacroExecuteImp.cpp @@ -44,6 +44,7 @@ #include "Macro.h" #include "MainWindow.h" #include "PythonEditor.h" +#include "WaitCursor.h" using namespace Gui; @@ -80,6 +81,7 @@ DlgMacroExecuteImp::DlgMacroExecuteImp( QWidget* parent, Qt::WindowFlags fl ) , WindowParameter( "Macro" ) , ui(new Ui_DlgMacroExecute) { + watcher = std::make_unique(this); ui->setupUi(this); setupConnections(); @@ -300,6 +302,9 @@ void DlgMacroExecuteImp::accept() QFileInfo fi(dir, item->text(0)); try { + WaitCursor wc; + PythonTracingLocker tracelock(watcher->getTrace()); + getMainWindow()->appendRecentMacro(fi.filePath()); Application::Instance->macroManager()->run(Gui::MacroManager::File, fi.filePath().toUtf8()); // after macro run recalculate the document diff --git a/src/Gui/DlgMacroExecuteImp.h b/src/Gui/DlgMacroExecuteImp.h index e6dbb800fe..88fe36ceb7 100644 --- a/src/Gui/DlgMacroExecuteImp.h +++ b/src/Gui/DlgMacroExecuteImp.h @@ -26,6 +26,7 @@ #include #include +#include "PythonTracing.h" #include "Window.h" class QTreeWidgetItem; @@ -71,6 +72,7 @@ protected: QString macroPath; private: + std::unique_ptr watcher; std::unique_ptr ui; }; diff --git a/src/Gui/PythonTracing.cpp b/src/Gui/PythonTracing.cpp index d128f1d308..e6063cdacf 100644 --- a/src/Gui/PythonTracing.cpp +++ b/src/Gui/PythonTracing.cpp @@ -23,12 +23,15 @@ #include "PreCompiled.h" #ifndef _PreComp_ +#include +#include #include #include #endif #include "PythonTracing.h" #include +#include using namespace Gui; @@ -113,6 +116,7 @@ void PythonTracing::setPythonTraceEnabled(bool enabled) const Private::profilerDisabled = true; } + Base::PyGILStateLocker lock; PyEval_SetTrace(trace, nullptr); } @@ -159,3 +163,36 @@ PythonTracingLocker::~PythonTracingLocker() { trace.deactivate(); } + +// ------------------------------------------------------------------------------------------------ + +PythonTracingWatcher::PythonTracingWatcher(QObject* parent) + : QObject(parent) +{ + qApp->installEventFilter(this); +} + +PythonTracingWatcher::~PythonTracingWatcher() +{ + qApp->removeEventFilter(this); +} + +bool PythonTracingWatcher::eventFilter(QObject* object, QEvent* event) +{ + if (event && event->type() == QEvent::ShortcutOverride) { + auto kevent = static_cast(event); + if (kevent->key() == Qt::Key_C && kevent->modifiers() == Qt::ControlModifier) { + if (trace.interrupt()) { + return true; + } + } + } + + return QObject::eventFilter(object, event); +} + +PythonTracing& PythonTracingWatcher::getTrace() +{ + trace.fetchFromSettings(); + return trace; +} diff --git a/src/Gui/PythonTracing.h b/src/Gui/PythonTracing.h index 8f09cef301..0939d2affd 100644 --- a/src/Gui/PythonTracing.h +++ b/src/Gui/PythonTracing.h @@ -24,6 +24,7 @@ #ifndef GUI_PYTHONTRACING_H #define GUI_PYTHONTRACING_H +#include #include #include #include @@ -113,6 +114,39 @@ private: PythonTracing& trace; }; +class GuiExport PythonTracingWatcher : public QObject +{ + // NOLINTNEXTLINE + Q_OBJECT + +public: + PythonTracingWatcher(QObject* parent = nullptr); + ~PythonTracingWatcher() override; + /*! + * \brief eventFilter + * Checks for Ctrl+C keyboard events and if pressed interrupts the Python interpreter. + * \param object + * \param event + * \return + */ + bool eventFilter(QObject* object, QEvent* event) override; + + /*! + * \brief getTrace + * Returns the Python tracing object. It's up to the calling instance to activate and decativate it. + * \return PythonTracing + */ + PythonTracing& getTrace(); + + PythonTracingWatcher(const PythonTracingWatcher&) = delete; + PythonTracingWatcher(PythonTracingWatcher&&) = delete; + PythonTracingWatcher& operator = (const PythonTracingWatcher&) = delete; + PythonTracingWatcher& operator = (PythonTracingWatcher&&) = delete; + +private: + PythonTracing trace; +}; + } // namespace Gui #endif // GUI_PYTHONTRACING_H From 2d2b2b899015461f72723346c152ad997e2131f2 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 9 Sep 2023 14:19:00 +0200 Subject: [PATCH 2/2] Gui: fixes #10617: Ctrl+C only stop python running in console not from macros This commit handles the execution from the Python editor --- src/Gui/EditorView.cpp | 10 +++++++++- src/Gui/EditorView.h | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Gui/EditorView.cpp b/src/Gui/EditorView.cpp index 3fc09cf801..cf031d0f61 100644 --- a/src/Gui/EditorView.cpp +++ b/src/Gui/EditorView.cpp @@ -50,6 +50,8 @@ #include "Macro.h" #include "MainWindow.h" #include "PythonEditor.h" +#include "PythonTracing.h" +#include "WaitCursor.h" #include #include @@ -596,9 +598,13 @@ PythonEditorView::PythonEditorView(PythonEditor* editor, QWidget* parent) { connect(this, &PythonEditorView::changeFileName, editor, &PythonEditor::setFileName); + watcher = new PythonTracingWatcher(this); } -PythonEditorView::~PythonEditorView() = default; +PythonEditorView::~PythonEditorView() +{ + delete watcher; +} /** * Runs the action specified by \a pMsg. @@ -644,6 +650,8 @@ void PythonEditorView::executeScript() if (EditorView::onHasMsg("Save")) EditorView::onMsg("Save", nullptr); try { + WaitCursor wc; + PythonTracingLocker tracelock(watcher->getTrace()); Application::Instance->macroManager()->run(Gui::MacroManager::File,fileName().toUtf8()); } catch (const Base::SystemExitException&) { diff --git a/src/Gui/EditorView.h b/src/Gui/EditorView.h index 3a09321fb0..8546a94397 100644 --- a/src/Gui/EditorView.h +++ b/src/Gui/EditorView.h @@ -41,6 +41,7 @@ namespace Gui { class EditorViewP; class TextEdit; +class PythonTracingWatcher; /** * A special view class which sends the messages from the application to @@ -140,6 +141,7 @@ public Q_SLOTS: private: PythonEditor* _pye; + PythonTracingWatcher* watcher; }; class SearchBar : public QWidget