source: trunk/source/visualization/OpenGL/src/G4OpenGLQtViewer.cc @ 847

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

last updates

  • Property svn:mime-type set to text/cpp
File size: 84.5 KB
RevLine 
[530]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//
[846]27// $Id: G4OpenGLQtViewer.cc,v 1.24 2008/06/20 13:55:06 lgarnier Exp $
[593]28// GEANT4 tag $Name:  $
[530]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
[844]38//#define GEANT4_QT_DEBUG
[610]39
[530]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"
[564]48#include "G4Scene.hh"
[581]49#include "G4OpenGLQtExportDialog.hh"
[730]50#include "G4OpenGLQtMovieDialog.hh"
[703]51#include "G4UnitsTable.hh"
[539]52#include "G4Qt.hh"
[569]53#include "G4UImanager.hh"
[702]54#include "G4UIcommandTree.hh"
[599]55#include <qlayout.h>
[595]56#include <qdialog.h>
[723]57#include <qprocess.h>
[790]58#include <qapplication.h>
[599]59
60#if QT_VERSION >= 0x040000
[595]61#include <qmenu.h>
62#include <qimagewriter.h>
[599]63#else
64#include <qaction.h>
65#include <qwidgetlist.h>
66#include <qpopupmenu.h>
67#include <qimage.h>
68#endif
69
[720]70#include <qapplication.h>
[595]71#include <qmessagebox.h>
72#include <qfiledialog.h>
73#include <qprinter.h>
[739]74#include <qdatetime.h>
[595]75#include <qpainter.h>
76#include <qgl.h> // include <qglwidget.h>
77#include <qdialog.h>
78#include <qevent.h> //include <qcontextmenuevent.h>
[530]79
[844]80
[530]81//////////////////////////////////////////////////////////////////////////////
82/**
[564]83   Implementation of virtual method of G4VViewer
84*/
[530]85void G4OpenGLQtViewer::SetView (
86)
87//////////////////////////////////////////////////////////////////////////////
88//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
89{
[844]90#ifdef GEANT4_QT_DEBUG
91  printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
92#endif
[564]93  //   if(!fHDC) return;
94  //   if(!fHGLRC) return;
95  //   ::wglMakeCurrent(fHDC,fHGLRC);
96  //  fWindow->makeCurrent();
[844]97  G4OpenGLViewer::SetView ();
98#ifdef GEANT4_QT_DEBUG
99  printf("G4OpenGLQtViewer::SetView --------------------\n");
[804]100#endif
[800]101}
102
[673]103/**
104 * Set the viewport of the scene
105 */
106void G4OpenGLQtViewer::setupViewport(int aWidth, int aHeight)
107{
[709]108  int side = aWidth;
109  if (aHeight < aWidth) side = aHeight;
[673]110  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
111 
112  glMatrixMode(GL_PROJECTION);
113  glLoadIdentity();
114  glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
115  glMatrixMode(GL_MODELVIEW);
116}
[530]117
118
119//////////////////////////////////////////////////////////////////////////////
120/**
[564]121   Implementation of virtual method of G4VViewer
122*/
[530]123void G4OpenGLQtViewer::ShowView (
124)
125//////////////////////////////////////////////////////////////////////////////
126//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
127{
[608]128#ifdef GEANT4_QT_DEBUG
[542]129  printf("G4OpenGLQtViewer::ShowView  +++++++++++++++++++++\n");
[608]130#endif
[579]131  if (!GLWindow) {
[638]132    G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
[579]133  } else {
[599]134#if QT_VERSION < 0x040000
135    GLWindow->setActiveWindow();
136#else
[579]137    GLWindow->activateWindow();
[599]138#endif
[608]139#ifdef GEANT4_QT_DEBUG
[579]140    printf("G4OpenGLQtViewer::ShowView -----------------------\n");
[608]141#endif
[579]142  }
[678]143  glFlush ();
[564]144  //   // Empty the Windows message queue :
145  //   MSG event;
146  //   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
147  //     ::TranslateMessage(&event);
148  //     ::DispatchMessage (&event);
149  //   }
[530]150}
151
152
153
154//////////////////////////////////////////////////////////////////////////////
155void G4OpenGLQtViewer::CreateGLQtContext (
156)
157//////////////////////////////////////////////////////////////////////////////
158//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
159{
[608]160#ifdef GEANT4_QT_DEBUG
[530]161  printf("G4OpenGLQtViewer::CreateGLQtContext \n");
[608]162#endif
[530]163}
164
[564]165
[530]166//////////////////////////////////////////////////////////////////////////////
167void G4OpenGLQtViewer::CreateMainWindow (
[585]168 QGLWidget* glWidget
169)
[530]170//////////////////////////////////////////////////////////////////////////////
171//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
172{
173
[538]174  if(fWindow) return; //Done.
[608]175#ifdef GEANT4_QT_DEBUG
[564]176  printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
[608]177#endif
[538]178
[539]179  // launch Qt if not
180  G4Qt* interactorManager = G4Qt::getInstance ();
181  //  G4UImanager* UI = G4UImanager::GetUIpointer();
182
[564]183  fWindow = glWidget ;
[543]184  //  fWindow->makeCurrent();
[538]185
[539]186  // create window
[569]187  if (((QApplication*)interactorManager->GetMainInteractor())) {
188    // look for the main window
189    bool found = false;
[599]190#if QT_VERSION < 0x040000
[631]191    // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
[626]192    // but if I comment them, it doesn't work...
193    QWidgetList  *list = QApplication::allWidgets();
194    QWidgetListIt it( *list );         // iterate over the widgets
195    QWidget * widget;
196    while ( (widget=it.current()) != 0 ) {  // for each widget...
197      ++it;
198      if ((found== false) && (widget->inherits("QMainWindow"))) {
199        GLWindow = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
200        found = true;
201      }
202    }
203    delete list;                      // delete the list, not the widgets
[599]204#else
[626]205    foreach (QWidget *widget, QApplication::allWidgets()) {
206      if ((found== false) && (widget->inherits("QMainWindow"))) {
[650]207        GLWindow = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
[626]208        found = true;
209      }
210    }
[608]211#endif
[612]212
213#if QT_VERSION < 0x040000
[610]214    glWidget->reparent(GLWindow,0,QPoint(0,0)); 
[612]215#else
216    glWidget->setParent(GLWindow); 
217#endif
[599]218
[569]219    if (found==false) {
[608]220#ifdef GEANT4_QT_DEBUG
[579]221      printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
[608]222#endif
[569]223      GLWindow = new QDialog();
224    }
[539]225  } else {
[608]226#ifdef GEANT4_QT_DEBUG
[579]227    printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
[608]228#endif
[539]229    GLWindow = new QDialog();
230  }
231
[599]232  QHBoxLayout *mainLayout = new QHBoxLayout(GLWindow);
[564]233
[539]234  mainLayout->addWidget(fWindow);
[599]235
236#if QT_VERSION < 0x040000
237  GLWindow->setCaption( tr( "QGl Viewer" ));
238#else
[539]239  GLWindow->setLayout(mainLayout);
[599]240  GLWindow->setWindowTitle(tr("QGl Viewer"));
241#endif
[564]242  GLWindow->resize(300, 300);
[570]243  GLWindow->move(900,300);
[539]244  GLWindow->show();
[530]245 
[579]246  // delete the pointer if close this
247  //  GLWindow->setAttribute(Qt::WA_DeleteOnClose);
248
[610]249#if QT_VERSION >= 0x040000
[711]250//   QObject ::connect(GLWindow,
251//                     SIGNAL(rejected()),
252//                     this,
253//                     SLOT(dialogClosed()));
[610]254#endif
[579]255
[564]256  WinSize_x = 400;
257  WinSize_y = 400;
258  if (WinSize_x < fVP.GetWindowSizeHintX ())
259    WinSize_x = fVP.GetWindowSizeHintX ();
260  if (WinSize_y < fVP.GetWindowSizeHintY ())
261    WinSize_y = fVP.GetWindowSizeHintY ();
[530]262
[564]263  if(!fWindow) return;
[608]264#ifdef GEANT4_QT_DEBUG
[564]265  printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
[608]266#endif
[530]267
[564]268  if (!fContextMenu)
269    createPopupMenu();
[530]270
[564]271}
[530]272
[610]273#if QT_VERSION >= 0x040000
[579]274/**  Close the dialog and set the pointer to NULL
275 */
[711]276// void G4OpenGLQtViewer::dialogClosed() {
277// #ifdef GEANT4_QT_DEBUG
278//   printf("G4OpenGLQtViewer::dialogClosed END\n");
279// #endif
280//   //  GLWindow = NULL;
281// }
[610]282#endif
[579]283
[564]284//////////////////////////////////////////////////////////////////////////////
285G4OpenGLQtViewer::G4OpenGLQtViewer (
286                                    G4OpenGLSceneHandler& scene
287                                    )
288  :G4VViewer (scene, -1)
289  ,G4OpenGLViewer (scene)
290  ,fWindow(0)
[723]291  ,fRecordFrameNumber(0)
[844]292  ,fRotationAngleX(0)
293  ,fRotationAngleY(0)
294  ,fDeltaRotationAngleX(0)
295  ,fDeltaRotationAngleY(0)
[564]296  ,fContextMenu(0)
[696]297  ,fMouseAction(STYLE1)
[728]298  ,fDeltaRotation(1)
[712]299  ,fDeltaSceneTranslation(0.01)
300  ,fDeltaDepth(0.01)
[798]301  ,fDeltaZoom(0.05)
[724]302  ,fDeltaMove(0.05)
[721]303  ,fHoldKeyEvent(false)
[728]304  ,fHoldMoveEvent(false)
305  ,fHoldRotateEvent(false)
[720]306  ,fAutoMove(false)
[739]307  ,fEncoderPath("")
308  ,fTempFolderPath("")
309  ,fMovieTempFolderPath("")
310  ,fSaveFileName("")
[740]311  ,fParameterFileName("mpeg_encode_parameter_file.par")
[732]312  ,fMovieParametersDialog(NULL)
[744]313  ,fRecordingStep(WAIT)
[754]314  ,fProcess(NULL)
[798]315  ,fLaunchSpinDelay(100)
[564]316{
[739]317
[790]318  // launch Qt if not
319  G4Qt* interactorManager = G4Qt::getInstance ();
320
[798]321  fLastPos3 = QPoint(-1,-1);   
322  fLastPos2 = QPoint(-1,-1);   
323  fLastPos1 = QPoint(-1,-1);   
324 
[735]325  initMovieParameters();
[738]326
[797]327  fLastEventTime = new QTime();
328
[608]329#ifdef GEANT4_QT_DEBUG
[790]330  printf("G4OpenGLQtViewer::G4OpenGLQtViewer END\n");
[608]331#endif
[530]332}
333
334//////////////////////////////////////////////////////////////////////////////
[564]335G4OpenGLQtViewer::~G4OpenGLQtViewer (
[539]336)
337//////////////////////////////////////////////////////////////////////////////
338//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
339{
[754]340#if QT_VERSION < 0x040000
341  G4cout <<removeTempFolder().ascii() <<G4endl;
342#else
[750]343  G4cout <<removeTempFolder().toStdString().c_str() <<G4endl;
[754]344#endif
345
[608]346#ifdef GEANT4_QT_DEBUG
[564]347  printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
[608]348#endif
[564]349}
[539]350
351
[564]352/**
353   Create a popup menu for the widget. This menu is activated by right-mouse click
354*/
355void G4OpenGLQtViewer::createPopupMenu()    {
[539]356
[599]357#if QT_VERSION < 0x040000
358  fContextMenu = new QPopupMenu( GLWindow,"All" );
359#else
[564]360  fContextMenu = new QMenu("All");
[599]361#endif
[539]362
[599]363#if QT_VERSION < 0x040000
364  QPopupMenu *mMouseAction = new QPopupMenu( fContextMenu );
[717]365  fContextMenu->insertItem("&Mouse actions",mMouseAction);
366#if QT_VERSION < 0x030200
367  fRotateAction =  new QAction("&Rotate","&Rotate",CTRL+Key_R,mMouseAction,0,true);
368  fMoveAction =  new QAction("&Move","&Move",CTRL+Key_M,mMouseAction,0,true);
369  fPickAction =  new QAction("&Pick","&Pick",CTRL+Key_P,mMouseAction,0,true);
[721]370  QAction * shortcutsAction =  new QAction("&Show shortcuts","&Show shortcuts",CTRL+Key_S,mMouseAction,0,true);
[617]371#else
[718]372  fRotateAction =  new QAction("&Rotate",CTRL+Key_R,mMouseAction);
373  fMoveAction =  new QAction("&Move",CTRL+Key_M,mMouseAction);
374  fPickAction =  new QAction("&Pick",CTRL+Key_P,mMouseAction);
[721]375  QAction *shortcutsAction =  new QAction("&Show shortcuts",CTRL+Key_S,mMouseAction);
[617]376#endif
[717]377  fRotateAction->addTo(mMouseAction);
378  fMoveAction->addTo(mMouseAction);
379  fPickAction->addTo(mMouseAction);
[721]380  shortcutsAction->addTo(mMouseAction);
[599]381
[717]382  fRotateAction->setToggleAction(true);
383  fMoveAction->setToggleAction(true);
384  fPickAction->setToggleAction(true);
[721]385  shortcutsAction->setToggleAction(true);
[599]386
[717]387  fRotateAction->setOn(true);
388  fMoveAction->setOn(false);
389  fPickAction->setOn(false);
[721]390  shortcutsAction->setOn(false);
[713]391
[678]392
[717]393  QObject ::connect(fRotateAction,
394                    SIGNAL(activated()),
395                    this,
[678]396                    SLOT(actionMouseRotate()));
397
[717]398  QObject ::connect(fMoveAction,
399                    SIGNAL(activated()),
400                    this,
[713]401                    SLOT(actionMouseMove()));
402
[717]403  QObject ::connect(fPickAction,
404                    SIGNAL(activated()),
405                    this,
[707]406                    SLOT(actionMousePick()));
[678]407
[721]408  QObject ::connect(shortcutsAction,
[717]409                    SIGNAL(activated()),
410                    this,
[721]411                    SLOT(showShortcuts()));
[678]412
413#else
[717]414  QMenu *mMouseAction = fContextMenu->addMenu("&Mouse actions");
[678]415
[717]416  fRotateAction = mMouseAction->addAction("Rotate");
417  fMoveAction = mMouseAction->addAction("Move");
418  fPickAction = mMouseAction->addAction("Pick");
[721]419  QAction *shortcutsAction = mMouseAction->addAction("Show shortcuts");
[717]420
421  fRotateAction->setCheckable(true);
422  fMoveAction->setCheckable(false);
423  fPickAction->setCheckable(false);
[721]424  shortcutsAction->setCheckable(false);
[717]425
426  fRotateAction->setChecked(true);
427  fMoveAction->setChecked(false);
428  fPickAction->setChecked(false);
[721]429  shortcutsAction->setChecked(false);
[717]430
431  QObject ::connect(fRotateAction,
[678]432                    SIGNAL(triggered(bool)),
433                    this,
434                    SLOT(actionMouseRotate()));
435
[717]436  QObject ::connect(fMoveAction,
[713]437                    SIGNAL(triggered(bool)),
438                    this,
439                    SLOT(actionMouseMove()));
440
[717]441  QObject ::connect(fPickAction,
[678]442                    SIGNAL(triggered(bool)),
443                    this,
[707]444                    SLOT(actionMousePick()));
[678]445
[721]446  QObject ::connect(shortcutsAction,
[678]447                    SIGNAL(triggered(bool)),
448                    this,
[721]449                    SLOT(showShortcuts()));
[678]450#endif
451
[599]452#if QT_VERSION < 0x040000
453  // === Style Menu ===
454  QPopupMenu *mStyle = new QPopupMenu(fContextMenu);
455
456  QPopupMenu *mRepresentation = new QPopupMenu(fContextMenu);
457
[703]458  QPopupMenu *mProjection = new QPopupMenu(fContextMenu);
[702]459
[617]460#if QT_VERSION < 0x030200
[715]461  QAction *polyhedron = new QAction("&Polyhedron","&Polyhedron",CTRL+Key_P,mRepresentation,0,true);
462  QAction *nurbs = new QAction("&NURBS","&NURBS",CTRL+Key_N,mRepresentation,0,true);
[702]463
[715]464  QAction *ortho = new QAction("&Orthographic","&Orthographic",CTRL+Key_O,mProjection,0,true);
465  QAction *perspective = new QAction("&Perspective","&Perspective",CTRL+Key_P,mProjection,0,true);
[617]466#else
[599]467  QAction *polyhedron = new QAction("&Polyhedron",CTRL+Key_P,mRepresentation);
468  QAction *nurbs = new QAction("&NURBS",CTRL+Key_N,mRepresentation);
[702]469
470  QAction *ortho = new QAction("&Orthographic",CTRL+Key_O,mProjection);
471  QAction *perspective = new QAction("&Perspective",CTRL+Key_P,mProjection);
[715]472  polyhedron->setToggleAction(true);
473  nurbs->setToggleAction(true);
474  ortho->setToggleAction(true);
475  perspective->setToggleAction(true);
[617]476#endif
[599]477  polyhedron->addTo(mRepresentation);
478  nurbs->addTo(mRepresentation);
479
[702]480  ortho->addTo(mProjection);
481  perspective->addTo(mProjection);
482
[599]483  mStyle->insertItem("&Representation",mRepresentation);
[702]484  mStyle->insertItem("&Projection",mProjection);
[599]485  fContextMenu->insertItem("&Style",mStyle);
486
[702]487
[599]488#else
489  // === Style Menu ===
[564]490  QMenu *mStyle = fContextMenu->addMenu("&Style");
491
492  QMenu *mRepresentation = mStyle->addMenu("&Representation");
[703]493  QMenu *mProjection = mStyle->addMenu("&Projection");
[632]494  QAction *polyhedron = mRepresentation->addAction("Polyhedron");
[564]495  QAction *nurbs = mRepresentation->addAction("NURBS");
[702]496
[703]497  QAction *ortho = mProjection->addAction("Orthographic");
498  QAction *perspective = mProjection->addAction("Persepective");
[599]499#endif
[702]500
501  // INIT mRepresentation
[564]502  G4ViewParameters::RepStyle style;
503  style = fVP.GetRepStyle();
504  if (style == G4ViewParameters::polyhedron) {
505    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
506  } else if (style == G4ViewParameters::nurbs) {
507    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
[539]508  } else {
[564]509    mRepresentation->clear();
[539]510  }
511
[702]512  // INIT mProjection
513  if (fVP.GetFieldHalfAngle() == 0) {
514    createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),1);
515  } else {
516    createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),2);
517  }
[539]518
[599]519#if QT_VERSION < 0x040000
520  // === Drawing Menu ===
521  QPopupMenu *mDrawing = new QPopupMenu(fContextMenu);
522  fContextMenu->insertItem("&Drawing",mDrawing);
523
[718]524#if QT_VERSION < 0x030200
525  fDrawingWireframe = new QAction("&Wireframe","&Wireframe",CTRL+Key_W,mDrawing,0,true);
526  fDrawingLineRemoval = new QAction("&Hidden line removal","&Hidden line removal",CTRL+Key_L,mDrawing,0,true);
527  fDrawingSurfaceRemoval = new QAction("&Hidden surface removal","&Hidden surface removal",CTRL+Key_S,mDrawing,0,true);
528  fDrawingLineSurfaceRemoval = new QAction("&Hidden line and surface removal","&Hidden line and surface removal",CTRL+Key_R,mDrawing,0,true);
529#else
530  fDrawingWireframe = new QAction("&Wireframe",CTRL+Key_W,mDrawing);
531  fDrawingLineRemoval = new QAction("&Hidden line removal",CTRL+Key_L,mDrawing);
532  fDrawingSurfaceRemoval = new QAction("&Hidden surface removal",CTRL+Key_S,mDrawing);
533  fDrawingLineSurfaceRemoval = new QAction("&Hidden line and surface removal",CTRL+Key_R,mDrawing);
534#endif
535  fDrawingWireframe->setToggleAction(true);
536  fDrawingLineRemoval->setToggleAction(true);
537  fDrawingSurfaceRemoval->setToggleAction(true);
538  fDrawingLineSurfaceRemoval->setToggleAction(true);
[599]539
[718]540  fDrawingWireframe->addTo(mDrawing);
541  fDrawingLineRemoval->addTo(mDrawing);
542  fDrawingSurfaceRemoval->addTo(mDrawing);
543  fDrawingLineSurfaceRemoval->addTo(mDrawing);
[599]544
545
546#else
547  // === Drawing Menu ===
[564]548  QMenu *mDrawing = mStyle->addMenu("&Drawing");
[599]549
[564]550  fDrawingWireframe = mDrawing->addAction("Wireframe");
551  fDrawingWireframe->setCheckable(true);
[599]552
[564]553  fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
554  fDrawingLineRemoval->setCheckable(true);
[599]555
[564]556  fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
557  fDrawingSurfaceRemoval->setCheckable(true);
[599]558
[564]559  fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
560  fDrawingLineSurfaceRemoval->setCheckable(true);
[599]561#endif
[564]562  // INIT Drawing
563  G4ViewParameters::DrawingStyle d_style;
564  d_style = fVP.GetDrawingStyle();
565 
[599]566#if QT_VERSION < 0x040000
[564]567  if (d_style == G4ViewParameters::wireframe) {
[718]568    fDrawingWireframe->setOn(true);
[599]569  } else if (d_style == G4ViewParameters::hlr) {
[718]570    fDrawingLineRemoval->setOn(true);
[599]571  } else if (d_style == G4ViewParameters::hsr) {
[718]572    fDrawingSurfaceRemoval->setOn(true);
[599]573  } else if (d_style == G4ViewParameters::hlhsr) {
[718]574    fDrawingLineSurfaceRemoval->setOn(true);
[599]575  } else {
576    mDrawing->clear();
577  }
[610]578#ifdef GEANT4_QT_DEBUG
579  printf("G4OpenGLQtViewer:: fDrawingWireframe 1\n");
580#endif
581  QObject ::connect(fDrawingWireframe,
[717]582                    SIGNAL(activated()),
[610]583                    this,
584                    SLOT(actionDrawingWireframe()));
585#ifdef GEANT4_QT_DEBUG
586  printf("G4OpenGLQtViewer:: fDrawingWireframe 2\n");
587#endif
588  QObject ::connect(fDrawingLineRemoval,
[717]589                    SIGNAL(activated()),
[610]590                    this,
591                    SLOT(actionDrawingLineRemoval()));
592  QObject ::connect(fDrawingSurfaceRemoval,
[717]593                    SIGNAL(activated()),
[610]594                    this,
595                    SLOT(actionDrawingSurfaceRemoval()));
596  QObject ::connect(fDrawingLineSurfaceRemoval,
[717]597                    SIGNAL(activated()),
[610]598                    this,
599                    SLOT(actionDrawingLineSurfaceRemoval()));
[599]600#else
601  if (d_style == G4ViewParameters::wireframe) {
[564]602    fDrawingWireframe->setChecked(true);
603  } else if (d_style == G4ViewParameters::hlr) {
604    fDrawingLineRemoval->setChecked(true);
605  } else if (d_style == G4ViewParameters::hsr) {
606    fDrawingSurfaceRemoval->setChecked(true);
607  } else if (d_style == G4ViewParameters::hlhsr) {
608    fDrawingLineSurfaceRemoval->setChecked(true);
609  } else {
610    mDrawing->clear();
611  }
[565]612  QObject ::connect(fDrawingWireframe,
613                    SIGNAL(triggered(bool)),
614                    this,
615                    SLOT(actionDrawingWireframe()));
616  QObject ::connect(fDrawingLineRemoval,
617                    SIGNAL(triggered(bool)),
618                    this,
619                    SLOT(actionDrawingLineRemoval()));
620  QObject ::connect(fDrawingSurfaceRemoval,
621                    SIGNAL(triggered(bool)),
622                    this,
623                    SLOT(actionDrawingSurfaceRemoval()));
624  QObject ::connect(fDrawingLineSurfaceRemoval,
625                    SIGNAL(triggered(bool)),
626                    this,
627                    SLOT(actionDrawingLineSurfaceRemoval()));
[610]628#endif
[564]629
630
[610]631
[599]632#if QT_VERSION < 0x040000
633  QPopupMenu *mBackground = new QPopupMenu(mStyle);
634  mStyle->insertItem("&Background color",mBackground);
635
[617]636#if QT_VERSION < 0x030200
[715]637  QAction *white = new QAction("&White","&White",CTRL+Key_W,mBackground,0,true);
638  QAction *black =  new QAction("&Black","&Black",CTRL+Key_B,mBackground,0,true);
[617]639#else
[599]640  QAction *white = new QAction("&White",CTRL+Key_W,mBackground);
641  QAction *black =  new QAction("&Black",CTRL+Key_B,mBackground);
[715]642  white->setToggleAction(true);
643  black->setToggleAction(true);
[617]644#endif
[599]645  white->addTo(mBackground);
646  black->addTo(mBackground);
647
648#else
[564]649  QMenu *mBackground = mStyle->addMenu("&Background color");
650  QAction *white = mBackground->addAction("White");
651  QAction *black = mBackground->addAction("Black");
[599]652
653#endif
[564]654  if (background.GetRed() == 1. &&
655      background.GetGreen() == 1. &&
656      background.GetBlue() == 1.) {
657    createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
658  } else {
659    createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
660  }
661
662
[599]663#if QT_VERSION < 0x040000
664  // === Action Menu ===
665  QPopupMenu *mActions = new QPopupMenu(fContextMenu);
666  fContextMenu->insertItem("&Actions",mActions);
667
[617]668#if QT_VERSION < 0x030200
[715]669  QAction *createEPS =  new QAction("&Save as ...","&Save as ...",CTRL+Key_S,mActions,0,true);
[617]670#else
[599]671  QAction *createEPS =  new QAction("&Save as ...",CTRL+Key_S,mActions);
[617]672#endif
[599]673  createEPS->addTo(mActions);
[610]674  QObject ::connect(createEPS,
675                    SIGNAL(activated()),
676                    this,
[731]677                    SLOT(actionSaveImage()));
[599]678
679#else
680  // === Action Menu ===
[564]681  QMenu *mActions = fContextMenu->addMenu("&Actions");
[568]682  QAction *createEPS = mActions->addAction("Save as ...");
[565]683  QObject ::connect(createEPS,
[566]684                    SIGNAL(triggered()),
[565]685                    this,
[731]686                    SLOT(actionSaveImage()));
[610]687#endif
[564]688
[730]689#if QT_VERSION < 0x040000
690#if QT_VERSION < 0x030200
[731]691  QAction *movieParameters =  new QAction("&Movie parameters...","&Make movie ...",CTRL+Key_M,mActions,0,true);
[730]692#else
[733]693  QAction *movieParameters =  new QAction("&Movie parameters...",CTRL+Key_M,mActions);
[730]694#endif
[731]695  movieParameters->addTo(mActions);
696  QObject ::connect(movieParameters,
[730]697                    SIGNAL(activated()),
698                    this,
[731]699                    SLOT(actionMovieParameters()));
[564]700
[730]701#else
702  // === Action Menu ===
[731]703  QAction *movieParameters = mActions->addAction("Movie parameters...");
704  QObject ::connect(movieParameters,
[730]705                    SIGNAL(triggered()),
706                    this,
[731]707                    SLOT(actionMovieParameters()));
[730]708#endif
[610]709
[730]710
711
712
[599]713#if QT_VERSION < 0x040000
714  // === Special Menu ===
715  QPopupMenu *mSpecial = new QPopupMenu(fContextMenu);
716  fContextMenu->insertItem("S&pecial",mSpecial);
717
718  QPopupMenu *mTransparency = new QPopupMenu(mSpecial);
719  mSpecial->insertItem("Transparency",mTransparency);
720
[617]721#if QT_VERSION < 0x030200
[715]722  QAction *transparencyOn = new QAction("&On","&On",CTRL+Key_O,mTransparency,0,true);
723  QAction *transparencyOff = new QAction("&Off","&Off",CTRL+Key_F,mTransparency,0,true);
[617]724#else
[599]725  QAction *transparencyOn = new QAction("&On",CTRL+Key_O,mTransparency);
726  QAction *transparencyOff = new QAction("&Off",CTRL+Key_F,mTransparency);
[715]727  transparencyOn->setToggleAction(true);
728  transparencyOff->setToggleAction(true);
[617]729#endif
[599]730  transparencyOn->addTo(mTransparency);
731  transparencyOff->addTo(mTransparency);
732
733#else
734  // === Special Menu ===
[564]735  QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
[599]736  QMenu *mTransparency = mSpecial->addMenu("Transparency");
737  QAction *transparencyOn = mTransparency->addAction("On");
738  QAction *transparencyOff = mTransparency->addAction("Off");
739#endif
740
[564]741  if (transparency_enabled == false) {
742    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
743  } else if (transparency_enabled == true) {
744    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
745  } else {
746    mSpecial->clear();
747  }
748
749
[599]750#if QT_VERSION < 0x040000
751  QPopupMenu *mAntialiasing = new QPopupMenu(mSpecial);
752  mSpecial->insertItem("Antialiasing",mAntialiasing);
753
[617]754#if QT_VERSION < 0x030200
[715]755  QAction *antialiasingOn = new QAction("&On","&On",CTRL+Key_O,mAntialiasing,0,true);
756  QAction *antialiasingOff = new QAction("&Off","&Off",CTRL+Key_F,mAntialiasing,0,true);
[617]757#else
[599]758  QAction *antialiasingOn = new QAction("&On",CTRL+Key_O,mAntialiasing);
759  QAction *antialiasingOff = new QAction("&Off",CTRL+Key_F,mAntialiasing);
[715]760  antialiasingOn->setToggleAction(true);
761  antialiasingOff->setToggleAction(true);
[617]762#endif
[599]763  antialiasingOn->addTo(mAntialiasing);
764  antialiasingOff->addTo(mAntialiasing);
765
766#else
[564]767  QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
768  QAction *antialiasingOn = mAntialiasing->addAction("On");
769  QAction *antialiasingOff = mAntialiasing->addAction("Off");
[599]770#endif
771
[564]772  if (antialiasing_enabled == false) {
773    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
774  } else if (antialiasing_enabled == true) {
775    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
776  } else {
777    mAntialiasing->clear();
778  }
779
[599]780#if QT_VERSION < 0x040000
781  QPopupMenu *mHaloing = new QPopupMenu(mSpecial);
782  mSpecial->insertItem("Haloing",mHaloing);
783
[617]784#if QT_VERSION < 0x030200
[715]785  QAction *haloingOn = new QAction("&On","&On",CTRL+Key_O,mHaloing,0,true);
786  QAction *haloingOff = new QAction("&Off","&Off",CTRL+Key_F,mHaloing,0,true);
[617]787#else
[599]788  QAction *haloingOn = new QAction("&On",CTRL+Key_O,mHaloing);
789  QAction *haloingOff = new QAction("&Off",CTRL+Key_F,mHaloing);
[715]790  haloingOn->setToggleAction(true);
791  haloingOff->setToggleAction(true);
[617]792#endif
[599]793  haloingOn->addTo(mHaloing);
794  haloingOff->addTo(mHaloing);
795#else
[564]796  QMenu *mHaloing = mSpecial->addMenu("Haloing");
797  QAction *haloingOn = mHaloing->addAction("On");
798  QAction *haloingOff = mHaloing->addAction("Off");
[599]799#endif
[564]800  if (haloing_enabled == false) {
801    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
802  } else if (haloing_enabled == true) {
803    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
804  } else {
805    mHaloing->clear();
806  }
807
[599]808#if QT_VERSION < 0x040000
809  QPopupMenu *mAux = new QPopupMenu(mSpecial);
810  mSpecial->insertItem("Auxiliairy edges",mAux);
811
[617]812#if QT_VERSION < 0x030200
[715]813  QAction *auxOn = new QAction("&On","&On",CTRL+Key_O,mAux,0,true);
814  QAction *auxOff = new QAction("&Off","&Off",CTRL+Key_F,mAux,0,true);
[617]815#else
[599]816  QAction *auxOn = new QAction("&On",CTRL+Key_O,mAux);
817  QAction *auxOff = new QAction("&Off",CTRL+Key_F,mAux);
[715]818  auxOn->setToggleAction(true);
819  auxOff->setToggleAction(true);
[617]820#endif
[599]821  auxOn->addTo(mAux);
822  auxOff->addTo(mAux);
823
824#else
[564]825  QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
826  QAction *auxOn = mAux->addAction("On");
827  QAction *auxOff = mAux->addAction("Off");
[599]828#endif
[564]829  if (!fVP.IsAuxEdgeVisible()) {
830    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
831  } else {
832    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
833  }
834
835
[715]836
[599]837#if QT_VERSION < 0x040000
838  QPopupMenu *mFullScreen = new QPopupMenu(mSpecial);
839  mSpecial->insertItem("Full screen",mFullScreen);
840
[715]841#if QT_VERSION < 0x030200
[717]842  fFullScreenOn = new QAction("&On","&On",CTRL+Key_O,mFullScreen,0,true);
843  fFullScreenOff = new QAction("&Off","&Off",CTRL+Key_F,mFullScreen,0,true);
[715]844#else
[717]845  fFullScreenOn = new QAction("&On",CTRL+Key_O,mFullScreen);
846  fFullScreenOff = new QAction("&Off",CTRL+Key_F,mFullScreen);
847  fFullScreenOn->setToggleAction(true);
848  fFullScreenOff->setToggleAction(true);
[715]849#endif
[717]850  fFullScreenOn->addTo(mFullScreen);
851  fFullScreenOff->addTo(mFullScreen);
[599]852#else
[715]853  QMenu *mFullScreen = mSpecial->addMenu("&Full screen");
[717]854  fFullScreenOn = mFullScreen->addAction("On");
855  fFullScreenOff = mFullScreen->addAction("Off");
[599]856#endif
[717]857  createRadioAction(fFullScreenOn,fFullScreenOff,SLOT(toggleFullScreen(bool)),2);
[564]858
859}
860
861void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
862{
[579]863  if (!GLWindow) {
[748]864    G4cerr << "Visualization window not defined, please choose one before" << G4endl;
[579]865  } else {
[539]866 
[579]867    if (!fContextMenu)
868      createPopupMenu();
869   
870    // launch menu
871    if ( fContextMenu ) {
872      fContextMenu->exec( e->globalPos() );
873      //    delete fContextMenu;
874    }
[564]875  }
876  e->accept();
877}
[539]878
879
[564]880/**
881   Create a radio button menu. The two menu will be connected. When click on one,
882   eatch state will be invert and callback method will be called.
883   @param action1 first action to connect
884   @param action2 second action to connect
885   @param method callback method
886   @param nCheck: 1 : first action will be set true. 2 : second action will be set true
887*/
[599]888#if QT_VERSION < 0x040000
[564]889void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
890
[599]891  if (action1->parent()->inherits("QPopupMenu")){
892    ((QPopupMenu*)action1->parent())->setCheckable(true);
[715]893    ((QPopupMenu*)action2->parent())->setCheckable(true);
[599]894  }
[715]895  action1->setOn(false);
896   action2->setOn(false);
[599]897
898  if (nCheck ==1)
[715]899    action1->setOn(true);
[599]900  else
[715]901    action2->setOn(true);
[599]902   
[717]903  //FIXME : Should not work on Qt3
[610]904  QObject ::connect(action1, SIGNAL(activated()),action2, SLOT(toggle()));
905  QObject ::connect(action2, SIGNAL(activated()),action1, SLOT(toggle()));
[599]906
907  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
908}
909
910#else
911void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
912
[564]913  action1->setCheckable(true);
914  action2->setCheckable(true);
915
916  if (nCheck ==1)
917    action1->setChecked (true);
918  else
919    action2->setChecked (true);
920   
921  QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
922  QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
923
924  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
925
926}
[599]927#endif
[564]928
929/**
[678]930   Slot activate when mouseAction->rotate menu is set
931 */
932void G4OpenGLQtViewer::actionMouseRotate() {
[696]933  emit toggleMouseAction(STYLE1);
[678]934}
935
936
937/**
[713]938   Slot activate when mouseAction->rotate menu is set
939 */
940void G4OpenGLQtViewer::actionMouseMove() {
941  emit toggleMouseAction(STYLE2);
942}
943
944
945/**
[707]946   Slot activate when mouseAction->zoom menu is set
[678]947 */
[707]948void G4OpenGLQtViewer::actionMousePick() {
[678]949#ifdef GEANT4_QT_DEBUG
[707]950  printf("G4OpenGLQtViewer::actionMousePick \n");
[678]951#endif
[696]952  emit toggleMouseAction(STYLE3);
[678]953}
954
[681]955
956/**
[564]957   Slot activate when drawing->wireframe menu is set
958 */
959void G4OpenGLQtViewer::actionDrawingWireframe() {
960  emit toggleDrawingAction(1);
961}
962
963/**
964   Slot activate when drawing->line removal menu is set
965 */
966void G4OpenGLQtViewer::actionDrawingLineRemoval() {
967  emit toggleDrawingAction(2);
968}
969
970/**
971   Slot activate when drawing->surface removal menu is set
972 */
973void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
974  emit toggleDrawingAction(3);
975}
976
977/**
978   Slot activate when drawing->wireframe menu is set
979 */
980void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
981  emit toggleDrawingAction(4);
982}
983
984
985/**
[678]986   Slot activated when mouse action is toggle
[696]987   @param aAction : STYLE1, STYLE2, STYLE3
[678]988 */
989void G4OpenGLQtViewer::toggleMouseAction(mouseActions aAction) {
990 
[713]991  if ((aAction == STYLE1) || //initialize all
992      (aAction == STYLE2) ||
[721]993      (aAction == STYLE3))  {
[713]994#if QT_VERSION < 0x040000
[717]995    fRotateAction->setOn (false);
996    fMoveAction->setOn (false);
997    fPickAction->setOn (false);
[713]998#else
[717]999    fRotateAction->setChecked (false);
1000    fMoveAction->setChecked (false);
1001    fPickAction->setChecked (false);
[713]1002#endif
1003    fVP.SetPicking(false);
1004    fMouseAction = aAction;
1005  }
1006  // rotate
[707]1007  if (aAction == STYLE1) {  // rotate
[721]1008    showShortcuts();
[678]1009#if QT_VERSION < 0x040000
[717]1010    fRotateAction->setOn (true);
[678]1011#else
[717]1012    fRotateAction->setChecked (true);
[678]1013#endif
[713]1014  } else  if (aAction == STYLE2) { //move
1015#if QT_VERSION < 0x040000
[717]1016    fMoveAction->setOn (true);
[713]1017#else
[717]1018    fMoveAction->setChecked (true);
[713]1019#endif
1020  } else  if (aAction == STYLE3) { //pick
[678]1021#if QT_VERSION < 0x040000
[717]1022    fPickAction->setOn (true);
[678]1023#else
[717]1024    fPickAction->setChecked (true);
[678]1025#endif
[707]1026    fVP.SetPicking(true);
[721]1027  }
1028}
[707]1029
[721]1030/**
1031   Show shortcuts for this mouse action
1032 */
1033void G4OpenGLQtViewer::showShortcuts() {
1034  if (fMouseAction == STYLE1) {  // rotate
[748]1035    G4cout << "Click and move mouse to rotate volume " << G4endl;
1036    G4cout << "Press left/right arrows to move volume left/right" << G4endl;
1037    G4cout << "Press up/down arrows to move volume up/down" << G4endl;
1038    G4cout << "Press ALT+up/down arrows to move volume toward/forward" << G4endl;
1039    G4cout << "Press SHIFT+left/right arrows to rotate volume left/right" << G4endl;
1040    G4cout << "Press SHIFT+up/down arrows to rotate volume up/down" << G4endl;
1041    G4cout << "Press ALT+/- to slow/speed auto rotation/move" << G4endl;
1042    G4cout << "In video mode : " << G4endl;
1043    G4cout << " Press SPACE to Start/Pause video recording " << G4endl;
1044    G4cout << " Press RETURN to Stop video recording " << G4endl;
[721]1045  } else  if (fMouseAction == STYLE2) { //move
[748]1046    G4cout << "Move camera point of view with mouse" << G4endl;
1047    G4cout << "Press left/right arrows to move volume left/right" << G4endl;
1048    G4cout << "Press up/down arrows to move volume up/down" << G4endl;
1049    G4cout << "Press ALT+up/down arrows to move volume toward/forward" << G4endl;
1050    G4cout << "Press SHIFT+left/right arrows to rotate volume left/right" << G4endl;
1051    G4cout << "Press SHIFT+up/down arrows to rotate volume up/down" << G4endl;
1052    G4cout << "Press +/- to zoom into volume" << G4endl;
1053    G4cout << "Press ALT+/- to slow/speed auto rotation/move" << G4endl;
1054    G4cout << "In video mode : " << G4endl;
1055    G4cout << " Press SPACE to Start/Pause video recording " << G4endl;
1056    G4cout << " Press RETURN to Stop video recording " << G4endl;
[721]1057  } else  if (fMouseAction == STYLE3) { //pick
[748]1058    G4cout << "Click and pick " << G4endl;
[678]1059  }
1060
1061}
[721]1062
1063
1064
[678]1065/**
[565]1066   Slot activated when drawing menu is toggle
1067   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1068   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1069   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1070   It will cause a redraw of the view
1071   @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
1072   @see G4OpenGLStoredQtViewer::DrawView
1073   @see G4XXXStoredViewer::CompareForKernelVisit
[564]1074 */
1075void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
1076
1077  G4ViewParameters::DrawingStyle d_style;
[539]1078 
1079
[718]1080  // initialize
1081  if ((aAction >0) && (aAction <5)) {
[600]1082#if QT_VERSION < 0x040000
[718]1083    fDrawingWireframe->setOn(false);
1084    fDrawingLineRemoval->setOn(false);
1085    fDrawingSurfaceRemoval->setOn(false);
1086    fDrawingLineSurfaceRemoval->setOn(false);
[600]1087#else
[718]1088    fDrawingWireframe->setChecked (false);
[564]1089    fDrawingLineRemoval->setChecked (false);
1090    fDrawingSurfaceRemoval->setChecked (false);
1091    fDrawingLineSurfaceRemoval->setChecked (false);
[600]1092#endif
[718]1093  }
1094  if (aAction ==1) {
1095#if QT_VERSION < 0x040000
1096    fDrawingWireframe->setOn(true);
1097#else
1098    fDrawingWireframe->setChecked (true);
1099#endif
[564]1100
1101    d_style = G4ViewParameters::wireframe;
1102
1103  } else  if (aAction ==2) {
[600]1104#if QT_VERSION < 0x040000
[718]1105    fDrawingLineRemoval->setOn(true);
[600]1106#else
[564]1107    fDrawingLineRemoval->setChecked (true);
[600]1108#endif
[564]1109
1110    d_style = G4ViewParameters::hlr;
1111
1112  } else  if (aAction ==3) {
[600]1113#if QT_VERSION < 0x040000
[718]1114    fDrawingSurfaceRemoval->setOn(true);
[600]1115#else
[564]1116    fDrawingSurfaceRemoval->setChecked (true);
[600]1117#endif
[564]1118
1119    d_style = G4ViewParameters::hsr;
1120
1121  } else  if (aAction ==4) {
[600]1122#if QT_VERSION < 0x040000
[718]1123    fDrawingLineSurfaceRemoval->setOn(true);
[600]1124#else
[564]1125    fDrawingLineSurfaceRemoval->setChecked (true);
[600]1126#endif
[564]1127    d_style = G4ViewParameters::hlhsr;
1128  }
1129  fVP.SetDrawingStyle(d_style);
1130
1131  updateQWidget();
[539]1132}
1133
[564]1134
1135/**
1136   SLOT Activate by a click on the representation menu
[565]1137   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1138   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1139   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1140   It will cause a redraw of the view
[564]1141   @param check : 1 polyhedron, 0 nurbs
[565]1142   @see G4OpenGLStoredQtViewer::DrawView
1143   @see G4XXXStoredViewer::CompareForKernelVisit
[564]1144*/
1145void G4OpenGLQtViewer::toggleRepresentation(bool check) {
1146
1147  G4ViewParameters::RepStyle style;
1148  if (check == 1) {
1149    style = G4ViewParameters::polyhedron;
1150  } else {
1151    style = G4ViewParameters::nurbs;
1152  }
1153  fVP.SetRepStyle (style);
1154
1155  updateQWidget();
[530]1156}
1157
[564]1158/**
[702]1159   SLOT Activate by a click on the projection menu
1160   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1161   KernelVisitDecision () will be call and will set the fNeedKernelVisit
1162   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1163   It will cause a redraw of the view
1164   @param check : 1 orthographic, 2 perspective
1165   @see G4OpenGLStoredQtViewer::DrawView
1166   @see G4XXXStoredViewer::CompareForKernelVisit
1167*/
1168void G4OpenGLQtViewer::toggleProjection(bool check) {
1169
1170  if (check == 1) {
1171    fVP.SetFieldHalfAngle (0);
1172  } else {
[703]1173
[702]1174    // look for the default parameter hidden in G4UIcommand parameters
1175    G4UImanager* UI = G4UImanager::GetUIpointer();
1176    if(UI==NULL)
1177      return;
1178    G4UIcommandTree * treeTop = UI->GetTree();
1179
1180    // find command
1181    G4UIcommand* command = treeTop->FindPath("/vis/viewer/set/projection");
1182    if (!command)
1183      return;
1184
1185    // find param
[703]1186    G4UIparameter * angleParam = NULL;
[702]1187    for(G4int i=0;  i<command->GetParameterEntries(); i++)
1188    {
1189      if( command->GetParameter(i)->GetParameterName() == "field-half-angle" ) {
[703]1190        angleParam = command->GetParameter(i);
[702]1191      }
1192    }
[703]1193    if (!angleParam)
[702]1194      return;
[703]1195
1196    // find unit
1197    G4UIparameter * unitParam = NULL;
1198    for(G4int i=0;  i<command->GetParameterEntries(); i++)
1199    {
1200      if( command->GetParameter(i)->GetParameterName() == "unit" ) {
1201        unitParam = command->GetParameter(i);
1202      }
1203    }
1204    if (!unitParam)
1205      return;
1206
1207    G4double defaultValue = command->ConvertToDouble(angleParam->GetDefaultValue())
1208                            * G4UnitDefinition::GetValueOf(unitParam->GetDefaultValue());
1209    if (defaultValue > 89.5 || defaultValue <= 0.0) {
[748]1210      G4cerr << "Field half angle should be 0 < angle <= 89.5 degrees. Check your default Field half angle parameter";
[703]1211    } else {
[748]1212      G4cout << "Perspective view has been set to default value. Field half angle="<<angleParam->GetDefaultValue() <<" " << G4endl;
[703]1213      fVP.SetFieldHalfAngle (defaultValue);
1214      SetView ();
1215    }
1216  } 
[702]1217  updateQWidget();
1218}
1219
1220
1221/**
[564]1222   SLOT Activate by a click on the background menu
1223@param check : 1 white, 0 black
1224*/
1225void G4OpenGLQtViewer::toggleBackground(bool check) {
1226
1227  //   //I need to revisit the kernel if the background colour changes and
1228  //   //hidden line removal is enabled, because hlr drawing utilises the
1229  //   //background colour in its drawing...
1230  //   // (Note added by JA 13/9/2005) Background now handled in view
1231  //   // parameters.  A kernel visit is triggered on change of background.
1232  if (check == 1) {
1233    ((G4ViewParameters&)this->GetViewParameters()).
1234      SetBackgroundColour(G4Colour(1.,1.,1.));  // White
1235  } else {
1236    ((G4ViewParameters&)this->GetViewParameters()).
1237      SetBackgroundColour(G4Colour(0.,0.,0.));  // Black
1238  }
1239  updateQWidget();
[530]1240}
1241
[564]1242/**
1243   SLOT Activate by a click on the transparency menu
1244@param check : 1 , 0
1245*/
1246void G4OpenGLQtViewer::toggleTransparency(bool check) {
1247 
1248  if (check) {
1249    transparency_enabled = false;
1250  } else {
1251    transparency_enabled = true;
1252  }
1253  SetNeedKernelVisit (true);
1254  updateQWidget();
1255}
[530]1256
[564]1257/**
1258   SLOT Activate by a click on the antialiasing menu
1259@param check : 1 , 0
1260*/
1261void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
1262
1263  if (!check) {
1264    antialiasing_enabled = false;
1265    glDisable (GL_LINE_SMOOTH);
1266    glDisable (GL_POLYGON_SMOOTH);
1267  } else {
1268    antialiasing_enabled = true;
1269    glEnable (GL_LINE_SMOOTH);
1270    glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
1271    glEnable (GL_POLYGON_SMOOTH);
1272    glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
[530]1273  }
[564]1274
1275  updateQWidget();
1276}
1277
1278/**
1279   SLOT Activate by a click on the haloing menu
1280@param check : 1 , 0
[530]1281*/
[564]1282//FIXME : I SEE NOTHING...
1283void G4OpenGLQtViewer::toggleHaloing(bool check) {
1284  if (check) {
1285    haloing_enabled = false;
1286  } else {
1287    haloing_enabled = true;
1288  }
[530]1289
[564]1290  updateQWidget();
[530]1291
[564]1292}
1293
1294/**
1295   SLOT Activate by a click on the auxiliaire edges menu
1296@param check : 1 , 0
1297*/
1298void G4OpenGLQtViewer::toggleAux(bool check) {
1299  if (check) {
1300    fVP.SetAuxEdgeVisible(false);
1301  } else {
1302    fVP.SetAuxEdgeVisible(true);
1303  }
1304  SetNeedKernelVisit (true);
1305  updateQWidget();
1306
1307}
1308
1309/**
1310   SLOT Activate by a click on the full screen menu
1311*/
[717]1312void G4OpenGLQtViewer::toggleFullScreen(bool check) {
1313  if (check != GLWindow->isFullScreen()) { //toggle
[617]1314#if QT_VERSION >= 0x030200
[717]1315    GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
[617]1316#else
[748]1317    G4cerr << "This version of Qt could not do fullScreen. Resizing the widget is the only solution available." << G4endl;
[717]1318#endif
1319  }
[564]1320}
1321
[723]1322
[739]1323void G4OpenGLQtViewer::savePPMToTemp() {
1324  if (fMovieTempFolderPath == "") {
[723]1325    return;
1326  }
[744]1327  QString fileName ="Test"+QString::number(fRecordFrameNumber)+".ppm";
1328  QString filePath =fMovieTempFolderPath+fileName;
[724]1329
[723]1330  QImage image;
1331  image = fWindow->grabFrameBuffer();
1332  bool res = false;
1333 
1334#if QT_VERSION < 0x040000
[756]1335  res = image.save(filePath,"PPM");
[723]1336#else
[771]1337  res = image.save(filePath,0);
[723]1338#endif
[744]1339  if (res == false) {
[750]1340    resetRecording();
[744]1341    setRecordingInfos("Can't save tmp file "+filePath);
[739]1342    return;
[723]1343  }
[739]1344 
[744]1345  setRecordingInfos("File "+fileName+" saved");
[739]1346  fRecordFrameNumber++;
[723]1347}
1348
1349
1350
[731]1351void G4OpenGLQtViewer::actionSaveImage() {
[580]1352  QString filters;
[600]1353#if QT_VERSION < 0x040000
1354  QStrList listFormat=QImageIO::outputFormats();
1355  char *tmp=listFormat.first();
1356  while (tmp!=0) {
1357    filters += QString(tmp) + ";;";
1358    tmp=listFormat.next();
1359  }
1360#else
[580]1361  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
1362  for (int i = 0; i < formats.size(); ++i) {
[600]1363    filters +=formats.at(i) + ";;";
[580]1364  }
[600]1365#endif
[588]1366  filters += "eps;;";
1367  filters += "ps;;";
1368  filters += "pdf";
[639]1369  QString* selectedFormat = new QString();
[600]1370#if QT_VERSION < 0x040000
1371  QString nomFich =  QFileDialog::getSaveFileName ( ".",
1372                                                    filters,
1373                                                    GLWindow,
1374                                                    "Save file dialog",
1375                                                    tr("Save as ..."),
[639]1376                                                    selectedFormat );
[600]1377#else
[580]1378  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
1379                                                    tr("Save as ..."),
1380                                                    ".",
[582]1381                                                    filters,
[639]1382                                                    selectedFormat );
[600]1383#endif
[580]1384  // bmp jpg jpeg png ppm xbm xpm
[582]1385  if (nomFich == "") {
1386    return;
1387  }
[600]1388#if QT_VERSION < 0x040000
[639]1389  nomFich += "."+QString(selectedFormat->ascii());
1390  QString format = selectedFormat->lower();
[600]1391#else
[639]1392  nomFich += "."+QString(selectedFormat->toStdString().c_str());
1393  QString format = selectedFormat->toLower();
[600]1394#endif
[656]1395  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,format,fWindow->height(),fWindow->width());
[585]1396  if(  exportDialog->exec()) {
[593]1397
1398    QImage image;
[655]1399    bool res = false;
[635]1400    if ((exportDialog->getWidth() !=fWindow->width()) ||
1401        (exportDialog->getHeight() !=fWindow->height())) {
[655]1402      if (format != QString("eps")) {
[748]1403      G4cerr << "Export->Change Size : This function is not implemented, to export in another size, please resize your frame to what you need" << G4endl;
[585]1404     
[635]1405      //    rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1406      //      QGLWidget* glResized = fWindow;
1407
1408      // FIXME :
1409      // L.Garnier : I've try to implement change size function, but the problem is
1410      // the renderPixmap function call the QGLWidget to resize and it doesn't draw
1411      // the content of this widget... It only draw the background.
1412
1413      //      fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2,true );
1414
1415      //      QPixmap pixmap = fWindow->renderPixmap ();
1416     
1417      //      image = pixmap->toImage();
[593]1418      //      glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1419      //      image = glResized->grabFrameBuffer();
[655]1420      }     
[635]1421    } else {
1422      image = fWindow->grabFrameBuffer();
1423    }   
[639]1424    if (format == QString("eps")) {
[652]1425      if (exportDialog->getVectorEPS()) {
[655]1426        res = generateVectorEPS(nomFich,exportDialog->getWidth(),exportDialog->getHeight(),image);
[652]1427      } else {
[655]1428        res = generateEPS(nomFich,exportDialog->getNbColor(),image);
[652]1429      }
[639]1430    } else if ((format == "ps") || (format == "pdf")) {
[655]1431      res = generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1432    } else if ((format == "tif") ||
1433               (format == "tiff") ||
1434               (format == "jpg") ||
1435               (format == "jpeg") ||
1436               (format == "png") ||
1437               (format == "pbm") ||
1438               (format == "pgm") ||
1439               (format == "ppm") ||
1440               (format == "bmp") ||
1441               (format == "xbm") ||
1442               (format == "xpm")) {
[639]1443#if QT_VERSION < 0x040000
[655]1444      res = image.save(nomFich,selectedFormat->ascii(),exportDialog->getSliderValue());
[639]1445#else
[655]1446      res = image.save(nomFich,0,exportDialog->getSliderValue());
[639]1447#endif
[585]1448    } else {
[748]1449      G4cerr << "This version of G4UI Could not generate the selected format" << G4endl;
[585]1450    }
[655]1451    if (res == false) {
1452#if QT_VERSION < 0x040000
[748]1453      G4cerr << "Error while saving file... "<<nomFich.ascii()<<"" << G4endl;
[655]1454#else
[748]1455      G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<< G4endl;
[655]1456#endif
1457    } else {
1458#if QT_VERSION < 0x040000
[748]1459      G4cout << "File "<<nomFich.ascii()<<" has been saved " << G4endl;
[655]1460#else
[748]1461      G4cout << "File "<<nomFich.toStdString().c_str()<<" has been saved " << G4endl;
[655]1462#endif
1463    }
[585]1464   
1465  } else { // cancel selected
1466    return;
[568]1467  }
[580]1468 
[608]1469#ifdef GEANT4_QT_DEBUG
[731]1470  printf("G4OpenGLQtViewer::actionSaveImage() \n");
[608]1471#endif
[565]1472}
1473
[730]1474
[731]1475void G4OpenGLQtViewer::actionMovieParameters() {
[730]1476
[731]1477  showMovieParametersDialog();
[730]1478#ifdef GEANT4_QT_DEBUG
[731]1479  printf("G4OpenGLQtViewer::actionMovieParameters() \n");
[730]1480#endif
1481}
1482
[731]1483
1484void G4OpenGLQtViewer::showMovieParametersDialog() {
[732]1485  if (!fMovieParametersDialog) {
1486    fMovieParametersDialog= new G4OpenGLQtMovieDialog(this,GLWindow);
[752]1487    displayRecordingStatus();
[756]1488    if (getEncoderPath() == "") {
1489      setRecordingInfos("mpeg_encode is needed to encode in video format. It is available here: http://bmrc.berkeley.edu/frame/research/mpeg/");
1490    }
[731]1491  }
[732]1492  fMovieParametersDialog->show();
[731]1493}
1494
1495
[580]1496/*
1497// 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
[565]1498
[580]1499void Graph::exportToSVG(const QString& fname)
1500{
1501  // enable workaround for Qt3 misalignments
1502  QwtPainter::setSVGMode(true);
1503  QPicture picture;
1504  QPainter p(&picture);
1505  d_plot->print(&p, d_plot->rect());
1506  p.end();
[565]1507
[580]1508  picture.save(fname, "svg");
1509}
1510*/
1511
1512
1513
1514
[564]1515/**
1516   Save the current mouse press point
1517   @param p mouse click point
1518*/
[720]1519#if QT_VERSION < 0x040000
[797]1520void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
[720]1521#else
[797]1522void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
[720]1523#endif
[564]1524{
[720]1525  fAutoMove = false; // stop automove
[798]1526  fLastPos1 = p;
1527  fLastPos2 = fLastPos1;
1528  fLastPos3 = fLastPos2;
[797]1529  fLastEventTime->start();
[707]1530  if (fMouseAction == STYLE2){  // pick
[681]1531    Pick(p.x(),p.y());
1532  }
[564]1533}
1534
[797]1535/**
1536*/
1537void G4OpenGLQtViewer::G4MouseReleaseEvent()
1538{
[798]1539#ifdef GEANT4_QT_DEBUG
1540  printf("G4OpenGLQtViewer::mouseRealease() %d,%d  %d,%d  %d,%d\n",fLastPos1.x(),fLastPos1.y(),fLastPos2.x(),fLastPos2.y(),fLastPos3.x(),fLastPos3.y());
1541#endif
[797]1542  fSpinningDelay = fLastEventTime->elapsed();
[798]1543  QPoint delta = (fLastPos3-fLastPos1);
1544  if ((delta.x() == 0) && (delta.y() == 0)) {
[797]1545#ifdef GEANT4_QT_DEBUG
[798]1546  printf("G4OpenGLQtViewer::mouseRealease() EXIT 1 \n");
[797]1547#endif
1548    return;
1549  }
[798]1550  if (fSpinningDelay < fLaunchSpinDelay ) {
[797]1551    fAutoMove = true;
1552    QTime lastMoveTime;
1553    lastMoveTime.start();
1554    // try to addapt speed move/rotate looking to drawing speed
1555    int cycles = 4;
1556    while (fAutoMove) {
[800]1557      //      if ( lastMoveTime.elapsed() > (fSpinningDelay / (cycles/2))) {
[797]1558        if (fMouseAction == STYLE1) {  // rotate
[798]1559          rotateQtScene(((float)delta.x())/cycles,((float)delta.y())/cycles);
[797]1560        } else if (fMouseAction == STYLE2) {  // move
[798]1561          moveScene(-((float)delta.x())/cycles,-((float)delta.y())/cycles,0,true);
[797]1562        }
1563        lastMoveTime.start();
1564        cycles = 1 ;
[800]1565        //      }
[797]1566      ((QApplication*)G4Qt::getInstance ())->processEvents();
[800]1567#ifdef GEANT4_QT_DEBUG
1568        printf("G4OpenGLQtViewer::mouseRealease() cycle :%d \n",lastMoveTime.elapsed());
1569#endif
[797]1570      cycles ++ ;
1571    }
1572  }
1573}
1574
1575
[564]1576/**
1577   @param pos_x mouse x position
1578   @param pos_y mouse y position
1579   @param mButtons mouse button active
[720]1580   @param mAutoMove true: apply this move till another evnt came, false :one time move
[564]1581*/
[599]1582
1583#if QT_VERSION < 0x040000
[797]1584void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::ButtonState mButtons)
[599]1585#else
[797]1586  void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons)
[599]1587#endif
[564]1588{
[713]1589
[797]1590  if (fAutoMove) {
1591    return;
[720]1592  }
[713]1593
[798]1594  fLastPos3 = fLastPos2;
1595  fLastPos2 = fLastPos1;
1596  fLastPos1 = QPoint(pos_x, pos_y);
[801]1597
[800]1598  int deltaX = fLastPos2.x()-fLastPos1.x();
1599  int deltaY = fLastPos2.y()-fLastPos1.y();
1600
[696]1601  if (fMouseAction == STYLE1) {  // rotate
[564]1602    if (mButtons & Qt::LeftButton) {
[801]1603      rotateQtScene(deltaX,deltaY);
[564]1604    }
[713]1605  } else if (fMouseAction == STYLE2) {  // move
1606    if (mButtons & Qt::LeftButton) {
[801]1607      moveScene(-deltaX,-deltaY,0,true);
[713]1608    }
[564]1609  }
[798]1610
[797]1611  fLastEventTime->start();
[564]1612}
1613
[720]1614
[696]1615/**
[711]1616   Move the scene of dx, dy, dz values.
[696]1617   @param dx delta mouse x position
1618   @param dy delta mouse y position
[798]1619   @param mouseMove : true if even comes from a mouse move, false if even comes from key action
[696]1620*/
1621
[798]1622void G4OpenGLQtViewer::moveScene(float dx,float dy, float dz,bool mouseMove)
[696]1623{
[728]1624  if (fHoldMoveEvent)
1625    return;
1626  fHoldMoveEvent = true;
1627
[720]1628  G4double coefTrans = 0;
1629  GLdouble coefDepth = 0;
[797]1630  if(mouseMove) {
1631    coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_x);
1632    if (WinSize_y <WinSize_x) {
1633      coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_y);
[720]1634    }
[797]1635  } else {
1636    coefTrans = getSceneNearWidth()*fDeltaSceneTranslation;
1637    coefDepth = getSceneDepth()*fDeltaDepth;
[720]1638  }
[797]1639  fVP.IncrementPan(-dx*coefTrans,dy*coefTrans,dz*coefDepth);
[843]1640  emit moveX(-dx*coefTrans);
1641  emit moveY(dy*coefTrans);
1642  emit moveZ(dz*coefTrans);
[797]1643 
1644  updateQWidget();
1645  if (fAutoMove)
1646    ((QApplication*)G4Qt::getInstance ())->processEvents();
1647 
[728]1648  fHoldMoveEvent = false;
[696]1649}
1650
1651
1652/**
1653   @param dx delta mouse x position
1654   @param dy delta mouse y position
1655*/
1656
[798]1657void G4OpenGLQtViewer::rotateQtScene(float dx, float dy)
[696]1658{
[798]1659  if (fHoldRotateEvent)
1660    return;
1661  fHoldRotateEvent = true;
[844]1662 
[843]1663  if( dx != 0) {
1664    rotateScene(dx,0,fDeltaRotation);
1665    emit rotateTheta(dx);
1666  }
1667  if( dy != 0) {
1668    rotateScene(0,dy,fDeltaRotation);
1669    emit rotatePhi(dy);
1670  }
[798]1671  updateQWidget();
1672 
1673  fHoldRotateEvent = false;
1674}
[797]1675
[798]1676/**
1677   @param dx delta mouse x position
1678   @param dy delta mouse y position
1679*/
1680
1681void G4OpenGLQtViewer::rotateQtCamera(float dx, float dy)
1682{
[728]1683  if (fHoldRotateEvent)
1684    return;
1685  fHoldRotateEvent = true;
1686
[803]1687  rotateScene(dx,dy,fDeltaRotation);
[843]1688  emit rotateTheta(dx);
1689  emit rotatePhi(dy);
[801]1690  updateQWidget();
1691 
1692  fHoldRotateEvent = false;
1693}
1694
1695
[800]1696
[801]1697
[660]1698/** This is the benning of a rescale function. It does nothing for the moment
1699    @param aWidth : new width
1700    @param aHeight : new height
1701*/
[585]1702void G4OpenGLQtViewer::rescaleImage(
1703 int aWidth
1704,int aHeight
1705){
[608]1706#ifdef GEANT4_QT_DEBUG
[586]1707  printf("should rescale \n");
[608]1708#endif
[660]1709  GLfloat* feedback_buffer;
1710  GLint returned;
1711  FILE* file;
1712 
[673]1713//   feedback_buffer = new GLfloat[size];
1714//   glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1715//   glRenderMode (GL_FEEDBACK);
[660]1716 
[673]1717//   glViewport (0, 0, aWidth, aHeight);
1718//   DrawView();
1719//   returned = glRenderMode (GL_RENDER);
[660]1720
[585]1721}
[586]1722
1723/**
[652]1724   Generate Vectorial Encapsulated Postscript form image
[586]1725   @param aFilename : name of file
1726   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1727   @param aImage : Image to print
1728*/
[652]1729bool G4OpenGLQtViewer::generateVectorEPS (
1730 QString aFilename
1731,int aWidth
1732,int aHeight
1733,QImage aImage
1734)
1735{
1736  // Print vectored PostScript
1737 
1738  G4int size = 5000000;
1739
1740  GLfloat* feedback_buffer;
1741  GLint returned;
1742  FILE* file;
1743 
1744  feedback_buffer = new GLfloat[size];
1745  glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1746  glRenderMode (GL_FEEDBACK);
1747 
[709]1748  int side = aWidth;
1749  if (aHeight < aWidth) side = aHeight;
[675]1750  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
[652]1751  DrawView();
1752
1753  returned = glRenderMode (GL_RENDER);
1754 
1755 
1756#if QT_VERSION < 0x040000
1757  file = fopen (aFilename.ascii(), "w");
1758#else
1759  file = fopen (aFilename.toStdString().c_str(), "w");
1760#endif
1761  if (file) {
1762    spewWireframeEPS (file, returned, feedback_buffer, "rendereps");
1763  } else {
[658]1764#if QT_VERSION < 0x040000
[748]1765    G4cerr << "Could not open "<< aFilename.ascii() << G4endl;
[658]1766#else
[748]1767    G4cerr << "Could not open "<< aFilename.toStdString().c_str() << G4endl;
[658]1768#endif
[652]1769  }
1770 
1771  delete[] feedback_buffer;
1772
1773  return true;
1774}
1775
1776/**
1777   Generate Encapsulated Postscript form image
1778   @param aFilename : name of file
1779   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1780   @param aImage : Image to print
1781*/
[588]1782bool G4OpenGLQtViewer::generateEPS (
[586]1783 QString aFilename
1784,int aInColor
1785,QImage aImage
1786)
1787{
[587]1788  // FIXME
[608]1789#ifdef GEANT4_QT_DEBUG
[586]1790  printf("saving EPS\n");
[608]1791#endif
[586]1792
1793  FILE* fp;
[587]1794
[649]1795  if (aImage.bits () == NULL)
[588]1796    return false;
[586]1797 
[600]1798#if QT_VERSION < 0x040000
1799  fp = fopen (aFilename.ascii(), "w");
1800#else
[602]1801  fp = fopen (aFilename.toStdString().c_str(), "w");
[600]1802#endif
[586]1803  if (fp == NULL) {
[588]1804    return false;
[586]1805  }
1806 
1807  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
[600]1808#if QT_VERSION < 0x040000
1809  fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1810#else
[602]1811  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
[600]1812#endif
[586]1813  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
[587]1814  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
[586]1815  fprintf (fp, "%%%%EndComments\n");
1816  fprintf (fp, "gsave\n");
1817  fprintf (fp, "/bwproc {\n");
1818  fprintf (fp, "    rgbproc\n");
1819  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
1820  fprintf (fp, "    5 -1 roll {\n");
1821  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
1822  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1823  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1824  fprintf (fp, "    { 2 1 roll } ifelse\n");
1825  fprintf (fp, "    }forall\n");
1826  fprintf (fp, "    pop pop pop\n");
1827  fprintf (fp, "} def\n");
1828  fprintf (fp, "systemdict /colorimage known not {\n");
1829  fprintf (fp, "   /colorimage {\n");
1830  fprintf (fp, "       pop\n");
1831  fprintf (fp, "       pop\n");
1832  fprintf (fp, "       /rgbproc exch def\n");
1833  fprintf (fp, "       { bwproc } image\n");
1834  fprintf (fp, "   }  def\n");
1835  fprintf (fp, "} if\n");
[587]1836  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1837  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1838  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1839  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
[586]1840  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1841  fprintf (fp, "false %d\n", aInColor);
1842  fprintf (fp, "colorimage\n");
1843 
1844
[588]1845  int width = aImage.width();
1846  int height = aImage.height();
1847  int depth = aImage.depth();
1848  int size = width*height;
1849 
1850  if (depth == 1)
1851    size = (width+7)/8*height;
1852  else if (aInColor == 1)
1853    size = size*3;
1854 
1855  int i = 0;
[649]1856  //  if ( aInColor ==1 ) {
1857  // FIXME : L. Garnier. For the moment 10 dec 2007, I could not find a way
1858  // to save correctly grayscale Image. I mean that color or grayscale image
1859  // have the same file save size !
1860 
1861  /* } else*/ if (depth == 8) {
[588]1862    for(int y=height-1; y >=0 ; y--) {
1863      const uchar * s = aImage.scanLine(y);
1864      for(int x=0; x <width; x++) {
1865        QRgb rgb = aImage.color(s[x]);
1866        if (aInColor == 1) {
1867          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1868          i++;
1869        } else {
1870          fprintf (fp, " %02hx %02hx %02hx",
1871                   (unsigned char) qRed(rgb),
1872                   (unsigned char) qGreen(rgb),
1873                   (unsigned char) qBlue(rgb));
1874          i += 3;
1875        }
1876      }
[586]1877      fprintf (fp, "\n");
[588]1878    }
1879  } else {
[601]1880#if QT_VERSION < 0x040000
[649]1881    bool alpha = aImage.hasAlphaBuffer();
[601]1882#else
[588]1883    bool alpha = aImage.hasAlphaChannel();
1884    for(int y=height-1; y >=0 ; y--) {
1885      QRgb * s = (QRgb*)(aImage.scanLine(y));
1886      for(int x=0; x <width; x++) {
1887        QRgb rgb = (*s++);
1888        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1889          rgb = qRgb(0xff, 0xff, 0xff);
1890        if (aInColor == 1) {
1891          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1892          i++;
1893        } else {
1894          fprintf (fp, " %02hx %02hx %02hx",
1895                   (unsigned char) qRed(rgb),
1896                   (unsigned char) qGreen(rgb),
1897                   (unsigned char) qBlue(rgb));
1898          i += 3;
1899        }
[587]1900      }
1901      fprintf (fp, "\n");
[588]1902    }
[601]1903#endif
1904
[586]1905  }
[588]1906
[586]1907  fprintf (fp, "grestore\n");
1908  fprintf (fp, "showpage\n");
1909  fclose (fp);
[588]1910
1911  return true;
[586]1912}
[588]1913/**
1914   Generate Postscript or PDF form image
1915   @param aFilename : name of file
1916   @param aInColor : numbers of colors : 1->BW 2->RGB
1917   @param aImage : Image to print
1918*/
1919bool G4OpenGLQtViewer::generatePS_PDF (
1920 QString aFilename
1921,int aInColor
1922,QImage aImage
1923)
1924{
[638]1925
[600]1926#if QT_VERSION < 0x040000
1927#ifdef Q_WS_MAC || Q_WS_X11
[588]1928  QPrinter printer;
1929  //  printer.setPageSize(pageSize);
1930  if (aInColor == 1) {
1931    printer.setColorMode(QPrinter::GrayScale);
1932  } else {
1933    printer.setColorMode(QPrinter::Color);
1934  }
1935
[600]1936  /* FIXME : I don't know which format it will save...
1937     if (aFilename.endsWith(".ps")) {
1938     printer.setOutputFormat(QPrinter::PostScriptFormat);
1939     } else {
1940     printer.setOutputFormat(QPrinter::PdfFormat);
1941     }
1942  */
1943  printer.setOutputFileName(aFilename);
1944  //  printer.setFullPage ( true);
1945  QPainter paint(&printer);
1946  paint.drawImage (0,0,aImage );
1947  paint.end();
1948#else
[748]1949  G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4" << G4endl;
[600]1950#endif
1951#else
1952  QPrinter printer;
1953  //  printer.setPageSize(pageSize);
[637]1954
1955  // FIXME : L. Garnier 4/12/07
1956  // This is not working, it does nothing. Image is staying in color mode
1957  // So I have desactivate the B/W button in GUI
1958  if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1959#if QT_VERSION < 0x040000
1960    aImage = aImage.convertDepth(1,Qt::MonoOnly);
1961#else
1962    aImage = aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1963#endif
[600]1964  }
1965
[637]1966
[588]1967  if (aFilename.endsWith(".ps")) {
[606]1968#if QT_VERSION > 0x040200
[588]1969    printer.setOutputFormat(QPrinter::PostScriptFormat);
[606]1970#endif
[588]1971  } else {
[606]1972#if QT_VERSION > 0x040100
[588]1973    printer.setOutputFormat(QPrinter::PdfFormat);
[606]1974#endif
[588]1975  }
[606]1976#if QT_VERSION > 0x040100
[588]1977  printer.setOutputFileName(aFilename);
[606]1978#endif
[588]1979  //  printer.setFullPage ( true);
1980  QPainter paint(&printer);
[637]1981  paint.drawImage (0,0,aImage);
[588]1982  paint.end();
[600]1983#endif
[588]1984  return true;
1985}
1986
1987
[804]1988void G4OpenGLQtViewer::G4wheelEvent (QWheelEvent * event)
1989{
1990  fVP.SetZoomFactor(fVP.GetZoomFactor()+(fVP.GetZoomFactor()*(event->delta())/1200));
1991  updateQWidget();
[720]1992
[804]1993#ifdef GEANT4_QT_DEBUG
1994  printf("G4OpenGLQtViewer::wheel event  +++++++++++++++++++++ %f %d\n",fVP.GetZoomFactor(),event->delta());
1995#endif
1996}
1997
1998
[696]1999void G4OpenGLQtViewer::G4keyPressEvent (QKeyEvent * event)
2000{
[721]2001  if (fHoldKeyEvent)
[702]2002    return;
2003
[721]2004  fHoldKeyEvent = true;
[702]2005
[709]2006#if QT_VERSION < 0x040000
2007  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::AltButton )) { // go backward
2008#else
[707]2009  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::AltModifier )) { // go backward
[709]2010#endif
[712]2011   
[720]2012    moveScene(0,0,1,false);
[707]2013  }
[709]2014#if QT_VERSION < 0x040000
2015  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::AltButton)) { // go forward
2016#else
[707]2017  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::AltModifier)) { // go forward
[709]2018#endif
[720]2019    moveScene(0,0,-1,false);
[707]2020  }
[709]2021#if QT_VERSION < 0x040000
2022  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::ShiftButton)) { // rotate phi
2023#else
[707]2024  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
[709]2025#endif
[798]2026    rotateQtCamera(0,-1);
[707]2027  }
[709]2028#if QT_VERSION < 0x040000
2029  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::ShiftButton)) { // rotate phi
2030#else
[707]2031  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
[709]2032#endif
[798]2033    rotateQtCamera(0,1);
[707]2034  }
[709]2035#if QT_VERSION < 0x040000
2036  if ((event->key() == Qt::Key_Left) && (event->state() & Qt::ShiftButton)) { // rotate theta
2037#else
[707]2038  if ((event->key() == Qt::Key_Left) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
[709]2039#endif
[798]2040    rotateQtCamera(1,0);
[707]2041  }
[709]2042#if QT_VERSION < 0x040000
[716]2043  else if ((event->key() == Qt::Key_Right) && (event->state() & Qt::ShiftButton)) { // rotate theta
[709]2044#else
2045  else if ((event->key() == Qt::Key_Right) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
2046#endif
[798]2047    rotateQtCamera(-1,0);
[707]2048  }
[721]2049
2050#if QT_VERSION < 0x040000
[722]2051  if ((event->state() & Qt::AltButton)) {
[721]2052#else
[722]2053  if ((event->modifiers() & Qt::AltModifier)) {
[721]2054#endif
[722]2055    if (event->key() == Qt::Key_Plus) {
[728]2056      fDeltaRotation = fDeltaRotation/0.7;
[722]2057    }
2058    else if (event->key() == Qt::Key_Minus) {
[728]2059      fDeltaRotation = fDeltaRotation*0.7;
[722]2060    }
2061  } else {
2062    if (event->key() == Qt::Key_Plus) {
2063      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1+fDeltaZoom));
2064      updateQWidget();
2065    }
2066    else if (event->key() == Qt::Key_Minus) {
2067      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1-fDeltaZoom));
2068      updateQWidget();
2069    }
[721]2070  }
[722]2071 
2072 
[704]2073  if (event->key() == Qt::Key_Escape) { // escaped from full screen
[717]2074#if QT_VERSION >= 0x030200
[756]2075      toggleFullScreen(false);
[717]2076#endif
[707]2077  }
[740]2078  // several case here : If return is pressed, in every case -> display the movie parameters dialog
2079  // If one parameter is wrong -> put it in red (only save filenam could be wrong..)
2080  // If encoder not found-> does nothing.Only display a message in status box
2081  // If all ok-> generate parameter file
2082  // If ok -> put encoder button enabled
2083 
[723]2084  if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)){ // end of video
[744]2085   stopVideo();
[723]2086  }
2087  if (event->key() == Qt::Key_Space){ // start/pause of video
[744]2088   startPauseVideo();
[723]2089  }
2090
[707]2091  // with no modifiers
[716]2092#if QT_VERSION < 0x040000
2093  if (event->state() == Qt::NoButton) {
2094#else
[771]2095  if ((event->modifiers() == Qt::NoModifier) || (event->modifiers() == Qt::KeypadModifier )) {
[716]2096#endif
[707]2097    if (event->key() == Qt::Key_Down) { // go down
[730]2098      moveScene(0,1,0,false);
[696]2099    }
[707]2100    else if (event->key() == Qt::Key_Up) {  // go up
[730]2101      moveScene(0,-1,0,false);
[696]2102    }
[707]2103    if (event->key() == Qt::Key_Left) { // go left
[720]2104      moveScene(-1,0,0,false);
[696]2105    }
[707]2106    else if (event->key() == Qt::Key_Right) { // go right
[720]2107      moveScene(1,0,0,false);
[696]2108    }
[707]2109  }
[721]2110  fHoldKeyEvent = false;
[696]2111}
[730]2112 
[696]2113
[744]2114/** Stop the video. Check all parameters and enable encoder button if all is ok.
2115*/
2116void G4OpenGLQtViewer::stopVideo() {
2117
2118 // if encoder parameter is wrong, display parameters dialog and return
2119  if (!fMovieParametersDialog) {
2120    showMovieParametersDialog();
2121  }
2122  setRecordingStatus(STOP);
2123
2124  if (fRecordFrameNumber >0) {
2125    // check parameters if they were modified (Re APPLY them...)
[747]2126    // It will enable/disable encode button
[744]2127    fMovieParametersDialog->checkAllParameters();
2128  } else {
[750]2129    resetRecording();
[744]2130    setRecordingInfos("No frame to encode.");
2131  }
2132}
2133
2134
2135/** Start/Pause the video..
2136*/
2137void G4OpenGLQtViewer::startPauseVideo() {
2138   
2139  // first time, if temp parameter is wrong, display parameters dialog and return
2140  if ( fRecordingStep == WAIT) {
2141    if ( fRecordFrameNumber == 0) {
2142      if (getTempFolderPath() == "") {
2143        showMovieParametersDialog();
2144        setRecordingInfos("You should specified the temp folder in order to make movie");
2145        return;
2146      } else  {
2147        // remove temp folder if it was create
2148        QString tmp = removeTempFolder();
2149        if (tmp !="") {
2150          setRecordingInfos(tmp);
2151          return;
2152        }
2153        tmp = createTempFolder();
2154        if (tmp != "") {
[748]2155          setRecordingInfos("Can't create temp folder."+tmp);
[744]2156          return;
2157        }
2158      }
2159    }
2160  }
2161  if (fRecordingStep == WAIT) {
[745]2162    setRecordingStatus(START);
[744]2163  } else if (fRecordingStep == START) {
[745]2164    setRecordingStatus(PAUSE);
[744]2165  } else if (fRecordingStep == PAUSE) {
[745]2166    setRecordingStatus(CONTINUE);
[744]2167  } else if (fRecordingStep == CONTINUE) {
[745]2168    setRecordingStatus(PAUSE);
[744]2169  }
2170}
2171
2172
2173void G4OpenGLQtViewer::setRecordingStatus(RECORDING_STEP step) {
[752]2174
2175  fRecordingStep = step;
2176  displayRecordingStatus();
2177}
2178
2179
2180void G4OpenGLQtViewer::displayRecordingStatus() {
[744]2181 
2182  QString txt = "";
[752]2183  if (fRecordingStep == WAIT) {
[744]2184    txt  = "Waiting to start...";
2185    fRecordFrameNumber = 0; // reset the frame number
[752]2186  } else if (fRecordingStep == START) {
[744]2187    txt  = "Start Recording...";
[752]2188  } else if (fRecordingStep == PAUSE) {
[744]2189    txt  = "Pause Recording...";
[752]2190  } else if (fRecordingStep == CONTINUE) {
[744]2191    txt  = "Continue Recording...";
[752]2192  } else if (fRecordingStep == STOP) {
[744]2193    txt  = "Stop Recording...";
[752]2194  } else if (fRecordingStep == READY_TO_ENCODE) {
[748]2195    txt  = "Ready to Encode...";
[752]2196  } else if (fRecordingStep == ENCODING) {
[744]2197    txt  = "Encoding...";
[752]2198  } else if (fRecordingStep == FAILED) {
[750]2199    txt  = "Failed to encode...";
[752]2200  } else if (fRecordingStep == SUCCESS) {
[751]2201    txt  = "File encoded successfully";
[744]2202  } else {
2203  }
2204
2205  if (fMovieParametersDialog) {
[745]2206    fMovieParametersDialog->setRecordingStatus(txt);
[744]2207  } else {
2208#if QT_VERSION < 0x040000
2209    G4cout << txt.ascii() << G4endl;
2210#else
2211    G4cout << txt.toStdString().c_str() << G4endl;
2212#endif
2213  }
2214  setRecordingInfos("");
2215}
2216
[752]2217
[744]2218void G4OpenGLQtViewer::setRecordingInfos(QString txt) {
2219  if (fMovieParametersDialog) {
[745]2220    fMovieParametersDialog->setRecordingInfos(txt);
[744]2221  } else {
2222#if QT_VERSION < 0x040000
2223    G4cout << txt.ascii() << G4endl;
2224#else
2225    G4cout << txt.toStdString().c_str() << G4endl;
2226#endif
2227  }
2228}
2229
[739]2230/** Init the movie parameters. Temp dir and encoder path
2231*/
2232void G4OpenGLQtViewer::initMovieParameters() {
2233  //init encoder
2234 
2235   //look for encoderPath
[754]2236     fProcess = new QProcess();
2237     
2238#if QT_VERSION < 0x040000
[756]2239     QObject ::connect(fProcess,SIGNAL(processExited ()),
[754]2240                       this,SLOT(processLookForFinished()));
2241     fProcess->setCommunication(QProcess::DupStderr);
2242     fProcess->setArguments(QStringList("which mpeg_encode"));
2243     fProcess->start();
2244#else
2245     QObject ::connect(fProcess,SIGNAL(finished ( int)),
2246                       this,SLOT(processLookForFinished()));
2247     fProcess->setReadChannelMode(QProcess::MergedChannels);
2248     fProcess->start ("which mpeg_encode");
2249#endif
[739]2250 
2251}
2252
[735]2253/** @return encoder path or "" if it does not exist
[730]2254 */
2255QString G4OpenGLQtViewer::getEncoderPath() {
[737]2256  return fEncoderPath;
[730]2257}
2258 
[720]2259
[730]2260/**
2261 * set the new encoder path
[733]2262 * @return "" if correct. The error otherwise
[730]2263*/
[733]2264QString G4OpenGLQtViewer::setEncoderPath(QString path) {
[735]2265  if (path == "") {
[736]2266    return "File does not exist";
[735]2267  }
[752]2268
[754]2269#if QT_VERSION < 0x040000
2270  path =  QDir::cleanDirPath(path);
2271#else
[740]2272  path =  QDir::cleanPath(path);
[754]2273#endif
2274  QFileInfo *f = new QFileInfo(path);
[732]2275  if (!f->exists()) {
[736]2276    return "File does not exist";
[754]2277  } else if (f->isDir()) {
2278    return "This is a directory";
2279  } else if (!f->isExecutable()) {
[733]2280    return "File exist but is not executable";
[754]2281  } else if (!f->isFile()) {
2282    return "This is not a file";
[732]2283  }
[730]2284  fEncoderPath = path;
[733]2285  return "";
[730]2286}
[732]2287
[745]2288
2289bool G4OpenGLQtViewer::isRecording(){
[746]2290  if ((fRecordingStep == START) || (fRecordingStep == CONTINUE)) {
[745]2291    return true;
2292  }
2293  return false;
[740]2294}
2295
[748]2296bool G4OpenGLQtViewer::isStopped(){
2297  if (fRecordingStep == STOP) {
2298    return true;
2299  }
2300  return false;
2301}
[745]2302
[748]2303
[745]2304bool G4OpenGLQtViewer::isReadyToEncode(){
[748]2305  if (fRecordingStep == READY_TO_ENCODE) {
[745]2306    return true;
2307  }
2308  return false;
2309}
2310
[750]2311void G4OpenGLQtViewer::resetRecording() {
2312    setRecordingStatus(WAIT);
2313}
[745]2314
[732]2315/**
2316 * set the temp folder path
[733]2317 * @return "" if correct. The error otherwise
[732]2318*/
[733]2319QString G4OpenGLQtViewer::setTempFolderPath(QString path) {
[732]2320
[735]2321  if (path == "") {
2322    return "Path does not exist";
2323  }
[754]2324#if QT_VERSION < 0x040000
2325  path =  QDir::cleanDirPath(path);
2326#else
[740]2327  path =  QDir::cleanPath(path);
[754]2328#endif
2329  QFileInfo *d = new QFileInfo(path);
2330  if (!d->exists()) {
[733]2331    return "Path does not exist";
[754]2332  } else if (!d->isDir()) {
2333    return "This is not a directory";
2334  } else if (!d->isReadable()) {
2335    return path +" is read protected";
2336  } else if (!d->isWritable()) {
2337    return path +" is write protected";
[732]2338  }
2339 
2340  fTempFolderPath = path;
[733]2341  return "";
[732]2342}
2343
[735]2344/** @return the temp folder path or "" if it does not exist
[732]2345 */
2346QString G4OpenGLQtViewer::getTempFolderPath() {
[738]2347  return fTempFolderPath;
[732]2348}
[730]2349 
[733]2350/**
2351 * set the save file name path
2352 * @return "" if correct. The error otherwise
2353*/
2354QString G4OpenGLQtViewer::setSaveFileName(QString path) {
[730]2355
[735]2356  if (path == "") {
2357    return "Path does not exist";
2358  }
[756]2359 
2360  QFileInfo *file = new QFileInfo(path);
2361  QDir dir = file->dir();
[754]2362#if QT_VERSION < 0x040000
2363  path =  QDir::cleanDirPath(path);
2364#else
[740]2365  path =  QDir::cleanPath(path);
[754]2366#endif
[756]2367  if (file->exists()) {
2368    return "File already exist, please choose a new one";
2369  } else if (!dir.exists()) {
2370    return "Dir does not exist";
2371  } else if (!dir.isReadable()) {
[754]2372    return path +" is read protected";
[733]2373  }
2374 
2375  fSaveFileName = path;
2376  return "";
2377}
2378
2379/** @return the save file path
2380 */
2381QString G4OpenGLQtViewer::getSaveFileName() {
2382  return fSaveFileName ;
2383}
2384
[738]2385/** Create a Qt_temp folder in the temp folder given
[740]2386* The temp folder will be like this /tmp/QtMovie_12-02-2008_12_12_58/
2387* @return "" if success. Error message if not.
[738]2388*/
[740]2389QString G4OpenGLQtViewer::createTempFolder() {
[738]2390  fMovieTempFolderPath = "";
2391
2392  //check
[740]2393  QString tmp = setTempFolderPath(fTempFolderPath);
2394  if (tmp != "") {
2395    return tmp;
[738]2396  }
[754]2397#if QT_VERSION < 0x040000
2398  QString sep = QChar(QDir::separator());
2399#else
[755]2400  QString sep = QString(QDir::separator());
[754]2401#endif
2402  QString path = sep+"QtMovie_"+QDateTime::currentDateTime ().toString("dd-MM-yyyy_hh-mm-ss")+sep;
2403#if QT_VERSION < 0x040000
2404  QDir *d = new QDir(QDir::cleanDirPath(fTempFolderPath));
2405#else
[740]2406  QDir *d = new QDir(QDir::cleanPath(fTempFolderPath));
[754]2407#endif
[738]2408  // check if it is already present
2409  if (d->exists(path)) {
[748]2410    return "Folder "+path+" already exists.Please remove it first";
[738]2411  }
[739]2412  if (d->mkdir(fTempFolderPath+path)) {
2413    fMovieTempFolderPath = fTempFolderPath+path;
[740]2414    return "";
2415  } else {
[748]2416    return "Can't create "+fTempFolderPath+path;
[738]2417  }
[740]2418  return "-";
[738]2419}
2420
2421/** Remove the Qt_temp folder in the temp folder
2422*/
[740]2423QString G4OpenGLQtViewer::removeTempFolder() {
[738]2424        // remove files in Qt_temp folder
[742]2425  if (fMovieTempFolderPath == "") {
2426    return "";
2427  }
[754]2428#if QT_VERSION < 0x040000
2429  QDir *d = new QDir(QDir::cleanDirPath(fMovieTempFolderPath));
2430#else
[740]2431  QDir *d = new QDir(QDir::cleanPath(fMovieTempFolderPath));
[754]2432#endif
[739]2433  if (!d->exists()) {
[740]2434    return "";  // already remove
[738]2435  }
2436
[751]2437  d->setFilter( QDir::Files );
[738]2438  QStringList subDirList = d->entryList();
[739]2439  int res = true;
[740]2440  QString error = "";
[738]2441  for (QStringList::ConstIterator it = subDirList.begin() ;(it != subDirList.end()) ; it++) {
[739]2442    const QString currentFile = *it;
[751]2443      if (!d->remove(currentFile)) {
2444        res = false;
2445        QString file = fMovieTempFolderPath+currentFile;
2446        error +="Removing file failed : "+file;
2447      } else {
2448      }
[738]2449  }
[739]2450  if (res) {
2451    if (d->rmdir(fMovieTempFolderPath)) {
[750]2452      fMovieTempFolderPath = "";
[740]2453      return "";
2454    } else {
[748]2455      return "Dir "+fMovieTempFolderPath+" should be empty, but could not remove it";
[739]2456    }
[740]2457
[738]2458  }
[748]2459  return "Could not remove "+fMovieTempFolderPath+" because of the following errors :"+error;
[738]2460}
2461
[730]2462
[732]2463
[720]2464bool G4OpenGLQtViewer::hasPendingEvents () {
2465  return ((QApplication*)G4Qt::getInstance ())->hasPendingEvents ();
2466}
2467
[740]2468bool G4OpenGLQtViewer::generateMpegEncoderParameters () {
[720]2469
[747]2470                // save the parameter file
[723]2471  FILE* fp;
[740]2472#if QT_VERSION < 0x040000
[754]2473  fp = fopen (QString(fMovieTempFolderPath+fParameterFileName).ascii(), "w");
[740]2474#else
2475  fp = fopen (QString(fMovieTempFolderPath+fParameterFileName).toStdString().c_str(), "w");
2476#endif
[723]2477
2478  if (fp == NULL) {
[747]2479    setRecordingInfos("Generation of parameter file failed");
[723]2480    return false;
2481  }
2482
2483  fprintf (fp,"# parameter file template with lots of comments to assist you\n");
2484  fprintf (fp,"#\n");
2485  fprintf (fp,"# you can use this as a template, copying it to a separate file then modifying\n");
2486  fprintf (fp,"# the copy\n");
2487  fprintf (fp,"#\n");
2488  fprintf (fp,"#\n");
2489  fprintf (fp,"# any line beginning with '#' is a comment\n");
2490  fprintf (fp,"#\n");
2491  fprintf (fp,"# no line should be longer than 255 characters\n");
2492  fprintf (fp,"#\n");
2493  fprintf (fp,"#\n");
2494  fprintf (fp,"# general format of each line is:\n");
2495  fprintf (fp,"#          \n");
2496  fprintf (fp,"#\n");
2497  fprintf (fp,"# lines can generally be in any order\n");
2498  fprintf (fp,"#\n");
2499  fprintf (fp,"# an exception is the option 'INPUT' which must be followed by input\n");
2500  fprintf (fp,"# files in the order in which they must appear, followed by 'END_INPUT'\n");
2501  fprintf (fp,"#\n");
2502  fprintf (fp,"# Also, if you use the `command` method of generating input file names,\n");
2503  fprintf (fp,"# the command will only be executed in the INPUT_DIR if INPUT_DIR preceeds\n");
2504  fprintf (fp,"# the INPUT parameter.\n");
2505  fprintf (fp,"#\n");
2506  fprintf (fp,"#  MUST be in UPPER CASE\n");
2507  fprintf (fp,"#\n");
2508  fprintf (fp,"\n");
2509  fprintf (fp,"# Pattern affects speed, quality and compression. See the User's Guide\n");
2510  fprintf (fp,"# for more info.\n");
2511  fprintf (fp,"\n");
2512  fprintf (fp,"PATTERN          IBBPBBPBBPBBPBBP\n");
2513#if QT_VERSION < 0x040000
[742]2514  fprintf (fp,"OUTPUT           %s\n",getSaveFileName().ascii());
[723]2515#else
[742]2516  fprintf (fp,"OUTPUT           %s\n",getSaveFileName().toStdString().c_str());
[723]2517#endif
2518  fprintf (fp,"\n");
2519  fprintf (fp,"# mpeg_encode really only accepts 3 different file formats, but using a\n");
2520  fprintf (fp,"# conversion statement it can effectively handle ANY file format\n");
2521  fprintf (fp,"#\n");
2522  fprintf (fp,"# You must specify the type of the input files.  The choices are:\n");
2523  fprintf (fp,"#    YUV, PPM, JMOVIE, Y, JPEG, PNM\n");
2524  fprintf (fp,"#        (must be upper case)\n");
2525  fprintf (fp,"#\n");
2526  fprintf (fp,"BASE_FILE_FORMAT PPM\n");
2527  fprintf (fp,"\n");
2528  fprintf (fp,"#\n");
2529  fprintf (fp,"# if YUV format (or using parallel version), must provide width and height\n");
2530  fprintf (fp,"# YUV_SIZE       widthxheight\n");
2531  fprintf (fp,"# this option is ignored if BASE_FILE_FORMAT is not YUV and you're running\n");
2532  fprintf (fp,"# on just one machine\n");
2533  fprintf (fp,"#\n");
2534  fprintf (fp,"YUV_SIZE 352x240\n");
2535  fprintf (fp,"\n");
2536  fprintf (fp,"# If you are using YUV, there are different supported file formats.\n");
2537  fprintf (fp,"# EYUV or UCB are the same as previous versions of this encoder.\n");
2538  fprintf (fp,"# (All the Y's, then U's then V's, in 4:2:0 subsampling.)\n");
2539  fprintf (fp,"# Other formats, such as Abekas, Phillips, or a general format are\n");
2540  fprintf (fp,"# permissible, the general format is a string of Y's, U's, and V's\n");
2541  fprintf (fp,"# to specify the file order.\n");
2542  fprintf (fp,"\n");
2543  fprintf (fp,"INPUT_FORMAT UCB\n");
2544  fprintf (fp,"\n");
2545  fprintf (fp,"# the conversion statement\n");
2546  fprintf (fp,"#\n");
2547  fprintf (fp,"# Each occurrence of '*' will be replaced by the input file\n");
2548  fprintf (fp,"#\n");
2549  fprintf (fp,"# e.g., if you have a bunch of GIF files, then this might be:\n");
2550  fprintf (fp,"#        INPUT_CONVERT   giftoppm *\n");
2551  fprintf (fp,"#\n");
2552  fprintf (fp,"# e.g., if you have a bunch of files like a.Y a.U a.V, etc., then:\n");
2553  fprintf (fp,"#        INPUT_CONVERT   cat *.Y *.U *.V\n");
2554  fprintf (fp,"#\n");
2555  fprintf (fp,"# e.g., if you are grabbing from laser disc you might have something like\n");
2556  fprintf (fp,"#        INPUT_CONVERT   goto frame *; grabppm\n");
2557  fprintf (fp,"# 'INPUT_CONVERT *' means the files are already in the base file format\n");
2558  fprintf (fp,"#\n");
2559  fprintf (fp,"INPUT_CONVERT    * \n");
2560  fprintf (fp,"\n");
2561  fprintf (fp,"# number of frames in a GOP.\n");
2562  fprintf (fp,"#\n");
2563  fprintf (fp,"# since each GOP must have at least one I-frame, the encoder will find the\n");
2564  fprintf (fp,"# the first I-frame after GOP_SIZE frames to start the next GOP\n");
2565  fprintf (fp,"#\n");
2566  fprintf (fp,"# later, will add more flexible GOP signalling\n");
2567  fprintf (fp,"#\n");
2568  fprintf (fp,"GOP_SIZE 16\n");
2569  fprintf (fp,"\n");
2570  fprintf (fp,"# number of slices in a frame\n");
2571  fprintf (fp,"#\n");
2572  fprintf (fp,"# 1 is a good number.  another possibility is the number of macroblock rows\n");
2573  fprintf (fp,"# (which is the height divided by 16)\n");
2574  fprintf (fp,"#\n");
2575  fprintf (fp,"SLICES_PER_FRAME 1\n");
2576  fprintf (fp,"\n");
2577  fprintf (fp,"# directory to get all input files from (makes this file easier to read)\n");
[740]2578#if QT_VERSION < 0x040000
2579  fprintf (fp,"INPUT_DIR        %s\n",fMovieTempFolderPath.ascii());
2580#else
2581  fprintf (fp,"INPUT_DIR        %s\n",fMovieTempFolderPath.toStdString().c_str());
2582#endif
[723]2583  fprintf (fp,"\n");
2584  fprintf (fp,"# There are a bunch of ways to specify the input files.\n");
2585  fprintf (fp,"# from a simple one-per-line listing, to the following \n");
2586  fprintf (fp,"# way of numbering them.  See the manual for more information.\n");
2587  fprintf (fp,"INPUT\n");
2588  fprintf (fp,"# '*' is replaced by the numbers 01, 02, 03, 04\n");
2589  fprintf (fp,"# if I instead do [01-11], it would be 01, 02, ..., 09, 10, 11\n");
2590  fprintf (fp,"# if I instead do [1-11], it would be 1, 2, 3, ..., 9, 10, 11\n");
2591  fprintf (fp,"# if I instead do [1-11+3], it would be 1, 4, 7, 10\n");
2592  fprintf (fp,"# the program assumes none of your input files has a name ending in ']'\n");
2593  fprintf (fp,"# if you do, too bad!!!\n");
2594  fprintf (fp,"#\n");
2595  fprintf (fp,"#\n");
[751]2596  fprintf (fp,"Test*.ppm        [0-%d]\n",fRecordFrameNumber-1);
[723]2597  fprintf (fp,"# can have more files here if you want...there is no limit on the number\n");
2598  fprintf (fp,"# of files\n");
2599  fprintf (fp,"END_INPUT\n");
2600  fprintf (fp,"\n");
2601  fprintf (fp,"\n");
2602  fprintf (fp,"\n");
2603  fprintf (fp,"# Many of the remaining options have to do with the motion search and qscale\n");
2604  fprintf (fp,"\n");
2605  fprintf (fp,"# FULL or HALF -- must be upper case\n");
2606  fprintf (fp,"# Should be FULL for computer generated images\n");
2607  fprintf (fp,"PIXEL            FULL\n");
2608  fprintf (fp,"\n");
2609  fprintf (fp,"# means +/- this many pixels for both P and B frame searches\n");
2610  fprintf (fp,"# specify two numbers if you wish to serc different ranges in the two.\n");
2611  fprintf (fp,"RANGE            10\n");
2612  fprintf (fp,"\n");
2613  fprintf (fp,"# The two search algorithm parameters below mostly affect speed,\n");
2614  fprintf (fp,"# with some affect on compression and almost none on quality.\n");
2615  fprintf (fp,"\n");
2616  fprintf (fp,"# this must be one of {EXHAUSTIVE, SUBSAMPLE, LOGARITHMIC}\n");
2617  fprintf (fp,"PSEARCH_ALG      LOGARITHMIC\n");
2618  fprintf (fp,"\n");
2619  fprintf (fp,"# this must be one of {SIMPLE, CROSS2, EXHAUSTIVE}\n");
2620  fprintf (fp,"#\n");
2621  fprintf (fp,"# note that EXHAUSTIVE is really, really, really slow\n");
2622  fprintf (fp,"#\n");
2623  fprintf (fp,"BSEARCH_ALG      SIMPLE\n");
2624  fprintf (fp,"\n");
2625  fprintf (fp,"#\n");
2626  fprintf (fp,"# these specify the q-scale for I, P, and B frames\n");
2627  fprintf (fp,"# (values must be between 1 and 31)\n");
2628  fprintf (fp,"# These are the Qscale values for the entire frame in variable bit-rate\n");
2629  fprintf (fp,"# mode, and starting points (but not important) for constant bit rate\n");
2630  fprintf (fp,"#\n");
2631  fprintf (fp,"\n");
2632  fprintf (fp,"# Qscale (Quantization scale) affects quality and compression,\n");
2633  fprintf (fp,"# but has very little effect on speed.\n");
2634  fprintf (fp,"\n");
2635  fprintf (fp,"IQSCALE          4\n");
2636  fprintf (fp,"PQSCALE          5\n");
2637  fprintf (fp,"BQSCALE          12\n");
2638  fprintf (fp,"\n");
2639  fprintf (fp,"# this must be ORIGINAL or DECODED\n");
2640  fprintf (fp,"REFERENCE_FRAME  ORIGINAL\n");
2641  fprintf (fp,"\n");
2642  fprintf (fp,"# for parallel parameters see parallel.param in the exmaples subdirectory\n");
2643  fprintf (fp,"\n");
2644  fprintf (fp,"# if you want constant bit-rate mode, specify it as follows (number is bits/sec):\n");
2645  fprintf (fp,"#BIT_RATE  1000000\n");
2646  fprintf (fp,"\n");
2647  fprintf (fp,"# To specify the buffer size (327680 is default, measused in bits, for 16bit words)\n");
2648  fprintf (fp,"BUFFER_SIZE 327680\n");
2649  fprintf (fp,"\n");
2650  fprintf (fp,"# The frame rate is the number of frames/second (legal values:\n");
2651  fprintf (fp,"# 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60\n");
2652  fprintf (fp,"FRAME_RATE 30\n");
2653  fprintf (fp,"\n");
2654  fprintf (fp,"# There are many more options, see the users manual for examples....\n");
2655  fprintf (fp,"# ASPECT_RATIO, USER_DATA, GAMMA, IQTABLE, etc.\n");
2656  fprintf (fp,"\n");
2657  fprintf (fp,"\n");
2658  fclose (fp);
2659
[747]2660  setRecordingInfos("Parameter file "+fParameterFileName+" generated in "+fMovieTempFolderPath);
[748]2661  setRecordingStatus(READY_TO_ENCODE);
[723]2662  return true;
2663}
2664
[749]2665void G4OpenGLQtViewer::encodeVideo()
[740]2666{
[747]2667  if ((getEncoderPath() != "") && (getSaveFileName() != "")) {
[751]2668    setRecordingStatus(ENCODING);
[754]2669   
2670#if QT_VERSION < 0x040000
[756]2671    QStringList args = QStringList(fEncoderPath);
2672    args.push_back(fMovieTempFolderPath+fParameterFileName);
2673    fProcess = new QProcess(args);
2674    QObject ::connect(fProcess,SIGNAL(processExited ()),
[754]2675                      this,SLOT(processEncodeFinished()));
[756]2676    QObject ::connect(fProcess,SIGNAL(readyReadStdout ()),
[754]2677                      this,SLOT(processEncodeStdout()));
2678    fProcess->setCommunication(QProcess::DupStderr);
[756]2679    fProcess->launch("");
[754]2680#else
[756]2681    fProcess = new QProcess();
[750]2682#if QT_VERSION > 0x040100
[754]2683    QObject ::connect(fProcess,SIGNAL(finished ( int,QProcess::ExitStatus)),
2684                      this,SLOT(processEncodeFinished()));
2685    QObject ::connect(fProcess,SIGNAL(readyReadStandardOutput ()),
2686                      this,SLOT(processEncodeStdout()));
[750]2687#else
[754]2688    QObject ::connect(fProcess,SIGNAL(finished ( int)),
2689                      this,SLOT(processEncodeFinished()));
2690    QObject ::connect(fProcess,SIGNAL(readyReadStandardOutput ()),
2691                      this,SLOT(processEncodeStdout()));
[750]2692#endif
[754]2693    fProcess->setReadChannelMode(QProcess::MergedChannels);
2694    fProcess->start (fEncoderPath, QStringList(fMovieTempFolderPath+fParameterFileName));
[750]2695#endif
[749]2696  }
2697}
[740]2698
2699
[756]2700// FIXME : does not work on Qt3
[754]2701void G4OpenGLQtViewer::processEncodeStdout()
[750]2702{
2703#if QT_VERSION > 0x040000
[751]2704  QString tmp = fProcess->readAllStandardOutput ().data();
[754]2705  int start = tmp.lastIndexOf("ESTIMATED TIME");
2706  tmp = tmp.mid(start,tmp.indexOf("\n",start)-start);
[750]2707#else
[751]2708  QString tmp = fProcess->readStdout ().data();
[754]2709  int start = tmp.findRev("ESTIMATED TIME");
2710  tmp = tmp.mid(start,tmp.find("\n",start)-start);
[750]2711#endif
[751]2712  setRecordingInfos(tmp);
[750]2713}
2714
2715
[754]2716void G4OpenGLQtViewer::processEncodeFinished()
[750]2717{
2718
[751]2719  QString txt = "";
[754]2720  txt = getProcessErrorMsg();
2721  if (txt == "") {
[750]2722    setRecordingStatus(SUCCESS);
2723  } else {
2724    setRecordingStatus(FAILED);
2725  }
[751]2726  setRecordingInfos(txt+removeTempFolder());
[750]2727}
2728
[754]2729
2730void G4OpenGLQtViewer::processLookForFinished()
2731 {
2732
2733  QString txt = getProcessErrorMsg();
2734  if (txt != "") {
2735    fEncoderPath = "";
2736  } else {
2737#if QT_VERSION > 0x040000
2738    fEncoderPath = QString(fProcess->readAllStandardOutput ().data()).trimmed();
2739#else
2740    fEncoderPath = QString(fProcess->readStdout ().data()).simplifyWhiteSpace();
2741#endif
2742    // if not found, return "not found"
2743    if (fEncoderPath.contains(" ")) {
2744      fEncoderPath = "";
2745    } else if (!fEncoderPath.contains("mpeg_encode")) {
2746      fEncoderPath = "";
2747    }
2748    setEncoderPath(fEncoderPath);
2749  }
2750  // init temp folder
2751#if QT_VERSION > 0x040000
2752  setTempFolderPath(QDir::temp ().absolutePath ());
2753#else
2754  // Let's have a try
2755  setTempFolderPath("/tmp/");
2756#endif
2757}
2758
2759
2760QString G4OpenGLQtViewer::getProcessErrorMsg()
[756]2761{
2762  QString txt = "";
[754]2763#if QT_VERSION < 0x040000
[756]2764  if (!fProcess->normalExit ()) {
2765    txt = "Exist status "+ fProcess->exitStatus ();
2766  }
[754]2767#else
[757]2768  if (fProcess->exitCode() != 0) {
[778]2769    switch (fProcess->error()) {
2770    case QProcess::FailedToStart:
2771      txt = "The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program.\n";
2772      break;
2773    case QProcess::Crashed:
2774      txt = "The process crashed some time after starting successfully.\n";
2775      break;
2776    case QProcess::Timedout:
2777      txt = "The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again.\n";
2778      break;
2779    case QProcess::WriteError:
2780      txt = "An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.\n";
2781      break;
2782    case QProcess::ReadError:
2783      txt = "An error occurred when attempting to read from the process. For example, the process may not be running.\n";
2784      break;
2785    case QProcess::UnknownError:
2786      txt = "An unknown error occurred. This is the default return value of error().\n";
2787      break;
[756]2788    }
2789  }
[754]2790#endif
2791   return txt;
2792}
2793
[588]2794/*
[696]2795 
[588]2796void MultiLayer::exportToSVG(const QString& fname)
2797{
2798  QPicture picture;
2799  QPainter p(&picture);
2800  for (int i=0;i<(int)graphsList->count();i++)
2801    {
2802      Graph *gr=(Graph *)graphsList->at(i);
2803      Plot *myPlot= (Plot *)gr->plotWidget();
2804     
2805      QPoint pos=gr->pos();
2806     
2807      int width=int(myPlot->frameGeometry().width());
2808      int height=int(myPlot->frameGeometry().height());
2809     
2810      myPlot->print(&p, QRect(pos,QSize(width,height)));
2811    }
2812 
2813  p.end();
2814  picture.save(fname, "svg");
2815}
2816*/
[707]2817#endif
Note: See TracBrowser for help on using the repository browser.