Geant4  10.02.p01
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 94206 2015-11-09 08:11:59Z 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 #include <qtabwidget.h>
45 
46 G4OpenGLStoredQtViewer::G4OpenGLStoredQtViewer
47 (G4OpenGLStoredSceneHandler& sceneHandler,
48  const G4String& name):
49  G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
50  G4OpenGLViewer (sceneHandler),
51  G4OpenGLQtViewer (sceneHandler),
52  G4OpenGLStoredViewer (sceneHandler), // FIXME : gerer le pb du parent !
53  QGLWidget()
54 {
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  resize(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
65 
66  if (fViewId < 0) return; // In case error in base class instantiation.
67 }
68 
69 G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
70  makeCurrent();
71  // this is connect to the Dialog for deleting it properly
72  // when close event.
73  // ((QDialog*)window())->reject();
74 }
75 
76 void G4OpenGLStoredQtViewer::Initialise() {
77  makeCurrent();
78 
79  fQGLWidgetInitialiseCompleted = false;
80  CreateMainWindow (this,QString(GetName()));
81 
82  glDrawBuffer (GL_BACK);
83 
84  // set the good tab active
85  if (QGLWidget::parentWidget()) {
86  QTabWidget *parentTab = dynamic_cast<QTabWidget*> (QGLWidget::parentWidget()->parent()) ;
87  if (parentTab) {
88  parentTab->setCurrentIndex(parentTab->count()-1);
89  }
90  }
91 
92  fQGLWidgetInitialiseCompleted = true;
93 }
94 
95 void G4OpenGLStoredQtViewer::initializeGL () {
96 
97  InitializeGLView ();
98 
99  if (fSceneHandler.GetScene() == 0) {
100  fHasToRepaint =false;
101  } else {
102  fHasToRepaint =true;
103  }
104 
105  // Set the component visible
106  // setVisible(true) ;
107 
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  makeCurrent();
192  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
193 
194  //Make sure current viewer is attached and clean...
195 
196  //See if things have changed from last time and remake if necessary...
197  // The fNeedKernelVisit flag might have been set by the user in
198  // /vis/viewer/rebuild, but if not, make decision and set flag only
199  // if necessary...
200  if (!fNeedKernelVisit) {
201  KernelVisitDecision ();
202  }
203  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
204  ProcessView ();
205 
206  if (fNeedKernelVisit) {
207  displaySceneTreeComponent();
208  }
209  if(dstyle!=G4ViewParameters::hlr &&
210  haloing_enabled) {
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  DrawDisplayLists ();
227  FinishView ();
228  } else {
229  // However, union cutaways are implemented in DrawDisplayLists, so make
230  // an extra pass...
231  if (fVP.IsCutaway() &&
232  fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
233  ClearView();
234  DrawDisplayLists ();
235  FinishView ();
236  } else { // ADD TO AVOID KernelVisit=1 and nothing to display
237  DrawDisplayLists ();
238  FinishView ();
239  }
240  }
241  }
242 
243  if (isRecording()) {
244  savePPMToTemp();
245  }
246 
247  fHasToRepaint = true;
248 }
249 
250 
254 void G4OpenGLStoredQtViewer::resizeGL(
255  int aWidth
256  ,int aHeight)
257 {
258  // Set new size, it will be update when next Repaint()->SetView() called
259  if ((aWidth > 0) && (aHeight > 0)) {
260  ResizeWindow(aWidth,aHeight);
261  fHasToRepaint = sizeHasChanged();
262  }
263 }
264 
265 
266 // We have to get several case :
267 // - Only activate the windows (mouse click for example) -> Do not redraw
268 // - resize window -> redraw
269 // - try to avoid recompute everything if we do not rescale picture (side is the same)
270 
271 void G4OpenGLStoredQtViewer::paintGL()
272 {
273  updateToolbarAndMouseContextMenu();
274 
275  if (fPaintEventLock) {
276 // return ;
277  }
278  fPaintEventLock = true;
279  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
280  return;
281  }
282 
283  if (!fQGLWidgetInitialiseCompleted) {
284  fPaintEventLock = false;
285  return;
286  }
287  // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
288  // WHEN CLICK ON THE FRAME FOR EXAMPLE
289  // EXECEPT WHEN MOUSE MOVE EVENT
290  if ( !fHasToRepaint) {
291  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
292  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
293  int sw = 0;
294  int sh = 0;
295  if (!isMaximized() && !isFullScreen()) {
296  sw = normalGeometry().width();
297  sh = normalGeometry().height();
298  } else {
299  sw = frameGeometry().width();
300  sh = frameGeometry().height();
301  }
302  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
303  return;
304  }
305  }
306  // Ensure that we really draw the BACK buffer
307  glDrawBuffer (GL_BACK);
308 
309  SetView();
310 
311  ClearView (); //ok, put the background correct
312  ComputeView();
313 
314  fHasToRepaint = false;
315 
316  fPaintEventLock = false;
317 }
318 
319 void G4OpenGLStoredQtViewer::paintEvent(QPaintEvent *) {
320  if (! fQGLWidgetInitialiseCompleted) {
321  return;
322  }
323  if ( fHasToRepaint) {
324  // Will really update the widget by calling CGLFlushDrawable
325  // The widget's rendering context will become the current context and initializeGL()
326  // will be called if it hasn't already been called.
327  // Copies the back buffer of a double-buffered context to the front buffer.
328  updateGL();
329  }
330 }
331 
332 void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
333 {
334  G4MousePressEvent(event);
335 }
336 
337 void G4OpenGLStoredQtViewer::keyPressEvent (QKeyEvent * event)
338 {
339  G4keyPressEvent(event);
340 }
341 
342 void G4OpenGLStoredQtViewer::keyReleaseEvent (QKeyEvent * event)
343 {
344  G4keyReleaseEvent(event);
345 }
346 
347 void G4OpenGLStoredQtViewer::wheelEvent (QWheelEvent * event)
348 {
349  G4wheelEvent(event);
350 }
351 
352 void G4OpenGLStoredQtViewer::showEvent (QShowEvent *)
353 {
354  if (fQGLWidgetInitialiseCompleted) {
355  fHasToRepaint = true;
356  }
357 }
358 
363 void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *)
364 {
365  G4MouseDoubleClickEvent();
366 }
367 
368 void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
369 {
370  G4MouseReleaseEvent(event);
371 }
372 
373 void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
374 {
375  G4MouseMoveEvent(event);
376 }
377 
378 
379 void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
380 {
381  G4manageContextMenuEvent(e);
382 }
383 
384 void G4OpenGLStoredQtViewer::updateQWidget() {
385  if (fUpdateGLLock) {
386  return;
387  }
388  fUpdateGLLock = true;
389  fHasToRepaint= true;
390  // Will really update the widget by calling CGLFlushDrawable
391  // The widget's rendering context will become the current context and initializeGL()
392  // will be called if it hasn't already been called.
393  // Copies the back buffer of a double-buffered context to the front buffer.
394  updateGL();
395  updateSceneTreeComponentTreeWidgetInfos();
396  fHasToRepaint= false;
397  fUpdateGLLock = false;
398 }
399 
400 void G4OpenGLStoredQtViewer::ShowView (
401 )
402 {
405  // Some X servers fail to draw all trajectories, particularly Mac
406  // XQuartz. Revisit this at a future date. Meanwhile, issue an
407  // extra...
408  ClearView();
409  DrawView();
410  activateWindow();
411  // glFlush(); // NO NEED and as drawView will already cause a flush
412  // that could do a double flush
413 
414 }
415 
416 
417 void G4OpenGLStoredQtViewer::DisplayTimePOColourModification (
418 G4Colour& c,
419 size_t poIndex) {
420  c = getColorForPoIndex(poIndex);
421 }
422 
423 #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