Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4VisCommandsSceneHandler.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 // $Id: G4VisCommandsSceneHandler.cc 99418 2016-09-21 09:18:42Z gcosmo $
28 
29 // /vis/sceneHandler commands - John Allison 10th October 1998
30 
32 
33 #include "G4VisManager.hh"
34 #include "G4GraphicsSystemList.hh"
35 #include "G4VisCommandsScene.hh"
36 #include "G4UImanager.hh"
37 #include "G4UIcommand.hh"
38 #include "G4UIcmdWithAString.hh"
39 #include "G4ios.hh"
40 #include <sstream>
41 
43 
45  G4bool omitable, currentAsDefault;
46  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
47  fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
48  fpCommand -> SetGuidance
49  ("If scene-name is omitted, current scene is attached. To see scenes and"
50  "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
51  fpCommand -> SetParameterName ("scene-name",
52  omitable = true,
53  currentAsDefault = true);
54 }
55 
57  delete fpCommand;
58 }
59 
61  G4Scene* pScene = fpVisManager -> GetCurrentScene ();
62  return pScene ? pScene -> GetName () : G4String("");
63 }
64 
66  G4String newValue) {
67 
69 
70  G4String& sceneName = newValue;
71 
72  if (sceneName.length () == 0) {
73  if (verbosity >= G4VisManager::warnings) {
74  G4cout <<
75  "WARNING: No scene specified. Maybe there are no scenes available"
76  "\n yet. Please create one." << G4endl;
77  }
78  return;
79  }
80 
81  G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
82  if (!pSceneHandler) {
83  if (verbosity >= G4VisManager::errors) {
84  G4cerr <<
85  "ERROR: Current scene handler not defined. Please select or create one."
86  << G4endl;
87  }
88  return;
89  }
90 
91  G4SceneList& sceneList = fpVisManager -> SetSceneList ();
92 
93  if (sceneList.empty ()) {
94  if (verbosity >= G4VisManager::errors) {
95  G4cerr <<
96  "ERROR: No valid scenes available yet. Please create one."
97  << G4endl;
98  }
99  return;
100  }
101 
102  G4int iScene, nScenes = sceneList.size ();
103  for (iScene = 0; iScene < nScenes; iScene++) {
104  if (sceneList [iScene] -> GetName () == sceneName) break;
105  }
106  if (iScene < nScenes) {
107  G4Scene* pScene = sceneList [iScene];
108  pSceneHandler -> SetScene (pScene);
109  // Make sure scene is current...
110  fpVisManager -> SetCurrentScene (pScene);
111  // Refresh viewer, if any (only if auto-refresh)...
112  G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
113  if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
114  pViewer -> SetView ();
115  pViewer -> ClearView ();
116  pViewer -> DrawView ();
117  }
118  if (verbosity >= G4VisManager::confirmations) {
119  G4cout << "Scene \"" << sceneName
120  << "\" attached to scene handler \""
121  << pSceneHandler -> GetName () <<
122  ".\n (You may have to refresh with \"/vis/viewer/flush\" if view"
123  " is not \"auto-refresh\".)"
124  << G4endl;
125  }
126  }
127  else {
128  if (verbosity >= G4VisManager::errors) {
129  G4cerr << "ERROR: Scene \"" << sceneName
130  << "\" not found. Use \"/vis/scene/list\" to see possibilities."
131  << G4endl;
132  }
133  }
134 }
135 
137 
139  G4bool omitable;
140  fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
141  fpCommand -> SetGuidance
142  ("Creates an scene handler for a specific graphics system.");
143  fpCommand -> SetGuidance
144  ("Attaches current scene, if any. (You can change attached scenes with"
145  "\n\"/vis/sceneHandler/attach\".) Invents a scene handler name if not"
146  "\nsupplied. This scene handler becomes current.");
147  G4UIparameter* parameter;
148  parameter = new G4UIparameter ("graphics-system-name",
149  's', omitable = false);
150  const G4GraphicsSystemList& gslist =
151  fpVisManager -> GetAvailableGraphicsSystems ();
152  G4String candidates;
153  for (const auto gs: gslist) {
154  const G4String& name = gs -> GetName ();
155  candidates += name + ' ';
156  for (const auto& nickname: gs -> GetNicknames ()) {
157  if (nickname != name) candidates += nickname + ' ';
158  }
159  }
160  candidates = candidates.strip ();
161  parameter -> SetParameterCandidates(candidates);
162  fpCommand -> SetParameter (parameter);
163  parameter = new G4UIparameter
164  ("scene-handler-name", 's', omitable = true);
165  parameter -> SetCurrentAsDefault (true);
166  fpCommand -> SetParameter (parameter);
167 }
168 
170  delete fpCommand;
171 }
172 
173 G4String G4VisCommandSceneHandlerCreate::NextName () {
174  std::ostringstream oss;
175  oss << "scene-handler-" << fId;
176  return oss.str();
177 }
178 
180 
181  G4String graphicsSystemName;
182  const G4VGraphicsSystem* graphicsSystem =
183  fpVisManager -> GetCurrentGraphicsSystem ();
184  if (graphicsSystem) {
185  graphicsSystemName = graphicsSystem -> GetName ();
186  }
187  else {
188  const G4GraphicsSystemList& gslist =
189  fpVisManager -> GetAvailableGraphicsSystems ();
190  if (gslist.size ()) {
191  graphicsSystemName = gslist [0] -> GetName ();
192  }
193  else {
194  graphicsSystemName = "none";
195  }
196  }
197 
198  return graphicsSystemName + " " + NextName ();
199 }
200 
202  G4String newValue) {
203 
205 
206  G4String graphicsSystem, newName;
207  std::istringstream is (newValue);
208  is >> graphicsSystem >> newName;
209 
210  const G4GraphicsSystemList& gsl =
211  fpVisManager -> GetAvailableGraphicsSystems ();
212  G4int nSystems = gsl.size ();
213  if (nSystems <= 0) {
214  if (verbosity >= G4VisManager::errors) {
215  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
216  " no graphics systems available."
217  "\n Did you instantiate any in"
218  " YourVisManager::RegisterGraphicsSystems()?"
219  << G4endl;
220  }
221  return;
222  }
223  G4int iGS; // Selector index.
224  G4bool found = false;
225  for (iGS = 0; iGS < nSystems; iGS++) {
226  const auto& gs = gsl[iGS];
227  if (graphicsSystem.compareTo(gs->GetName(), G4String::ignoreCase) == 0) {
228  found = true;
229  break; // Match found
230  } else {
231  const auto& nicknames = gs->GetNicknames();
232  for (size_t i = 0; i < nicknames.size(); ++i) {
233  const auto& nickname = nicknames[i];
234  if (graphicsSystem.compareTo (nickname, G4String::ignoreCase) == 0) {
235  found = true;
236  break; // Match found
237  }
238  }
239  if (found) {
240  break; // Match found
241  }
242  }
243  }
244  if (!found) {
245  // Invalid command line argument or none.
246  // This shouldn't happen!!!!!!
247  if (verbosity >= G4VisManager::errors) {
248  G4cerr <<
249  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
250  "\n invalid graphics system \""
251  << graphicsSystem
252  << "\" requested."
253  << G4endl;
254  }
255  return;
256  }
257 
258  // Check UI session compatibility.
259  G4bool fallback = false;
260  while (!gsl[iGS]->IsUISessionCompatible()) {
261  // Not compatible, search for a fallback
262  fallback = false;
263  G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
264  for (iGS = 0; iGS < nSystems; iGS++) {
265  const auto& nicknames = gsl[iGS]->GetNicknames();
266  for (size_t i = 0; i < nicknames.size(); ++i) {
267  const auto& nickname = nicknames[i];
268  if (fallbackNickname.compareTo (nickname, G4String::ignoreCase) == 0) {
269  fallback = true;
270  break; // Match found
271  }
272  }
273  if (fallback) {
274  break; // Match found
275  }
276  }
277  if (iGS < 0 || iGS >= nSystems) {
278  if (verbosity >= G4VisManager::errors) {
279  G4cerr <<
280  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
281  " could not find fallback graphics system for \""
282  << graphicsSystem
283  << "\"."
284  << G4endl;
285  }
286  return;
287  }
288  // A fallback system found...but go back and check this too.
289  }
290 
291  // A graphics system has been found
292  G4VGraphicsSystem* pSystem = gsl [iGS];
293 
294  if (fallback && verbosity >= G4VisManager::warnings) {
295  G4cout << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
296  "\n Using fallback graphics system: "
297  << pSystem -> GetName ()
298  << " ("
299  << pSystem -> GetNickname ()
300  << ')'
301  << G4endl;
302  }
303 
304  // Set current graphics system in preparation for
305  // creating scene handler.
306  fpVisManager -> SetCurrentGraphicsSystem (pSystem);
307  if (verbosity >= G4VisManager::confirmations) {
308  G4cout << "Graphics system set to "
309  << pSystem -> GetName ()
310  << " ("
311  << pSystem -> GetNickname ()
312  << ')'
313  << G4endl;
314  }
315 
316  // Now deal with name of scene handler.
317  G4String nextName = NextName ();
318  if (newName == "") {
319  newName = nextName;
320  }
321  if (newName == nextName) fId++;
322 
323  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
324  size_t iScene;
325  for (iScene = 0; iScene < list.size (); iScene++) {
326  G4VSceneHandler* sceneHandler = list [iScene];
327  if (sceneHandler -> GetName () == newName) {
328  if (verbosity >= G4VisManager::errors) {
329  G4cerr << "ERROR: Scene handler \"" << newName
330  << "\" already exists." << G4endl;
331  }
332  return;
333  }
334  }
335 
336  //Create scene handler.
337  fpVisManager -> CreateSceneHandler (newName);
338  if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
339  if (verbosity >= G4VisManager::errors) {
340  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
341  " Curious name mismatch."
342  "\n Current name \""
343  << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
344  << "\" is not the new name \""
345  << newName
346  << "\".\n Please report to vis coordinator."
347  << G4endl;
348  }
349  return;
350  }
351 
352  if (verbosity >= G4VisManager::confirmations) {
353  G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
354  }
355 
356  // Attach scene.
357  if (fpVisManager -> GetCurrentScene ())
358  G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
359 }
360 
362 
364  G4bool omitable;
365  fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
366  fpCommand -> SetGuidance ("Lists scene handler(s).");
367  fpCommand -> SetGuidance
368  ("\"help /vis/verbose\" for definition of verbosity.");
369  G4UIparameter* parameter;
370  parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
371  parameter -> SetDefaultValue ("all");
372  fpCommand -> SetParameter (parameter);
373  parameter = new G4UIparameter ("verbosity", 's', omitable = true);
374  parameter -> SetDefaultValue ("warnings");
375  fpCommand -> SetParameter (parameter);
376 }
377 
379  delete fpCommand;
380 }
381 
383  return "";
384 }
385 
387  G4String newValue) {
388  G4String name, verbosityString;
389  std::istringstream is (newValue);
390  is >> name >> verbosityString;
391  G4VisManager::Verbosity verbosity =
392  fpVisManager->GetVerbosityValue(verbosityString);
393  const G4VSceneHandler* currentSceneHandler =
394  fpVisManager -> GetCurrentSceneHandler ();
395  G4String currentName;
396  if (currentSceneHandler) currentName = currentSceneHandler->GetName();
397 
398  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
399  G4bool found = false;
400  for (size_t iSH = 0; iSH < list.size (); iSH++) {
401  const G4String& iName = list [iSH] -> GetName ();
402  if (name != "all") {
403  if (name != iName) continue;
404  }
405  found = true;
406  if (iName == currentName) {
407  G4cout << " (current)";
408  }
409  else {
410  G4cout << " ";
411  }
412  G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
413  << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
414  if (verbosity >= G4VisManager::parameters) {
415  G4cout << "\n " << *(list [iSH]);
416  }
417  G4cout << G4endl;
418  }
419  if (!found) {
420  G4cout << "No scene handlers found";
421  if (name != "all") {
422  G4cout << " of name \"" << name << "\"";
423  }
424  G4cout << "." << G4endl;
425  }
426 }
427 
429 
431  G4bool omitable;
432  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
433  fpCommand -> SetGuidance ("Selects a scene handler.");
434  fpCommand -> SetGuidance
435  ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
436  "\n possible scene handler names.");
437  fpCommand -> SetParameterName ("scene-handler-name",
438  omitable = false);
439 }
440 
442  delete fpCommand;
443 }
444 
446  return "";
447 }
448 
450  G4String newValue) {
451 
453 
454  G4String& selectName = newValue;
455  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
456 
457  size_t iSH;
458  for (iSH = 0; iSH < list.size (); iSH++) {
459  if (list [iSH] -> GetName () == selectName) break;
460  }
461  if (iSH < list.size ()) {
462  if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
463  == selectName) {
464  if (verbosity >= G4VisManager::confirmations) {
465  G4cout << "Scene handler \"" << selectName << "\""
466  << " already selected." << G4endl;
467  }
468  }
469  else {
470  if (verbosity >= G4VisManager::confirmations) {
471  G4cout << "Scene handler \"" << selectName << "\""
472  << " being selected." << G4endl;
473  }
474  fpVisManager -> SetCurrentSceneHandler (list [iSH]);
475  }
476  }
477  else {
478  if (verbosity >= G4VisManager::errors) {
479  G4cerr << "ERROR: Scene handler \"" << selectName << "\""
480  << " not found - \"/vis/sceneHandler/list\" to see possibilities."
481  << G4endl;
482  }
483  }
484 }
const XML_Char * name
Definition: expat.h:151
G4String strip(G4int strip_Type=trailing, char c=' ')
const G4String & GetName() const
int G4int
Definition: G4Types.hh:78
static Verbosity GetVerbosityValue(const G4String &)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:59
G4String GetCurrentValue(G4UIcommand *command)
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
G4String GetCurrentValue(G4UIcommand *command)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
static Verbosity GetVerbosity()
G4int compareTo(const char *, caseCompare mode=exact) const
void SetNewValue(G4UIcommand *command, G4String newValue)
void SetNewValue(G4UIcommand *command, G4String newValue)
#define G4endl
Definition: G4ios.hh:61
G4String GetCurrentValue(G4UIcommand *command)
G4GLOB_DLL std::ostream G4cerr
void SetNewValue(G4UIcommand *command, G4String newValue)
static G4VisManager * fpVisManager