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

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

debud de la video

  • Property svn:mime-type set to text/cpp
File size: 69.6 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.10 2008/01/30 10:54:13 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#include "G4UnitsTable.hh"
51#include "G4Qt.hh"
52#include "G4UImanager.hh"
53#include "G4UIcommandTree.hh"
54#include <qapplication.h>
55#include <qlayout.h>
56#include <qdialog.h>
57#include <qprocess.h>
58
59#if QT_VERSION >= 0x040000
60#include <qmenu.h>
61#include <qimagewriter.h>
62#else
63#include <qaction.h>
64#include <qwidgetlist.h>
65#include <qpopupmenu.h>
66#include <qimage.h>
67#endif
68
69#include <qapplication.h>
70#include <qmessagebox.h>
71#include <qfiledialog.h>
72#include <qprinter.h>
73#include <qpainter.h>
74#include <qgl.h> // include <qglwidget.h>
75#include <qdialog.h>
76#include <qevent.h> //include <qcontextmenuevent.h>
77
78
79//////////////////////////////////////////////////////////////////////////////
80/**
81   Implementation of virtual method of G4VViewer
82*/
83void G4OpenGLQtViewer::SetView (
84)
85//////////////////////////////////////////////////////////////////////////////
86//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
87{
88#ifdef GEANT4_QT_DEBUG
89  printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
90#endif
91  //   if(!fHDC) return;
92  //   if(!fHGLRC) return;
93  //   ::wglMakeCurrent(fHDC,fHGLRC);
94  //  fWindow->makeCurrent();
95  G4OpenGLViewer::SetView ();
96#ifdef GEANT4_QT_DEBUG
97  printf("G4OpenGLQtViewer::SetView --------------------\n");
98#endif
99}
100
101/**
102 * Set the viewport of the scene
103 */
104void G4OpenGLQtViewer::setupViewport(int aWidth, int aHeight)
105{
106  int side = aWidth;
107  if (aHeight < aWidth) side = aHeight;
108  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
109 
110  glMatrixMode(GL_PROJECTION);
111  glLoadIdentity();
112  glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
113  glMatrixMode(GL_MODELVIEW);
114}
115
116
117//////////////////////////////////////////////////////////////////////////////
118/**
119   Implementation of virtual method of G4VViewer
120*/
121void G4OpenGLQtViewer::ShowView (
122)
123//////////////////////////////////////////////////////////////////////////////
124//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
125{
126#ifdef GEANT4_QT_DEBUG
127  printf("G4OpenGLQtViewer::ShowView  +++++++++++++++++++++\n");
128#endif
129  if (!GLWindow) {
130    G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
131  } else {
132#if QT_VERSION < 0x040000
133    GLWindow->setActiveWindow();
134#else
135    GLWindow->activateWindow();
136#endif
137#ifdef GEANT4_QT_DEBUG
138    printf("G4OpenGLQtViewer::ShowView -----------------------\n");
139#endif
140  }
141  glFlush ();
142  //   // Empty the Windows message queue :
143  //   MSG event;
144  //   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
145  //     ::TranslateMessage(&event);
146  //     ::DispatchMessage (&event);
147  //   }
148}
149
150
151
152//////////////////////////////////////////////////////////////////////////////
153void G4OpenGLQtViewer::CreateGLQtContext (
154)
155//////////////////////////////////////////////////////////////////////////////
156//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
157{
158#ifdef GEANT4_QT_DEBUG
159  printf("G4OpenGLQtViewer::CreateGLQtContext \n");
160#endif
161}
162
163
164//////////////////////////////////////////////////////////////////////////////
165void G4OpenGLQtViewer::CreateMainWindow (
166 QGLWidget* glWidget
167)
168//////////////////////////////////////////////////////////////////////////////
169//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
170{
171
172  if(fWindow) return; //Done.
173#ifdef GEANT4_QT_DEBUG
174  printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
175#endif
176
177  // launch Qt if not
178  G4Qt* interactorManager = G4Qt::getInstance ();
179  //  G4UImanager* UI = G4UImanager::GetUIpointer();
180
181  fWindow = glWidget ;
182  //  fWindow->makeCurrent();
183
184  // create window
185  if (((QApplication*)interactorManager->GetMainInteractor())) {
186    // look for the main window
187    bool found = false;
188#if QT_VERSION < 0x040000
189    // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
190    // but if I comment them, it doesn't work...
191    QWidgetList  *list = QApplication::allWidgets();
192    QWidgetListIt it( *list );         // iterate over the widgets
193    QWidget * widget;
194    while ( (widget=it.current()) != 0 ) {  // for each widget...
195      ++it;
196      if ((found== false) && (widget->inherits("QMainWindow"))) {
197        GLWindow = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
198        found = true;
199      }
200    }
201    delete list;                      // delete the list, not the widgets
202#else
203    foreach (QWidget *widget, QApplication::allWidgets()) {
204      if ((found== false) && (widget->inherits("QMainWindow"))) {
205        GLWindow = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
206        found = true;
207      }
208    }
209#endif
210
211#if QT_VERSION < 0x040000
212    glWidget->reparent(GLWindow,0,QPoint(0,0)); 
213#else
214    glWidget->setParent(GLWindow); 
215#endif
216
217    if (found==false) {
218#ifdef GEANT4_QT_DEBUG
219      printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
220#endif
221      GLWindow = new QDialog();
222    }
223  } else {
224#ifdef GEANT4_QT_DEBUG
225    printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
226#endif
227    GLWindow = new QDialog();
228  }
229
230  QHBoxLayout *mainLayout = new QHBoxLayout(GLWindow);
231
232  mainLayout->addWidget(fWindow);
233
234#if QT_VERSION < 0x040000
235  GLWindow->setCaption( tr( "QGl Viewer" ));
236#else
237  GLWindow->setLayout(mainLayout);
238  GLWindow->setWindowTitle(tr("QGl Viewer"));
239#endif
240  GLWindow->resize(300, 300);
241  GLWindow->move(900,300);
242  GLWindow->show();
243 
244  // delete the pointer if close this
245  //  GLWindow->setAttribute(Qt::WA_DeleteOnClose);
246
247#if QT_VERSION >= 0x040000
248//   QObject ::connect(GLWindow,
249//                     SIGNAL(rejected()),
250//                     this,
251//                     SLOT(dialogClosed()));
252#endif
253
254  WinSize_x = 400;
255  WinSize_y = 400;
256  if (WinSize_x < fVP.GetWindowSizeHintX ())
257    WinSize_x = fVP.GetWindowSizeHintX ();
258  if (WinSize_y < fVP.GetWindowSizeHintY ())
259    WinSize_y = fVP.GetWindowSizeHintY ();
260
261  if(!fWindow) return;
262#ifdef GEANT4_QT_DEBUG
263  printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
264#endif
265
266  if (!fContextMenu)
267    createPopupMenu();
268
269}
270
271#if QT_VERSION >= 0x040000
272/**  Close the dialog and set the pointer to NULL
273 */
274// void G4OpenGLQtViewer::dialogClosed() {
275// #ifdef GEANT4_QT_DEBUG
276//   printf("G4OpenGLQtViewer::dialogClosed END\n");
277// #endif
278//   //  GLWindow = NULL;
279// }
280#endif
281
282//////////////////////////////////////////////////////////////////////////////
283G4OpenGLQtViewer::G4OpenGLQtViewer (
284                                    G4OpenGLSceneHandler& scene
285                                    )
286  :G4VViewer (scene, -1)
287  ,G4OpenGLViewer (scene)
288  ,fWindow(0)
289  ,fRecordFrames(false)
290  ,fRecordFrameNumber(0)
291  ,fContextMenu(0)
292  ,fMouseAction(STYLE1)
293  ,fDeltaRotation(1)
294  ,fDeltaSceneTranslation(0.01)
295  ,fDeltaDepth(0.01)
296  ,fDeltaZoom(0.1)
297  ,fDeltaMove(0.2)
298  ,fHoldKeyEvent(false)
299  ,fAutoMove(false)
300{
301#ifdef GEANT4_QT_DEBUG
302  printf("G4OpenGLQtViewer::G4OpenGLQtViewer \n");
303#endif
304}
305
306//////////////////////////////////////////////////////////////////////////////
307G4OpenGLQtViewer::~G4OpenGLQtViewer (
308)
309//////////////////////////////////////////////////////////////////////////////
310//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
311{
312#ifdef GEANT4_QT_DEBUG
313  printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
314#endif
315}
316
317
318/**
319   Create a popup menu for the widget. This menu is activated by right-mouse click
320*/
321void G4OpenGLQtViewer::createPopupMenu()    {
322
323#if QT_VERSION < 0x040000
324  fContextMenu = new QPopupMenu( GLWindow,"All" );
325#else
326  fContextMenu = new QMenu("All");
327#endif
328
329#if QT_VERSION < 0x040000
330  QPopupMenu *mMouseAction = new QPopupMenu( fContextMenu );
331  fContextMenu->insertItem("&Mouse actions",mMouseAction);
332#if QT_VERSION < 0x030200
333  fRotateAction =  new QAction("&Rotate","&Rotate",CTRL+Key_R,mMouseAction,0,true);
334  fMoveAction =  new QAction("&Move","&Move",CTRL+Key_M,mMouseAction,0,true);
335  fPickAction =  new QAction("&Pick","&Pick",CTRL+Key_P,mMouseAction,0,true);
336  QAction * shortcutsAction =  new QAction("&Show shortcuts","&Show shortcuts",CTRL+Key_S,mMouseAction,0,true);
337#else
338  fRotateAction =  new QAction("&Rotate",CTRL+Key_R,mMouseAction);
339  fMoveAction =  new QAction("&Move",CTRL+Key_M,mMouseAction);
340  fPickAction =  new QAction("&Pick",CTRL+Key_P,mMouseAction);
341  QAction *shortcutsAction =  new QAction("&Show shortcuts",CTRL+Key_S,mMouseAction);
342#endif
343  fRotateAction->addTo(mMouseAction);
344  fMoveAction->addTo(mMouseAction);
345  fPickAction->addTo(mMouseAction);
346  shortcutsAction->addTo(mMouseAction);
347
348  fRotateAction->setToggleAction(true);
349  fMoveAction->setToggleAction(true);
350  fPickAction->setToggleAction(true);
351  shortcutsAction->setToggleAction(true);
352
353  fRotateAction->setOn(true);
354  fMoveAction->setOn(false);
355  fPickAction->setOn(false);
356  shortcutsAction->setOn(false);
357
358
359  QObject ::connect(fRotateAction,
360                    SIGNAL(activated()),
361                    this,
362                    SLOT(actionMouseRotate()));
363
364  QObject ::connect(fMoveAction,
365                    SIGNAL(activated()),
366                    this,
367                    SLOT(actionMouseMove()));
368
369  QObject ::connect(fPickAction,
370                    SIGNAL(activated()),
371                    this,
372                    SLOT(actionMousePick()));
373
374  QObject ::connect(shortcutsAction,
375                    SIGNAL(activated()),
376                    this,
377                    SLOT(showShortcuts()));
378
379#else
380  QMenu *mMouseAction = fContextMenu->addMenu("&Mouse actions");
381
382  fRotateAction = mMouseAction->addAction("Rotate");
383  fMoveAction = mMouseAction->addAction("Move");
384  fPickAction = mMouseAction->addAction("Pick");
385  QAction *shortcutsAction = mMouseAction->addAction("Show shortcuts");
386
387  fRotateAction->setCheckable(true);
388  fMoveAction->setCheckable(false);
389  fPickAction->setCheckable(false);
390  shortcutsAction->setCheckable(false);
391
392  fRotateAction->setChecked(true);
393  fMoveAction->setChecked(false);
394  fPickAction->setChecked(false);
395  shortcutsAction->setChecked(false);
396
397  QObject ::connect(fRotateAction,
398                    SIGNAL(triggered(bool)),
399                    this,
400                    SLOT(actionMouseRotate()));
401
402  QObject ::connect(fMoveAction,
403                    SIGNAL(triggered(bool)),
404                    this,
405                    SLOT(actionMouseMove()));
406
407  QObject ::connect(fPickAction,
408                    SIGNAL(triggered(bool)),
409                    this,
410                    SLOT(actionMousePick()));
411
412  QObject ::connect(shortcutsAction,
413                    SIGNAL(triggered(bool)),
414                    this,
415                    SLOT(showShortcuts()));
416#endif
417
418#if QT_VERSION < 0x040000
419  // === Style Menu ===
420  QPopupMenu *mStyle = new QPopupMenu(fContextMenu);
421
422  QPopupMenu *mRepresentation = new QPopupMenu(fContextMenu);
423
424  QPopupMenu *mProjection = new QPopupMenu(fContextMenu);
425
426#if QT_VERSION < 0x030200
427  QAction *polyhedron = new QAction("&Polyhedron","&Polyhedron",CTRL+Key_P,mRepresentation,0,true);
428  QAction *nurbs = new QAction("&NURBS","&NURBS",CTRL+Key_N,mRepresentation,0,true);
429
430  QAction *ortho = new QAction("&Orthographic","&Orthographic",CTRL+Key_O,mProjection,0,true);
431  QAction *perspective = new QAction("&Perspective","&Perspective",CTRL+Key_P,mProjection,0,true);
432#else
433  QAction *polyhedron = new QAction("&Polyhedron",CTRL+Key_P,mRepresentation);
434  QAction *nurbs = new QAction("&NURBS",CTRL+Key_N,mRepresentation);
435
436  QAction *ortho = new QAction("&Orthographic",CTRL+Key_O,mProjection);
437  QAction *perspective = new QAction("&Perspective",CTRL+Key_P,mProjection);
438  polyhedron->setToggleAction(true);
439  nurbs->setToggleAction(true);
440  ortho->setToggleAction(true);
441  perspective->setToggleAction(true);
442#endif
443  polyhedron->addTo(mRepresentation);
444  nurbs->addTo(mRepresentation);
445
446  ortho->addTo(mProjection);
447  perspective->addTo(mProjection);
448
449  mStyle->insertItem("&Representation",mRepresentation);
450  mStyle->insertItem("&Projection",mProjection);
451  fContextMenu->insertItem("&Style",mStyle);
452
453
454#else
455  // === Style Menu ===
456  QMenu *mStyle = fContextMenu->addMenu("&Style");
457
458  QMenu *mRepresentation = mStyle->addMenu("&Representation");
459  QMenu *mProjection = mStyle->addMenu("&Projection");
460  QAction *polyhedron = mRepresentation->addAction("Polyhedron");
461  QAction *nurbs = mRepresentation->addAction("NURBS");
462
463  QAction *ortho = mProjection->addAction("Orthographic");
464  QAction *perspective = mProjection->addAction("Persepective");
465#endif
466
467  // INIT mRepresentation
468  G4ViewParameters::RepStyle style;
469  style = fVP.GetRepStyle();
470  if (style == G4ViewParameters::polyhedron) {
471    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
472  } else if (style == G4ViewParameters::nurbs) {
473    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
474  } else {
475    mRepresentation->clear();
476  }
477
478  // INIT mProjection
479  if (fVP.GetFieldHalfAngle() == 0) {
480    createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),1);
481  } else {
482    createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),2);
483  }
484
485#if QT_VERSION < 0x040000
486  // === Drawing Menu ===
487  QPopupMenu *mDrawing = new QPopupMenu(fContextMenu);
488  fContextMenu->insertItem("&Drawing",mDrawing);
489
490#if QT_VERSION < 0x030200
491  fDrawingWireframe = new QAction("&Wireframe","&Wireframe",CTRL+Key_W,mDrawing,0,true);
492  fDrawingLineRemoval = new QAction("&Hidden line removal","&Hidden line removal",CTRL+Key_L,mDrawing,0,true);
493  fDrawingSurfaceRemoval = new QAction("&Hidden surface removal","&Hidden surface removal",CTRL+Key_S,mDrawing,0,true);
494  fDrawingLineSurfaceRemoval = new QAction("&Hidden line and surface removal","&Hidden line and surface removal",CTRL+Key_R,mDrawing,0,true);
495#else
496  fDrawingWireframe = new QAction("&Wireframe",CTRL+Key_W,mDrawing);
497  fDrawingLineRemoval = new QAction("&Hidden line removal",CTRL+Key_L,mDrawing);
498  fDrawingSurfaceRemoval = new QAction("&Hidden surface removal",CTRL+Key_S,mDrawing);
499  fDrawingLineSurfaceRemoval = new QAction("&Hidden line and surface removal",CTRL+Key_R,mDrawing);
500#endif
501  fDrawingWireframe->setToggleAction(true);
502  fDrawingLineRemoval->setToggleAction(true);
503  fDrawingSurfaceRemoval->setToggleAction(true);
504  fDrawingLineSurfaceRemoval->setToggleAction(true);
505
506  fDrawingWireframe->addTo(mDrawing);
507  fDrawingLineRemoval->addTo(mDrawing);
508  fDrawingSurfaceRemoval->addTo(mDrawing);
509  fDrawingLineSurfaceRemoval->addTo(mDrawing);
510
511
512#else
513  // === Drawing Menu ===
514  QMenu *mDrawing = mStyle->addMenu("&Drawing");
515
516  fDrawingWireframe = mDrawing->addAction("Wireframe");
517  fDrawingWireframe->setCheckable(true);
518
519  fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
520  fDrawingLineRemoval->setCheckable(true);
521
522  fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
523  fDrawingSurfaceRemoval->setCheckable(true);
524
525  fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
526  fDrawingLineSurfaceRemoval->setCheckable(true);
527#endif
528  // INIT Drawing
529  G4ViewParameters::DrawingStyle d_style;
530  d_style = fVP.GetDrawingStyle();
531 
532#if QT_VERSION < 0x040000
533  if (d_style == G4ViewParameters::wireframe) {
534    fDrawingWireframe->setOn(true);
535  } else if (d_style == G4ViewParameters::hlr) {
536    fDrawingLineRemoval->setOn(true);
537  } else if (d_style == G4ViewParameters::hsr) {
538    fDrawingSurfaceRemoval->setOn(true);
539  } else if (d_style == G4ViewParameters::hlhsr) {
540    fDrawingLineSurfaceRemoval->setOn(true);
541  } else {
542    mDrawing->clear();
543  }
544#ifdef GEANT4_QT_DEBUG
545  printf("G4OpenGLQtViewer:: fDrawingWireframe 1\n");
546#endif
547  QObject ::connect(fDrawingWireframe,
548                    SIGNAL(activated()),
549                    this,
550                    SLOT(actionDrawingWireframe()));
551#ifdef GEANT4_QT_DEBUG
552  printf("G4OpenGLQtViewer:: fDrawingWireframe 2\n");
553#endif
554  QObject ::connect(fDrawingLineRemoval,
555                    SIGNAL(activated()),
556                    this,
557                    SLOT(actionDrawingLineRemoval()));
558  QObject ::connect(fDrawingSurfaceRemoval,
559                    SIGNAL(activated()),
560                    this,
561                    SLOT(actionDrawingSurfaceRemoval()));
562  QObject ::connect(fDrawingLineSurfaceRemoval,
563                    SIGNAL(activated()),
564                    this,
565                    SLOT(actionDrawingLineSurfaceRemoval()));
566#else
567  if (d_style == G4ViewParameters::wireframe) {
568    fDrawingWireframe->setChecked(true);
569  } else if (d_style == G4ViewParameters::hlr) {
570    fDrawingLineRemoval->setChecked(true);
571  } else if (d_style == G4ViewParameters::hsr) {
572    fDrawingSurfaceRemoval->setChecked(true);
573  } else if (d_style == G4ViewParameters::hlhsr) {
574    fDrawingLineSurfaceRemoval->setChecked(true);
575  } else {
576    mDrawing->clear();
577  }
578  QObject ::connect(fDrawingWireframe,
579                    SIGNAL(triggered(bool)),
580                    this,
581                    SLOT(actionDrawingWireframe()));
582  QObject ::connect(fDrawingLineRemoval,
583                    SIGNAL(triggered(bool)),
584                    this,
585                    SLOT(actionDrawingLineRemoval()));
586  QObject ::connect(fDrawingSurfaceRemoval,
587                    SIGNAL(triggered(bool)),
588                    this,
589                    SLOT(actionDrawingSurfaceRemoval()));
590  QObject ::connect(fDrawingLineSurfaceRemoval,
591                    SIGNAL(triggered(bool)),
592                    this,
593                    SLOT(actionDrawingLineSurfaceRemoval()));
594#endif
595
596
597
598#if QT_VERSION < 0x040000
599  QPopupMenu *mBackground = new QPopupMenu(mStyle);
600  mStyle->insertItem("&Background color",mBackground);
601
602#if QT_VERSION < 0x030200
603  QAction *white = new QAction("&White","&White",CTRL+Key_W,mBackground,0,true);
604  QAction *black =  new QAction("&Black","&Black",CTRL+Key_B,mBackground,0,true);
605#else
606  QAction *white = new QAction("&White",CTRL+Key_W,mBackground);
607  QAction *black =  new QAction("&Black",CTRL+Key_B,mBackground);
608  white->setToggleAction(true);
609  black->setToggleAction(true);
610#endif
611  white->addTo(mBackground);
612  black->addTo(mBackground);
613
614#else
615  QMenu *mBackground = mStyle->addMenu("&Background color");
616  QAction *white = mBackground->addAction("White");
617  QAction *black = mBackground->addAction("Black");
618
619#endif
620  if (background.GetRed() == 1. &&
621      background.GetGreen() == 1. &&
622      background.GetBlue() == 1.) {
623    createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
624  } else {
625    createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
626  }
627
628
629#if QT_VERSION < 0x040000
630  // === Action Menu ===
631  QPopupMenu *mActions = new QPopupMenu(fContextMenu);
632  fContextMenu->insertItem("&Actions",mActions);
633
634#if QT_VERSION < 0x030200
635  QAction *createEPS =  new QAction("&Save as ...","&Save as ...",CTRL+Key_S,mActions,0,true);
636#else
637  QAction *createEPS =  new QAction("&Save as ...",CTRL+Key_S,mActions);
638#endif
639  createEPS->addTo(mActions);
640  QObject ::connect(createEPS,
641                    SIGNAL(activated()),
642                    this,
643                    SLOT(actionCreateEPS()));
644
645#else
646  // === Action Menu ===
647  QMenu *mActions = fContextMenu->addMenu("&Actions");
648  QAction *createEPS = mActions->addAction("Save as ...");
649  QObject ::connect(createEPS,
650                    SIGNAL(triggered()),
651                    this,
652                    SLOT(actionCreateEPS()));
653#endif
654
655
656
657#if QT_VERSION < 0x040000
658  // === Special Menu ===
659  QPopupMenu *mSpecial = new QPopupMenu(fContextMenu);
660  fContextMenu->insertItem("S&pecial",mSpecial);
661
662  QPopupMenu *mTransparency = new QPopupMenu(mSpecial);
663  mSpecial->insertItem("Transparency",mTransparency);
664
665#if QT_VERSION < 0x030200
666  QAction *transparencyOn = new QAction("&On","&On",CTRL+Key_O,mTransparency,0,true);
667  QAction *transparencyOff = new QAction("&Off","&Off",CTRL+Key_F,mTransparency,0,true);
668#else
669  QAction *transparencyOn = new QAction("&On",CTRL+Key_O,mTransparency);
670  QAction *transparencyOff = new QAction("&Off",CTRL+Key_F,mTransparency);
671  transparencyOn->setToggleAction(true);
672  transparencyOff->setToggleAction(true);
673#endif
674  transparencyOn->addTo(mTransparency);
675  transparencyOff->addTo(mTransparency);
676
677#else
678  // === Special Menu ===
679  QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
680  QMenu *mTransparency = mSpecial->addMenu("Transparency");
681  QAction *transparencyOn = mTransparency->addAction("On");
682  QAction *transparencyOff = mTransparency->addAction("Off");
683#endif
684
685  if (transparency_enabled == false) {
686    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
687  } else if (transparency_enabled == true) {
688    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
689  } else {
690    mSpecial->clear();
691  }
692
693
694#if QT_VERSION < 0x040000
695  QPopupMenu *mAntialiasing = new QPopupMenu(mSpecial);
696  mSpecial->insertItem("Antialiasing",mAntialiasing);
697
698#if QT_VERSION < 0x030200
699  QAction *antialiasingOn = new QAction("&On","&On",CTRL+Key_O,mAntialiasing,0,true);
700  QAction *antialiasingOff = new QAction("&Off","&Off",CTRL+Key_F,mAntialiasing,0,true);
701#else
702  QAction *antialiasingOn = new QAction("&On",CTRL+Key_O,mAntialiasing);
703  QAction *antialiasingOff = new QAction("&Off",CTRL+Key_F,mAntialiasing);
704  antialiasingOn->setToggleAction(true);
705  antialiasingOff->setToggleAction(true);
706#endif
707  antialiasingOn->addTo(mAntialiasing);
708  antialiasingOff->addTo(mAntialiasing);
709
710#else
711  QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
712  QAction *antialiasingOn = mAntialiasing->addAction("On");
713  QAction *antialiasingOff = mAntialiasing->addAction("Off");
714#endif
715
716  if (antialiasing_enabled == false) {
717    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
718  } else if (antialiasing_enabled == true) {
719    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
720  } else {
721    mAntialiasing->clear();
722  }
723
724#if QT_VERSION < 0x040000
725  QPopupMenu *mHaloing = new QPopupMenu(mSpecial);
726  mSpecial->insertItem("Haloing",mHaloing);
727
728#if QT_VERSION < 0x030200
729  QAction *haloingOn = new QAction("&On","&On",CTRL+Key_O,mHaloing,0,true);
730  QAction *haloingOff = new QAction("&Off","&Off",CTRL+Key_F,mHaloing,0,true);
731#else
732  QAction *haloingOn = new QAction("&On",CTRL+Key_O,mHaloing);
733  QAction *haloingOff = new QAction("&Off",CTRL+Key_F,mHaloing);
734  haloingOn->setToggleAction(true);
735  haloingOff->setToggleAction(true);
736#endif
737  haloingOn->addTo(mHaloing);
738  haloingOff->addTo(mHaloing);
739#else
740  QMenu *mHaloing = mSpecial->addMenu("Haloing");
741  QAction *haloingOn = mHaloing->addAction("On");
742  QAction *haloingOff = mHaloing->addAction("Off");
743#endif
744  if (haloing_enabled == false) {
745    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
746  } else if (haloing_enabled == true) {
747    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
748  } else {
749    mHaloing->clear();
750  }
751
752#if QT_VERSION < 0x040000
753  QPopupMenu *mAux = new QPopupMenu(mSpecial);
754  mSpecial->insertItem("Auxiliairy edges",mAux);
755
756#if QT_VERSION < 0x030200
757  QAction *auxOn = new QAction("&On","&On",CTRL+Key_O,mAux,0,true);
758  QAction *auxOff = new QAction("&Off","&Off",CTRL+Key_F,mAux,0,true);
759#else
760  QAction *auxOn = new QAction("&On",CTRL+Key_O,mAux);
761  QAction *auxOff = new QAction("&Off",CTRL+Key_F,mAux);
762  auxOn->setToggleAction(true);
763  auxOff->setToggleAction(true);
764#endif
765  auxOn->addTo(mAux);
766  auxOff->addTo(mAux);
767
768#else
769  QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
770  QAction *auxOn = mAux->addAction("On");
771  QAction *auxOff = mAux->addAction("Off");
772#endif
773  if (!fVP.IsAuxEdgeVisible()) {
774    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
775  } else {
776    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
777  }
778
779
780
781#if QT_VERSION < 0x040000
782  QPopupMenu *mFullScreen = new QPopupMenu(mSpecial);
783  mSpecial->insertItem("Full screen",mFullScreen);
784
785#if QT_VERSION < 0x030200
786  fFullScreenOn = new QAction("&On","&On",CTRL+Key_O,mFullScreen,0,true);
787  fFullScreenOff = new QAction("&Off","&Off",CTRL+Key_F,mFullScreen,0,true);
788#else
789  fFullScreenOn = new QAction("&On",CTRL+Key_O,mFullScreen);
790  fFullScreenOff = new QAction("&Off",CTRL+Key_F,mFullScreen);
791  fFullScreenOn->setToggleAction(true);
792  fFullScreenOff->setToggleAction(true);
793#endif
794  fFullScreenOn->addTo(mFullScreen);
795  fFullScreenOff->addTo(mFullScreen);
796#else
797  QMenu *mFullScreen = mSpecial->addMenu("&Full screen");
798  fFullScreenOn = mFullScreen->addAction("On");
799  fFullScreenOff = mFullScreen->addAction("Off");
800#endif
801  createRadioAction(fFullScreenOn,fFullScreenOff,SLOT(toggleFullScreen(bool)),2);
802
803}
804
805void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
806{
807  if (!GLWindow) {
808    G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
809  } else {
810 
811    if (!fContextMenu)
812      createPopupMenu();
813   
814    // launch menu
815    if ( fContextMenu ) {
816      fContextMenu->exec( e->globalPos() );
817      //    delete fContextMenu;
818    }
819  }
820  e->accept();
821}
822
823
824/**
825   Create a radio button menu. The two menu will be connected. When click on one,
826   eatch state will be invert and callback method will be called.
827   @param action1 first action to connect
828   @param action2 second action to connect
829   @param method callback method
830   @param nCheck: 1 : first action will be set true. 2 : second action will be set true
831*/
832#if QT_VERSION < 0x040000
833void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
834
835  if (action1->parent()->inherits("QPopupMenu")){
836    ((QPopupMenu*)action1->parent())->setCheckable(true);
837    ((QPopupMenu*)action2->parent())->setCheckable(true);
838  }
839  action1->setOn(false);
840   action2->setOn(false);
841
842  if (nCheck ==1)
843    action1->setOn(true);
844  else
845    action2->setOn(true);
846   
847  //FIXME : Should not work on Qt3
848  QObject ::connect(action1, SIGNAL(activated()),action2, SLOT(toggle()));
849  QObject ::connect(action2, SIGNAL(activated()),action1, SLOT(toggle()));
850
851  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
852}
853
854#else
855void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
856
857  action1->setCheckable(true);
858  action2->setCheckable(true);
859
860  if (nCheck ==1)
861    action1->setChecked (true);
862  else
863    action2->setChecked (true);
864   
865  QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
866  QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
867
868  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
869
870}
871#endif
872
873/**
874   Slot activate when mouseAction->rotate menu is set
875 */
876void G4OpenGLQtViewer::actionMouseRotate() {
877  emit toggleMouseAction(STYLE1);
878}
879
880
881/**
882   Slot activate when mouseAction->rotate menu is set
883 */
884void G4OpenGLQtViewer::actionMouseMove() {
885  emit toggleMouseAction(STYLE2);
886}
887
888
889/**
890   Slot activate when mouseAction->zoom menu is set
891 */
892void G4OpenGLQtViewer::actionMousePick() {
893#ifdef GEANT4_QT_DEBUG
894  printf("G4OpenGLQtViewer::actionMousePick \n");
895#endif
896  emit toggleMouseAction(STYLE3);
897}
898
899
900/**
901   Slot activate when drawing->wireframe menu is set
902 */
903void G4OpenGLQtViewer::actionDrawingWireframe() {
904  emit toggleDrawingAction(1);
905}
906
907/**
908   Slot activate when drawing->line removal menu is set
909 */
910void G4OpenGLQtViewer::actionDrawingLineRemoval() {
911  emit toggleDrawingAction(2);
912}
913
914/**
915   Slot activate when drawing->surface removal menu is set
916 */
917void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
918  emit toggleDrawingAction(3);
919}
920
921/**
922   Slot activate when drawing->wireframe menu is set
923 */
924void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
925  emit toggleDrawingAction(4);
926}
927
928
929/**
930   Slot activated when mouse action is toggle
931   @param aAction : STYLE1, STYLE2, STYLE3
932 */
933void G4OpenGLQtViewer::toggleMouseAction(mouseActions aAction) {
934 
935  if ((aAction == STYLE1) || //initialize all
936      (aAction == STYLE2) ||
937      (aAction == STYLE3))  {
938#if QT_VERSION < 0x040000
939    fRotateAction->setOn (false);
940    fMoveAction->setOn (false);
941    fPickAction->setOn (false);
942#else
943    fRotateAction->setChecked (false);
944    fMoveAction->setChecked (false);
945    fPickAction->setChecked (false);
946#endif
947    fVP.SetPicking(false);
948    fMouseAction = aAction;
949  }
950  // rotate
951  if (aAction == STYLE1) {  // rotate
952    showShortcuts();
953#if QT_VERSION < 0x040000
954    fRotateAction->setOn (true);
955#else
956    fRotateAction->setChecked (true);
957#endif
958  } else  if (aAction == STYLE2) { //move
959#if QT_VERSION < 0x040000
960    fMoveAction->setOn (true);
961#else
962    fMoveAction->setChecked (true);
963#endif
964  } else  if (aAction == STYLE3) { //pick
965#if QT_VERSION < 0x040000
966    fPickAction->setOn (true);
967#else
968    fPickAction->setChecked (true);
969#endif
970    fVP.SetPicking(true);
971  }
972}
973
974/**
975   Show shortcuts for this mouse action
976 */
977void G4OpenGLQtViewer::showShortcuts() {
978  if (fMouseAction == STYLE1) {  // rotate
979    G4cout << "Click and move mouse to rotate volume \n" << G4endl;
980    G4cout << "Press left/right arrows to move volume left/right\n" << G4endl;
981    G4cout << "Press up/down arrows to move volume up/down\n" << G4endl;
982    G4cout << "Press ALT+up/down arrows to move volume toward/forward\n" << G4endl;
983    G4cout << "Press SHIFT+left/right arrows to rotate volume left/right\n" << G4endl;
984    G4cout << "Press SHIFT+up/down arrows to rotate volume up/down\n" << G4endl;
985    G4cout << "Press ALT+/- to slow/speed auto rotation/move\n" << G4endl;
986    G4cout << "In video mode : \n" << G4endl;
987    G4cout << "  Press SPACE to Start/Pause video recording \n" << G4endl;
988    G4cout << "  Press RETURN to Stop video recording \n" << G4endl;
989  } else  if (fMouseAction == STYLE2) { //move
990    G4cout << "Move camera point of view with mouse\n" << G4endl;
991    G4cout << "Press left/right arrows to move volume left/right\n" << G4endl;
992    G4cout << "Press up/down arrows to move volume up/down\n" << G4endl;
993    G4cout << "Press ALT+up/down arrows to move volume toward/forward\n" << G4endl;
994    G4cout << "Press SHIFT+left/right arrows to rotate volume left/right\n" << G4endl;
995    G4cout << "Press SHIFT+up/down arrows to rotate volume up/down\n" << G4endl;
996    G4cout << "Press +/- to zoom into volume\n" << G4endl;
997    G4cout << "Press ALT+/- to slow/speed auto rotation/move\n" << G4endl;
998    G4cout << "In video mode : \n" << G4endl;
999    G4cout << "  Press SPACE to Start/Pause video recording \n" << G4endl;
1000    G4cout << "  Press RETURN to Stop video recording \n" << G4endl;
1001  } else  if (fMouseAction == STYLE3) { //pick
1002    G4cout << "Click and pick \n" << G4endl;
1003  }
1004
1005}
1006
1007
1008
1009/**
1010   Slot activated when drawing menu is toggle
1011   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1012   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1013   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1014   It will cause a redraw of the view
1015   @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
1016   @see G4OpenGLStoredQtViewer::DrawView
1017   @see G4XXXStoredViewer::CompareForKernelVisit
1018 */
1019void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
1020
1021  G4ViewParameters::DrawingStyle d_style;
1022 
1023
1024  // initialize
1025  if ((aAction >0) && (aAction <5)) {
1026#if QT_VERSION < 0x040000
1027    fDrawingWireframe->setOn(false);
1028    fDrawingLineRemoval->setOn(false);
1029    fDrawingSurfaceRemoval->setOn(false);
1030    fDrawingLineSurfaceRemoval->setOn(false);
1031#else
1032    fDrawingWireframe->setChecked (false);
1033    fDrawingLineRemoval->setChecked (false);
1034    fDrawingSurfaceRemoval->setChecked (false);
1035    fDrawingLineSurfaceRemoval->setChecked (false);
1036#endif
1037  }
1038  if (aAction ==1) {
1039#if QT_VERSION < 0x040000
1040    fDrawingWireframe->setOn(true);
1041#else
1042    fDrawingWireframe->setChecked (true);
1043#endif
1044
1045    d_style = G4ViewParameters::wireframe;
1046
1047  } else  if (aAction ==2) {
1048#if QT_VERSION < 0x040000
1049    fDrawingLineRemoval->setOn(true);
1050#else
1051    fDrawingLineRemoval->setChecked (true);
1052#endif
1053
1054    d_style = G4ViewParameters::hlr;
1055
1056  } else  if (aAction ==3) {
1057#if QT_VERSION < 0x040000
1058    fDrawingSurfaceRemoval->setOn(true);
1059#else
1060    fDrawingSurfaceRemoval->setChecked (true);
1061#endif
1062
1063    d_style = G4ViewParameters::hsr;
1064
1065  } else  if (aAction ==4) {
1066#if QT_VERSION < 0x040000
1067    fDrawingLineSurfaceRemoval->setOn(true);
1068#else
1069    fDrawingLineSurfaceRemoval->setChecked (true);
1070#endif
1071    d_style = G4ViewParameters::hlhsr;
1072  }
1073  fVP.SetDrawingStyle(d_style);
1074
1075  updateQWidget();
1076}
1077
1078
1079/**
1080   SLOT Activate by a click on the representation menu
1081   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1082   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1083   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1084   It will cause a redraw of the view
1085   @param check : 1 polyhedron, 0 nurbs
1086   @see G4OpenGLStoredQtViewer::DrawView
1087   @see G4XXXStoredViewer::CompareForKernelVisit
1088*/
1089void G4OpenGLQtViewer::toggleRepresentation(bool check) {
1090
1091  G4ViewParameters::RepStyle style;
1092  if (check == 1) {
1093    style = G4ViewParameters::polyhedron;
1094  } else {
1095    style = G4ViewParameters::nurbs;
1096  }
1097  fVP.SetRepStyle (style);
1098
1099  updateQWidget();
1100}
1101
1102/**
1103   SLOT Activate by a click on the projection menu
1104   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1105   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1106   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1107   It will cause a redraw of the view
1108   @param check : 1 orthographic, 2 perspective
1109   @see G4OpenGLStoredQtViewer::DrawView
1110   @see G4XXXStoredViewer::CompareForKernelVisit
1111*/
1112void G4OpenGLQtViewer::toggleProjection(bool check) {
1113
1114  if (check == 1) {
1115    fVP.SetFieldHalfAngle (0);
1116  } else {
1117
1118    // look for the default parameter hidden in G4UIcommand parameters
1119    G4UImanager* UI = G4UImanager::GetUIpointer();
1120    if(UI==NULL)
1121      return;
1122    G4UIcommandTree * treeTop = UI->GetTree();
1123
1124    // find command
1125    G4UIcommand* command = treeTop->FindPath("/vis/viewer/set/projection");
1126    if (!command)
1127      return;
1128
1129    // find param
1130    G4UIparameter * angleParam = NULL;
1131    for(G4int i=0;  i<command->GetParameterEntries(); i++)
1132    {
1133      if( command->GetParameter(i)->GetParameterName() == "field-half-angle" ) {
1134        angleParam = command->GetParameter(i);
1135      }
1136    }
1137    if (!angleParam)
1138      return;
1139
1140    // find unit
1141    G4UIparameter * unitParam = NULL;
1142    for(G4int i=0;  i<command->GetParameterEntries(); i++)
1143    {
1144      if( command->GetParameter(i)->GetParameterName() == "unit" ) {
1145        unitParam = command->GetParameter(i);
1146      }
1147    }
1148    if (!unitParam)
1149      return;
1150
1151    G4double defaultValue = command->ConvertToDouble(angleParam->GetDefaultValue())
1152                            * G4UnitDefinition::GetValueOf(unitParam->GetDefaultValue());
1153    if (defaultValue > 89.5 || defaultValue <= 0.0) {
1154      G4cerr << "Field half angle should be 0 < angle <= 89.5 degrees. Check your default Field half angle parameter\n";
1155    } else {
1156      G4cout << "Perspective view has been set to default value. Field half angle="<<angleParam->GetDefaultValue() <<" \n" << G4endl;
1157      fVP.SetFieldHalfAngle (defaultValue);
1158      SetView ();
1159    }
1160  } 
1161  updateQWidget();
1162}
1163
1164
1165/**
1166   SLOT Activate by a click on the background menu
1167@param check : 1 white, 0 black
1168*/
1169void G4OpenGLQtViewer::toggleBackground(bool check) {
1170
1171  //   //I need to revisit the kernel if the background colour changes and
1172  //   //hidden line removal is enabled, because hlr drawing utilises the
1173  //   //background colour in its drawing...
1174  //   // (Note added by JA 13/9/2005) Background now handled in view
1175  //   // parameters.  A kernel visit is triggered on change of background.
1176  if (check == 1) {
1177    ((G4ViewParameters&)this->GetViewParameters()).
1178      SetBackgroundColour(G4Colour(1.,1.,1.));  // White
1179  } else {
1180    ((G4ViewParameters&)this->GetViewParameters()).
1181      SetBackgroundColour(G4Colour(0.,0.,0.));  // Black
1182  }
1183  updateQWidget();
1184}
1185
1186/**
1187   SLOT Activate by a click on the transparency menu
1188@param check : 1 , 0
1189*/
1190void G4OpenGLQtViewer::toggleTransparency(bool check) {
1191 
1192  if (check) {
1193    transparency_enabled = false;
1194  } else {
1195    transparency_enabled = true;
1196  }
1197  SetNeedKernelVisit (true);
1198  updateQWidget();
1199}
1200
1201/**
1202   SLOT Activate by a click on the antialiasing menu
1203@param check : 1 , 0
1204*/
1205void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
1206
1207  if (!check) {
1208    antialiasing_enabled = false;
1209    glDisable (GL_LINE_SMOOTH);
1210    glDisable (GL_POLYGON_SMOOTH);
1211  } else {
1212    antialiasing_enabled = true;
1213    glEnable (GL_LINE_SMOOTH);
1214    glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
1215    glEnable (GL_POLYGON_SMOOTH);
1216    glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
1217  }
1218
1219  updateQWidget();
1220}
1221
1222/**
1223   SLOT Activate by a click on the haloing menu
1224@param check : 1 , 0
1225*/
1226//FIXME : I SEE NOTHING...
1227void G4OpenGLQtViewer::toggleHaloing(bool check) {
1228  if (check) {
1229    haloing_enabled = false;
1230  } else {
1231    haloing_enabled = true;
1232  }
1233
1234  updateQWidget();
1235
1236}
1237
1238/**
1239   SLOT Activate by a click on the auxiliaire edges menu
1240@param check : 1 , 0
1241*/
1242void G4OpenGLQtViewer::toggleAux(bool check) {
1243  if (check) {
1244    fVP.SetAuxEdgeVisible(false);
1245  } else {
1246    fVP.SetAuxEdgeVisible(true);
1247  }
1248  SetNeedKernelVisit (true);
1249  updateQWidget();
1250
1251}
1252
1253/**
1254   SLOT Activate by a click on the full screen menu
1255*/
1256void G4OpenGLQtViewer::toggleFullScreen(bool check) {
1257#ifdef GEANT4_QT_DEBUG
1258  printf("G4OpenGLQtViewer::toggleFullScreen checked:%d() \n",check);
1259#endif
1260  if (check != GLWindow->isFullScreen()) { //toggle
1261#if QT_VERSION >= 0x030200
1262#if QT_VERSION < 0x040000
1263    GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
1264//     fFullScreenOn->setOn(check);
1265//     fFullScreenOff->setOn(!check);
1266#else
1267    GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
1268//     fFullScreenOn->setChecked(check);
1269//     fFullScreenOff->setChecked(!check);
1270#endif
1271#else
1272    G4cerr << "This version of Qt could not do fullScreen. Resizing the widget is the only solution available.\n" << G4endl;
1273#endif
1274  }
1275}
1276
1277
1278void G4OpenGLQtViewer::saveForVideo(QString nomFich) {
1279  if (nomFich == "") {
1280    return;
1281  }
1282  nomFich += "."+QString("ppm");
1283  QImage image;
1284  image = fWindow->grabFrameBuffer();
1285  bool res = false;
1286 
1287#if QT_VERSION < 0x040000
1288  res = image.save(nomFich,"ppm");
1289#else
1290  res = image.save(nomFich,0);
1291#endif
1292  if (res == false) {
1293#if QT_VERSION < 0x040000
1294    G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1295#else
1296    G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1297#endif
1298    fRecordFrames = false;
1299  }
1300}
1301
1302
1303
1304void G4OpenGLQtViewer::actionCreateEPS() {
1305  QString filters;
1306#if QT_VERSION < 0x040000
1307  QStrList listFormat=QImageIO::outputFormats();
1308  char *tmp=listFormat.first();
1309  while (tmp!=0) {
1310    filters += QString(tmp) + ";;";
1311    tmp=listFormat.next();
1312  }
1313#else
1314  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
1315  for (int i = 0; i < formats.size(); ++i) {
1316    filters +=formats.at(i) + ";;";
1317  }
1318#endif
1319  filters += "eps;;";
1320  filters += "ps;;";
1321  filters += "pdf";
1322  QString* selectedFormat = new QString();
1323#if QT_VERSION < 0x040000
1324  QString nomFich =  QFileDialog::getSaveFileName ( ".",
1325                                                    filters,
1326                                                    GLWindow,
1327                                                    "Save file dialog",
1328                                                    tr("Save as ..."),
1329                                                    selectedFormat );
1330#else
1331  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
1332                                                    tr("Save as ..."),
1333                                                    ".",
1334                                                    filters,
1335                                                    selectedFormat );
1336#endif
1337  // bmp jpg jpeg png ppm xbm xpm
1338  if (nomFich == "") {
1339    return;
1340  }
1341#if QT_VERSION < 0x040000
1342  nomFich += "."+QString(selectedFormat->ascii());
1343  QString format = selectedFormat->lower();
1344#else
1345  nomFich += "."+QString(selectedFormat->toStdString().c_str());
1346  QString format = selectedFormat->toLower();
1347#endif
1348  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,format,fWindow->height(),fWindow->width());
1349  if(  exportDialog->exec()) {
1350
1351    QImage image;
1352    bool res = false;
1353    if ((exportDialog->getWidth() !=fWindow->width()) ||
1354        (exportDialog->getHeight() !=fWindow->height())) {
1355      if (format != QString("eps")) {
1356      G4cerr << "Export->Change Size : This function is not implemented, to export in another size, please resize your frame to what you need\n" << G4endl;
1357     
1358      //    rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1359      //      QGLWidget* glResized = fWindow;
1360
1361      // FIXME :
1362      // L.Garnier : I've try to implement change size function, but the problem is
1363      // the renderPixmap function call the QGLWidget to resize and it doesn't draw
1364      // the content of this widget... It only draw the background.
1365
1366      //      fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2,true );
1367
1368      //      QPixmap pixmap = fWindow->renderPixmap ();
1369     
1370      //      image = pixmap->toImage();
1371      //      glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1372      //      image = glResized->grabFrameBuffer();
1373      }     
1374    } else {
1375      image = fWindow->grabFrameBuffer();
1376    }   
1377    if (format == QString("eps")) {
1378      if (exportDialog->getVectorEPS()) {
1379        res = generateVectorEPS(nomFich,exportDialog->getWidth(),exportDialog->getHeight(),image);
1380      } else {
1381        res = generateEPS(nomFich,exportDialog->getNbColor(),image);
1382      }
1383    } else if ((format == "ps") || (format == "pdf")) {
1384      res = generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1385    } else if ((format == "tif") ||
1386               (format == "tiff") ||
1387               (format == "jpg") ||
1388               (format == "jpeg") ||
1389               (format == "png") ||
1390               (format == "pbm") ||
1391               (format == "pgm") ||
1392               (format == "ppm") ||
1393               (format == "bmp") ||
1394               (format == "xbm") ||
1395               (format == "xpm")) {
1396#if QT_VERSION < 0x040000
1397      res = image.save(nomFich,selectedFormat->ascii(),exportDialog->getSliderValue());
1398#else
1399      res = image.save(nomFich,0,exportDialog->getSliderValue());
1400#endif
1401    } else {
1402      G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1403    }
1404    if (res == false) {
1405#if QT_VERSION < 0x040000
1406      G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1407#else
1408      G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1409#endif
1410    } else {
1411#if QT_VERSION < 0x040000
1412      G4cout << "File "<<nomFich.ascii()<<" has been saved \n" << G4endl;
1413#else
1414      G4cout << "File "<<nomFich.toStdString().c_str()<<" has been saved \n" << G4endl;
1415#endif
1416    }
1417   
1418  } else { // cancel selected
1419    return;
1420  }
1421 
1422#ifdef GEANT4_QT_DEBUG
1423  printf("G4OpenGLQtViewer::actionCreateEPS() \n");
1424#endif
1425}
1426
1427/*
1428// 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
1429
1430void Graph::exportToSVG(const QString& fname)
1431{
1432  // enable workaround for Qt3 misalignments
1433  QwtPainter::setSVGMode(true);
1434  QPicture picture;
1435  QPainter p(&picture);
1436  d_plot->print(&p, d_plot->rect());
1437  p.end();
1438
1439  picture.save(fname, "svg");
1440}
1441*/
1442
1443
1444
1445
1446/**
1447   Save the current mouse press point
1448   @param p mouse click point
1449*/
1450#if QT_VERSION < 0x040000
1451void G4OpenGLQtViewer::G4MousePressEvent(QPoint p,Qt::ButtonState mButtons)
1452#else
1453void G4OpenGLQtViewer::G4MousePressEvent(QPoint p,Qt::MouseButtons mButtons)
1454#endif
1455{
1456  fAutoMove = false; // stop automove
1457  fLastPos = p;
1458  if (fMouseAction == STYLE2){  // pick
1459    Pick(p.x(),p.y());
1460  }
1461}
1462
1463
1464/**
1465   @param pos_x mouse x position
1466   @param pos_y mouse y position
1467   @param mButtons mouse button active
1468   @param mAutoMove true: apply this move till another evnt came, false :one time move
1469*/
1470
1471#if QT_VERSION < 0x040000
1472void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::ButtonState mButtons,bool mAutoMove)
1473#else
1474  void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons,bool mAutoMove)
1475#endif
1476{
1477  fAutoMove = mAutoMove;
1478#ifdef GEANT4_QT_DEBUG
1479  printf("                   mouse move event %d %d\n",pos_x,pos_y);
1480#endif
1481
1482  if (!fAutoMove) {  // keep old delta if automove
1483    fDeltaPosX = fLastPos.x() - pos_x;
1484    fDeltaPosY = fLastPos.y() - pos_y;
1485  }
1486
1487  if ((fDeltaPosX == 0) && (fDeltaPosY == 0)) {
1488    fAutoMove = false;
1489  }
1490
1491  if (fMouseAction == STYLE1) {  // rotate
1492    if (mButtons & Qt::LeftButton) {
1493      if (fAutoMove) {
1494        while (fAutoMove) {
1495          rotateScene(fDeltaPosX,fDeltaPosY);
1496          ((QApplication*)G4Qt::getInstance ())->processEvents();
1497        }
1498      } else {
1499        rotateScene(fDeltaPosX,fDeltaPosY);
1500      }
1501    }
1502  } else if (fMouseAction == STYLE2) {  // move
1503    if (mButtons & Qt::LeftButton) {
1504      if (fAutoMove) {
1505        while (fAutoMove) {
1506          moveScene(-fDeltaPosX,-fDeltaPosY,0,true);
1507          ((QApplication*)G4Qt::getInstance ())->processEvents();
1508        }
1509      } else {
1510        moveScene(-fDeltaPosX,-fDeltaPosY,0,true);
1511      }
1512    }
1513  }
1514  fLastPos = QPoint(pos_x, pos_y);
1515#ifdef GEANT4_QT_DEBUG
1516  if (mButtons & Qt::LeftButton) {
1517    printf("                  end of  mouse move event %d %d delta:%f %f\n",pos_x,pos_y,fDeltaPosX,fDeltaPosY);
1518  } else {
1519    printf("                  end of  mouse move event %d %d delta:%f %f --------------NO BUTTON\n",pos_x,pos_y,fDeltaPosX,fDeltaPosY);
1520  }
1521#endif
1522}
1523
1524
1525/**
1526   Move the scene of dx, dy, dz values.
1527   @param dx delta mouse x position
1528   @param dy delta mouse y position
1529   @param mouseMove : true if even comes froma mouse move, false if even comes from key action
1530*/
1531
1532void G4OpenGLQtViewer::moveScene(G4double dx,G4double dy, G4double dz,bool mouseMove)
1533{
1534  G4double coefTrans = 0;
1535  GLdouble coefDepth = 0;
1536  if(mouseMove) {
1537    coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_x);
1538    if (WinSize_y <WinSize_x) {
1539      coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_y);
1540    }
1541  } else {
1542    coefTrans = getSceneNearWidth()*fDeltaSceneTranslation;
1543    coefDepth = getSceneDepth()*fDeltaDepth;
1544  }
1545  fVP.IncrementPan(-dx*coefTrans,dy*coefTrans,dz*coefDepth);
1546 
1547  updateQWidget();
1548}
1549
1550
1551/**
1552   @param dx delta mouse x position
1553   @param dy delta mouse y position
1554*/
1555
1556void G4OpenGLQtViewer::rotateScene(G4double dx, G4double dy)
1557{
1558  dx = dx * fDeltaRotation;
1559  dy = dy * fDeltaRotation;
1560
1561  //phi spin stuff here
1562 
1563  G4Vector3D vp = fVP.GetViewpointDirection ().unit ();
1564  G4Vector3D up = fVP.GetUpVector ().unit ();
1565 
1566  G4Vector3D yprime = (up.cross(vp)).unit();
1567  G4Vector3D zprime = (vp.cross(yprime)).unit();
1568 
1569  G4double delta_alpha;
1570  G4double delta_theta;
1571 
1572  if (fVP.GetLightsMoveWithCamera()) {
1573    delta_alpha = dy;
1574    delta_theta = -dx;
1575  } else {
1576    delta_alpha = -dy;
1577    delta_theta = dx;
1578  }   
1579 
1580  delta_alpha *= deg;
1581  delta_theta *= deg;
1582 
1583  G4Vector3D new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1584 
1585  G4Vector3D new_up;
1586  if (fVP.GetLightsMoveWithCamera()) {
1587    new_up = (new_vp.cross(yprime)).unit();
1588    fVP.SetUpVector(new_up);
1589  } else {
1590    new_up = up;
1591  }
1592  ////////////////
1593  // Rotates by fixed azimuthal angle delta_theta.
1594 
1595  G4double cosalpha = new_up.dot (new_vp.unit());
1596  G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1597  yprime = (new_up.cross (new_vp.unit())).unit ();
1598  G4Vector3D xprime = yprime.cross (new_up);
1599  // Projection of vp on plane perpendicular to up...
1600  G4Vector3D a1 = sinalpha * xprime;
1601  // Required new projection...
1602  G4Vector3D a2 =
1603    sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1604  // Required Increment vector...
1605  G4Vector3D delta = a2 - a1;
1606  // So new viewpoint is...
1607  G4Vector3D viewPoint = new_vp.unit() + delta;
1608 
1609  fVP.SetViewAndLights (viewPoint);
1610  updateQWidget();
1611 
1612}
1613
1614/** This is the benning of a rescale function. It does nothing for the moment
1615    @param aWidth : new width
1616    @param aHeight : new height
1617*/
1618void G4OpenGLQtViewer::rescaleImage(
1619 int aWidth
1620,int aHeight
1621){
1622#ifdef GEANT4_QT_DEBUG
1623  printf("should rescale \n");
1624#endif
1625  GLfloat* feedback_buffer;
1626  GLint returned;
1627  FILE* file;
1628 
1629//   feedback_buffer = new GLfloat[size];
1630//   glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1631//   glRenderMode (GL_FEEDBACK);
1632 
1633//   glViewport (0, 0, aWidth, aHeight);
1634//   DrawView();
1635//   returned = glRenderMode (GL_RENDER);
1636
1637}
1638
1639/**
1640   Generate Vectorial Encapsulated Postscript form image
1641   @param aFilename : name of file
1642   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1643   @param aImage : Image to print
1644*/
1645bool G4OpenGLQtViewer::generateVectorEPS (
1646 QString aFilename
1647,int aWidth
1648,int aHeight
1649,QImage aImage
1650)
1651{
1652  // Print vectored PostScript
1653 
1654  G4int size = 5000000;
1655
1656  GLfloat* feedback_buffer;
1657  GLint returned;
1658  FILE* file;
1659 
1660  feedback_buffer = new GLfloat[size];
1661  glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1662  glRenderMode (GL_FEEDBACK);
1663 
1664  int side = aWidth;
1665  if (aHeight < aWidth) side = aHeight;
1666  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
1667  DrawView();
1668
1669  returned = glRenderMode (GL_RENDER);
1670 
1671 
1672#if QT_VERSION < 0x040000
1673  file = fopen (aFilename.ascii(), "w");
1674#else
1675  file = fopen (aFilename.toStdString().c_str(), "w");
1676#endif
1677  if (file) {
1678    spewWireframeEPS (file, returned, feedback_buffer, "rendereps");
1679  } else {
1680#if QT_VERSION < 0x040000
1681    G4cerr << "Could not open "<< aFilename.ascii()<<"\n" << G4endl;
1682#else
1683    G4cerr << "Could not open "<< aFilename.toStdString().c_str()<<"\n" << G4endl;
1684#endif
1685  }
1686 
1687  delete[] feedback_buffer;
1688
1689  return true;
1690}
1691
1692/**
1693   Generate Encapsulated Postscript form image
1694   @param aFilename : name of file
1695   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1696   @param aImage : Image to print
1697*/
1698bool G4OpenGLQtViewer::generateEPS (
1699 QString aFilename
1700,int aInColor
1701,QImage aImage
1702)
1703{
1704  // FIXME
1705#ifdef GEANT4_QT_DEBUG
1706  printf("saving EPS\n");
1707#endif
1708
1709  FILE* fp;
1710
1711  if (aImage.bits () == NULL)
1712    return false;
1713 
1714#if QT_VERSION < 0x040000
1715  fp = fopen (aFilename.ascii(), "w");
1716#else
1717  fp = fopen (aFilename.toStdString().c_str(), "w");
1718#endif
1719  if (fp == NULL) {
1720    return false;
1721  }
1722 
1723  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
1724#if QT_VERSION < 0x040000
1725  fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1726#else
1727  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
1728#endif
1729  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
1730  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
1731  fprintf (fp, "%%%%EndComments\n");
1732  fprintf (fp, "gsave\n");
1733  fprintf (fp, "/bwproc {\n");
1734  fprintf (fp, "    rgbproc\n");
1735  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
1736  fprintf (fp, "    5 -1 roll {\n");
1737  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
1738  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1739  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1740  fprintf (fp, "    { 2 1 roll } ifelse\n");
1741  fprintf (fp, "    }forall\n");
1742  fprintf (fp, "    pop pop pop\n");
1743  fprintf (fp, "} def\n");
1744  fprintf (fp, "systemdict /colorimage known not {\n");
1745  fprintf (fp, "   /colorimage {\n");
1746  fprintf (fp, "       pop\n");
1747  fprintf (fp, "       pop\n");
1748  fprintf (fp, "       /rgbproc exch def\n");
1749  fprintf (fp, "       { bwproc } image\n");
1750  fprintf (fp, "   }  def\n");
1751  fprintf (fp, "} if\n");
1752  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1753  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1754  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1755  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
1756  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1757  fprintf (fp, "false %d\n", aInColor);
1758  fprintf (fp, "colorimage\n");
1759 
1760
1761  int width = aImage.width();
1762  int height = aImage.height();
1763  int depth = aImage.depth();
1764  int size = width*height;
1765 
1766  if (depth == 1)
1767    size = (width+7)/8*height;
1768  else if (aInColor == 1)
1769    size = size*3;
1770 
1771  int i = 0;
1772  //  if ( aInColor ==1 ) {
1773  // FIXME : L. Garnier. For the moment 10 dec 2007, I could not find a way
1774  // to save correctly grayscale Image. I mean that color or grayscale image
1775  // have the same file save size !
1776 
1777  /* } else*/ if (depth == 8) {
1778    for(int y=height-1; y >=0 ; y--) {
1779      const uchar * s = aImage.scanLine(y);
1780      for(int x=0; x <width; x++) {
1781        QRgb rgb = aImage.color(s[x]);
1782        if (aInColor == 1) {
1783          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1784          i++;
1785        } else {
1786          fprintf (fp, " %02hx %02hx %02hx",
1787                   (unsigned char) qRed(rgb),
1788                   (unsigned char) qGreen(rgb),
1789                   (unsigned char) qBlue(rgb));
1790          i += 3;
1791        }
1792      }
1793      fprintf (fp, "\n");
1794    }
1795  } else {
1796#if QT_VERSION < 0x040000
1797    bool alpha = aImage.hasAlphaBuffer();
1798#else
1799    bool alpha = aImage.hasAlphaChannel();
1800    for(int y=height-1; y >=0 ; y--) {
1801      QRgb * s = (QRgb*)(aImage.scanLine(y));
1802      for(int x=0; x <width; x++) {
1803        QRgb rgb = (*s++);
1804        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1805          rgb = qRgb(0xff, 0xff, 0xff);
1806        if (aInColor == 1) {
1807          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1808          i++;
1809        } else {
1810          fprintf (fp, " %02hx %02hx %02hx",
1811                   (unsigned char) qRed(rgb),
1812                   (unsigned char) qGreen(rgb),
1813                   (unsigned char) qBlue(rgb));
1814          i += 3;
1815        }
1816      }
1817      fprintf (fp, "\n");
1818    }
1819#endif
1820
1821  }
1822
1823  fprintf (fp, "grestore\n");
1824  fprintf (fp, "showpage\n");
1825  fclose (fp);
1826
1827  return true;
1828}
1829/**
1830   Generate Postscript or PDF form image
1831   @param aFilename : name of file
1832   @param aInColor : numbers of colors : 1->BW 2->RGB
1833   @param aImage : Image to print
1834*/
1835bool G4OpenGLQtViewer::generatePS_PDF (
1836 QString aFilename
1837,int aInColor
1838,QImage aImage
1839)
1840{
1841
1842#if QT_VERSION < 0x040000
1843#ifdef Q_WS_MAC || Q_WS_X11
1844  QPrinter printer;
1845  //  printer.setPageSize(pageSize);
1846  if (aInColor == 1) {
1847    printer.setColorMode(QPrinter::GrayScale);
1848  } else {
1849    printer.setColorMode(QPrinter::Color);
1850  }
1851
1852  /* FIXME : I don't know which format it will save...
1853     if (aFilename.endsWith(".ps")) {
1854     printer.setOutputFormat(QPrinter::PostScriptFormat);
1855     } else {
1856     printer.setOutputFormat(QPrinter::PdfFormat);
1857     }
1858  */
1859  printer.setOutputFileName(aFilename);
1860  //  printer.setFullPage ( true);
1861  QPainter paint(&printer);
1862  paint.drawImage (0,0,aImage );
1863  paint.end();
1864#else
1865  G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4\n" << G4endl;
1866#endif
1867#else
1868  QPrinter printer;
1869  //  printer.setPageSize(pageSize);
1870
1871  // FIXME : L. Garnier 4/12/07
1872  // This is not working, it does nothing. Image is staying in color mode
1873  // So I have desactivate the B/W button in GUI
1874  if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1875#if QT_VERSION < 0x040000
1876    aImage = aImage.convertDepth(1,Qt::MonoOnly);
1877#else
1878    aImage = aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1879#endif
1880  }
1881
1882
1883  if (aFilename.endsWith(".ps")) {
1884#if QT_VERSION > 0x040200
1885    printer.setOutputFormat(QPrinter::PostScriptFormat);
1886#endif
1887  } else {
1888#if QT_VERSION > 0x040100
1889    printer.setOutputFormat(QPrinter::PdfFormat);
1890#endif
1891  }
1892#if QT_VERSION > 0x040100
1893  printer.setOutputFileName(aFilename);
1894#endif
1895  //  printer.setFullPage ( true);
1896  QPainter paint(&printer);
1897  paint.drawImage (0,0,aImage);
1898  paint.end();
1899#endif
1900  return true;
1901}
1902
1903
1904
1905void G4OpenGLQtViewer::G4keyPressEvent (QKeyEvent * event)
1906{
1907  if (fHoldKeyEvent)
1908    return;
1909
1910  fHoldKeyEvent = true;
1911
1912#if QT_VERSION < 0x040000
1913  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::AltButton )) { // go backward
1914#else
1915  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::AltModifier )) { // go backward
1916#endif
1917   
1918    moveScene(0,0,1,false);
1919  }
1920#if QT_VERSION < 0x040000
1921  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::AltButton)) { // go forward
1922#else
1923  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::AltModifier)) { // go forward
1924#endif
1925    moveScene(0,0,-1,false);
1926  }
1927#if QT_VERSION < 0x040000
1928  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::ShiftButton)) { // rotate phi
1929#else
1930  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1931#endif
1932    rotateScene(0,-1);
1933  }
1934#if QT_VERSION < 0x040000
1935  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::ShiftButton)) { // rotate phi
1936#else
1937  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1938#endif
1939    rotateScene(0,1);
1940  }
1941#if QT_VERSION < 0x040000
1942  if ((event->key() == Qt::Key_Left) && (event->state() & Qt::ShiftButton)) { // rotate theta
1943#else
1944  if ((event->key() == Qt::Key_Left) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1945#endif
1946    rotateScene(1,0);
1947  }
1948#if QT_VERSION < 0x040000
1949  else if ((event->key() == Qt::Key_Right) && (event->state() & Qt::ShiftButton)) { // rotate theta
1950#else
1951  else if ((event->key() == Qt::Key_Right) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1952#endif
1953    rotateScene(-1,0);
1954  }
1955
1956#if QT_VERSION < 0x040000
1957  if ((event->state() & Qt::AltButton)) {
1958#else
1959  if ((event->modifiers() & Qt::AltModifier)) {
1960#endif
1961    if (event->key() == Qt::Key_Plus) {
1962      fDeltaPosX =fDeltaPosX*(1+fDeltaMove);
1963      fDeltaPosY =fDeltaPosY*(1+fDeltaMove);
1964    }
1965    else if (event->key() == Qt::Key_Minus) {
1966      fDeltaPosX =fDeltaPosX*(1-fDeltaMove);
1967      fDeltaPosY =fDeltaPosY*(1-fDeltaMove);
1968    }
1969  } else {
1970    if (event->key() == Qt::Key_Plus) {
1971      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1+fDeltaZoom));
1972      updateQWidget();
1973    }
1974    else if (event->key() == Qt::Key_Minus) {
1975      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1-fDeltaZoom));
1976      updateQWidget();
1977    }
1978  }
1979 
1980 
1981  if (event->key() == Qt::Key_Escape) { // escaped from full screen
1982#if QT_VERSION >= 0x030200
1983    if (GLWindow->isFullScreen()) {
1984#if QT_VERSION < 0x040000
1985      fFullScreenOn->activated();
1986#else
1987      fFullScreenOn->trigger();
1988#endif
1989    }
1990#endif
1991  }
1992
1993  if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)){ // end of video
1994    fRecordFrames = false;
1995    saveVideo("/temp/output.mpg");
1996  }
1997  if (event->key() == Qt::Key_Space){ // start/pause of video
1998    fRecordFrames = !fRecordFrames;
1999  }
2000
2001  // with no modifiers
2002#if QT_VERSION < 0x040000
2003  if (event->state() == Qt::NoButton) {
2004#else
2005  if (event->modifiers() == Qt::NoModifier) {
2006#endif
2007    if (event->key() == Qt::Key_Down) { // go down
2008      moveScene(0,-1,0,false);
2009    }
2010    else if (event->key() == Qt::Key_Up) {  // go up
2011      moveScene(0,1,0,false);
2012    }
2013    if (event->key() == Qt::Key_Left) { // go left
2014      moveScene(-1,0,0,false);
2015    }
2016    else if (event->key() == Qt::Key_Right) { // go right
2017      moveScene(1,0,0,false);
2018    }
2019  }
2020  fHoldKeyEvent = false;
2021}
2022
2023
2024bool G4OpenGLQtViewer::hasPendingEvents () {
2025  return ((QApplication*)G4Qt::getInstance ())->hasPendingEvents ();
2026}
2027
2028bool G4OpenGLQtViewer::saveVideo (QString outputName) {
2029
2030  // save the parameter file
2031  FILE* fp;
2032  fp = fopen ("/temp/parameter_file.par", "w");
2033
2034  if (fp == NULL) {
2035    return false;
2036  }
2037
2038  fprintf (fp,"# parameter file template with lots of comments to assist you\n");
2039  fprintf (fp,"#\n");
2040  fprintf (fp,"# you can use this as a template, copying it to a separate file then modifying\n");
2041  fprintf (fp,"# the copy\n");
2042  fprintf (fp,"#\n");
2043  fprintf (fp,"#\n");
2044  fprintf (fp,"# any line beginning with '#' is a comment\n");
2045  fprintf (fp,"#\n");
2046  fprintf (fp,"# no line should be longer than 255 characters\n");
2047  fprintf (fp,"#\n");
2048  fprintf (fp,"#\n");
2049  fprintf (fp,"# general format of each line is:\n");
2050  fprintf (fp,"#          \n");
2051  fprintf (fp,"#\n");
2052  fprintf (fp,"# lines can generally be in any order\n");
2053  fprintf (fp,"#\n");
2054  fprintf (fp,"# an exception is the option 'INPUT' which must be followed by input\n");
2055  fprintf (fp,"# files in the order in which they must appear, followed by 'END_INPUT'\n");
2056  fprintf (fp,"#\n");
2057  fprintf (fp,"# Also, if you use the `command` method of generating input file names,\n");
2058  fprintf (fp,"# the command will only be executed in the INPUT_DIR if INPUT_DIR preceeds\n");
2059  fprintf (fp,"# the INPUT parameter.\n");
2060  fprintf (fp,"#\n");
2061  fprintf (fp,"#  MUST be in UPPER CASE\n");
2062  fprintf (fp,"#\n");
2063  fprintf (fp,"\n");
2064  fprintf (fp,"# Pattern affects speed, quality and compression. See the User's Guide\n");
2065  fprintf (fp,"# for more info.\n");
2066  fprintf (fp,"\n");
2067  fprintf (fp,"PATTERN          IBBPBBPBBPBBPBBP\n");
2068#if QT_VERSION < 0x040000
2069  fprintf (fp,"OUTPUT           %s\n",outputName.ascii());
2070#else
2071  fprintf (fp,"OUTPUT           %s\n",outputName.toStdString().c_str());
2072#endif
2073  fprintf (fp,"\n");
2074  fprintf (fp,"# mpeg_encode really only accepts 3 different file formats, but using a\n");
2075  fprintf (fp,"# conversion statement it can effectively handle ANY file format\n");
2076  fprintf (fp,"#\n");
2077  fprintf (fp,"# You must specify the type of the input files.  The choices are:\n");
2078  fprintf (fp,"#    YUV, PPM, JMOVIE, Y, JPEG, PNM\n");
2079  fprintf (fp,"#        (must be upper case)\n");
2080  fprintf (fp,"#\n");
2081  fprintf (fp,"BASE_FILE_FORMAT PPM\n");
2082  fprintf (fp,"\n");
2083  fprintf (fp,"#\n");
2084  fprintf (fp,"# if YUV format (or using parallel version), must provide width and height\n");
2085  fprintf (fp,"# YUV_SIZE       widthxheight\n");
2086  fprintf (fp,"# this option is ignored if BASE_FILE_FORMAT is not YUV and you're running\n");
2087  fprintf (fp,"# on just one machine\n");
2088  fprintf (fp,"#\n");
2089  fprintf (fp,"YUV_SIZE 352x240\n");
2090  fprintf (fp,"\n");
2091  fprintf (fp,"# If you are using YUV, there are different supported file formats.\n");
2092  fprintf (fp,"# EYUV or UCB are the same as previous versions of this encoder.\n");
2093  fprintf (fp,"# (All the Y's, then U's then V's, in 4:2:0 subsampling.)\n");
2094  fprintf (fp,"# Other formats, such as Abekas, Phillips, or a general format are\n");
2095  fprintf (fp,"# permissible, the general format is a string of Y's, U's, and V's\n");
2096  fprintf (fp,"# to specify the file order.\n");
2097  fprintf (fp,"\n");
2098  fprintf (fp,"INPUT_FORMAT UCB\n");
2099  fprintf (fp,"\n");
2100  fprintf (fp,"# the conversion statement\n");
2101  fprintf (fp,"#\n");
2102  fprintf (fp,"# Each occurrence of '*' will be replaced by the input file\n");
2103  fprintf (fp,"#\n");
2104  fprintf (fp,"# e.g., if you have a bunch of GIF files, then this might be:\n");
2105  fprintf (fp,"#        INPUT_CONVERT   giftoppm *\n");
2106  fprintf (fp,"#\n");
2107  fprintf (fp,"# e.g., if you have a bunch of files like a.Y a.U a.V, etc., then:\n");
2108  fprintf (fp,"#        INPUT_CONVERT   cat *.Y *.U *.V\n");
2109  fprintf (fp,"#\n");
2110  fprintf (fp,"# e.g., if you are grabbing from laser disc you might have something like\n");
2111  fprintf (fp,"#        INPUT_CONVERT   goto frame *; grabppm\n");
2112  fprintf (fp,"# 'INPUT_CONVERT *' means the files are already in the base file format\n");
2113  fprintf (fp,"#\n");
2114  fprintf (fp,"INPUT_CONVERT    * \n");
2115  fprintf (fp,"\n");
2116  fprintf (fp,"# number of frames in a GOP.\n");
2117  fprintf (fp,"#\n");
2118  fprintf (fp,"# since each GOP must have at least one I-frame, the encoder will find the\n");
2119  fprintf (fp,"# the first I-frame after GOP_SIZE frames to start the next GOP\n");
2120  fprintf (fp,"#\n");
2121  fprintf (fp,"# later, will add more flexible GOP signalling\n");
2122  fprintf (fp,"#\n");
2123  fprintf (fp,"GOP_SIZE 16\n");
2124  fprintf (fp,"\n");
2125  fprintf (fp,"# number of slices in a frame\n");
2126  fprintf (fp,"#\n");
2127  fprintf (fp,"# 1 is a good number.  another possibility is the number of macroblock rows\n");
2128  fprintf (fp,"# (which is the height divided by 16)\n");
2129  fprintf (fp,"#\n");
2130  fprintf (fp,"SLICES_PER_FRAME 1\n");
2131  fprintf (fp,"\n");
2132  fprintf (fp,"# directory to get all input files from (makes this file easier to read)\n");
2133  fprintf (fp,"INPUT_DIR        /temp\n");
2134  fprintf (fp,"\n");
2135  fprintf (fp,"# There are a bunch of ways to specify the input files.\n");
2136  fprintf (fp,"# from a simple one-per-line listing, to the following \n");
2137  fprintf (fp,"# way of numbering them.  See the manual for more information.\n");
2138  fprintf (fp,"INPUT\n");
2139  fprintf (fp,"# '*' is replaced by the numbers 01, 02, 03, 04\n");
2140  fprintf (fp,"# if I instead do [01-11], it would be 01, 02, ..., 09, 10, 11\n");
2141  fprintf (fp,"# if I instead do [1-11], it would be 1, 2, 3, ..., 9, 10, 11\n");
2142  fprintf (fp,"# if I instead do [1-11+3], it would be 1, 4, 7, 10\n");
2143  fprintf (fp,"# the program assumes none of your input files has a name ending in ']'\n");
2144  fprintf (fp,"# if you do, too bad!!!\n");
2145  fprintf (fp,"#\n");
2146  fprintf (fp,"#\n");
2147  fprintf (fp,"Test*.ppm        [0-%d]\n",fRecordFrameNumber);
2148  fprintf (fp,"# can have more files here if you want...there is no limit on the number\n");
2149  fprintf (fp,"# of files\n");
2150  fprintf (fp,"END_INPUT\n");
2151  fprintf (fp,"\n");
2152  fprintf (fp,"\n");
2153  fprintf (fp,"\n");
2154  fprintf (fp,"# Many of the remaining options have to do with the motion search and qscale\n");
2155  fprintf (fp,"\n");
2156  fprintf (fp,"# FULL or HALF -- must be upper case\n");
2157  fprintf (fp,"# Should be FULL for computer generated images\n");
2158  fprintf (fp,"PIXEL            FULL\n");
2159  fprintf (fp,"\n");
2160  fprintf (fp,"# means +/- this many pixels for both P and B frame searches\n");
2161  fprintf (fp,"# specify two numbers if you wish to serc different ranges in the two.\n");
2162  fprintf (fp,"RANGE            10\n");
2163  fprintf (fp,"\n");
2164  fprintf (fp,"# The two search algorithm parameters below mostly affect speed,\n");
2165  fprintf (fp,"# with some affect on compression and almost none on quality.\n");
2166  fprintf (fp,"\n");
2167  fprintf (fp,"# this must be one of {EXHAUSTIVE, SUBSAMPLE, LOGARITHMIC}\n");
2168  fprintf (fp,"PSEARCH_ALG      LOGARITHMIC\n");
2169  fprintf (fp,"\n");
2170  fprintf (fp,"# this must be one of {SIMPLE, CROSS2, EXHAUSTIVE}\n");
2171  fprintf (fp,"#\n");
2172  fprintf (fp,"# note that EXHAUSTIVE is really, really, really slow\n");
2173  fprintf (fp,"#\n");
2174  fprintf (fp,"BSEARCH_ALG      SIMPLE\n");
2175  fprintf (fp,"\n");
2176  fprintf (fp,"#\n");
2177  fprintf (fp,"# these specify the q-scale for I, P, and B frames\n");
2178  fprintf (fp,"# (values must be between 1 and 31)\n");
2179  fprintf (fp,"# These are the Qscale values for the entire frame in variable bit-rate\n");
2180  fprintf (fp,"# mode, and starting points (but not important) for constant bit rate\n");
2181  fprintf (fp,"#\n");
2182  fprintf (fp,"\n");
2183  fprintf (fp,"# Qscale (Quantization scale) affects quality and compression,\n");
2184  fprintf (fp,"# but has very little effect on speed.\n");
2185  fprintf (fp,"\n");
2186  fprintf (fp,"IQSCALE          4\n");
2187  fprintf (fp,"PQSCALE          5\n");
2188  fprintf (fp,"BQSCALE          12\n");
2189  fprintf (fp,"\n");
2190  fprintf (fp,"# this must be ORIGINAL or DECODED\n");
2191  fprintf (fp,"REFERENCE_FRAME  ORIGINAL\n");
2192  fprintf (fp,"\n");
2193  fprintf (fp,"# for parallel parameters see parallel.param in the exmaples subdirectory\n");
2194  fprintf (fp,"\n");
2195  fprintf (fp,"# if you want constant bit-rate mode, specify it as follows (number is bits/sec):\n");
2196  fprintf (fp,"#BIT_RATE  1000000\n");
2197  fprintf (fp,"\n");
2198  fprintf (fp,"# To specify the buffer size (327680 is default, measused in bits, for 16bit words)\n");
2199  fprintf (fp,"BUFFER_SIZE 327680\n");
2200  fprintf (fp,"\n");
2201  fprintf (fp,"# The frame rate is the number of frames/second (legal values:\n");
2202  fprintf (fp,"# 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60\n");
2203  fprintf (fp,"FRAME_RATE 30\n");
2204  fprintf (fp,"\n");
2205  fprintf (fp,"# There are many more options, see the users manual for examples....\n");
2206  fprintf (fp,"# ASPECT_RATIO, USER_DATA, GAMMA, IQTABLE, etc.\n");
2207  fprintf (fp,"\n");
2208  fprintf (fp,"\n");
2209  fclose (fp);
2210
2211  QProcess::execute ("/sw/bin/mpeg_encode", QStringList("/temp/parameter_file.par"));
2212  return true;
2213}
2214
2215/*
2216 
2217void MultiLayer::exportToSVG(const QString& fname)
2218{
2219  QPicture picture;
2220  QPainter p(&picture);
2221  for (int i=0;i<(int)graphsList->count();i++)
2222    {
2223      Graph *gr=(Graph *)graphsList->at(i);
2224      Plot *myPlot= (Plot *)gr->plotWidget();
2225     
2226      QPoint pos=gr->pos();
2227     
2228      int width=int(myPlot->frameGeometry().width());
2229      int height=int(myPlot->frameGeometry().height());
2230     
2231      myPlot->print(&p, QRect(pos,QSize(width,height)));
2232    }
2233 
2234  p.end();
2235  picture.save(fname, "svg");
2236}
2237*/
2238#endif
Note: See TracBrowser for help on using the repository browser.