ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLStoredQtViewer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenGLStoredQtViewer.cc
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 //
28 //
29 // Class G4OpenGLStoredQtViewer : a class derived from G4OpenGLQtViewer and
30 // G4OpenGLStoredViewer.
31 
32 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
33 
35 
37 #include "G4ios.hh"
38 #ifdef G4MULTITHREADED
39 #include "G4Threading.hh"
40 #endif
41 
42 #include <qapplication.h>
43 #include <qtabwidget.h>
44 
45 G4OpenGLStoredQtViewer::G4OpenGLStoredQtViewer
46 (G4OpenGLStoredSceneHandler& sceneHandler,
47  const G4String& name):
48  G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
49  G4OpenGLViewer (sceneHandler),
50  G4OpenGLQtViewer (sceneHandler),
51  G4OpenGLStoredViewer (sceneHandler), // FIXME : gerer le pb du parent !
52  QGLWidget()
53 {
54  fQGLWidgetInitialiseCompleted = false;
55 
56  // Indicates that the widget has no background, i.e. when the widget receives paint events, the background is not automatically repainted. Note: Unlike WA_OpaquePaintEvent, newly exposed areas are never filled with the background (e.g., after showing a window for the first time the user can see "through" it until the application processes the paint events). This flag is set or cleared by the widget's author.
57  QGLWidget::setAttribute (Qt::WA_NoSystemBackground);
58 
59  setFocusPolicy(Qt::StrongFocus); // enable keybord events
60  fHasToRepaint = false;
61  fPaintEventLock = false;
62  fUpdateGLLock = false;
63 
64  if (fViewId < 0) return; // In case error in base class instantiation.
65 }
66 
67 G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
68  makeCurrent();
69  // this is connect to the Dialog for deleting it properly
70  // when close event.
71  // ((QDialog*)window())->reject();
72 }
73 
74 void G4OpenGLStoredQtViewer::Initialise() {
75  makeCurrent();
76 
77  fQGLWidgetInitialiseCompleted = false;
78  CreateMainWindow (this,QString(GetName()));
79 
80  glDrawBuffer (GL_BACK);
81 
82  // set the good tab active
83  if (QGLWidget::parentWidget()) {
84  QTabWidget *parentTab = dynamic_cast<QTabWidget*> (QGLWidget::parentWidget()->parent()) ;
85  if (parentTab) {
86  parentTab->setCurrentIndex(parentTab->count()-1);
87  }
88  }
89 
90  fQGLWidgetInitialiseCompleted = true;
91 }
92 
93 void G4OpenGLStoredQtViewer::initializeGL () {
94 
95  InitializeGLView ();
96 
97  if (fSceneHandler.GetScene() == 0) {
98  fHasToRepaint =false;
99  } else {
100  fHasToRepaint =true;
101  }
102 
103  // Set the component visible
104  // setVisible(true) ;
105 
106 }
107 
108 G4bool G4OpenGLStoredQtViewer::CompareForKernelVisit(G4ViewParameters& lastVP)
109 {
110  // Identical to G4OpenGLStoredViewer::CompareForKernelVisit except
111  // for checking of VisAttributesModifiers, because
112  // G4OpenGLStoredQtViewer keeps track of its own touchable
113  // modifiers (fTreeItemModels, etc.).
114  if (
115  (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) ||
116  (lastVP.GetNumberOfCloudPoints() != fVP.GetNumberOfCloudPoints()) ||
117  (lastVP.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) ||
118  (lastVP.IsCulling () != fVP.IsCulling ()) ||
119  (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
120  (lastVP.IsDensityCulling () != fVP.IsDensityCulling ()) ||
121  (lastVP.IsCullingCovered () != fVP.IsCullingCovered ()) ||
122  (lastVP.GetCBDAlgorithmNumber() !=
123  fVP.GetCBDAlgorithmNumber()) ||
124  (lastVP.IsSection () != fVP.IsSection ()) ||
125  // Section (DCUT) implemented locally. But still need to visit
126  // kernel if status changes so that back plane culling can be
127  // switched.
128  (lastVP.IsCutaway () != fVP.IsCutaway ()) ||
129  // Cutaways implemented locally. But still need to visit kernel
130  // if status changes so that back plane culling can be switched.
131  (lastVP.IsExplode () != fVP.IsExplode ()) ||
132  (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) ||
133  (lastVP.GetDefaultVisAttributes()->GetColour() !=
134  fVP.GetDefaultVisAttributes()->GetColour()) ||
135  (lastVP.GetDefaultTextVisAttributes()->GetColour() !=
136  fVP.GetDefaultTextVisAttributes()->GetColour()) ||
137  (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
138  (lastVP.IsPicking () != fVP.IsPicking ()))
139  return true;
140 
141  // Don't check VisAttributesModifiers if this comparison has been
142  // initiated by a mouse interaction on the scene tree.
143  if (fMouseOnSceneTree) {
144  // Reset the flag.
145  fMouseOnSceneTree = false;
146  } else {
147  // Not initiated by a mouse so compare for kernel visit.
148  if (lastVP.GetVisAttributesModifiers() != fVP.GetVisAttributesModifiers()) {
149  return true;
150  }
151  }
152 
153  if (lastVP.IsDensityCulling () &&
154  (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
155  return true;
156 
157 // /**************************************************************
158 // If section (DCUT) is implemented locally, comment this out.
159  if (lastVP.IsSection () &&
160  (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
161  return true;
162 // ***************************************************************/
163 
164  /**************************************************************
165  If cutaways are implemented locally, comment this out.
166  if (lastVP.IsCutaway ()) {
167  if (lastVP.GetCutawayPlanes ().size () !=
168  fVP.GetCutawayPlanes ().size ()) return true;
169  for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
170  if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
171  return true;
172  }
173  ***************************************************************/
174 
175  if (lastVP.GetCBDAlgorithmNumber() > 0) {
176  if (lastVP.GetCBDParameters().size() != fVP.GetCBDParameters().size()) return true;
177  else if (lastVP.GetCBDParameters() != fVP.GetCBDParameters()) return true;
178  }
179 
180  if (lastVP.IsExplode () &&
181  (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
182  return true;
183 
184  return false;
185 }
186 
187 G4bool G4OpenGLStoredQtViewer::POSelected(size_t POListIndex)
188 {
189  return isTouchableVisible(POListIndex);
190 }
191 
192 G4bool G4OpenGLStoredQtViewer::TOSelected(size_t)
193 {
194  return true;
195 }
196 
197 void G4OpenGLStoredQtViewer::DrawView () {
198  updateQWidget();
199 }
200 
201 void G4OpenGLStoredQtViewer::ComputeView () {
202 
203  makeCurrent();
204  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
205 
206  //Make sure current viewer is attached and clean...
207 
208  //See if things have changed from last time and remake if necessary...
209  // The fNeedKernelVisit flag might have been set by the user in
210  // /vis/viewer/rebuild, but if not, make decision and set flag only
211  // if necessary...
212  if (!fNeedKernelVisit) {
213  KernelVisitDecision ();
214  }
215  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
216  ProcessView ();
217 
218  if (kernelVisitWasNeeded) {
219  displaySceneTreeComponent();
220  }
221 
222  if(dstyle!=G4ViewParameters::hlr &&
223  haloing_enabled) {
224 
225  HaloingFirstPass ();
226  DrawDisplayLists ();
227  glFlush ();
228 
229  HaloingSecondPass ();
230 
231  DrawDisplayLists ();
232  FinishView ();
233 
234  } else {
235 
236  // If kernel visit was needed, drawing and FinishView will already
237  // have been done, so...
238  if (!kernelVisitWasNeeded) {
239  DrawDisplayLists ();
240  FinishView ();
241  } else {
242  // However, union cutaways are implemented in DrawDisplayLists, so make
243  // an extra pass...
244  if (fVP.IsCutaway() &&
245  fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
246  ClearView();
247  DrawDisplayLists ();
248  FinishView ();
249  } else { // ADD TO AVOID KernelVisit=1 and nothing to display
250  DrawDisplayLists ();
251  FinishView ();
252  }
253  }
254  }
255 
256  if (isRecording()) {
257  savePPMToTemp();
258  }
259 
260  fHasToRepaint = true;
261 }
262 
263 
267 void G4OpenGLStoredQtViewer::resizeGL(
268  int aWidth
269  ,int aHeight)
270 {
271  // Set new size, it will be update when next Repaint()->SetView() called
272  if ((aWidth > 0) && (aHeight > 0)) {
273  ResizeWindow(aWidth,aHeight);
274  fHasToRepaint = sizeHasChanged();
275  }
276 }
277 
278 
279 // We have to get several case :
280 // - Only activate the windows (mouse click for example) -> Do not redraw
281 // - resize window -> redraw
282 // - try to avoid recompute everything if we do not rescale picture (side is the same)
283 
284 void G4OpenGLStoredQtViewer::paintGL()
285 {
286  updateToolbarAndMouseContextMenu();
287 
288  if (fPaintEventLock) {
289 // return ;
290  }
291  fPaintEventLock = true;
292  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
293  return;
294  }
295 
296  if (!fQGLWidgetInitialiseCompleted) {
297  fPaintEventLock = false;
298  return;
299  }
300  // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
301  // WHEN CLICK ON THE FRAME FOR EXAMPLE
302  // EXECEPT WHEN MOUSE MOVE EVENT
303  if ( !fHasToRepaint) {
304  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
305  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
306  int sw = 0;
307  int sh = 0;
308  if (!isMaximized() && !isFullScreen()) {
309  sw = normalGeometry().width();
310  sh = normalGeometry().height();
311  } else {
312  sw = frameGeometry().width();
313  sh = frameGeometry().height();
314  }
315  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
316  return;
317  }
318  }
319  // Ensure that we really draw the BACK buffer
320  glDrawBuffer (GL_BACK);
321 
322  SetView();
323 
324  ClearView (); //ok, put the background correct
325  ComputeView();
326 
327  fHasToRepaint = false;
328 
329  fPaintEventLock = false;
330 }
331 
332 void G4OpenGLStoredQtViewer::paintEvent(QPaintEvent *) {
333  if (! fQGLWidgetInitialiseCompleted) {
334  return;
335  }
336  // Force a repaint next time if the FRAMEBUFFER is not READY
337  fHasToRepaint = isFramebufferReady();
338  if ( fHasToRepaint) {
339  // Will really update the widget by calling CGLFlushDrawable
340  // The widget's rendering context will become the current context and initializeGL()
341  // will be called if it hasn't already been called.
342  // Copies the back buffer of a double-buffered context to the front buffer.
343  updateGL();
344  }
345 }
346 
347 void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
348 {
349  G4MousePressEvent(event);
350 }
351 
352 void G4OpenGLStoredQtViewer::keyPressEvent (QKeyEvent * event)
353 {
354  G4keyPressEvent(event);
355 }
356 
357 void G4OpenGLStoredQtViewer::keyReleaseEvent (QKeyEvent * event)
358 {
359  G4keyReleaseEvent(event);
360 }
361 
362 void G4OpenGLStoredQtViewer::wheelEvent (QWheelEvent * event)
363 {
364  G4wheelEvent(event);
365 }
366 
367 void G4OpenGLStoredQtViewer::showEvent (QShowEvent *)
368 {
369  if (fQGLWidgetInitialiseCompleted) {
370  fHasToRepaint = true;
371  }
372 }
373 
378 void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *)
379 {
380  G4MouseDoubleClickEvent();
381 }
382 
383 void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
384 {
385  G4MouseReleaseEvent(event);
386 }
387 
388 void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
389 {
390  G4MouseMoveEvent(event);
391 }
392 
393 
394 void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
395 {
396  G4manageContextMenuEvent(e);
397 }
398 
399 void G4OpenGLStoredQtViewer::updateQWidget() {
400  if (fUpdateGLLock) {
401  return;
402  }
403 
404  if (! isCurrentWidget()){
405  return;
406  }
407 
408  fUpdateGLLock = true;
409  fHasToRepaint= true;
410  // Will really update the widget by calling CGLFlushDrawable
411  // The widget's rendering context will become the current context and initializeGL()
412  // will be called if it hasn't already been called.
413  // Copies the back buffer of a double-buffered context to the front buffer.
414  repaint(); // will read scene tree state
415  // updateGL() // From J.Allison picking branch
416  updateViewerPropertiesTableWidget();
417  updateSceneTreeWidget();
418  fUpdateGLLock = false;
419 }
420 
421 void G4OpenGLStoredQtViewer::ShowView (
422 )
423 
424 
425 {
426  // glFlush (); // Tentativley offered by JA 29/04/16.
427 
428  // Some X servers fail to draw all trajectories, particularly Mac
429  // XQuartz. Revisit this at a future date. Meanwhile, issue an
430  // extra...
431  // ClearView(); // Necessary? JA 29/04/16
432  // DrawView(); // Necessary? JA 29/04/16
433  activateWindow();
434  // glFlush(); // NO NEED and as drawView will already cause a flush
435  // that could do a double flush
436 
437 }
438 
439 
440 void G4OpenGLStoredQtViewer::DisplayTimePOColourModification (
441 G4Colour& c,
442 size_t poIndex) {
443  c = getColorForPoIndex(poIndex);
444 }
445 
446 #endif