GEMC  1.8
Geant4 Monte-Carlo Framework
camera_control.cc
Go to the documentation of this file.
1 // %%%%%%%%%%
2 // Qt headers
3 // %%%%%%%%%%
4 #include <QtGui>
5 
6 // windows.h must come before gl.h in windows
7 #ifdef _MSC_VER
8  #include <windows.h>
9 #endif
10 
11 #include <gl.h>
12 
13 // %%%%%%%%%%%%%
14 // gemc headers
15 // %%%%%%%%%%%%%
16 #include "camera_control.h"
17 #include "string_utilities.h"
18 
19 // %%%%%%%%%%%
20 // C++ headers
21 // %%%%%%%%%%%
22 
24 {
25  gemcOpt = Opts;
26  UImanager = G4UImanager::GetUIpointer();
27 
28  QGroupBox *anglesGroup = new QGroupBox(tr("Camera Control"));
29  anglesGroup->setMinimumWidth(450);
30 
31  QLabel *moveLabel = new QLabel(tr("Move:"));
32  moveCombo = new QComboBox;
33  moveCombo->addItem(tr("Detector"));
34  moveCombo->addItem(tr("Light"));
35 
36  QLabel *projLabel = new QLabel(tr("Projection:"));
37  projCombo = new QComboBox;
38  projCombo->addItem(tr("Orthogonal"));
39  projCombo->addItem(tr("Perspective 30"));
40  projCombo->addItem(tr("Perspective 45"));
41  projCombo->addItem(tr("Perspective 60"));
42  connect ( projCombo , SIGNAL( currentIndexChanged (int) ), this, SLOT( set_perspective(int) ) );
43 
44  QHBoxLayout *mpLayout = new QHBoxLayout;
45  mpLayout->addWidget(moveLabel);
46  mpLayout->addWidget(moveCombo);
47  mpLayout->addSpacing(100);
48  mpLayout->addWidget(projLabel);
49  mpLayout->addWidget(projCombo);
50 
51  theta_hall = phi_hall = 0; // initial angles
52  zoom_value = 1; //initial zoom
53 
54  // Theta Controls
55  QLabel *thetaLabel = new QLabel(tr("theta"));
56  theta_slider = new QSlider(Qt::Horizontal);
57  theta_slider->setTickInterval(1);
58  theta_slider->setRange(0, 180);
59  QLCDNumber *Theta_LCD = new QLCDNumber(this);
60  Theta_LCD->setFont(QFont("Helvetica", 32, QFont::Bold));
61  Theta_LCD->setMaximumSize(45, 45);
62  Theta_LCD->setSegmentStyle(QLCDNumber::Flat);
63  for(int t=0; t<=180; t+=30)
64  {
65  char tmp[100];
66  sprintf(tmp, "%d", t);
67  ThetaSet.push_back(tmp);
68  }
69  QComboBox *ThetaCombo = new QComboBox;
70  for(unsigned int i=0; i<ThetaSet.size(); i++) ThetaCombo->addItem(tr(ThetaSet[i].c_str()));
71  ThetaCombo->setMaximumSize(60, 45);
72  QHBoxLayout *thetaLayout = new QHBoxLayout;
73  thetaLayout->addWidget(thetaLabel);
74  thetaLayout->addWidget(theta_slider);
75  thetaLayout->addWidget(Theta_LCD);
76  thetaLayout->addWidget(ThetaCombo);
77 
78  connect ( theta_slider , SIGNAL( valueChanged (int) ), this, SLOT( change_theta(int) ) );
79  connect ( theta_slider , SIGNAL( valueChanged (int) ), Theta_LCD, SLOT( display(int) ) );
80  connect ( ThetaCombo , SIGNAL( currentIndexChanged (int) ), this, SLOT( set_theta(int) ) );
81  connect ( ThetaCombo , SIGNAL( currentIndexChanged (int) ), this, SLOT( change_theta_s(int) ) );
82 
83  // Phi Controls
84  QLabel *phiLabel = new QLabel(tr("phi"));
85  phi_slider = new QSlider(Qt::Horizontal);
86  phi_slider->setTickInterval(1);
87  phi_slider->setRange(0, 360);
88  QLCDNumber *Phi_LCD = new QLCDNumber(this);
89  Phi_LCD->setFont(QFont("Helvetica", 32, QFont::Bold));
90  Phi_LCD->setMaximumSize(45, 45);
91  Phi_LCD->setSegmentStyle(QLCDNumber::Flat);
92  for(int t=0; t<=360; t+=30)
93  {
94  char tmp[100];
95  sprintf(tmp, "%d", t);
96  PhiSet.push_back(tmp);
97  }
98  QComboBox *PhiCombo = new QComboBox;
99  for(unsigned int i=0; i<PhiSet.size(); i++) PhiCombo->addItem(tr(PhiSet[i].c_str()));
100  PhiCombo->setMaximumSize(60, 45);
101  QHBoxLayout *phiLayout = new QHBoxLayout;
102  phiLayout->addWidget(phiLabel);
103  phiLayout->addWidget(phi_slider);
104  phiLayout->addWidget(Phi_LCD);
105  phiLayout->addWidget(PhiCombo);
106 
107  connect ( phi_slider , SIGNAL( valueChanged (int) ), this, SLOT( change_phi(int) ) );
108  connect ( phi_slider , SIGNAL( valueChanged (int) ), Phi_LCD, SLOT( display(int) ) );
109  connect ( PhiCombo , SIGNAL( currentIndexChanged (int) ), this, SLOT( set_phi(int) ) );
110  connect ( PhiCombo , SIGNAL( currentIndexChanged (int) ), this, SLOT( change_phi_s(int) ) );
111 
112  QVBoxLayout *anglesLayout = new QVBoxLayout;
113  anglesLayout->addLayout(mpLayout);
114  anglesLayout->addSpacing(12);
115  anglesLayout->addLayout(thetaLayout);
116  anglesLayout->addSpacing(2);
117  anglesLayout->addLayout(phiLayout);
118  anglesGroup->setLayout(anglesLayout);
119 
120 
121  // Pan label, buttons, slider
122  QGroupBox *PanGroup = new QGroupBox(tr("Pan"));
123 
124  QToolButton *pan_up = new QToolButton(this);
125  pan_up->setArrowType(Qt::UpArrow);
126  pan_up->setAutoRepeat(1);
127  QHBoxLayout *pan_upLayout = new QHBoxLayout;
128  pan_upLayout->addWidget(pan_up);
129 
130  QLabel *panLabel = new QLabel(tr("Pan Strength"));
131  QToolButton *pan_left = new QToolButton(this);
132  pan_left->setAutoRepeat(1);
133  pan_left->setArrowType(Qt::LeftArrow);
134 
135  pan_slider = new QSlider(Qt::Horizontal);
136  pan_slider->setTickInterval(1);
137  pan_slider->setRange(1, 25);
138  pan_slider->setValue(4);
139 
140  QToolButton *pan_right = new QToolButton(this);
141  pan_right->setArrowType(Qt::RightArrow);
142  pan_right->setAutoRepeat(1);
143 
144  QHBoxLayout *pan_lrLayout = new QHBoxLayout;
145  pan_lrLayout->addWidget(pan_left);
146  pan_lrLayout->addWidget(panLabel);
147  pan_lrLayout->addWidget(pan_slider);
148  pan_lrLayout->addWidget(pan_right);
149 
150  QToolButton *pan_down = new QToolButton(this);
151  pan_down->setArrowType(Qt::DownArrow);
152  pan_down->setAutoRepeat(1);
153  QHBoxLayout *pan_downLayout = new QHBoxLayout;
154  pan_downLayout->addWidget(pan_down);
155 
156  connect(pan_up, SIGNAL(pressed()), this, SLOT( clickpan_up() ) );
157  connect(pan_left, SIGNAL(pressed()), this, SLOT( clickpan_left() ) );
158  connect(pan_right,SIGNAL(pressed()), this, SLOT( clickpan_right() ) );
159  connect(pan_down, SIGNAL(pressed()), this, SLOT( clickpan_down() ) );
160 
161  QVBoxLayout *pansLayout = new QVBoxLayout;
162  pansLayout->addLayout(pan_upLayout);
163  pansLayout->addLayout(pan_lrLayout);
164  pansLayout->addLayout(pan_downLayout);
165  PanGroup->setLayout(pansLayout);
166 
167 
168 
169  QGroupBox *vOptionsGroup = new QGroupBox(tr("Visualization Options"));
170 
171  QLabel *antialiasingLabel = new QLabel(tr("Anti-Aliasing"));
172  aliasing = new QComboBox;
173  aliasing->addItem(tr("OFF"));
174  aliasing->addItem(tr("ON"));
175  QHBoxLayout *aliasingLayout = new QHBoxLayout;
176  aliasingLayout->addWidget(antialiasingLabel);
177  aliasingLayout->addWidget(aliasing);
178  connect ( aliasing , SIGNAL( currentIndexChanged (int) ), this, SLOT( switch_antialiasing(int) ) );
179 
180  QLabel *sides_per_circlesLabel = new QLabel(tr("Sides per circle"));
181  sides_per_circle = new QComboBox;
182  sides_per_circle->addItem(tr("50"));
183  sides_per_circle->addItem(tr("100"));
184  sides_per_circle->addItem(tr("200"));
185  sides_per_circle->addItem(tr("500"));
186  sides_per_circle->setCurrentIndex(1);
187  QHBoxLayout *sides_per_circleLayout = new QHBoxLayout;
188  sides_per_circleLayout->addWidget(sides_per_circlesLabel);
189  sides_per_circleLayout->addWidget(sides_per_circle);
190  connect ( sides_per_circle , SIGNAL( currentIndexChanged (int) ), this, SLOT( switch_sides_per_circle(int) ) );
191 
192  QLabel *auxiliaryEdgesLabel = new QLabel(tr("Auxiliary Edges"));
193  auxiliary = new QComboBox;
194  auxiliary->addItem(tr("OFF"));
195  auxiliary->addItem(tr("ON"));
196  QHBoxLayout *auxedgesLayout = new QHBoxLayout;
197  auxedgesLayout->addWidget(auxiliaryEdgesLabel);
198  auxedgesLayout->addWidget(auxiliary);
199  connect ( auxiliary , SIGNAL( currentIndexChanged (int) ), this, SLOT( switch_auxiliary_edges(int) ) );
200 
201  QVBoxLayout *voptionsLayout = new QVBoxLayout;
202  voptionsLayout->addLayout(aliasingLayout);
203  voptionsLayout->addLayout(sides_per_circleLayout);
204  voptionsLayout->addLayout(auxedgesLayout);
205  vOptionsGroup->setLayout(voptionsLayout);
206 
207 
208  QHBoxLayout *PanvOptionsLayout = new QHBoxLayout;
209  PanvOptionsLayout->addWidget(PanGroup);
210  PanvOptionsLayout->addWidget(vOptionsGroup);
211 
212 
213  // Zoom Controls
214  QGroupBox *ZoomGroup = new QGroupBox(tr("Zoom"));
215  zoom_slider = new QSlider(Qt::Horizontal);
216  zoom_slider->setTickInterval(1);
217  zoom_slider->setRange(0, 360);
218 
219  QHBoxLayout *zoomLayout = new QHBoxLayout;
220  zoomLayout->addWidget(zoom_slider);
221  ZoomGroup->setLayout(zoomLayout);
222  connect ( zoom_slider , SIGNAL( valueChanged(int) ), this, SLOT( zoom(int) ) );
223 
224 
225  // Various Controls
226  QGroupBox *OptionsGroup = new QGroupBox(tr(""));
227  QPushButton *back_color = new QPushButton("Background Color", this);
228  QPushButton *screenshot = new QPushButton("Screenshot", this);
229  connect ( back_color, SIGNAL( pressed() ), this, SLOT( change_background_color() ) );
230  connect ( screenshot, SIGNAL( pressed() ), this, SLOT( take_screenshot() ) );
231 
232 
233  QHBoxLayout *optionsLayout = new QHBoxLayout;
234  optionsLayout->addWidget(back_color);
235  optionsLayout->addWidget(screenshot);
236  OptionsGroup->setLayout(optionsLayout);
237 
238 
239  // All together
240  QVBoxLayout *mainLayout = new QVBoxLayout;
241  mainLayout->addWidget(anglesGroup);
242  mainLayout->addLayout(PanvOptionsLayout);
243  mainLayout->addSpacing(6);
244  mainLayout->addWidget(ZoomGroup);
245  mainLayout->addSpacing(8);
246  mainLayout->addWidget(OptionsGroup);
247  mainLayout->addStretch(1);
248  setLayout(mainLayout);
249 
250 
251 
252 }
253 
254 
255 
256 
257 void camera_control::change_theta(int theta)
258 {
259  char command[100];
260  theta_hall = theta - 1;
261 
262  if(gemc_tostring(moveCombo->currentText()) == "Detector")
263  sprintf(command,"/vis/viewer/set/viewpointThetaPhi %d %d.", theta_hall, phi_hall);
264 
265  if(gemc_tostring(moveCombo->currentText()) == "Light")
266  sprintf(command,"/vis/viewer/set/lightsThetaPhi %d %d.", theta_hall, phi_hall);
267 
268  UImanager->ApplyCommand(command);
269 }
270 
271 void camera_control::set_theta(int index)
272 {
273  char command[100];
274  theta_hall = atoi(ThetaSet[index].c_str());
275  sprintf(command,"/vis/viewer/set/viewpointThetaPhi %d %d.", theta_hall, phi_hall);
276  UImanager->ApplyCommand(command);
277 }
278 
279 void camera_control::change_theta_s(int theta)
280 {
281  theta_slider->setSliderPosition(atoi(ThetaSet[theta].c_str()));
282 }
283 
284 void camera_control::change_phi(int phi)
285 {
286  char command[100];
287  phi_hall = phi - 1;
288 
289 
290  if(gemc_tostring(moveCombo->currentText()) == "Detector")
291  sprintf(command,"/vis/viewer/set/viewpointThetaPhi %d %d", theta_hall, phi_hall);
292 
293  if(gemc_tostring(moveCombo->currentText()) == "Light")
294  sprintf(command,"/vis/viewer/set/lightsThetaPhi %d %d.", theta_hall, phi_hall);
295 
296  UImanager->ApplyCommand(command);
297 }
298 
299 void camera_control::change_phi_s(int phi)
300 {
301  phi_slider->setSliderPosition(atoi(PhiSet[phi].c_str()));
302 }
303 
304 void camera_control::set_phi(int index)
305 {
306  char command[100];
307  phi_hall = atoi(PhiSet[index].c_str());
308  sprintf(command,"/vis/viewer/set/viewpointThetaPhi %d %d.", theta_hall, phi_hall);
309  UImanager->ApplyCommand(command);
310 }
311 
312 void camera_control::clickpan_left()
313 {
314  char command[100];
315  sprintf(command,"/vis/viewer/pan %3.2f 0 cm", -(pan_slider->value()*pan_slider->value()/4.0) /zoom_value);
316  UImanager->ApplyCommand(command);
317 }
318 
319 void camera_control::clickpan_right()
320 {
321  char command[100];
322  sprintf(command,"/vis/viewer/pan %3.2f 0 cm", (pan_slider->value()*pan_slider->value()/4.0) / zoom_value);
323  UImanager->ApplyCommand(command);
324 }
325 
326 void camera_control::clickpan_up()
327 {
328  char command[100];
329  sprintf(command,"/vis/viewer/pan 0 %3.2f cm", (pan_slider->value()*pan_slider->value()/4.0) / zoom_value);
330  UImanager->ApplyCommand(command);
331 }
332 
333 void camera_control::clickpan_down()
334 {
335  char command[100];
336  sprintf(command,"/vis/viewer/pan 0 %3.2f cm", -(pan_slider->value()*pan_slider->value()/4.0) / zoom_value);
337  UImanager->ApplyCommand(command);
338 }
339 
340 void camera_control::zoom(int zoom)
341 {
342  static float current_zoom = 0;
343  char command[100];
344  float dz = zoom - current_zoom;
345 
346  zoom_value = 1 + dz/18.0;
347 
348  // cout << current_zoom << " " << dz << " " << zoom_value << endl;
349 
350  sprintf(command,"/vis/viewer/zoom %3.2f", zoom_value);
351 
352  UImanager->ApplyCommand(command);
353  current_zoom = zoom_slider->value();
354 }
355 
356 void camera_control::set_perspective(int index)
357 {
358  char command[100];
359  double angles[4] = { 0, 30, 45, 60};
360  string which[4] = {"o", "p", "p", "p"};
361 
362  sprintf(command,"/vis/viewer/set/projection %s %4.2f ",which[index].c_str(), angles[index]);
363  UImanager->ApplyCommand(command);
364 }
365 
366 void camera_control::switch_antialiasing(int index)
367 {
368  if(index == 0)
369  {
370  glDisable (GL_LINE_SMOOTH);
371  glDisable (GL_POLYGON_SMOOTH);
372  }
373  else
374  {
375  glEnable (GL_LINE_SMOOTH);
376  glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
377  glEnable (GL_POLYGON_SMOOTH);
378  glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
379  }
380  UImanager->ApplyCommand("/vis/viewer/flush");
381 }
382 
383 void camera_control::switch_auxiliary_edges(int index)
384 {
385  if(index == 0)
386  {
387  UImanager->ApplyCommand("/vis/viewer/set/auxiliaryEdge 0");
388  UImanager->ApplyCommand("/vis/viewer/set/hiddenEdge 0");
389  }
390  else
391  {
392  UImanager->ApplyCommand("/vis/viewer/set/auxiliaryEdge 1");
393  UImanager->ApplyCommand("/vis/viewer/set/hiddenEdge 1");
394  }
395  UImanager->ApplyCommand("/vis/viewer/flush");
396 }
397 
398 void camera_control::switch_sides_per_circle(int index)
399 {
400  char command[100];
401  int sides[4] = { 50, 100, 200, 500};
402 
403  sprintf(command,"/vis/viewer/set/lineSegmentsPerCircle %d ", sides[index]);
404  UImanager->ApplyCommand(command);
405  UImanager->ApplyCommand("/vis/viewer/flush");
406 }
407 
408 void camera_control::change_background_color()
409 {
410  int r, g, b;
411  QColor color = QColorDialog::getColor(Qt::green, this);
412  color.getRgb(&r, &g, &b);
413  char command[100];
414  sprintf(command, "/vis/viewer/set/background %3.2f %3.2f %3.2f", r/255.0, g/255.0, b/255.0);
415  UImanager->ApplyCommand(command);
416 }
417 
418 void camera_control::take_screenshot()
419 {
420 }
421 
422 void camera_control::update_angles()
423 {
424 // G4VViewer* currentViewer = visManager->GetCurrentViewer();
425 // theta_hall = (int) floor(currentViewer->GetViewParameters().GetViewpointDirection().getTheta()/deg);
426 // phi_hall = (int) floor(currentViewer->GetViewParameters().GetViewpointDirection().getPhi()/deg );
427 // TransfSli[0]->setSliderPosition ( theta_hall + 1);
428 // TransfSli[1]->setSliderPosition ( phi_hall + 1);
429 }
430 
431 
433 {
434  string hd_msg = gemcOpt->args["LOG_MSG"].args ;
435  double VERB = gemcOpt->args["GEO_VERBOSITY"].arg ;
436  if(VERB>2)
437  cout << hd_msg << " Camera Control Widget Deleted." << endl;
438 
439 }
440 
441 
camera_control(QWidget *parent, gemc_opts *)
G4UImanager * UImanager
string gemc_tostring(QString input)
map< string, opts > args
Options map.
Definition: usage.h:68
gemc_opts * gemcOpt
double r
Definition: dc12geom.h:54