source: trunk/geant4/visualization/OpenGL/src/G4OpenGLStoredQtViewer.cc @ 675

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

marche des que l on a lance un run : dessine un rubber band. Pb de alpha channel soupsonne

  • Property svn:mime-type set to text/cpp
File size: 15.4 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//
[635]27// $Id: G4OpenGLStoredQtViewer.cc,v 1.6 2007/11/15 18:24:28 lgarnier Exp $
[593]28// GEANT4 tag $Name:  $
[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
[635]36#define GEANT4_QT_DEBUG
[610]37
[533]38#include "G4OpenGLStoredQtViewer.hh"
[635]39#include "G4VisManager.hh"
[531]40
41#include "G4ios.hh"
42
[595]43//#include <qmouseevent.h>
44#include <qevent.h> // include <qcontextmenuevent.h>
[673]45#include <qpainter.h>
[593]46
[533]47G4OpenGLStoredQtViewer::G4OpenGLStoredQtViewer
[531]48(G4OpenGLStoredSceneHandler& sceneHandler,
49 const G4String&  name):
[564]50 G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
51 G4OpenGLViewer (sceneHandler),
52 G4OpenGLQtViewer (sceneHandler),
53 G4OpenGLStoredViewer (sceneHandler),
[675]54 QGLWidget(QGLFormat(QGL::SampleBuffers))             // FIXME : gerer le pb du parent !
[564]55 {
[673]56   zoomAction = false;
[564]57   nbPaint =0;
58   hasToRepaint =false;
[531]59  if (fViewId < 0) return;  // In case error in base class instantiation.
60}
61
[564]62G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
[608]63#ifdef GEANT4_QT_DEBUG
[564]64   printf("GLWidget::~GLWidget \n");
[608]65#endif
[579]66   makeCurrent();
67   // this is connect to the Dialog for deleting it properly
68   // when close event.
69   //   ((QDialog*)window())->reject();
[608]70#ifdef GEANT4_QT_DEBUG
[564]71   printf("GLWidget::~GLWidget END\n");
[608]72#endif
[564]73}
[531]74
[564]75void G4OpenGLStoredQtViewer::Initialise() {
[608]76#ifdef GEANT4_QT_DEBUG
[564]77   printf("GLWidget::Initialise \n");
[608]78#endif
[564]79   readyToPaint = false;
80   CreateGLQtContext ();
[608]81#ifdef GEANT4_QT_DEBUG
[564]82   printf("G4OpenGLStoredQtViewer::Initialise () 2\n");
[608]83#endif
[564]84  CreateMainWindow (this);
[608]85#ifdef GEANT4_QT_DEBUG
[564]86  printf("G4OpenGLStoredQtViewer::Initialise () 3\n");
[608]87#endif
[675]88  //  CreateFontLists ();  // FIXME Does nothing!
[564]89 
[608]90#ifdef GEANT4_QT_DEBUG
[564]91  printf("readyToPaint = true \n");
[608]92#endif
[564]93  readyToPaint = true;
94 
95  // First Draw
96  SetView();
[608]97#ifdef GEANT4_QT_DEBUG
[564]98  printf("    ClearView\n");
[608]99#endif
[564]100  ClearView (); //ok, put the background correct
101  ShowView();
102  FinishView();
[675]103#ifdef GEANT4_QT_DEBUG
104  printf("G4OpenGLStoredQtViewer::Initialise () 4\n");
105#endif
[564]106}
[531]107
[564]108void G4OpenGLStoredQtViewer::initializeGL () {
109
110   InitializeGLView ();
111
[608]112#ifdef GEANT4_QT_DEBUG
[564]113   printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
[608]114#endif
[564]115
116   // clear the buffers and window.
117   ClearView ();
118   //   printf("G4OpenGLStoredQtViewer::InitialiseGL () 2\n");
119   FinishView ();
120   
121   glDepthFunc (GL_LEQUAL);
122   glDepthMask (GL_TRUE);
123
124   hasToRepaint =true;
125
[608]126#ifdef GEANT4_QT_DEBUG
[564]127   printf("G4OpenGLStoredQtViewer::InitialiseGL  -------------------------------------------------------------------------------------\n");
[608]128#endif
[531]129}
130
[564]131
[533]132void G4OpenGLStoredQtViewer::DrawView () {
[608]133#ifdef GEANT4_QT_DEBUG
[564]134  printf("G4OpenGLStoredQtViewer::DrawView %d %d   VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",WinSize_x, WinSize_y);
[608]135#endif
[564]136   G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
[531]137
[564]138   //Make sure current viewer is attached and clean...
139   //Qt version needed
[652]140   //   glViewport (0, 0, WinSize_x, WinSize_y);
[531]141
[564]142   //See if things have changed from last time and remake if necessary...
143   // The fNeedKernelVisit flag might have been set by the user in
144   // /vis/viewer/rebuild, but if not, make decision and set flag only
145   // if necessary...
[565]146   if (!fNeedKernelVisit)
147
[564]148   if (!fNeedKernelVisit) KernelVisitDecision ();
[565]149   
[564]150   G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
151   ProcessView ();
152   
[531]153
[564]154   if(style!=G4ViewParameters::hlr &&
155      haloing_enabled) {
[608]156#ifdef GEANT4_QT_DEBUG
[564]157     printf("G4OpenGLStoredQtViewer::DrawView DANS LE IF\n");
[608]158#endif
[531]159
[564]160     HaloingFirstPass ();
161     DrawDisplayLists ();
162     glFlush ();
[531]163
[564]164     HaloingSecondPass ();
[531]165
[564]166     DrawDisplayLists ();
[565]167     FinishView ();
[531]168
[564]169   } else {
[608]170#ifdef GEANT4_QT_DEBUG
[565]171     printf("***************************  CASE 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]172#endif
[564]173     
174     // If kernel visit was needed, drawing and FinishView will already
175     // have been done, so...
176     if (!kernelVisitWasNeeded) {
[608]177#ifdef GEANT4_QT_DEBUG
[565]178       printf("***************************  CASE 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]179#endif
[564]180       DrawDisplayLists ();
[565]181       FinishView ();
[564]182     } else {
[608]183#ifdef GEANT4_QT_DEBUG
[565]184       printf("***************************  CASE 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]185#endif
[564]186       // However, union cutaways are implemented in DrawDisplayLists, so make
187       // an extra pass...
188       if (fVP.IsCutaway() &&
189           fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
190         ClearView();
191         DrawDisplayLists ();
[565]192         FinishView ();
[608]193#ifdef GEANT4_QT_DEBUG
[565]194         printf("***************************  CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]195#endif
[565]196       } else { // ADD TO AVOID KernelVisit=1 and nothing to display
197         DrawDisplayLists ();
198         FinishView ();
[564]199       }
200     }
201   }
202
[608]203#ifdef GEANT4_QT_DEBUG
[565]204   printf("G4OpenGLStoredQtViewer::DrawView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",WinSize_x, WinSize_y);
[608]205#endif
[564]206   hasToRepaint =true;
[531]207}
208
[564]209
[531]210//////////////////////////////////////////////////////////////////////////////
[533]211void G4OpenGLStoredQtViewer::FinishView (
[531]212)
213//////////////////////////////////////////////////////////////////////////////
214//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
215{
[608]216#ifdef GEANT4_QT_DEBUG
[564]217  printf("G4OpenGLStoredQtViewer::FinishView VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
[608]218#endif
[531]219
220  glFlush ();
[564]221  swapBuffers ();
[608]222#ifdef GEANT4_QT_DEBUG
[564]223  printf("G4OpenGLStoredQtViewer::FinishView ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]224#endif
[564]225
[531]226}
227
[564]228
229/**
230 - Lors du resize de la fenetre, on doit non pas redessiner le detecteur, mais aussi les evenements
231 */
232void G4OpenGLStoredQtViewer::resizeGL(
233 int aWidth
234,int aHeight)
235
[673]236  setupViewport(aWidth,aHeight);
[564]237
[673]238//   glViewport(0, 0, aWidth, aHeight);
239//   glMatrixMode(GL_PROJECTION);
240//   glMatrixMode(GL_MODELVIEW);
241
[564]242   if (((WinSize_x != (G4int)aWidth)) || (WinSize_y != (G4int) aHeight)) {
243     hasToRepaint =true;
244   }
245   WinSize_x = (G4int) aWidth;
246   WinSize_y = (G4int) aHeight;
247
[608]248#ifdef GEANT4_QT_DEBUG
[564]249  printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
[608]250#endif
[564]251}
252
253
[673]254/**
255@see :
256*/
[564]257
258void G4OpenGLStoredQtViewer::paintGL()
259 {
[673]260   if (zoomAction) {
[675]261     //     glEnable (GL_DEPTH_TEST);
262     //     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
263
264     setupViewport(width(),height());     
265     SetView();
266     ClearView (); //ok, put the background correct
267
[673]268     QPainter painter;
269     painter.begin(this);
[675]270     //      painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
271     //      painter.setCompositionMode(QPainter::CompositionMode_SourceOver); // diagonale en pointillé
272     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); // blanc
273     //     painter.setCompositionMode(QPainter::CompositionMode_Clear);// diagonale noire
274     //     painter.setCompositionMode(QPainter::CompositionMode_Source); // diagonale en pointillé
275     //     painter.setCompositionMode(QPainter::CompositionMode_Destination); // blanc
276     //     painter.setCompositionMode(QPainter::CompositionMode_SourceIn); // blanc ?
277     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); // blanc
278     //     painter.setCompositionMode(QPainter::CompositionMode_SourceOut); // digonale noire
279     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationOut); // diagonale noire
280     //     painter.setCompositionMode(QPainter::CompositionMode_SourceAtop); // diagonale noire
281     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop); // blanc
282     //     painter.setCompositionMode(QPainter::CompositionMode_Xor); // diagonale noire
[673]283     //     painter.setRenderHint(QPainter::Antialiasing);
[675]284     //         painter.setBackgroundMode(Qt::OpaqueMode);
285          painter.drawImage(0, 0, glBufferImage);
286     
287     //     painter.setRenderHint(QPainter::Antialiasing);
288     //      QColor bg = QColor(glBufferImage.pixel(0,0));
289     //      printf("%d+%d+%d+%d %d+%d+%d+%d",bg.red(),bg.green(),bg.blue(),bg.alpha(),qAlpha(glBufferImage.pixel(0,0)),qRed(glBufferImage.pixel(0,0)),qGreen(glBufferImage.pixel(0,0)),qBlue(glBufferImage.pixel(0,0)));
290     //      painter.setBackground(QBrush(bg));
291     //      //     painter.drawRect(10,10,50,80);
292     // //      QImage test = grabFrameBuffer().copy();
293     // //      painter.drawImage(0, 0,test);
[673]294     for (int ay=0;ay<glBufferImage.height();ay++) {
295       for (int ax=0;ax<glBufferImage.width();ax++) {
[675]296         //         if(QColor(glBufferImage.pixel(ax,ay)) != bg) {
297         //.           painter.setPen(QColor(glBufferImage.pixel(ax,ay)));
298         //.           painter.drawPoint(ax,ay);
299           //          } else {
300           //            printf("o");
301           //          }
[673]302         //          printf("%d+%d+%d+%d ",QColor(glBufferImage.pixel(ax,ay)).red(),QColor(glBufferImage.pixel(ax,ay)).green(),QColor(glBufferImage.pixel(ax,ay)).blue(),QColor(glBufferImage.pixel(ax,ay)).alpha());
[675]303         //          if (glBufferImage.pixel(ax,ay) == 255) {
304         //            test.setPixel(ax,ay,1);
305         //          } else {
306         //            test.setPixel(ax,ay,0);
307         //          }
[673]308       }
309       //       printf("\n");
310     }
[675]311     // //      for (int ay=0;ay<test.height();ay++) {
312     // //        for (int ax=0;ax<test.width();ax++) {
313     // //          if (test.pixel(ax,ay) == 1) {
314     // //            printf(".");
315     // //          } else if (!test.pixel(ax,ay)) {
316     // //            printf("X");
317     // //          } else {
318     // //            printf("%d",test.pixel(ax,ay));
319     // //          }
320     // //        }
321     // //        printf("\n");
322     // //      }
[673]323     
324     //     painter.drawImage(0, 0, glBufferImage);
325     //     painter.drawImage(0, 0, test,0,0,-1,-1,Qt::ColorOnly);
326     
[675]327#ifdef GEANT4_QT_DEBUG
328       printf("G4OpenGLStoredQtViewer::paintGL ============  zoom event\n");
329#endif
330       painter.setBrush(Qt::NoBrush);
331       painter.setPen(Qt::red);
332       painter.setPen(Qt::DashLine);
333       painter.drawRect(beginZoom.x(),beginZoom.y(),endZoom.x()-beginZoom.x(),endZoom.y()-beginZoom.y());
[673]334     painter.end();
335   } else {
336     
337     if (!readyToPaint) {
[608]338#ifdef GEANT4_QT_DEBUG
[673]339       printf("G4OpenGLStoredQtViewer::paintGL ============  Not ready %d\n",readyToPaint);
[608]340#endif
[673]341       readyToPaint= true;
342       return;
343     }
344     // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
345     //    WHEN CLICK ON THE FRAME FOR EXAMPLE
346     //    EXECEPT WHEN MOUSE MOVE EVENT
347     if ( !hasToRepaint) {
348       if (((WinSize_x == (G4int)width())) &&(WinSize_y == (G4int) height())) {
[608]349#ifdef GEANT4_QT_DEBUG
[673]350         printf("G4OpenGLStoredQtViewer::paintGL ============  Dont repaint\n");
[608]351#endif
[673]352         return;
353       }
[564]354     }
[673]355     nbPaint++;
[608]356#ifdef GEANT4_QT_DEBUG
[673]357     printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
[608]358#endif
[673]359     WinSize_x = (G4int) width();
360     WinSize_y = (G4int) height();
361     
362     setupViewport(width(),height());
363     //     glViewport (0, 0, width(), height());
364     //   glLoadIdentity();
365     
366     
367     SetView();
368     
369     //   //  printf("before ClearView\n");
[608]370#ifdef GEANT4_QT_DEBUG
[673]371     printf("    ClearView\n");
[608]372#endif
[673]373     
374     ClearView (); //ok, put the background correct
375     DrawView();
376     
377     hasToRepaint =false;
378     
[675]379   }
[608]380#ifdef GEANT4_QT_DEBUG
[673]381     printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
[608]382#endif
[564]383 }
384
385void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
386{
[608]387#ifdef GEANT4_QT_DEBUG
[564]388  printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
[608]389#endif
[673]390  setMouseTracking(true);
[564]391  G4MousePressEvent(event->pos());
392}
393
[673]394void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *event)
395{
396#ifdef GEANT4_QT_DEBUG
397  printf("G4OpenGLStoredQtViewer::mouseDoubleClickEvent\n");
398#endif
399  setMouseTracking(true);
400  zoomAction=true;
[675]401  //  makeCurrent();
402//   glBufferImage = QImage( width(), height(), QImage::Format_ARGB32_Premultiplied );
403//   glReadPixels( 0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, glBufferImage.bits() );
404  glBufferImage = grabFrameBuffer().convertToFormat(QImage::Format_ARGB32_Premultiplied);
[673]405 
406#ifdef GEANT4_QT_DEBUG
407//   for (int ay=0;ay<glBufferImage.height();ay++) {
408//     for (int ax=0;ax<glBufferImage.width();ax++) {
409//       if (glBufferImage.pixel(ax,ay) == 255) {
410//         printf(".");
411//       } else if (!glBufferImage.pixel(ax,ay)) {
412//         printf("X");
413//       } else {
414//         printf("%d",glBufferImage.pixel(ax,ay));
415//       }
416//     }
417//     printf("\n");
418//   }
419#endif
420  beginZoom.setX(event->x());
421  beginZoom.setY(event->y());
422}
423
424void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
425{
426#ifdef GEANT4_QT_DEBUG
427  printf("G4OpenGLStoredQtViewer::mouseReleaseEvent\n");
428#endif
429  setMouseTracking(false);
430  zoomAction =false;
431}
432
[564]433void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
434{
[673]435  // special case of mouse zoom action, it should draw a rect on
436  // the openGL buffer, so it could only be done in this class
437  // and sould be reimplemented in G4OpenGLImmediateQtViewer
438  if (!zoomAction) {
[606]439#if QT_VERSION < 0x040000
[673]440    G4MouseMoveEvent(event->x(),event->y(),event->state());
[606]441#else
[673]442    G4MouseMoveEvent(event->x(),event->y(),event->buttons());
[606]443#endif
[673]444  } else {
445#ifdef GEANT4_QT_DEBUG
446    printf("G4OpenGLStoredQtViewer::mouseMoveEvent zoom\n");
447#endif
448    endZoom.setX(event->x());
449    endZoom.setY(event->y());
450    updateGL();
451  }
[564]452  //  DrawView();
453}
454
455
456void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
457{
[635]458#ifdef GEANT4_QT_DEBUG
459  printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
460#endif
[564]461  manageContextMenuEvent(e);
462}
463
464void G4OpenGLStoredQtViewer::updateQWidget() {
465  hasToRepaint= true;
466  updateGL();
467  hasToRepaint= false;
468}
469
[673]470
[531]471#endif
Note: See TracBrowser for help on using the repository browser.