pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
qml_bridge.hpp
Go to the documentation of this file.
1#ifndef QML_BRIDGE_HPP
2#define QML_BRIDGE_HPP
3
4/**
5 * \file qml_bridge.hpp
6 * \brief Bridge class to expose C++ functionality to QML.
7 * \author Le Bars, Yoann
8 * \ingroup UIWindows
9 *
10 * This file is part of the pure C++ benchmark.
11 *
12 * This program is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation, either version 3 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26#include <QObject>
27#include <QPointer>
28#include <QString>
29#include <QWidget>
30#include <memory>
31#include <optional>
32
33#include "config.hpp"
34#include "display.hpp"
35
36namespace Window {
37 /**
38 * \brief Bridge class to expose C++ functionality to QML.
39 *
40 * This class acts as an interface between QML and the C++ backend,
41 * exposing the Display widget and application actions (About, Quit).
42 * \ingroup UIWindows
43 */
44 class QmlBridge : public QObject {
45 Q_OBJECT
46 Q_PROPERTY(QWidget* displayContainer READ displayContainer NOTIFY
48 Q_PROPERTY(QString aboutText READ aboutText NOTIFY aboutTextChanged)
49
50 public:
51 /**
52 * \brief Constructs the QML bridge.
53 * \param config The simulation configuration object.
54 * \param parent The parent QObject.
55 */
56 explicit QmlBridge(const Configuration::SimulationConfig& config,
57 QObject* parent = nullptr);
58
59 /**
60 * \brief Destructor.
61 */
62 ~QmlBridge() override;
63
64 /**
65 * \brief Gets the widget container for the 3D display.
66 * \return A pointer to the widget container, or nullptr if not created.
67 */
68 QWidget* displayContainer() const;
69
70 /**
71 * \brief Gets the "About" page text.
72 * \return The about text as a QString.
73 */
74 QString aboutText() const;
75
76 /**
77 * \brief Starts the simulation after the window is shown.
78 */
79 Q_INVOKABLE void startSimulation();
80
81 /**
82 * \brief Stops the physics thread and prints profiling reports.
83 *
84 * UI teardown is handled separately by releaseDisplayDeferred() or
85 * releaseDisplayStrict(), depending on --cleanup-mode.
86 */
87 Q_INVOKABLE void shutdownSimulation();
88
89 /**
90 * \brief Schedules deferred destruction of Display and its container.
91 *
92 * Used in fast cleanup mode (benchmark-friendly, similar to async
93 * Python GC). Display is destroyed first; the container follows via a
94 * queued connection.
95 */
96 void releaseDisplayDeferred(QWidget* container_for_deferred_delete);
97
98 /**
99 * \brief Synchronously destroys the display container and its window.
100 *
101 * Used in strict cleanup mode (Valgrind and conservative teardown).
102 */
103 void releaseDisplayStrict(QWidget* container);
104
105 public slots:
106 /**
107 * \brief Slot to display the "About" dialogue.
108 */
109 void showAbout();
110
111 /**
112 * \brief Slot to display the "About Qt" dialogue.
113 */
114 void showAboutQt();
115
116 /**
117 * \brief Slot to quit the application.
118 */
119 void quit();
120
121 signals:
122 /**
123 * \brief Emitted when the display container is created.
124 */
126
127 /**
128 * \brief Emitted when the about text changes.
129 */
131
132 /**
133 * \brief Emitted when the simulation finishes.
134 */
136
137 private:
138 /**
139 * \brief Loads the "About" page content from resources.
140 * \return The content of the about page as a QString, or
141 * `std::nullopt` if loading fails.
142 */
143 std::optional<QString> loadAboutText();
144
145 /// \brief The 3D display view for the simulation.
146 QPointer<Display> display_;
147
148 /// \brief The widget container for the 3D display.
149 QPointer<QWidget> container_;
150
151 /// \brief The cached about text.
152 QString aboutText_;
153
154 /// \brief True after UI resources have been released.
155 bool uiReleased_ = false;
156 };
157} // namespace Window
158
159#endif // QML_BRIDGE_HPP
Bridge class to expose C++ functionality to QML.
Definition: qml_bridge.hpp:44
void releaseDisplayStrict(QWidget *container)
Synchronously destroys the display container and its window.
Definition: qml_bridge.cpp:116
QmlBridge(const Configuration::SimulationConfig &config, QObject *parent=nullptr)
Constructs the QML bridge.
Definition: qml_bridge.cpp:33
Q_INVOKABLE void startSimulation()
Starts the simulation after the window is shown.
Definition: qml_bridge.cpp:78
std::optional< QString > loadAboutText()
Loads the "About" page content from resources.
Definition: qml_bridge.cpp:139
QString aboutText_
The cached about text.
Definition: qml_bridge.hpp:152
void aboutTextChanged()
Emitted when the about text changes.
QPointer< Display > display_
The 3D display view for the simulation.
Definition: qml_bridge.hpp:146
void displayContainerChanged()
Emitted when the display container is created.
bool uiReleased_
True after UI resources have been released.
Definition: qml_bridge.hpp:155
Q_INVOKABLE void shutdownSimulation()
Stops the physics thread and prints profiling reports.
Definition: qml_bridge.cpp:84
void showAboutQt()
Slot to display the "About Qt" dialogue.
Definition: qml_bridge.cpp:135
void releaseDisplayDeferred(QWidget *container_for_deferred_delete)
Schedules deferred destruction of Display and its container.
Definition: qml_bridge.cpp:90
~QmlBridge() override
Destructor.
Definition: qml_bridge.cpp:67
QPointer< QWidget > container_
The widget container for the 3D display.
Definition: qml_bridge.hpp:149
void quit()
Slot to quit the application.
Definition: qml_bridge.cpp:137
void simulationFinished()
Emitted when the simulation finishes.
void showAbout()
Slot to display the "About" dialogue.
Definition: qml_bridge.cpp:129
Displaying spherical moving bodies.