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 displayContainerChanged)
47 Q_PROPERTY(QString aboutText READ aboutText NOTIFY aboutTextChanged)
48
49 public:
50 /**
51 * \brief Constructs the QML bridge.
52 * \param config The simulation configuration object.
53 * \param parent The parent QObject.
54 */
55 explicit QmlBridge(const Configuration::SimulationConfig& config,
56 QObject* parent = nullptr);
57
58 /**
59 * \brief Destructor.
60 */
61 ~QmlBridge() override;
62
63 /**
64 * \brief Gets the widget container for the 3D display.
65 * \return A pointer to the widget container, or nullptr if not created.
66 */
67 QWidget* displayContainer() const;
68
69 /**
70 * \brief Gets the "About" page text.
71 * \return The about text as a QString.
72 */
73 QString aboutText() const;
74
75 /**
76 * \brief Starts the simulation after the window is shown.
77 */
78 Q_INVOKABLE void startSimulation();
79
80 /**
81 * \brief Triggers cleanup of child components (stops threads, prints reports).
82 */
83 Q_INVOKABLE void cleanup();
84
85 /**
86 * \brief Releases the Display (destroys it manually).
87 * This must be called before the container is destroyed to avoid
88 * segfaults when the container tries to access the Display during destruction.
89 */
90 void releaseDisplay();
91
92 /**
93 * \brief Releases the Display using deferred destruction.
94 *
95 * This schedules destruction with deleteLater() to avoid blocking
96 * the caller during heavy Qt3D teardown. If a container is provided,
97 * its deletion is chained to Display destruction, guaranteeing that
98 * Display is destroyed first.
99 */
100 void releaseDisplayDeferred(QWidget* container_for_deferred_delete);
101
102 public slots:
103 /**
104 * \brief Slot to display the "About" dialogue.
105 */
106 void showAbout();
107
108 /**
109 * \brief Slot to display the "About Qt" dialogue.
110 */
111 void showAboutQt();
112
113 /**
114 * \brief Slot to quit the application.
115 */
116 void quit();
117
118 signals:
119 /**
120 * \brief Emitted when the display container is created.
121 */
123
124 /**
125 * \brief Emitted when the about text changes.
126 */
128
129 /**
130 * \brief Emitted when the simulation finishes.
131 */
133
134 private:
135 /**
136 * \brief Loads the "About" page content from resources.
137 * \return The content of the about page as a QString, or
138 * `std::nullopt` if loading fails.
139 */
140 std::optional<QString> loadAboutText();
141
142 /// \brief The 3D display view for the simulation.
143 std::unique_ptr<Display> display_;
144
145 /// \brief The widget container for the 3D display.
146 QWidget* container_;
147
148 /// \brief The cached about text.
149 QString aboutText_;
150 };
151} // namespace Window
152
153#endif // QML_BRIDGE_HPP
154
Bridge class to expose C++ functionality to QML.
Definition: qml_bridge.hpp:44
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:77
std::optional< QString > loadAboutText()
Loads the "About" page content from resources.
Definition: qml_bridge.cpp:131
QString aboutText_
The cached about text.
Definition: qml_bridge.hpp:149
QWidget * container_
The widget container for the 3D display.
Definition: qml_bridge.hpp:146
void aboutTextChanged()
Emitted when the about text changes.
std::unique_ptr< Display > display_
The 3D display view for the simulation.
Definition: qml_bridge.hpp:143
void displayContainerChanged()
Emitted when the display container is created.
Q_INVOKABLE void cleanup()
Triggers cleanup of child components (stops threads, prints reports).
Definition: qml_bridge.cpp:83
void showAboutQt()
Slot to display the "About Qt" dialogue.
Definition: qml_bridge.cpp:125
void releaseDisplayDeferred(QWidget *container_for_deferred_delete)
Releases the Display using deferred destruction.
Definition: qml_bridge.cpp:100
~QmlBridge() override
Destructor.
Definition: qml_bridge.cpp:66
void quit()
Slot to quit the application.
Definition: qml_bridge.cpp:129
void simulationFinished()
Emitted when the simulation finishes.
void showAbout()
Slot to display the "About" dialogue.
Definition: qml_bridge.cpp:119
void releaseDisplay()
Releases the Display (destroys it manually). This must be called before the container is destroyed to...
Definition: qml_bridge.cpp:91
Displaying spherical moving bodies.