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

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

debut de correction du ticket #105

  • Property svn:mime-type set to text/cpp
File size: 13.3 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),
54 QGLWidget() // FIXME : gerer le pb du parent !
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
[564]88 CreateFontLists (); // FIXME Does nothing!
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();
103}
[531]104
[564]105void G4OpenGLStoredQtViewer::initializeGL () {
106
107 InitializeGLView ();
108
[608]109#ifdef GEANT4_QT_DEBUG
[564]110 printf("G4OpenGLStoredQtViewer::InitialiseGL () 1\n");
[608]111#endif
[564]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
[608]123#ifdef GEANT4_QT_DEBUG
[564]124 printf("G4OpenGLStoredQtViewer::InitialiseGL -------------------------------------------------------------------------------------\n");
[608]125#endif
[531]126}
127
[564]128
[533]129void G4OpenGLStoredQtViewer::DrawView () {
[531]130
[608]131#ifdef GEANT4_QT_DEBUG
[564]132 printf("G4OpenGLStoredQtViewer::DrawView %d %d VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n",WinSize_x, WinSize_y);
[608]133#endif
[564]134 G4ViewParameters::DrawingStyle style = GetViewParameters().GetDrawingStyle();
[531]135
[564]136 //Make sure current viewer is attached and clean...
137 //Qt version needed
[652]138 // glViewport (0, 0, WinSize_x, WinSize_y);
[531]139
[564]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...
[565]144 if (!fNeedKernelVisit)
145
[564]146 if (!fNeedKernelVisit) KernelVisitDecision ();
[565]147
[564]148 G4bool kernelVisitWasNeeded = fNeedKernelVisit; // Keep (ProcessView resets).
149 ProcessView ();
150
[531]151
[564]152 if(style!=G4ViewParameters::hlr &&
153 haloing_enabled) {
[608]154#ifdef GEANT4_QT_DEBUG
[564]155 printf("G4OpenGLStoredQtViewer::DrawView DANS LE IF\n");
[608]156#endif
[531]157
[564]158 HaloingFirstPass ();
159 DrawDisplayLists ();
160 glFlush ();
[531]161
[564]162 HaloingSecondPass ();
[531]163
[564]164 DrawDisplayLists ();
[565]165 FinishView ();
[531]166
[564]167 } else {
[608]168#ifdef GEANT4_QT_DEBUG
[565]169 printf("*************************** CASE 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]170#endif
[564]171
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
[565]176 printf("*************************** CASE 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]177#endif
[564]178 DrawDisplayLists ();
[565]179 FinishView ();
[564]180 } else {
[608]181#ifdef GEANT4_QT_DEBUG
[565]182 printf("*************************** CASE 3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]183#endif
[564]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 ();
[565]190 FinishView ();
[608]191#ifdef GEANT4_QT_DEBUG
[565]192 printf("*************************** CASE 4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]193#endif
[565]194 } else { // ADD TO AVOID KernelVisit=1 and nothing to display
195 DrawDisplayLists ();
196 FinishView ();
[564]197 }
198 }
199 }
200
[608]201#ifdef GEANT4_QT_DEBUG
[565]202 printf("G4OpenGLStoredQtViewer::DrawView %d %d ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n",WinSize_x, WinSize_y);
[608]203#endif
[564]204 hasToRepaint =true;
[531]205}
206
[564]207
[531]208//////////////////////////////////////////////////////////////////////////////
[533]209void G4OpenGLStoredQtViewer::FinishView (
[531]210)
211//////////////////////////////////////////////////////////////////////////////
212//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
213{
[608]214#ifdef GEANT4_QT_DEBUG
[564]215 printf("G4OpenGLStoredQtViewer::FinishView VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
[608]216#endif
[531]217
218 glFlush ();
[564]219 swapBuffers ();
[608]220#ifdef GEANT4_QT_DEBUG
[564]221 printf("G4OpenGLStoredQtViewer::FinishView ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n");
[608]222#endif
[564]223
[531]224}
225
[564]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{
[673]234 setupViewport(aWidth,aHeight);
[564]235
[673]236// glViewport(0, 0, aWidth, aHeight);
237// glMatrixMode(GL_PROJECTION);
238// glMatrixMode(GL_MODELVIEW);
239
[564]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
[608]246#ifdef GEANT4_QT_DEBUG
[564]247 printf("G4OpenGLStoredQtViewer::resizeGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d %d=%d %d=%d\n",hasToRepaint,width(),aWidth,height(),aHeight);
[608]248#endif
[564]249}
250
251
[673]252/**
253@see :
254*/
[564]255
256void G4OpenGLStoredQtViewer::paintGL()
257 {
[673]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) {
[608]307#ifdef GEANT4_QT_DEBUG
[673]308 printf("G4OpenGLStoredQtViewer::paintGL ============ Not ready %d\n",readyToPaint);
[608]309#endif
[673]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())) {
[608]318#ifdef GEANT4_QT_DEBUG
[673]319 printf("G4OpenGLStoredQtViewer::paintGL ============ Dont repaint\n");
[608]320#endif
[673]321 return;
322 }
[564]323 }
[673]324 nbPaint++;
[608]325#ifdef GEANT4_QT_DEBUG
[673]326 printf("G4OpenGLStoredQtViewer::paintGL VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV %d ready %d\n",nbPaint,readyToPaint);
[608]327#endif
[673]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");
[608]339#ifdef GEANT4_QT_DEBUG
[673]340 printf(" ClearView\n");
[608]341#endif
[673]342
343 ClearView (); //ok, put the background correct
344 DrawView();
345
346 hasToRepaint =false;
347
[608]348#ifdef GEANT4_QT_DEBUG
[673]349 printf("G4OpenGLStoredQtViewer::paintGL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %d ready %d\n",nbPaint,readyToPaint);
[608]350#endif
[673]351 }
[564]352 }
353
354void G4OpenGLStoredQtViewer::mousePressEvent(QMouseEvent *event)
355{
[608]356#ifdef GEANT4_QT_DEBUG
[564]357 printf("G4OpenGLStoredQtViewer::mousePressEvent\n");
[608]358#endif
[673]359 setMouseTracking(true);
[564]360 G4MousePressEvent(event->pos());
361}
362
[673]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
[564]402void G4OpenGLStoredQtViewer::mouseMoveEvent(QMouseEvent *event)
403{
[673]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) {
[606]408#if QT_VERSION < 0x040000
[673]409 G4MouseMoveEvent(event->x(),event->y(),event->state());
[606]410#else
[673]411 G4MouseMoveEvent(event->x(),event->y(),event->buttons());
[606]412#endif
[673]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 }
[564]421 // DrawView();
422}
423
424
425void G4OpenGLStoredQtViewer::contextMenuEvent(QContextMenuEvent *e)
426{
[635]427#ifdef GEANT4_QT_DEBUG
428 printf("G4OpenGLStoredQtViewer::contextMenuEvent\n");
429#endif
[564]430 manageContextMenuEvent(e);
431}
432
433void G4OpenGLStoredQtViewer::updateQWidget() {
434 hasToRepaint= true;
435 updateGL();
436 hasToRepaint= false;
437}
438
[673]439
[531]440#endif
Note: See TracBrowser for help on using the repository browser.