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

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

test

  • Property svn:mime-type set to text/cpp
File size: 70.5 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  nomFich += "."+QString("ppm");
1288#if QT_VERSION < 0x040000
1289  res = image.save(nomFich,"ppm");
1290#else
1291  res = image.save(nomFich,0);
1292#endif
1293  if (res == false) {
1294#if QT_VERSION < 0x040000
1295    G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1296#else
1297    G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1298#endif
1299    fRecordFrames = false;
1300  }
1301
1302#if QT_VERSION < 0x040000
1303  printf("SaveForVideo ^^^^^^^^^^^^^^^^^^^^^^^^ %s\n",nomFich.ascii());
1304#else
1305  printf("SaveForVideo ^^^^^^^^^^^^^^^^^^^^^^^^ %s\n",nomFich.toStdString().c_str());
1306#endif
1307}
1308
1309
1310
1311void G4OpenGLQtViewer::actionCreateEPS() {
1312  QString filters;
1313#if QT_VERSION < 0x040000
1314  QStrList listFormat=QImageIO::outputFormats();
1315  char *tmp=listFormat.first();
1316  while (tmp!=0) {
1317    filters += QString(tmp) + ";;";
1318    tmp=listFormat.next();
1319  }
1320#else
1321  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
1322  for (int i = 0; i < formats.size(); ++i) {
1323    filters +=formats.at(i) + ";;";
1324  }
1325#endif
1326  filters += "eps;;";
1327  filters += "ps;;";
1328  filters += "pdf";
1329  QString* selectedFormat = new QString();
1330#if QT_VERSION < 0x040000
1331  QString nomFich =  QFileDialog::getSaveFileName ( ".",
1332                                                    filters,
1333                                                    GLWindow,
1334                                                    "Save file dialog",
1335                                                    tr("Save as ..."),
1336                                                    selectedFormat );
1337#else
1338  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
1339                                                    tr("Save as ..."),
1340                                                    ".",
1341                                                    filters,
1342                                                    selectedFormat );
1343#endif
1344  // bmp jpg jpeg png ppm xbm xpm
1345  if (nomFich == "") {
1346    return;
1347  }
1348#if QT_VERSION < 0x040000
1349  nomFich += "."+QString(selectedFormat->ascii());
1350  QString format = selectedFormat->lower();
1351#else
1352  nomFich += "."+QString(selectedFormat->toStdString().c_str());
1353  QString format = selectedFormat->toLower();
1354#endif
1355  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,format,fWindow->height(),fWindow->width());
1356  if(  exportDialog->exec()) {
1357
1358    QImage image;
1359    bool res = false;
1360    if ((exportDialog->getWidth() !=fWindow->width()) ||
1361        (exportDialog->getHeight() !=fWindow->height())) {
1362      if (format != QString("eps")) {
1363      G4cerr << "Export->Change Size : This function is not implemented, to export in another size, please resize your frame to what you need\n" << G4endl;
1364     
1365      //    rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1366      //      QGLWidget* glResized = fWindow;
1367
1368      // FIXME :
1369      // L.Garnier : I've try to implement change size function, but the problem is
1370      // the renderPixmap function call the QGLWidget to resize and it doesn't draw
1371      // the content of this widget... It only draw the background.
1372
1373      //      fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2,true );
1374
1375      //      QPixmap pixmap = fWindow->renderPixmap ();
1376     
1377      //      image = pixmap->toImage();
1378      //      glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1379      //      image = glResized->grabFrameBuffer();
1380      }     
1381    } else {
1382      image = fWindow->grabFrameBuffer();
1383    }   
1384    if (format == QString("eps")) {
1385      if (exportDialog->getVectorEPS()) {
1386        res = generateVectorEPS(nomFich,exportDialog->getWidth(),exportDialog->getHeight(),image);
1387      } else {
1388        res = generateEPS(nomFich,exportDialog->getNbColor(),image);
1389      }
1390    } else if ((format == "ps") || (format == "pdf")) {
1391      res = generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1392    } else if ((format == "tif") ||
1393               (format == "tiff") ||
1394               (format == "jpg") ||
1395               (format == "jpeg") ||
1396               (format == "png") ||
1397               (format == "pbm") ||
1398               (format == "pgm") ||
1399               (format == "ppm") ||
1400               (format == "bmp") ||
1401               (format == "xbm") ||
1402               (format == "xpm")) {
1403#if QT_VERSION < 0x040000
1404      res = image.save(nomFich,selectedFormat->ascii(),exportDialog->getSliderValue());
1405#else
1406      res = image.save(nomFich,0,exportDialog->getSliderValue());
1407#endif
1408    } else {
1409      G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1410    }
1411    if (res == false) {
1412#if QT_VERSION < 0x040000
1413      G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1414#else
1415      G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1416#endif
1417    } else {
1418#if QT_VERSION < 0x040000
1419      G4cout << "File "<<nomFich.ascii()<<" has been saved \n" << G4endl;
1420#else
1421      G4cout << "File "<<nomFich.toStdString().c_str()<<" has been saved \n" << G4endl;
1422#endif
1423    }
1424   
1425  } else { // cancel selected
1426    return;
1427  }
1428 
1429#ifdef GEANT4_QT_DEBUG
1430  printf("G4OpenGLQtViewer::actionCreateEPS() \n");
1431#endif
1432}
1433
1434/*
1435// 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
1436
1437void Graph::exportToSVG(const QString& fname)
1438{
1439  // enable workaround for Qt3 misalignments
1440  QwtPainter::setSVGMode(true);
1441  QPicture picture;
1442  QPainter p(&picture);
1443  d_plot->print(&p, d_plot->rect());
1444  p.end();
1445
1446  picture.save(fname, "svg");
1447}
1448*/
1449
1450
1451
1452
1453/**
1454   Save the current mouse press point
1455   @param p mouse click point
1456*/
1457#if QT_VERSION < 0x040000
1458void G4OpenGLQtViewer::G4MousePressEvent(QPoint p,Qt::ButtonState mButtons)
1459#else
1460void G4OpenGLQtViewer::G4MousePressEvent(QPoint p,Qt::MouseButtons mButtons)
1461#endif
1462{
1463  fAutoMove = false; // stop automove
1464  fLastPos = p;
1465  if (fMouseAction == STYLE2){  // pick
1466    Pick(p.x(),p.y());
1467  }
1468}
1469
1470
1471/**
1472   @param pos_x mouse x position
1473   @param pos_y mouse y position
1474   @param mButtons mouse button active
1475   @param mAutoMove true: apply this move till another evnt came, false :one time move
1476*/
1477
1478#if QT_VERSION < 0x040000
1479void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::ButtonState mButtons,bool mAutoMove)
1480#else
1481  void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons,bool mAutoMove)
1482#endif
1483{
1484  fAutoMove = mAutoMove;
1485#ifdef GEANT4_QT_DEBUG
1486  printf("                   mouse move event %d %d\n",pos_x,pos_y);
1487#endif
1488
1489  if (!fAutoMove) {  // keep old delta if automove
1490    fDeltaPosX = fLastPos.x() - pos_x;
1491    fDeltaPosY = fLastPos.y() - pos_y;
1492  }
1493
1494  if ((fDeltaPosX == 0) && (fDeltaPosY == 0)) {
1495    fAutoMove = false;
1496  }
1497
1498  if (fMouseAction == STYLE1) {  // rotate
1499    if (mButtons & Qt::LeftButton) {
1500      rotateScene(fDeltaPosX,fDeltaPosY,fAutoMove);
1501    }
1502  } else if (fMouseAction == STYLE2) {  // move
1503    if (mButtons & Qt::LeftButton) {
1504      if (fAutoMove) {
1505        while (fAutoMove) {
1506          moveScene(-fDeltaPosX,-fDeltaPosY,0,true,true);
1507          ((QApplication*)G4Qt::getInstance ())->processEvents();
1508        }
1509      } else {
1510        moveScene(-fDeltaPosX,-fDeltaPosY,0,true,false);
1511      }
1512    }
1513  }
1514  fLastPos = QPoint(pos_x, pos_y);
1515#ifdef GEANT4_QT_DEBUG
1516  if (mButtons & Qt::LeftButton) {
1517    printf("                  end of  mouse move event %d %d delta:%f %f\n",pos_x,pos_y,fDeltaPosX,fDeltaPosY);
1518  } else {
1519    printf("                  end of  mouse move event %d %d delta:%f %f --------------NO BUTTON\n",pos_x,pos_y,fDeltaPosX,fDeltaPosY);
1520  }
1521#endif
1522}
1523
1524
1525/**
1526   Move the scene of dx, dy, dz values.
1527   @param dx delta mouse x position
1528   @param dy delta mouse y position
1529   @param mouseMove : true if even comes froma mouse move, false if even comes from key action
1530*/
1531
1532void G4OpenGLQtViewer::moveScene(G4double dx,G4double dy, G4double dz,bool mouseMove,bool mAutoMove)
1533{
1534  if( mAutoMove == false) {
1535    fAutoMove = true;
1536  }
1537  G4double coefTrans = 0;
1538  GLdouble coefDepth = 0;
1539  while (fAutoMove) {
1540    if( mAutoMove == false) {
1541      fAutoMove = false;
1542    }
1543    if(mouseMove) {
1544      coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_x);
1545      if (WinSize_y <WinSize_x) {
1546        coefTrans = ((G4double)getSceneNearWidth())/((G4double)WinSize_y);
1547      }
1548    } else {
1549      coefTrans = getSceneNearWidth()*fDeltaSceneTranslation;
1550      coefDepth = getSceneDepth()*fDeltaDepth;
1551    }
1552    fVP.IncrementPan(-dx*coefTrans,dy*coefTrans,dz*coefDepth);
1553   
1554    updateQWidget();
1555    ((QApplication*)G4Qt::getInstance ())->processEvents();
1556  }
1557}
1558
1559
1560/**
1561   @param dx delta mouse x position
1562   @param dy delta mouse y position
1563*/
1564
1565void G4OpenGLQtViewer::rotateScene(G4double dx, G4double dy,bool mAutoRotate)
1566{
1567  if( mAutoRotate == false) {
1568    fAutoMove = true;
1569  }
1570  dx = dx * fDeltaRotation;
1571  dy = dy * fDeltaRotation;
1572  G4Vector3D vp;
1573  G4Vector3D up;
1574 
1575  G4Vector3D xprime;
1576  G4Vector3D yprime;
1577  G4Vector3D zprime;
1578 
1579  G4double delta_alpha;
1580  G4double delta_theta;
1581 
1582  G4Vector3D new_vp;
1583  G4Vector3D new_up;
1584 
1585  G4double cosalpha;
1586  G4double sinalpha;
1587 
1588  G4Vector3D a1;
1589  G4Vector3D a2;
1590  G4Vector3D delta;
1591  G4Vector3D viewPoint;
1592
1593  while (fAutoMove) {
1594    if( mAutoRotate == false) {
1595      fAutoMove = false;
1596    }
1597   
1598    //phi spin stuff here
1599 
1600    vp = fVP.GetViewpointDirection ().unit ();
1601    up = fVP.GetUpVector ().unit ();
1602 
1603    yprime = (up.cross(vp)).unit();
1604    zprime = (vp.cross(yprime)).unit();
1605 
1606    if (fVP.GetLightsMoveWithCamera()) {
1607      delta_alpha = dy;
1608      delta_theta = -dx;
1609    } else {
1610      delta_alpha = -dy;
1611      delta_theta = dx;
1612    }   
1613 
1614    delta_alpha *= deg;
1615    delta_theta *= deg;
1616 
1617    new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1618 
1619    if (fVP.GetLightsMoveWithCamera()) {
1620      new_up = (new_vp.cross(yprime)).unit();
1621      fVP.SetUpVector(new_up);
1622    } else {
1623      new_up = up;
1624    }
1625    ////////////////
1626    // Rotates by fixed azimuthal angle delta_theta.
1627   
1628    cosalpha = new_up.dot (new_vp.unit());
1629    sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1630    yprime = (new_up.cross (new_vp.unit())).unit ();
1631    xprime = yprime.cross (new_up);
1632    // Projection of vp on plane perpendicular to up...
1633    a1 = sinalpha * xprime;
1634    // Required new projection...
1635    a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1636    // Required Increment vector...
1637    delta = a2 - a1;
1638    // So new viewpoint is...
1639    viewPoint = new_vp.unit() + delta;
1640   
1641    fVP.SetViewAndLights (viewPoint);
1642    updateQWidget();
1643   
1644    ((QApplication*)G4Qt::getInstance ())->processEvents();
1645  }
1646}
1647
1648/** This is the benning of a rescale function. It does nothing for the moment
1649    @param aWidth : new width
1650    @param aHeight : new height
1651*/
1652void G4OpenGLQtViewer::rescaleImage(
1653 int aWidth
1654,int aHeight
1655){
1656#ifdef GEANT4_QT_DEBUG
1657  printf("should rescale \n");
1658#endif
1659  GLfloat* feedback_buffer;
1660  GLint returned;
1661  FILE* file;
1662 
1663//   feedback_buffer = new GLfloat[size];
1664//   glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1665//   glRenderMode (GL_FEEDBACK);
1666 
1667//   glViewport (0, 0, aWidth, aHeight);
1668//   DrawView();
1669//   returned = glRenderMode (GL_RENDER);
1670
1671}
1672
1673/**
1674   Generate Vectorial Encapsulated Postscript form image
1675   @param aFilename : name of file
1676   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1677   @param aImage : Image to print
1678*/
1679bool G4OpenGLQtViewer::generateVectorEPS (
1680 QString aFilename
1681,int aWidth
1682,int aHeight
1683,QImage aImage
1684)
1685{
1686  // Print vectored PostScript
1687 
1688  G4int size = 5000000;
1689
1690  GLfloat* feedback_buffer;
1691  GLint returned;
1692  FILE* file;
1693 
1694  feedback_buffer = new GLfloat[size];
1695  glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1696  glRenderMode (GL_FEEDBACK);
1697 
1698  int side = aWidth;
1699  if (aHeight < aWidth) side = aHeight;
1700  glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
1701  DrawView();
1702
1703  returned = glRenderMode (GL_RENDER);
1704 
1705 
1706#if QT_VERSION < 0x040000
1707  file = fopen (aFilename.ascii(), "w");
1708#else
1709  file = fopen (aFilename.toStdString().c_str(), "w");
1710#endif
1711  if (file) {
1712    spewWireframeEPS (file, returned, feedback_buffer, "rendereps");
1713  } else {
1714#if QT_VERSION < 0x040000
1715    G4cerr << "Could not open "<< aFilename.ascii()<<"\n" << G4endl;
1716#else
1717    G4cerr << "Could not open "<< aFilename.toStdString().c_str()<<"\n" << G4endl;
1718#endif
1719  }
1720 
1721  delete[] feedback_buffer;
1722
1723  return true;
1724}
1725
1726/**
1727   Generate Encapsulated Postscript form image
1728   @param aFilename : name of file
1729   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1730   @param aImage : Image to print
1731*/
1732bool G4OpenGLQtViewer::generateEPS (
1733 QString aFilename
1734,int aInColor
1735,QImage aImage
1736)
1737{
1738  // FIXME
1739#ifdef GEANT4_QT_DEBUG
1740  printf("saving EPS\n");
1741#endif
1742
1743  FILE* fp;
1744
1745  if (aImage.bits () == NULL)
1746    return false;
1747 
1748#if QT_VERSION < 0x040000
1749  fp = fopen (aFilename.ascii(), "w");
1750#else
1751  fp = fopen (aFilename.toStdString().c_str(), "w");
1752#endif
1753  if (fp == NULL) {
1754    return false;
1755  }
1756 
1757  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
1758#if QT_VERSION < 0x040000
1759  fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1760#else
1761  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
1762#endif
1763  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
1764  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
1765  fprintf (fp, "%%%%EndComments\n");
1766  fprintf (fp, "gsave\n");
1767  fprintf (fp, "/bwproc {\n");
1768  fprintf (fp, "    rgbproc\n");
1769  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
1770  fprintf (fp, "    5 -1 roll {\n");
1771  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
1772  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1773  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1774  fprintf (fp, "    { 2 1 roll } ifelse\n");
1775  fprintf (fp, "    }forall\n");
1776  fprintf (fp, "    pop pop pop\n");
1777  fprintf (fp, "} def\n");
1778  fprintf (fp, "systemdict /colorimage known not {\n");
1779  fprintf (fp, "   /colorimage {\n");
1780  fprintf (fp, "       pop\n");
1781  fprintf (fp, "       pop\n");
1782  fprintf (fp, "       /rgbproc exch def\n");
1783  fprintf (fp, "       { bwproc } image\n");
1784  fprintf (fp, "   }  def\n");
1785  fprintf (fp, "} if\n");
1786  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1787  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1788  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1789  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
1790  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1791  fprintf (fp, "false %d\n", aInColor);
1792  fprintf (fp, "colorimage\n");
1793 
1794
1795  int width = aImage.width();
1796  int height = aImage.height();
1797  int depth = aImage.depth();
1798  int size = width*height;
1799 
1800  if (depth == 1)
1801    size = (width+7)/8*height;
1802  else if (aInColor == 1)
1803    size = size*3;
1804 
1805  int i = 0;
1806  //  if ( aInColor ==1 ) {
1807  // FIXME : L. Garnier. For the moment 10 dec 2007, I could not find a way
1808  // to save correctly grayscale Image. I mean that color or grayscale image
1809  // have the same file save size !
1810 
1811  /* } else*/ if (depth == 8) {
1812    for(int y=height-1; y >=0 ; y--) {
1813      const uchar * s = aImage.scanLine(y);
1814      for(int x=0; x <width; x++) {
1815        QRgb rgb = aImage.color(s[x]);
1816        if (aInColor == 1) {
1817          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1818          i++;
1819        } else {
1820          fprintf (fp, " %02hx %02hx %02hx",
1821                   (unsigned char) qRed(rgb),
1822                   (unsigned char) qGreen(rgb),
1823                   (unsigned char) qBlue(rgb));
1824          i += 3;
1825        }
1826      }
1827      fprintf (fp, "\n");
1828    }
1829  } else {
1830#if QT_VERSION < 0x040000
1831    bool alpha = aImage.hasAlphaBuffer();
1832#else
1833    bool alpha = aImage.hasAlphaChannel();
1834    for(int y=height-1; y >=0 ; y--) {
1835      QRgb * s = (QRgb*)(aImage.scanLine(y));
1836      for(int x=0; x <width; x++) {
1837        QRgb rgb = (*s++);
1838        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1839          rgb = qRgb(0xff, 0xff, 0xff);
1840        if (aInColor == 1) {
1841          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1842          i++;
1843        } else {
1844          fprintf (fp, " %02hx %02hx %02hx",
1845                   (unsigned char) qRed(rgb),
1846                   (unsigned char) qGreen(rgb),
1847                   (unsigned char) qBlue(rgb));
1848          i += 3;
1849        }
1850      }
1851      fprintf (fp, "\n");
1852    }
1853#endif
1854
1855  }
1856
1857  fprintf (fp, "grestore\n");
1858  fprintf (fp, "showpage\n");
1859  fclose (fp);
1860
1861  return true;
1862}
1863/**
1864   Generate Postscript or PDF form image
1865   @param aFilename : name of file
1866   @param aInColor : numbers of colors : 1->BW 2->RGB
1867   @param aImage : Image to print
1868*/
1869bool G4OpenGLQtViewer::generatePS_PDF (
1870 QString aFilename
1871,int aInColor
1872,QImage aImage
1873)
1874{
1875
1876#if QT_VERSION < 0x040000
1877#ifdef Q_WS_MAC || Q_WS_X11
1878  QPrinter printer;
1879  //  printer.setPageSize(pageSize);
1880  if (aInColor == 1) {
1881    printer.setColorMode(QPrinter::GrayScale);
1882  } else {
1883    printer.setColorMode(QPrinter::Color);
1884  }
1885
1886  /* FIXME : I don't know which format it will save...
1887     if (aFilename.endsWith(".ps")) {
1888     printer.setOutputFormat(QPrinter::PostScriptFormat);
1889     } else {
1890     printer.setOutputFormat(QPrinter::PdfFormat);
1891     }
1892  */
1893  printer.setOutputFileName(aFilename);
1894  //  printer.setFullPage ( true);
1895  QPainter paint(&printer);
1896  paint.drawImage (0,0,aImage );
1897  paint.end();
1898#else
1899  G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4\n" << G4endl;
1900#endif
1901#else
1902  QPrinter printer;
1903  //  printer.setPageSize(pageSize);
1904
1905  // FIXME : L. Garnier 4/12/07
1906  // This is not working, it does nothing. Image is staying in color mode
1907  // So I have desactivate the B/W button in GUI
1908  if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1909#if QT_VERSION < 0x040000
1910    aImage = aImage.convertDepth(1,Qt::MonoOnly);
1911#else
1912    aImage = aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1913#endif
1914  }
1915
1916
1917  if (aFilename.endsWith(".ps")) {
1918#if QT_VERSION > 0x040200
1919    printer.setOutputFormat(QPrinter::PostScriptFormat);
1920#endif
1921  } else {
1922#if QT_VERSION > 0x040100
1923    printer.setOutputFormat(QPrinter::PdfFormat);
1924#endif
1925  }
1926#if QT_VERSION > 0x040100
1927  printer.setOutputFileName(aFilename);
1928#endif
1929  //  printer.setFullPage ( true);
1930  QPainter paint(&printer);
1931  paint.drawImage (0,0,aImage);
1932  paint.end();
1933#endif
1934  return true;
1935}
1936
1937
1938
1939void G4OpenGLQtViewer::G4keyPressEvent (QKeyEvent * event)
1940{
1941  if (fHoldKeyEvent)
1942    return;
1943
1944  fHoldKeyEvent = true;
1945
1946#if QT_VERSION < 0x040000
1947  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::AltButton )) { // go backward
1948#else
1949  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::AltModifier )) { // go backward
1950#endif
1951   
1952    moveScene(0,0,1,false);
1953  }
1954#if QT_VERSION < 0x040000
1955  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::AltButton)) { // go forward
1956#else
1957  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::AltModifier)) { // go forward
1958#endif
1959    moveScene(0,0,-1,false);
1960  }
1961#if QT_VERSION < 0x040000
1962  if ((event->key() == Qt::Key_Down) && (event->state() & Qt::ShiftButton)) { // rotate phi
1963#else
1964  if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1965#endif
1966    rotateScene(0,-1);
1967  }
1968#if QT_VERSION < 0x040000
1969  else if ((event->key() == Qt::Key_Up) && (event->state() & Qt::ShiftButton)) { // rotate phi
1970#else
1971  else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1972#endif
1973    rotateScene(0,1);
1974  }
1975#if QT_VERSION < 0x040000
1976  if ((event->key() == Qt::Key_Left) && (event->state() & Qt::ShiftButton)) { // rotate theta
1977#else
1978  if ((event->key() == Qt::Key_Left) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1979#endif
1980    rotateScene(1,0);
1981  }
1982#if QT_VERSION < 0x040000
1983  else if ((event->key() == Qt::Key_Right) && (event->state() & Qt::ShiftButton)) { // rotate theta
1984#else
1985  else if ((event->key() == Qt::Key_Right) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1986#endif
1987    rotateScene(-1,0);
1988  }
1989
1990#if QT_VERSION < 0x040000
1991  if ((event->state() & Qt::AltButton)) {
1992#else
1993  if ((event->modifiers() & Qt::AltModifier)) {
1994#endif
1995    if (event->key() == Qt::Key_Plus) {
1996      fDeltaPosX =fDeltaPosX*(1+fDeltaMove);
1997      fDeltaPosY =fDeltaPosY*(1+fDeltaMove);
1998    }
1999    else if (event->key() == Qt::Key_Minus) {
2000      fDeltaPosX =fDeltaPosX*(1-fDeltaMove);
2001      fDeltaPosY =fDeltaPosY*(1-fDeltaMove);
2002    }
2003  } else {
2004    if (event->key() == Qt::Key_Plus) {
2005      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1+fDeltaZoom));
2006      updateQWidget();
2007    }
2008    else if (event->key() == Qt::Key_Minus) {
2009      fVP.SetZoomFactor(fVP.GetZoomFactor()*(1-fDeltaZoom));
2010      updateQWidget();
2011    }
2012  }
2013 
2014 
2015  if (event->key() == Qt::Key_Escape) { // escaped from full screen
2016#if QT_VERSION >= 0x030200
2017    if (GLWindow->isFullScreen()) {
2018#if QT_VERSION < 0x040000
2019      fFullScreenOn->activated();
2020#else
2021      fFullScreenOn->trigger();
2022#endif
2023    }
2024#endif
2025  }
2026
2027  if ((event->key() == Qt::Key_Return) || (event->key() == Qt::Key_Enter)){ // end of video
2028    fRecordFrames = false;
2029    G4cout << "Recording Stop... \n" << G4endl;
2030    G4cout << "Saving /temp/output.mpg \n" << G4endl;
2031    saveVideo("/temp/output.mpg");
2032  }
2033  if (event->key() == Qt::Key_Space){ // start/pause of video
2034    fRecordFrames = !fRecordFrames;
2035    if (fRecordFrames) {
2036      G4cout << "Recording Start... \n" << G4endl;
2037    } else {
2038      G4cout << "Recording Pause... \n" << G4endl;
2039    }
2040  }
2041
2042  // with no modifiers
2043#if QT_VERSION < 0x040000
2044  if (event->state() == Qt::NoButton) {
2045#else
2046  if (event->modifiers() == Qt::NoModifier) {
2047#endif
2048    if (event->key() == Qt::Key_Down) { // go down
2049      moveScene(0,-1,0,false);
2050    }
2051    else if (event->key() == Qt::Key_Up) {  // go up
2052      moveScene(0,1,0,false);
2053    }
2054    if (event->key() == Qt::Key_Left) { // go left
2055      moveScene(-1,0,0,false);
2056    }
2057    else if (event->key() == Qt::Key_Right) { // go right
2058      moveScene(1,0,0,false);
2059    }
2060  }
2061  fHoldKeyEvent = false;
2062}
2063
2064
2065bool G4OpenGLQtViewer::hasPendingEvents () {
2066  return ((QApplication*)G4Qt::getInstance ())->hasPendingEvents ();
2067}
2068
2069bool G4OpenGLQtViewer::saveVideo (QString outputName) {
2070
2071  // save the parameter file
2072  FILE* fp;
2073  fp = fopen ("/temp/parameter_file.par", "w");
2074
2075  if (fp == NULL) {
2076    return false;
2077  }
2078
2079  fprintf (fp,"# parameter file template with lots of comments to assist you\n");
2080  fprintf (fp,"#\n");
2081  fprintf (fp,"# you can use this as a template, copying it to a separate file then modifying\n");
2082  fprintf (fp,"# the copy\n");
2083  fprintf (fp,"#\n");
2084  fprintf (fp,"#\n");
2085  fprintf (fp,"# any line beginning with '#' is a comment\n");
2086  fprintf (fp,"#\n");
2087  fprintf (fp,"# no line should be longer than 255 characters\n");
2088  fprintf (fp,"#\n");
2089  fprintf (fp,"#\n");
2090  fprintf (fp,"# general format of each line is:\n");
2091  fprintf (fp,"#          \n");
2092  fprintf (fp,"#\n");
2093  fprintf (fp,"# lines can generally be in any order\n");
2094  fprintf (fp,"#\n");
2095  fprintf (fp,"# an exception is the option 'INPUT' which must be followed by input\n");
2096  fprintf (fp,"# files in the order in which they must appear, followed by 'END_INPUT'\n");
2097  fprintf (fp,"#\n");
2098  fprintf (fp,"# Also, if you use the `command` method of generating input file names,\n");
2099  fprintf (fp,"# the command will only be executed in the INPUT_DIR if INPUT_DIR preceeds\n");
2100  fprintf (fp,"# the INPUT parameter.\n");
2101  fprintf (fp,"#\n");
2102  fprintf (fp,"#  MUST be in UPPER CASE\n");
2103  fprintf (fp,"#\n");
2104  fprintf (fp,"\n");
2105  fprintf (fp,"# Pattern affects speed, quality and compression. See the User's Guide\n");
2106  fprintf (fp,"# for more info.\n");
2107  fprintf (fp,"\n");
2108  fprintf (fp,"PATTERN          IBBPBBPBBPBBPBBP\n");
2109#if QT_VERSION < 0x040000
2110  fprintf (fp,"OUTPUT           %s\n",outputName.ascii());
2111#else
2112  fprintf (fp,"OUTPUT           %s\n",outputName.toStdString().c_str());
2113#endif
2114  fprintf (fp,"\n");
2115  fprintf (fp,"# mpeg_encode really only accepts 3 different file formats, but using a\n");
2116  fprintf (fp,"# conversion statement it can effectively handle ANY file format\n");
2117  fprintf (fp,"#\n");
2118  fprintf (fp,"# You must specify the type of the input files.  The choices are:\n");
2119  fprintf (fp,"#    YUV, PPM, JMOVIE, Y, JPEG, PNM\n");
2120  fprintf (fp,"#        (must be upper case)\n");
2121  fprintf (fp,"#\n");
2122  fprintf (fp,"BASE_FILE_FORMAT PPM\n");
2123  fprintf (fp,"\n");
2124  fprintf (fp,"#\n");
2125  fprintf (fp,"# if YUV format (or using parallel version), must provide width and height\n");
2126  fprintf (fp,"# YUV_SIZE       widthxheight\n");
2127  fprintf (fp,"# this option is ignored if BASE_FILE_FORMAT is not YUV and you're running\n");
2128  fprintf (fp,"# on just one machine\n");
2129  fprintf (fp,"#\n");
2130  fprintf (fp,"YUV_SIZE 352x240\n");
2131  fprintf (fp,"\n");
2132  fprintf (fp,"# If you are using YUV, there are different supported file formats.\n");
2133  fprintf (fp,"# EYUV or UCB are the same as previous versions of this encoder.\n");
2134  fprintf (fp,"# (All the Y's, then U's then V's, in 4:2:0 subsampling.)\n");
2135  fprintf (fp,"# Other formats, such as Abekas, Phillips, or a general format are\n");
2136  fprintf (fp,"# permissible, the general format is a string of Y's, U's, and V's\n");
2137  fprintf (fp,"# to specify the file order.\n");
2138  fprintf (fp,"\n");
2139  fprintf (fp,"INPUT_FORMAT UCB\n");
2140  fprintf (fp,"\n");
2141  fprintf (fp,"# the conversion statement\n");
2142  fprintf (fp,"#\n");
2143  fprintf (fp,"# Each occurrence of '*' will be replaced by the input file\n");
2144  fprintf (fp,"#\n");
2145  fprintf (fp,"# e.g., if you have a bunch of GIF files, then this might be:\n");
2146  fprintf (fp,"#        INPUT_CONVERT   giftoppm *\n");
2147  fprintf (fp,"#\n");
2148  fprintf (fp,"# e.g., if you have a bunch of files like a.Y a.U a.V, etc., then:\n");
2149  fprintf (fp,"#        INPUT_CONVERT   cat *.Y *.U *.V\n");
2150  fprintf (fp,"#\n");
2151  fprintf (fp,"# e.g., if you are grabbing from laser disc you might have something like\n");
2152  fprintf (fp,"#        INPUT_CONVERT   goto frame *; grabppm\n");
2153  fprintf (fp,"# 'INPUT_CONVERT *' means the files are already in the base file format\n");
2154  fprintf (fp,"#\n");
2155  fprintf (fp,"INPUT_CONVERT    * \n");
2156  fprintf (fp,"\n");
2157  fprintf (fp,"# number of frames in a GOP.\n");
2158  fprintf (fp,"#\n");
2159  fprintf (fp,"# since each GOP must have at least one I-frame, the encoder will find the\n");
2160  fprintf (fp,"# the first I-frame after GOP_SIZE frames to start the next GOP\n");
2161  fprintf (fp,"#\n");
2162  fprintf (fp,"# later, will add more flexible GOP signalling\n");
2163  fprintf (fp,"#\n");
2164  fprintf (fp,"GOP_SIZE 16\n");
2165  fprintf (fp,"\n");
2166  fprintf (fp,"# number of slices in a frame\n");
2167  fprintf (fp,"#\n");
2168  fprintf (fp,"# 1 is a good number.  another possibility is the number of macroblock rows\n");
2169  fprintf (fp,"# (which is the height divided by 16)\n");
2170  fprintf (fp,"#\n");
2171  fprintf (fp,"SLICES_PER_FRAME 1\n");
2172  fprintf (fp,"\n");
2173  fprintf (fp,"# directory to get all input files from (makes this file easier to read)\n");
2174  fprintf (fp,"INPUT_DIR        /temp\n");
2175  fprintf (fp,"\n");
2176  fprintf (fp,"# There are a bunch of ways to specify the input files.\n");
2177  fprintf (fp,"# from a simple one-per-line listing, to the following \n");
2178  fprintf (fp,"# way of numbering them.  See the manual for more information.\n");
2179  fprintf (fp,"INPUT\n");
2180  fprintf (fp,"# '*' is replaced by the numbers 01, 02, 03, 04\n");
2181  fprintf (fp,"# if I instead do [01-11], it would be 01, 02, ..., 09, 10, 11\n");
2182  fprintf (fp,"# if I instead do [1-11], it would be 1, 2, 3, ..., 9, 10, 11\n");
2183  fprintf (fp,"# if I instead do [1-11+3], it would be 1, 4, 7, 10\n");
2184  fprintf (fp,"# the program assumes none of your input files has a name ending in ']'\n");
2185  fprintf (fp,"# if you do, too bad!!!\n");
2186  fprintf (fp,"#\n");
2187  fprintf (fp,"#\n");
2188  fprintf (fp,"Test*.ppm        [0-%d]\n",fRecordFrameNumber);
2189  fprintf (fp,"# can have more files here if you want...there is no limit on the number\n");
2190  fprintf (fp,"# of files\n");
2191  fprintf (fp,"END_INPUT\n");
2192  fprintf (fp,"\n");
2193  fprintf (fp,"\n");
2194  fprintf (fp,"\n");
2195  fprintf (fp,"# Many of the remaining options have to do with the motion search and qscale\n");
2196  fprintf (fp,"\n");
2197  fprintf (fp,"# FULL or HALF -- must be upper case\n");
2198  fprintf (fp,"# Should be FULL for computer generated images\n");
2199  fprintf (fp,"PIXEL            FULL\n");
2200  fprintf (fp,"\n");
2201  fprintf (fp,"# means +/- this many pixels for both P and B frame searches\n");
2202  fprintf (fp,"# specify two numbers if you wish to serc different ranges in the two.\n");
2203  fprintf (fp,"RANGE            10\n");
2204  fprintf (fp,"\n");
2205  fprintf (fp,"# The two search algorithm parameters below mostly affect speed,\n");
2206  fprintf (fp,"# with some affect on compression and almost none on quality.\n");
2207  fprintf (fp,"\n");
2208  fprintf (fp,"# this must be one of {EXHAUSTIVE, SUBSAMPLE, LOGARITHMIC}\n");
2209  fprintf (fp,"PSEARCH_ALG      LOGARITHMIC\n");
2210  fprintf (fp,"\n");
2211  fprintf (fp,"# this must be one of {SIMPLE, CROSS2, EXHAUSTIVE}\n");
2212  fprintf (fp,"#\n");
2213  fprintf (fp,"# note that EXHAUSTIVE is really, really, really slow\n");
2214  fprintf (fp,"#\n");
2215  fprintf (fp,"BSEARCH_ALG      SIMPLE\n");
2216  fprintf (fp,"\n");
2217  fprintf (fp,"#\n");
2218  fprintf (fp,"# these specify the q-scale for I, P, and B frames\n");
2219  fprintf (fp,"# (values must be between 1 and 31)\n");
2220  fprintf (fp,"# These are the Qscale values for the entire frame in variable bit-rate\n");
2221  fprintf (fp,"# mode, and starting points (but not important) for constant bit rate\n");
2222  fprintf (fp,"#\n");
2223  fprintf (fp,"\n");
2224  fprintf (fp,"# Qscale (Quantization scale) affects quality and compression,\n");
2225  fprintf (fp,"# but has very little effect on speed.\n");
2226  fprintf (fp,"\n");
2227  fprintf (fp,"IQSCALE          4\n");
2228  fprintf (fp,"PQSCALE          5\n");
2229  fprintf (fp,"BQSCALE          12\n");
2230  fprintf (fp,"\n");
2231  fprintf (fp,"# this must be ORIGINAL or DECODED\n");
2232  fprintf (fp,"REFERENCE_FRAME  ORIGINAL\n");
2233  fprintf (fp,"\n");
2234  fprintf (fp,"# for parallel parameters see parallel.param in the exmaples subdirectory\n");
2235  fprintf (fp,"\n");
2236  fprintf (fp,"# if you want constant bit-rate mode, specify it as follows (number is bits/sec):\n");
2237  fprintf (fp,"#BIT_RATE  1000000\n");
2238  fprintf (fp,"\n");
2239  fprintf (fp,"# To specify the buffer size (327680 is default, measused in bits, for 16bit words)\n");
2240  fprintf (fp,"BUFFER_SIZE 327680\n");
2241  fprintf (fp,"\n");
2242  fprintf (fp,"# The frame rate is the number of frames/second (legal values:\n");
2243  fprintf (fp,"# 23.976, 24, 25, 29.97, 30, 50 ,59.94, 60\n");
2244  fprintf (fp,"FRAME_RATE 30\n");
2245  fprintf (fp,"\n");
2246  fprintf (fp,"# There are many more options, see the users manual for examples....\n");
2247  fprintf (fp,"# ASPECT_RATIO, USER_DATA, GAMMA, IQTABLE, etc.\n");
2248  fprintf (fp,"\n");
2249  fprintf (fp,"\n");
2250  fclose (fp);
2251
2252  QProcess::execute ("/sw/bin/mpeg_encode", QStringList("/temp/parameter_file.par"));
2253  return true;
2254}
2255
2256/*
2257 
2258void MultiLayer::exportToSVG(const QString& fname)
2259{
2260  QPicture picture;
2261  QPainter p(&picture);
2262  for (int i=0;i<(int)graphsList->count();i++)
2263    {
2264      Graph *gr=(Graph *)graphsList->at(i);
2265      Plot *myPlot= (Plot *)gr->plotWidget();
2266     
2267      QPoint pos=gr->pos();
2268     
2269      int width=int(myPlot->frameGeometry().width());
2270      int height=int(myPlot->frameGeometry().height());
2271     
2272      myPlot->print(&p, QRect(pos,QSize(width,height)));
2273    }
2274 
2275  p.end();
2276  picture.save(fname, "svg");
2277}
2278*/
2279#endif
Note: See TracBrowser for help on using the repository browser.