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