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

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

modif pour les formats d export

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