source: trunk/geant4/visualization/management/src/G4VisCommandsSceneHandler.cc @ 625

Last change on this file since 625 was 617, checked in by garnier, 17 years ago

r670@mac-90108: laurentgarnier | 2007-11-26 18:13:28 +0100
mise a jour pour Qt3 linux

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