Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4InteractorMessenger.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27
29
30#include "G4UIcommand.hh"
31#include "G4UIdirectory.hh"
33
34#include <cstdlib>
35#include <cstring>
36
37#define STRDUP(str) \
38 ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
39#define STRDEL(str) \
40 { \
41 if ((str) != NULL) { \
42 free(str); \
43 str = NULL; \
44 } \
45 }
46
47static G4bool GetValues(G4String, G4int, G4String*);
48
50{
51 session = a_session;
52
53 G4UIparameter* parameter;
54
55 // gui commands should *not* be broadcast to workers
56 G4bool propagateToWorkers;
57 interactorDirectory = new G4UIdirectory("/gui/", propagateToWorkers = false);
58 interactorDirectory->SetGuidance("UI interactors commands.");
59
60 // /gui/addMenu :
61 addMenu = new G4UIcommand("/gui/addMenu", this);
62 addMenu->SetGuidance("Add a menu to menu bar.");
63 parameter = new G4UIparameter("Name", 's', false);
64 parameter->SetDefaultValue("dummy");
65 addMenu->SetParameter(parameter);
66 parameter = new G4UIparameter("Label", 's', false);
67 parameter->SetDefaultValue("dummy");
68 addMenu->SetParameter(parameter);
69
70 // /gui/addButton :
71 addButton = new G4UIcommand("/gui/addButton", this);
72 addButton->SetGuidance("Add a button to menu.");
73 parameter = new G4UIparameter("Menu", 's', false);
74 parameter->SetDefaultValue("dummy");
75 addButton->SetParameter(parameter);
76 parameter = new G4UIparameter("Label", 's', false);
77 parameter->SetDefaultValue("dummy");
78 addButton->SetParameter(parameter);
79 parameter = new G4UIparameter("Command", 's', false);
80 parameter->SetDefaultValue("");
81 addButton->SetParameter(parameter);
82
83 // /gui/defaultIcons :
84 defaultIcons = new G4UIcommand("/gui/defaultIcons", this);
85 defaultIcons->SetGuidance("Set the Geant4 defaults icons in Qt driver.");
86 defaultIcons->SetGuidance("By default, Geant4 icons are enable.");
87
88 parameter = new G4UIparameter("bool", 'b', true);
89 parameter->SetDefaultValue("true");
90 defaultIcons->SetParameter(parameter);
91
92 // /gui/addIcon :
93 addIcon = new G4UIcommand("/gui/addIcon", this);
94 addIcon->SetGuidance("Add a non-checkable icon to the Icon toolbar.");
95 addIcon->SetGuidance(
96 "If the Icon parameter is set to \"user_icon\", you should provide the icon file in xpm "
97 "format, otherwise you have to choose one of the candidate icons");
98 addIcon->SetGuidance(
99 "A command given without parameters will display a window that will allow one to choose the "
100 "parameters (if needed) for this command.");
101 addIcon->SetGuidance(
102 "E.g: /gui/addIcon \"Change background color\" user_icon /vis/viewer/set/background "
103 "../Images/background.xpm");
104 addIcon->SetGuidance("Special cases for the Icon parameter:");
105 addIcon->SetGuidance(
106 " - open: Open an open-file-selector that can run the Command with File as argument.");
107 addIcon->SetGuidance(
108 " - save: Open a save-file-selector that can run the Command with File as argument.");
109 addIcon->SetGuidance(
110 " - move/rotate/pick/zoom_in/zoom_out: Theses icons are radio-button icons that can change "
111 "cursor action.");
112 addIcon->SetGuidance(
113 " - wireframe/solid/hidden_line_removal/hidden_line_and_surface_removal: These icons are "
114 "radio-button icons that can change drawing style.");
115 addIcon->SetGuidance(
116 " - perspective/ortho: These icons are radio-button icons that can change projection style.");
117
118 parameter = new G4UIparameter("Label", 's', false);
119 parameter->SetDefaultValue("");
120 addIcon->SetParameter(parameter);
121
122 parameter = new G4UIparameter("Icon", 's', false);
123 parameter->SetDefaultValue("");
124 parameter->SetParameterCandidates(
125 "open save move rotate pick zoom_in zoom_out wireframe solid hidden_line_removal "
126 "hidden_line_and_surface_removal perspective ortho exit user_icon");
127 addIcon->SetParameter(parameter);
128
129 parameter = new G4UIparameter("Command", 's', true);
130 parameter->SetDefaultValue("no_command");
131 addIcon->SetParameter(parameter);
132
133 parameter = new G4UIparameter("File", 's', true);
134 parameter->SetDefaultValue("no_file");
135 addIcon->SetParameter(parameter);
136
137 // /gui/system :
138 sys = new G4UIcommand("/gui/system", this);
139 sys->SetGuidance("Send a command to the system.");
140 parameter = new G4UIparameter("Command", 's', false);
141 parameter->SetDefaultValue("");
142 sys->SetParameter(parameter);
143
144 // /gui/outputStyle :
145 outputStyle = new G4UIcommand("/gui/outputStyle", this);
146 outputStyle->SetGuidance("Set output style.");
147 outputStyle->SetGuidance("First parameter: destination stream.");
148 outputStyle->SetGuidance("Second parameter: style.");
149 outputStyle->SetGuidance(
150 "Note that highlight for cout refers only to echoed commands, i.e.,"
151 "\ncommands echoed by \"/control/verbose 2\".");
152 outputStyle->SetGuidance("Note also that plain resets both highlight and bold.");
153 parameter = new G4UIparameter("destination", 's', true); // Omitable
154 const auto& outputStyles = session->GetOutputStyles();
155 G4String candidates;
156 for (const auto& style : outputStyles) {
157 candidates += style.first + ' ';
158 }
159 candidates += "all";
160 parameter->SetParameterCandidates(candidates);
161 parameter->SetDefaultValue("all");
162 outputStyle->SetParameter(parameter);
163 parameter = new G4UIparameter("style", 's', true); // Omitable
164 parameter->SetParameterCandidates("fixed proportional bold plain highlight");
165 parameter->SetDefaultValue("fixed");
166 outputStyle->SetParameter(parameter);
167
168 // /gui/nativeMenuBar :
169 nativeMenu = new G4UIcommand("/gui/nativeMenuBar", this);
170 nativeMenu->SetGuidance("Allow native menu bar in Geant4 Qt driver.");
171 nativeMenu->SetGuidance("By default, enable.");
172
173 parameter = new G4UIparameter("bool", 'b', true);
174 parameter->SetDefaultValue("true");
175 nativeMenu->SetParameter(parameter);
176 // /gui/clearMenu
177 clearMenu = new G4UIcommand("/gui/clearMenu", this);
178 clearMenu->SetGuidance("Clear menu bar, remove all user defined menu entries.");
179}
180
182{
183 delete clearMenu;
184 delete nativeMenu;
185 delete outputStyle;
186 delete sys;
187 delete defaultIcons;
188 delete addIcon;
189 delete addButton;
190 delete addMenu;
191 delete interactorDirectory;
192}
193
195{
196 const auto paramn = command->GetParameterEntries();
197 auto params = new G4String[paramn];
198 if (GetValues(newValue, (G4int)paramn, params)) {
199 if (command == addMenu) {
200 session->AddMenu((const char*)params[0], (const char*)params[1]);
201 }
202 else if (command == addButton) {
203 session->AddButton((const char*)params[0], (const char*)params[1], (const char*)params[2]);
204 }
205 else if (command == addIcon) {
206 session->AddIcon((const char*)params[0], (const char*)params[1], (const char*)params[2],
207 (const char*)params[3]);
208 }
209 else if (command == defaultIcons) {
210 session->DefaultIcons(command->ConvertToBool(newValue));
211 }
212 else if (command == sys) {
213 G4int rc = system((const char*)params[0]);
214 if (rc < 0) {
215 }
216 }
217 else if (command == outputStyle) {
218 session->SetOutputStyle((const char*)params[0], (const char*)params[1]);
219 }
220 else if (command == nativeMenu) {
221 session->NativeMenu(command->ConvertToBool(newValue));
222 }
223 else if (command == clearMenu) {
224 session->ClearMenu();
225 }
226 }
227 delete[] params;
228}
229G4bool GetValues(G4String newValue, G4int paramn, G4String* params)
230{
231 char* value = STRDUP(newValue.data());
232 if (value == nullptr) return false;
233 char* tok = strtok(value, " ");
234 for (G4int i = 0; i < paramn; ++i) {
235 if (tok == nullptr) {
236 STRDEL(value);
237 return false;
238 }
239 G4String token = tok;
240 if (token[0] == '"') {
241 while (token.back() != '"') {
242 tok = strtok(nullptr, " ");
243 if ((tok == nullptr) || (*tok == '\0')) {
244 STRDEL(value);
245 return false;
246 }
247 token += " ";
248 token += tok;
249 }
250 G4StrUtil::strip(token, '"');
251 }
252 if (token.empty()) {
253 STRDEL(value);
254 return false;
255 }
256
257 params[i] = token;
258
259 tok = strtok(nullptr, " ");
260 }
261 STRDEL(value);
262 return true;
263}
#define STRDUP(str)
#define STRDEL(str)
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4InteractorMessenger(G4VInteractiveSession *session)
void SetNewValue(G4UIcommand *command, G4String newValue) override
std::size_t GetParameterEntries() const
void SetParameter(G4UIparameter *const newParameter)
void SetGuidance(const char *aGuidance)
static G4bool ConvertToBool(const char *st)
void SetDefaultValue(const char *theDefaultValue)
void SetParameterCandidates(const char *theString)