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