Debian Codegraph @

src/Gui/PythonTracing.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 **************************************************************************/
23
24 #ifndef GUI_PYTHONTRACING_H
25 #define GUI_PYTHONTRACING_H
26
27 #include <QObject>
28 #include <Python.h>
29 #include <frameobject.h>
30 #include <memory>
31 #include <FCGlobal.h>
32
33
34 namespace Gui
35 {
36
37 class GuiExport PythonTracing
38 {
39 public:
40 PythonTracing();
41 ~PythonTracing();
42
43 PythonTracing(const PythonTracing&) = delete;
44 PythonTracing(PythonTracing&&) = delete;
45 PythonTracing& operator=(const PythonTracing&) = delete;
46 PythonTracing& operator=(PythonTracing&&) = delete;
47
48 /*!
49 * \brief isActive
50 * Returns true if the tracing is active, false otherwise.
51 * \return
52 */
53 bool isActive() const;
54 /*!
55 * \brief activate
56 * Activates the Python tracing.
57 */
58 void activate();
59 /*!
60 * \brief deactivate
61 * Deactivates the Python tracing.
62 */
63 void deactivate();
64 /*!
65 * \brief fetchFromSettings
66 * Fetch parameters from user settings.
67 */
68 void fetchFromSettings();
69 /*!
70 * \brief interrupt
71 * If the tracing is enabled it interrupts the Python interpreter.
72 * True is returned if the tracing is active, false otherwise.
73 */
74 bool interrupt() const;
75 /*!
76 * \brief setTimeout
77 * Sets the interval after which the Qt event loop will be processed.
78 */
79 void setTimeout(int ms);
80 /*!
81 * \brief timeout
82 * \return the timeout of processing the event loop.
83 */
84 int timeout() const;
85
86 private:
87 void setPythonTraceEnabled(bool enabled) const;
88 static int tracer_callback(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg);
89
90 private:
91 struct Private;
92 std::unique_ptr<Private> d;
93 };
94
95 class GuiExport PythonTracingLocker
96 {
97 public:
98 /*!
99 * \brief PythonTracingLocker
100 * Activates the passed tracing object.
101 * \param trace
102 */
103 explicit PythonTracingLocker(PythonTracing& trace);
104 /*!
105 * Deactivates the tracing object.
106 */
107 ~PythonTracingLocker();
108
109 PythonTracingLocker(const PythonTracingLocker&) = delete;
110 PythonTracingLocker(PythonTracingLocker&&) = delete;
111 PythonTracingLocker& operator=(const PythonTracingLocker&) = delete;
112 PythonTracingLocker& operator=(PythonTracingLocker&&) = delete;
113
114 private:
115 PythonTracing& trace;
116 };
117
118 class GuiExport PythonTracingWatcher: public QObject
119 {
120 // NOLINTNEXTLINE
121 Q_OBJECT
122
123 public:
124 PythonTracingWatcher(QObject* parent = nullptr);
125 ~PythonTracingWatcher() override;
126 /*!
127 * \brief eventFilter
128 * Checks for Ctrl+C keyboard events and if pressed interrupts the Python interpreter.
129 * \param object
130 * \param event
131 * \return
132 */
133 bool eventFilter(QObject* object, QEvent* event) override;
134
135 /*!
136 * \brief getTrace
137 * Returns the Python tracing object. It's up to the calling instance to activate and decativate
138 * it.
139 * \return PythonTracing
140 */
141 PythonTracing& getTrace();
142
143 PythonTracingWatcher(const PythonTracingWatcher&) = delete;
144 PythonTracingWatcher(PythonTracingWatcher&&) = delete;
145 PythonTracingWatcher& operator=(const PythonTracingWatcher&) = delete;
146 PythonTracingWatcher& operator=(PythonTracingWatcher&&) = delete;
147
148 private:
149 PythonTracing trace;
150 };
151
152 } // namespace Gui
153
154 #endif // GUI_PYTHONTRACING_H