gboard
Loading...
Searching...
No Matches
gboard_example.cc
Go to the documentation of this file.
1// geant4
2#include "G4VisExecutive.hh"
3#include "G4RunManagerFactory.hh"
4
5// gboard
6#include "gboard.h"
7#include "gui_session.h"
8
9// qt
10#include <QApplication>
11#include <QMainWindow>
12#include <QTimer>
13
47int main(int argc, char* argv[]) {
48 // Initialize options and logging.
49 // The options structure is shared across module components via shared_ptr.
50 auto gopts = std::make_shared<GOptions>(argc, argv, gboard::defineOptions());
51 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, GBOARD_LOGGER);
52 auto gui = gopts->getSwitch("gui");
53 auto timeout = gopts->getScalarDouble("tt");
54 int ret = EXIT_SUCCESS;
55
56 log->info(0, "Starting gboard example...");
57
58 // Optional GUI setup (only if --gui is passed).
59 // Note: We allocate Qt objects dynamically here because the example explicitly deletes them.
60 QApplication* app = nullptr;
61 QMainWindow* window = nullptr;
62
63 if (gui) {
64 log->info(0, "gboard", "Running in GUI mode...");
65 app = new QApplication(argc, argv);
66 window = new QMainWindow();
67 window->setWindowTitle(QString::fromUtf8("displayUI example"));
68 }
69
70 // Initialize Geant4 visualization manager.
71 // This is included to resemble the typical environment where Geant4 produces UI output.
72 auto visManager = new G4VisExecutive;
73 visManager->Initialize();
74
75 // If GUI, show the window and run Qt loop.
76 if (gui) {
77 // GBoard is parented to the window, but the example explicitly deletes it at the end as well.
78 auto* gboard = new GBoard(gopts, window);
79
80 // GUI_Session installs itself as Geant4 cout destination and forwards to the board.
81 // The session does not own the board.
82 auto gui_session = std::make_unique<GUI_Session>(gopts, gboard);
83
84 window->setCentralWidget(gboard);
85 window->show();
86
87 // Quit after timeout: this demonstrates deterministic shutdown for automated runs.
88 QTimer::singleShot(timeout, [] {
89 QCoreApplication::quit(); // stop the event loop
90 });
91
92 ret = QApplication::exec();
93
94 delete gboard;
95 delete window;
96 delete app;
97 }
98 else {
99 // CLI mode: no Qt objects are created.
100 log->info(0, "Running gboard in command line mode...");
101 }
102
103 delete visManager;
104
105 return ret;
106}
A Qt widget that displays read-only log text with a compact "top bar" UI.
Definition gboard.h:56
constexpr const char * GBOARD_LOGGER
Definition gboard.h:10
#define SFUNCTION_NAME
Helper namespace for the gboard module.
GOptions defineOptions()
Defines the module options used by the gboard components.
Definition gboard.h:28
int main(int argc, char *argv[])