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

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

r617@mac-90108: laurentgarnier | 2007-09-26 18:41:06 +0200
eps ok

  • Property svn:mime-type set to text/cpp
File size: 32.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.17 2006/06/29 21:19:36 gunter Exp $
28// GEANT4 tag $Name: geant4-08-01-patch-01 $
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 <QtGui/qboxlayout.h>
53#include <QtGui/qdialog.h>
54#include <QtGui/qmenu.h>
55#include <QImageWriter>
56#include <QMessageBox>
57#include <QFileDialog.h>
58#include <QPrinter.h>
59#include <QPainter.h>
60
61//////////////////////////////////////////////////////////////////////////////
62/**
63   Implementation of virtual method of G4VViewer
64*/
65void G4OpenGLQtViewer::SetView (
66)
67//////////////////////////////////////////////////////////////////////////////
68//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
69{
70  printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
71  //   if(!fHDC) return;
72  //   if(!fHGLRC) return;
73  //   ::wglMakeCurrent(fHDC,fHGLRC);
74  //  fWindow->makeCurrent();
75  G4OpenGLViewer::SetView ();
76  printf("G4OpenGLQtViewer::SetView --------------------\n");
77}
78
79
80
81//////////////////////////////////////////////////////////////////////////////
82/**
83   Implementation of virtual method of G4VViewer
84*/
85void G4OpenGLQtViewer::ShowView (
86)
87//////////////////////////////////////////////////////////////////////////////
88//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
89{
90  printf("G4OpenGLQtViewer::ShowView  +++++++++++++++++++++\n");
91  glFlush ();
92  if (!GLWindow) {
93    G4cerr << "Visualization window not defined, please choose one before" << G4endl;
94  } else {
95    GLWindow->activateWindow();
96    printf("G4OpenGLQtViewer::ShowView -----------------------\n");
97  }
98  //   // Empty the Windows message queue :
99  //   MSG event;
100  //   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
101  //     ::TranslateMessage(&event);
102  //     ::DispatchMessage (&event);
103  //   }
104}
105
106
107
108//////////////////////////////////////////////////////////////////////////////
109void G4OpenGLQtViewer::CreateGLQtContext (
110)
111//////////////////////////////////////////////////////////////////////////////
112//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
113{
114  printf("G4OpenGLQtViewer::CreateGLQtContext \n");
115}
116
117
118//////////////////////////////////////////////////////////////////////////////
119void G4OpenGLQtViewer::CreateMainWindow (
120 QGLWidget* glWidget
121)
122//////////////////////////////////////////////////////////////////////////////
123//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
124{
125
126  if(fWindow) return; //Done.
127  printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
128
129  // launch Qt if not
130  G4Qt* interactorManager = G4Qt::getInstance ();
131  //  G4UImanager* UI = G4UImanager::GetUIpointer();
132
133  fWindow = glWidget ;
134  //  fWindow->makeCurrent();
135
136  // create window
137  if (((QApplication*)interactorManager->GetMainInteractor())) {
138    // look for the main window
139    bool found = false;
140    foreach (QWidget *widget, QApplication::allWidgets()) {
141      if ((found== false) && (widget->inherits("QMainWindow"))) {
142        printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
143        GLWindow = new QDialog(widget,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
144        found = true;
145      }
146    }
147    if (found==false) {
148      printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
149      GLWindow = new QDialog();
150    }
151  } else {
152    printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
153    GLWindow = new QDialog();
154  }
155
156  QHBoxLayout *mainLayout = new QHBoxLayout;
157
158  mainLayout->addWidget(fWindow);
159  GLWindow->setLayout(mainLayout);
160  GLWindow->setWindowTitle("QGl Viewer");
161  GLWindow->resize(300, 300);
162  GLWindow->move(900,300);
163  GLWindow->show();
164 
165  // delete the pointer if close this
166  //  GLWindow->setAttribute(Qt::WA_DeleteOnClose);
167
168  QObject ::connect(GLWindow,
169                    SIGNAL(rejected()),
170                    this,
171                    SLOT(dialogClosed()));
172
173  WinSize_x = 400;
174  WinSize_y = 400;
175  if (WinSize_x < fVP.GetWindowSizeHintX ())
176    WinSize_x = fVP.GetWindowSizeHintX ();
177  if (WinSize_y < fVP.GetWindowSizeHintY ())
178    WinSize_y = fVP.GetWindowSizeHintY ();
179
180  if(!fWindow) return;
181  printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
182
183  if (!fContextMenu)
184    createPopupMenu();
185
186}
187
188/**  Close the dialog and set the pointer to NULL
189 */
190void G4OpenGLQtViewer::dialogClosed() {
191  GLWindow = NULL;
192}
193
194
195//////////////////////////////////////////////////////////////////////////////
196G4OpenGLQtViewer::G4OpenGLQtViewer (
197                                    G4OpenGLSceneHandler& scene
198                                    )
199  :G4VViewer (scene, -1)
200  ,G4OpenGLViewer (scene)
201  ,fWindow(0)
202  ,fContextMenu(0)
203  ,fMouseAction(true)
204{
205  printf("G4OpenGLQtViewer::G4OpenGLQtViewer \n");
206}
207
208//////////////////////////////////////////////////////////////////////////////
209G4OpenGLQtViewer::~G4OpenGLQtViewer (
210)
211//////////////////////////////////////////////////////////////////////////////
212//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
213{
214  printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
215  delete fContextMenu;
216}
217
218
219/**
220   Create a popup menu for the widget. This menu is activated by right-mouse click
221*/
222void G4OpenGLQtViewer::createPopupMenu()    {
223
224  fContextMenu = new QMenu("All");
225
226  QMenu *mMouseAction = fContextMenu->addMenu("&Mouse action");
227  QAction *rotate = mMouseAction->addAction("&Rotate scene");
228  QAction *move = mMouseAction->addAction("&Move scene");
229  // INIT mMouse
230  createRadioAction(rotate,move,SLOT(toggleMouseAction(bool)),1);
231
232  //Style Menu
233  QMenu *mStyle = fContextMenu->addMenu("&Style");
234
235  QMenu *mRepresentation = mStyle->addMenu("&Representation");
236  QAction *polyhedron = mRepresentation->addAction("Polyhedron");
237  QAction *nurbs = mRepresentation->addAction("NURBS");
238  // INIT mStyle
239  G4ViewParameters::RepStyle style;
240  style = fVP.GetRepStyle();
241  if (style == G4ViewParameters::polyhedron) {
242    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
243  } else if (style == G4ViewParameters::nurbs) {
244    createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
245  } else {
246    mRepresentation->clear();
247  }
248
249
250  QMenu *mDrawing = mStyle->addMenu("&Drawing");
251  fDrawingWireframe = mDrawing->addAction("Wireframe");
252  fDrawingWireframe->setCheckable(true);
253  fDrawingWireframe->setChecked(true);
254  fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
255  fDrawingLineRemoval->setCheckable(true);
256  fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
257  fDrawingSurfaceRemoval->setCheckable(true);
258  fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
259  fDrawingLineSurfaceRemoval->setCheckable(true);
260  // INIT Drawing
261  G4ViewParameters::DrawingStyle d_style;
262  d_style = fVP.GetDrawingStyle();
263 
264  fDrawingWireframe->setCheckable(true);
265  fDrawingLineRemoval->setCheckable(true);
266  fDrawingSurfaceRemoval->setCheckable(true);
267  fDrawingLineSurfaceRemoval->setCheckable(true);
268
269  if (d_style == G4ViewParameters::wireframe) {
270    fDrawingWireframe->setChecked(true);
271  } else if (d_style == G4ViewParameters::hlr) {
272    fDrawingLineRemoval->setChecked(true);
273  } else if (d_style == G4ViewParameters::hsr) {
274    fDrawingSurfaceRemoval->setChecked(true);
275  } else if (d_style == G4ViewParameters::hlhsr) {
276    fDrawingLineSurfaceRemoval->setChecked(true);
277  } else {
278    mDrawing->clear();
279  }
280
281  QObject ::connect(fDrawingWireframe,
282                    SIGNAL(triggered(bool)),
283                    this,
284                    SLOT(actionDrawingWireframe()));
285  QObject ::connect(fDrawingLineRemoval,
286                    SIGNAL(triggered(bool)),
287                    this,
288                    SLOT(actionDrawingLineRemoval()));
289  QObject ::connect(fDrawingSurfaceRemoval,
290                    SIGNAL(triggered(bool)),
291                    this,
292                    SLOT(actionDrawingSurfaceRemoval()));
293  QObject ::connect(fDrawingLineSurfaceRemoval,
294                    SIGNAL(triggered(bool)),
295                    this,
296                    SLOT(actionDrawingLineSurfaceRemoval()));
297
298
299  QMenu *mBackground = mStyle->addMenu("&Background color");
300  QAction *white = mBackground->addAction("White");
301  QAction *black = mBackground->addAction("Black");
302  if (background.GetRed() == 1. &&
303      background.GetGreen() == 1. &&
304      background.GetBlue() == 1.) {
305    createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
306  } else {
307    createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
308  }
309
310
311  // Action Menu
312  QMenu *mActions = fContextMenu->addMenu("&Actions");
313  QAction *controlPanels = mActions->addAction("Control panels");
314  QAction *exitG4 = mActions->addAction("Exit to G4Vis >");
315  QAction *createEPS = mActions->addAction("Save as ...");
316  QObject ::connect(controlPanels,
317                    SIGNAL(triggered()),
318                    this,
319                    SLOT(actionControlPanels()));
320  QObject ::connect(exitG4,
321                    SIGNAL(triggered()),
322                    this,
323                    SLOT(actionExitG4()));
324  QObject ::connect(createEPS,
325                    SIGNAL(triggered()),
326                    this,
327                    SLOT(actionCreateEPS()));
328
329
330  // Special Menu
331  QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
332  QMenu *transparency = mSpecial->addMenu("Transparency");
333  QAction *transparencyOn = transparency->addAction("On");
334  QAction *transparencyOff = transparency->addAction("Off");
335  if (transparency_enabled == false) {
336    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
337  } else if (transparency_enabled == true) {
338    createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
339  } else {
340    mSpecial->clear();
341  }
342
343
344  QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
345  QAction *antialiasingOn = mAntialiasing->addAction("On");
346  QAction *antialiasingOff = mAntialiasing->addAction("Off");
347  if (antialiasing_enabled == false) {
348    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
349  } else if (antialiasing_enabled == true) {
350    createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
351  } else {
352    mAntialiasing->clear();
353  }
354
355  QMenu *mHaloing = mSpecial->addMenu("Haloing");
356  QAction *haloingOn = mHaloing->addAction("On");
357  QAction *haloingOff = mHaloing->addAction("Off");
358  if (haloing_enabled == false) {
359    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
360  } else if (haloing_enabled == true) {
361    createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
362  } else {
363    mHaloing->clear();
364  }
365
366  QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
367  QAction *auxOn = mAux->addAction("On");
368  QAction *auxOff = mAux->addAction("Off");
369  if (!fVP.IsAuxEdgeVisible()) {
370    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
371  } else {
372    createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
373  }
374
375
376  QMenu *mFullScreen = mSpecial->addMenu("Full screen");
377  QAction *fullOn = mFullScreen->addAction("On");
378  QAction *fullOff = mFullScreen->addAction("Off");
379  createRadioAction(fullOn,fullOff,SLOT(toggleFullScreen(bool)),2);
380
381}
382
383void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
384{
385  if (!GLWindow) {
386    G4cerr << "Visualization window not defined, please choose one before" << G4endl;
387  } else {
388 
389    if (!fContextMenu)
390      createPopupMenu();
391   
392    // launch menu
393    if ( fContextMenu ) {
394      fContextMenu->exec( e->globalPos() );
395      //    delete fContextMenu;
396    }
397  }
398  e->accept();
399}
400
401
402/**
403   Create a radio button menu. The two menu will be connected. When click on one,
404   eatch state will be invert and callback method will be called.
405   @param action1 first action to connect
406   @param action2 second action to connect
407   @param method callback method
408   @param nCheck: 1 : first action will be set true. 2 : second action will be set true
409*/
410void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
411
412  action1->setCheckable(true);
413  action2->setCheckable(true);
414
415  if (nCheck ==1)
416    action1->setChecked (true);
417  else
418    action2->setChecked (true);
419   
420  QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
421  QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
422
423  QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
424
425}
426
427/**
428   Slot activate when drawing->wireframe menu is set
429 */
430void G4OpenGLQtViewer::actionDrawingWireframe() {
431  emit toggleDrawingAction(1);
432}
433
434/**
435   Slot activate when drawing->line removal menu is set
436 */
437void G4OpenGLQtViewer::actionDrawingLineRemoval() {
438  emit toggleDrawingAction(2);
439}
440
441/**
442   Slot activate when drawing->surface removal menu is set
443 */
444void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
445  emit toggleDrawingAction(3);
446}
447
448/**
449   Slot activate when drawing->wireframe menu is set
450 */
451void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
452  emit toggleDrawingAction(4);
453}
454
455
456/**
457   Slot activated when drawing menu is toggle
458   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
459   KernelVisitDecision () will be call and will set the fNeedKernelVisit
460   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
461   It will cause a redraw of the view
462   @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
463   @see G4OpenGLStoredQtViewer::DrawView
464   @see G4XXXStoredViewer::CompareForKernelVisit
465 */
466void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
467
468  G4ViewParameters::DrawingStyle d_style;
469 
470
471  if (aAction ==1) {
472    fDrawingWireframe->setChecked (true);
473    fDrawingLineRemoval->setChecked (false);
474    fDrawingSurfaceRemoval->setChecked (false);
475    fDrawingLineSurfaceRemoval->setChecked (false);
476
477    d_style = G4ViewParameters::wireframe;
478
479  } else  if (aAction ==2) {
480    fDrawingWireframe->setChecked (false);
481    fDrawingLineRemoval->setChecked (true);
482    fDrawingSurfaceRemoval->setChecked (false);
483    fDrawingLineSurfaceRemoval->setChecked (false);
484
485    d_style = G4ViewParameters::hlr;
486
487  } else  if (aAction ==3) {
488    fDrawingWireframe->setChecked (false);
489    fDrawingLineRemoval->setChecked (false);
490    fDrawingSurfaceRemoval->setChecked (true);
491    fDrawingLineSurfaceRemoval->setChecked (false);
492
493    d_style = G4ViewParameters::hsr;
494
495  } else  if (aAction ==4) {
496    fDrawingWireframe->setChecked (false);
497    fDrawingLineRemoval->setChecked (false);
498    fDrawingSurfaceRemoval->setChecked (false);
499    fDrawingLineSurfaceRemoval->setChecked (true);
500
501    d_style = G4ViewParameters::hlhsr;
502  }
503  fVP.SetDrawingStyle(d_style);
504
505  updateQWidget();
506  printf("G4OpenGLQtViewer::toggleDrawingAction\n");
507}
508
509
510/**
511   SLOT Activate by a click on the representation menu
512   Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
513   KernelVisitDecision () will be call and will set the fNeedKernelVisit
514   to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
515   It will cause a redraw of the view
516   @param check : 1 polyhedron, 0 nurbs
517   @see G4OpenGLStoredQtViewer::DrawView
518   @see G4XXXStoredViewer::CompareForKernelVisit
519*/
520void G4OpenGLQtViewer::toggleRepresentation(bool check) {
521
522  G4ViewParameters::RepStyle style;
523  if (check == 1) {
524    style = G4ViewParameters::polyhedron;
525  } else {
526    style = G4ViewParameters::nurbs;
527  }
528  fVP.SetRepStyle (style);
529
530  printf("G4OpenGLQtViewer::toggleRepresentation 3%d\n",check);
531  updateQWidget();
532  printf("G4OpenGLQtViewer::toggleRepresentation 4%d\n",check);
533}
534
535/**
536   SLOT Activate by a click on the background menu
537@param check : 1 white, 0 black
538*/
539void G4OpenGLQtViewer::toggleBackground(bool check) {
540
541  //   //I need to revisit the kernel if the background colour changes and
542  //   //hidden line removal is enabled, because hlr drawing utilises the
543  //   //background colour in its drawing...
544  //   // (Note added by JA 13/9/2005) Background now handled in view
545  //   // parameters.  A kernel visit is triggered on change of background.
546  if (check == 1) {
547    ((G4ViewParameters&)this->GetViewParameters()).
548      SetBackgroundColour(G4Colour(1.,1.,1.));  // White
549  } else {
550    ((G4ViewParameters&)this->GetViewParameters()).
551      SetBackgroundColour(G4Colour(0.,0.,0.));  // Black
552  }
553  updateQWidget();
554}
555
556/**
557   SLOT Activate by a click on the transparency menu
558@param check : 1 , 0
559*/
560void G4OpenGLQtViewer::toggleTransparency(bool check) {
561 
562  if (check) {
563    transparency_enabled = false;
564  } else {
565    transparency_enabled = true;
566  }
567  SetNeedKernelVisit (true);
568  updateQWidget();
569}
570
571/**
572   SLOT Activate by a click on the antialiasing menu
573@param check : 1 , 0
574*/
575void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
576
577  if (!check) {
578    antialiasing_enabled = false;
579    glDisable (GL_LINE_SMOOTH);
580    glDisable (GL_POLYGON_SMOOTH);
581  } else {
582    antialiasing_enabled = true;
583    glEnable (GL_LINE_SMOOTH);
584    glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
585    glEnable (GL_POLYGON_SMOOTH);
586    glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
587  }
588
589  updateQWidget();
590  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
591}
592
593/**
594   SLOT Activate by a click on the haloing menu
595@param check : 1 , 0
596*/
597//FIXME : I SEE NOTHING...
598void G4OpenGLQtViewer::toggleHaloing(bool check) {
599  if (check) {
600    haloing_enabled = false;
601  } else {
602    haloing_enabled = true;
603  }
604
605  updateQWidget();
606
607  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
608}
609
610/**
611   SLOT Activate by a click on the auxiliaire edges menu
612@param check : 1 , 0
613*/
614void G4OpenGLQtViewer::toggleAux(bool check) {
615  if (check) {
616    fVP.SetAuxEdgeVisible(false);
617  } else {
618    fVP.SetAuxEdgeVisible(true);
619  }
620  SetNeedKernelVisit (true);
621  updateQWidget();
622
623  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
624}
625
626/**
627   SLOT Activate by a click on the full screen menu
628@param check : 1 , 0
629*/
630void G4OpenGLQtViewer::toggleFullScreen(bool check) {
631  GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
632
633  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
634}
635
636/**
637   SLOT Activate by a click on the mouse action menu
638   @param check : 1 , 0
639*/
640void G4OpenGLQtViewer::toggleMouseAction(bool check) {
641  if (check) { // rotate scene
642    fMouseAction = true;
643  } else { // move scene
644    fMouseAction = false;
645  }
646
647  printf("G4OpenGLQtViewer::toggleRepresentation %d\n",check);
648}
649
650
651void G4OpenGLQtViewer::actionControlPanels() {
652  printf("G4OpenGLQtViewer::actionControlPanels \n");
653}
654
655void G4OpenGLQtViewer::actionExitG4() {
656  printf("G4OpenGLQtViewer::actionExitG4() \n");
657}
658
659void G4OpenGLQtViewer::actionCreateEPS() {
660  QString filters;
661  QList<QByteArray> formats =  QImageWriter::supportedImageFormats ();
662  for (int i = 0; i < formats.size(); ++i) {
663    filters +=formats.at(i)+";;";
664    //    if ((i+1) <formats.size()) {
665    //      filters += ";;";
666    //    }
667  }
668  filters += "eps;;";
669  filters += "ps;;";
670  filters += "pdf";
671  QString* selectedFilter = new QString();
672  QString nomFich =  QFileDialog::getSaveFileName ( GLWindow,
673                                                    tr("Save as ..."),
674                                                    ".",
675                                                    filters,
676                                                    selectedFilter );
677  // bmp jpg jpeg png ppm xbm xpm
678  if (nomFich == "") {
679    return;
680  }
681  nomFich += "."+selectedFilter->toLower();
682  printf("G4OpenGLQtViewer::name %s\n",nomFich.toStdString().c_str());
683  G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,nomFich,fWindow->height(),fWindow->width());
684  if(  exportDialog->exec()) {
685   
686    printf("rescaling before\n");
687    //    QPixmap * pixmap = new QPixmap(fWindow->renderPixmap (exportDialog->getWidth(),exportDialog->getHeight() )) ;
688    QImage image = fWindow->grabFrameBuffer();
689    printf("rescaling after\n");
690     
691   
692    if ((exportDialog->getWidth() !=fWindow->width()) ||
693         (exportDialog->getWidth() !=fWindow->width())) {
694
695      //      rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
696      printf("rescaling\n");
697    }
698   
699    // jpeg format
700    if (nomFich.endsWith(".jpg") ||
701        nomFich.endsWith(".jpeg")) {
702      // grabFrameBuffer() :: Returns an image of the frame buffer. If withAlpha is true the alpha channel is included.
703      image.save(nomFich,0,exportDialog->getSliderValue());
704      printf("saving jpeg quality : %d\n",exportDialog->getSliderValue());
705    } else if (nomFich.endsWith(".eps")) {
706      generateEPS(nomFich,exportDialog->getNbColor(),image);
707    } else if (nomFich.endsWith(".ps") ||nomFich.endsWith(".pdf")) {
708      generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
709    } else if (nomFich.endsWith(".tif") ||
710               nomFich.endsWith(".tiff") ||
711               nomFich.endsWith(".jpg") ||
712               nomFich.endsWith(".png") ||
713               nomFich.endsWith(".bmp") ||
714               nomFich.endsWith(".xpm")) {
715      image.save(nomFich,0,exportDialog->getSliderValue());
716      printf("saving ELSE\n");
717    } else {
718      G4cerr << "This version of G4UI Could not generate the selected format" << G4endl;
719    }
720   
721  } else { // cancel selected
722    return;
723  }
724 
725  printf("G4OpenGLQtViewer::actionCreateEPS() \n");
726}
727
728/*
729// 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
730
731void Graph::exportToSVG(const QString& fname)
732{
733  // enable workaround for Qt3 misalignments
734  QwtPainter::setSVGMode(true);
735  QPicture picture;
736  QPainter p(&picture);
737  d_plot->print(&p, d_plot->rect());
738  p.end();
739
740  picture.save(fname, "svg");
741}
742*/
743
744
745
746
747/**
748   Save the current mouse press point
749   @param p mouse click point
750*/
751void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
752{
753  lastPos = p;
754}
755
756/**
757   @param pos_x mouse x position
758   @param pos_y mouse y position
759   @param mButtons mouse button active
760*/
761void G4OpenGLQtViewer::G4MouseMoveEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons)
762{
763  int dx = pos_x - lastPos.x();
764  int dy = pos_y - lastPos.y();
765 
766  if (fMouseAction) {  // rotate
767    if (mButtons & Qt::LeftButton) {
768      //phi spin stuff here
769     
770      G4Vector3D vp = fVP.GetViewpointDirection ().unit ();
771      G4Vector3D up = fVP.GetUpVector ().unit ();
772     
773      G4Vector3D yprime = (up.cross(vp)).unit();
774      G4Vector3D zprime = (vp.cross(yprime)).unit();
775     
776      G4double delta_alpha;
777      G4double delta_theta;
778     
779      if (fVP.GetLightsMoveWithCamera()) {
780        delta_alpha = dy;
781        delta_theta = -dx;
782      } else {
783        delta_alpha = -dy;
784        delta_theta = dx;
785      }   
786
787      delta_alpha *= deg;
788      delta_theta *= deg;
789
790      G4Vector3D new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
791     
792      G4Vector3D new_up;
793      if (fVP.GetLightsMoveWithCamera()) {
794        new_up = (new_vp.cross(yprime)).unit();
795        fVP.SetUpVector(new_up);
796      } else {
797        new_up = up;
798      }
799      ////////////////
800      // Rotates by fixed azimuthal angle delta_theta.
801
802      G4double cosalpha = new_up.dot (new_vp.unit());
803      G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
804      yprime = (new_up.cross (new_vp.unit())).unit ();
805      G4Vector3D xprime = yprime.cross (new_up);
806      // Projection of vp on plane perpendicular to up...
807      G4Vector3D a1 = sinalpha * xprime;
808      // Required new projection...
809      G4Vector3D a2 =
810        sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
811      // Required Increment vector...
812      G4Vector3D delta = a2 - a1;
813      // So new viewpoint is...
814      G4Vector3D viewPoint = new_vp.unit() + delta;
815
816      fVP.SetViewAndLights (viewPoint);
817      updateQWidget();
818     
819    } else if (mButtons & Qt::RightButton) {
820      // NEVER DONE BECAUSE OF MOUSE MENU
821      //       printf("G4OpenGLQtViewer::mouseMoveEvent Right \n");
822      //       setXRotation(xRot + dy/2);
823      //       setZRotation(zRot + dx/2);
824      //       updateQWidget();
825    }
826  } else {  // move
827
828    float dx = pos_x - lastPos.x();
829    float dy = pos_y - lastPos.y();
830   
831    G4Point3D stp
832      = GetSceneHandler()->GetScene()->GetStandardTargetPoint();
833   
834    G4Point3D tp = stp + fVP.GetCurrentTargetPoint ();
835   
836    const G4Vector3D& upVector = fVP.GetUpVector ();
837    const G4Vector3D& vpVector = fVP.GetViewpointDirection ();
838   
839    G4Vector3D unitRight = (upVector.cross (vpVector)).unit();
840    G4Vector3D unitUp    = (vpVector.cross (unitRight)).unit();
841   
842    tp += -dx * unitRight + dy * unitUp;
843    fVP.SetCurrentTargetPoint (tp - stp);
844   
845    updateQWidget();
846  }
847  lastPos = QPoint(pos_x, pos_y);
848}
849
850void G4OpenGLQtViewer::rescaleImage(
851 int aWidth
852,int aHeight
853){
854  printf("should rescale \n");
855}
856
857/**
858   Generate Postscript form image
859   @param aFilename : name of file
860   @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
861   @param aImage : Image to print
862*/
863bool G4OpenGLQtViewer::generateEPS (
864 QString aFilename
865,int aInColor
866,QImage aImage
867)
868{
869  // FIXME
870  printf("saving EPS\n");
871
872  FILE* fp;
873  aInColor = 3;
874
875  const uchar * pixels = aImage.bits ();
876   
877  if (pixels == NULL)
878    return false;
879 
880  fp = fopen (aFilename.toStdString().c_str(), "w");
881  if (fp == NULL) {
882    return false;
883  }
884 
885  fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
886  fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
887  fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
888  fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
889  fprintf (fp, "%%%%EndComments\n");
890  fprintf (fp, "gsave\n");
891  fprintf (fp, "/bwproc {\n");
892  fprintf (fp, "    rgbproc\n");
893  fprintf (fp, "    dup length 3 idiv string 0 3 0 \n");
894  fprintf (fp, "    5 -1 roll {\n");
895  fprintf (fp, "    add 2 1 roll 1 sub dup 0 eq\n");
896  fprintf (fp, "    { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
897  fprintf (fp, "       3 1 roll 5 -1 roll } put 1 add 3 0 \n");
898  fprintf (fp, "    { 2 1 roll } ifelse\n");
899  fprintf (fp, "    }forall\n");
900  fprintf (fp, "    pop pop pop\n");
901  fprintf (fp, "} def\n");
902  fprintf (fp, "systemdict /colorimage known not {\n");
903  fprintf (fp, "   /colorimage {\n");
904  fprintf (fp, "       pop\n");
905  fprintf (fp, "       pop\n");
906  fprintf (fp, "       /rgbproc exch def\n");
907  fprintf (fp, "       { bwproc } image\n");
908  fprintf (fp, "   }  def\n");
909  fprintf (fp, "} if\n");
910  fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
911  fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
912  fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
913  fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
914  fprintf (fp, "{currentfile picstr readhexstring pop}\n");
915  fprintf (fp, "false %d\n", aInColor);
916  fprintf (fp, "colorimage\n");
917 
918
919  int width = aImage.width();
920  int height = aImage.height();
921  int depth = aImage.depth();
922  int size = width*height;
923 
924  if (depth == 1)
925    size = (width+7)/8*height;
926  else if (aInColor == 1)
927    size = size*3;
928 
929  int i = 0;
930  if (depth == 1) {
931    //  To be implemented
932    //    QImage::Endian bitOrder = aImage.bitOrder();
933    /*    for(int y=0; y < height; y++) {
934      const uchar * s = aImage.scanLine(y);
935      for(int x=0; x < width; x++) {
936        // need to copy bit for bit...
937        bool b = (bitOrder == QImage::LittleEndian) ?
938          (*(s + (x >> 3)) >> (x & 7)) & 1 :
939          (*(s + (x >> 3)) << (x & 7)) & 0x80 ;
940        if (b)
941          pixel[i >> 3] ^= (0x80 >> (i & 7));
942        i++;
943      }
944      // we need to align to 8 bit here
945      i = (i+7) & 0xffffff8;
946    }
947    */
948  } else if (depth == 8) {
949    printf("has 8 bit\n");
950    for(int y=height-1; y >=0 ; y--) {
951      const uchar * s = aImage.scanLine(y);
952      for(int x=0; x <width; x++) {
953        QRgb rgb = aImage.color(s[x]);
954        if (aInColor == 1) {
955          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
956          i++;
957        } else {
958          fprintf (fp, " %02hx %02hx %02hx",
959                   (unsigned char) qRed(rgb),
960                   (unsigned char) qGreen(rgb),
961                   (unsigned char) qBlue(rgb));
962          i += 3;
963        }
964      }
965      fprintf (fp, "\n");
966    }
967  } else {
968    bool alpha = aImage.hasAlphaChannel();
969    printf("has else %d alpha %d\n",depth,alpha);
970    for(int y=height-1; y >=0 ; y--) {
971      QRgb * s = (QRgb*)(aImage.scanLine(y));
972      for(int x=0; x <width; x++) {
973        QRgb rgb = (*s++);
974        if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
975          rgb = qRgb(0xff, 0xff, 0xff);
976        if (aInColor == 1) {
977          fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
978          i++;
979        } else {
980          fprintf (fp, " %02hx %02hx %02hx",
981                   (unsigned char) qRed(rgb),
982                   (unsigned char) qGreen(rgb),
983                   (unsigned char) qBlue(rgb));
984          i += 3;
985        }
986      }
987      fprintf (fp, "\n");
988    }
989  }
990
991  fprintf (fp, "grestore\n");
992  fprintf (fp, "showpage\n");
993  fclose (fp);
994
995  return true;
996}
997/**
998   Generate Postscript or PDF form image
999   @param aFilename : name of file
1000   @param aInColor : numbers of colors : 1->BW 2->RGB
1001   @param aImage : Image to print
1002*/
1003bool G4OpenGLQtViewer::generatePS_PDF (
1004 QString aFilename
1005,int aInColor
1006,QImage aImage
1007)
1008{
1009  QPrinter printer;
1010  //  printer.setPageSize(pageSize);
1011  if (aInColor == 1) {
1012    printer.setColorMode(QPrinter::GrayScale);
1013  } else {
1014    printer.setColorMode(QPrinter::Color);
1015  }
1016
1017  if (aFilename.endsWith(".ps")) {
1018    printer.setOutputFormat(QPrinter::PostScriptFormat);
1019  } else {
1020    printer.setOutputFormat(QPrinter::PdfFormat);
1021  }
1022  printer.setOutputFileName(aFilename);
1023  //  printer.setFullPage ( true);
1024  QPainter paint(&printer);
1025  paint.drawImage (0,0,aImage );
1026  paint.end();
1027  return true;
1028}
1029
1030#endif
1031
1032/*
1033
1034void MultiLayer::exportToSVG(const QString& fname)
1035{
1036  QPicture picture;
1037  QPainter p(&picture);
1038  for (int i=0;i<(int)graphsList->count();i++)
1039    {
1040      Graph *gr=(Graph *)graphsList->at(i);
1041      Plot *myPlot= (Plot *)gr->plotWidget();
1042     
1043      QPoint pos=gr->pos();
1044     
1045      int width=int(myPlot->frameGeometry().width());
1046      int height=int(myPlot->frameGeometry().height());
1047     
1048      myPlot->print(&p, QRect(pos,QSize(width,height)));
1049    }
1050 
1051  p.end();
1052  picture.save(fname, "svg");
1053}
1054*/
Note: See TracBrowser for help on using the repository browser.