source: trunk/source/visualization/management/src/G4VisCommandsSceneHandler.cc @ 1346

Last change on this file since 1346 was 1346, checked in by garnier, 13 years ago

before tag

  • Property svn:mime-type set to text/cpp
File size: 14.0 KB
Line 
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,v 1.32 2006/06/29 21:29:46 gunter Exp $
28// GEANT4 tag $Name:  $
29
30// /vis/sceneHandler commands - John Allison  10th October 1998
31
32#include "G4VisCommandsSceneHandler.hh"
33
34#include "G4VisManager.hh"
35#include "G4GraphicsSystemList.hh"
36#include "G4VisCommandsScene.hh"
37#include "G4UImanager.hh"
38#include "G4UIcommand.hh"
39#include "G4UIcmdWithAString.hh"
40#include "G4ios.hh"
41#include <sstream>
42
43////////////// /vis/sceneHandler/attach ///////////////////////////////////////
44
45G4VisCommandSceneHandlerAttach::G4VisCommandSceneHandlerAttach () {
46  G4bool omitable, currentAsDefault;
47  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/attach", this);
48  fpCommand -> SetGuidance ("Attaches scene to current scene handler.");
49  fpCommand -> SetGuidance
50    ("If scene-name is omitted, current scene is attached.  To see scenes and"
51  "\nscene handlers, use \"/vis/scene/list\" and \"/vis/sceneHandler/list\"");
52  fpCommand -> SetParameterName ("scene-name",
53                                 omitable = true,
54                                 currentAsDefault = true);
55}
56
57G4VisCommandSceneHandlerAttach::~G4VisCommandSceneHandlerAttach () {
58  delete fpCommand;
59}
60
61G4String G4VisCommandSceneHandlerAttach::GetCurrentValue (G4UIcommand*) {
62  G4Scene* pScene = fpVisManager -> GetCurrentScene ();
63  return pScene ? pScene -> GetName () : G4String("");
64}
65
66void G4VisCommandSceneHandlerAttach::SetNewValue (G4UIcommand*,
67                                                  G4String newValue) {
68
69  G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
70
71  G4String& sceneName = newValue;
72
73  if (sceneName.length () == 0) {
74    if (verbosity >= G4VisManager::warnings) {
75      G4cout <<
76        "WARNING: No scene specified.  Maybe there are no scenes available"
77        "\n  yet.  Please create one." << G4endl;
78    }
79    return;
80  }
81
82  G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler ();
83  if (!pSceneHandler) {
84    if (verbosity >= G4VisManager::errors) {
85      G4cout <<
86      "ERROR: Current scene handler not defined.  Please select or create one."
87             << G4endl;
88    }
89    return;
90  }
91
92  G4SceneList& sceneList = fpVisManager -> SetSceneList ();
93
94  if (sceneList.empty ()) {
95    if (verbosity >= G4VisManager::errors) {
96      G4cout <<
97      "ERROR: No valid scenes available yet.  Please create one."
98             << G4endl;
99    }
100    return;
101  }
102
103  G4int iScene, nScenes = sceneList.size ();
104  for (iScene = 0; iScene < nScenes; iScene++) {
105    if (sceneList [iScene] -> GetName () == sceneName) break;
106  }
107  if (iScene < nScenes) {
108    G4Scene* pScene = sceneList [iScene];
109    pSceneHandler -> SetScene (pScene);
110    // Make sure scene is current...
111    fpVisManager -> SetCurrentScene (pScene);
112    // Refresh viewer, if any (only if auto-refresh)...
113    G4VViewer* pViewer = pSceneHandler -> GetCurrentViewer();
114    if (pViewer && pViewer -> GetViewParameters().IsAutoRefresh()) {
115      pViewer -> SetView ();
116      pViewer -> ClearView ();
117      pViewer -> DrawView ();
118    }
119    if (verbosity >= G4VisManager::confirmations) {
120      G4cout << "Scene \"" << sceneName
121             << "\" attached to scene handler \""
122             << pSceneHandler -> GetName () <<
123        ".\n  (You may have to refresh with \"/vis/viewer/flush\" if view"
124        " is not \"auto-refresh\".)"
125             << G4endl;
126    }
127  }
128  else {
129    if (verbosity >= G4VisManager::errors) {
130      G4cout << "ERROR: Scene \"" << sceneName
131             << "\" not found.  Use \"/vis/scene/list\" to see possibilities."
132             << G4endl;
133    }
134  }
135}
136
137////////////// /vis/sceneHandler/create ///////////////////////////////////////
138
139G4VisCommandSceneHandlerCreate::G4VisCommandSceneHandlerCreate (): fId (0) {
140  G4bool omitable;
141  fpCommand = new G4UIcommand ("/vis/sceneHandler/create", this);
142  fpCommand -> SetGuidance
143    ("Creates an scene handler for a specific graphics system.");
144  fpCommand -> SetGuidance
145    ("Attaches current scene, if any.  (You can change attached scenes with"
146     "\n\"/vis/sceneHandler/attach\".)  Invents a scene handler name if not"
147     "\nsupplied.  This scene handler becomes current.");
148  G4UIparameter* parameter;
149  parameter = new G4UIparameter ("graphics-system-name",
150                                 's', omitable = false);
151  const G4GraphicsSystemList& gslist =
152    fpVisManager -> GetAvailableGraphicsSystems ();
153  G4String candidates;
154  for (size_t igslist = 0; igslist < gslist.size (); igslist++) {
155    const G4String& name = gslist [igslist] -> GetName ();
156    const G4String& nickname = gslist [igslist] -> GetNickname ();
157    if (nickname.isNull ()) {
158      candidates += name;
159    }
160    else {
161      candidates += nickname;
162    }
163    candidates += " ";
164  }
165  candidates = candidates.strip ();
166  parameter -> SetParameterCandidates(candidates);
167  fpCommand -> SetParameter (parameter);
168  parameter = new G4UIparameter
169    ("scene-handler-name", 's', omitable = true);
170  parameter -> SetCurrentAsDefault (true);
171  fpCommand -> SetParameter (parameter);
172}
173
174G4VisCommandSceneHandlerCreate::~G4VisCommandSceneHandlerCreate () {
175  delete fpCommand;
176}
177
178G4String G4VisCommandSceneHandlerCreate::NextName () {
179  std::ostringstream oss;
180  oss << "scene-handler-" << fId;
181  return oss.str();
182}
183
184G4String G4VisCommandSceneHandlerCreate::GetCurrentValue(G4UIcommand*) {
185
186  G4String graphicsSystemName;
187  const G4VGraphicsSystem* graphicsSystem =
188    fpVisManager -> GetCurrentGraphicsSystem ();
189  if (graphicsSystem) {
190    graphicsSystemName = graphicsSystem -> GetName ();
191  }
192  else {
193    const G4GraphicsSystemList& gslist =
194      fpVisManager -> GetAvailableGraphicsSystems ();
195    if (gslist.size ()) {
196      graphicsSystemName = gslist [0] -> GetName ();
197    }
198    else {
199      graphicsSystemName = "none";
200    }
201  }
202
203  return graphicsSystemName + " " + NextName ();
204}
205
206void G4VisCommandSceneHandlerCreate::SetNewValue (G4UIcommand*,
207                                                  G4String newValue) {
208
209  G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
210
211  G4String graphicsSystem, newName;
212  std::istringstream is (newValue);
213  is >> graphicsSystem >> newName;
214
215  const G4GraphicsSystemList& gsl =
216    fpVisManager -> GetAvailableGraphicsSystems ();
217  int nSystems = gsl.size ();
218  if (nSystems <= 0) {
219    if (verbosity >= G4VisManager::errors) {
220      G4cout << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
221        " no graphics systems available."
222        "\n  Did you instantiate any in"
223        " YourVisManager::RegisterGraphicsSystems()?"
224             << G4endl;
225    }
226    return;
227  }
228  int iGS;  // Selector index.
229  for (iGS = 0; iGS < nSystems; iGS++) {
230    if (graphicsSystem.compareTo (gsl [iGS] -> GetName (),
231                                  G4String::ignoreCase) == 0 ||
232        graphicsSystem.compareTo (gsl [iGS] -> GetNickname (),
233                                  G4String::ignoreCase) == 0) {
234      break;  // Match found.
235    }
236  }
237  if (iGS < 0 || iGS >= nSystems) {
238    // Invalid command line argument or non.
239    // This shouldn't happen!!!!!!
240    if (verbosity >= G4VisManager::errors) {
241      G4cout << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
242        " invalid graphics system specified."
243             << G4endl;
244    }
245    return;
246  }
247  // Valid index.  Set current graphics system in preparation for
248  // creating scene handler.
249  G4VGraphicsSystem* pSystem = gsl [iGS];
250  fpVisManager -> SetCurrentGraphicsSystem (pSystem);
251  if (verbosity >= G4VisManager::confirmations) {
252    G4cout << "Graphics system set to " << pSystem -> GetName () << G4endl;
253  }
254
255  // Now deal with name of scene handler.
256  G4String nextName = NextName ();
257  if (newName == "") {
258    newName = nextName;
259  }
260  if (newName == nextName) fId++;
261
262  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
263  size_t iScene;
264  for (iScene = 0; iScene < list.size (); iScene++) {
265    G4VSceneHandler* sceneHandler = list [iScene];
266    if (sceneHandler -> GetName () == newName) {
267      if (verbosity >= G4VisManager::errors) {
268        G4cout << "ERROR: Scene handler \"" << newName
269               << "\" already exists." << G4endl;
270      }
271      return;
272    }
273  }
274
275  //Create scene handler.
276  fpVisManager -> CreateSceneHandler (newName);
277  if (fpVisManager -> GetCurrentSceneHandler () -> GetName () != newName) {
278    if (verbosity >= G4VisManager::errors) {
279      G4cout << "ERROR: G4VisCommandSceneHandlerCreate::SetNewValue:"
280        " Curious name mismatch."
281        "\n Current name \""
282             << fpVisManager -> GetCurrentSceneHandler () -> GetName ()
283             << "\" is not the new name \""
284             << newName
285             << "\".\n  Please report to vis coordinator."
286             << G4endl;
287    }
288    return;
289  }
290
291  if (verbosity >= G4VisManager::confirmations) {
292    G4cout << "New scene handler \"" << newName << "\" created." << G4endl;
293  }
294
295  // Attach scene.
296  if (fpVisManager -> GetCurrentScene ())
297    G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/sceneHandler/attach");
298}
299
300////////////// /vis/sceneHandler/list ///////////////////////////////////////
301
302G4VisCommandSceneHandlerList::G4VisCommandSceneHandlerList () {
303  G4bool omitable;
304  fpCommand = new G4UIcommand ("/vis/sceneHandler/list", this);
305  fpCommand -> SetGuidance ("Lists scene handler(s).");
306  fpCommand -> SetGuidance
307    ("\"help /vis/verbose\" for definition of verbosity.");
308  G4UIparameter* parameter;
309  parameter = new G4UIparameter("scene-handler-name", 's', omitable = true);
310  parameter -> SetDefaultValue ("all");
311  fpCommand -> SetParameter (parameter);
312  parameter = new G4UIparameter ("verbosity", 's', omitable = true);
313  parameter -> SetDefaultValue ("warnings");
314  fpCommand -> SetParameter (parameter);
315}
316
317G4VisCommandSceneHandlerList::~G4VisCommandSceneHandlerList () {
318  delete fpCommand;
319}
320
321G4String G4VisCommandSceneHandlerList::GetCurrentValue (G4UIcommand*) {
322  return "";
323}
324
325void G4VisCommandSceneHandlerList::SetNewValue (G4UIcommand*,
326                                                G4String newValue) {
327  G4String name, verbosityString;
328  std::istringstream is (newValue);
329  is >> name >> verbosityString;
330  G4VisManager::Verbosity verbosity =
331    fpVisManager->GetVerbosityValue(verbosityString);
332  const G4VSceneHandler* currentSceneHandler =
333    fpVisManager -> GetCurrentSceneHandler ();
334  G4String currentName;
335  if (currentSceneHandler) currentName = currentSceneHandler->GetName();
336
337  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
338  G4bool found = false;
339  for (size_t iSH = 0; iSH < list.size (); iSH++) {
340    const G4String& iName = list [iSH] -> GetName ();
341    if (name != "all") {
342      if (name != iName) continue;
343    }
344    found = true;
345    if (iName == currentName) {
346      G4cout << "  (current)";
347    }
348    else {
349      G4cout << "           ";
350    }
351    G4cout << " scene handler \"" << list [iSH] -> GetName () << "\""
352           << " (" << list [iSH] -> GetGraphicsSystem () -> GetName () << ")";
353    if (verbosity >= G4VisManager::parameters) {
354      G4cout << "\n  " << *(list [iSH]);
355    }
356    G4cout << G4endl;
357  }
358  if (!found) {
359    G4cout << "No scene handlers found";
360    if (name != "all") {
361      G4cout << " of name \"" << name << "\"";
362    }
363    G4cout << "." << G4endl;
364  }
365}
366
367////////////// /vis/sceneHandler/select ///////////////////////////////////////
368
369G4VisCommandSceneHandlerSelect::G4VisCommandSceneHandlerSelect () {
370  G4bool omitable;
371  fpCommand = new G4UIcmdWithAString ("/vis/sceneHandler/select", this);
372  fpCommand -> SetGuidance ("Selects a scene handler.");
373  fpCommand -> SetGuidance
374    ("Makes the scene handler current.  \"/vis/sceneHandler/list\" to see"
375     "\n possible scene handler names.");
376  fpCommand -> SetParameterName ("scene-handler-name",
377                                 omitable = false);
378}
379
380G4VisCommandSceneHandlerSelect::~G4VisCommandSceneHandlerSelect () {
381  delete fpCommand;
382}
383
384G4String G4VisCommandSceneHandlerSelect::GetCurrentValue (G4UIcommand*) {
385  return "";
386}
387
388void G4VisCommandSceneHandlerSelect::SetNewValue (G4UIcommand*,
389                                                  G4String newValue) {
390
391  G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
392
393  G4String& selectName = newValue;
394  const G4SceneHandlerList& list = fpVisManager -> GetAvailableSceneHandlers ();
395
396  size_t iSH;
397  for (iSH = 0; iSH < list.size (); iSH++) {
398    if (list [iSH] -> GetName () == selectName) break;
399  }
400  if (iSH < list.size ()) {
401    if (fpVisManager -> GetCurrentSceneHandler () -> GetName ()
402        == selectName) {
403      if (verbosity >= G4VisManager::confirmations) {
404        G4cout << "Scene handler \"" << selectName << "\""
405               << " already selected." << G4endl;
406      }
407    }
408    else {
409      if (verbosity >= G4VisManager::confirmations) {
410        G4cout << "Scene handler \"" << selectName << "\""
411               << " being selected." << G4endl;
412      }
413      fpVisManager -> SetCurrentSceneHandler (list [iSH]);
414    }
415  }
416  else {
417    if (verbosity >= G4VisManager::errors) {
418      G4cout << "ERROR: Scene handler \"" << selectName << "\""
419             << " not found - \"/vis/sceneHandler/list\""
420        "\n  to see possibilities."
421             << G4endl;
422    }
423  }
424}
Note: See TracBrowser for help on using the repository browser.