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

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

Modification pour Qt4 sur windows

  • Property svn:mime-type set to text/cpp
File size: 45.0 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.7 2007/11/15 18:24:28 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
51#include "G4Qt.hh"
52#include "G4UIsession.hh"
53#include "G4UImanager.hh"
54#include <qapplication.h>
55#include <qlayout.h>
56#include <qdialog.h>
57
58#if QT_VERSION >= 0x040000
59#include <qmenu.h>
60#include <qimagewriter.h>
61#else
62#include <qaction.h>
63#include <qwidgetlist.h>
64#include <qpopupmenu.h>
65#include <qimage.h>
66#endif
67
68#include <qmessagebox.h>
69#include <qfiledialog.h>
70#include <qprinter.h>
71#include <qpainter.h>
72#include <qgl.h> // include <qglwidget.h>
73#include <qdialog.h>
74#include <qevent.h> //include <qcontextmenuevent.h>
75
76
77//////////////////////////////////////////////////////////////////////////////
78/**
79   Implementation of virtual method of G4VViewer
80*/
81void G4OpenGLQtViewer::SetView (
82)
83//////////////////////////////////////////////////////////////////////////////
84//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
85{
86#ifdef GEANT4_QT_DEBUG
87  printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
88#endif
89  //   if(!fHDC) return;
90  //   if(!fHGLRC) return;
91  //   ::wglMakeCurrent(fHDC,fHGLRC);
92  //  fWindow->makeCurrent();
93  G4OpenGLViewer::SetView ();
94#ifdef GEANT4_QT_DEBUG
95  printf("G4OpenGLQtViewer::SetView --------------------\n");
96#endif
97}
98
99
100
101//////////////////////////////////////////////////////////////////////////////
102/**
103   Implementation of virtual method of G4VViewer
104*/
105void G4OpenGLQtViewer::ShowView (
106)
107//////////////////////////////////////////////////////////////////////////////
108//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
109{
110#ifdef GEANT4_QT_DEBUG
111  printf("G4OpenGLQtViewer::ShowView  +++++++++++++++++++++\n");
112#endif
113  glFlush ();
114  if (!GLWindow) {
115    G4cerr << "Visualization window not defined, please choose one before" << G4endl;
116  } else {
117#if QT_VERSION < 0x040000
118    GLWindow->setActiveWindow();
119#else
120    GLWindow->activateWindow();
121#endif
122#ifdef GEANT4_QT_DEBUG
123    printf("G4OpenGLQtViewer::ShowView -----------------------\n");
124#endif
125  }
126  //   // Empty the Windows message queue :
127  //   MSG event;
128  //   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
129  //     ::TranslateMessage(&event);
130  //     ::DispatchMessage (&event);
131  //   }
132}
133
134
135
136//////////////////////////////////////////////////////////////////////////////
137void G4OpenGLQtViewer::CreateGLQtContext (
138)
139//////////////////////////////////////////////////////////////////////////////
140//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
141{
142#ifdef GEANT4_QT_DEBUG
143  printf("G4OpenGLQtViewer::CreateGLQtContext \n");
144#endif
145}
146
147
148//////////////////////////////////////////////////////////////////////////////
149void G4OpenGLQtViewer::CreateMainWindow (
150 QGLWidget* glWidget
151)
152//////////////////////////////////////////////////////////////////////////////
153//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
154{
155
156  if(fWindow) return; //Done.
157#ifdef GEANT4_QT_DEBUG
158  printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
159#endif
160
161  // launch Qt if not
162  G4Qt* interactorManager = G4Qt::getInstance ();
163  //  G4UImanager* UI = G4UImanager::GetUIpointer();
164
165  fWindow = glWidget ;
166  //  fWindow->makeCurrent();
167
168  // create window
169  if (((QApplication*)interactorManager->GetMainInteractor())) {
170    // look for the main window
171    bool found = false;
172#if QT_VERSION < 0x040000
173    QWidgetList  *list = QApplication::allWidgets();
174    QWidgetListIt it( *list );         // iterate over the widgets
175    QWidget * widget;
176    while ( (widget=it.current()) != 0 ) {  // for each widget...
177      ++it;
178      if ((found== false) && (widget->inherits("QMainWindow"))) {
179#ifdef GEANT4_QT_DEBUG
180        printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
181#endif
182        GLWindow = new QDialog(widget,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
183        found = true;
184      }
185    }
186    delete list;                      // delete the list, not the widgets
187#else
188    foreach (QWidget *widget, QApplication::allWidgets()) {
189      if ((found== false) && (widget->inherits("QMainWindow"))) {
190#ifdef GEANT4_QT_DEBUG
191        printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
192#endif
193        GLWindow = new QDialog(widget,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
194        found = true;
195      }
196    }
197#endif
198
199#if QT_VERSION < 0x040000
200    glWidget->reparent(GLWindow,0,QPoint(0,0)); 
201#else
202    glWidget->setParent(GLWindow); 
203#endif
204
205    if (found==false) {
206#ifdef GEANT4_QT_DEBUG
207      printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
208#endif
209      GLWindow = new QDialog();
210    }
211  } else {
212#ifdef GEANT4_QT_DEBUG
213    printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
214#endif
215    GLWindow = new QDialog();
216  }
217
218#if QT_VERSION < 0x040000
219  QHBoxLayout *mainLayout = new QHBoxLayout(GLWindow);
220#else
221  QHBoxLayout *mainLayout = new QHBoxLayout;
222#endif
223
224  mainLayout->addWidget(fWindow);
225
226#if QT_VERSION < 0x040000
227  GLWindow->setCaption( tr( "QGl Viewer" ));
228#else
229  GLWindow->setLayout(mainLayout);
230  GLWindow->setWindowTitle(tr("QGl Viewer"));
231#endif
232  GLWindow->resize(300, 300);
233  GLWindow->move(900,300);
234#ifdef GEANT4_QT_DEBUG
235  printf("G4OpenGLQtViewer::Should show glWidget\n");
236#endif
237  GLWindow->show();
238 
239  // delete the pointer if close this
240  //  GLWindow->setAttribute(Qt::WA_DeleteOnClose);
241
242#if QT_VERSION >= 0x040000
243  QObject ::connect(GLWindow,
244                    SIGNAL(rejected()),
245                    this,
246                    SLOT(dialogClosed()));
247#endif
248
249  WinSize_x = 400;
250  WinSize_y = 400;
251  if (WinSize_x < fVP.GetWindowSizeHintX ())
252    WinSize_x = fVP.GetWindowSizeHintX ();
253  if (WinSize_y < fVP.GetWindowSizeHintY ())
254    WinSize_y = fVP.GetWindowSizeHintY ();
255
256  if(!fWindow) return;
257#ifdef GEANT4_QT_DEBUG
258  printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
259#endif
260
261  if (!fContextMenu)
262    createPopupMenu();
263
264}
265
266#if QT_VERSION >= 0x040000
267/**  Close the dialog and set the pointer to NULL
268 */
269void G4OpenGLQtViewer::dialogClosed() {
270#ifdef GEANT4_QT_DEBUG
271  printf("G4OpenGLQtViewer::dialogClosed END\n");
272#endif
273  //  GLWindow = NULL;
274}
275#endif
276
277//////////////////////////////////////////////////////////////////////////////
278G4OpenGLQtViewer::G4OpenGLQtViewer (
279                                    G4OpenGLSceneHandler& scene
280                                    )
281  :G4VViewer (scene, -1)
282  ,G4OpenGLViewer (scene)
283  ,fWindow(0)
284  ,fContextMenu(0)
285  ,fMouseAction(true)
286{
287#ifdef GEANT4_QT_DEBUG
288  printf("G4OpenGLQtViewer::G4OpenGLQtViewer \n");
289#endif
290}
291
292//////////////////////////////////////////////////////////////////////////////
293G4OpenGLQtViewer::~G4OpenGLQtViewer (
294)
295//////////////////////////////////////////////////////////////////////////////
296//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
297{
298#ifdef GEANT4_QT_DEBUG
299  printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
300#endif
301  delete fContextMenu;
302}
303
304
305/**
306   Create a popup menu for the widget. This menu is activated by right-mouse click
307*/
308void G4OpenGLQtViewer::createPopupMenu()    {
309
310#if QT_VERSION < 0x040000
311  fContextMenu = new QPopupMenu( GLWindow,"All" );
312#else
313  fContextMenu = new QMenu("All");
314#endif
315  Q_CHECK_PTR( fContextMenu );
316
317#if QT_VERSION < 0x040000
318  // === Mouse menu ===
319  QPopupMenu *mMouseAction = new QPopupMenu( fContextMenu );
320  Q_CHECK_PTR( mMouseAction );
321
322  QAction *rotate = new QAction("&Rotate scene",CTRL+Key_N,mMouseAction);
323  QAction *move =  new QAction("&Move scene",CTRL+Key_M,mMouseAction);
324  rotate->addTo(mMouseAction);
325  move->addTo(mMouseAction);
326
327  fContextMenu->insertItem( "&Mouse action", mMouseAction);
328
329#else
330  // === Mouse menu ===
331  QMenu *mMouseAction = fContextMenu->addMenu("&Mouse action");
332
333  QAction *rotate = mMouseAction->addAction("&Rotate scene");
334  QAction *move = mMouseAction->addAction("&Move scene");
335#endif
336
337  // INIT mMouse
338  createRadioAction(rotate,move,SLOT(toggleMouseAction(bool)),1);
339
340#if QT_VERSION < 0x040000
341  // === Style Menu ===
342  QPopupMenu *mStyle = new QPopupMenu(fContextMenu);
343
344  QPopupMenu *mRepresentation = new QPopupMenu(fContextMenu);
345
346  QAction *polyhedron = new QAction("&Polyhedron",CTRL+Key_P,mRepresentation);
347  QAction *nurbs = new QAction("&NURBS",CTRL+Key_N,mRepresentation);
348  polyhedron->addTo(mRepresentation);
349  nurbs->addTo(mRepresentation);
350
351  mStyle->insertItem("&Representation",mRepresentation);
352  fContextMenu->insertItem("&Style",mStyle);
353
354#else
355  // === Style Menu ===
356  QMenu *mStyle = fContextMenu->addMenu("&Style");
357
358  QMenu *mRepresentation = mStyle->addMenu("&Representation");
359 QAction *polyhedron = mRepresentation->addAction("Polyhedron");
360  QAction *nurbs = mRepresentation->addAction("NURBS");
361#endif
362  // INIT mStyle
363  G4ViewParameters::RepStyle style;
364  style = fVP.GetRepStyle();
365  if (style == G4ViewParameters::polyhedron) {
366    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
367  } else if (style == G4ViewParameters::nurbs) {
368    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
369  } else {
370    mRepresentation->clear();
371  }
372
373
374#if QT_VERSION < 0x040000
375  // === Drawing Menu ===
376  QPopupMenu *mDrawing = new QPopupMenu(fContextMenu);
377  fContextMenu->insertItem("&Drawing",mDrawing);
378
379  fDrawingWireframe = new QPopupMenu(mDrawing);
380  mDrawing->insertItem("&Wireframe",fDrawingWireframe);
381#ifdef GEANT4_QT_DEBUG
382  printf("G4OpenGLQtViewer:: fDrawingWireframe creation\n");
383#endif
384
385  mDrawing->setCheckable(true);
386  fDrawingWireframe->setCheckable(true);
387
388  fDrawingLineRemoval = new QPopupMenu(mDrawing);
389  mDrawing->insertItem("&Hidden line removal",fDrawingLineRemoval);
390  fDrawingLineRemoval->setCheckable(true);
391
392  fDrawingSurfaceRemoval = new QPopupMenu(mDrawing);
393  mDrawing->insertItem("&Hidden surface removal",fDrawingSurfaceRemoval);
394  fDrawingSurfaceRemoval->setCheckable(true);
395
396  fDrawingLineSurfaceRemoval = new QPopupMenu(mDrawing);
397  mDrawing->insertItem("&Hidden line and surface removal",fDrawingLineSurfaceRemoval);
398  fDrawingLineSurfaceRemoval->setCheckable(true);
399
400#else
401  // === Drawing Menu ===
402  QMenu *mDrawing = mStyle->addMenu("&Drawing");
403
404  fDrawingWireframe = mDrawing->addAction("Wireframe");
405  fDrawingWireframe->setCheckable(true);
406
407  fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
408  fDrawingLineRemoval->setCheckable(true);
409
410  fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
411  fDrawingSurfaceRemoval->setCheckable(true);
412
413  fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
414  fDrawingLineSurfaceRemoval->setCheckable(true);
415#endif
416  // INIT Drawing
417  G4ViewParameters::DrawingStyle d_style;
418  d_style = fVP.GetDrawingStyle();
419 
420#if QT_VERSION < 0x040000
421  if (d_style == G4ViewParameters::wireframe) {
422    fDrawingWireframe->setItemChecked(0,true);
423  } else if (d_style == G4ViewParameters::hlr) {
424    fDrawingLineRemoval->setItemChecked(0,true);
425  } else if (d_style == G4ViewParameters::hsr) {
426    fDrawingSurfaceRemoval->setItemChecked(0,true);
427  } else if (d_style == G4ViewParameters::hlhsr) {
428    fDrawingLineSurfaceRemoval->setItemChecked(0,true);
429  } else {
430    mDrawing->clear();
431  }
432#ifdef GEANT4_QT_DEBUG
433  printf("G4OpenGLQtViewer:: fDrawingWireframe 1\n");
434#endif
435  QObject ::connect(fDrawingWireframe,
436                    SIGNAL(activated(int)),
437                    this,
438                    SLOT(actionDrawingWireframe()));
439#ifdef GEANT4_QT_DEBUG
440  printf("G4OpenGLQtViewer:: fDrawingWireframe 2\n");
441#endif
442  QObject ::connect(fDrawingLineRemoval,
443                    SIGNAL(activated(int)),
444                    this,
445                    SLOT(actionDrawingLineRemoval()));
446  QObject ::connect(fDrawingSurfaceRemoval,
447                    SIGNAL(activated(int)),
448                    this,
449                    SLOT(actionDrawingSurfaceRemoval()));
450  QObject ::connect(fDrawingLineSurfaceRemoval,
451                    SIGNAL(activated(int)),
452                    this,
453                    SLOT(actionDrawingLineSurfaceRemoval()));
454#else
455  if (d_style == G4ViewParameters::wireframe) {
456    fDrawingWireframe->setChecked(true);
457  } else if (d_style == G4ViewParameters::hlr) {
458    fDrawingLineRemoval->setChecked(true);
459  } else if (d_style == G4ViewParameters::hsr) {
460    fDrawingSurfaceRemoval->setChecked(true);
461  } else if (d_style == G4ViewParameters::hlhsr) {
462    fDrawingLineSurfaceRemoval->setChecked(true);
463  } else {
464    mDrawing->clear();
465  }
466  QObject ::connect(fDrawingWireframe,
467                    SIGNAL(triggered(bool)),
468                    this,
469                    SLOT(actionDrawingWireframe()));
470  QObject ::connect(fDrawingLineRemoval,
471                    SIGNAL(triggered(bool)),
472                    this,
473                    SLOT(actionDrawingLineRemoval()));
474  QObject ::connect(fDrawingSurfaceRemoval,
475                    SIGNAL(triggered(bool)),
476                    this,
477                    SLOT(actionDrawingSurfaceRemoval()));
478  QObject ::connect(fDrawingLineSurfaceRemoval,
479                    SIGNAL(triggered(bool)),
480                    this,
481                    SLOT(actionDrawingLineSurfaceRemoval()));
482#endif
483
484
485
486#if QT_VERSION < 0x040000
487  QPopupMenu *mBackground = new QPopupMenu(mStyle);
488  mStyle->insertItem("&Background color",mBackground);
489
490  QAction *white = new QAction("&White",CTRL+Key_W,mBackground);
491  QAction *black =  new QAction("&Black",CTRL+Key_B,mBackground);
492  white->addTo(mBackground);
493  black->addTo(mBackground);
494
495#else
496  QMenu *mBackground = mStyle->addMenu("&Background color");
497  QAction *white = mBackground->addAction("White");
498  QAction *black = mBackground->addAction("Black");
499
500#endif
501  if (background.GetRed() == 1. &&
502      background.GetGreen() == 1. &&
503      background.GetBlue() == 1.) {
504    createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
505  } else {
506    createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
507  }
508
509
510#if QT_VERSION < 0x040000
511  // === Action Menu ===
512  QPopupMenu *mActions = new QPopupMenu(fContextMenu);
513  fContextMenu->insertItem("&Actions",mActions);
514
515  QAction *controlPanels = new QAction("&Control panels",CTRL+Key_C,mActions);
516  QAction *exitG4 =  new QAction("&Exit to G4Vis >",CTRL+Key_E,mActions);
517  QAction *createEPS =  new QAction("&Save as ...",CTRL+Key_S,mActions);
518  controlPanels->addTo(mActions);
519  exitG4->addTo(mActions);
520  createEPS->addTo(mActions);
521  QObject ::connect(controlPanels,
522                    SIGNAL(activated()),
523                    this,
524                    SLOT(actionControlPanels()));
525  QObject ::connect(exitG4,
526                    SIGNAL(activated()),
527                    this,
528                    SLOT(actionExitG4()));
529  QObject ::connect(createEPS,
530                    SIGNAL(activated()),
531                    this,
532                    SLOT(actionCreateEPS()));
533
534#else
535  // === Action Menu ===
536  QMenu *mActions = fContextMenu->addMenu("&Actions");
537  QAction *controlPanels = mActions->addAction("Control panels");
538  QAction *exitG4 = mActions->addAction("Exit to G4Vis >");
539  QAction *createEPS = mActions->addAction("Save as ...");
540  QObject ::connect(controlPanels,
541                    SIGNAL(triggered()),
542                    this,
543                    SLOT(actionControlPanels()));
544  QObject ::connect(exitG4,
545                    SIGNAL(triggered()),
546                    this,
547                    SLOT(actionExitG4()));
548  QObject ::connect(createEPS,
549                    SIGNAL(triggered()),
550                    this,
551                    SLOT(actionCreateEPS()));
552#endif
553
554
555
556#if QT_VERSION < 0x040000
557  // === Special Menu ===
558  QPopupMenu *mSpecial = new QPopupMenu(fContextMenu);
559  fContextMenu->insertItem("S&pecial",mSpecial);
560
561  QPopupMenu *mTransparency = new QPopupMenu(mSpecial);
562  mSpecial->insertItem("Transparency",mTransparency);
563
564  QAction *transparencyOn = new QAction("&On",CTRL+Key_O,mTransparency);
565  QAction *transparencyOff = new QAction("&Off",CTRL+Key_F,mTransparency);
566  transparencyOn->addTo(mTransparency);
567  transparencyOff->addTo(mTransparency);
568
569#else
570  // === Special Menu ===
571  QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
572  QMenu *mTransparency = mSpecial->addMenu("Transparency");
573  QAction *transparencyOn = mTransparency->addAction("On");
574  QAction *transparencyOff = mTransparency->addAction("Off");
575#endif
576
577  if (transparency_enabled == false) {
578    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
579  } else if (transparency_enabled == true) {
580    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
581  } else {
582    mSpecial->clear();
583  }
584
585
586#if QT_VERSION < 0x040000
587  QPopupMenu *mAntialiasing = new QPopupMenu(mSpecial);
588  mSpecial->insertItem("Antialiasing",mAntialiasing);
589
590  QAction *antialiasingOn = new QAction("&On",CTRL+Key_O,mAntialiasing);
591  QAction *antialiasingOff = new QAction("&Off",CTRL+Key_F,mAntialiasing);
592  antialiasingOn->addTo(mAntialiasing);
593  antialiasingOff->addTo(mAntialiasing);
594
595#else
596  QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
597  QAction *antialiasingOn = mAntialiasing->addAction("On");
598  QAction *antialiasingOff = mAntialiasing->addAction("Off");
599#endif
600
601  if (antialiasing_enabled == false) {
602    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
603  } else if (antialiasing_enabled == true) {
604    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
605  } else {
606    mAntialiasing->clear();
607  }
608
609#if QT_VERSION < 0x040000
610  QPopupMenu *mHaloing = new QPopupMenu(mSpecial);
611  mSpecial->insertItem("Haloing",mHaloing);
612
613  QAction *haloingOn = new QAction("&On",CTRL+Key_O,mHaloing);
614  QAction *haloingOff = new QAction("&Off",CTRL+Key_F,mHaloing);
615  haloingOn->addTo(mHaloing);
616  haloingOff->addTo(mHaloing);
617#else
618  QMenu *mHaloing = mSpecial->addMenu("Haloing");
619  QAction *haloingOn = mHaloing->addAction("On");
620  QAction *haloingOff = mHaloing->addAction("Off");
621#endif
622  if (haloing_enabled == false) {
623    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
624  } else if (haloing_enabled == true) {
625    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
626  } else {
627    mHaloing->clear();
628  }
629
630#if QT_VERSION < 0x040000
631  QPopupMenu *mAux = new QPopupMenu(mSpecial);
632  mSpecial->insertItem("Auxiliairy edges",mAux);
633
634  QAction *auxOn = new QAction("&On",CTRL+Key_O,mAux);
635  QAction *auxOff = new QAction("&Off",CTRL+Key_F,mAux);
636  auxOn->addTo(mAux);
637  auxOff->addTo(mAux);
638
639#else
640  QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
641  QAction *auxOn = mAux->addAction("On");
642  QAction *auxOff = mAux->addAction("Off");
643#endif
644  if (!fVP.IsAuxEdgeVisible()) {
645    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
646  } else {
647    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
648  }
649
650
651#if QT_VERSION < 0x040000
652  QPopupMenu *mFullScreen = new QPopupMenu(mSpecial);
653  mSpecial->insertItem("Full screen",mFullScreen);
654
655  QAction *fullOn = new QAction("&On",CTRL+Key_O,mFullScreen);
656  QAction *fullOff = new QAction("&Off",CTRL+Key_F,mFullScreen);
657  fullOn->addTo(mFullScreen);
658  fullOff->addTo(mFullScreen);
659#else
660  QMenu *mFullScreen = mSpecial->addMenu("Full screen");
661  QAction *fullOn = mFullScreen->addAction("On");
662  QAction *fullOff = mFullScreen->addAction("Off");
663#endif
664  createRadioAction(fullOn,fullOff,SLOT(toggleFullScreen(bool)),2);
665
666}
667
668void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
669{
670  if (!GLWindow) {
671    G4cerr << "Visualization window not defined, please choose one before" << G4endl;
672  } else {
673 
674    if (!fContextMenu)
675      createPopupMenu();
676   
677    // launch menu
678    if ( fContextMenu ) {
679      fContextMenu->exec( e->globalPos() );
680      //    delete fContextMenu;
681    }
682  }
683  e->accept();
684}
685
686
687/**
688   Create a radio button menu. The two menu will be connected. When click on one,
689   eatch state will be invert and callback method will be called.
690   @param action1 first action to connect
691   @param action2 second action to connect
692   @param method callback method
693   @param nCheck: 1 : first action will be set true. 2 : second action will be set true
694*/
695#if QT_VERSION < 0x040000
696void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
697
698  if (action1->parent()->inherits("QPopupMenu")){
699    ((QPopupMenu*)action1->parent())->setCheckable(true);
700  }
701  ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
702  ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
703
704  if (nCheck ==1)
705    ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
706  else
707    ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
708   
709  //FIXME : Should not work on Qt3
710  QObject ::connect(action1, SIGNAL(activated()),action2, SLOT(toggle()));
711  QObject ::connect(action2, SIGNAL(activated()),action1, SLOT(toggle()));
712
713  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
714}
715
716#else
717void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
718
719  action1->setCheckable(true);
720  action2->setCheckable(true);
721
722  if (nCheck ==1)
723    action1->setChecked (true);
724  else
725    action2->setChecked (true);
726   
727  QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
728  QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
729
730  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
731
732}
733#endif
734
735/**
736   Slot activate when drawing->wireframe menu is set
737 */
738void G4OpenGLQtViewer::actionDrawingWireframe() {
739  emit toggleDrawingAction(1);
740}
741
742/**
743   Slot activate when drawing->line removal menu is set
744 */
745void G4OpenGLQtViewer::actionDrawingLineRemoval() {
746  emit toggleDrawingAction(2);
747}
748
749/**
750   Slot activate when drawing->surface removal menu is set
751 */
752void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
753  emit toggleDrawingAction(3);
754}
755
756/**
757   Slot activate when drawing->wireframe menu is set
758 */
759void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
760  emit toggleDrawingAction(4);
761}
762
763
764/**
765   Slot activated when drawing menu is toggle
766   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
767   KernelVisitDecision () will be call and will set the fNeedKernelVisit
768   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
769   It will cause a redraw of the view
770   @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
771   @see G4OpenGLStoredQtViewer::DrawView
772   @see G4XXXStoredViewer::CompareForKernelVisit
773 */
774void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
775
776  G4ViewParameters::DrawingStyle d_style;
777 
778
779  if (aAction ==1) {
780#if QT_VERSION < 0x040000
781    fDrawingWireframe->setItemChecked (0,true);
782    fDrawingLineRemoval->setItemChecked (0,false);
783    fDrawingSurfaceRemoval->setItemChecked (0,false);
784    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
785#else
786    fDrawingWireframe->setChecked (true);
787    fDrawingLineRemoval->setChecked (false);
788    fDrawingSurfaceRemoval->setChecked (false);
789    fDrawingLineSurfaceRemoval->setChecked (false);
790#endif
791
792    d_style = G4ViewParameters::wireframe;
793
794  } else  if (aAction ==2) {
795#if QT_VERSION < 0x040000
796    fDrawingWireframe->setItemChecked (0,false);
797    fDrawingLineRemoval->setItemChecked (0,true);
798    fDrawingSurfaceRemoval->setItemChecked (0,false);
799    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
800#else
801    fDrawingWireframe->setChecked (false);
802    fDrawingLineRemoval->setChecked (true);
803    fDrawingSurfaceRemoval->setChecked (false);
804    fDrawingLineSurfaceRemoval->setChecked (false);
805#endif
806
807    d_style = G4ViewParameters::hlr;
808
809  } else  if (aAction ==3) {
810#if QT_VERSION < 0x040000
811    fDrawingWireframe->setItemChecked (0,false);
812    fDrawingLineRemoval->setItemChecked (0,false);
813    fDrawingSurfaceRemoval->setItemChecked (0,true);
814    fDrawingLineSurfaceRemoval->setItemChecked (0,false);
815#else
816    fDrawingWireframe->setChecked (false);
817    fDrawingLineRemoval->setChecked (false);
818    fDrawingSurfaceRemoval->setChecked (true);
819    fDrawingLineSurfaceRemoval->setChecked (false);
820#endif
821
822    d_style = G4ViewParameters::hsr;
823
824  } else  if (aAction ==4) {
825#if QT_VERSION < 0x040000
826    fDrawingWireframe->setItemChecked (0,false);
827    fDrawingLineRemoval->setItemChecked (0,false);
828    fDrawingSurfaceRemoval->setItemChecked (0,false);
829    fDrawingLineSurfaceRemoval->setItemChecked (0,true);
830#else
831    fDrawingWireframe->setChecked (false);
832    fDrawingLineRemoval->setChecked (false);
833    fDrawingSurfaceRemoval->setChecked (false);
834    fDrawingLineSurfaceRemoval->setChecked (true);
835#endif
836    d_style = G4ViewParameters::hlhsr;
837  }
838  fVP.SetDrawingStyle(d_style);
839
840  updateQWidget();
841#ifdef GEANT4_QT_DEBUG
842  printf("G4OpenGLQtViewer::toggleDrawingAction\n");
843#endif
844}
845
846
847/**
848   SLOT Activate by a click on the representation menu
849   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
850   KernelVisitDecision () will be call and will set the fNeedKernelVisit
851   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
852   It will cause a redraw of the view
853   @param check : 1 polyhedron, 0 nurbs
854   @see G4OpenGLStoredQtViewer::DrawView
855   @see G4XXXStoredViewer::CompareForKernelVisit
856*/
857void G4OpenGLQtViewer::toggleRepresentation(bool check) {
858
859  G4ViewParameters::RepStyle style;
860  if (check == 1) {
861    style = G4ViewParameters::polyhedron;
862  } else {
863    style = G4ViewParameters::nurbs;
864  }
865  fVP.SetRepStyle (style);
866
867#ifdef GEANT4_QT_DEBUG
868  printf("G4OpenGLQtViewer::toggleRepresentation 3%d\n",check);
869#endif
870  updateQWidget();
871#ifdef GEANT4_QT_DEBUG
872  printf("G4OpenGLQtViewer::toggleRepresentation 4%d\n",check);
873#endif
874}
875
876/**
877   SLOT Activate by a click on the background menu
878@param check : 1 white, 0 black
879*/
880void G4OpenGLQtViewer::toggleBackground(bool check) {
881
882  //   //I need to revisit the kernel if the background colour changes and
883  //   //hidden line removal is enabled, because hlr drawing utilises the
884  //   //background colour in its drawing...
885  //   // (Note added by JA 13/9/2005) Background now handled in view
886  //   // parameters.  A kernel visit is triggered on change of background.
887  if (check == 1) {
888    ((G4ViewParameters&)this->GetViewParameters()).
889      SetBackgroundColour(G4Colour(1.,1.,1.));  // White
890  } else {
891    ((G4ViewParameters&)this->GetViewParameters()).
892      SetBackgroundColour(G4Colour(0.,0.,0.));  // Black
893  }
894  updateQWidget();
895}
896
897/**
898   SLOT Activate by a click on the transparency menu
899@param check : 1 , 0
900*/
901void G4OpenGLQtViewer::toggleTransparency(bool check) {
902 
903  if (check) {
904    transparency_enabled = false;
905  } else {
906    transparency_enabled = true;
907  }
908  SetNeedKernelVisit (true);
909  updateQWidget();
910}
911
912/**
913   SLOT Activate by a click on the antialiasing menu
914@param check : 1 , 0
915*/
916void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
917
918  if (!check) {
919    antialiasing_enabled = false;
920    glDisable (GL_LINE_SMOOTH);
921    glDisable (GL_POLYGON_SMOOTH);
922  } else {
923    antialiasing_enabled = true;
924    glEnable (GL_LINE_SMOOTH);
925    glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
926    glEnable (GL_POLYGON_SMOOTH);
927    glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
928  }
929
930  updateQWidget();
931#ifdef GEANT4_QT_DEBUG
932  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
933#endif
934}
935
936/**
937   SLOT Activate by a click on the haloing menu
938@param check : 1 , 0
939*/
940//FIXME : I SEE NOTHING...
941void G4OpenGLQtViewer::toggleHaloing(bool check) {
942  if (check) {
943    haloing_enabled = false;
944  } else {
945    haloing_enabled = true;
946  }
947
948  updateQWidget();
949
950#ifdef GEANT4_QT_DEBUG
951  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
952#endif
953}
954
955/**
956   SLOT Activate by a click on the auxiliaire edges menu
957@param check : 1 , 0
958*/
959void G4OpenGLQtViewer::toggleAux(bool check) {
960  if (check) {
961    fVP.SetAuxEdgeVisible(false);
962  } else {
963    fVP.SetAuxEdgeVisible(true);
964  }
965  SetNeedKernelVisit (true);
966  updateQWidget();
967
968#ifdef GEANT4_QT_DEBUG
969  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
970#endif
971}
972
973/**
974   SLOT Activate by a click on the full screen menu
975@param check : 1 , 0
976*/
977void G4OpenGLQtViewer::toggleFullScreen(bool check) {
978  GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
979
980#ifdef GEANT4_QT_DEBUG
981  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
982#endif
983}
984
985/**
986   SLOT Activate by a click on the mouse action menu
987   @param check : 1 , 0
988*/
989void G4OpenGLQtViewer::toggleMouseAction(bool check) {
990  if (check) { // rotate scene
991    fMouseAction = true;
992  } else { // move scene
993    fMouseAction = false;
994  }
995
996#ifdef GEANT4_QT_DEBUG
997  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
998#endif
999}
1000
1001
1002void G4OpenGLQtViewer::actionControlPanels() {
1003#ifdef GEANT4_QT_DEBUG
1004  printf("G4OpenGLQtViewer::actionControlPanels \n");
1005#endif
1006}
1007
1008void G4OpenGLQtViewer::actionExitG4() {
1009#ifdef GEANT4_QT_DEBUG
1010  printf("G4OpenGLQtViewer::actionExitG4() \n");
1011#endif
1012}
1013
1014void G4OpenGLQtViewer::actionCreateEPS() {
1015  QString filters;
1016#if QT_VERSION < 0x040000
1017  QStrList listFormat=QImageIO::outputFormats();
1018  char *tmp=listFormat.first();
1019  while (tmp!=0) {
1020    filters += QString(tmp) + ";;";
1021    tmp=listFormat.next();
1022  }
1023#else
1024  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
1025  for (int i = 0; i < formats.size(); ++i) {
1026    filters +=formats.at(i) + ";;";
1027  }
1028#endif
1029  filters += "eps;;";
1030  filters += "ps;;";
1031  filters += "pdf";
1032  QString* selectedFilter = new QString();
1033#if QT_VERSION < 0x040000
1034  QString nomFich =  QFileDialog::getSaveFileName ( ".",
1035                                                    filters,
1036                                                    GLWindow,
1037                                                    "Save file dialog",
1038                                                    tr("Save as ..."),
1039                                                    selectedFilter );
1040#else
1041  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
1042                                                    tr("Save as ..."),
1043                                                    ".",
1044                                                    filters,
1045                                                    selectedFilter );
1046#endif
1047  // bmp jpg jpeg png ppm xbm xpm
1048  if (nomFich == "") {
1049    return;
1050  }
1051#if QT_VERSION < 0x040000
1052  nomFich += "."+selectedFilter->lower();
1053#ifdef GEANT4_QT_DEBUG
1054  printf("G4OpenGLQtViewer::name %s\n",nomFich.ascii());
1055#endif
1056#else
1057  nomFich += "."+selectedFilter->toLower();
1058#ifdef GEANT4_QT_DEBUG
1059  printf("G4OpenGLQtViewer::name %s\n",nomFich.toStdString().c_str());
1060#endif
1061#endif
1062  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,nomFich,fWindow->height(),fWindow->width());
1063  if(  exportDialog->exec()) {
1064
1065    QImage image;
1066    //    if ((exportDialog->getWidth() !=fWindow->width()) ||
1067    //        (exportDialog->getHeight() !=fWindow->height())) {
1068     
1069      //      rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1070#ifdef GEANT4_QT_DEBUG
1071      printf("rescaling\n");
1072#endif
1073      QGLWidget* glResized = fWindow;
1074      fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2 ).save("/Users/laurentgarnier/Desktop/zzz.jpg","jpg");
1075      QPixmap * pixmap = new QPixmap(fWindow->renderPixmap (exportDialog->getWidth(),exportDialog->getHeight() )) ;
1076      //      image = pixmap.toImage();
1077      //      glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1078#ifdef GEANT4_QT_DEBUG
1079      printf("rescaling after\n");
1080#endif
1081      //      image = glResized->grabFrameBuffer();
1082     
1083      //    } else {
1084      // image = fWindow->grabFrameBuffer();
1085      //  }   
1086    // jpeg format
1087    if (nomFich.endsWith(".jpg") ||
1088        nomFich.endsWith(".jpeg")) {
1089      // grabFrameBuffer() :: Returns an image of the frame buffer. If withAlpha is true the alpha channel is included.
1090      image.save(nomFich,0,exportDialog->getSliderValue());
1091#ifdef GEANT4_QT_DEBUG
1092      printf("saving jpeg quality : %d\n",exportDialog->getSliderValue());
1093#endif
1094    } else if (nomFich.endsWith(".eps")) {
1095      generateEPS(nomFich,exportDialog->getNbColor(),image);
1096    } else if (nomFich.endsWith(".ps") ||nomFich.endsWith(".pdf")) {
1097      generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1098    } else if (nomFich.endsWith(".tif") ||
1099               nomFich.endsWith(".tiff") ||
1100               nomFich.endsWith(".jpg") ||
1101               nomFich.endsWith(".png") ||
1102               nomFich.endsWith(".bmp") ||
1103               nomFich.endsWith(".xpm")) {
1104      image.save(nomFich,0,exportDialog->getSliderValue());
1105#ifdef GEANT4_QT_DEBUG
1106      printf("saving ELSE\n");
1107#endif
1108    } else {
1109      G4cerr << "This version of G4UI Could not generate the selected format" << G4endl;
1110    }
1111   
1112  } else { // cancel selected
1113    return;
1114  }
1115 
1116#ifdef GEANT4_QT_DEBUG
1117  printf("G4OpenGLQtViewer::actionCreateEPS() \n");
1118#endif
1119}
1120
1121/*
1122// 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
1123
1124void Graph::exportToSVG(const QString& fname)
1125{
1126  // enable workaround for Qt3 misalignments
1127  QwtPainter::setSVGMode(true);
1128  QPicture picture;
1129  QPainter p(&picture);
1130  d_plot->print(&p, d_plot->rect());
1131  p.end();
1132
1133  picture.save(fname, "svg");
1134}
1135*/
1136
1137
1138
1139
1140/**
1141   Save the current mouse press point
1142   @param p mouse click point
1143*/
1144void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
1145{
1146  lastPos = p;
1147}
1148
1149/**
1150   @param pos_x mouse x position
1151   @param pos_y mouse y position
1152   @param mButtons mouse button active
1153*/
1154
1155#if QT_VERSION < 0x040000
1156void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::ButtonState mButtons)
1157#else
1158void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons)
1159#endif
1160{
1161  int dx = pos_x - lastPos.x();
1162  int dy = pos_y - lastPos.y();
1163 
1164  if (fMouseAction) {  // rotate
1165    if (mButtons & Qt::LeftButton) {
1166      //phi spin stuff here
1167     
1168      G4Vector3D vp = fVP.GetViewpointDirection ().unit ();
1169      G4Vector3D up = fVP.GetUpVector ().unit ();
1170     
1171      G4Vector3D yprime = (up.cross(vp)).unit();
1172      G4Vector3D zprime = (vp.cross(yprime)).unit();
1173     
1174      G4double delta_alpha;
1175      G4double delta_theta;
1176     
1177      if (fVP.GetLightsMoveWithCamera()) {
1178        delta_alpha = dy;
1179        delta_theta = -dx;
1180      } else {
1181        delta_alpha = -dy;
1182        delta_theta = dx;
1183      }   
1184
1185      delta_alpha *= deg;
1186      delta_theta *= deg;
1187
1188      G4Vector3D new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1189     
1190      G4Vector3D new_up;
1191      if (fVP.GetLightsMoveWithCamera()) {
1192        new_up = (new_vp.cross(yprime)).unit();
1193        fVP.SetUpVector(new_up);
1194      } else {
1195        new_up = up;
1196      }
1197      ////////////////
1198      // Rotates by fixed azimuthal angle delta_theta.
1199
1200      G4double cosalpha = new_up.dot (new_vp.unit());
1201      G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1202      yprime = (new_up.cross (new_vp.unit())).unit ();
1203      G4Vector3D xprime = yprime.cross (new_up);
1204      // Projection of vp on plane perpendicular to up...
1205      G4Vector3D a1 = sinalpha * xprime;
1206      // Required new projection...
1207      G4Vector3D a2 =
1208        sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1209      // Required Increment vector...
1210      G4Vector3D delta = a2 - a1;
1211      // So new viewpoint is...
1212      G4Vector3D viewPoint = new_vp.unit() + delta;
1213
1214      fVP.SetViewAndLights (viewPoint);
1215      updateQWidget();
1216     
1217    } else if (mButtons & Qt::RightButton) {
1218      // NEVER DONE BECAUSE OF MOUSE MENU
1219#ifdef GEANT4_QT_DEBUG
1220      //       printf("G4OpenGLQtViewer::mouseMoveEvent Right \n");
1221#endif
1222      //       setXRotation(xRot + dy/2);
1223      //       setZRotation(zRot + dx/2);
1224      //       updateQWidget();
1225    }
1226  } else {  // move
1227
1228    float dx = pos_x - lastPos.x();
1229    float dy = pos_y - lastPos.y();
1230   
1231    G4Point3D stp
1232      = GetSceneHandler()->GetScene()->GetStandardTargetPoint();
1233   
1234    G4Point3D tp = stp + fVP.GetCurrentTargetPoint ();
1235   
1236    const G4Vector3D& upVector = fVP.GetUpVector ();
1237    const G4Vector3D& vpVector = fVP.GetViewpointDirection ();
1238   
1239    G4Vector3D unitRight = (upVector.cross (vpVector)).unit();
1240    G4Vector3D unitUp    = (vpVector.cross (unitRight)).unit();
1241   
1242    tp += -dx * unitRight + dy * unitUp;
1243    fVP.SetCurrentTargetPoint (tp - stp);
1244   
1245    updateQWidget();
1246  }
1247  lastPos = QPoint(pos_x, pos_y);
1248}
1249
1250void G4OpenGLQtViewer::rescaleImage(
1251 int aWidth
1252,int aHeight
1253){
1254#ifdef GEANT4_QT_DEBUG
1255  printf("should rescale \n");
1256#endif
1257}
1258
1259/**
1260   Generate Postscript form image
1261   @param aFilename : name of file
1262   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1263   @param aImage : Image to print
1264*/
1265bool G4OpenGLQtViewer::generateEPS (
1266 QString aFilename
1267,int aInColor
1268,QImage aImage
1269)
1270{
1271  // FIXME
1272#ifdef GEANT4_QT_DEBUG
1273  printf("saving EPS\n");
1274#endif
1275
1276  FILE* fp;
1277
1278  if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1279#if QT_VERSION < 0x040000
1280    aImage.convertDepth(1,Qt::MonoOnly);
1281#else
1282    aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1283#endif
1284  }
1285  const uchar * pixels = aImage.bits ();
1286   
1287  if (pixels == NULL)
1288    return false;
1289 
1290#if QT_VERSION < 0x040000
1291  fp = fopen (aFilename.ascii(), "w");
1292#else
1293  fp = fopen (aFilename.toStdString().c_str(), "w");
1294#endif
1295  if (fp == NULL) {
1296    return false;
1297  }
1298 
1299  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
1300#if QT_VERSION < 0x040000
1301  fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1302#else
1303  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
1304#endif
1305  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
1306  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
1307  fprintf (fp, "%%%%EndComments\n");
1308  fprintf (fp, "gsave\n");
1309  fprintf (fp, "/bwproc {\n");
1310  fprintf (fp, "    rgbproc\n");
1311  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
1312  fprintf (fp, "    5 -1 roll {\n");
1313  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
1314  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1315  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1316  fprintf (fp, "    { 2 1 roll } ifelse\n");
1317  fprintf (fp, "    }forall\n");
1318  fprintf (fp, "    pop pop pop\n");
1319  fprintf (fp, "} def\n");
1320  fprintf (fp, "systemdict /colorimage known not {\n");
1321  fprintf (fp, "   /colorimage {\n");
1322  fprintf (fp, "       pop\n");
1323  fprintf (fp, "       pop\n");
1324  fprintf (fp, "       /rgbproc exch def\n");
1325  fprintf (fp, "       { bwproc } image\n");
1326  fprintf (fp, "   }  def\n");
1327  fprintf (fp, "} if\n");
1328  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1329  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1330  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1331  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
1332  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1333  fprintf (fp, "false %d\n", aInColor);
1334  fprintf (fp, "colorimage\n");
1335 
1336
1337  int width = aImage.width();
1338  int height = aImage.height();
1339  int depth = aImage.depth();
1340  int size = width*height;
1341 
1342  if (depth == 1)
1343    size = (width+7)/8*height;
1344  else if (aInColor == 1)
1345    size = size*3;
1346 
1347  int i = 0;
1348  if (depth == 1) {
1349    //  To be implemented
1350    //    QImage::Endian bitOrder = aImage.bitOrder();
1351    /*    for(int y=0; y < height; y++) {
1352      const uchar * s = aImage.scanLine(y);
1353      for(int x=0; x < width; x++) {
1354        // need to copy bit for bit...
1355        bool b = (bitOrder == QImage::LittleEndian) ?
1356          (*(s + (x >> 3)) >> (x & 7)) & 1 :
1357          (*(s + (x >> 3)) << (x & 7)) & 0x80 ;
1358        if (b)
1359          pixel[i >> 3] ^= (0x80 >> (i & 7));
1360        i++;
1361      }
1362      // we need to align to 8 bit here
1363      i = (i+7) & 0xffffff8;
1364    }
1365    */
1366  } else if (depth == 8) {
1367#ifdef GEANT4_QT_DEBUG
1368    printf("has 8 bit\n");
1369#endif
1370    for(int y=height-1; y >=0 ; y--) {
1371      const uchar * s = aImage.scanLine(y);
1372      for(int x=0; x <width; x++) {
1373        QRgb rgb = aImage.color(s[x]);
1374        if (aInColor == 1) {
1375          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1376          i++;
1377        } else {
1378          fprintf (fp, " %02hx %02hx %02hx",
1379                   (unsigned char) qRed(rgb),
1380                   (unsigned char) qGreen(rgb),
1381                   (unsigned char) qBlue(rgb));
1382          i += 3;
1383        }
1384      }
1385      fprintf (fp, "\n");
1386    }
1387  } else {
1388#if QT_VERSION < 0x040000
1389  G4cerr << "GenerateEPS:: No alpha channel image with Qt3. This is only supported with Qt4" << G4endl;
1390#else
1391    bool alpha = aImage.hasAlphaChannel();
1392#ifdef GEANT4_QT_DEBUG
1393    printf("has else %d alpha %d\n",depth,alpha);
1394#endif
1395    for(int y=height-1; y >=0 ; y--) {
1396      QRgb * s = (QRgb*)(aImage.scanLine(y));
1397      for(int x=0; x <width; x++) {
1398        QRgb rgb = (*s++);
1399        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1400          rgb = qRgb(0xff, 0xff, 0xff);
1401        if (aInColor == 1) {
1402          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1403          i++;
1404        } else {
1405          fprintf (fp, " %02hx %02hx %02hx",
1406                   (unsigned char) qRed(rgb),
1407                   (unsigned char) qGreen(rgb),
1408                   (unsigned char) qBlue(rgb));
1409          i += 3;
1410        }
1411      }
1412      fprintf (fp, "\n");
1413    }
1414#endif
1415
1416  }
1417
1418  fprintf (fp, "grestore\n");
1419  fprintf (fp, "showpage\n");
1420  fclose (fp);
1421
1422  return true;
1423}
1424/**
1425   Generate Postscript or PDF form image
1426   @param aFilename : name of file
1427   @param aInColor : numbers of colors : 1->BW 2->RGB
1428   @param aImage : Image to print
1429*/
1430bool G4OpenGLQtViewer::generatePS_PDF (
1431 QString aFilename
1432,int aInColor
1433,QImage aImage
1434)
1435{
1436#if QT_VERSION < 0x040000
1437#ifdef Q_WS_MAC || Q_WS_X11
1438  QPrinter printer;
1439  //  printer.setPageSize(pageSize);
1440  if (aInColor == 1) {
1441    printer.setColorMode(QPrinter::GrayScale);
1442  } else {
1443    printer.setColorMode(QPrinter::Color);
1444  }
1445
1446  /* FIXME : I don't know which format it will save...
1447     if (aFilename.endsWith(".ps")) {
1448     printer.setOutputFormat(QPrinter::PostScriptFormat);
1449     } else {
1450     printer.setOutputFormat(QPrinter::PdfFormat);
1451     }
1452  */
1453  printer.setOutputFileName(aFilename);
1454  //  printer.setFullPage ( true);
1455  QPainter paint(&printer);
1456  paint.drawImage (0,0,aImage );
1457  paint.end();
1458#else
1459  G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4" << G4endl;
1460#endif
1461#else
1462  QPrinter printer;
1463  //  printer.setPageSize(pageSize);
1464  if (aInColor == 1) {
1465    printer.setColorMode(QPrinter::GrayScale);
1466  } else {
1467    printer.setColorMode(QPrinter::Color);
1468  }
1469
1470  if (aFilename.endsWith(".ps")) {
1471#if QT_VERSION > 0x040200
1472    printer.setOutputFormat(QPrinter::PostScriptFormat);
1473#endif
1474  } else {
1475#if QT_VERSION > 0x040100
1476    printer.setOutputFormat(QPrinter::PdfFormat);
1477#endif
1478  }
1479#if QT_VERSION > 0x040100
1480  printer.setOutputFileName(aFilename);
1481#endif
1482  //  printer.setFullPage ( true);
1483  QPainter paint(&printer);
1484  paint.drawImage (0,0,aImage );
1485  paint.end();
1486#endif
1487  return true;
1488}
1489
1490#endif
1491
1492/*
1493
1494void MultiLayer::exportToSVG(const QString& fname)
1495{
1496  QPicture picture;
1497  QPainter p(&picture);
1498  for (int i=0;i<(int)graphsList->count();i++)
1499    {
1500      Graph *gr=(Graph *)graphsList->at(i);
1501      Plot *myPlot= (Plot *)gr->plotWidget();
1502     
1503      QPoint pos=gr->pos();
1504     
1505      int width=int(myPlot->frameGeometry().width());
1506      int height=int(myPlot->frameGeometry().height());
1507     
1508      myPlot->print(&p, QRect(pos,QSize(width,height)));
1509    }
1510 
1511  p.end();
1512  picture.save(fname, "svg");
1513}
1514*/
Note: See TracBrowser for help on using the repository browser.