Geant4  10.01.p03
G4OpenGLStoredQtViewer.cc
Go to the documentation of this file.
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: G4OpenGLStoredQtViewer.cc 87164 2014-11-26 08:48:31Z gcosmo $
28 //
29 //
30 // Class G4OpenGLStoredQtViewer : a class derived from G4OpenGLQtViewer and
31 // G4OpenGLStoredViewer.
32 
33 #ifdef G4VIS_BUILD_OPENGLQT_DRIVER
34 
36 
38 #include "G4ios.hh"
39 #ifdef G4MULTITHREADED
40 #include "G4Threading.hh"
41 #endif
42 
43 #include <qapplication.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 
55  // 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.
56  QGLWidget::setAttribute (Qt::WA_NoSystemBackground);
57 
58  setFocusPolicy(Qt::StrongFocus); // enable keybord events
59  fHasToRepaint = false;
60  fPaintEventLock = false;
61  fUpdateGLLock = false;
62 
63  resize(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
64 
65  if (fViewId < 0) return; // In case error in base class instantiation.
66 }
67 
68 G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
69  makeCurrent();
70  // this is connect to the Dialog for deleting it properly
71  // when close event.
72  // ((QDialog*)window())->reject();
73 }
74 
75 void G4OpenGLStoredQtViewer::Initialise() {
76  makeCurrent();
77 #ifdef G4DEBUG_VIS_OGL
78  printf("G4OpenGLStoredQtViewer::Initialise 1\n");
79 #endif
80  hide();
81  fQGLWidgetInitialiseCompleted = false;
82  CreateMainWindow (this,QString(GetName()));
83 
84  glDrawBuffer (GL_BACK);
85  fQGLWidgetInitialiseCompleted = true;
86 
87 }
88 
89 void G4OpenGLStoredQtViewer::initializeGL () {
90 
91  InitializeGLView ();
92 
93 #ifdef G4DEBUG_VIS_OGL
94  printf("G4OpenGLStoredQtViewer::InitialiseGL () 1 %d\n", this);
95 #endif
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  // and update it immediatly before wait for SessionStart() (batch mode)
107  QCoreApplication::sendPostedEvents () ;
108 
109 #ifdef G4DEBUG_VIS_OGL
110  printf("G4OpenGLStoredQtViewer::InitialiseGL END\n");
111 #endif
112 }
113 
114 G4bool G4OpenGLStoredQtViewer::CompareForKernelVisit(G4ViewParameters& lastVP)
115 {
116  // Identical to G4OpenGLStoredViewer::CompareForKernelVisit except
117  // for checking of VisAttributesModifiers, because
118  // G4OpenGLStoredQtViewer keeps track of its own touchable
119  // modifiers (fTreeItemModels, etc.).
120  if (
121  (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) ||
122  (lastVP.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) ||
123  (lastVP.IsCulling () != fVP.IsCulling ()) ||
124  (lastVP.IsCullingInvisible () != fVP.IsCullingInvisible ()) ||
125  (lastVP.IsDensityCulling () != fVP.IsDensityCulling ()) ||
126  (lastVP.IsCullingCovered () != fVP.IsCullingCovered ()) ||
127  (lastVP.IsSection () != fVP.IsSection ()) ||
128  // Section (DCUT) implemented locally. But still need to visit
129  // kernel if status changes so that back plane culling can be
130  // switched.
131  (lastVP.IsCutaway () != fVP.IsCutaway ()) ||
132  // Cutaways implemented locally. But still need to visit kernel
133  // if status changes so that back plane culling can be switched.
134  (lastVP.IsExplode () != fVP.IsExplode ()) ||
135  (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) ||
136  (lastVP.GetDefaultVisAttributes()->GetColour() !=
137  fVP.GetDefaultVisAttributes()->GetColour()) ||
138  (lastVP.GetDefaultTextVisAttributes()->GetColour() !=
139  fVP.GetDefaultTextVisAttributes()->GetColour()) ||
140  (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
141  (lastVP.IsPicking () != fVP.IsPicking ())
142 // ||
143 // (lastVP.GetVisAttributesModifiers().size() !=
144 // fVP.GetVisAttributesModifiers().size())
145  )
146  return true;
147 
148  if (lastVP.IsDensityCulling () &&
149  (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
150  return true;
151 
152  /**************************************************************
153  Section (DCUT) implemented locally. No need to visit kernel if
154  section plane itself changes.
155  if (lastVP.IsSection () &&
156  (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
157  return true;
158  ***************************************************************/
159 
160  /**************************************************************
161  Cutaways implemented locally. No need to visit kernel if cutaway
162  planes themselves change.
163  if (lastVP.IsCutaway ()) {
164  if (lastVP.GetCutawayPlanes ().size () !=
165  fVP.GetCutawayPlanes ().size ()) return true;
166  for (size_t i = 0; i < lastVP.GetCutawayPlanes().size(); ++i)
167  if (lastVP.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i])
168  return true;
169  }
170  ***************************************************************/
171 
172  if (lastVP.IsExplode () &&
173  (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
174  return true;
175 
176  return false;
177 }
178 
179 G4bool G4OpenGLStoredQtViewer::POSelected(size_t POListIndex)
180 {
181  return isTouchableVisible(POListIndex);
182 }
183 
184 G4bool G4OpenGLStoredQtViewer::TOSelected(size_t)
185 {
186  return true;
187 }
188 
189 void G4OpenGLStoredQtViewer::DrawView () {
190 #ifdef G4MULTITHREADED
192  updateQWidget();
193  }
194 #else
195  updateQWidget();
196 #endif
197 }
198 
199 void G4OpenGLStoredQtViewer::ComputeView () {
200 
201 #ifdef G4DEBUG_VIS_OGL
202  printf("G4OpenGLStoredQtViewer::ComputeView %d %d VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",getWinWidth(), getWinHeight());
203 #endif
204  makeCurrent();
205  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
206 
207  //Make sure current viewer is attached and clean...
208 
209  //See if things have changed from last time and remake if necessary...
210  // The fNeedKernelVisit flag might have been set by the user in
211  // /vis/viewer/rebuild, but if not, make decision and set flag only
212  // if necessary...
213  if (!fNeedKernelVisit) {
214  KernelVisitDecision ();
215  }
216  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
217  ProcessView ();
218 
219  if (fNeedKernelVisit) {
220  displaySceneTreeComponent();
221  }
222  if(dstyle!=G4ViewParameters::hlr &&
223  haloing_enabled) {
224 #ifdef G4DEBUG_VIS_OGL
225  printf("G4OpenGLStoredQtViewer::ComputeView DANS LE IF\n");
226 #endif
227 
228  HaloingFirstPass ();
229  DrawDisplayLists ();
230  glFlush ();
231 
232  HaloingSecondPass ();
233 
234  DrawDisplayLists ();
235  FinishView ();
236 
237  } else {
238 
239  // If kernel visit was needed, drawing and FinishView will already
240  // have been done, so...
241  if (!kernelVisitWasNeeded) {
242 #ifdef G4DEBUG_VIS_OGL
243  printf("************************** G4OpenGLStoredQtViewer::ComputeView Don't need kernel Visit \n");
244 #endif
245  DrawDisplayLists ();
246  FinishView ();
247  } else {
248 #ifdef G4DEBUG_VIS_OGL
249  printf("************************** G4OpenGLStoredQtViewer::ComputeView need kernel Visit \n");
250 #endif
251  // However, union cutaways are implemented in DrawDisplayLists, so make
252  // an extra pass...
253  if (fVP.IsCutaway() &&
254  fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
255  ClearView();
256  DrawDisplayLists ();
257  FinishView ();
258 #ifdef G4DEBUG_VIS_OGL
259  printf("*************************** CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
260 #endif
261  } else { // ADD TO AVOID KernelVisit=1 and nothing to display
262  DrawDisplayLists ();
263  FinishView ();
264  }
265  }
266  }
267 
268  if (isRecording()) {
269  savePPMToTemp();
270  }
271 
272 #ifdef G4DEBUG_VIS_OGL
273  printf("G4OpenGLStoredQtViewer::ComputeView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",getWinWidth(), getWinHeight());
274 #endif
275  fHasToRepaint = true;
276 }
277 
278 
282 void G4OpenGLStoredQtViewer::resizeGL(
283  int aWidth
284  ,int aHeight)
285 {
286  // Set new size, it will be update when next Repaint()->SetView() called
287  if ((aWidth > 0) && (aHeight > 0)) {
288  ResizeWindow(aWidth,aHeight);
289  fHasToRepaint = sizeHasChanged();
290  }
291 }
292 
293 
294 // We have to get several case :
295 // - Only activate the windows (mouse click for example) -> Do not redraw
296 // - resize window -> redraw
297 // - try to avoid recompute everything if we do not rescale picture (side is the same)
298 
299 void G4OpenGLStoredQtViewer::paintGL()
300 {
301  updateToolbarAndMouseContextMenu();
302 
303 #ifdef G4DEBUG_VIS_OGL
304  printf("G4OpenGLStoredQtViewer::paintGL \n");
305 #endif
306  if (fPaintEventLock) {
307 // return ;
308  }
309  fPaintEventLock = true;
310  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
311  return;
312  }
313 
314 #ifdef G4DEBUG_VIS_OGL
315  printf("G4OpenGLStoredQtViewer::paintGL ready:%d fHasTo:%d??\n",fReadyToPaint,fHasToRepaint);
316 #endif
317  if (!fQGLWidgetInitialiseCompleted) {
318  fPaintEventLock = false;
319  return;
320  }
321  // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
322  // WHEN CLICK ON THE FRAME FOR EXAMPLE
323  // EXECEPT WHEN MOUSE MOVE EVENT
324  if ( !fHasToRepaint) {
325  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
326  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
327  int sw = 0;
328  int sh = 0;
329  if (!isMaximized() && !isFullScreen()) {
330  sw = normalGeometry().width();
331  sh = normalGeometry().height();
332  } else {
333  sw = frameGeometry().width();
334  sh = frameGeometry().height();
335  }
336  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
337  return;
338  }
339  }
340 #ifdef G4DEBUG_VIS_OGL
341  printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV ready %d\n",fReadyToPaint);
342 #endif
343  // Ensure that we really draw the BACK buffer
344  glDrawBuffer (GL_BACK);
345 
346  SetView();
347 
348  ClearView (); //ok, put the background correct
349  ComputeView();
350 
351  fHasToRepaint = false;
352 
353 #ifdef G4DEBUG_VIS_OGL
354  printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ready %d\n",fReadyToPaint);
355 #endif
356  fPaintEventLock = false;
357 }
358 
359 void G4OpenGLStoredQtViewer::paintEvent(QPaintEvent *) {
360  if (! fQGLWidgetInitialiseCompleted) {
361  return;
362  }
363  if ( fHasToRepaint) {
364  // Will really update the widget by calling CGLFlushDrawable
365  // The widget's rendering context will become the current context and initializeGL()
366  // will be called if it hasn't already been called.
367  // Copies the back buffer of a double-buffered context to the front buffer.
368  updateGL();
369  }
370 }
371 
372 void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
373 {
374  G4MousePressEvent(event);
375 }
376 
377 void G4OpenGLStoredQtViewer::keyPressEvent (QKeyEvent * event)
378 {
379  G4keyPressEvent(event);
380 }
381 
382 void G4OpenGLStoredQtViewer::keyReleaseEvent (QKeyEvent * event)
383 {
384  G4keyReleaseEvent(event);
385 }
386 
387 void G4OpenGLStoredQtViewer::wheelEvent (QWheelEvent * event)
388 {
389  G4wheelEvent(event);
390 }
391 
392 void G4OpenGLStoredQtViewer::showEvent (QShowEvent *)
393 {
394  fHasToRepaint = true;
395 }
396 
401 void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *)
402 {
403  G4MouseDoubleClickEvent();
404 }
405 
406 void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
407 {
408  G4MouseReleaseEvent(event);
409 }
410 
411 void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
412 {
413  G4MouseMoveEvent(event);
414 }
415 
416 
417 void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
418 {
419  G4manageContextMenuEvent(e);
420 }
421 
422 void G4OpenGLStoredQtViewer::updateQWidget() {
423  if (fUpdateGLLock) {
424  return;
425  }
426  fUpdateGLLock = true;
427  fHasToRepaint= true;
428  // Will really update the widget by calling CGLFlushDrawable
429  // The widget's rendering context will become the current context and initializeGL()
430  // will be called if it hasn't already been called.
431  // Copies the back buffer of a double-buffered context to the front buffer.
432  updateGL();
433  updateSceneTreeComponentTreeWidgetInfos();
434  fHasToRepaint= false;
435  fUpdateGLLock = false;
436 }
437 
438 void G4OpenGLStoredQtViewer::ShowView (
439 )
440 {
443  // Some X servers fail to draw all trajectories, particularly Mac
444  // XQuartz. Revisit this at a future date. Meanwhile, issue an
445  // extra...
446  ClearView();
447  DrawView();
448  activateWindow();
449  // glFlush(); // NO NEED and as drawView will already cause a flush
450  // that could do a double flush
451 
452 }
453 
454 
455 void G4OpenGLStoredQtViewer::DisplayTimePOColourModification (
456 G4Colour& c,
457 size_t poIndex) {
458  c = getColorForPoIndex(poIndex);
459 }
460 
461 #endif
const G4Colour & GetBackgroundColour() const
G4double GetVisibleDensity() const
G4bool IsCullingInvisible() const
G4double GetExplodeFactor() const
G4String name
Definition: TRTMaterials.hh:40
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition: G4Colour.cc:126
const G4Colour & GetColour() const
G4bool IsDensityCulling() const
G4bool IsAuxEdgeVisible() const
bool G4bool
Definition: G4Types.hh:79
G4bool IsCullingCovered() const
const G4VisAttributes * GetDefaultTextVisAttributes() const
G4bool IsExplode() const
G4bool IsSection() const
G4bool IsCutaway() const
G4int GetNoOfSides() const
DrawingStyle GetDrawingStyle() const
G4bool IsPicking() const
const G4VisAttributes * GetDefaultVisAttributes() const
G4bool IsCulling() const
G4int G4GetThreadId()
Definition: G4Threading.cc:127