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

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

r670@mac-90108: laurentgarnier | 2007-11-26 18:13:28 +0100
mise a jour pour Qt3 linux

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