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

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

r844@wl-72126: garnier | 2008-05-20 19:09:54 +0200
retour a la case depart

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