5#ifdef GEMC_HAS_QTCHARTS
6#include "gAnalysisView.h"
10#include "G4UImanager.hh"
18void resetSceneBeforeGeometryReload(G4UImanager* g4uim) {
19 if (!g4uim) {
return; }
21 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
22 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
23 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
24 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
25 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
26 g4uim->ApplyCommand(
"/vis/viewer/clear");
27 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
28 g4uim->ApplyCommand(
"/vis/viewer/flush");
29 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
32void resetEventDrawingBeforeRun(G4UImanager* g4uim) {
33 if (!g4uim) {
return; }
35 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
36 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
37 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
38 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
39 g4uim->ApplyCommand(
"/vis/viewer/clear");
40 g4uim->ApplyCommand(
"/vis/viewer/flush");
41 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
44void restoreSceneModels(G4UImanager* g4uim,
const std::shared_ptr<GOptions>& gopts,
bool includeEventModels) {
45 if (!g4uim) {
return; }
50 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
51 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
52 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
53 g4uim->ApplyCommand(
"/vis/viewer/clear");
54 g4uim->ApplyCommand(
"/vis/drawVolume");
55 g4uim->ApplyCommand(
"/vis/viewer/set/background " + g4view.background);
56 g4uim->ApplyCommand(
"/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
57 for (
const auto& command : g4SceneProperties.addSceneDecorations(gopts)) { g4uim->ApplyCommand(command); }
58 for (
const auto& command : g4SceneProperties.addSceneTexts(gopts)) { g4uim->ApplyCommand(command); }
59 if (includeEventModels) {
61 g4uim->ApplyCommand(
"/vis/scene/add/trajectories smooth");
62 g4uim->ApplyCommand(
"/vis/modeling/trajectories/create/drawByCharge");
63 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true");
64 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2");
65 g4uim->ApplyCommand(
"/vis/scene/add/hits");
66 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 10000");
67 if (decorations.eventID) {
68 g4uim->ApplyCommand(
"/vis/scene/add/eventID " + std::to_string(decorations.eventIDSize));
71 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
72 g4uim->ApplyCommand(
"/vis/viewer/flush");
78 std::shared_ptr<EventDispenser> ed,
80 std::shared_ptr<GAnalysisAccumulator> analyzer,
81 bool viewerAlreadyInitialized,
85 analysisAccumulator(std::move(analyzer)),
87 detectorConstruction(dc),
88 viewerInitialized(viewerAlreadyInitialized) {
97 guiSession = std::make_unique<GUI_Session>(gopts,
gboard);
100 createRightContent(gopts, dc,
gboard);
103 auto* topLayout =
new QHBoxLayout;
104 createTopButtons(topLayout);
107 auto* bottomLayout =
new QHBoxLayout;
108 bottomLayout->setContentsMargins(0, 0, 0, 0);
109 bottomLayout->setSpacing(6);
111 bottomLayout->addWidget(leftButtons, 1);
112 bottomLayout->addWidget(rightContent, 10);
115 auto* mainLayout =
new QVBoxLayout;
116 mainLayout->setContentsMargins(6, 6, 6, 6);
117 mainLayout->setSpacing(6);
118 mainLayout->addLayout(topLayout);
119 mainLayout->addLayout(bottomLayout, 1);
120 mainLayout->addWidget(
gboard, 0);
122 setLayout(mainLayout);
123 setWindowTitle(tr(
"GEMC: Geant4 Monte-Carlo"));
125 setMinimumHeight(760);
126 resize(1000, std::max(sizeHint().height(), 760));
127 QTimer::singleShot(0,
this, [
this]() {
129 const int preferredHeight = std::max(sizeHint().height(), 760);
130 setMinimumHeight(preferredHeight);
131 resize(1000, preferredHeight);
135 gtimer =
new QTimer(
this);
136 connect(gtimer, SIGNAL(timeout()),
this, SLOT(cycleBeamOn()));
139 connect(leftButtons->buttonsWidget,
140 SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem*)),
141 this, SLOT(change_page(QListWidgetItem *, QListWidgetItem*)));
145void GemcGUI::updateGui() {
147 std::vector<std::string> sBefore =
150 int nThatWasRun = nEvents->text().toInt();
151 int nBefore = stoi(sBefore[2]);
153 QString newNEvents(
"Event Number: ");
154 newNEvents.append(std::to_string(nBefore + nThatWasRun).c_str());
156 eventNumberLabel->setText(newNEvents);
160void GemcGUI::resetVisualizationBeforeGeometryReload() {
161#ifdef GEMC_HAS_QTCHARTS
162 if (analysisView !=
nullptr) { analysisView->clearForGeometryReload(); }
164 resetSceneBeforeGeometryReload(G4UImanager::GetUIpointer());
167void GemcGUI::refreshVisualizationFromOptions() {
168 if (!detectorConstruction || !detectorConstruction->has_built_geometry()) {
return; }
170 auto* g4uim = G4UImanager::GetUIpointer();
171 if (!g4uim) {
return; }
173 restoreSceneModels(g4uim, guiOptions,
false);
174 visualizationNeedsRunRestore =
true;
177void GemcGUI::refreshGeometryTree() {
178 if (!rightContent || !detectorConstruction || !guiOptions || !geometryTree) {
return; }
180 geometryReloadedSinceRun =
true;
182 const int treeIndex = rightContent->indexOf(geometryTree);
183 if (treeIndex < 0) {
return; }
185 const int currentIndex = rightContent->currentIndex();
186 auto* g4uim = G4UImanager::GetUIpointer();
187 if (!g4uim) {
return; }
189 const auto g4volumes = detectorConstruction->has_built_geometry()
190 ? detectorConstruction->get_g4volumes_map()
191 : std::unordered_map<std::string, G4Volume*>{};
192 const auto gvolumes = detectorConstruction->has_built_geometry()
193 ? detectorConstruction->get_gvolumes_flat_map()
194 : std::unordered_map<std::string, const GVolume*>{};
195 const auto gmirrors = detectorConstruction->has_built_geometry()
196 ? detectorConstruction->get_gmirrors_flat_map()
197 : std::unordered_map<std::string, const GMirror*>{};
198 auto* refreshedTree =
new GTree(guiOptions, g4volumes, gvolumes,
nullptr, gmirrors);
200 rightContent->removeWidget(geometryTree);
201 geometryTree->deleteLater();
202 geometryTree = refreshedTree;
203 rightContent->insertWidget(treeIndex, geometryTree);
204 rightContent->setCurrentIndex(currentIndex == treeIndex ? treeIndex : currentIndex);
206 if (g4volumes.size() > 1) {
207 if (!viewerInitialized) {
209 G4SceneProperties g4SceneProperties(guiOptions);
210 auto commands = g4SceneProperties.scene_commands(guiOptions);
211 for (
const auto& command : commands) { g4uim->ApplyCommand(command); }
212 viewerInitialized =
true;
215 restoreSceneModels(g4uim, guiOptions,
false);
219void GemcGUI::prepareGeometryForBeamOn() {
220 if ((!geometryReloadedSinceRun && !visualizationNeedsRunRestore) || !detectorConstruction) {
return; }
221 if (!detectorConstruction->has_built_geometry()) {
return; }
223 auto* g4uim = G4UImanager::GetUIpointer();
224 resetEventDrawingBeforeRun(g4uim);
226 if (geometryReloadedSinceRun) {
227 detectorConstruction->prepare_geometry_for_run();
228 if (eventDispenser) {
229 eventDispenser->resetRunContext();
236 restoreSceneModels(g4uim, guiOptions,
true);
238 geometryReloadedSinceRun =
false;
239 visualizationNeedsRunRestore =
false;
249void GemcGUI::change_page(QListWidgetItem* current, QListWidgetItem* previous) {
257 rightContent->setCurrentIndex(thisIndex);
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)
vector< std::string > getStringVectorFromString(const std::string &input)