6#ifdef GEMC_HAS_QTCHARTS
7#include "gAnalysisView.h"
11#include "G4UImanager.hh"
19void resetSceneBeforeGeometryReload(G4UImanager* g4uim) {
20 if (!g4uim) {
return; }
22 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
23 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
24 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
25 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
26 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
27 g4uim->ApplyCommand(
"/vis/viewer/clear");
28 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
29 g4uim->ApplyCommand(
"/vis/viewer/flush");
30 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
33void resetEventDrawingBeforeRun(G4UImanager* g4uim) {
34 if (!g4uim) {
return; }
36 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
37 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
38 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
39 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
40 g4uim->ApplyCommand(
"/vis/viewer/clear");
41 g4uim->ApplyCommand(
"/vis/viewer/flush");
42 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
45void restoreSceneModels(G4UImanager* g4uim,
const std::shared_ptr<GOptions>& gopts,
bool includeEventModels) {
46 if (!g4uim) {
return; }
51 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
52 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
53 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
54 g4uim->ApplyCommand(
"/vis/viewer/clear");
55 g4uim->ApplyCommand(
"/vis/drawVolume");
56 g4uim->ApplyCommand(
"/vis/viewer/set/background " + g4view.background);
57 g4uim->ApplyCommand(
"/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
58 for (
const auto& command : g4SceneProperties.addSceneDecorations(gopts)) { g4uim->ApplyCommand(command); }
59 for (
const auto& command : g4SceneProperties.addSceneTexts(gopts)) { g4uim->ApplyCommand(command); }
60 if (includeEventModels) {
62 g4uim->ApplyCommand(
"/vis/scene/add/trajectories smooth");
63 g4uim->ApplyCommand(
"/vis/modeling/trajectories/create/drawByCharge");
64 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true");
65 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2");
66 g4uim->ApplyCommand(
"/vis/scene/add/hits");
67 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 10000");
68 if (decorations.eventID) {
69 g4uim->ApplyCommand(
"/vis/scene/add/eventID " + std::to_string(decorations.eventIDSize));
72 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
73 g4uim->ApplyCommand(
"/vis/viewer/flush");
79 std::shared_ptr<EventDispenser> ed,
81 std::shared_ptr<GAnalysisAccumulator> analyzer,
82 bool viewerAlreadyInitialized,
86 analysisAccumulator(std::move(analyzer)),
88 detectorConstruction(dc),
89 viewerInitialized(viewerAlreadyInitialized) {
98 guiSession = std::make_unique<GUI_Session>(gopts,
gboard);
101 createRightContent(gopts, dc,
gboard);
104 auto* topLayout =
new QHBoxLayout;
105 createTopButtons(topLayout);
108 auto* bottomLayout =
new QHBoxLayout;
109 bottomLayout->setContentsMargins(0, 0, 0, 0);
110 bottomLayout->setSpacing(6);
112 bottomLayout->addWidget(leftButtons, 1);
113 bottomLayout->addWidget(rightContent, 10);
116 auto* mainLayout =
new QVBoxLayout;
117 mainLayout->setContentsMargins(6, 6, 6, 6);
118 mainLayout->setSpacing(6);
119 mainLayout->addLayout(topLayout);
120 mainLayout->addLayout(bottomLayout, 1);
121 mainLayout->addWidget(
gboard, 0);
123 setLayout(mainLayout);
124 setWindowTitle(tr(
"GEMC: Geant4 Monte-Carlo"));
125 setMinimumWidth(1000);
126 setMinimumHeight(760);
127 resize(1000, std::max(sizeHint().height(), 760));
128 QTimer::singleShot(0,
this, [
this]() {
130 const int preferredHeight = std::max(sizeHint().height(), 760);
131 setMinimumHeight(preferredHeight);
132 resize(std::max(1000, sizeHint().width()), preferredHeight);
136 gtimer =
new QTimer(
this);
137 connect(gtimer, SIGNAL(timeout()),
this, SLOT(cycleBeamOn()));
140 connect(leftButtons->buttonsWidget,
141 SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem*)),
142 this, SLOT(change_page(QListWidgetItem *, QListWidgetItem*)));
146void GemcGUI::updateGui() {
148 displayedEventCount += nEvents->text().toULongLong();
149 eventNumberLabel->setText(tr(
"Event Number: %1").arg(displayedEventCount));
153void GemcGUI::resetVisualizationBeforeGeometryReload() {
154#ifdef GEMC_HAS_QTCHARTS
155 if (analysisView !=
nullptr) { analysisView->clearForGeometryReload(); }
157 resetSceneBeforeGeometryReload(G4UImanager::GetUIpointer());
160void GemcGUI::refreshVisualizationFromOptions() {
161 if (!detectorConstruction || !detectorConstruction->has_built_geometry()) {
return; }
163 auto* g4uim = G4UImanager::GetUIpointer();
164 if (!g4uim) {
return; }
166 restoreSceneModels(g4uim, guiOptions,
false);
167 visualizationNeedsRunRestore =
true;
170void GemcGUI::refreshGeometryTree() {
171 if (!rightContent || !detectorConstruction || !guiOptions || !geometryTree) {
return; }
173 geometryReloadedSinceRun =
true;
175 const int treeIndex = rightContent->indexOf(geometryTree);
176 if (treeIndex < 0) {
return; }
178 const int currentIndex = rightContent->currentIndex();
179 auto* g4uim = G4UImanager::GetUIpointer();
180 if (!g4uim) {
return; }
182 const auto g4volumes = detectorConstruction->has_built_geometry()
183 ? detectorConstruction->get_g4volumes_map()
184 : std::unordered_map<std::string, G4Volume*>{};
185 const auto gvolumes = detectorConstruction->has_built_geometry()
186 ? detectorConstruction->get_gvolumes_flat_map()
187 : std::unordered_map<std::string, const GVolume*>{};
188 const auto gmirrors = detectorConstruction->has_built_geometry()
189 ? detectorConstruction->get_gmirrors_flat_map()
190 : std::unordered_map<std::string, const GMirror*>{};
191 auto* refreshedTree =
new GTree(guiOptions, g4volumes, gvolumes,
nullptr, gmirrors);
193 rightContent->removeWidget(geometryTree);
194 geometryTree->deleteLater();
195 geometryTree = refreshedTree;
196 rightContent->insertWidget(treeIndex, geometryTree);
197 rightContent->setCurrentIndex(currentIndex == treeIndex ? treeIndex : currentIndex);
199 if (g4volumes.size() > 1) {
200 if (!viewerInitialized) {
202 G4SceneProperties g4SceneProperties(guiOptions);
203 auto commands = g4SceneProperties.scene_commands(guiOptions);
204 for (
const auto& command : commands) { g4uim->ApplyCommand(command); }
205 viewerInitialized =
true;
208 restoreSceneModels(g4uim, guiOptions,
false);
212void GemcGUI::prepareGeometryForBeamOn() {
213 if ((!geometryReloadedSinceRun && !visualizationNeedsRunRestore) || !detectorConstruction) {
return; }
214 if (!detectorConstruction->has_built_geometry()) {
return; }
216 auto* g4uim = G4UImanager::GetUIpointer();
217 resetEventDrawingBeforeRun(g4uim);
219 if (geometryReloadedSinceRun) {
220 detectorConstruction->prepare_geometry_for_run();
221 if (eventDispenser) {
222 eventDispenser->resetRunContext();
229 restoreSceneModels(g4uim, guiOptions,
true);
231 geometryReloadedSinceRun =
false;
232 visualizationNeedsRunRestore =
false;
242void GemcGUI::change_page(QListWidgetItem* current, QListWidgetItem* previous) {
250 rightContent->setCurrentIndex(thisIndex);
251 if (rightContent->currentWidget() == physicsView) { physicsView->
refreshProcesses(); }
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.
~GemcGUI() override
Destroy the GUI widget and release explicitly owned resources.
G4View getG4View(const std::shared_ptr< GOptions > &gopts)
G4Decorations getG4Decorations(const std::shared_ptr< GOptions > &gopts)