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

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

maj a jour par rapport au repository de geant4

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