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

Last change on this file since 677 was 677, checked in by garnier, 17 years ago

correction du ticket #106

  • Property svn:mime-type set to text/cpp
File size: 15.5 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  FinishView();
102#ifdef GEANT4_QT_DEBUG
103  printf("G4OpenGLStoredQtViewer::Initialise () 4\n");
104#endif
105}
106
107void G4OpenGLStoredQtViewer::initializeGL () {
108
109   InitializeGLView ();
110
111#ifdef GEANT4_QT_DEBUG
112   printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
113#endif
114
115   // clear the buffers and window.
116   ClearView ();
117   //   printf("G4OpenGLStoredQtViewer::InitialiseGL () 2\n");
118   FinishView ();
119   
120   glDepthFunc (GL_LEQUAL);
121   glDepthMask (GL_TRUE);
122
123   hasToRepaint =true;
124
125#ifdef GEANT4_QT_DEBUG
126   printf("G4OpenGLStoredQtViewer::InitialiseGL  -------------------------------------------------------------------------------------\n");
127#endif
128}
129
130
131void G4OpenGLStoredQtViewer::DrawView () {
132#ifdef GEANT4_QT_DEBUG
133  printf("G4OpenGLStoredQtViewer::DrawView %d %d   VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",WinSize_x, WinSize_y);
134#endif
135   G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
136
137   //Make sure current viewer is attached and clean...
138   //Qt version needed
139   //   glViewport (0, 0, WinSize_x, WinSize_y);
140
141   //See if things have changed from last time and remake if necessary...
142   // The fNeedKernelVisit flag might have been set by the user in
143   // /vis/viewer/rebuild, but if not, make decision and set flag only
144   // if necessary...
145   if (!fNeedKernelVisit)
146
147   if (!fNeedKernelVisit) KernelVisitDecision ();
148   
149   G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
150   ProcessView ();
151   
152
153   if(style!=G4ViewParameters::hlr &&
154      haloing_enabled) {
155#ifdef GEANT4_QT_DEBUG
156     printf("G4OpenGLStoredQtViewer::DrawView DANS LE IF\n");
157#endif
158
159     HaloingFirstPass ();
160     DrawDisplayLists ();
161     glFlush ();
162
163     HaloingSecondPass ();
164
165     DrawDisplayLists ();
166     FinishView ();
167
168   } else {
169#ifdef GEANT4_QT_DEBUG
170     printf("***************************  CASE 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
171#endif
172     
173     // If kernel visit was needed, drawing and FinishView will already
174     // have been done, so...
175     if (!kernelVisitWasNeeded) {
176#ifdef GEANT4_QT_DEBUG
177       printf("***************************  CASE 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
178#endif
179       DrawDisplayLists ();
180       FinishView ();
181     } else {
182#ifdef GEANT4_QT_DEBUG
183       printf("***************************  CASE 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
184#endif
185       // However, union cutaways are implemented in DrawDisplayLists, so make
186       // an extra pass...
187       if (fVP.IsCutaway() &&
188           fVP.GetCutawayMode() == G4ViewParameters::cutawayUnion) {
189         ClearView();
190         DrawDisplayLists ();
191         FinishView ();
192#ifdef GEANT4_QT_DEBUG
193         printf("***************************  CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
194#endif
195       } else { // ADD TO AVOID KernelVisit=1 and nothing to display
196         DrawDisplayLists ();
197         FinishView ();
198       }
199     }
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#ifdef GEANT4_QT_DEBUG
216  printf("G4OpenGLStoredQtViewer::FinishView VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
217#endif
218
219  glFlush ();
220  swapBuffers ();
221#ifdef GEANT4_QT_DEBUG
222  printf("G4OpenGLStoredQtViewer::FinishView ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
223#endif
224
225}
226
227
228/**
229 - Lors du resize de la fenetre, on doit non pas redessiner le detecteur, mais aussi les evenements
230 */
231void G4OpenGLStoredQtViewer::resizeGL(
232 int aWidth
233,int aHeight)
234
235  setupViewport(aWidth,aHeight);
236
237//   glViewport(0, 0, aWidth, aHeight);
238//   glMatrixMode(GL_PROJECTION);
239//   glMatrixMode(GL_MODELVIEW);
240
241   if (((WinSize_x != (G4int)aWidth)) || (WinSize_y != (G4int) aHeight)) {
242     hasToRepaint =true;
243   }
244   WinSize_x = (G4int) aWidth;
245   WinSize_y = (G4int) aHeight;
246
247#ifdef GEANT4_QT_DEBUG
248  printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
249#endif
250}
251
252
253/**
254@see :
255*/
256
257void G4OpenGLStoredQtViewer::paintGL()
258 {
259   if (zoomAction) {
260     glEnable (GL_DEPTH_TEST);
261     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
262
263     setupViewport(width(),height());     
264     SetView();
265     ClearView (); //ok, put the background correct
266
267     QPainter painter;
268     painter.begin(this);
269     //      painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
270     //      painter.setCompositionMode(QPainter::CompositionMode_SourceOver); // diagonale en pointillé
271     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); // blanc
272     //     painter.setCompositionMode(QPainter::CompositionMode_Clear);// diagonale noire
273     //     painter.setCompositionMode(QPainter::CompositionMode_Source); // diagonale en pointillé
274     //     painter.setCompositionMode(QPainter::CompositionMode_Destination); // blanc
275     //     painter.setCompositionMode(QPainter::CompositionMode_SourceIn); // blanc ?
276     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); // blanc
277     //     painter.setCompositionMode(QPainter::CompositionMode_SourceOut); // digonale noire
278     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationOut); // diagonale noire
279     //     painter.setCompositionMode(QPainter::CompositionMode_SourceAtop); // diagonale noire
280     //     painter.setCompositionMode(QPainter::CompositionMode_DestinationAtop); // blanc
281     //     painter.setCompositionMode(QPainter::CompositionMode_Xor); // diagonale noire
282     //     painter.setRenderHint(QPainter::Antialiasing);
283     //         painter.setBackgroundMode(Qt::OpaqueMode);
284     //          painter.drawImage(0, 0, glBufferImage);
285     
286     //     painter.setRenderHint(QPainter::Antialiasing);
287     //      QColor bg = QColor(glBufferImage.pixel(0,0));
288     //      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)));
289     //      painter.setBackground(QBrush(bg));
290     //      //     painter.drawRect(10,10,50,80);
291     // //      QImage test = grabFrameBuffer().copy();
292     // //      painter.drawImage(0, 0,test);
293     for (int ay=0;ay<glBufferImage.height();ay++) {
294       for (int ax=0;ax<glBufferImage.width();ax++) {
295         //         if(QColor(glBufferImage.pixel(ax,ay)) != bg) {
296         //.           painter.setPen(QColor(glBufferImage.pixel(ax,ay)));
297         //.           painter.drawPoint(ax,ay);
298           //          } else {
299           //            printf("o");
300           //          }
301         //          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());
302         //          if (glBufferImage.pixel(ax,ay) == 255) {
303         //            test.setPixel(ax,ay,1);
304         //          } else {
305         //            test.setPixel(ax,ay,0);
306         //          }
307       }
308       //       printf("\n");
309     }
310     // //      for (int ay=0;ay<test.height();ay++) {
311     // //        for (int ax=0;ax<test.width();ax++) {
312     // //          if (test.pixel(ax,ay) == 1) {
313     // //            printf(".");
314     // //          } else if (!test.pixel(ax,ay)) {
315     // //            printf("X");
316     // //          } else {
317     // //            printf("%d",test.pixel(ax,ay));
318     // //          }
319     // //        }
320     // //        printf("\n");
321     // //      }
322     
323     //     painter.drawImage(0, 0, glBufferImage);
324     //     painter.drawImage(0, 0, test,0,0,-1,-1,Qt::ColorOnly);
325     
326#ifdef GEANT4_QT_DEBUG
327       printf("G4OpenGLStoredQtViewer::paintGL ============  zoom event\n");
328#endif
329       painter.setBrush(Qt::NoBrush);
330       painter.setPen(Qt::red);
331       painter.setPen(Qt::DashLine);
332       painter.drawRect(beginZoom.x(),beginZoom.y(),endZoom.x()-beginZoom.x(),endZoom.y()-beginZoom.y());
333     painter.end();
334   } else {
335     
336     if (!readyToPaint) {
337#ifdef GEANT4_QT_DEBUG
338       printf("G4OpenGLStoredQtViewer::paintGL ============  Not ready %d\n",readyToPaint);
339#endif
340       readyToPaint= true;
341       return;
342     }
343     // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
344     //    WHEN CLICK ON THE FRAME FOR EXAMPLE
345     //    EXECEPT WHEN MOUSE MOVE EVENT
346     if ( !hasToRepaint) {
347       if (((WinSize_x == (G4int)width())) &&(WinSize_y == (G4int) height())) {
348#ifdef GEANT4_QT_DEBUG
349         printf("G4OpenGLStoredQtViewer::paintGL ============  Dont repaint\n");
350#endif
351         return;
352       }
353     }
354     nbPaint++;
355#ifdef GEANT4_QT_DEBUG
356     printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
357#endif
358     WinSize_x = (G4int) width();
359     WinSize_y = (G4int) height();
360     
361     setupViewport(width(),height());
362     //     glViewport (0, 0, width(), height());
363     //   glLoadIdentity();
364     
365     
366     SetView();
367     
368     //   //  printf("before ClearView\n");
369#ifdef GEANT4_QT_DEBUG
370     printf("    ClearView\n");
371#endif
372     
373     ClearView (); //ok, put the background correct
374     DrawView();
375     
376     hasToRepaint =false;
377     
378   }
379#ifdef GEANT4_QT_DEBUG
380     printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
381#endif
382 }
383
384void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
385{
386#ifdef GEANT4_QT_DEBUG
387  printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
388#endif
389  setMouseTracking(true);
390  G4MousePressEvent(event->pos());
391}
392
393void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *event)
394{
395#ifdef GEANT4_QT_DEBUG
396  printf("G4OpenGLStoredQtViewer::mouseDoubleClickEvent\n");
397#endif
398  setMouseTracking(true);
399  zoomAction=true;
400  //  makeCurrent();
401//   glBufferImage = QImage( width(), height(), QImage::Format_ARGB32_Premultiplied );
402//   glReadPixels( 0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, glBufferImage.bits() );
403  glBufferImage = grabFrameBuffer().convertToFormat(QImage::Format_ARGB32);//_Premultiplied);
404  QImage acha( glBufferImage.size(), QImage::Format_RGB32 );
405  acha.fill( Qt::black );
406  glBufferImage.setAlphaChannel( acha );
407 
408#ifdef GEANT4_QT_DEBUG
409//   for (int ay=0;ay<glBufferImage.height();ay++) {
410//     for (int ax=0;ax<glBufferImage.width();ax++) {
411//       if (glBufferImage.pixel(ax,ay) == 255) {
412//         printf(".");
413//       } else if (!glBufferImage.pixel(ax,ay)) {
414//         printf("X");
415//       } else {
416//         printf("%d",glBufferImage.pixel(ax,ay));
417//       }
418//     }
419//     printf("\n");
420//   }
421#endif
422  beginZoom.setX(event->x());
423  beginZoom.setY(event->y());
424}
425
426void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
427{
428#ifdef GEANT4_QT_DEBUG
429  printf("G4OpenGLStoredQtViewer::mouseReleaseEvent\n");
430#endif
431  setMouseTracking(false);
432  zoomAction =false;
433}
434
435void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
436{
437  // special case of mouse zoom action, it should draw a rect on
438  // the openGL buffer, so it could only be done in this class
439  // and sould be reimplemented in G4OpenGLImmediateQtViewer
440  if (!zoomAction) {
441#if QT_VERSION < 0x040000
442    G4MouseMoveEvent(event->x(),event->y(),event->state());
443#else
444    G4MouseMoveEvent(event->x(),event->y(),event->buttons());
445#endif
446  } else {
447#ifdef GEANT4_QT_DEBUG
448    printf("G4OpenGLStoredQtViewer::mouseMoveEvent zoom\n");
449#endif
450    endZoom.setX(event->x());
451    endZoom.setY(event->y());
452    updateGL();
453  }
454  //  DrawView();
455}
456
457
458void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
459{
460#ifdef GEANT4_QT_DEBUG
461  printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
462#endif
463  manageContextMenuEvent(e);
464}
465
466void G4OpenGLStoredQtViewer::updateQWidget() {
467  hasToRepaint= true;
468  updateGL();
469  hasToRepaint= false;
470}
471
472
473#endif
Note: See TracBrowser for help on using the repository browser.