source: trunk/geant4/visualization/OpenGL/src/G4OpenGLQtViewer.cc @ 696

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

avancement sur la zone de recherche

  • Property svn:mime-type set to text/cpp
File size: 52.8 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: G4OpenGLQtViewer.cc,v 1.9 2008/01/15 11:05:08 lgarnier Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// G4OpenGLQtViewer : Class to provide Qt specific
32//                     functionality for OpenGL in GEANT4
33//
34// 27/06/2003 : G.Barrand : implementation (at last !).
35
36#ifdef G4VIS_BUILD_OPENGLQT_DRIVER
37
38#define GEANT4_QT_DEBUG
39
40#include "G4OpenGLQtViewer.hh"
41
42#include "G4ios.hh"
43#include "G4VisExtent.hh"
44#include "G4LogicalVolume.hh"
45#include "G4VSolid.hh"
46#include "G4Point3D.hh"
47#include "G4Normal3D.hh"
48#include "G4Scene.hh"
49#include "G4OpenGLQtExportDialog.hh"
50
51#include "G4Qt.hh"
52#include "G4UIsession.hh"
53#include "G4UImanager.hh"
54#include <qapplication.h>
55#include <qlayout.h>
56#include <qdialog.h>
57
58#if QT_VERSION >= 0x040000
59#include <qmenu.h>
60#include <qimagewriter.h>
61#else
62#include <qaction.h>
63#include <qwidgetlist.h>
64#include <qpopupmenu.h>
65#include <qimage.h>
66#endif
67
68#include <qmessagebox.h>
69#include <qfiledialog.h>
70#include <qprinter.h>
71#include <qpainter.h>
72#include <qgl.h> // include <qglwidget.h>
73#include <qdialog.h>
74#include <qevent.h> //include <qcontextmenuevent.h>
75
76
77//////////////////////////////////////////////////////////////////////////////
78/**
79   Implementation of virtual method of G4VViewer
80*/
81void G4OpenGLQtViewer::SetView (
82)
83//////////////////////////////////////////////////////////////////////////////
84//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
85{
86#ifdef GEANT4_QT_DEBUG
87  printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
88#endif
89  //   if(!fHDC) return;
90  //   if(!fHGLRC) return;
91  //   ::wglMakeCurrent(fHDC,fHGLRC);
92  //  fWindow->makeCurrent();
93  G4OpenGLViewer::SetView ();
94#ifdef GEANT4_QT_DEBUG
95  printf("G4OpenGLQtViewer::SetView --------------------\n");
96#endif
97}
98
99/**
100 * Set the viewport of the scene
101 */
102void G4OpenGLQtViewer::setupViewport(int aWidth, int aHeight)
103{
104  int side = qMin(aWidth, aHeight);
105  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
106 
107  glMatrixMode(GL_PROJECTION);
108  glLoadIdentity();
109  glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
110  glMatrixMode(GL_MODELVIEW);
111}
112
113
114//////////////////////////////////////////////////////////////////////////////
115/**
116   Implementation of virtual method of G4VViewer
117*/
118void G4OpenGLQtViewer::ShowView (
119)
120//////////////////////////////////////////////////////////////////////////////
121//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
122{
123#ifdef GEANT4_QT_DEBUG
124  printf("G4OpenGLQtViewer::ShowView  +++++++++++++++++++++\n");
125#endif
126  if (!GLWindow) {
127    G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
128  } else {
129#if QT_VERSION < 0x040000
130    GLWindow->setActiveWindow();
131#else
132    GLWindow->activateWindow();
133#endif
134#ifdef GEANT4_QT_DEBUG
135    printf("G4OpenGLQtViewer::ShowView -----------------------\n");
136#endif
137  }
138  glFlush ();
139  //   // Empty the Windows message queue :
140  //   MSG event;
141  //   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
142  //     ::TranslateMessage(&event);
143  //     ::DispatchMessage (&event);
144  //   }
145}
146
147
148
149//////////////////////////////////////////////////////////////////////////////
150void G4OpenGLQtViewer::CreateGLQtContext (
151)
152//////////////////////////////////////////////////////////////////////////////
153//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
154{
155#ifdef GEANT4_QT_DEBUG
156  printf("G4OpenGLQtViewer::CreateGLQtContext \n");
157#endif
158}
159
160
161//////////////////////////////////////////////////////////////////////////////
162void G4OpenGLQtViewer::CreateMainWindow (
163 QGLWidget* glWidget
164)
165//////////////////////////////////////////////////////////////////////////////
166//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
167{
168
169  if(fWindow) return; //Done.
170#ifdef GEANT4_QT_DEBUG
171  printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
172#endif
173
174  // launch Qt if not
175  G4Qt* interactorManager = G4Qt::getInstance ();
176  //  G4UImanager* UI = G4UImanager::GetUIpointer();
177
178  fWindow = glWidget ;
179  //  fWindow->makeCurrent();
180
181  // create window
182  if (((QApplication*)interactorManager->GetMainInteractor())) {
183    // look for the main window
184    bool found = false;
185#if QT_VERSION < 0x040000
186    // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
187    // but if I comment them, it doesn't work...
188    QWidgetList  *list = QApplication::allWidgets();
189    QWidgetListIt it( *list );         // iterate over the widgets
190    QWidget * widget;
191    while ( (widget=it.current()) != 0 ) {  // for each widget...
192      ++it;
193      if ((found== false) && (widget->inherits("QMainWindow"))) {
194        GLWindow = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
195        found = true;
196      }
197    }
198    delete list;                      // delete the list, not the widgets
199#else
200    foreach (QWidget *widget, QApplication::allWidgets()) {
201      if ((found== false) && (widget->inherits("QMainWindow"))) {
202        GLWindow = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
203        found = true;
204      }
205    }
206#endif
207
208#if QT_VERSION < 0x040000
209    glWidget->reparent(GLWindow,0,QPoint(0,0)); 
210#else
211    glWidget->setParent(GLWindow); 
212#endif
213
214    if (found==false) {
215#ifdef GEANT4_QT_DEBUG
216      printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
217#endif
218      GLWindow = new QDialog();
219    }
220  } else {
221#ifdef GEANT4_QT_DEBUG
222    printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
223#endif
224    GLWindow = new QDialog();
225  }
226
227  QHBoxLayout *mainLayout = new QHBoxLayout(GLWindow);
228
229  mainLayout->addWidget(fWindow);
230
231#if QT_VERSION < 0x040000
232  GLWindow->setCaption( tr( "QGl Viewer" ));
233#else
234  GLWindow->setLayout(mainLayout);
235  GLWindow->setWindowTitle(tr("QGl Viewer"));
236#endif
237  GLWindow->resize(300, 300);
238  GLWindow->move(900,300);
239  GLWindow->show();
240 
241  // delete the pointer if close this
242  //  GLWindow->setAttribute(Qt::WA_DeleteOnClose);
243
244#if QT_VERSION >= 0x040000
245  QObject ::connect(GLWindow,
246                    SIGNAL(rejected()),
247                    this,
248                    SLOT(dialogClosed()));
249#endif
250
251  WinSize_x = 400;
252  WinSize_y = 400;
253  if (WinSize_x < fVP.GetWindowSizeHintX ())
254    WinSize_x = fVP.GetWindowSizeHintX ();
255  if (WinSize_y < fVP.GetWindowSizeHintY ())
256    WinSize_y = fVP.GetWindowSizeHintY ();
257
258  if(!fWindow) return;
259#ifdef GEANT4_QT_DEBUG
260  printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
261#endif
262
263  if (!fContextMenu)
264    createPopupMenu();
265
266}
267
268#if QT_VERSION >= 0x040000
269/**  Close the dialog and set the pointer to NULL
270 */
271void G4OpenGLQtViewer::dialogClosed() {
272#ifdef GEANT4_QT_DEBUG
273  printf("G4OpenGLQtViewer::dialogClosed END\n");
274#endif
275  //  GLWindow = NULL;
276}
277#endif
278
279//////////////////////////////////////////////////////////////////////////////
280G4OpenGLQtViewer::G4OpenGLQtViewer (
281                                    G4OpenGLSceneHandler& scene
282                                    )
283  :G4VViewer (scene, -1)
284  ,G4OpenGLViewer (scene)
285  ,fWindow(0)
286  ,fContextMenu(0)
287  ,fMouseAction(STYLE1)
288  ,fDeltaSceneTranslation(1/100)
289  ,fDeltaZoom(2)
290{
291#ifdef GEANT4_QT_DEBUG
292  printf("G4OpenGLQtViewer::G4OpenGLQtViewer \n");
293#endif
294}
295
296//////////////////////////////////////////////////////////////////////////////
297G4OpenGLQtViewer::~G4OpenGLQtViewer (
298)
299//////////////////////////////////////////////////////////////////////////////
300//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
301{
302#ifdef GEANT4_QT_DEBUG
303  printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
304#endif
305}
306
307
308/**
309   Create a popup menu for the widget. This menu is activated by right-mouse click
310*/
311void G4OpenGLQtViewer::createPopupMenu()    {
312
313#if QT_VERSION < 0x040000
314  fContextMenu = new QPopupMenu( GLWindow,"All" );
315#else
316  fContextMenu = new QMenu("All");
317#endif
318
319#if QT_VERSION < 0x040000
320  QPopupMenu *mMouseAction = new QPopupMenu( fContextMenu );
321  fContextMenu->insertItem("&Mouse actions");
322#else
323  QMenu *mMouseAction = fContextMenu->addMenu("&Mouse actions");
324#endif
325
326#if QT_VERSION < 0x040000
327  // === Mouse menu ===
328
329  fMouseRotate = new QPopupMenu(mMouseAction);
330  mMouseAction->insertItem("&Rotate",fMouseRotate);
331
332  mMouseAction->setCheckable(true);
333  fMouseRotate->setCheckable(true);
334
335  fMouseMove = new QPopupMenu(mMouseAction);
336  mMouseAction->insertItem("&Move",fMouseMove);
337  fMouseMove->setCheckable(true);
338
339  fMouseZoom = new QPopupMenu(mMouseAction);
340  mMouseAction->insertItem("&Zoom",fMouseZoom);
341  fMouseZoom->setCheckable(true);
342
343  fMousePick = new QPopupMenu(mMouseAction);
344  mMouseAction->insertItem("&Pick",fMousePick);
345  fMousePick->setCheckable(true);
346
347#else
348  // === Mouse menu ===
349
350  fMouseRotate = mMouseAction->addAction("Rotate");
351  fMouseRotate->setCheckable(true);
352
353  fMouseMove = mMouseAction->addAction("Move");
354  fMouseMove->setCheckable(true);
355
356  fMouseZoom = mMouseAction->addAction("Zoom");
357  fMouseZoom->setCheckable(true);
358
359  fMousePick = mMouseAction->addAction("Pick");
360  fMousePick->setCheckable(true);
361#endif
362
363  // INIT mMouse
364#if QT_VERSION < 0x040000
365  fMouseRotate->setItemCheckeded(0,true);
366
367  QObject ::connect(fMouseRotate,
368                    SIGNAL(activated(int)),
369                    this,
370                    SLOT(actionMouseRotate()));
371
372  QObject ::connect(fMouseMove,
373                    SIGNAL(activated(int)),
374                    this,
375                    SLOT(actionMouseMove()));
376
377  QObject ::connect(fMouseZoom,
378                    SIGNAL(activated(int)),
379                    this,
380                    SLOT(actionMouseZoom()));
381
382  QObject ::connect(fMousePick,
383                    SIGNAL(activated(int)),
384                    this,
385                    SLOT(actionMousePick()));
386
387#else
388  fMouseRotate->setChecked(true);
389
390  QObject ::connect(fMouseRotate,
391                    SIGNAL(triggered(bool)),
392                    this,
393                    SLOT(actionMouseRotate()));
394
395  QObject ::connect(fMouseMove,
396                    SIGNAL(triggered(bool)),
397                    this,
398                    SLOT(actionMouseMove()));
399
400  QObject ::connect(fMouseZoom,
401                    SIGNAL(triggered(bool)),
402                    this,
403                    SLOT(actionMouseZoom()));
404
405  QObject ::connect(fMousePick,
406                    SIGNAL(triggered(bool)),
407                    this,
408                    SLOT(actionMousePick()));
409#endif
410
411#if QT_VERSION < 0x040000
412  // === Style Menu ===
413  QPopupMenu *mStyle = new QPopupMenu(fContextMenu);
414
415  QPopupMenu *mRepresentation = new QPopupMenu(fContextMenu);
416
417#if QT_VERSION < 0x030200
418  QAction *polyhedron = new QAction("&Polyhedron","&Polyhedron",CTRL+Key_P,mRepresentation);
419  QAction *nurbs = new QAction("&NURBS","&NURBS",CTRL+Key_N,mRepresentation);
420#else
421  QAction *polyhedron = new QAction("&Polyhedron",CTRL+Key_P,mRepresentation);
422  QAction *nurbs = new QAction("&NURBS",CTRL+Key_N,mRepresentation);
423#endif
424  polyhedron->addTo(mRepresentation);
425  nurbs->addTo(mRepresentation);
426
427  mStyle->insertItem("&Representation",mRepresentation);
428  fContextMenu->insertItem("&Style",mStyle);
429
430#else
431  // === Style Menu ===
432  QMenu *mStyle = fContextMenu->addMenu("&Style");
433
434  QMenu *mRepresentation = mStyle->addMenu("&Representation");
435  QAction *polyhedron = mRepresentation->addAction("Polyhedron");
436  QAction *nurbs = mRepresentation->addAction("NURBS");
437#endif
438  // INIT mStyle
439  G4ViewParameters::RepStyle style;
440  style = fVP.GetRepStyle();
441  if (style == G4ViewParameters::polyhedron) {
442    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
443  } else if (style == G4ViewParameters::nurbs) {
444    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
445  } else {
446    mRepresentation->clear();
447  }
448
449
450#if QT_VERSION < 0x040000
451  // === Drawing Menu ===
452  QPopupMenu *mDrawing = new QPopupMenu(fContextMenu);
453  fContextMenu->insertItem("&Drawing",mDrawing);
454
455  fDrawingWireframe = new QPopupMenu(mDrawing);
456  mDrawing->insertItem("&Wireframe",fDrawingWireframe);
457
458  mDrawing->setCheckable(true);
459  fDrawingWireframe->setCheckable(true);
460
461  fDrawingLineRemoval = new QPopupMenu(mDrawing);
462  mDrawing->insertItem("&Hidden line removal",fDrawingLineRemoval);
463  fDrawingLineRemoval->setCheckable(true);
464
465  fDrawingSurfaceRemoval = new QPopupMenu(mDrawing);
466  mDrawing->insertItem("&Hidden surface removal",fDrawingSurfaceRemoval);
467  fDrawingSurfaceRemoval->setCheckable(true);
468
469  fDrawingLineSurfaceRemoval = new QPopupMenu(mDrawing);
470  mDrawing->insertItem("&Hidden line and surface removal",fDrawingLineSurfaceRemoval);
471  fDrawingLineSurfaceRemoval->setCheckable(true);
472
473#else
474  // === Drawing Menu ===
475  QMenu *mDrawing = mStyle->addMenu("&Drawing");
476
477  fDrawingWireframe = mDrawing->addAction("Wireframe");
478  fDrawingWireframe->setCheckable(true);
479
480  fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
481  fDrawingLineRemoval->setCheckable(true);
482
483  fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
484  fDrawingSurfaceRemoval->setCheckable(true);
485
486  fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
487  fDrawingLineSurfaceRemoval->setCheckable(true);
488#endif
489  // INIT Drawing
490  G4ViewParameters::DrawingStyle d_style;
491  d_style = fVP.GetDrawingStyle();
492 
493#if QT_VERSION < 0x040000
494  if (d_style == G4ViewParameters::wireframe) {
495    fDrawingWireframe->setItemChecked(0,true);
496  } else if (d_style == G4ViewParameters::hlr) {
497    fDrawingLineRemoval->setItemChecked(0,true);
498  } else if (d_style == G4ViewParameters::hsr) {
499    fDrawingSurfaceRemoval->setItemChecked(0,true);
500  } else if (d_style == G4ViewParameters::hlhsr) {
501    fDrawingLineSurfaceRemoval->setItemChecked(0,true);
502  } else {
503    mDrawing->clear();
504  }
505#ifdef GEANT4_QT_DEBUG
506  printf("G4OpenGLQtViewer:: fDrawingWireframe 1\n");
507#endif
508  QObject ::connect(fDrawingWireframe,
509                    SIGNAL(activated(int)),
510                    this,
511                    SLOT(actionDrawingWireframe()));
512#ifdef GEANT4_QT_DEBUG
513  printf("G4OpenGLQtViewer:: fDrawingWireframe 2\n");
514#endif
515  QObject ::connect(fDrawingLineRemoval,
516                    SIGNAL(activated(int)),
517                    this,
518                    SLOT(actionDrawingLineRemoval()));
519  QObject ::connect(fDrawingSurfaceRemoval,
520                    SIGNAL(activated(int)),
521                    this,
522                    SLOT(actionDrawingSurfaceRemoval()));
523  QObject ::connect(fDrawingLineSurfaceRemoval,
524                    SIGNAL(activated(int)),
525                    this,
526                    SLOT(actionDrawingLineSurfaceRemoval()));
527#else
528  if (d_style == G4ViewParameters::wireframe) {
529    fDrawingWireframe->setChecked(true);
530  } else if (d_style == G4ViewParameters::hlr) {
531    fDrawingLineRemoval->setChecked(true);
532  } else if (d_style == G4ViewParameters::hsr) {
533    fDrawingSurfaceRemoval->setChecked(true);
534  } else if (d_style == G4ViewParameters::hlhsr) {
535    fDrawingLineSurfaceRemoval->setChecked(true);
536  } else {
537    mDrawing->clear();
538  }
539  QObject ::connect(fDrawingWireframe,
540                    SIGNAL(triggered(bool)),
541                    this,
542                    SLOT(actionDrawingWireframe()));
543  QObject ::connect(fDrawingLineRemoval,
544                    SIGNAL(triggered(bool)),
545                    this,
546                    SLOT(actionDrawingLineRemoval()));
547  QObject ::connect(fDrawingSurfaceRemoval,
548                    SIGNAL(triggered(bool)),
549                    this,
550                    SLOT(actionDrawingSurfaceRemoval()));
551  QObject ::connect(fDrawingLineSurfaceRemoval,
552                    SIGNAL(triggered(bool)),
553                    this,
554                    SLOT(actionDrawingLineSurfaceRemoval()));
555#endif
556
557
558
559#if QT_VERSION < 0x040000
560  QPopupMenu *mBackground = new QPopupMenu(mStyle);
561  mStyle->insertItem("&Background color",mBackground);
562
563#if QT_VERSION < 0x030200
564  QAction *white = new QAction("&White","&White",CTRL+Key_W,mBackground);
565  QAction *black =  new QAction("&Black","&Black",CTRL+Key_B,mBackground);
566#else
567  QAction *white = new QAction("&White",CTRL+Key_W,mBackground);
568  QAction *black =  new QAction("&Black",CTRL+Key_B,mBackground);
569#endif
570  white->addTo(mBackground);
571  black->addTo(mBackground);
572
573#else
574  QMenu *mBackground = mStyle->addMenu("&Background color");
575  QAction *white = mBackground->addAction("White");
576  QAction *black = mBackground->addAction("Black");
577
578#endif
579  if (background.GetRed() == 1. &&
580      background.GetGreen() == 1. &&
581      background.GetBlue() == 1.) {
582    createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
583  } else {
584    createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
585  }
586
587
588#if QT_VERSION < 0x040000
589  // === Action Menu ===
590  QPopupMenu *mActions = new QPopupMenu(fContextMenu);
591  fContextMenu->insertItem("&Actions",mActions);
592
593#if QT_VERSION < 0x030200
594  QAction *createEPS =  new QAction("&Save as ...","&Save as ...",CTRL+Key_S,mActions);
595#else
596  QAction *createEPS =  new QAction("&Save as ...",CTRL+Key_S,mActions);
597#endif
598  createEPS->addTo(mActions);
599  QObject ::connect(createEPS,
600                    SIGNAL(activated()),
601                    this,
602                    SLOT(actionCreateEPS()));
603
604#else
605  // === Action Menu ===
606  QMenu *mActions = fContextMenu->addMenu("&Actions");
607  QAction *createEPS = mActions->addAction("Save as ...");
608  QObject ::connect(createEPS,
609                    SIGNAL(triggered()),
610                    this,
611                    SLOT(actionCreateEPS()));
612#endif
613
614
615
616#if QT_VERSION < 0x040000
617  // === Special Menu ===
618  QPopupMenu *mSpecial = new QPopupMenu(fContextMenu);
619  fContextMenu->insertItem("S&pecial",mSpecial);
620
621  QPopupMenu *mTransparency = new QPopupMenu(mSpecial);
622  mSpecial->insertItem("Transparency",mTransparency);
623
624#if QT_VERSION < 0x030200
625  QAction *transparencyOn = new QAction("&On","&On",CTRL+Key_O,mTransparency);
626  QAction *transparencyOff = new QAction("&Off","&Off",CTRL+Key_F,mTransparency);
627#else
628  QAction *transparencyOn = new QAction("&On",CTRL+Key_O,mTransparency);
629  QAction *transparencyOff = new QAction("&Off",CTRL+Key_F,mTransparency);
630#endif
631  transparencyOn->addTo(mTransparency);
632  transparencyOff->addTo(mTransparency);
633
634#else
635  // === Special Menu ===
636  QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
637  QMenu *mTransparency = mSpecial->addMenu("Transparency");
638  QAction *transparencyOn = mTransparency->addAction("On");
639  QAction *transparencyOff = mTransparency->addAction("Off");
640#endif
641
642  if (transparency_enabled == false) {
643    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
644  } else if (transparency_enabled == true) {
645    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
646  } else {
647    mSpecial->clear();
648  }
649
650
651#if QT_VERSION < 0x040000
652  QPopupMenu *mAntialiasing = new QPopupMenu(mSpecial);
653  mSpecial->insertItem("Antialiasing",mAntialiasing);
654
655#if QT_VERSION < 0x030200
656  QAction *antialiasingOn = new QAction("&On","&On",CTRL+Key_O,mAntialiasing);
657  QAction *antialiasingOff = new QAction("&Off","&Off",CTRL+Key_F,mAntialiasing);
658#else
659  QAction *antialiasingOn = new QAction("&On",CTRL+Key_O,mAntialiasing);
660  QAction *antialiasingOff = new QAction("&Off",CTRL+Key_F,mAntialiasing);
661#endif
662  antialiasingOn->addTo(mAntialiasing);
663  antialiasingOff->addTo(mAntialiasing);
664
665#else
666  QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
667  QAction *antialiasingOn = mAntialiasing->addAction("On");
668  QAction *antialiasingOff = mAntialiasing->addAction("Off");
669#endif
670
671  if (antialiasing_enabled == false) {
672    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
673  } else if (antialiasing_enabled == true) {
674    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
675  } else {
676    mAntialiasing->clear();
677  }
678
679#if QT_VERSION < 0x040000
680  QPopupMenu *mHaloing = new QPopupMenu(mSpecial);
681  mSpecial->insertItem("Haloing",mHaloing);
682
683#if QT_VERSION < 0x030200
684  QAction *haloingOn = new QAction("&On","&On",CTRL+Key_O,mHaloing);
685  QAction *haloingOff = new QAction("&Off","&Off",CTRL+Key_F,mHaloing);
686#else
687  QAction *haloingOn = new QAction("&On",CTRL+Key_O,mHaloing);
688  QAction *haloingOff = new QAction("&Off",CTRL+Key_F,mHaloing);
689#endif
690  haloingOn->addTo(mHaloing);
691  haloingOff->addTo(mHaloing);
692#else
693  QMenu *mHaloing = mSpecial->addMenu("Haloing");
694  QAction *haloingOn = mHaloing->addAction("On");
695  QAction *haloingOff = mHaloing->addAction("Off");
696#endif
697  if (haloing_enabled == false) {
698    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
699  } else if (haloing_enabled == true) {
700    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
701  } else {
702    mHaloing->clear();
703  }
704
705#if QT_VERSION < 0x040000
706  QPopupMenu *mAux = new QPopupMenu(mSpecial);
707  mSpecial->insertItem("Auxiliairy edges",mAux);
708
709#if QT_VERSION < 0x030200
710  QAction *auxOn = new QAction("&On","&On",CTRL+Key_O,mAux);
711  QAction *auxOff = new QAction("&Off","&Off",CTRL+Key_F,mAux);
712#else
713  QAction *auxOn = new QAction("&On",CTRL+Key_O,mAux);
714  QAction *auxOff = new QAction("&Off",CTRL+Key_F,mAux);
715#endif
716  auxOn->addTo(mAux);
717  auxOff->addTo(mAux);
718
719#else
720  QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
721  QAction *auxOn = mAux->addAction("On");
722  QAction *auxOff = mAux->addAction("Off");
723#endif
724  if (!fVP.IsAuxEdgeVisible()) {
725    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
726  } else {
727    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
728  }
729
730
731#if QT_VERSION >= 0x030200
732#if QT_VERSION < 0x040000
733  QPopupMenu *mFullScreen = new QPopupMenu(mSpecial);
734  mSpecial->insertItem("Full screen",mFullScreen);
735
736  QAction *fullOn = new QAction("&On",CTRL+Key_O,mFullScreen);
737  QAction *fullOff = new QAction("&Off",CTRL+Key_F,mFullScreen);
738  fullOn->addTo(mFullScreen);
739  fullOff->addTo(mFullScreen);
740#else
741  QMenu *mFullScreen = mSpecial->addMenu("Full screen");
742  QAction *fullOn = mFullScreen->addAction("On");
743  QAction *fullOff = mFullScreen->addAction("Off");
744#endif
745  createRadioAction(fullOn,fullOff,SLOT(toggleFullScreen(bool)),2);
746#endif
747
748}
749
750void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
751{
752  if (!GLWindow) {
753    G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
754  } else {
755 
756    if (!fContextMenu)
757      createPopupMenu();
758   
759    // launch menu
760    if ( fContextMenu ) {
761      fContextMenu->exec( e->globalPos() );
762      //    delete fContextMenu;
763    }
764  }
765  e->accept();
766}
767
768
769/**
770   Create a radio button menu. The two menu will be connected. When click on one,
771   eatch state will be invert and callback method will be called.
772   @param action1 first action to connect
773   @param action2 second action to connect
774   @param method callback method
775   @param nCheck: 1 : first action will be set true. 2 : second action will be set true
776*/
777#if QT_VERSION < 0x040000
778void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
779
780  if (action1->parent()->inherits("QPopupMenu")){
781    ((QPopupMenu*)action1->parent())->setCheckable(true);
782  }
783  ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
784  ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
785
786  if (nCheck ==1)
787    ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
788  else
789    ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
790   
791  //FIXME : Should not work on Qt3
792  QObject ::connect(action1, SIGNAL(activated()),action2, SLOT(toggle()));
793  QObject ::connect(action2, SIGNAL(activated()),action1, SLOT(toggle()));
794
795  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
796}
797
798#else
799void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
800
801  action1->setCheckable(true);
802  action2->setCheckable(true);
803
804  if (nCheck ==1)
805    action1->setChecked (true);
806  else
807    action2->setChecked (true);
808   
809  QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
810  QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
811
812  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
813
814}
815#endif
816
817/**
818   Slot activate when mouseAction->rotate menu is set
819 */
820void G4OpenGLQtViewer::actionMouseRotate() {
821  emit toggleMouseAction(STYLE1);
822}
823
824/**
825   Slot activate when mouseAction->move menu is set
826 */
827void G4OpenGLQtViewer::actionMouseMove() {
828  emit toggleMouseAction(STYLE2);
829}
830
831/**
832   Slot activate when mouseAction->pick menu is set
833 */
834void G4OpenGLQtViewer::actionMouseZoom() {
835#ifdef GEANT4_QT_DEBUG
836  printf("G4OpenGLQtViewer::actionMouseZoom \n");
837#endif
838  emit toggleMouseAction(STYLE3);
839}
840
841/**
842   Slot activate when mouseAction->zoom menu is set
843 */
844void G4OpenGLQtViewer::actionMousePick() {
845#ifdef GEANT4_QT_DEBUG
846  printf("G4OpenGLQtViewer::actionMousePick \n");
847#endif
848  emit toggleMouseAction(STYLE4);
849}
850
851/**
852   Slot activate when drawing->wireframe menu is set
853 */
854void G4OpenGLQtViewer::actionDrawingWireframe() {
855  emit toggleDrawingAction(1);
856}
857
858/**
859   Slot activate when drawing->line removal menu is set
860 */
861void G4OpenGLQtViewer::actionDrawingLineRemoval() {
862  emit toggleDrawingAction(2);
863}
864
865/**
866   Slot activate when drawing->surface removal menu is set
867 */
868void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
869  emit toggleDrawingAction(3);
870}
871
872/**
873   Slot activate when drawing->wireframe menu is set
874 */
875void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
876  emit toggleDrawingAction(4);
877}
878
879
880/**
881   Slot activated when mouse action is toggle
882   @param aAction : STYLE1, STYLE2, STYLE3
883 */
884void G4OpenGLQtViewer::toggleMouseAction(mouseActions aAction) {
885 
886  if (aAction == STYLE1) {
887    G4cout << "Clic and move mouse to rotate Volume \n" << G4endl;
888    G4cout << "Press left/right arrows to move camera left/right\n" << G4endl;
889    G4cout << "Press up/down arrows to move camera toward/forward\n" << G4endl;
890    G4cout << "Press SHIFT+up/down arrows to move camera up/down\n" << G4endl;
891    G4cout << "Press +/- to zoom into volume\n" << G4endl;
892#if QT_VERSION < 0x040000
893    fMouseRotate->setItemChecked (0,true);
894    fMouseMove->setItemChecked (0,false);
895    fMouseZoom->setItemChecked (0,false);
896    fMousePick->setItemChecked (0,false);
897#else
898    fMouseRotate->setChecked (true);
899    fMouseMove->setChecked (false);
900    fMouseZoom->setChecked (false);
901    fMousePick->setChecked (false);
902#endif
903    fMouseAction = STYLE1;
904    fVP.SetPicking(false);
905  } else  if (aAction == STYLE2) {
906    G4cout << "Clic and move mouse to move Volume \n" << G4endl;
907#if QT_VERSION < 0x040000
908    fMouseRotate->setItemChecked (0,false);
909    fMouseMove->setItemChecked (0,true);
910    fMouseZoom->setItemChecked (0,false);
911    fMousePick->setItemChecked (0,false);
912#else
913    fMouseRotate->setChecked (false);
914    fMouseMove->setChecked (true);
915    fMouseZoom->setChecked (false);
916    fMousePick->setChecked (false);
917#endif
918    fMouseAction = STYLE2;
919    fVP.SetPicking(false);
920  } else  if (aAction == STYLE3) {
921    G4cout << "Clic and move mouse to rotate Volume \n" << G4endl;
922#if QT_VERSION < 0x040000
923    fMouseRotate->setItemChecked (0,false);
924    fMouseMove->setItemChecked (0,false);
925    fMouseZoom->setItemChecked (0,true);
926    fMousePick->setItemChecked (0,false);
927#else
928    fMouseRotate->setChecked (false);
929    fMouseMove->setChecked (false);
930    fMouseZoom->setChecked (true);
931    fMousePick->setChecked (false);
932#endif
933    fMouseAction = STYLE3;
934    fVP.SetPicking(false);
935  } else  if (aAction == STYLE4) {
936#if QT_VERSION < 0x040000
937    fMouseRotate->setItemChecked (0,false);
938    fMouseMove->setItemChecked (0,false);
939    fMouseZoom->setItemChecked (0,false);
940    fMousePick->setItemChecked (0,true);
941#else
942    fMouseRotate->setChecked (false);
943    fMouseMove->setChecked (false);
944    fMouseZoom->setChecked (false);
945    fMousePick->setChecked (true);
946#endif
947    fVP.SetPicking(true);
948    fMouseAction = STYLE4;
949  }
950
951}
952/**
953   Slot activated when drawing menu is toggle
954   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
955   KernelVisitDecision () will be call and will set the fNeedKernelVisit
956   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
957   It will cause a redraw of the view
958   @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
959   @see G4OpenGLStoredQtViewer::DrawView
960   @see G4XXXStoredViewer::CompareForKernelVisit
961 */
962void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
963
964  G4ViewParameters::DrawingStyle d_style;
965 
966
967  if (aAction ==1) {
968#if QT_VERSION < 0x040000
969    fDrawingWireframe->setItemChecked (0,true);
970    fDrawingLineRemoval->setItemChecked (0,false);
971    fDrawingSurfaceRemoval->setItemChecked (0,false);
972    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
973#else
974    fDrawingWireframe->setChecked (true);
975    fDrawingLineRemoval->setChecked (false);
976    fDrawingSurfaceRemoval->setChecked (false);
977    fDrawingLineSurfaceRemoval->setChecked (false);
978#endif
979
980    d_style = G4ViewParameters::wireframe;
981
982  } else  if (aAction ==2) {
983#if QT_VERSION < 0x040000
984    fDrawingWireframe->setItemChecked (0,false);
985    fDrawingLineRemoval->setItemChecked (0,true);
986    fDrawingSurfaceRemoval->setItemChecked (0,false);
987    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
988#else
989    fDrawingWireframe->setChecked (false);
990    fDrawingLineRemoval->setChecked (true);
991    fDrawingSurfaceRemoval->setChecked (false);
992    fDrawingLineSurfaceRemoval->setChecked (false);
993#endif
994
995    d_style = G4ViewParameters::hlr;
996
997  } else  if (aAction ==3) {
998#if QT_VERSION < 0x040000
999    fDrawingWireframe->setItemChecked (0,false);
1000    fDrawingLineRemoval->setItemChecked (0,false);
1001    fDrawingSurfaceRemoval->setItemChecked (0,true);
1002    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
1003#else
1004    fDrawingWireframe->setChecked (false);
1005    fDrawingLineRemoval->setChecked (false);
1006    fDrawingSurfaceRemoval->setChecked (true);
1007    fDrawingLineSurfaceRemoval->setChecked (false);
1008#endif
1009
1010    d_style = G4ViewParameters::hsr;
1011
1012  } else  if (aAction ==4) {
1013#if QT_VERSION < 0x040000
1014    fDrawingWireframe->setItemChecked (0,false);
1015    fDrawingLineRemoval->setItemChecked (0,false);
1016    fDrawingSurfaceRemoval->setItemChecked (0,false);
1017    fDrawingLineSurfaceRemoval->setItemChecked (0,true);
1018#else
1019    fDrawingWireframe->setChecked (false);
1020    fDrawingLineRemoval->setChecked (false);
1021    fDrawingSurfaceRemoval->setChecked (false);
1022    fDrawingLineSurfaceRemoval->setChecked (true);
1023#endif
1024    d_style = G4ViewParameters::hlhsr;
1025  }
1026  fVP.SetDrawingStyle(d_style);
1027
1028  updateQWidget();
1029#ifdef GEANT4_QT_DEBUG
1030  printf("G4OpenGLQtViewer::toggleDrawingAction\n");
1031#endif
1032}
1033
1034
1035/**
1036   SLOT Activate by a click on the representation menu
1037   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1038   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1039   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1040   It will cause a redraw of the view
1041   @param check : 1 polyhedron, 0 nurbs
1042   @see G4OpenGLStoredQtViewer::DrawView
1043   @see G4XXXStoredViewer::CompareForKernelVisit
1044*/
1045void G4OpenGLQtViewer::toggleRepresentation(bool check) {
1046
1047  G4ViewParameters::RepStyle style;
1048  if (check == 1) {
1049    style = G4ViewParameters::polyhedron;
1050  } else {
1051    style = G4ViewParameters::nurbs;
1052  }
1053  fVP.SetRepStyle (style);
1054
1055#ifdef GEANT4_QT_DEBUG
1056  printf("G4OpenGLQtViewer::toggleRepresentation 3%d\n",check);
1057#endif
1058  updateQWidget();
1059#ifdef GEANT4_QT_DEBUG
1060  printf("G4OpenGLQtViewer::toggleRepresentation 4%d\n",check);
1061#endif
1062}
1063
1064/**
1065   SLOT Activate by a click on the background menu
1066@param check : 1 white, 0 black
1067*/
1068void G4OpenGLQtViewer::toggleBackground(bool check) {
1069
1070  //   //I need to revisit the kernel if the background colour changes and
1071  //   //hidden line removal is enabled, because hlr drawing utilises the
1072  //   //background colour in its drawing...
1073  //   // (Note added by JA 13/9/2005) Background now handled in view
1074  //   // parameters.  A kernel visit is triggered on change of background.
1075  if (check == 1) {
1076    ((G4ViewParameters&)this->GetViewParameters()).
1077      SetBackgroundColour(G4Colour(1.,1.,1.));  // White
1078  } else {
1079    ((G4ViewParameters&)this->GetViewParameters()).
1080      SetBackgroundColour(G4Colour(0.,0.,0.));  // Black
1081  }
1082  updateQWidget();
1083}
1084
1085/**
1086   SLOT Activate by a click on the transparency menu
1087@param check : 1 , 0
1088*/
1089void G4OpenGLQtViewer::toggleTransparency(bool check) {
1090 
1091  if (check) {
1092    transparency_enabled = false;
1093  } else {
1094    transparency_enabled = true;
1095  }
1096  SetNeedKernelVisit (true);
1097  updateQWidget();
1098}
1099
1100/**
1101   SLOT Activate by a click on the antialiasing menu
1102@param check : 1 , 0
1103*/
1104void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
1105
1106  if (!check) {
1107    antialiasing_enabled = false;
1108    glDisable (GL_LINE_SMOOTH);
1109    glDisable (GL_POLYGON_SMOOTH);
1110  } else {
1111    antialiasing_enabled = true;
1112    glEnable (GL_LINE_SMOOTH);
1113    glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
1114    glEnable (GL_POLYGON_SMOOTH);
1115    glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
1116  }
1117
1118  updateQWidget();
1119#ifdef GEANT4_QT_DEBUG
1120  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
1121#endif
1122}
1123
1124/**
1125   SLOT Activate by a click on the haloing menu
1126@param check : 1 , 0
1127*/
1128//FIXME : I SEE NOTHING...
1129void G4OpenGLQtViewer::toggleHaloing(bool check) {
1130  if (check) {
1131    haloing_enabled = false;
1132  } else {
1133    haloing_enabled = true;
1134  }
1135
1136  updateQWidget();
1137
1138#ifdef GEANT4_QT_DEBUG
1139  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
1140#endif
1141}
1142
1143/**
1144   SLOT Activate by a click on the auxiliaire edges menu
1145@param check : 1 , 0
1146*/
1147void G4OpenGLQtViewer::toggleAux(bool check) {
1148  if (check) {
1149    fVP.SetAuxEdgeVisible(false);
1150  } else {
1151    fVP.SetAuxEdgeVisible(true);
1152  }
1153  SetNeedKernelVisit (true);
1154  updateQWidget();
1155
1156#ifdef GEANT4_QT_DEBUG
1157  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
1158#endif
1159}
1160
1161/**
1162   SLOT Activate by a click on the full screen menu
1163@param check : 1 , 0
1164*/
1165void G4OpenGLQtViewer::toggleFullScreen(bool check) {
1166#if QT_VERSION >= 0x030200
1167  GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
1168#else
1169  G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1170#endif
1171#ifdef GEANT4_QT_DEBUG
1172  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
1173#endif
1174}
1175
1176void G4OpenGLQtViewer::actionCreateEPS() {
1177  QString filters;
1178#if QT_VERSION < 0x040000
1179  QStrList listFormat=QImageIO::outputFormats();
1180  char *tmp=listFormat.first();
1181  while (tmp!=0) {
1182    filters += QString(tmp) + ";;";
1183    tmp=listFormat.next();
1184  }
1185#else
1186  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
1187  for (int i = 0; i < formats.size(); ++i) {
1188    filters +=formats.at(i) + ";;";
1189  }
1190#endif
1191  filters += "eps;;";
1192  filters += "ps;;";
1193  filters += "pdf";
1194  QString* selectedFormat = new QString();
1195#if QT_VERSION < 0x040000
1196  QString nomFich =  QFileDialog::getSaveFileName ( ".",
1197                                                    filters,
1198                                                    GLWindow,
1199                                                    "Save file dialog",
1200                                                    tr("Save as ..."),
1201                                                    selectedFormat );
1202#else
1203  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
1204                                                    tr("Save as ..."),
1205                                                    ".",
1206                                                    filters,
1207                                                    selectedFormat );
1208#endif
1209  // bmp jpg jpeg png ppm xbm xpm
1210  if (nomFich == "") {
1211    return;
1212  }
1213#if QT_VERSION < 0x040000
1214  nomFich += "."+QString(selectedFormat->ascii());
1215  QString format = selectedFormat->lower();
1216#else
1217  nomFich += "."+QString(selectedFormat->toStdString().c_str());
1218  QString format = selectedFormat->toLower();
1219#endif
1220  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,format,fWindow->height(),fWindow->width());
1221  if(  exportDialog->exec()) {
1222
1223    QImage image;
1224    bool res = false;
1225    if ((exportDialog->getWidth() !=fWindow->width()) ||
1226        (exportDialog->getHeight() !=fWindow->height())) {
1227      if (format != QString("eps")) {
1228      G4cerr << "Export->Change Size : This function is not implemented, to export in another size, please resize your frame to what you need\n" << G4endl;
1229     
1230      //    rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1231      //      QGLWidget* glResized = fWindow;
1232
1233      // FIXME :
1234      // L.Garnier : I've try to implement change size function, but the problem is
1235      // the renderPixmap function call the QGLWidget to resize and it doesn't draw
1236      // the content of this widget... It only draw the background.
1237
1238      //      fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2,true );
1239
1240      //      QPixmap pixmap = fWindow->renderPixmap ();
1241     
1242      //      image = pixmap->toImage();
1243      //      glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1244      //      image = glResized->grabFrameBuffer();
1245      }     
1246    } else {
1247      image = fWindow->grabFrameBuffer();
1248    }   
1249    if (format == QString("eps")) {
1250      if (exportDialog->getVectorEPS()) {
1251        res = generateVectorEPS(nomFich,exportDialog->getWidth(),exportDialog->getHeight(),image);
1252      } else {
1253        res = generateEPS(nomFich,exportDialog->getNbColor(),image);
1254      }
1255    } else if ((format == "ps") || (format == "pdf")) {
1256      res = generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1257    } else if ((format == "tif") ||
1258               (format == "tiff") ||
1259               (format == "jpg") ||
1260               (format == "jpeg") ||
1261               (format == "png") ||
1262               (format == "pbm") ||
1263               (format == "pgm") ||
1264               (format == "ppm") ||
1265               (format == "bmp") ||
1266               (format == "xbm") ||
1267               (format == "xpm")) {
1268#if QT_VERSION < 0x040000
1269      res = image.save(nomFich,selectedFormat->ascii(),exportDialog->getSliderValue());
1270#else
1271      res = image.save(nomFich,0,exportDialog->getSliderValue());
1272#endif
1273    } else {
1274      G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1275    }
1276    if (res == false) {
1277#if QT_VERSION < 0x040000
1278      G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1279#else
1280      G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1281#endif
1282    } else {
1283#if QT_VERSION < 0x040000
1284      G4cout << "File "<<nomFich.ascii()<<" has been saved \n" << G4endl;
1285#else
1286      G4cout << "File "<<nomFich.toStdString().c_str()<<" has been saved \n" << G4endl;
1287#endif
1288    }
1289   
1290  } else { // cancel selected
1291    return;
1292  }
1293 
1294#ifdef GEANT4_QT_DEBUG
1295  printf("G4OpenGLQtViewer::actionCreateEPS() \n");
1296#endif
1297}
1298
1299/*
1300// http://www.google.com/codesearch?hl=en&q=+jpg+Qt+quality+QDialog+show:FZkUoth8oiw:TONpW2mR-_c:tyTfrKMO-xI&sa=N&cd=2&ct=rc&cs_p=http://soft.proindependent.com/src/qtiplot-0.8.9.zip&cs_f=qtiplot-0.8.9/qtiplot/src/application.cpp#a0
1301
1302void Graph::exportToSVG(const QString& fname)
1303{
1304  // enable workaround for Qt3 misalignments
1305  QwtPainter::setSVGMode(true);
1306  QPicture picture;
1307  QPainter p(&picture);
1308  d_plot->print(&p, d_plot->rect());
1309  p.end();
1310
1311  picture.save(fname, "svg");
1312}
1313*/
1314
1315
1316
1317
1318/**
1319   Save the current mouse press point
1320   @param p mouse click point
1321*/
1322void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
1323{
1324  fLastPos = p;
1325  if (fMouseAction == STYLE4){  // pick
1326    Pick(p.x(),p.y());
1327  }
1328}
1329
1330
1331/**
1332   @param pos_x mouse x position
1333   @param pos_y mouse y position
1334   @param mButtons mouse button active
1335*/
1336
1337#if QT_VERSION < 0x040000
1338void G4OpenGLQtViewer::G4MouseEvent(int pos_x, int pos_y,Qt::ButtonState mButtons)
1339#else
1340void G4OpenGLQtViewer::G4MouseEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons)
1341#endif
1342{
1343  int dx = pos_x - fLastPos.x();
1344  int dy = pos_y - fLastPos.y();
1345  if (fMouseAction == STYLE1) {  // rotate
1346    if (mButtons & Qt::LeftButton) {
1347      G4MouseRotateEvent(dx,dy);
1348    }
1349  } else   if (fMouseAction == STYLE2){  // move
1350    if (mButtons & Qt::LeftButton) {
1351      G4MouseMoveEvent(dx,dy,0);
1352    }
1353  } else   if (fMouseAction == STYLE3){  // zoom
1354    if (mButtons & Qt::LeftButton) {
1355      G4cerr << "Zoom not implemented for the moment\n" << G4endl;
1356    }
1357  }
1358  fLastPos = QPoint(pos_x, pos_y);
1359}
1360
1361/**
1362   @param dx delta mouse x position
1363   @param dy delta mouse y position
1364*/
1365
1366void G4OpenGLQtViewer::G4MouseMoveEvent(int dx, int dy, int dz)
1367{
1368  G4Point3D stp
1369    = GetSceneHandler()->GetScene()->GetStandardTargetPoint();
1370 
1371  G4Point3D tp = stp + fVP.GetCurrentTargetPoint ();
1372 
1373  const G4Vector3D& upVector = fVP.GetUpVector ();
1374  const G4Vector3D& vpVector = fVP.GetViewpointDirection ();
1375 
1376  G4Vector3D unitRight = (upVector.cross (vpVector)).unit();
1377  G4Vector3D unitUp    = (vpVector.cross (unitRight)).unit();
1378 
1379#ifdef GEANT4_QT_DEBUG
1380  G4cout << fVP<<G4endl;
1381  printf("Mouse event Target Point : %f %f %f\n",tp.x(),tp.y(),tp.z());
1382  printf("Mouse event upVector : %f %f %f\n",upVector.x(),upVector.y(),upVector.z());
1383  printf("Mouse event vpVector Point : %f %f %f\n",vpVector.x(),vpVector.y(),vpVector.z());
1384  printf("Mouse event unitRight Point : %f %f %f\n",unitRight.x(),unitRight.y(),unitRight.z());
1385  printf("Mouse event unitUp Point : %f %f %f\n",unitUp.x(),unitUp.y(),unitUp.z());
1386#endif
1387  tp += -dx * unitRight + dy * unitUp + dz * vpVector;
1388  fVP.SetCurrentTargetPoint (tp - stp);
1389 
1390  updateQWidget();
1391}
1392
1393
1394/**
1395   @param dx delta mouse x position
1396   @param dy delta mouse y position
1397*/
1398
1399void G4OpenGLQtViewer::G4MouseRotateEvent(int dx, int dy)
1400{
1401  //phi spin stuff here
1402 
1403  G4Vector3D vp = fVP.GetViewpointDirection ().unit ();
1404  G4Vector3D up = fVP.GetUpVector ().unit ();
1405 
1406  G4Vector3D yprime = (up.cross(vp)).unit();
1407  G4Vector3D zprime = (vp.cross(yprime)).unit();
1408 
1409  G4double delta_alpha;
1410  G4double delta_theta;
1411 
1412  if (fVP.GetLightsMoveWithCamera()) {
1413    delta_alpha = dy;
1414    delta_theta = -dx;
1415  } else {
1416    delta_alpha = -dy;
1417    delta_theta = dx;
1418  }   
1419 
1420  delta_alpha *= deg;
1421  delta_theta *= deg;
1422 
1423  G4Vector3D new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1424 
1425  G4Vector3D new_up;
1426  if (fVP.GetLightsMoveWithCamera()) {
1427    new_up = (new_vp.cross(yprime)).unit();
1428    fVP.SetUpVector(new_up);
1429  } else {
1430    new_up = up;
1431  }
1432  ////////////////
1433  // Rotates by fixed azimuthal angle delta_theta.
1434 
1435  G4double cosalpha = new_up.dot (new_vp.unit());
1436  G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1437  yprime = (new_up.cross (new_vp.unit())).unit ();
1438  G4Vector3D xprime = yprime.cross (new_up);
1439  // Projection of vp on plane perpendicular to up...
1440  G4Vector3D a1 = sinalpha * xprime;
1441  // Required new projection...
1442  G4Vector3D a2 =
1443    sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1444  // Required Increment vector...
1445  G4Vector3D delta = a2 - a1;
1446  // So new viewpoint is...
1447  G4Vector3D viewPoint = new_vp.unit() + delta;
1448 
1449  fVP.SetViewAndLights (viewPoint);
1450  updateQWidget();
1451 
1452}
1453
1454/** This is the benning of a rescale function. It does nothing for the moment
1455    @param aWidth : new width
1456    @param aHeight : new height
1457*/
1458void G4OpenGLQtViewer::rescaleImage(
1459 int aWidth
1460,int aHeight
1461){
1462#ifdef GEANT4_QT_DEBUG
1463  printf("should rescale \n");
1464#endif
1465  GLfloat* feedback_buffer;
1466  GLint returned;
1467  FILE* file;
1468 
1469//   feedback_buffer = new GLfloat[size];
1470//   glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1471//   glRenderMode (GL_FEEDBACK);
1472 
1473//   glViewport (0, 0, aWidth, aHeight);
1474//   DrawView();
1475//   returned = glRenderMode (GL_RENDER);
1476
1477}
1478
1479/**
1480   Generate Vectorial Encapsulated Postscript form image
1481   @param aFilename : name of file
1482   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1483   @param aImage : Image to print
1484*/
1485bool G4OpenGLQtViewer::generateVectorEPS (
1486 QString aFilename
1487,int aWidth
1488,int aHeight
1489,QImage aImage
1490)
1491{
1492  // Print vectored PostScript
1493 
1494  G4int size = 5000000;
1495
1496  GLfloat* feedback_buffer;
1497  GLint returned;
1498  FILE* file;
1499 
1500  feedback_buffer = new GLfloat[size];
1501  glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1502  glRenderMode (GL_FEEDBACK);
1503 
1504  int side = qMin(aWidth, aHeight);
1505  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
1506  DrawView();
1507
1508  returned = glRenderMode (GL_RENDER);
1509 
1510 
1511#if QT_VERSION < 0x040000
1512  file = fopen (aFilename.ascii(), "w");
1513#else
1514  file = fopen (aFilename.toStdString().c_str(), "w");
1515#endif
1516  if (file) {
1517    spewWireframeEPS (file, returned, feedback_buffer, "rendereps");
1518  } else {
1519#if QT_VERSION < 0x040000
1520    G4cerr << "Could not open "<< aFilename.ascii()<<"\n" << G4endl;
1521#else
1522    G4cerr << "Could not open "<< aFilename.toStdString().c_str()<<"\n" << G4endl;
1523#endif
1524  }
1525 
1526  delete[] feedback_buffer;
1527
1528  return true;
1529}
1530
1531/**
1532   Generate Encapsulated Postscript form image
1533   @param aFilename : name of file
1534   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1535   @param aImage : Image to print
1536*/
1537bool G4OpenGLQtViewer::generateEPS (
1538 QString aFilename
1539,int aInColor
1540,QImage aImage
1541)
1542{
1543  // FIXME
1544#ifdef GEANT4_QT_DEBUG
1545  printf("saving EPS\n");
1546#endif
1547
1548  FILE* fp;
1549
1550  if (aImage.bits () == NULL)
1551    return false;
1552 
1553#if QT_VERSION < 0x040000
1554  fp = fopen (aFilename.ascii(), "w");
1555#else
1556  fp = fopen (aFilename.toStdString().c_str(), "w");
1557#endif
1558  if (fp == NULL) {
1559    return false;
1560  }
1561 
1562  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
1563#if QT_VERSION < 0x040000
1564  fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1565#else
1566  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
1567#endif
1568  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
1569  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
1570  fprintf (fp, "%%%%EndComments\n");
1571  fprintf (fp, "gsave\n");
1572  fprintf (fp, "/bwproc {\n");
1573  fprintf (fp, "    rgbproc\n");
1574  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
1575  fprintf (fp, "    5 -1 roll {\n");
1576  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
1577  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1578  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1579  fprintf (fp, "    { 2 1 roll } ifelse\n");
1580  fprintf (fp, "    }forall\n");
1581  fprintf (fp, "    pop pop pop\n");
1582  fprintf (fp, "} def\n");
1583  fprintf (fp, "systemdict /colorimage known not {\n");
1584  fprintf (fp, "   /colorimage {\n");
1585  fprintf (fp, "       pop\n");
1586  fprintf (fp, "       pop\n");
1587  fprintf (fp, "       /rgbproc exch def\n");
1588  fprintf (fp, "       { bwproc } image\n");
1589  fprintf (fp, "   }  def\n");
1590  fprintf (fp, "} if\n");
1591  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1592  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1593  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1594  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
1595  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1596  fprintf (fp, "false %d\n", aInColor);
1597  fprintf (fp, "colorimage\n");
1598 
1599
1600  int width = aImage.width();
1601  int height = aImage.height();
1602  int depth = aImage.depth();
1603  int size = width*height;
1604 
1605  if (depth == 1)
1606    size = (width+7)/8*height;
1607  else if (aInColor == 1)
1608    size = size*3;
1609 
1610  int i = 0;
1611  //  if ( aInColor ==1 ) {
1612  // FIXME : L. Garnier. For the moment 10 dec 2007, I could not find a way
1613  // to save correctly grayscale Image. I mean that color or grayscale image
1614  // have the same file save size !
1615 
1616  /* } else*/ if (depth == 8) {
1617    for(int y=height-1; y >=0 ; y--) {
1618      const uchar * s = aImage.scanLine(y);
1619      for(int x=0; x <width; x++) {
1620        QRgb rgb = aImage.color(s[x]);
1621        if (aInColor == 1) {
1622          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1623          i++;
1624        } else {
1625          fprintf (fp, " %02hx %02hx %02hx",
1626                   (unsigned char) qRed(rgb),
1627                   (unsigned char) qGreen(rgb),
1628                   (unsigned char) qBlue(rgb));
1629          i += 3;
1630        }
1631      }
1632      fprintf (fp, "\n");
1633    }
1634  } else {
1635#if QT_VERSION < 0x040000
1636    bool alpha = aImage.hasAlphaBuffer();
1637#else
1638    bool alpha = aImage.hasAlphaChannel();
1639    for(int y=height-1; y >=0 ; y--) {
1640      QRgb * s = (QRgb*)(aImage.scanLine(y));
1641      for(int x=0; x <width; x++) {
1642        QRgb rgb = (*s++);
1643        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1644          rgb = qRgb(0xff, 0xff, 0xff);
1645        if (aInColor == 1) {
1646          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1647          i++;
1648        } else {
1649          fprintf (fp, " %02hx %02hx %02hx",
1650                   (unsigned char) qRed(rgb),
1651                   (unsigned char) qGreen(rgb),
1652                   (unsigned char) qBlue(rgb));
1653          i += 3;
1654        }
1655      }
1656      fprintf (fp, "\n");
1657    }
1658#endif
1659
1660  }
1661
1662  fprintf (fp, "grestore\n");
1663  fprintf (fp, "showpage\n");
1664  fclose (fp);
1665
1666  return true;
1667}
1668/**
1669   Generate Postscript or PDF form image
1670   @param aFilename : name of file
1671   @param aInColor : numbers of colors : 1->BW 2->RGB
1672   @param aImage : Image to print
1673*/
1674bool G4OpenGLQtViewer::generatePS_PDF (
1675 QString aFilename
1676,int aInColor
1677,QImage aImage
1678)
1679{
1680
1681#if QT_VERSION < 0x040000
1682#ifdef Q_WS_MAC || Q_WS_X11
1683  QPrinter printer;
1684  //  printer.setPageSize(pageSize);
1685  if (aInColor == 1) {
1686    printer.setColorMode(QPrinter::GrayScale);
1687  } else {
1688    printer.setColorMode(QPrinter::Color);
1689  }
1690
1691  /* FIXME : I don't know which format it will save...
1692     if (aFilename.endsWith(".ps")) {
1693     printer.setOutputFormat(QPrinter::PostScriptFormat);
1694     } else {
1695     printer.setOutputFormat(QPrinter::PdfFormat);
1696     }
1697  */
1698  printer.setOutputFileName(aFilename);
1699  //  printer.setFullPage ( true);
1700  QPainter paint(&printer);
1701  paint.drawImage (0,0,aImage );
1702  paint.end();
1703#else
1704  G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4\n" << G4endl;
1705#endif
1706#else
1707  QPrinter printer;
1708  //  printer.setPageSize(pageSize);
1709
1710  // FIXME : L. Garnier 4/12/07
1711  // This is not working, it does nothing. Image is staying in color mode
1712  // So I have desactivate the B/W button in GUI
1713  if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1714#if QT_VERSION < 0x040000
1715    aImage = aImage.convertDepth(1,Qt::MonoOnly);
1716#else
1717    aImage = aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1718#endif
1719  }
1720
1721
1722  if (aFilename.endsWith(".ps")) {
1723#if QT_VERSION > 0x040200
1724    printer.setOutputFormat(QPrinter::PostScriptFormat);
1725#endif
1726  } else {
1727#if QT_VERSION > 0x040100
1728    printer.setOutputFormat(QPrinter::PdfFormat);
1729#endif
1730  }
1731#if QT_VERSION > 0x040100
1732  printer.setOutputFileName(aFilename);
1733#endif
1734  //  printer.setFullPage ( true);
1735  QPainter paint(&printer);
1736  paint.drawImage (0,0,aImage);
1737  paint.end();
1738#endif
1739  return true;
1740}
1741
1742#endif
1743
1744void G4OpenGLQtViewer::G4keyPressEvent (QKeyEvent * event)
1745{
1746#ifdef GEANT4_QT_DEBUG
1747  printf("G4OpenGLQtViewer::keyPressEvent count:%d autoRepeat:%d key:%d\n",event->count(),event->isAutoRepeat(),event->key());
1748#endif
1749  if (event->key() == Qt::Key_Down)
1750    {
1751      G4MouseMoveEvent(0,0,-1);
1752    }
1753  if (event->key() == Qt::Key_Up)
1754    {
1755      G4MouseMoveEvent(0,0,1);
1756    }
1757  if (event->key() == Qt::Key_Left)
1758    {
1759      G4MouseMoveEvent(-1,0,0);
1760    }
1761  if (event->key() == Qt::Key_Right)
1762    {
1763      G4MouseMoveEvent(1,0,0);
1764    }
1765  if (event->key() == Qt::Key_Plus)
1766    {
1767      fVP.SetZoomFactor(fVP.GetZoomFactor()*fDeltaZoom);
1768      updateQWidget();
1769    }
1770  if (event->key() == Qt::Key_Minus)
1771    {
1772      fVP.SetZoomFactor(fVP.GetZoomFactor()/fDeltaZoom);
1773      updateQWidget();
1774    }
1775}
1776
1777/*
1778 
1779void MultiLayer::exportToSVG(const QString& fname)
1780{
1781  QPicture picture;
1782  QPainter p(&picture);
1783  for (int i=0;i<(int)graphsList->count();i++)
1784    {
1785      Graph *gr=(Graph *)graphsList->at(i);
1786      Plot *myPlot= (Plot *)gr->plotWidget();
1787     
1788      QPoint pos=gr->pos();
1789     
1790      int width=int(myPlot->frameGeometry().width());
1791      int height=int(myPlot->frameGeometry().height());
1792     
1793      myPlot->print(&p, QRect(pos,QSize(width,height)));
1794    }
1795 
1796  p.end();
1797  picture.save(fname, "svg");
1798}
1799*/
Note: See TracBrowser for help on using the repository browser.