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

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

mise a jour par rapport au commit sur cvs de geant4

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