gui
Loading...
Searching...
No Matches
gui.cc
Go to the documentation of this file.
1// gui
2#include "gui.h"
3#include "gtree.h"
4#include "g4SceneProperties.h"
5#include "gAnalysisView.h"
6
7// geant4
8#include "G4UImanager.hh"
9
10// c++
11#include <algorithm>
12#include <utility>
13
14
15namespace {
16void resetSceneBeforeGeometryReload(G4UImanager* g4uim) {
17 if (!g4uim) { return; }
18
19 g4uim->ApplyCommand("/vis/viewer/set/autoRefresh false");
20 g4uim->ApplyCommand("/vis/scene/endOfEventAction accumulate 0");
21 g4uim->ApplyCommand("/vis/scene/endOfRunAction refresh");
22 g4uim->ApplyCommand("/vis/scene/removeModel all");
23 g4uim->ApplyCommand("/vis/viewer/clearTransients");
24 g4uim->ApplyCommand("/vis/viewer/clear");
25 g4uim->ApplyCommand("/vis/viewer/set/autoRefresh true");
26 g4uim->ApplyCommand("/vis/viewer/flush");
27 g4uim->ApplyCommand("/vis/viewer/clearTransients");
28}
29
30void resetEventDrawingBeforeRun(G4UImanager* g4uim) {
31 if (!g4uim) { return; }
32
33 g4uim->ApplyCommand("/vis/viewer/set/autoRefresh false");
34 g4uim->ApplyCommand("/vis/scene/endOfEventAction accumulate 0");
35 g4uim->ApplyCommand("/vis/scene/endOfRunAction refresh");
36 g4uim->ApplyCommand("/vis/viewer/clearTransients");
37 g4uim->ApplyCommand("/vis/viewer/clear");
38 g4uim->ApplyCommand("/vis/viewer/flush");
39 g4uim->ApplyCommand("/vis/viewer/clearTransients");
40}
41
42void restoreSceneModels(G4UImanager* g4uim, const std::shared_ptr<GOptions>& gopts, bool includeEventModels) {
43 if (!g4uim) { return; }
44
45 const auto g4view = g4display::getG4View(gopts);
46 G4SceneProperties g4SceneProperties(gopts);
47
48 g4uim->ApplyCommand("/vis/viewer/set/autoRefresh false");
49 g4uim->ApplyCommand("/vis/scene/removeModel all");
50 g4uim->ApplyCommand("/vis/viewer/clearTransients");
51 g4uim->ApplyCommand("/vis/viewer/clear");
52 g4uim->ApplyCommand("/vis/drawVolume");
53 g4uim->ApplyCommand("/vis/viewer/set/background " + g4view.background);
54 g4uim->ApplyCommand("/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
55 for (const auto& command : g4SceneProperties.addSceneDecorations(gopts)) { g4uim->ApplyCommand(command); }
56 for (const auto& command : g4SceneProperties.addSceneTexts(gopts)) { g4uim->ApplyCommand(command); }
57 if (includeEventModels) {
58 const auto decorations = g4display::getG4Decorations(gopts);
59 g4uim->ApplyCommand("/vis/scene/add/trajectories smooth");
60 g4uim->ApplyCommand("/vis/modeling/trajectories/create/drawByCharge");
61 g4uim->ApplyCommand("/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true");
62 g4uim->ApplyCommand("/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2");
63 g4uim->ApplyCommand("/vis/scene/add/hits");
64 g4uim->ApplyCommand("/vis/scene/endOfEventAction accumulate 10000");
65 if (decorations.eventID) {
66 g4uim->ApplyCommand("/vis/scene/add/eventID " + std::to_string(decorations.eventIDSize));
67 }
68 }
69 g4uim->ApplyCommand("/vis/viewer/set/autoRefresh true");
70 g4uim->ApplyCommand("/vis/viewer/flush");
71}
72}
73
74
75GemcGUI::GemcGUI(std::shared_ptr<GOptions> gopts,
76 std::shared_ptr<EventDispenser> ed,
78 std::shared_ptr<GAnalysisAccumulator> analyzer,
79 bool viewerAlreadyInitialized,
80 QWidget* parent) :
81 QWidget(parent),
83 analysisAccumulator(std::move(analyzer)),
84 guiOptions(gopts),
85 detectorConstruction(dc),
86 viewerInitialized(viewerAlreadyInitialized) {
87
88 // Create the left navigation pane first; right content initialization uses it to sync selection state.
89 createLeftButtons(); // instantiates leftButtons
90
91 // The board is a long-lived widget attached to this GUI; it is used by other pages (e.g. dialog).
92 auto* gboard = new GBoard(gopts, this);
93
94 // Session object ties together options and the board; it is kept alive for the GUI lifetime.
95 guiSession = std::make_unique<GUI_Session>(gopts, gboard);
96
97 // Create the right stacked content pages (display, dialog, setup, tree) and synchronize default selection.
98 createRightContent(gopts, dc, gboard); // instantiates rightContent: g4control, g4dialog, etc
99
100 // Top row control buttons (run controls and counters).
101 auto* topLayout = new QHBoxLayout;
102 createTopButtons(topLayout);
103
104 // Bottom row contains left navigation and right content; stretch factor favors the content area.
105 auto* bottomLayout = new QHBoxLayout;
106 bottomLayout->setContentsMargins(0, 0, 0, 0);
107 bottomLayout->setSpacing(6);
108 // second argument is stretch factor. Right content can have 10 times more space.
109 bottomLayout->addWidget(leftButtons, 1);
110 bottomLayout->addWidget(rightContent, 10);
111
112 // Main layout: top controls, bottom panes, and board.
113 auto* mainLayout = new QVBoxLayout;
114 mainLayout->setContentsMargins(6, 6, 6, 6);
115 mainLayout->setSpacing(6);
116 mainLayout->addLayout(topLayout);
117 mainLayout->addLayout(bottomLayout, 1);
118 mainLayout->addWidget(gboard, 0);
119
120 setLayout(mainLayout);
121 setWindowTitle(tr("GEMC: Geant4 Monte-Carlo"));
122 setFixedWidth(1000);
123 setMinimumHeight(760);
124 resize(1000, std::max(sizeHint().height(), 760));
125 QTimer::singleShot(0, this, [this]() {
126 adjustSize();
127 const int preferredHeight = std::max(sizeHint().height(), 760);
128 setMinimumHeight(preferredHeight);
129 resize(1000, preferredHeight);
130 });
131
132 // Timer used for cycle mode; timeouts are connected to cycleBeamOn().
133 gtimer = new QTimer(this);
134 connect(gtimer, SIGNAL(timeout()), this, SLOT(cycleBeamOn()));
135
136 // Page switching: left button selection drives the stacked widget page index.
137 connect(leftButtons->buttonsWidget,
138 SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem*)),
139 this, SLOT(change_page(QListWidgetItem *, QListWidgetItem*)));
140}
141
142
143void GemcGUI::updateGui() {
144 // Parse the current label text and increment by the amount run in the most recent batch.
145 std::vector<std::string> sBefore =
146 gutilities::getStringVectorFromString(eventNumberLabel->text().toStdString());
147
148 int nThatWasRun = nEvents->text().toInt();
149 int nBefore = stoi(sBefore[2]);
150
151 QString newNEvents("Event Number: ");
152 newNEvents.append(std::to_string(nBefore + nThatWasRun).c_str());
153
154 eventNumberLabel->setText(newNEvents);
155}
156
157
158void GemcGUI::resetVisualizationBeforeGeometryReload() {
159 if (analysisView != nullptr) { analysisView->clearForGeometryReload(); }
160 resetSceneBeforeGeometryReload(G4UImanager::GetUIpointer());
161}
162
163void GemcGUI::refreshVisualizationFromOptions() {
164 if (!detectorConstruction || !detectorConstruction->has_built_geometry()) { return; }
165
166 auto* g4uim = G4UImanager::GetUIpointer();
167 if (!g4uim) { return; }
168
169 restoreSceneModels(g4uim, guiOptions, false);
170 visualizationNeedsRunRestore = true;
171}
172
173void GemcGUI::refreshGeometryTree() {
174 if (!rightContent || !detectorConstruction || !guiOptions || !geometryTree) { return; }
175
176 geometryReloadedSinceRun = true;
177
178 const int treeIndex = rightContent->indexOf(geometryTree);
179 if (treeIndex < 0) { return; }
180
181 const int currentIndex = rightContent->currentIndex();
182 auto* g4uim = G4UImanager::GetUIpointer();
183 if (!g4uim) { return; }
184
185 const auto g4volumes = detectorConstruction->has_built_geometry()
186 ? detectorConstruction->get_g4volumes_map()
187 : std::unordered_map<std::string, G4Volume*>{};
188 const auto gvolumes = detectorConstruction->has_built_geometry()
189 ? detectorConstruction->get_gvolumes_flat_map()
190 : std::unordered_map<std::string, const GVolume*>{};
191 auto* refreshedTree = new GTree(guiOptions, g4volumes, gvolumes);
192
193 rightContent->removeWidget(geometryTree);
194 geometryTree->deleteLater();
195 geometryTree = refreshedTree;
196 rightContent->insertWidget(treeIndex, geometryTree);
197 rightContent->setCurrentIndex(currentIndex == treeIndex ? treeIndex : currentIndex);
198
199 if (g4volumes.size() > 1) {
200 if (!viewerInitialized) {
201 // First geometry load: no viewer exists yet; create the named scene and open one.
202 G4SceneProperties g4SceneProperties(guiOptions);
203 auto commands = g4SceneProperties.scene_commands(guiOptions);
204 for (const auto& command : commands) { g4uim->ApplyCommand(command); }
205 viewerInitialized = true;
206 }
207
208 restoreSceneModels(g4uim, guiOptions, false);
209 }
210}
211
212void GemcGUI::prepareGeometryForBeamOn() {
213 if ((!geometryReloadedSinceRun && !visualizationNeedsRunRestore) || !detectorConstruction) { return; }
214 if (!detectorConstruction->has_built_geometry()) { return; }
215
216 auto* g4uim = G4UImanager::GetUIpointer();
217 resetEventDrawingBeforeRun(g4uim);
218
219 if (geometryReloadedSinceRun) {
220 detectorConstruction->prepare_geometry_for_run();
221 if (eventDispenser) {
222 eventDispenser->resetRunContext();
223 }
224 }
225
226 // Restore visualization models after geometry reinitialization.
227 // The run reinitialization can replace the world volume, so restore the same
228 // persistent models used by refreshGeometryTree() before BeamOn draws events.
229 restoreSceneModels(g4uim, guiOptions, true);
230
231 geometryReloadedSinceRun = false;
232 visualizationNeedsRunRestore = false;
233}
234
235
237 // All widgets are owned via Qt parent/child relationships once placed into the layout hierarchy.
238 // Rely on Qt to destroy children when GemcGUI is destroyed.
239}
240
241
242void GemcGUI::change_page(QListWidgetItem* current, QListWidgetItem* previous) {
243
244 // Qt may emit selection changes with a null current item; fall back to the previous selection.
245 if (!current)
246 current = previous;
247
248 // The left button widget provides the active page index; apply it to the stacked widget.
249 int thisIndex = leftButtons->button_pressed();
250 rightContent->setCurrentIndex(thisIndex);
251}
int button_pressed() const
GemcGUI(std::shared_ptr< GOptions > gopts, std::shared_ptr< EventDispenser > ed, GDetectorConstruction *dc, std::shared_ptr< GAnalysisAccumulator > analysisAccumulator, bool viewerAlreadyInitialized=false, QWidget *parent=nullptr)
Construct the main GUI widget.
Definition gui.cc:75
~GemcGUI() override
Destroy the GUI widget and release explicitly owned resources.
Definition gui.cc:236
G4View getG4View(const std::shared_ptr< GOptions > &gopts)
G4Decorations getG4Decorations(const std::shared_ptr< GOptions > &gopts)
vector< std::string > getStringVectorFromString(const std::string &input)