source: trunk/source/visualization/OpenGL/src/G4OpenGLStoredQtViewer.cc @ 862

Last change on this file since 862 was 858, checked in by garnier, 16 years ago

protection against no scene + Qapp could be now get on external app

  • Property svn:mime-type set to text/cpp
File size: 10.9 KB
Line 
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,v 1.15 2008/06/20 13:55:06 lgarnier Exp $
28// GEANT4 tag $Name: HEAD $
29//
30//
31// Class G4OpenGLStoredQtViewer : a class derived from G4OpenGLQtViewer and
32//                                G4OpenGLStoredViewer.
33
34#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
35
36//#define GEANT4_QT_DEBUG
37
38#include "G4OpenGLStoredQtViewer.hh"
39
40#include "G4ios.hh"
41
42//#include <qmouseevent.h>
43#include <qevent.h> // include <qcontextmenuevent.h>
44
45G4OpenGLStoredQtViewer::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{
53#if QT_VERSION < 0x040000
54  setFocusPolicy(QWidget::StrongFocus); // enable keybord events
55#else
56  setFocusPolicy(Qt::StrongFocus); // enable keybord events
57#endif
58  nbPaint =0;
59  hasToRepaint =false;
60
61  // if no scene...
62  if (fSceneHandler.GetScene() == 0) {
63    G4cerr << "G4OpenGLStoredQtViewer: Creating a Viewer without a scene is not allowed. \nPlease use /vis/scene/create before /vis/open/.... "
64           << G4endl;
65    return;
66  }
67  if (fViewId < 0) return;  // In case error in base class instantiation.
68}
69
70G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
71#ifdef GEANT4_QT_DEBUG
72  printf("GLWidget::~GLWidget \n");
73#endif
74  makeCurrent();
75  // this is connect to the Dialog for deleting it properly
76  // when close event.
77  //   ((QDialog*)window())->reject();
78#ifdef GEANT4_QT_DEBUG
79  printf("GLWidget::~GLWidget END\n");
80#endif
81}
82
83void G4OpenGLStoredQtViewer::Initialise() {
84#ifdef GEANT4_QT_DEBUG
85  printf("G4OpenGLStoredQtViewer::Initialise 1\n");
86#endif
87  readyToPaint = false;
88  CreateGLQtContext ();
89#ifdef GEANT4_QT_DEBUG
90  printf("G4OpenGLStoredQtViewer::Initialise () 2\n");
91#endif
92  CreateMainWindow (this);
93#ifdef GEANT4_QT_DEBUG
94  printf("G4OpenGLStoredQtViewer::Initialise () 3\n");
95#endif
96  CreateFontLists ();  // FIXME Does nothing!
97 
98#ifdef GEANT4_QT_DEBUG
99  printf("readyToPaint = true \n");
100#endif
101  readyToPaint = true;
102}
103
104void G4OpenGLStoredQtViewer::initializeGL () {
105
106  InitializeGLView ();
107
108#ifdef GEANT4_QT_DEBUG
109  printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
110#endif
111
112  // clear the buffers and window.
113  ClearView ();
114  //   printf("G4OpenGLStoredQtViewer::InitialiseGL () 2\n");
115  FinishView ();
116   
117  glDepthFunc (GL_LEQUAL);
118  glDepthMask (GL_TRUE);
119
120  hasToRepaint =true;
121
122#ifdef GEANT4_QT_DEBUG
123  printf("G4OpenGLStoredQtViewer::InitialiseGL  -------------------------------------------------------------------------------------\n");
124#endif
125}
126
127
128void G4OpenGLStoredQtViewer::DrawView () {
129
130#ifdef GEANT4_QT_DEBUG
131  printf("G4OpenGLQtViewer::setupViewport\n");
132  printf("G4OpenGLStoredQtViewer::DrawView %d %d   VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",WinSize_x, WinSize_y);
133#endif
134  G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
135
136  //Make sure current viewer is attached and clean...
137  //Qt version needed
138  //   glViewport (0, 0, WinSize_x, WinSize_y);
139
140  //See if things have changed from last time and remake if necessary...
141  // The fNeedKernelVisit flag might have been set by the user in
142  // /vis/viewer/rebuild, but if not, make decision and set flag only
143  // if necessary...
144  if (!fNeedKernelVisit)
145
146    if (!fNeedKernelVisit) KernelVisitDecision ();
147   
148  G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
149  ProcessView ();
150   
151
152  if(style!=G4ViewParameters::hlr &&
153     haloing_enabled) {
154#ifdef GEANT4_QT_DEBUG
155    printf("G4OpenGLStoredQtViewer::DrawView DANS LE IF\n");
156#endif
157
158    HaloingFirstPass ();
159    DrawDisplayLists ();
160    glFlush ();
161
162    HaloingSecondPass ();
163
164    DrawDisplayLists ();
165    FinishView ();
166
167  } else {
168     
169    // If kernel visit was needed, drawing and FinishView will already
170    // have been done, so...
171    if (!kernelVisitWasNeeded) {
172#ifdef GEANT4_QT_DEBUG
173      printf("**************************  G4OpenGLStoredQtViewer::DrawView Don't need kernel Visit \n");
174#endif
175      DrawDisplayLists ();
176      FinishView ();
177    } else {
178#ifdef GEANT4_QT_DEBUG
179      printf("**************************  G4OpenGLStoredQtViewer::DrawView need kernel Visit \n");
180#endif
181      // However, union cutaways are implemented in DrawDisplayLists, so make
182      // an extra pass...
183      if (fVP.IsCutaway() &&
184          fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
185        ClearView();
186        DrawDisplayLists ();
187        FinishView ();
188#ifdef GEANT4_QT_DEBUG
189        printf("***************************  CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
190#endif
191      } else { // ADD TO AVOID KernelVisit=1 and nothing to display
192        DrawDisplayLists ();
193        FinishView ();
194      }
195    }
196  }
197
198  if (isRecording()) {
199    savePPMToTemp();
200  }
201
202#ifdef GEANT4_QT_DEBUG
203  printf("G4OpenGLStoredQtViewer::DrawView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",WinSize_x, WinSize_y);
204#endif
205  hasToRepaint =true;
206}
207
208
209//////////////////////////////////////////////////////////////////////////////
210void G4OpenGLStoredQtViewer::FinishView (
211)
212//////////////////////////////////////////////////////////////////////////////
213//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
214{
215  glFlush ();
216  swapBuffers ();
217}
218
219
220/**
221   - Lors du resize de la fenetre, on doit non pas redessiner le detecteur, mais aussi les evenements
222*/
223void G4OpenGLStoredQtViewer::resizeGL(
224                                      int aWidth
225                                      ,int aHeight)
226
227  setupViewport(aWidth,aHeight);
228
229  if (((WinSize_x != (G4int)aWidth)) || (WinSize_y != (G4int) aHeight)) {
230    hasToRepaint =true;
231  }
232  WinSize_x = (G4int) aWidth;
233  WinSize_y = (G4int) aHeight;
234 
235#ifdef GEANT4_QT_DEBUG
236  printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
237#endif
238}
239
240
241/**
242    @see :
243*/
244
245void G4OpenGLStoredQtViewer::paintGL()
246{
247  if (!readyToPaint) {
248#ifdef GEANT4_QT_DEBUG
249    printf("G4OpenGLStoredQtViewer::paintGL ============  Not ready %d\n",readyToPaint);
250#endif
251    readyToPaint= true;
252    return;
253  }
254  // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
255  //    WHEN CLICK ON THE FRAME FOR EXAMPLE
256  //    EXECEPT WHEN MOUSE MOVE EVENT
257  if ( !hasToRepaint) {
258    if (((WinSize_x == (G4int)width())) &&(WinSize_y == (G4int) height())) {
259#ifdef GEANT4_QT_DEBUG
260      printf("G4OpenGLStoredQtViewer::paintGL ============  Dont repaint\n");
261#endif
262      return;
263    }
264  }
265  nbPaint++;
266#ifdef GEANT4_QT_DEBUG
267  printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
268#endif
269  WinSize_x = (G4int) width();
270  WinSize_y = (G4int) height();
271     
272  setupViewport(width(),height());
273  //     glViewport (0, 0, width(), height());
274  //   glLoadIdentity();
275     
276     
277  SetView();
278     
279  //   //  printf("before ClearView\n");
280#ifdef GEANT4_QT_DEBUG
281  printf("    ClearView\n");
282#endif
283     
284  ClearView (); //ok, put the background correct
285  DrawView();
286     
287  hasToRepaint =false;
288     
289#ifdef GEANT4_QT_DEBUG
290  printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
291#endif
292}
293
294void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
295{
296#if QT_VERSION < 0x040000
297  if ((event->button() & Qt::LeftButton)
298      && !((event->state() & Qt::ShiftButton)
299           || (event->state() & Qt::ControlButton)
300           || (event->state() & Qt::AltButton)
301           || (event->state() & Qt::MetaButton))) {
302#else
303  if ((event->buttons() & Qt::LeftButton)
304      && !((event->modifiers() & Qt::ShiftModifier)
305           || (event->modifiers() & Qt::ControlModifier)
306           || (event->modifiers() & Qt::AltModifier)
307           || (event->modifiers() & Qt::MetaModifier))) {
308#endif
309#ifdef GEANT4_QT_DEBUG
310    printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
311#endif
312    setMouseTracking(true);
313    G4MousePressEvent(event->pos());
314  }
315}
316
317void G4OpenGLStoredQtViewer::keyPressEvent (QKeyEvent * event)
318{
319  G4keyPressEvent(event);
320}
321
322void G4OpenGLStoredQtViewer::wheelEvent (QWheelEvent * event)
323{
324  G4wheelEvent(event);
325}
326
327/**
328 * This function was build in order to make a zoom on double clic event.
329 * It was think to build a rubberband on the zoom area, but never work fine
330 */
331void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *event)
332{
333#ifdef GEANT4_QT_DEBUG
334  printf("G4OpenGLStoredQtViewer::mouseDoubleClickEvent\n");
335#endif
336  setMouseTracking(true);
337  //   glBufferImage = grabFrameBuffer().convertToFormat(QImage::Format_ARGB32);//_Premultiplied); 
338}
339
340void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
341{
342  G4MouseReleaseEvent();
343#ifdef GEANT4_QT_DEBUG
344  printf("G4OpenGLStoredQtViewer::mouseReleaseEvent ================\n");
345#endif
346  setMouseTracking(false);
347}
348
349void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
350{
351 
352#if QT_VERSION < 0x040000
353  G4MouseMoveEvent(event->x(),event->y(),event->state());
354#else
355  G4MouseMoveEvent(event->x(),event->y(),event->buttons());
356#endif
357}
358
359
360void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
361{
362#ifdef GEANT4_QT_DEBUG
363  printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
364#endif
365  manageContextMenuEvent(e);
366}
367
368void G4OpenGLStoredQtViewer::updateQWidget() {
369  hasToRepaint= true;
370  updateGL();
371  hasToRepaint= false;
372}
373
374
375#endif
Note: See TracBrowser for help on using the repository browser.