Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TStructViewerGUI.cxx
Go to the documentation of this file.
1 // @(#)root/gviz3d:$Id$
2 // Author: Tomasz Sosnicki 18/09/09
3 
4 /************************************************************************
5 * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11 
12 #include "TStructViewerGUI.h"
13 #include <TRandom.h>
14 #include "TStructViewer.h"
15 #include "TStructNodeEditor.h"
16 #include "TStructNodeProperty.h"
17 #include "TStructNode.h"
18 #include <TCanvas.h>
19 #include <RQ_OBJECT.h>
20 #include <TGLLogicalShape.h>
21 #include <TGLPhysicalShape.h>
22 #include <TGLWidget.h>
23 #include <TGButtonGroup.h>
24 #include <TGSplitter.h>
25 #include <TList.h>
26 #include <TClass.h>
27 #include <TDataMember.h>
28 #include <TExMap.h>
29 #include <TPolyLine3D.h>
30 #include <TObjArray.h>
31 #include <TColor.h>
32 #include <TGTab.h>
33 #include <TGeoManager.h>
34 #include <TGeoMatrix.h>
35 #include <TMath.h>
36 #include <TROOT.h>
37 #include <TApplication.h>
38 
39 ClassImp(TStructViewerGUI);
40 
41 //________________________________________________________________________
42 //////////////////////////////////////////////////////////////////////////
43 //
44 // TStructViewerGUI is main window of TStructViewer. It provides graphical
45 // interface. In the window we can find panel with tabs and frame with
46 // GLViewer. Tab "Info" serves information about node and is used to naviagate
47 // backward and forward. Second tab "Options" is used to set few options
48 // such as links visibility, scaling method or setting a pointer.
49 // Last tab "Editor" is tab when the TStructNodeEditor is placed.
50 //
51 //////////////////////////////////////////////////////////////////////////
52 
53 TGeoMedium* TStructViewerGUI::fgMedium = NULL;
54 UInt_t TStructViewerGUI::fgCounter = 0;
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Constructs window with "w" as width, "h" as height and given parent "p". Argument "parent" is a pointer to TStructViewer which contains this GUI.
58 /// This constructor build window with all controls, build map with colors, init OpenGL Viewer and create TGeoVolumes.
59 
60 TStructViewerGUI::TStructViewerGUI(TStructViewer* parent, TStructNode* nodePtr, TList* colors, const TGWindow *p,UInt_t w,UInt_t h)
61  : TGMainFrame(p, w, h, kHorizontalFrame)
62 {
63  fParent = parent;
64  fNodePtr = nodePtr;
65 
66  fMaxSlices = 10;
67  fMouseX = 0;
68  fMouseY = 0;
69  fSelectedObject = NULL;
70  fMaxRatio = 0;
71  fColors = colors;
72 
73  if (!gGeoManager) new TGeoManager("tmp","tmp");
74  if (!fgMedium) {
75  fgMedium = new TGeoMedium("MED",1,new TGeoMaterial("Mat", 26.98,13,2.7));
76  }
77 
78  SetCleanup(kDeepCleanup);
79  //////////////////////////////////////////////////////////////////////////
80  // layout
81  //////////////////////////////////////////////////////////////////////////
82  TGVerticalFrame* leftFrame = new TGVerticalFrame(this, 200, 200, kFixedWidth);
83  this->AddFrame(leftFrame, new TGLayoutHints(kFixedWidth, 1, 1, 1, 1));
84  TGTab* tabs = new TGTab(leftFrame);
85  TGLayoutHints* expandX = new TGLayoutHints(kLHintsLeft | kLHintsExpandX, 5,5,5,5);
86  //////////////////////////////////////////////////////////////////////////
87  // INFO
88  //////////////////////////////////////////////////////////////////////////
89  TGCompositeFrame* infoFrame = tabs->AddTab("Info");
90  TGGroupFrame* fInfoMenu = new TGGroupFrame(infoFrame, "Info");
91  fNodeNameLabel = new TGLabel(fInfoMenu, "Name:");
92  fInfoMenu->AddFrame(fNodeNameLabel, expandX);
93  fNodeTypelabel = new TGLabel(fInfoMenu, "Type:");
94  fInfoMenu->AddFrame(fNodeTypelabel, expandX);
95  fMembersCountLabel = new TGLabel(fInfoMenu, "Members:");
96  fInfoMenu->AddFrame(fMembersCountLabel, expandX);
97  fAllMembersCountLabel = new TGLabel(fInfoMenu, "All members:");
98  fInfoMenu->AddFrame(fAllMembersCountLabel, expandX);
99  fLevelLabel = new TGLabel(fInfoMenu, "Level:");
100  fInfoMenu->AddFrame(fLevelLabel, expandX);
101  fSizeLabel = new TGLabel(fInfoMenu, "Size:");
102  fInfoMenu->AddFrame(fSizeLabel, expandX);
103  fTotalSizeLabel = new TGLabel(fInfoMenu, "Total size:");
104  fInfoMenu->AddFrame(fTotalSizeLabel, expandX);
105  infoFrame->AddFrame(fInfoMenu, expandX);
106 
107  //////////////////////////////////////////////////////////////////////////
108  // OPTIONS
109  //////////////////////////////////////////////////////////////////////////
110  TGCompositeFrame* options = tabs->AddTab("Options");
111 
112  fShowLinksCheckButton = new TGCheckButton(options, "Show links");
113  fShowLinksCheckButton->Connect("Toggled(Bool_t)", "TStructViewerGUI", this, "ShowLinksToggled(Bool_t)");
114  options->AddFrame(fShowLinksCheckButton);
115  fShowLinksCheckButton->SetOn();
116 
117  TGVButtonGroup* scaleByGroup = new TGVButtonGroup(options, "Scale by");
118  fScaleBySizeButton = new TGRadioButton(scaleByGroup, "Size");
119  fScaleBySizeButton->Connect("Clicked()", "TStructViewerGUI", this, "ScaleByChangedSlot()");
120  fScaleBySizeButton->SetOn();
121  fScaleByMembersButton = new TGRadioButton(scaleByGroup, "Members count");
122  fScaleByMembersButton->Connect("Clicked()", "TStructViewerGUI", this, "ScaleByChangedSlot()");
123  options->AddFrame(scaleByGroup, expandX);
124 
125  TGHorizontalFrame* defaultColorFrame = new TGHorizontalFrame(options);
126  options->AddFrame(defaultColorFrame, expandX);
127  TGLabel* defColorlabel = new TGLabel(defaultColorFrame, "Default color");
128  defaultColorFrame->AddFrame(defColorlabel, expandX);
129  TGColorSelect* defColorSelect = new TGColorSelect(defaultColorFrame, GetDefaultColor()->GetPixel());
130  defColorSelect->Connect("ColorSelected(Pixel_t)", "TStructViewerGUI", this, "ColorSelectedSlot(Pixel_t)");
131  defaultColorFrame->AddFrame(defColorSelect);
132 
133  TGHorizontalFrame* boxHeightFrame = new TGHorizontalFrame(options);
134  options->AddFrame(boxHeightFrame, expandX);
135  TGLabel* boxHeightLabel = new TGLabel(boxHeightFrame, "Box height:");
136  boxHeightFrame->AddFrame(boxHeightLabel, expandX);
137  fBoxHeightEntry = new TGNumberEntry(boxHeightFrame, 0.1);
138  fBoxHeightEntry->SetLimits(TGNumberEntry::kNELLimitMin, 0.01);
139  fBoxHeightEntry->Connect("ValueSet(Long_t)", "TStructViewerGUI", this, "BoxHeightValueSetSlot(Long_t)");
140  boxHeightFrame->AddFrame(fBoxHeightEntry);
141 
142  TGHorizontalFrame* levelDistanceFrame = new TGHorizontalFrame(options);
143  options->AddFrame(levelDistanceFrame, expandX);
144  TGLabel* lvlDistLabel = new TGLabel(levelDistanceFrame, "Distance between levels");
145  levelDistanceFrame->AddFrame(lvlDistLabel, expandX);
146  fLevelDistanceEntry = new TGNumberEntry(levelDistanceFrame, 1.1);
147  fLevelDistanceEntry->SetLimits(TGNumberEntry::kNELLimitMin, 0.01);
148  fLevelDistanceEntry->Connect("ValueSet(Long_t)", "TStructViewerGUI", this, "LevelDistValueSetSlot(Long_t)");
149  levelDistanceFrame->AddFrame(fLevelDistanceEntry);
150 
151  fAutoRefesh = new TGCheckButton(options, "Auto refresh");
152  fAutoRefesh->SetOn();
153  fAutoRefesh->Connect("Toggled(Bool_t)", "TStructViewerGUI", this, "AutoRefreshButtonSlot(Bool_t)");
154  options->AddFrame(fAutoRefesh, expandX);
155 
156  TGLabel* pointerLabel = new TGLabel(options, "Pointer:");
157  options->AddFrame(pointerLabel, expandX);
158  fPointerTextEntry = new TGTextEntry(options, "0x0000000");
159  options->AddFrame(fPointerTextEntry, expandX);
160  TGLabel* fPointerTypeLabel = new TGLabel(options, "Pointer Type:");
161  options->AddFrame(fPointerTypeLabel, expandX);
162  fPointerTypeTextEntry = new TGTextEntry(options, "TObject");
163  options->AddFrame(fPointerTypeTextEntry, expandX);
164  TGTextButton* setPointerButton = new TGTextButton(options, "Set pointer");
165  setPointerButton->Connect("Clicked()", "TStructViewerGUI", this, "SetPointerButtonSlot()");
166  options->AddFrame(setPointerButton, expandX);
167 
168  //////////////////////////////////////////////////////////////////////////
169  // EDITOR
170  //////////////////////////////////////////////////////////////////////////
171  TGCompositeFrame* editTab = tabs->AddTab("Editor");
172  fEditor = new TStructNodeEditor(fColors, editTab);
173  fEditor->Connect("Update(Bool_t)", "TStructViewerGUI", this, "Update(Bool_t)");
174  editTab->AddFrame(fEditor, expandX);
175 
176  leftFrame->AddFrame(tabs, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 1,1,1,1));
177 
178  TGVSplitter* splitter = new TGVSplitter(this);
179  splitter->SetFrame(leftFrame, true);
180  this->AddFrame(splitter, new TGLayoutHints(kLHintsLeft | kLHintsExpandY));
181 
182  //////////////////////////////////////////////////////////////////////////
183  // NAVIGATE
184  //////////////////////////////////////////////////////////////////////////
185  fUndoButton = new TGTextButton(leftFrame, "Undo");
186  fUndoButton->Connect("Clicked()", "TStructViewerGUI", this, "UndoButtonSlot()");
187  fUndoButton->SetEnabled(false);
188  leftFrame->AddFrame(fUndoButton, expandX);
189 
190  fRedoButton = new TGTextButton(leftFrame, "Redo");
191  fRedoButton->Connect("Clicked()", "TStructViewerGUI", this, "RedoButtonSlot()");
192  fRedoButton->SetEnabled(false);
193  leftFrame->AddFrame(fRedoButton, expandX);
194 
195  TGTextButton* resetCameraButton = new TGTextButton(leftFrame, "Reset camera");
196  leftFrame->AddFrame(resetCameraButton, expandX);
197  resetCameraButton->Connect("Clicked()", "TStructViewerGUI", this, "ResetButtonSlot()");
198 
199  TGTextButton* updateButton = new TGTextButton(leftFrame, "Update");
200  updateButton->Connect("Clicked()", "TStructViewerGUI", this, "UpdateButtonSlot()");
201  leftFrame->AddFrame(updateButton, expandX);
202 
203  TGTextButton* quitButton = new TGTextButton(leftFrame, "Quit");
204  leftFrame->AddFrame(quitButton, expandX);
205  quitButton->Connect("Clicked()", "TApplication", gApplication, "Terminate()");
206 
207  fTopVolume = gGeoManager->MakeBox("TOPVolume", fgMedium,100, 100, 100);
208  gGeoManager->SetTopVolume(fTopVolume);
209  gGeoManager->SetNsegments(40);
210 
211  fCanvas = new TCanvas("c", "c", 0, 0);
212  // drawing after creating canvas to avoid drawing in default canvas
213  fGLViewer = new TGLEmbeddedViewer(this, fCanvas);
214  AddFrame(fGLViewer->GetFrame(), new TGLayoutHints(kLHintsExpandX| kLHintsExpandY, 10,10,10,10));
215  fGLViewer->PadPaint(fCanvas);
216  fGLViewer->Connect("MouseOver(TGLPhysicalShape*)", "TStructViewerGUI", this, "MouseOverSlot(TGLPhysicalShape*)");
217  fGLViewer->GetGLWidget()->Connect("ProcessedEvent(Event_t*)", "TStructViewerGUI", this, "GLWidgetProcessedEventSlot(Event_t*))");
218  fGLViewer->Connect("DoubleClicked()", "TStructViewerGUI", this, "DoubleClickedSlot()");
219  fGLViewer->SetCurrentCamera(TGLViewer::kCameraPerspXOY);
220  Update();
221  fGLViewer->SetResetCamerasOnUpdate(false);
222 
223  SetWindowName("Struct Viewer");
224  MapSubwindows();
225  this->SetWMSizeHints(w, h, 2000, 2000, 0, 0);
226  Resize(GetDefaultSize());
227  MapWindow();
228 
229  fToolTip = new TGToolTip(0, 0, "ToolTip", 500);
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// Destructor
234 
235 TStructViewerGUI::~TStructViewerGUI()
236 {
237  delete fCanvas;
238 }
239 
240 ////////////////////////////////////////////////////////////////////////////////
241 /// Activated when user chage condition
242 
243 void TStructViewerGUI::AutoRefreshButtonSlot(Bool_t on)
244 {
245  if (on) {
246  Update();
247  }
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// Emmited when user changes height of boxes
252 
253 void TStructViewerGUI::BoxHeightValueSetSlot(Long_t /* h */)
254 {
255  if(fAutoRefesh->IsOn()) {
256  Update();
257  }
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Recursive method to calculating nodes posistion in 3D space
262 
263 void TStructViewerGUI::CalculatePosistion(TStructNode* parent)
264 {
265  // choose scaling method
266  if (fScaleBySizeButton->GetState() == kButtonDown) {
267  TStructNode::SetScaleBy(kSize);
268  } else if (fScaleByMembersButton->GetState() == kButtonDown) {
269  TStructNode::SetScaleBy(kMembers);
270  }
271  Float_t ratio = (Float_t)((parent->GetLevel()+1.0) / parent->GetLevel());
272 
273  // changing the angle between parent object and daughters
274  // if center of parent is 0 that is real piramid
275  parent->SetWidth(1);
276  parent->SetHeight(1);
277  parent->SetX(-parent->GetWidth()/2);
278  parent->SetY(-parent->GetHeight()/2);
279 
280  fMaxRatio = parent->GetVolumeRatio();
281 
282  // sorting list of members by size or number of members
283  parent->GetMembers()->Sort(kSortDescending);
284  Divide(parent->GetMembers(), (parent->GetX()) *ratio, (parent->GetX() + parent->GetWidth())* ratio, (parent->GetY())* ratio, (parent->GetY() + parent->GetHeight())*ratio);
285 
286  // sclale all the objects
287  Scale(parent);
288 }
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// Check if all of nodes can be displayed on scene. Hides redendant nodes.
292 
293 void TStructViewerGUI::CheckMaxObjects(TStructNode* parent)
294 {
295  UInt_t object = 0;
296 
297  TList queue;
298  queue.Add(parent);
299  TStructNode* node;
300 
301  while ((node = (TStructNode*) queue.First() )) {
302  object++;
303 
304  if (object > fNodePtr->GetMaxObjects() || node->GetLevel() - fNodePtr->GetLevel() >= fNodePtr->GetMaxLevel()) {
305  break;
306  }
307 
308  node->SetVisible(true);
309 
310  queue.AddAll(node->GetMembers());
311  queue.RemoveFirst();
312 
313  fVisibleObjects.Add(node);
314  }
315 
316  TIter it(&fVisibleObjects);
317  TStructNode* member;
318  while ((node = (TStructNode*) it() )) {
319  if(node->GetLevel() - fNodePtr->GetLevel() == fNodePtr->GetMaxLevel()-1 && node->GetMembersCount() > 0) {
320  node->SetCollapsed(true);
321  continue;
322  }
323 
324  TIter memIt(node->GetMembers());
325  while ((member = (TStructNode*) memIt() )) {
326  if(member->IsVisible() == false) {
327  node->SetCollapsed(true);
328  break;
329  }
330  }
331  }
332 }
333 
334 ////////////////////////////////////////////////////////////////////////////////
335 /// Delete window
336 
337 void TStructViewerGUI::CloseWindow()
338 {
339  DeleteWindow();
340 }
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Slot for default color selsect.
344 /// Sets default colot to "pixel"
345 
346 void TStructViewerGUI::ColorSelectedSlot(Pixel_t pixel)
347 {
348  TStructNodeProperty* prop = GetDefaultColor();
349  if(prop) {
350  prop->SetColor(pixel);
351  Update();
352  }
353 }
354 
355 ////////////////////////////////////////////////////////////////////////////////
356 /// Divides rectangle where the outlining box is placed.
357 
358 void TStructViewerGUI::Divide(TList* list, Float_t x1, Float_t x2, Float_t y1, Float_t y2)
359 {
360  if (list->GetSize() > 1) { // spliting node into two lists
361  ULong_t sum1 = 0, sum = 0;
362 
363  TStructNode* node;
364  TList list1, list2;
365  TIter it(list);
366 
367  while((node = (TStructNode*) it() )) {
368  sum += node->GetVolume();
369  }
370  it.Reset();
371  while((node = (TStructNode*) it() )) {
372  if(sum1 >= sum/2.0) {
373  list2.Add(node);
374  } else {
375  sum1 += node->GetVolume();
376  list1.Add(node);
377  }
378  }
379 
380  if (!sum) return;
381  Float_t ratio = (float)sum1/sum;
382 
383  Float_t width = x2 - x1;
384  Float_t height = y2 - y1;
385  if (width < height) { // vertical split
386  Float_t split = y1 + ratio * height;
387  Divide(&list1, x1, x2, y1, split);
388  Divide(&list2, x1, x2, split, y2);
389  } else { // horizontal
390  Float_t split = x1 + ratio * width;
391  Divide(&list1, x1, split, y1, y2);
392  Divide(&list2, split, x2, y1, y2);
393  }
394  } else if (list->GetSize() == 1) { // divide place to node
395  TStructNode* node = (TStructNode*)(list->First());
396 
397  node->SetWidth(x2 - x1);
398  node->SetHeight(y2 - y1);
399  node->SetX(x1);
400  node->SetY(y1);
401 
402  if (node->GetVolumeRatio() > fMaxRatio) {
403  fMaxRatio = node->GetVolumeRatio();
404  }
405 
406  Float_t ratio = (Float_t)((node->GetLevel()+1.0)/node->GetLevel());
407  node->GetMembers()->Sort(kSortDescending);
408  Divide(node->GetMembers(), x1*ratio, x2*ratio, y1*ratio, y2*ratio);
409  }
410 }
411 
412 ////////////////////////////////////////////////////////////////////////////////
413 /// Activated when user double click on objects on 3D scene. Sets clicked node to top node
414 /// and updates scene with camers reset.
415 
416 void TStructViewerGUI::DoubleClickedSlot()
417 {
418  if (fSelectedObject) {
419  if(fSelectedObject == fNodePtr) {
420  return;
421  }
422 
423  fUndoList.Add(fNodePtr);
424  fNodePtr = fSelectedObject;
425  fUndoButton->SetEnabled(true);
426 
427  Update(kTRUE);
428  }
429 }
430 ////////////////////////////////////////////////////////////////////////////////
431 /// Check limits and draws nodes and links
432 
433 void TStructViewerGUI::Draw(Option_t* /*option*/)
434 {
435  fVolumes.Clear();
436  CheckMaxObjects(fNodePtr);
437 
438  CalculatePosistion(fNodePtr);
439  DrawVolumes(fNodePtr);
440 
441  if(fShowLinksCheckButton->GetState() == kButtonDown) {
442  DrawLink(fNodePtr);
443  }
444 
445  UnCheckMaxObjects();
446 }
447 
448 ////////////////////////////////////////////////////////////////////////////////
449 /// Recursive method to draw links
450 
451 void TStructViewerGUI::DrawLink(TStructNode* parent)
452 {
453  if(parent->GetLevel() - fNodePtr->GetLevel() >= fNodePtr->GetMaxLevel()) {
454  return;
455  }
456 
457  if(parent->IsCollapsed()) {
458  return;
459  }
460 
461  TIter it(parent->GetMembers());
462  TStructNode* node;
463  while((node = (TStructNode*) it())) {
464  TPolyLine3D *l = new TPolyLine3D(2);
465  l->SetPoint(0 ,node->GetCenter(), node->GetMiddle(), -(node->GetLevel() * fLevelDistanceEntry->GetNumber()));
466  l->SetPoint(1 ,parent->GetCenter(), parent->GetMiddle(), -(parent->GetLevel() * fLevelDistanceEntry->GetNumber()));
467 
468  l->SetLineColor(GetColor(node));
469  l->SetLineWidth(1);
470  l->Draw();
471 
472  if(!node->IsCollapsed()) {
473  DrawLink(node);
474  }
475  }
476 }
477 
478 ////////////////////////////////////////////////////////////////////////////////
479 /// Creates and draws TGeoVolume from given "node"
480 
481 void TStructViewerGUI::DrawNode(TStructNode* node)
482 {
483  TGeoVolume* vol;
484 
485  /*if(node->IsCollapsed())
486  {
487  //float r = (node->GetWidth() < node->GetHeight() ? 0.5 * node->GetWidth() : 0.5 * node->GetHeight());
488  //vol = gGeoManager->MakeTorus(node->GetName(),TStructNode::GetMedium(), 0.75*r, 0, r/4);
489 
490  vol = gGeoManager->MakeBox(TString(node->GetName()) + "up",TStructNode::GetMedium(), 0.45*node->GetWidth(), 0.45*node->GetHeight(), (node->GetWidth() < node->GetHeight() ? 0.45 * node->GetWidth() : 0.45 * node->GetHeight()));
491  Double_t max = TMath::Max(0.22 * node->GetWidth(), 0.22 * node->GetHeight());
492  TGeoVolume* subvol = gGeoManager->MakeTrd2(node->GetName(), TStructNode::GetMedium(), 0, 0.45 * node->GetWidth(), 0, 0.45 * node->GetHeight(), max);
493  subvol->SetLineColor(GetColor(node));
494  subvol->SetNumber((Int_t)node);
495  TGeoTranslation* subtrans = new TGeoTranslation("subtranslation", 0, 0, -max);
496  vol->AddNodeOverlap(subvol, 1, subtrans);
497 
498  subvol = gGeoManager->MakeTrd2(TString(node->GetName()) + "down", TStructNode::GetMedium(), 0.45 * node->GetWidth(), 0, 0.45 * node->GetHeight(), 0, max);
499  subvol->SetLineColor(GetColor(node));
500  subvol->SetNumber((Int_t)node);
501  subtrans = new TGeoTranslation("subtranslation", 0, 0, max);
502  vol->AddNodeOverlap(subvol, 1, subtrans);
503  }
504  else*/ if(node->GetNodeType() == kCollection) {
505  vol = gGeoManager->MakeBox(Form("%s_%d", node->GetName(), fgCounter++), fgMedium, 0.45*node->GetWidth(), 0.45*node->GetHeight(), fBoxHeightEntry->GetNumber());
506  // subboxes
507  Float_t slices = (Float_t)(node->GetMembersCount());
508  if (slices > fMaxSlices) {
509  slices = (Float_t)fMaxSlices;
510  }
511 
512  for (Float_t i = -(slices-1)/2; i < slices/2; i++) {
513  TGeoVolume* sub = gGeoManager->MakeBox(Form("%s_%d", node->GetName(), fgCounter++), fgMedium,0.45*node->GetWidth() * 0.7 / slices, 0.45*node->GetHeight(), fBoxHeightEntry->GetNumber());
514  sub->SetLineColor(GetColor(node));
515  fVolumes.Add((Long_t)sub, (Long_t)node);
516  TGeoTranslation* subtrans = new TGeoTranslation("subtranslation", i * node->GetWidth() / slices, 0, 0);
517  vol->AddNodeOverlap(sub, 1, subtrans);
518  }
519  } else {
520  vol = gGeoManager->MakeBox(Form("%s_%d", node->GetName(), fgCounter++), fgMedium, 0.45*node->GetWidth(), 0.45*node->GetHeight(), fBoxHeightEntry->GetNumber());
521  }
522 
523  vol->SetLineColor(GetColor(node));
524  vol->SetLineWidth(1);
525 
526  TGeoTranslation* trans = new TGeoTranslation("translation", node->GetCenter(), node->GetMiddle(), -(node->GetLevel() * fLevelDistanceEntry->GetNumber()));
527  fVolumes.Add((Long_t)vol, (Long_t)node);
528 
529  fTopVolume->AddNode(vol,1, trans);
530 }
531 
532 ////////////////////////////////////////////////////////////////////////////////
533 /// Recursive method to draw GeoVolumes
534 
535 void TStructViewerGUI::DrawVolumes(TStructNode* parent)
536 {
537  if(parent->GetLevel() - fNodePtr->GetLevel() >= fNodePtr->GetMaxLevel()) {
538  return;
539  }
540 
541  DrawNode(parent);
542 
543  if(parent->IsCollapsed()) {
544  return;
545  }
546 
547  TIter nextVis(parent->GetMembers());
548  TStructNode* node;
549  while((node = (TStructNode*)nextVis())) {
550  DrawVolumes(node);
551  }
552 }
553 
554 ////////////////////////////////////////////////////////////////////////////////
555 /// Returns pointer to property associated with node "node". If property is not found
556 /// then it returns default property
557 
558 TStructNodeProperty* TStructViewerGUI::FindNodeProperty(TStructNode* node)
559 {
560  TIter it(fColors);
561  TStructNodeProperty* prop;
562  while ((prop = (TStructNodeProperty*) it() )) {
563  TString propName(prop->GetName());
564  if (propName.EndsWith("+")) {
565 
566  if (TClass* cl = TClass::GetClass(node->GetTypeName())) {
567  propName.Remove(propName.Length()-1, 1);
568  if (cl->InheritsFrom(propName.Data())) {
569  return prop;
570  }
571  }
572  } else {
573  if (propName == TString(node->GetTypeName())) {
574  return prop;
575  }
576  }
577  }
578 
579  return (TStructNodeProperty*)fColors->Last();
580 }
581 
582 //________________________________________________________________________`
583 TCanvas* TStructViewerGUI::GetCanvas()
584 {
585  // Returns canvas used to keep TGeoVolumes
586 
587  return fCanvas;
588 }
589 ////////////////////////////////////////////////////////////////////////////////
590 /// Returns color form fColors for given "node"
591 
592 Int_t TStructViewerGUI::GetColor(TStructNode* node)
593 {
594  TStructNodeProperty* prop = FindNodeProperty(node);
595  if (prop) {
596  return prop->GetColor().GetNumber();
597  }
598 
599  return 2;
600 }
601 
602 ////////////////////////////////////////////////////////////////////////////////
603 /// Return default color for nodes
604 
605 TStructNodeProperty* TStructViewerGUI::GetDefaultColor()
606 {
607  return ((TStructNodeProperty*)(fColors->Last()));
608 }
609 
610 ////////////////////////////////////////////////////////////////////////////////
611 /// Returns true if links are visible, otherwise return false.
612 
613 Bool_t TStructViewerGUI::GetLinksVisibility() const
614 {
615  if (fShowLinksCheckButton->GetState() == kButtonDown) {
616  return true;
617  } else {
618  return false;
619  }
620 }
621 
622 ////////////////////////////////////////////////////////////////////////////////
623 /// Returns top node pointer
624 
625 TStructNode* TStructViewerGUI::GetNodePtr() const
626 {
627  return fNodePtr;
628 }
629 
630 ////////////////////////////////////////////////////////////////////////////////
631 /// Handle events. Sets fMouseX and fMouseY when user move a mouse over viewer and hides ToolTip
632 
633 void TStructViewerGUI::GLWidgetProcessedEventSlot(Event_t* event)
634 {
635  switch (event->fType) {
636  case kMotionNotify:
637  fMouseX = event->fXRoot + 15;
638  fMouseY = event->fYRoot + 15;
639  break;
640 
641  case kButtonPress:
642  fToolTip->Hide();
643  if (fSelectedObject) {
644  UpdateLabels(fSelectedObject);
645  fEditor->SetModel(fSelectedObject);
646  }
647  break;
648 
649  default:
650  break;
651  }
652 }
653 
654 ////////////////////////////////////////////////////////////////////////////////
655 /// Emmited when user changes distance between levels
656 
657 void TStructViewerGUI::LevelDistValueSetSlot(Long_t /* dist */)
658 {
659  if(fAutoRefesh->IsOn()) {
660  Update(kTRUE);
661  }
662 }
663 
664 ////////////////////////////////////////////////////////////////////////////////
665 /// MouseOver slot. Activated when user out mouse over object on scene.
666 /// Sets ToolTip and updates labels
667 
668 void TStructViewerGUI::MouseOverSlot(TGLPhysicalShape* shape)
669 {
670  fToolTip->Hide();
671  fSelectedObject = NULL;
672  if (shape && shape->GetLogical()) {
673  fSelectedObject = (TStructNode*)(shape->GetLogical()->ID());
674  if (fSelectedObject) {
675  if (fSelectedObject->IsA()->InheritsFrom(TPolyLine3D::Class())) {
676  fSelectedObject = NULL;
677  return;
678  }
679  Long_t shapeID = (Long_t)(shape->GetLogical()->ID());
680  Long_t volValue = (Long_t)fVolumes.GetValue(shapeID);
681  fSelectedObject = (TStructNode*)volValue;
682 
683  fToolTip->SetText(TString(fSelectedObject->GetName()) + "\n" + fSelectedObject->GetTypeName());
684  fToolTip->SetPosition(fMouseX, fMouseY);
685  fToolTip->Reset();
686  UpdateLabels(fSelectedObject);
687  }
688  }
689 }
690 
691 ////////////////////////////////////////////////////////////////////////////////
692 /// Activated when user click Redo button. Repeat last Undo action.
693 
694 void TStructViewerGUI::RedoButtonSlot()
695 {
696  fUndoList.Add(fNodePtr);
697  fUndoButton->SetEnabled(true);
698  fNodePtr = (TStructNode*) fRedoList.Last();
699  fRedoList.RemoveLast();
700  if (!fRedoList.First()) {
701  fRedoButton->SetEnabled(false);
702  }
703  Update(kTRUE);
704  UpdateLabels(fNodePtr);
705 }
706 
707 ////////////////////////////////////////////////////////////////////////////////
708 /// Resets camera
709 
710 void TStructViewerGUI::ResetButtonSlot()
711 {
712  fGLViewer->UpdateScene();
713  fGLViewer->ResetCurrentCamera();
714 }
715 
716 ////////////////////////////////////////////////////////////////////////////////
717 /// Recursive method to scaling all modes on scene. We have to scale nodes to get real ratio between nodes.
718 /// Uses fMaxRatio.
719 
720 void TStructViewerGUI::Scale(TStructNode* parent)
721 {
722  // newRatio = sqrt(ratio/maxratio)
723  Float_t newRatio = (Float_t)(TMath::Sqrt(parent->GetRelativeVolumeRatio()/fMaxRatio));
724  // set left top conner in the center
725  parent->SetX(parent->GetX() + parent->GetWidth()/2);
726  parent->SetY(parent->GetY() + parent->GetHeight()/2);
727  // set new size
728  Float_t min = (Float_t)TMath::Min(parent->GetWidth(), parent->GetHeight());
729  parent->SetWidth(parent->GetWidth() * newRatio);
730  parent->SetHeight(parent->GetHeight() * newRatio);
731  // fit the ratio -> height to width
732  Float_t sqrt = (Float_t)(TMath::Sqrt(parent->GetWidth() * parent->GetHeight()));
733  // it's a square
734  if (min > sqrt) {
735  parent->SetWidth(sqrt);
736  parent->SetHeight(sqrt);
737  } else { // it's rectangle
738  if (parent->GetHeight() > parent->GetWidth()) {
739  parent->SetWidth(min);
740  parent->SetHeight(sqrt * sqrt / min);
741  } else {
742  parent->SetWidth(sqrt * sqrt / min);
743  parent->SetHeight(min);
744  }
745  }
746  // move left top corner
747  parent->SetX(parent->GetX() - parent->GetWidth()/2);
748  parent->SetY(parent->GetY() - parent->GetHeight()/2);
749 
750  // scale others nodes
751  TStructNode* node;
752  TIter it(parent->GetMembers());
753  while ((node = (TStructNode*) it() )) {
754  Scale(node);
755  }
756 }
757 
758 ////////////////////////////////////////////////////////////////////////////////
759 /// Sets top node pointer and updates view
760 
761 void TStructViewerGUI::SetNodePtr(TStructNode* val)
762 {
763  fNodePtr = val;
764  Update(kTRUE);
765 }
766 
767 ////////////////////////////////////////////////////////////////////////////////
768 /// Sets links visibility to "visible"
769 
770 void TStructViewerGUI::SetLinksVisibility(Bool_t visible)
771 {
772  if (visible) {
773  fShowLinksCheckButton->SetState(kButtonDown);
774  } else {
775  fShowLinksCheckButton->SetState(kButtonUp);
776  }
777 }
778 
779 ////////////////////////////////////////////////////////////////////////////////
780 /// Sets pointer given in fPointerTestEntry to the main pointer
781 
782 void TStructViewerGUI::SetPointerButtonSlot()
783 {
784  void* obj = (void*)gROOT->ProcessLine(fPointerTextEntry->GetText());
785  fParent->SetPointer(obj, fPointerTypeTextEntry->GetText());
786 }
787 
788 ////////////////////////////////////////////////////////////////////////////////
789 /// Changes links visibility and refresh view.
790 
791 void TStructViewerGUI::ShowLinksToggled(Bool_t /*on*/)
792 {
793  if (fAutoRefesh->IsOn()) {
794  Update();
795  }
796 }
797 
798 ////////////////////////////////////////////////////////////////////////////////
799 /// Shows hidden nodes
800 
801 void TStructViewerGUI::UnCheckMaxObjects()
802 {
803  TStructNode* node;
804  TIter it(&fVisibleObjects);
805 
806  while ((node = (TStructNode*) it() )) {
807  node->SetCollapsed(false);
808  node->SetVisible(false);
809  }
810 
811  fVisibleObjects.Clear();
812 }
813 
814 ////////////////////////////////////////////////////////////////////////////////
815 /// Updates view. Clear all the nodes, call draw function and update scene. Doesn't reset camera.
816 
817 void TStructViewerGUI::Update(Bool_t resetCamera)
818 {
819  if (!fNodePtr) {
820  return;
821  }
822 
823  fCanvas->GetListOfPrimitives()->Clear();
824  fTopVolume->ClearNodes();
825  Draw();
826  fCanvas->GetListOfPrimitives()->Add(fTopVolume);
827  fGLViewer->UpdateScene();
828 
829  if(resetCamera) {
830  fGLViewer->ResetCurrentCamera();
831  }
832 }
833 
834 ////////////////////////////////////////////////////////////////////////////////
835 /// Update button slot. Updates scene
836 
837 void TStructViewerGUI::UpdateButtonSlot()
838 {
839  Update();
840 }
841 
842 ////////////////////////////////////////////////////////////////////////////////
843 /// Refresh information in labels when user put mouse over object
844 
845 void TStructViewerGUI::UpdateLabels(TStructNode* node)
846 {
847  fNodeNameLabel->SetText(node->GetName());
848  fNodeTypelabel->SetText(node->GetTypeName());
849 
850  TString name = "Members: ";
851  name += node->GetMembersCount();
852  fMembersCountLabel->SetText(name);
853  name = "All members: ";
854  name += node->GetAllMembersCount();
855  fAllMembersCountLabel->SetText(name);
856  name = "Level: ";
857  name += node->GetLevel();
858  fLevelLabel->SetText(name);
859  name = "Size: ";
860  name += node->GetSize();
861  fSizeLabel->SetText(name);
862  name = "Total size: ";
863  name += node->GetTotalSize();
864  fTotalSizeLabel->SetText(name);
865 }
866 
867 ////////////////////////////////////////////////////////////////////////////////
868 /// UndoButton Slot. Activated when user press Undo button. Restore last top node pointer.
869 
870 void TStructViewerGUI::UndoButtonSlot()
871 {
872  fRedoList.Add(fNodePtr);
873  fRedoButton->SetEnabled(true);
874  fNodePtr = (TStructNode*) fUndoList.Last();
875  fUndoList.RemoveLast();
876  if (!fUndoList.First()) {
877  fUndoButton->SetEnabled(false);
878  }
879  Update(kTRUE);
880  UpdateLabels(fNodePtr);
881 }
882 
883 ////////////////////////////////////////////////////////////////////////////////
884 /// Activated when user press radio button
885 
886 void TStructViewerGUI::ScaleByChangedSlot()
887 {
888  if (fAutoRefesh->IsOn()) {
889  Update();
890  }
891 }