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

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

add picking for Qt

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