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

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

video avec de vector:en test

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