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

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

r615@mac-90108: laurentgarnier | 2007-09-26 15:32:59 +0200
ok pour le moment avec alpha, mais tout les cas ne sont pas geres

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