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
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.6 2007/11/15 18:24:28 lgarnier Exp $
28// GEANT4 tag $Name:  $
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#include "G4VisManager.hh"
40
41#include "G4ios.hh"
42
43//#include <qmouseevent.h>
44#include <qevent.h> // include <qcontextmenuevent.h>
45#include <qpainter.h>
46
47G4OpenGLStoredQtViewer::G4OpenGLStoredQtViewer
48(G4OpenGLStoredSceneHandler& sceneHandler,
49 const G4String&  name):
50 G4VViewer (sceneHandler, sceneHandler.IncrementViewCount (), name),
51 G4OpenGLViewer (sceneHandler),
52 G4OpenGLQtViewer (sceneHandler),
53 G4OpenGLStoredViewer (sceneHandler),
54 QGLWidget(QGLFormat(QGL::SampleBuffers))             // FIXME : gerer le pb du parent !
55 {
56   zoomAction = false;
57   nbPaint =0;
58   hasToRepaint =false;
59  if (fViewId < 0) return;  // In case error in base class instantiation.
60}
61
62G4OpenGLStoredQtViewer::~G4OpenGLStoredQtViewer() {
63#ifdef GEANT4_QT_DEBUG
64   printf("GLWidget::~GLWidget \n");
65#endif
66   makeCurrent();
67   // this is connect to the Dialog for deleting it properly
68   // when close event.
69   //   ((QDialog*)window())->reject();
70#ifdef GEANT4_QT_DEBUG
71   printf("GLWidget::~GLWidget END\n");
72#endif
73}
74
75void G4OpenGLStoredQtViewer::Initialise() {
76#ifdef GEANT4_QT_DEBUG
77   printf("GLWidget::Initialise \n");
78#endif
79   readyToPaint = false;
80   CreateGLQtContext ();
81#ifdef GEANT4_QT_DEBUG
82   printf("G4OpenGLStoredQtViewer::Initialise () 2\n");
83#endif
84  CreateMainWindow (this);
85#ifdef GEANT4_QT_DEBUG
86  printf("G4OpenGLStoredQtViewer::Initialise () 3\n");
87#endif
88  //  CreateFontLists ();  // FIXME Does nothing!
89 
90#ifdef GEANT4_QT_DEBUG
91  printf("readyToPaint = true \n");
92#endif
93  readyToPaint = true;
94 
95  // First Draw
96  SetView();
97#ifdef GEANT4_QT_DEBUG
98  printf("    ClearView\n");
99#endif
100  ClearView (); //ok, put the background correct
101  ShowView();
102  FinishView();
103#ifdef GEANT4_QT_DEBUG
104  printf("G4OpenGLStoredQtViewer::Initialise () 4\n");
105#endif
106}
107
108void G4OpenGLStoredQtViewer::initializeGL () {
109
110   InitializeGLView ();
111
112#ifdef GEANT4_QT_DEBUG
113   printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
114#endif
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
126#ifdef GEANT4_QT_DEBUG
127   printf("G4OpenGLStoredQtViewer::InitialiseGL  -------------------------------------------------------------------------------------\n");
128#endif
129}
130
131
132void G4OpenGLStoredQtViewer::DrawView () {
133#ifdef GEANT4_QT_DEBUG
134  printf("G4OpenGLStoredQtViewer::DrawView %d %d   VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",WinSize_x, WinSize_y);
135#endif
136   G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
137
138   //Make sure current viewer is attached and clean...
139   //Qt version needed
140   //   glViewport (0, 0, WinSize_x, WinSize_y);
141
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...
146   if (!fNeedKernelVisit)
147
148   if (!fNeedKernelVisit) KernelVisitDecision ();
149   
150   G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
151   ProcessView ();
152   
153
154   if(style!=G4ViewParameters::hlr &&
155      haloing_enabled) {
156#ifdef GEANT4_QT_DEBUG
157     printf("G4OpenGLStoredQtViewer::DrawView DANS LE IF\n");
158#endif
159
160     HaloingFirstPass ();
161     DrawDisplayLists ();
162     glFlush ();
163
164     HaloingSecondPass ();
165
166     DrawDisplayLists ();
167     FinishView ();
168
169   } else {
170#ifdef GEANT4_QT_DEBUG
171     printf("***************************  CASE 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
172#endif
173     
174     // If kernel visit was needed, drawing and FinishView will already
175     // have been done, so...
176     if (!kernelVisitWasNeeded) {
177#ifdef GEANT4_QT_DEBUG
178       printf("***************************  CASE 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
179#endif
180       DrawDisplayLists ();
181       FinishView ();
182     } else {
183#ifdef GEANT4_QT_DEBUG
184       printf("***************************  CASE 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
185#endif
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 ();
192         FinishView ();
193#ifdef GEANT4_QT_DEBUG
194         printf("***************************  CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
195#endif
196       } else { // ADD TO AVOID KernelVisit=1 and nothing to display
197         DrawDisplayLists ();
198         FinishView ();
199       }
200     }
201   }
202
203#ifdef GEANT4_QT_DEBUG
204   printf("G4OpenGLStoredQtViewer::DrawView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",WinSize_x, WinSize_y);
205#endif
206   hasToRepaint =true;
207}
208
209
210//////////////////////////////////////////////////////////////////////////////
211void G4OpenGLStoredQtViewer::FinishView (
212)
213//////////////////////////////////////////////////////////////////////////////
214//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
215{
216#ifdef GEANT4_QT_DEBUG
217  printf("G4OpenGLStoredQtViewer::FinishView VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
218#endif
219
220  glFlush ();
221  swapBuffers ();
222#ifdef GEANT4_QT_DEBUG
223  printf("G4OpenGLStoredQtViewer::FinishView ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
224#endif
225
226}
227
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
236  setupViewport(aWidth,aHeight);
237
238//   glViewport(0, 0, aWidth, aHeight);
239//   glMatrixMode(GL_PROJECTION);
240//   glMatrixMode(GL_MODELVIEW);
241
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
248#ifdef GEANT4_QT_DEBUG
249  printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
250#endif
251}
252
253
254/**
255@see :
256*/
257
258void G4OpenGLStoredQtViewer::paintGL()
259 {
260   if (zoomAction) {
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
268     QPainter painter;
269     painter.begin(this);
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
283     //     painter.setRenderHint(QPainter::Antialiasing);
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);
294     for (int ay=0;ay<glBufferImage.height();ay++) {
295       for (int ax=0;ax<glBufferImage.width();ax++) {
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           //          }
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());
303         //          if (glBufferImage.pixel(ax,ay) == 255) {
304         //            test.setPixel(ax,ay,1);
305         //          } else {
306         //            test.setPixel(ax,ay,0);
307         //          }
308       }
309       //       printf("\n");
310     }
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     // //      }
323     
324     //     painter.drawImage(0, 0, glBufferImage);
325     //     painter.drawImage(0, 0, test,0,0,-1,-1,Qt::ColorOnly);
326     
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());
334     painter.end();
335   } else {
336     
337     if (!readyToPaint) {
338#ifdef GEANT4_QT_DEBUG
339       printf("G4OpenGLStoredQtViewer::paintGL ============  Not ready %d\n",readyToPaint);
340#endif
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())) {
349#ifdef GEANT4_QT_DEBUG
350         printf("G4OpenGLStoredQtViewer::paintGL ============  Dont repaint\n");
351#endif
352         return;
353       }
354     }
355     nbPaint++;
356#ifdef GEANT4_QT_DEBUG
357     printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
358#endif
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");
370#ifdef GEANT4_QT_DEBUG
371     printf("    ClearView\n");
372#endif
373     
374     ClearView (); //ok, put the background correct
375     DrawView();
376     
377     hasToRepaint =false;
378     
379   }
380#ifdef GEANT4_QT_DEBUG
381     printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
382#endif
383 }
384
385void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
386{
387#ifdef GEANT4_QT_DEBUG
388  printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
389#endif
390  setMouseTracking(true);
391  G4MousePressEvent(event->pos());
392}
393
394void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *event)
395{
396#ifdef GEANT4_QT_DEBUG
397  printf("G4OpenGLStoredQtViewer::mouseDoubleClickEvent\n");
398#endif
399  setMouseTracking(true);
400  zoomAction=true;
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);
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
433void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
434{
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) {
439#if QT_VERSION < 0x040000
440    G4MouseMoveEvent(event->x(),event->y(),event->state());
441#else
442    G4MouseMoveEvent(event->x(),event->y(),event->buttons());
443#endif
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  }
452  //  DrawView();
453}
454
455
456void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
457{
458#ifdef GEANT4_QT_DEBUG
459  printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
460#endif
461  manageContextMenuEvent(e);
462}
463
464void G4OpenGLStoredQtViewer::updateQWidget() {
465  hasToRepaint= true;
466  updateGL();
467  hasToRepaint= false;
468}
469
470
471#endif
Note: See TracBrowser for help on using the repository browser.