Geant4  10.03.p03
 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: G4OpenGLStoredQtViewer.cc 101714 2016-11-22 08:53:13Z 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  fQGLWidgetInitialiseCompleted = false;
56 
57  // 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.
58  QGLWidget::setAttribute (Qt::WA_NoSystemBackground);
59 
60  setFocusPolicy(Qt::StrongFocus); // enable keybord events
61  fHasToRepaint = false;
62  fPaintEventLock = false;
63  fUpdateGLLock = false;
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 
78  fQGLWidgetInitialiseCompleted = false;
79  CreateMainWindow (this,QString(GetName()));
80 
81  glDrawBuffer (GL_BACK);
82 
83  // set the good tab active
84  if (QGLWidget::parentWidget()) {
85  QTabWidget *parentTab = dynamic_cast<QTabWidget*> (QGLWidget::parentWidget()->parent()) ;
86  if (parentTab) {
87  parentTab->setCurrentIndex(parentTab->count()-1);
88  }
89  }
90 
91  fQGLWidgetInitialiseCompleted = true;
92 }
93 
94 void G4OpenGLStoredQtViewer::initializeGL () {
95 
96  InitializeGLView ();
97 
98  if (fSceneHandler.GetScene() == 0) {
99  fHasToRepaint =false;
100  } else {
101  fHasToRepaint =true;
102  }
103 
104  // Set the component visible
105  // setVisible(true) ;
106 
107 }
108 
109 G4bool G4OpenGLStoredQtViewer::CompareForKernelVisit(G4ViewParameters& lastVP)
110 {
111  // Identical to G4OpenGLStoredViewer::CompareForKernelVisit except
112  // for checking of VisAttributesModifiers, because
113  // G4OpenGLStoredQtViewer keeps track of its own touchable
114  // modifiers (fTreeItemModels, etc.).
115  if (
116  (lastVP.GetDrawingStyle () != fVP.GetDrawingStyle ()) ||
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.IsSection () != fVP.IsSection ()) ||
123  // Section (DCUT) implemented locally. But still need to visit
124  // kernel if status changes so that back plane culling can be
125  // switched.
126  (lastVP.IsCutaway () != fVP.IsCutaway ()) ||
127  // Cutaways implemented locally. But still need to visit kernel
128  // if status changes so that back plane culling can be switched.
129  (lastVP.IsExplode () != fVP.IsExplode ()) ||
130  (lastVP.GetNoOfSides () != fVP.GetNoOfSides ()) ||
131  (lastVP.GetDefaultVisAttributes()->GetColour() !=
132  fVP.GetDefaultVisAttributes()->GetColour()) ||
133  (lastVP.GetDefaultTextVisAttributes()->GetColour() !=
134  fVP.GetDefaultTextVisAttributes()->GetColour()) ||
135  (lastVP.GetBackgroundColour ()!= fVP.GetBackgroundColour ())||
136  (lastVP.IsPicking () != fVP.IsPicking ()))
137  return true;
138 
139  // Don't check VisAttributesModifiers if this comparison has been
140  // initiated by a mouse interaction on the scene tree.
141  if (fMouseOnSceneTree) {
142  // Reset the flag.
143  fMouseOnSceneTree = false;
144  } else {
145  // Not initiated by a mouse so compare for kernel visit.
146  if (lastVP.GetVisAttributesModifiers() != fVP.GetVisAttributesModifiers()) {
147  return true;
148  }
149  }
150 
151  if (lastVP.IsDensityCulling () &&
152  (lastVP.GetVisibleDensity () != fVP.GetVisibleDensity ()))
153  return true;
154 
155  /**************************************************************
156  Section (DCUT) implemented locally. No need to visit kernel if
157  section plane itself changes.
158  if (lastVP.IsSection () &&
159  (lastVP.GetSectionPlane () != fVP.GetSectionPlane ()))
160  return true;
161  ***************************************************************/
162 
163  /**************************************************************
164  Cutaways implemented locally. No need to visit kernel if cutaway
165  planes themselves change.
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.IsExplode () &&
176  (lastVP.GetExplodeFactor () != fVP.GetExplodeFactor ()))
177  return true;
178 
179  return false;
180 }
181 
182 G4bool G4OpenGLStoredQtViewer::POSelected(size_t POListIndex)
183 {
184  return isTouchableVisible(POListIndex);
185 }
186 
187 G4bool G4OpenGLStoredQtViewer::TOSelected(size_t)
188 {
189  return true;
190 }
191 
192 void G4OpenGLStoredQtViewer::DrawView () {
193  updateQWidget();
194 }
195 
196 void G4OpenGLStoredQtViewer::ComputeView () {
197 
198  makeCurrent();
199  G4ViewParameters::DrawingStyle dstyle = GetViewParameters().GetDrawingStyle();
200 
201  //Make sure current viewer is attached and clean...
202 
203  //See if things have changed from last time and remake if necessary...
204  // The fNeedKernelVisit flag might have been set by the user in
205  // /vis/viewer/rebuild, but if not, make decision and set flag only
206  // if necessary...
207  if (!fNeedKernelVisit) {
208  KernelVisitDecision ();
209  }
210  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
211  ProcessView ();
212 
213  if (kernelVisitWasNeeded) {
214  displaySceneTreeComponent();
215  }
216 
217  if(dstyle!=G4ViewParameters::hlr &&
218  haloing_enabled) {
219 
220  HaloingFirstPass ();
221  DrawDisplayLists ();
222  glFlush ();
223 
224  HaloingSecondPass ();
225 
226  DrawDisplayLists ();
227  FinishView ();
228 
229  } else {
230 
231  // If kernel visit was needed, drawing and FinishView will already
232  // have been done, so...
233  if (!kernelVisitWasNeeded) {
234  DrawDisplayLists ();
235  FinishView ();
236  } else {
237  // However, union cutaways are implemented in DrawDisplayLists, so make
238  // an extra pass...
239  if (fVP.IsCutaway() &&
240  fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
241  ClearView();
242  DrawDisplayLists ();
243  FinishView ();
244  } else { // ADD TO AVOID KernelVisit=1 and nothing to display
245  DrawDisplayLists ();
246  FinishView ();
247  }
248  }
249  }
250 
251  if (isRecording()) {
252  savePPMToTemp();
253  }
254 
255  fHasToRepaint = true;
256 }
257 
258 
262 void G4OpenGLStoredQtViewer::resizeGL(
263  int aWidth
264  ,int aHeight)
265 {
266  // Set new size, it will be update when next Repaint()->SetView() called
267  if ((aWidth > 0) && (aHeight > 0)) {
268  ResizeWindow(aWidth,aHeight);
269  fHasToRepaint = sizeHasChanged();
270  }
271 }
272 
273 
274 // We have to get several case :
275 // - Only activate the windows (mouse click for example) -> Do not redraw
276 // - resize window -> redraw
277 // - try to avoid recompute everything if we do not rescale picture (side is the same)
278 
279 void G4OpenGLStoredQtViewer::paintGL()
280 {
281  updateToolbarAndMouseContextMenu();
282 
283  if (fPaintEventLock) {
284 // return ;
285  }
286  fPaintEventLock = true;
287  if ((getWinWidth() == 0) && (getWinHeight() == 0)) {
288  return;
289  }
290 
291  if (!fQGLWidgetInitialiseCompleted) {
292  fPaintEventLock = false;
293  return;
294  }
295  // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
296  // WHEN CLICK ON THE FRAME FOR EXAMPLE
297  // EXECEPT WHEN MOUSE MOVE EVENT
298  if ( !fHasToRepaint) {
299  // L. Garnier : Trap to get the size with mac OSX 10.6 and Qt 4.6(devel)
300  // Tested on Qt4.5 on mac, 4.4 on windows, 4.5 on unbuntu
301  int sw = 0;
302  int sh = 0;
303  if (!isMaximized() && !isFullScreen()) {
304  sw = normalGeometry().width();
305  sh = normalGeometry().height();
306  } else {
307  sw = frameGeometry().width();
308  sh = frameGeometry().height();
309  }
310  if ((getWinWidth() == (unsigned int)sw) &&(getWinHeight() == (unsigned int)sh)) {
311  return;
312  }
313  }
314  // Ensure that we really draw the BACK buffer
315  glDrawBuffer (GL_BACK);
316 
317  SetView();
318 
319  ClearView (); //ok, put the background correct
320  ComputeView();
321 
322  fHasToRepaint = false;
323 
324  fPaintEventLock = false;
325 }
326 
327 void G4OpenGLStoredQtViewer::paintEvent(QPaintEvent *) {
328  if (! fQGLWidgetInitialiseCompleted) {
329  return;
330  }
331  // Force a repaint next time if the FRAMEBUFFER is not READY
332  fHasToRepaint = isFramebufferReady();
333  if ( fHasToRepaint) {
334  // Will really update the widget by calling CGLFlushDrawable
335  // The widget's rendering context will become the current context and initializeGL()
336  // will be called if it hasn't already been called.
337  // Copies the back buffer of a double-buffered context to the front buffer.
338  updateGL();
339  }
340 }
341 
342 void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
343 {
344  G4MousePressEvent(event);
345 }
346 
347 void G4OpenGLStoredQtViewer::keyPressEvent (QKeyEvent * event)
348 {
349  G4keyPressEvent(event);
350 }
351 
352 void G4OpenGLStoredQtViewer::keyReleaseEvent (QKeyEvent * event)
353 {
354  G4keyReleaseEvent(event);
355 }
356 
357 void G4OpenGLStoredQtViewer::wheelEvent (QWheelEvent * event)
358 {
359  G4wheelEvent(event);
360 }
361 
362 void G4OpenGLStoredQtViewer::showEvent (QShowEvent *)
363 {
364  if (fQGLWidgetInitialiseCompleted) {
365  fHasToRepaint = true;
366  }
367 }
368 
373 void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *)
374 {
375  G4MouseDoubleClickEvent();
376 }
377 
378 void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
379 {
380  G4MouseReleaseEvent(event);
381 }
382 
383 void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
384 {
385  G4MouseMoveEvent(event);
386 }
387 
388 
389 void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
390 {
391  G4manageContextMenuEvent(e);
392 }
393 
394 void G4OpenGLStoredQtViewer::updateQWidget() {
395  if (fUpdateGLLock) {
396  return;
397  }
398 
399  if (! isCurrentWidget()){
400  return;
401  }
402 
403  fUpdateGLLock = true;
404  fHasToRepaint= true;
405  // Will really update the widget by calling CGLFlushDrawable
406  // The widget's rendering context will become the current context and initializeGL()
407  // will be called if it hasn't already been called.
408  // Copies the back buffer of a double-buffered context to the front buffer.
409  repaint(); // will read scene tree state
410  updateViewerPropertiesTableWidget();
411  updateSceneTreeWidget();
412  fUpdateGLLock = false;
413 }
414 
415 void G4OpenGLStoredQtViewer::ShowView (
416 )
417 {
420  // glFlush (); // Tentativley offered by JA 29/04/16.
421 
422  // Some X servers fail to draw all trajectories, particularly Mac
423  // XQuartz. Revisit this at a future date. Meanwhile, issue an
424  // extra...
425  ClearView(); // Necessary? JA 29/04/16
426  DrawView(); // Necessary? JA 29/04/16
427  activateWindow();
428  // glFlush(); // NO NEED and as drawView will already cause a flush
429  // that could do a double flush
430 
431 }
432 
433 
434 void G4OpenGLStoredQtViewer::DisplayTimePOColourModification (
435 G4Colour& c,
436 size_t poIndex) {
437  c = getColorForPoIndex(poIndex);
438 }
439 
440 #endif
const XML_Char * name
Definition: expat.h:151
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 std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() 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
tuple c
Definition: test.py:13
G4bool IsCulling() const