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

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

r642@mac-90108: laurentgarnier | 2007-11-12 18:27:57 +0100
modif pour qt3

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