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

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

debut de correction du ticket #105

  • Property svn:mime-type set to text/cpp
File size: 13.3 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()                      // 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}
104
105void G4OpenGLStoredQtViewer::initializeGL () {
106
107   InitializeGLView ();
108
109#ifdef GEANT4_QT_DEBUG
110   printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
111#endif
112
113   // clear the buffers and window.
114   ClearView ();
115   //   printf("G4OpenGLStoredQtViewer::InitialiseGL () 2\n");
116   FinishView ();
117   
118   glDepthFunc (GL_LEQUAL);
119   glDepthMask (GL_TRUE);
120
121   hasToRepaint =true;
122
123#ifdef GEANT4_QT_DEBUG
124   printf("G4OpenGLStoredQtViewer::InitialiseGL  -------------------------------------------------------------------------------------\n");
125#endif
126}
127
128
129void G4OpenGLStoredQtViewer::DrawView () {
130
131#ifdef GEANT4_QT_DEBUG
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#ifdef GEANT4_QT_DEBUG
169     printf("***************************  CASE 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
170#endif
171     
172     // If kernel visit was needed, drawing and FinishView will already
173     // have been done, so...
174     if (!kernelVisitWasNeeded) {
175#ifdef GEANT4_QT_DEBUG
176       printf("***************************  CASE 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
177#endif
178       DrawDisplayLists ();
179       FinishView ();
180     } else {
181#ifdef GEANT4_QT_DEBUG
182       printf("***************************  CASE 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
183#endif
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 ();
191#ifdef GEANT4_QT_DEBUG
192         printf("***************************  CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
193#endif
194       } else { // ADD TO AVOID KernelVisit=1 and nothing to display
195         DrawDisplayLists ();
196         FinishView ();
197       }
198     }
199   }
200
201#ifdef GEANT4_QT_DEBUG
202   printf("G4OpenGLStoredQtViewer::DrawView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",WinSize_x, WinSize_y);
203#endif
204   hasToRepaint =true;
205}
206
207
208//////////////////////////////////////////////////////////////////////////////
209void G4OpenGLStoredQtViewer::FinishView (
210)
211//////////////////////////////////////////////////////////////////////////////
212//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
213{
214#ifdef GEANT4_QT_DEBUG
215  printf("G4OpenGLStoredQtViewer::FinishView VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
216#endif
217
218  glFlush ();
219  swapBuffers ();
220#ifdef GEANT4_QT_DEBUG
221  printf("G4OpenGLStoredQtViewer::FinishView ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
222#endif
223
224}
225
226
227/**
228 - Lors du resize de la fenetre, on doit non pas redessiner le detecteur, mais aussi les evenements
229 */
230void G4OpenGLStoredQtViewer::resizeGL(
231 int aWidth
232,int aHeight)
233
234  setupViewport(aWidth,aHeight);
235
236//   glViewport(0, 0, aWidth, aHeight);
237//   glMatrixMode(GL_PROJECTION);
238//   glMatrixMode(GL_MODELVIEW);
239
240   if (((WinSize_x != (G4int)aWidth)) || (WinSize_y != (G4int) aHeight)) {
241     hasToRepaint =true;
242   }
243   WinSize_x = (G4int) aWidth;
244   WinSize_y = (G4int) aHeight;
245
246#ifdef GEANT4_QT_DEBUG
247  printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
248#endif
249}
250
251
252/**
253@see :
254*/
255
256void G4OpenGLStoredQtViewer::paintGL()
257 {
258   if (zoomAction) {
259     QPainter painter;
260     painter.begin(this);
261     //     painter.setRenderHint(QPainter::Antialiasing);
262     QColor bg = QColor(glBufferImage.pixel(0,0));
263     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)));
264     painter.setBackground(QBrush(bg));
265     //     painter.drawRect(10,10,50,80);
266//      QImage test = grabFrameBuffer().copy();
267//      painter.drawImage(0, 0,test);
268     for (int ay=0;ay<glBufferImage.height();ay++) {
269       for (int ax=0;ax<glBufferImage.width();ax++) {
270         if(QColor(glBufferImage.pixel(ax,ay)) != bg) {
271//            painter.setPen(QColor(glBufferImage.pixel(ax,ay)));
272//            painter.drawPoint(ax,glBufferImage.height()-ay);
273         } else {
274           printf("o");
275         }
276         //          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());
277//          if (glBufferImage.pixel(ax,ay) == 255) {
278//            test.setPixel(ax,ay,1);
279//          } else {
280//            test.setPixel(ax,ay,0);
281//          }
282       }
283       //       printf("\n");
284     }
285//      for (int ay=0;ay<test.height();ay++) {
286//        for (int ax=0;ax<test.width();ax++) {
287//          if (test.pixel(ax,ay) == 1) {
288//            printf(".");
289//          } else if (!test.pixel(ax,ay)) {
290//            printf("X");
291//          } else {
292//            printf("%d",test.pixel(ax,ay));
293//          }
294//        }
295//        printf("\n");
296//      }
297     
298     //     painter.drawImage(0, 0, glBufferImage);
299     //     painter.drawImage(0, 0, test,0,0,-1,-1,Qt::ColorOnly);
300     
301     printf("paint event zoom \n");
302     //     painter.drawRect(beginZoom.x(),beginZoom.y(),endZoom.x()-beginZoom.x(),endZoom.y()-beginZoom.y());
303     painter.end();
304   } else {
305     
306     if (!readyToPaint) {
307#ifdef GEANT4_QT_DEBUG
308       printf("G4OpenGLStoredQtViewer::paintGL ============  Not ready %d\n",readyToPaint);
309#endif
310       readyToPaint= true;
311       return;
312     }
313     // DO NOT RESIZE IF SIZE HAS NOT CHANGE :
314     //    WHEN CLICK ON THE FRAME FOR EXAMPLE
315     //    EXECEPT WHEN MOUSE MOVE EVENT
316     if ( !hasToRepaint) {
317       if (((WinSize_x == (G4int)width())) &&(WinSize_y == (G4int) height())) {
318#ifdef GEANT4_QT_DEBUG
319         printf("G4OpenGLStoredQtViewer::paintGL ============  Dont repaint\n");
320#endif
321         return;
322       }
323     }
324     nbPaint++;
325#ifdef GEANT4_QT_DEBUG
326     printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
327#endif
328     WinSize_x = (G4int) width();
329     WinSize_y = (G4int) height();
330     
331     setupViewport(width(),height());
332     //     glViewport (0, 0, width(), height());
333     //   glLoadIdentity();
334     
335     
336     SetView();
337     
338     //   //  printf("before ClearView\n");
339#ifdef GEANT4_QT_DEBUG
340     printf("    ClearView\n");
341#endif
342     
343     ClearView (); //ok, put the background correct
344     DrawView();
345     
346     hasToRepaint =false;
347     
348#ifdef GEANT4_QT_DEBUG
349     printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
350#endif
351   }
352 }
353
354void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
355{
356#ifdef GEANT4_QT_DEBUG
357  printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
358#endif
359  setMouseTracking(true);
360  G4MousePressEvent(event->pos());
361}
362
363void G4OpenGLStoredQtViewer::mouseDoubleClickEvent(QMouseEvent *event)
364{
365#ifdef GEANT4_QT_DEBUG
366  printf("G4OpenGLStoredQtViewer::mouseDoubleClickEvent\n");
367#endif
368  setMouseTracking(true);
369  zoomAction=true;
370  makeCurrent();
371  glBufferImage = QImage( width(), height(), QImage::Format_ARGB32_Premultiplied );
372  glReadPixels( 0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, glBufferImage.bits() );
373  //  glBufferImage = grabFrameBuffer(false).convertToFormat(QImage::Format_RGB16); //QImage::Format_ARGB32_Premultiplied
374 
375#ifdef GEANT4_QT_DEBUG
376//   for (int ay=0;ay<glBufferImage.height();ay++) {
377//     for (int ax=0;ax<glBufferImage.width();ax++) {
378//       if (glBufferImage.pixel(ax,ay) == 255) {
379//         printf(".");
380//       } else if (!glBufferImage.pixel(ax,ay)) {
381//         printf("X");
382//       } else {
383//         printf("%d",glBufferImage.pixel(ax,ay));
384//       }
385//     }
386//     printf("\n");
387//   }
388#endif
389  beginZoom.setX(event->x());
390  beginZoom.setY(event->y());
391}
392
393void G4OpenGLStoredQtViewer::mouseReleaseEvent(QMouseEvent *event)
394{
395#ifdef GEANT4_QT_DEBUG
396  printf("G4OpenGLStoredQtViewer::mouseReleaseEvent\n");
397#endif
398  setMouseTracking(false);
399  zoomAction =false;
400}
401
402void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
403{
404  // special case of mouse zoom action, it should draw a rect on
405  // the openGL buffer, so it could only be done in this class
406  // and sould be reimplemented in G4OpenGLImmediateQtViewer
407  if (!zoomAction) {
408#if QT_VERSION < 0x040000
409    G4MouseMoveEvent(event->x(),event->y(),event->state());
410#else
411    G4MouseMoveEvent(event->x(),event->y(),event->buttons());
412#endif
413  } else {
414#ifdef GEANT4_QT_DEBUG
415    printf("G4OpenGLStoredQtViewer::mouseMoveEvent zoom\n");
416#endif
417    endZoom.setX(event->x());
418    endZoom.setY(event->y());
419    updateGL();
420  }
421  //  DrawView();
422}
423
424
425void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
426{
427#ifdef GEANT4_QT_DEBUG
428  printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
429#endif
430  manageContextMenuEvent(e);
431}
432
433void G4OpenGLStoredQtViewer::updateQWidget() {
434  hasToRepaint= true;
435  updateGL();
436  hasToRepaint= false;
437}
438
439
440#endif
Note: See TracBrowser for help on using the repository browser.