Geant4  10.02
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 91721 2015-08-03 12:06:54Z 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 (auto&& gs: gslist) {
154  const G4String& name = gs -> GetName ();
155  candidates += name + ' ';
156  for (auto&& nickname: gs -> GetNicknames ())
157  candidates += nickname + ' ';
158  }
159  candidates = candidates.strip ();
160  parameter -> SetParameterCandidates(candidates);
161  fpCommand -> SetParameter (parameter);
162  parameter = new G4UIparameter
163  ("scene-handler-name", 's', omitable = true);
164  parameter -> SetCurrentAsDefault (true);
165  fpCommand -> SetParameter (parameter);
166 }
167 
169  delete fpCommand;
170 }
171 
173  std::ostringstream oss;
174  oss << "scene-handler-" << fId;
175  return oss.str();
176 }
177 
179 
180  G4String graphicsSystemName;
181  const G4VGraphicsSystem* graphicsSystem =
182  fpVisManager -> GetCurrentGraphicsSystem ();
183  if (graphicsSystem) {
184  graphicsSystemName = graphicsSystem -> GetName ();
185  }
186  else {
187  const G4GraphicsSystemList& gslist =
188  fpVisManager -> GetAvailableGraphicsSystems ();
189  if (gslist.size ()) {
190  graphicsSystemName = gslist [0] -> GetName ();
191  }
192  else {
193  graphicsSystemName = "none";
194  }
195  }
196 
197  return graphicsSystemName + " " + NextName ();
198 }
199 
201  G4String newValue) {
202 
204 
205  G4String graphicsSystem, newName;
206  std::istringstream is (newValue);
207  is >> graphicsSystem >> newName;
208 
209  const G4GraphicsSystemList& gsl =
210  fpVisManager -> GetAvailableGraphicsSystems ();
211  G4int nSystems = gsl.size ();
212  if (nSystems <= 0) {
213  if (verbosity >= G4VisManager::errors) {
214  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
215  " no graphics systems available."
216  "\n Did you instantiate any in"
217  " YourVisManager::RegisterGraphicsSystems()?"
218  << G4endl;
219  }
220  return;
221  }
222  G4int iGS; // Selector index.
223  G4bool found = false;
224  for (iGS = 0; iGS < nSystems; iGS++) {
225  auto&& gs = gsl[iGS];
226  if (graphicsSystem.compareTo(gs->GetName(), G4String::ignoreCase) == 0) {
227  found = true;
228  break; // Match found
229  } else {
230  auto&& nicknames = gs->GetNicknames();
231  for (size_t i = 0; i < nicknames.size(); ++i) {
232  auto&& nickname = nicknames[i];
233  if (graphicsSystem.compareTo (nickname, G4String::ignoreCase) == 0) {
234  found = true;
235  break; // Match found
236  }
237  }
238  if (found) {
239  break; // Match found
240  }
241  }
242  }
243  if (!found) {
244  // Invalid command line argument or none.
245  // This shouldn't happen!!!!!!
246  if (verbosity >= G4VisManager::errors) {
247  G4cerr <<
248  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
249  "\n invalid graphics system \""
250  << graphicsSystem
251  << "\" requested."
252  << G4endl;
253  }
254  return;
255  }
256 
257  // Check UI session compatibility.
258  G4bool fallback = false;
259  while (!gsl[iGS]->IsUISessionCompatible()) {
260  // Not compatible, search for a fallback
261  fallback = false;
262  G4String fallbackNickname = gsl[iGS]->GetNickname() + "_FALLBACK";
263  for (iGS = 0; iGS < nSystems; iGS++) {
264  auto&& nicknames = gsl[iGS]->GetNicknames();
265  for (size_t i = 0; i < nicknames.size(); ++i) {
266  auto&& nickname = nicknames[i];
267  if (fallbackNickname.compareTo (nickname, G4String::ignoreCase) == 0) {
268  fallback = true;
269  break; // Match found
270  }
271  }
272  if (fallback) {
273  break; // Match found
274  }
275  }
276  if (iGS < 0 || iGS >= nSystems) {
277  if (verbosity >= G4VisManager::errors) {
278  G4cerr <<
279  "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
280  " could not find fallback graphics system for \""
281  << graphicsSystem
282  << "\"."
283  << G4endl;
284  }
285  return;
286  }
287  // A fallback system found...but go back and check this too.
288  }
289 
290  // A graphics system has been found
291  G4VGraphicsSystem* pSystem = gsl [iGS];
292 
293  if (fallback && verbosity >= G4VisManager::warnings) {
294  G4cout << "WARNING: G4VisCommandSceneHandlerCreate::SetNewValue:"
295  "\n Using fallback graphics system: "
296  << pSystem -> GetName ()
297  << " ("
298  << pSystem -> GetNickname ()
299  << ')'
300  << G4endl;
301  }
302 
303  // Set current graphics system in preparation for
304  // creating scene handler.
305  fpVisManager -> SetCurrentGraphicsSystem (pSystem);
306  if (verbosity >= G4VisManager::confirmations) {
307  G4cout << "Graphics system set to "
308  << pSystem -> GetName ()
309  << " ("
310  << pSystem -> GetNickname ()
311  << ')'
312  << G4endl;
313  }
314 
315  // Now deal with name of scene handler.
316  G4String nextName = NextName ();
317  if (newName == "") {
318  newName = nextName;
319  }
320  if (newName == nextName) fId++;
321 
322  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
323  size_t iScene;
324  for (iScene = 0; iScene < list.size (); iScene++) {
325  G4VSceneHandler* sceneHandler = list [iScene];
326  if (sceneHandler -> GetName () == newName) {
327  if (verbosity >= G4VisManager::errors) {
328  G4cerr << "ERROR: Scene handler \"" << newName
329  << "\" already exists." << G4endl;
330  }
331  return;
332  }
333  }
334 
335  //Create scene handler.
336  fpVisManager -> CreateSceneHandler (newName);
337  if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
338  if (verbosity >= G4VisManager::errors) {
339  G4cerr << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
340  " Curious name mismatch."
341  "\n Current name \""
342  << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
343  << "\" is not the new name \""
344  << newName
345  << "\".\n Please report to vis coordinator."
346  << G4endl;
347  }
348  return;
349  }
350 
351  if (verbosity >= G4VisManager::confirmations) {
352  G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
353  }
354 
355  // Attach scene.
356  if (fpVisManager -> GetCurrentScene ())
357  G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
358 }
359 
361 
363  G4bool omitable;
364  fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
365  fpCommand -> SetGuidance ("Lists scene handler(s).");
366  fpCommand -> SetGuidance
367  ("\"help /vis/verbose\" for definition of verbosity.");
368  G4UIparameter* parameter;
369  parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
370  parameter -> SetDefaultValue ("all");
371  fpCommand -> SetParameter (parameter);
372  parameter = new G4UIparameter ("verbosity", 's', omitable = true);
373  parameter -> SetDefaultValue ("warnings");
374  fpCommand -> SetParameter (parameter);
375 }
376 
378  delete fpCommand;
379 }
380 
382  return "";
383 }
384 
386  G4String newValue) {
387  G4String name, verbosityString;
388  std::istringstream is (newValue);
389  is >> name >> verbosityString;
390  G4VisManager::Verbosity verbosity =
391  fpVisManager->GetVerbosityValue(verbosityString);
392  const G4VSceneHandler* currentSceneHandler =
393  fpVisManager -> GetCurrentSceneHandler ();
394  G4String currentName;
395  if (currentSceneHandler) currentName = currentSceneHandler->GetName();
396 
397  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
398  G4bool found = false;
399  for (size_t iSH = 0; iSH < list.size (); iSH++) {
400  const G4String& iName = list [iSH] -> GetName ();
401  if (name != "all") {
402  if (name != iName) continue;
403  }
404  found = true;
405  if (iName == currentName) {
406  G4cout << " (current)";
407  }
408  else {
409  G4cout << " ";
410  }
411  G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
412  << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
413  if (verbosity >= G4VisManager::parameters) {
414  G4cout << "\n " << *(list [iSH]);
415  }
416  G4cout << G4endl;
417  }
418  if (!found) {
419  G4cout << "No scene handlers found";
420  if (name != "all") {
421  G4cout << " of name \"" << name << "\"";
422  }
423  G4cout << "." << G4endl;
424  }
425 }
426 
428 
430  G4bool omitable;
431  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
432  fpCommand -> SetGuidance ("Selects a scene handler.");
433  fpCommand -> SetGuidance
434  ("Makes the scene handler current. \"/vis/sceneHandler/list\" to see"
435  "\n possible scene handler names.");
436  fpCommand -> SetParameterName ("scene-handler-name",
437  omitable = false);
438 }
439 
441  delete fpCommand;
442 }
443 
445  return "";
446 }
447 
449  G4String newValue) {
450 
452 
453  G4String& selectName = newValue;
454  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
455 
456  size_t iSH;
457  for (iSH = 0; iSH < list.size (); iSH++) {
458  if (list [iSH] -> GetName () == selectName) break;
459  }
460  if (iSH < list.size ()) {
461  if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
462  == selectName) {
463  if (verbosity >= G4VisManager::confirmations) {
464  G4cout << "Scene handler \"" << selectName << "\""
465  << " already selected." << G4endl;
466  }
467  }
468  else {
469  if (verbosity >= G4VisManager::confirmations) {
470  G4cout << "Scene handler \"" << selectName << "\""
471  << " being selected." << G4endl;
472  }
473  fpVisManager -> SetCurrentSceneHandler (list [iSH]);
474  }
475  }
476  else {
477  if (verbosity >= G4VisManager::errors) {
478  G4cerr << "ERROR: Scene handler \"" << selectName << "\""
479  << " not found - \"/vis/sceneHandler/list\" to see possibilities."
480  << G4endl;
481  }
482  }
483 }
G4String strip(G4int strip_Type=trailing, char c=' ')
G4String name
Definition: TRTMaterials.hh:40
const G4String & GetName() const
int G4int
Definition: G4Types.hh:78
static Verbosity GetVerbosityValue(const G4String &)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:58
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