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

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

r657@mac-90108: laurentgarnier | 2007-11-15 19:44:52 +0100
ajout d un flag pour debug

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