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

Last change on this file since 707 was 707, checked in by garnier, 18 years ago

modifs en cours...step de deplacement dans G4OpenGLQtViewer.ccc

  • Property svn:mime-type set to text/cpp
File size: 55.3 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.10 2008/01/30 10:54:13 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#define GEANT4_QT_DEBUG
39
40#include "G4OpenGLQtViewer.hh"
41
42#include "G4ios.hh"
43#include "G4VisExtent.hh"
44#include "G4LogicalVolume.hh"
45#include "G4VSolid.hh"
46#include "G4Point3D.hh"
47#include "G4Normal3D.hh"
48#include "G4Scene.hh"
49#include "G4OpenGLQtExportDialog.hh"
50#include "G4UnitsTable.hh"
51#include "G4Qt.hh"
52#include "G4UImanager.hh"
53#include "G4UIcommandTree.hh"
54#include <qapplication.h>
55#include <qlayout.h>
56#include <qdialog.h>
57
58#if QT_VERSION >= 0x040000
59#include <qmenu.h>
60#include <qimagewriter.h>
61#else
62#include <qaction.h>
63#include <qwidgetlist.h>
64#include <qpopupmenu.h>
65#include <qimage.h>
66#endif
67
68#include <qmessagebox.h>
69#include <qfiledialog.h>
70#include <qprinter.h>
71#include <qpainter.h>
72#include <qgl.h> // include <qglwidget.h>
73#include <qdialog.h>
74#include <qevent.h> //include <qcontextmenuevent.h>
75
76
77//////////////////////////////////////////////////////////////////////////////
78/**
79 Implementation of virtual method of G4VViewer
80*/
81void G4OpenGLQtViewer::SetView (
82)
83//////////////////////////////////////////////////////////////////////////////
84//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
85{
86#ifdef GEANT4_QT_DEBUG
87 printf("G4OpenGLQtViewer::SetView ++++++++++++++++++++\n");
88#endif
89 // if(!fHDC) return;
90 // if(!fHGLRC) return;
91 // ::wglMakeCurrent(fHDC,fHGLRC);
92 // fWindow->makeCurrent();
93 G4OpenGLViewer::SetView ();
94#ifdef GEANT4_QT_DEBUG
95 printf("G4OpenGLQtViewer::SetView --------------------\n");
96#endif
97}
98
99/**
100 * Set the viewport of the scene
101 */
102void G4OpenGLQtViewer::setupViewport(int aWidth, int aHeight)
103{
104 int side = qMin(aWidth, aHeight);
105 glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
106
107 glMatrixMode(GL_PROJECTION);
108 glLoadIdentity();
109 glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
110 glMatrixMode(GL_MODELVIEW);
111}
112
113
114//////////////////////////////////////////////////////////////////////////////
115/**
116 Implementation of virtual method of G4VViewer
117*/
118void G4OpenGLQtViewer::ShowView (
119)
120//////////////////////////////////////////////////////////////////////////////
121//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
122{
123#ifdef GEANT4_QT_DEBUG
124 printf("G4OpenGLQtViewer::ShowView +++++++++++++++++++++\n");
125#endif
126 if (!GLWindow) {
127 G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
128 } else {
129#if QT_VERSION < 0x040000
130 GLWindow->setActiveWindow();
131#else
132 GLWindow->activateWindow();
133#endif
134#ifdef GEANT4_QT_DEBUG
135 printf("G4OpenGLQtViewer::ShowView -----------------------\n");
136#endif
137 }
138 glFlush ();
139 // // Empty the Windows message queue :
140 // MSG event;
141 // while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
142 // ::TranslateMessage(&event);
143 // ::DispatchMessage (&event);
144 // }
145}
146
147
148
149//////////////////////////////////////////////////////////////////////////////
150void G4OpenGLQtViewer::CreateGLQtContext (
151)
152//////////////////////////////////////////////////////////////////////////////
153//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
154{
155#ifdef GEANT4_QT_DEBUG
156 printf("G4OpenGLQtViewer::CreateGLQtContext \n");
157#endif
158}
159
160
161//////////////////////////////////////////////////////////////////////////////
162void G4OpenGLQtViewer::CreateMainWindow (
163 QGLWidget* glWidget
164)
165//////////////////////////////////////////////////////////////////////////////
166//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
167{
168
169 if(fWindow) return; //Done.
170#ifdef GEANT4_QT_DEBUG
171 printf("G4OpenGLQtViewer::CreateMainWindow glWidget\n");
172#endif
173
174 // launch Qt if not
175 G4Qt* interactorManager = G4Qt::getInstance ();
176 // G4UImanager* UI = G4UImanager::GetUIpointer();
177
178 fWindow = glWidget ;
179 // fWindow->makeCurrent();
180
181 // create window
182 if (((QApplication*)interactorManager->GetMainInteractor())) {
183 // look for the main window
184 bool found = false;
185#if QT_VERSION < 0x040000
186 // theses lines does nothing exept this one "GLWindow = new QDialog(0..."
187 // but if I comment them, it doesn't work...
188 QWidgetList *list = QApplication::allWidgets();
189 QWidgetListIt it( *list ); // iterate over the widgets
190 QWidget * widget;
191 while ( (widget=it.current()) != 0 ) { // for each widget...
192 ++it;
193 if ((found== false) && (widget->inherits("QMainWindow"))) {
194 GLWindow = new QDialog(0,0,FALSE,Qt::WStyle_Title | Qt::WStyle_SysMenu | Qt::WStyle_MinMax );
195 found = true;
196 }
197 }
198 delete list; // delete the list, not the widgets
199#else
200 foreach (QWidget *widget, QApplication::allWidgets()) {
201 if ((found== false) && (widget->inherits("QMainWindow"))) {
202 GLWindow = new QDialog(0,Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
203 found = true;
204 }
205 }
206#endif
207
208#if QT_VERSION < 0x040000
209 glWidget->reparent(GLWindow,0,QPoint(0,0));
210#else
211 glWidget->setParent(GLWindow);
212#endif
213
214 if (found==false) {
215#ifdef GEANT4_QT_DEBUG
216 printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist, but not found\n");
217#endif
218 GLWindow = new QDialog();
219 }
220 } else {
221#ifdef GEANT4_QT_DEBUG
222 printf("G4OpenGLQtViewer::CreateMainWindow case Qapp exist\n");
223#endif
224 GLWindow = new QDialog();
225 }
226
227 QHBoxLayout *mainLayout = new QHBoxLayout(GLWindow);
228
229 mainLayout->addWidget(fWindow);
230
231#if QT_VERSION < 0x040000
232 GLWindow->setCaption( tr( "QGl Viewer" ));
233#else
234 GLWindow->setLayout(mainLayout);
235 GLWindow->setWindowTitle(tr("QGl Viewer"));
236#endif
237 GLWindow->resize(300, 300);
238 GLWindow->move(900,300);
239 GLWindow->show();
240
241 // delete the pointer if close this
242 // GLWindow->setAttribute(Qt::WA_DeleteOnClose);
243
244#if QT_VERSION >= 0x040000
245 QObject ::connect(GLWindow,
246 SIGNAL(rejected()),
247 this,
248 SLOT(dialogClosed()));
249#endif
250
251 WinSize_x = 400;
252 WinSize_y = 400;
253 if (WinSize_x < fVP.GetWindowSizeHintX ())
254 WinSize_x = fVP.GetWindowSizeHintX ();
255 if (WinSize_y < fVP.GetWindowSizeHintY ())
256 WinSize_y = fVP.GetWindowSizeHintY ();
257
258 if(!fWindow) return;
259#ifdef GEANT4_QT_DEBUG
260 printf("G4OpenGLQtViewer::CreateMainWindow glWidget END\n");
261#endif
262
263 if (!fContextMenu)
264 createPopupMenu();
265
266}
267
268#if QT_VERSION >= 0x040000
269/** Close the dialog and set the pointer to NULL
270 */
271void G4OpenGLQtViewer::dialogClosed() {
272#ifdef GEANT4_QT_DEBUG
273 printf("G4OpenGLQtViewer::dialogClosed END\n");
274#endif
275 // GLWindow = NULL;
276}
277#endif
278
279//////////////////////////////////////////////////////////////////////////////
280G4OpenGLQtViewer::G4OpenGLQtViewer (
281 G4OpenGLSceneHandler& scene
282 )
283 :G4VViewer (scene, -1)
284 ,G4OpenGLViewer (scene)
285 ,fWindow(0)
286 ,fContextMenu(0)
287 ,fMouseAction(STYLE1)
288 ,fDeltaSceneTranslation(1/100)
289 ,fDeltaZoom(1.1)
290 ,holdKeyEvent(false)
291{
292#ifdef GEANT4_QT_DEBUG
293 printf("G4OpenGLQtViewer::G4OpenGLQtViewer \n");
294#endif
295}
296
297//////////////////////////////////////////////////////////////////////////////
298G4OpenGLQtViewer::~G4OpenGLQtViewer (
299)
300//////////////////////////////////////////////////////////////////////////////
301//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
302{
303#ifdef GEANT4_QT_DEBUG
304 printf("G4OpenGLQtViewer::~G4OpenGLQtViewer \n");
305#endif
306}
307
308
309/**
310 Create a popup menu for the widget. This menu is activated by right-mouse click
311*/
312void G4OpenGLQtViewer::createPopupMenu() {
313
314#if QT_VERSION < 0x040000
315 fContextMenu = new QPopupMenu( GLWindow,"All" );
316#else
317 fContextMenu = new QMenu("All");
318#endif
319
320#if QT_VERSION < 0x040000
321 QPopupMenu *mMouseAction = new QPopupMenu( fContextMenu );
322 fContextMenu->insertItem("&Mouse actions");
323#else
324 QMenu *mMouseAction = fContextMenu->addMenu("&Mouse actions");
325#endif
326
327#if QT_VERSION < 0x040000
328 // === Mouse menu ===
329
330 fMouseRotate = new QPopupMenu(mMouseAction);
331 mMouseAction->insertItem("&Rotate",fMouseRotate);
332
333 mMouseAction->setCheckable(true);
334 fMouseRotate->setCheckable(true);
335
336 fMousePick = new QPopupMenu(mMouseAction);
337 mMouseAction->insertItem("&Pick",fMousePick);
338 fMousePick->setCheckable(true);
339
340 fMouseShortcuts = new QPopupMenu(mMouseAction);
341 mMouseAction->insertItem("&Show Shortcuts",fMouseShortcuts);
342 fMouseShortcuts->setCheckable(true);
343
344#else
345 // === Mouse menu ===
346
347 fMouseRotate = mMouseAction->addAction("Rotate");
348 fMouseRotate->setCheckable(true);
349
350 fMousePick = mMouseAction->addAction("Pick");
351 fMousePick->setCheckable(true);
352
353 fMouseShortcuts = mMouseAction->addAction("Show Shortcuts");
354 fMouseShortcuts->setCheckable(true);
355#endif
356
357 // INIT mMouse
358#if QT_VERSION < 0x040000
359 fMouseRotate->setItemCheckeded(0,true);
360
361 QObject ::connect(fMouseRotate,
362 SIGNAL(activated(int)),
363 this,
364 SLOT(actionMouseRotate()));
365
366 QObject ::connect(fMousePick,
367 SIGNAL(activated(int)),
368 this,
369 SLOT(actionMousePick()));
370
371 QObject ::connect(fMouseShortcuts,
372 SIGNAL(activated(int)),
373 this,
374 SLOT(actionMouseShortcuts()));
375
376#else
377 fMouseRotate->setChecked(true);
378
379 QObject ::connect(fMouseRotate,
380 SIGNAL(triggered(bool)),
381 this,
382 SLOT(actionMouseRotate()));
383
384 QObject ::connect(fMousePick,
385 SIGNAL(triggered(bool)),
386 this,
387 SLOT(actionMousePick()));
388
389 QObject ::connect(fMouseShortcuts,
390 SIGNAL(triggered(bool)),
391 this,
392 SLOT(actionMouseShortcuts()));
393#endif
394
395#if QT_VERSION < 0x040000
396 // === Style Menu ===
397 QPopupMenu *mStyle = new QPopupMenu(fContextMenu);
398
399 QPopupMenu *mRepresentation = new QPopupMenu(fContextMenu);
400
401 QPopupMenu *mProjection = new QPopupMenu(fContextMenu);
402
403#if QT_VERSION < 0x030200
404 QAction *polyhedron = new QAction("&Polyhedron","&Polyhedron",CTRL+Key_P,mRepresentation);
405 QAction *nurbs = new QAction("&NURBS","&NURBS",CTRL+Key_N,mRepresentation);
406
407 QAction *ortho = new QAction("&Orthographic","&Orthographic",CTRL+Key_O,mProjection);
408 QAction *perspective = new QAction("&Perspective","&Perspective",CTRL+Key_P,mProjection);
409#else
410 QAction *polyhedron = new QAction("&Polyhedron",CTRL+Key_P,mRepresentation);
411 QAction *nurbs = new QAction("&NURBS",CTRL+Key_N,mRepresentation);
412
413 QAction *ortho = new QAction("&Orthographic",CTRL+Key_O,mProjection);
414 QAction *perspective = new QAction("&Perspective",CTRL+Key_P,mProjection);
415#endif
416 polyhedron->addTo(mRepresentation);
417 nurbs->addTo(mRepresentation);
418
419 ortho->addTo(mProjection);
420 perspective->addTo(mProjection);
421
422 mStyle->insertItem("&Representation",mRepresentation);
423 mStyle->insertItem("&Projection",mProjection);
424 fContextMenu->insertItem("&Style",mStyle);
425
426
427#else
428 // === Style Menu ===
429 QMenu *mStyle = fContextMenu->addMenu("&Style");
430
431 QMenu *mRepresentation = mStyle->addMenu("&Representation");
432 QMenu *mProjection = mStyle->addMenu("&Projection");
433 QAction *polyhedron = mRepresentation->addAction("Polyhedron");
434 QAction *nurbs = mRepresentation->addAction("NURBS");
435
436 QAction *ortho = mProjection->addAction("Orthographic");
437 QAction *perspective = mProjection->addAction("Persepective");
438#endif
439
440 // INIT mRepresentation
441 G4ViewParameters::RepStyle style;
442 style = fVP.GetRepStyle();
443 if (style == G4ViewParameters::polyhedron) {
444 createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),1);
445 } else if (style == G4ViewParameters::nurbs) {
446 createRadioAction(polyhedron,nurbs,SLOT(toggleRepresentation(bool)),2);
447 } else {
448 mRepresentation->clear();
449 }
450
451 // INIT mProjection
452 if (fVP.GetFieldHalfAngle() == 0) {
453 createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),1);
454 } else {
455 createRadioAction(ortho, perspective,SLOT(toggleProjection(bool)),2);
456 }
457
458#if QT_VERSION < 0x040000
459 // === Drawing Menu ===
460 QPopupMenu *mDrawing = new QPopupMenu(fContextMenu);
461 fContextMenu->insertItem("&Drawing",mDrawing);
462
463 fDrawingWireframe = new QPopupMenu(mDrawing);
464 mDrawing->insertItem("&Wireframe",fDrawingWireframe);
465
466 mDrawing->setCheckable(true);
467 fDrawingWireframe->setCheckable(true);
468
469 fDrawingLineRemoval = new QPopupMenu(mDrawing);
470 mDrawing->insertItem("&Hidden line removal",fDrawingLineRemoval);
471 fDrawingLineRemoval->setCheckable(true);
472
473 fDrawingSurfaceRemoval = new QPopupMenu(mDrawing);
474 mDrawing->insertItem("&Hidden surface removal",fDrawingSurfaceRemoval);
475 fDrawingSurfaceRemoval->setCheckable(true);
476
477 fDrawingLineSurfaceRemoval = new QPopupMenu(mDrawing);
478 mDrawing->insertItem("&Hidden line and surface removal",fDrawingLineSurfaceRemoval);
479 fDrawingLineSurfaceRemoval->setCheckable(true);
480
481#else
482 // === Drawing Menu ===
483 QMenu *mDrawing = mStyle->addMenu("&Drawing");
484
485 fDrawingWireframe = mDrawing->addAction("Wireframe");
486 fDrawingWireframe->setCheckable(true);
487
488 fDrawingLineRemoval = mDrawing->addAction("Hidden line removal");
489 fDrawingLineRemoval->setCheckable(true);
490
491 fDrawingSurfaceRemoval = mDrawing->addAction("Hidden Surface removal");
492 fDrawingSurfaceRemoval->setCheckable(true);
493
494 fDrawingLineSurfaceRemoval = mDrawing->addAction("Hidden line and surface removal");
495 fDrawingLineSurfaceRemoval->setCheckable(true);
496#endif
497 // INIT Drawing
498 G4ViewParameters::DrawingStyle d_style;
499 d_style = fVP.GetDrawingStyle();
500
501#if QT_VERSION < 0x040000
502 if (d_style == G4ViewParameters::wireframe) {
503 fDrawingWireframe->setItemChecked(0,true);
504 } else if (d_style == G4ViewParameters::hlr) {
505 fDrawingLineRemoval->setItemChecked(0,true);
506 } else if (d_style == G4ViewParameters::hsr) {
507 fDrawingSurfaceRemoval->setItemChecked(0,true);
508 } else if (d_style == G4ViewParameters::hlhsr) {
509 fDrawingLineSurfaceRemoval->setItemChecked(0,true);
510 } else {
511 mDrawing->clear();
512 }
513#ifdef GEANT4_QT_DEBUG
514 printf("G4OpenGLQtViewer:: fDrawingWireframe 1\n");
515#endif
516 QObject ::connect(fDrawingWireframe,
517 SIGNAL(activated(int)),
518 this,
519 SLOT(actionDrawingWireframe()));
520#ifdef GEANT4_QT_DEBUG
521 printf("G4OpenGLQtViewer:: fDrawingWireframe 2\n");
522#endif
523 QObject ::connect(fDrawingLineRemoval,
524 SIGNAL(activated(int)),
525 this,
526 SLOT(actionDrawingLineRemoval()));
527 QObject ::connect(fDrawingSurfaceRemoval,
528 SIGNAL(activated(int)),
529 this,
530 SLOT(actionDrawingSurfaceRemoval()));
531 QObject ::connect(fDrawingLineSurfaceRemoval,
532 SIGNAL(activated(int)),
533 this,
534 SLOT(actionDrawingLineSurfaceRemoval()));
535#else
536 if (d_style == G4ViewParameters::wireframe) {
537 fDrawingWireframe->setChecked(true);
538 } else if (d_style == G4ViewParameters::hlr) {
539 fDrawingLineRemoval->setChecked(true);
540 } else if (d_style == G4ViewParameters::hsr) {
541 fDrawingSurfaceRemoval->setChecked(true);
542 } else if (d_style == G4ViewParameters::hlhsr) {
543 fDrawingLineSurfaceRemoval->setChecked(true);
544 } else {
545 mDrawing->clear();
546 }
547 QObject ::connect(fDrawingWireframe,
548 SIGNAL(triggered(bool)),
549 this,
550 SLOT(actionDrawingWireframe()));
551 QObject ::connect(fDrawingLineRemoval,
552 SIGNAL(triggered(bool)),
553 this,
554 SLOT(actionDrawingLineRemoval()));
555 QObject ::connect(fDrawingSurfaceRemoval,
556 SIGNAL(triggered(bool)),
557 this,
558 SLOT(actionDrawingSurfaceRemoval()));
559 QObject ::connect(fDrawingLineSurfaceRemoval,
560 SIGNAL(triggered(bool)),
561 this,
562 SLOT(actionDrawingLineSurfaceRemoval()));
563#endif
564
565
566
567#if QT_VERSION < 0x040000
568 QPopupMenu *mBackground = new QPopupMenu(mStyle);
569 mStyle->insertItem("&Background color",mBackground);
570
571#if QT_VERSION < 0x030200
572 QAction *white = new QAction("&White","&White",CTRL+Key_W,mBackground);
573 QAction *black = new QAction("&Black","&Black",CTRL+Key_B,mBackground);
574#else
575 QAction *white = new QAction("&White",CTRL+Key_W,mBackground);
576 QAction *black = new QAction("&Black",CTRL+Key_B,mBackground);
577#endif
578 white->addTo(mBackground);
579 black->addTo(mBackground);
580
581#else
582 QMenu *mBackground = mStyle->addMenu("&Background color");
583 QAction *white = mBackground->addAction("White");
584 QAction *black = mBackground->addAction("Black");
585
586#endif
587 if (background.GetRed() == 1. &&
588 background.GetGreen() == 1. &&
589 background.GetBlue() == 1.) {
590 createRadioAction(white,black,SLOT(toggleBackground(bool)),1);
591 } else {
592 createRadioAction(white,black,SLOT(toggleBackground(bool)),2);
593 }
594
595
596#if QT_VERSION < 0x040000
597 // === Action Menu ===
598 QPopupMenu *mActions = new QPopupMenu(fContextMenu);
599 fContextMenu->insertItem("&Actions",mActions);
600
601#if QT_VERSION < 0x030200
602 QAction *createEPS = new QAction("&Save as ...","&Save as ...",CTRL+Key_S,mActions);
603#else
604 QAction *createEPS = new QAction("&Save as ...",CTRL+Key_S,mActions);
605#endif
606 createEPS->addTo(mActions);
607 QObject ::connect(createEPS,
608 SIGNAL(activated()),
609 this,
610 SLOT(actionCreateEPS()));
611
612#else
613 // === Action Menu ===
614 QMenu *mActions = fContextMenu->addMenu("&Actions");
615 QAction *createEPS = mActions->addAction("Save as ...");
616 QObject ::connect(createEPS,
617 SIGNAL(triggered()),
618 this,
619 SLOT(actionCreateEPS()));
620#endif
621
622
623
624#if QT_VERSION < 0x040000
625 // === Special Menu ===
626 QPopupMenu *mSpecial = new QPopupMenu(fContextMenu);
627 fContextMenu->insertItem("S&pecial",mSpecial);
628
629 QPopupMenu *mTransparency = new QPopupMenu(mSpecial);
630 mSpecial->insertItem("Transparency",mTransparency);
631
632#if QT_VERSION < 0x030200
633 QAction *transparencyOn = new QAction("&On","&On",CTRL+Key_O,mTransparency);
634 QAction *transparencyOff = new QAction("&Off","&Off",CTRL+Key_F,mTransparency);
635#else
636 QAction *transparencyOn = new QAction("&On",CTRL+Key_O,mTransparency);
637 QAction *transparencyOff = new QAction("&Off",CTRL+Key_F,mTransparency);
638#endif
639 transparencyOn->addTo(mTransparency);
640 transparencyOff->addTo(mTransparency);
641
642#else
643 // === Special Menu ===
644 QMenu *mSpecial = fContextMenu->addMenu("S&pecial");
645 QMenu *mTransparency = mSpecial->addMenu("Transparency");
646 QAction *transparencyOn = mTransparency->addAction("On");
647 QAction *transparencyOff = mTransparency->addAction("Off");
648#endif
649
650 if (transparency_enabled == false) {
651 createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),2);
652 } else if (transparency_enabled == true) {
653 createRadioAction(transparencyOn,transparencyOff,SLOT(toggleTransparency(bool)),1);
654 } else {
655 mSpecial->clear();
656 }
657
658
659#if QT_VERSION < 0x040000
660 QPopupMenu *mAntialiasing = new QPopupMenu(mSpecial);
661 mSpecial->insertItem("Antialiasing",mAntialiasing);
662
663#if QT_VERSION < 0x030200
664 QAction *antialiasingOn = new QAction("&On","&On",CTRL+Key_O,mAntialiasing);
665 QAction *antialiasingOff = new QAction("&Off","&Off",CTRL+Key_F,mAntialiasing);
666#else
667 QAction *antialiasingOn = new QAction("&On",CTRL+Key_O,mAntialiasing);
668 QAction *antialiasingOff = new QAction("&Off",CTRL+Key_F,mAntialiasing);
669#endif
670 antialiasingOn->addTo(mAntialiasing);
671 antialiasingOff->addTo(mAntialiasing);
672
673#else
674 QMenu *mAntialiasing = mSpecial->addMenu("Antialiasing");
675 QAction *antialiasingOn = mAntialiasing->addAction("On");
676 QAction *antialiasingOff = mAntialiasing->addAction("Off");
677#endif
678
679 if (antialiasing_enabled == false) {
680 createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),2);
681 } else if (antialiasing_enabled == true) {
682 createRadioAction(antialiasingOn,antialiasingOff,SLOT(toggleAntialiasing(bool)),1);
683 } else {
684 mAntialiasing->clear();
685 }
686
687#if QT_VERSION < 0x040000
688 QPopupMenu *mHaloing = new QPopupMenu(mSpecial);
689 mSpecial->insertItem("Haloing",mHaloing);
690
691#if QT_VERSION < 0x030200
692 QAction *haloingOn = new QAction("&On","&On",CTRL+Key_O,mHaloing);
693 QAction *haloingOff = new QAction("&Off","&Off",CTRL+Key_F,mHaloing);
694#else
695 QAction *haloingOn = new QAction("&On",CTRL+Key_O,mHaloing);
696 QAction *haloingOff = new QAction("&Off",CTRL+Key_F,mHaloing);
697#endif
698 haloingOn->addTo(mHaloing);
699 haloingOff->addTo(mHaloing);
700#else
701 QMenu *mHaloing = mSpecial->addMenu("Haloing");
702 QAction *haloingOn = mHaloing->addAction("On");
703 QAction *haloingOff = mHaloing->addAction("Off");
704#endif
705 if (haloing_enabled == false) {
706 createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),2);
707 } else if (haloing_enabled == true) {
708 createRadioAction(haloingOn,haloingOff,SLOT(toggleHaloing(bool)),1);
709 } else {
710 mHaloing->clear();
711 }
712
713#if QT_VERSION < 0x040000
714 QPopupMenu *mAux = new QPopupMenu(mSpecial);
715 mSpecial->insertItem("Auxiliairy edges",mAux);
716
717#if QT_VERSION < 0x030200
718 QAction *auxOn = new QAction("&On","&On",CTRL+Key_O,mAux);
719 QAction *auxOff = new QAction("&Off","&Off",CTRL+Key_F,mAux);
720#else
721 QAction *auxOn = new QAction("&On",CTRL+Key_O,mAux);
722 QAction *auxOff = new QAction("&Off",CTRL+Key_F,mAux);
723#endif
724 auxOn->addTo(mAux);
725 auxOff->addTo(mAux);
726
727#else
728 QMenu *mAux = mSpecial->addMenu("Auxiliary edges");
729 QAction *auxOn = mAux->addAction("On");
730 QAction *auxOff = mAux->addAction("Off");
731#endif
732 if (!fVP.IsAuxEdgeVisible()) {
733 createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),1);
734 } else {
735 createRadioAction(auxOn,auxOff,SLOT(toggleAux(bool)),2);
736 }
737
738
739#if QT_VERSION >= 0x030200
740#if QT_VERSION < 0x040000
741 QPopupMenu *mFullScreen = new QPopupMenu(mSpecial);
742 mSpecial->insertItem("Full screen",mFullScreen);
743
744 QAction *fullOn = new QAction("&On",CTRL+Key_O,mFullScreen);
745 QAction *fullOff = new QAction("&Off",CTRL+Key_F,mFullScreen);
746 fullOn->addTo(mFullScreen);
747 fullOff->addTo(mFullScreen);
748#else
749 QMenu *mFullScreen = mSpecial->addMenu("Full screen");
750 QAction *fullOn = mFullScreen->addAction("On");
751 QAction *fullOff = mFullScreen->addAction("Off");
752#endif
753 createRadioAction(fullOn,fullOff,SLOT(toggleFullScreen()),2);
754#endif
755
756}
757
758void G4OpenGLQtViewer::manageContextMenuEvent(QContextMenuEvent *e)
759{
760 if (!GLWindow) {
761 G4cerr << "Visualization window not defined, please choose one before\n" << G4endl;
762 } else {
763
764 if (!fContextMenu)
765 createPopupMenu();
766
767 // launch menu
768 if ( fContextMenu ) {
769 fContextMenu->exec( e->globalPos() );
770 // delete fContextMenu;
771 }
772 }
773 e->accept();
774}
775
776
777/**
778 Create a radio button menu. The two menu will be connected. When click on one,
779 eatch state will be invert and callback method will be called.
780 @param action1 first action to connect
781 @param action2 second action to connect
782 @param method callback method
783 @param nCheck: 1 : first action will be set true. 2 : second action will be set true
784*/
785#if QT_VERSION < 0x040000
786void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
787
788 if (action1->parent()->inherits("QPopupMenu")){
789 ((QPopupMenu*)action1->parent())->setCheckable(true);
790 }
791 ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
792 ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
793
794 if (nCheck ==1)
795 ((QPopupMenu*)action1->parent())->setItemChecked(0,true);
796 else
797 ((QPopupMenu*)action1->parent())->setItemChecked(1,true);
798
799 //FIXME : Should not work on Qt3
800 QObject ::connect(action1, SIGNAL(activated()),action2, SLOT(toggle()));
801 QObject ::connect(action2, SIGNAL(activated()),action1, SLOT(toggle()));
802
803 QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
804}
805
806#else
807void G4OpenGLQtViewer::createRadioAction(QAction *action1,QAction *action2, const std::string& method,unsigned int nCheck) {
808
809 action1->setCheckable(true);
810 action2->setCheckable(true);
811
812 if (nCheck ==1)
813 action1->setChecked (true);
814 else
815 action2->setChecked (true);
816
817 QObject ::connect(action1, SIGNAL(triggered(bool)),action2, SLOT(toggle()));
818 QObject ::connect(action2, SIGNAL(triggered(bool)),action1, SLOT(toggle()));
819
820 QObject ::connect(action1, SIGNAL(toggled(bool)),this, method.c_str());
821
822}
823#endif
824
825/**
826 Slot activate when mouseAction->rotate menu is set
827 */
828void G4OpenGLQtViewer::actionMouseRotate() {
829 emit toggleMouseAction(STYLE1);
830}
831
832
833/**
834 Slot activate when mouseAction->zoom menu is set
835 */
836void G4OpenGLQtViewer::actionMousePick() {
837#ifdef GEANT4_QT_DEBUG
838 printf("G4OpenGLQtViewer::actionMousePick \n");
839#endif
840 emit toggleMouseAction(STYLE3);
841}
842
843/**
844 Slot activate when mouseAction->zoom menu is set
845 */
846void G4OpenGLQtViewer::actionMouseShortcuts() {
847#ifdef GEANT4_QT_DEBUG
848 printf("G4OpenGLQtViewer::actionMouseShortcuts \n");
849#endif
850 emit toggleMouseAction(STYLE3);
851}
852
853/**
854 Slot activate when drawing->wireframe menu is set
855 */
856void G4OpenGLQtViewer::actionDrawingWireframe() {
857 emit toggleDrawingAction(1);
858}
859
860/**
861 Slot activate when drawing->line removal menu is set
862 */
863void G4OpenGLQtViewer::actionDrawingLineRemoval() {
864 emit toggleDrawingAction(2);
865}
866
867/**
868 Slot activate when drawing->surface removal menu is set
869 */
870void G4OpenGLQtViewer::actionDrawingSurfaceRemoval() {
871 emit toggleDrawingAction(3);
872}
873
874/**
875 Slot activate when drawing->wireframe menu is set
876 */
877void G4OpenGLQtViewer::actionDrawingLineSurfaceRemoval() {
878 emit toggleDrawingAction(4);
879}
880
881
882/**
883 Slot activated when mouse action is toggle
884 @param aAction : STYLE1, STYLE2, STYLE3
885 */
886void G4OpenGLQtViewer::toggleMouseAction(mouseActions aAction) {
887
888 if (aAction == STYLE1) { // rotate
889 G4cout << "Click and move mouse to rotate volume \n" << G4endl;
890 G4cout << "Press left/right arrows to move volume left/right\n" << G4endl;
891 G4cout << "Press up/down arrows to move volume up/down\n" << G4endl;
892 G4cout << "Press CONTROL+up/down arrows to move volume toward/forward\n" << G4endl;
893 G4cout << "Press SHIFT+left/right arrows to rotate volume left/right\n" << G4endl;
894 G4cout << "Press SHIFT+up/down arrows to rotate volume up/down\n" << G4endl;
895 G4cout << "Press +/- to zoom into volume\n" << G4endl;
896#if QT_VERSION < 0x040000
897 fMouseRotate->setItemChecked (0,true);
898 fMousePick->setItemChecked (0,false);
899 fMouseShortcuts->setItemChecked (0,false);
900#else
901 fMouseRotate->setChecked (true);
902 fMousePick->setChecked (false);
903 fMouseShortcuts->setChecked (false);
904#endif
905 fMouseAction = STYLE1;
906 fVP.SetPicking(false);
907 } else if (aAction == STYLE2) { //pick
908 G4cout << "Click and pick \n" << G4endl;
909#if QT_VERSION < 0x040000
910 fMouseRotate->setItemChecked (0,false);
911 fMousePick->setItemChecked (0,true);
912#else
913 fMouseRotate->setChecked (false);
914 fMousePick->setChecked (true);
915#endif
916 fVP.SetPicking(true);
917 fMouseAction = STYLE2;
918 } else if (aAction == STYLE3) { // display shortcuts
919#if QT_VERSION < 0x040000
920 fMouseRotate->setItemChecked (0,false);
921 fMousePick->setItemChecked (0,false);
922 fMouseShortcuts->setItemChecked (0,true);
923#else
924 fMouseRotate->setChecked (false);
925 fMousePick->setChecked (false);
926 fMouseShortcuts->setChecked (true);
927#endif
928 fMouseAction = STYLE3;
929 fVP.SetPicking(false);
930
931 G4cout << "Click and move mouse to rotate volume \n" << G4endl;
932 G4cout << "Press left/right arrows to move volume left/right\n" << G4endl;
933 G4cout << "Press up/down arrows to move volume up/down\n" << G4endl;
934 G4cout << "Press CONTROL+up/down arrows to move volume toward/forward\n" << G4endl;
935 G4cout << "Press SHIFT+left/right arrows to rotate volume left/right\n" << G4endl;
936 G4cout << "Press SHIFT+up/down arrows to rotate volume up/down\n" << G4endl;
937 G4cout << "Press +/- to zoom into volume\n" << G4endl;
938 }
939
940}
941/**
942 Slot activated when drawing menu is toggle
943 Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
944 KernelVisitDecision () will be call and will set the fNeedKernelVisit
945 to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
946 It will cause a redraw of the view
947 @param aAction : 1 wireframe, 2 line removal, 3 surface removal, 4 line & surface removal
948 @see G4OpenGLStoredQtViewer::DrawView
949 @see G4XXXStoredViewer::CompareForKernelVisit
950 */
951void G4OpenGLQtViewer::toggleDrawingAction(int aAction) {
952
953 G4ViewParameters::DrawingStyle d_style;
954
955
956 if (aAction ==1) {
957#if QT_VERSION < 0x040000
958 fDrawingWireframe->setItemChecked (0,true);
959 fDrawingLineRemoval->setItemChecked (0,false);
960 fDrawingSurfaceRemoval->setItemChecked (0,false);
961 fDrawingLineSurfaceRemoval->setItemChecked (0,false);
962#else
963 fDrawingWireframe->setChecked (true);
964 fDrawingLineRemoval->setChecked (false);
965 fDrawingSurfaceRemoval->setChecked (false);
966 fDrawingLineSurfaceRemoval->setChecked (false);
967#endif
968
969 d_style = G4ViewParameters::wireframe;
970
971 } else if (aAction ==2) {
972#if QT_VERSION < 0x040000
973 fDrawingWireframe->setItemChecked (0,false);
974 fDrawingLineRemoval->setItemChecked (0,true);
975 fDrawingSurfaceRemoval->setItemChecked (0,false);
976 fDrawingLineSurfaceRemoval->setItemChecked (0,false);
977#else
978 fDrawingWireframe->setChecked (false);
979 fDrawingLineRemoval->setChecked (true);
980 fDrawingSurfaceRemoval->setChecked (false);
981 fDrawingLineSurfaceRemoval->setChecked (false);
982#endif
983
984 d_style = G4ViewParameters::hlr;
985
986 } else if (aAction ==3) {
987#if QT_VERSION < 0x040000
988 fDrawingWireframe->setItemChecked (0,false);
989 fDrawingLineRemoval->setItemChecked (0,false);
990 fDrawingSurfaceRemoval->setItemChecked (0,true);
991 fDrawingLineSurfaceRemoval->setItemChecked (0,false);
992#else
993 fDrawingWireframe->setChecked (false);
994 fDrawingLineRemoval->setChecked (false);
995 fDrawingSurfaceRemoval->setChecked (true);
996 fDrawingLineSurfaceRemoval->setChecked (false);
997#endif
998
999 d_style = G4ViewParameters::hsr;
1000
1001 } else if (aAction ==4) {
1002#if QT_VERSION < 0x040000
1003 fDrawingWireframe->setItemChecked (0,false);
1004 fDrawingLineRemoval->setItemChecked (0,false);
1005 fDrawingSurfaceRemoval->setItemChecked (0,false);
1006 fDrawingLineSurfaceRemoval->setItemChecked (0,true);
1007#else
1008 fDrawingWireframe->setChecked (false);
1009 fDrawingLineRemoval->setChecked (false);
1010 fDrawingSurfaceRemoval->setChecked (false);
1011 fDrawingLineSurfaceRemoval->setChecked (true);
1012#endif
1013 d_style = G4ViewParameters::hlhsr;
1014 }
1015 fVP.SetDrawingStyle(d_style);
1016
1017 updateQWidget();
1018#ifdef GEANT4_QT_DEBUG
1019 printf("G4OpenGLQtViewer::toggleDrawingAction\n");
1020#endif
1021}
1022
1023
1024/**
1025 SLOT Activate by a click on the representation menu
1026 Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1027 KernelVisitDecision () will be call and will set the fNeedKernelVisit
1028 to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1029 It will cause a redraw of the view
1030 @param check : 1 polyhedron, 0 nurbs
1031 @see G4OpenGLStoredQtViewer::DrawView
1032 @see G4XXXStoredViewer::CompareForKernelVisit
1033*/
1034void G4OpenGLQtViewer::toggleRepresentation(bool check) {
1035
1036 G4ViewParameters::RepStyle style;
1037 if (check == 1) {
1038 style = G4ViewParameters::polyhedron;
1039 } else {
1040 style = G4ViewParameters::nurbs;
1041 }
1042 fVP.SetRepStyle (style);
1043
1044 updateQWidget();
1045}
1046
1047/**
1048 SLOT Activate by a click on the projection menu
1049 Warning : When G4OpenGLStoredQtViewer::DrawView() method call,
1050 KernelVisitDecision () will be call and will set the fNeedKernelVisit
1051 to 1. See G4XXXStoredViewer::CompareForKernelVisit for explanations.
1052 It will cause a redraw of the view
1053 @param check : 1 orthographic, 2 perspective
1054 @see G4OpenGLStoredQtViewer::DrawView
1055 @see G4XXXStoredViewer::CompareForKernelVisit
1056*/
1057void G4OpenGLQtViewer::toggleProjection(bool check) {
1058
1059 if (check == 1) {
1060 fVP.SetFieldHalfAngle (0);
1061 } else {
1062
1063 // look for the default parameter hidden in G4UIcommand parameters
1064 G4UImanager* UI = G4UImanager::GetUIpointer();
1065 if(UI==NULL)
1066 return;
1067 G4UIcommandTree * treeTop = UI->GetTree();
1068
1069 // find command
1070 G4UIcommand* command = treeTop->FindPath("/vis/viewer/set/projection");
1071 if (!command)
1072 return;
1073
1074 // find param
1075 G4UIparameter * angleParam = NULL;
1076 for(G4int i=0; i<command->GetParameterEntries(); i++)
1077 {
1078 if( command->GetParameter(i)->GetParameterName() == "field-half-angle" ) {
1079 angleParam = command->GetParameter(i);
1080 }
1081 }
1082 if (!angleParam)
1083 return;
1084
1085 // find unit
1086 G4UIparameter * unitParam = NULL;
1087 for(G4int i=0; i<command->GetParameterEntries(); i++)
1088 {
1089 if( command->GetParameter(i)->GetParameterName() == "unit" ) {
1090 unitParam = command->GetParameter(i);
1091 }
1092 }
1093 if (!unitParam)
1094 return;
1095
1096 G4double defaultValue = command->ConvertToDouble(angleParam->GetDefaultValue())
1097 * G4UnitDefinition::GetValueOf(unitParam->GetDefaultValue());
1098 if (defaultValue > 89.5 || defaultValue <= 0.0) {
1099 G4cerr << "Field half angle should be 0 < angle <= 89.5 degrees. Check your default Field half angle parameter\n";
1100 } else {
1101 G4cout << "Perspective view has been set to default value. Field half angle="<<angleParam->GetDefaultValue() <<" \n" << G4endl;
1102 fVP.SetFieldHalfAngle (defaultValue);
1103 SetView ();
1104 }
1105 }
1106 updateQWidget();
1107}
1108
1109
1110/**
1111 SLOT Activate by a click on the background menu
1112@param check : 1 white, 0 black
1113*/
1114void G4OpenGLQtViewer::toggleBackground(bool check) {
1115
1116 // //I need to revisit the kernel if the background colour changes and
1117 // //hidden line removal is enabled, because hlr drawing utilises the
1118 // //background colour in its drawing...
1119 // // (Note added by JA 13/9/2005) Background now handled in view
1120 // // parameters. A kernel visit is triggered on change of background.
1121 if (check == 1) {
1122 ((G4ViewParameters&)this->GetViewParameters()).
1123 SetBackgroundColour(G4Colour(1.,1.,1.)); // White
1124 } else {
1125 ((G4ViewParameters&)this->GetViewParameters()).
1126 SetBackgroundColour(G4Colour(0.,0.,0.)); // Black
1127 }
1128 updateQWidget();
1129}
1130
1131/**
1132 SLOT Activate by a click on the transparency menu
1133@param check : 1 , 0
1134*/
1135void G4OpenGLQtViewer::toggleTransparency(bool check) {
1136
1137 if (check) {
1138 transparency_enabled = false;
1139 } else {
1140 transparency_enabled = true;
1141 }
1142 SetNeedKernelVisit (true);
1143 updateQWidget();
1144}
1145
1146/**
1147 SLOT Activate by a click on the antialiasing menu
1148@param check : 1 , 0
1149*/
1150void G4OpenGLQtViewer::toggleAntialiasing(bool check) {
1151
1152 if (!check) {
1153 antialiasing_enabled = false;
1154 glDisable (GL_LINE_SMOOTH);
1155 glDisable (GL_POLYGON_SMOOTH);
1156 } else {
1157 antialiasing_enabled = true;
1158 glEnable (GL_LINE_SMOOTH);
1159 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
1160 glEnable (GL_POLYGON_SMOOTH);
1161 glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
1162 }
1163
1164 updateQWidget();
1165}
1166
1167/**
1168 SLOT Activate by a click on the haloing menu
1169@param check : 1 , 0
1170*/
1171//FIXME : I SEE NOTHING...
1172void G4OpenGLQtViewer::toggleHaloing(bool check) {
1173 if (check) {
1174 haloing_enabled = false;
1175 } else {
1176 haloing_enabled = true;
1177 }
1178
1179 updateQWidget();
1180
1181}
1182
1183/**
1184 SLOT Activate by a click on the auxiliaire edges menu
1185@param check : 1 , 0
1186*/
1187void G4OpenGLQtViewer::toggleAux(bool check) {
1188 if (check) {
1189 fVP.SetAuxEdgeVisible(false);
1190 } else {
1191 fVP.SetAuxEdgeVisible(true);
1192 }
1193 SetNeedKernelVisit (true);
1194 updateQWidget();
1195
1196}
1197
1198/**
1199 SLOT Activate by a click on the full screen menu
1200*/
1201void G4OpenGLQtViewer::toggleFullScreen() {
1202#if QT_VERSION >= 0x030200
1203 GLWindow->setWindowState(GLWindow->windowState() ^ Qt::WindowFullScreen);
1204#else
1205 G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1206#endif
1207}
1208
1209void G4OpenGLQtViewer::actionCreateEPS() {
1210 QString filters;
1211#if QT_VERSION < 0x040000
1212 QStrList listFormat=QImageIO::outputFormats();
1213 char *tmp=listFormat.first();
1214 while (tmp!=0) {
1215 filters += QString(tmp) + ";;";
1216 tmp=listFormat.next();
1217 }
1218#else
1219 QList<QByteArray> formats = QImageWriter::supportedImageFormats ();
1220 for (int i = 0; i < formats.size(); ++i) {
1221 filters +=formats.at(i) + ";;";
1222 }
1223#endif
1224 filters += "eps;;";
1225 filters += "ps;;";
1226 filters += "pdf";
1227 QString* selectedFormat = new QString();
1228#if QT_VERSION < 0x040000
1229 QString nomFich = QFileDialog::getSaveFileName ( ".",
1230 filters,
1231 GLWindow,
1232 "Save file dialog",
1233 tr("Save as ..."),
1234 selectedFormat );
1235#else
1236 QString nomFich = QFileDialog::getSaveFileName ( GLWindow,
1237 tr("Save as ..."),
1238 ".",
1239 filters,
1240 selectedFormat );
1241#endif
1242 // bmp jpg jpeg png ppm xbm xpm
1243 if (nomFich == "") {
1244 return;
1245 }
1246#if QT_VERSION < 0x040000
1247 nomFich += "."+QString(selectedFormat->ascii());
1248 QString format = selectedFormat->lower();
1249#else
1250 nomFich += "."+QString(selectedFormat->toStdString().c_str());
1251 QString format = selectedFormat->toLower();
1252#endif
1253 G4OpenGLQtExportDialog* exportDialog= new G4OpenGLQtExportDialog(GLWindow,format,fWindow->height(),fWindow->width());
1254 if( exportDialog->exec()) {
1255
1256 QImage image;
1257 bool res = false;
1258 if ((exportDialog->getWidth() !=fWindow->width()) ||
1259 (exportDialog->getHeight() !=fWindow->height())) {
1260 if (format != QString("eps")) {
1261 G4cerr << "Export->Change Size : This function is not implemented, to export in another size, please resize your frame to what you need\n" << G4endl;
1262
1263 // rescaleImage(exportDialog->getWidth(),exportDialog->getHeight());// re-scale image
1264 // QGLWidget* glResized = fWindow;
1265
1266 // FIXME :
1267 // L.Garnier : I've try to implement change size function, but the problem is
1268 // the renderPixmap function call the QGLWidget to resize and it doesn't draw
1269 // the content of this widget... It only draw the background.
1270
1271 // fWindow->renderPixmap (exportDialog->getWidth()*2,exportDialog->getHeight()*2,true );
1272
1273 // QPixmap pixmap = fWindow->renderPixmap ();
1274
1275 // image = pixmap->toImage();
1276 // glResized->resize(exportDialog->getWidth()*2,exportDialog->getHeight()*2);
1277 // image = glResized->grabFrameBuffer();
1278 }
1279 } else {
1280 image = fWindow->grabFrameBuffer();
1281 }
1282 if (format == QString("eps")) {
1283 if (exportDialog->getVectorEPS()) {
1284 res = generateVectorEPS(nomFich,exportDialog->getWidth(),exportDialog->getHeight(),image);
1285 } else {
1286 res = generateEPS(nomFich,exportDialog->getNbColor(),image);
1287 }
1288 } else if ((format == "ps") || (format == "pdf")) {
1289 res = generatePS_PDF(nomFich,exportDialog->getNbColor(),image);
1290 } else if ((format == "tif") ||
1291 (format == "tiff") ||
1292 (format == "jpg") ||
1293 (format == "jpeg") ||
1294 (format == "png") ||
1295 (format == "pbm") ||
1296 (format == "pgm") ||
1297 (format == "ppm") ||
1298 (format == "bmp") ||
1299 (format == "xbm") ||
1300 (format == "xpm")) {
1301#if QT_VERSION < 0x040000
1302 res = image.save(nomFich,selectedFormat->ascii(),exportDialog->getSliderValue());
1303#else
1304 res = image.save(nomFich,0,exportDialog->getSliderValue());
1305#endif
1306 } else {
1307 G4cerr << "This version of G4UI Could not generate the selected format\n" << G4endl;
1308 }
1309 if (res == false) {
1310#if QT_VERSION < 0x040000
1311 G4cerr << "Error while saving file... "<<nomFich.ascii()<<"\n" << G4endl;
1312#else
1313 G4cerr << "Error while saving file... "<<nomFich.toStdString().c_str()<<"\n" << G4endl;
1314#endif
1315 } else {
1316#if QT_VERSION < 0x040000
1317 G4cout << "File "<<nomFich.ascii()<<" has been saved \n" << G4endl;
1318#else
1319 G4cout << "File "<<nomFich.toStdString().c_str()<<" has been saved \n" << G4endl;
1320#endif
1321 }
1322
1323 } else { // cancel selected
1324 return;
1325 }
1326
1327#ifdef GEANT4_QT_DEBUG
1328 printf("G4OpenGLQtViewer::actionCreateEPS() \n");
1329#endif
1330}
1331
1332/*
1333// 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
1334
1335void Graph::exportToSVG(const QString& fname)
1336{
1337 // enable workaround for Qt3 misalignments
1338 QwtPainter::setSVGMode(true);
1339 QPicture picture;
1340 QPainter p(&picture);
1341 d_plot->print(&p, d_plot->rect());
1342 p.end();
1343
1344 picture.save(fname, "svg");
1345}
1346*/
1347
1348
1349
1350
1351/**
1352 Save the current mouse press point
1353 @param p mouse click point
1354*/
1355void G4OpenGLQtViewer::G4MousePressEvent(QPoint p)
1356{
1357 fLastPos = p;
1358 if (fMouseAction == STYLE2){ // pick
1359 Pick(p.x(),p.y());
1360 }
1361}
1362
1363
1364/**
1365 @param pos_x mouse x position
1366 @param pos_y mouse y position
1367 @param mButtons mouse button active
1368*/
1369
1370#if QT_VERSION < 0x040000
1371void G4OpenGLQtViewer::G4MouseEvent(int pos_x, int pos_y,Qt::ButtonState mButtons)
1372#else
1373void G4OpenGLQtViewer::G4MouseEvent(int pos_x, int pos_y,Qt::MouseButtons mButtons)
1374#endif
1375{
1376 int dx = fLastPos.x() - pos_x;
1377 int dy = fLastPos.y() - pos_y;
1378 if (fMouseAction == STYLE1) { // rotate
1379 if (mButtons & Qt::LeftButton) {
1380 G4MouseRotateEvent(dx,dy);
1381 }
1382 }
1383 fLastPos = QPoint(pos_x, pos_y);
1384}
1385
1386/**
1387 @param dx delta mouse x position
1388 @param dy delta mouse y position
1389*/
1390
1391void G4OpenGLQtViewer::G4MouseMoveEvent(int dx, int dy, int dz)
1392{
1393 int deltaX =
1394GLenum clip = glGet(GL_CLIP_PLANE);
1395 int deltaY = ;
1396
1397
1398 G4Point3D stp
1399 = GetSceneHandler()->GetScene()->GetStandardTargetPoint();
1400
1401 G4Point3D tp = stp + fVP.GetCurrentTargetPoint ();
1402
1403 const G4Vector3D& upVector = fVP.GetUpVector ();
1404 const G4Vector3D& vpVector = fVP.GetViewpointDirection ();
1405
1406 G4Vector3D unitRight = (upVector.cross (vpVector)).unit();
1407 G4Vector3D unitUp = (vpVector.cross (unitRight)).unit();
1408
1409#ifdef GEANT4_QT_DEBUG
1410 G4cout << fVP<<G4endl;
1411 printf("Mouse event Target Point : %f %f %f\n",tp.x(),tp.y(),tp.z());
1412 printf("Mouse event upVector : %f %f %f\n",upVector.x(),upVector.y(),upVector.z());
1413 printf("Mouse event vpVector Point : %f %f %f\n",vpVector.x(),vpVector.y(),vpVector.z());
1414 printf("Mouse event unitRight Point : %f %f %f\n",unitRight.x(),unitRight.y(),unitRight.z());
1415 printf("Mouse event unitUp Point : %f %f %f\n",unitUp.x(),unitUp.y(),unitUp.z());
1416#endif
1417 tp += -dx * unitRight + dy * unitUp + dz * vpVector;
1418 fVP.SetCurrentTargetPoint (tp - stp);
1419
1420 updateQWidget();
1421}
1422
1423
1424/**
1425 @param dx delta mouse x position
1426 @param dy delta mouse y position
1427*/
1428
1429void G4OpenGLQtViewer::G4MouseRotateEvent(int dx, int dy)
1430{
1431 //phi spin stuff here
1432
1433 G4Vector3D vp = fVP.GetViewpointDirection ().unit ();
1434 G4Vector3D up = fVP.GetUpVector ().unit ();
1435
1436 G4Vector3D yprime = (up.cross(vp)).unit();
1437 G4Vector3D zprime = (vp.cross(yprime)).unit();
1438
1439 G4double delta_alpha;
1440 G4double delta_theta;
1441
1442 if (fVP.GetLightsMoveWithCamera()) {
1443 delta_alpha = dy;
1444 delta_theta = -dx;
1445 } else {
1446 delta_alpha = -dy;
1447 delta_theta = dx;
1448 }
1449
1450 delta_alpha *= deg;
1451 delta_theta *= deg;
1452
1453 G4Vector3D new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime;
1454
1455 G4Vector3D new_up;
1456 if (fVP.GetLightsMoveWithCamera()) {
1457 new_up = (new_vp.cross(yprime)).unit();
1458 fVP.SetUpVector(new_up);
1459 } else {
1460 new_up = up;
1461 }
1462 ////////////////
1463 // Rotates by fixed azimuthal angle delta_theta.
1464
1465 G4double cosalpha = new_up.dot (new_vp.unit());
1466 G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
1467 yprime = (new_up.cross (new_vp.unit())).unit ();
1468 G4Vector3D xprime = yprime.cross (new_up);
1469 // Projection of vp on plane perpendicular to up...
1470 G4Vector3D a1 = sinalpha * xprime;
1471 // Required new projection...
1472 G4Vector3D a2 =
1473 sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
1474 // Required Increment vector...
1475 G4Vector3D delta = a2 - a1;
1476 // So new viewpoint is...
1477 G4Vector3D viewPoint = new_vp.unit() + delta;
1478
1479 fVP.SetViewAndLights (viewPoint);
1480 updateQWidget();
1481
1482}
1483
1484/** This is the benning of a rescale function. It does nothing for the moment
1485 @param aWidth : new width
1486 @param aHeight : new height
1487*/
1488void G4OpenGLQtViewer::rescaleImage(
1489 int aWidth
1490,int aHeight
1491){
1492#ifdef GEANT4_QT_DEBUG
1493 printf("should rescale \n");
1494#endif
1495 GLfloat* feedback_buffer;
1496 GLint returned;
1497 FILE* file;
1498
1499// feedback_buffer = new GLfloat[size];
1500// glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1501// glRenderMode (GL_FEEDBACK);
1502
1503// glViewport (0, 0, aWidth, aHeight);
1504// DrawView();
1505// returned = glRenderMode (GL_RENDER);
1506
1507}
1508
1509/**
1510 Generate Vectorial Encapsulated Postscript form image
1511 @param aFilename : name of file
1512 @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1513 @param aImage : Image to print
1514*/
1515bool G4OpenGLQtViewer::generateVectorEPS (
1516 QString aFilename
1517,int aWidth
1518,int aHeight
1519,QImage aImage
1520)
1521{
1522 // Print vectored PostScript
1523
1524 G4int size = 5000000;
1525
1526 GLfloat* feedback_buffer;
1527 GLint returned;
1528 FILE* file;
1529
1530 feedback_buffer = new GLfloat[size];
1531 glFeedbackBuffer (size, GL_3D_COLOR, feedback_buffer);
1532 glRenderMode (GL_FEEDBACK);
1533
1534 int side = qMin(aWidth, aHeight);
1535 glViewport((aWidth - side) / 2, (aHeight - side) / 2, side, side);
1536 DrawView();
1537
1538 returned = glRenderMode (GL_RENDER);
1539
1540
1541#if QT_VERSION < 0x040000
1542 file = fopen (aFilename.ascii(), "w");
1543#else
1544 file = fopen (aFilename.toStdString().c_str(), "w");
1545#endif
1546 if (file) {
1547 spewWireframeEPS (file, returned, feedback_buffer, "rendereps");
1548 } else {
1549#if QT_VERSION < 0x040000
1550 G4cerr << "Could not open "<< aFilename.ascii()<<"\n" << G4endl;
1551#else
1552 G4cerr << "Could not open "<< aFilename.toStdString().c_str()<<"\n" << G4endl;
1553#endif
1554 }
1555
1556 delete[] feedback_buffer;
1557
1558 return true;
1559}
1560
1561/**
1562 Generate Encapsulated Postscript form image
1563 @param aFilename : name of file
1564 @param aInColor : numbers of colors : 1->BW 2->RGB 3->RGB+Alpha
1565 @param aImage : Image to print
1566*/
1567bool G4OpenGLQtViewer::generateEPS (
1568 QString aFilename
1569,int aInColor
1570,QImage aImage
1571)
1572{
1573 // FIXME
1574#ifdef GEANT4_QT_DEBUG
1575 printf("saving EPS\n");
1576#endif
1577
1578 FILE* fp;
1579
1580 if (aImage.bits () == NULL)
1581 return false;
1582
1583#if QT_VERSION < 0x040000
1584 fp = fopen (aFilename.ascii(), "w");
1585#else
1586 fp = fopen (aFilename.toStdString().c_str(), "w");
1587#endif
1588 if (fp == NULL) {
1589 return false;
1590 }
1591
1592 fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
1593#if QT_VERSION < 0x040000
1594 fprintf (fp, "%%%%Title: %s\n", aFilename.ascii());
1595#else
1596 fprintf (fp, "%%%%Title: %s\n", aFilename.toStdString().c_str());
1597#endif
1598 fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
1599 fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", aImage.width(), aImage.height());
1600 fprintf (fp, "%%%%EndComments\n");
1601 fprintf (fp, "gsave\n");
1602 fprintf (fp, "/bwproc {\n");
1603 fprintf (fp, " rgbproc\n");
1604 fprintf (fp, " dup length 3 idiv string 0 3 0 \n");
1605 fprintf (fp, " 5 -1 roll {\n");
1606 fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n");
1607 fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
1608 fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
1609 fprintf (fp, " { 2 1 roll } ifelse\n");
1610 fprintf (fp, " }forall\n");
1611 fprintf (fp, " pop pop pop\n");
1612 fprintf (fp, "} def\n");
1613 fprintf (fp, "systemdict /colorimage known not {\n");
1614 fprintf (fp, " /colorimage {\n");
1615 fprintf (fp, " pop\n");
1616 fprintf (fp, " pop\n");
1617 fprintf (fp, " /rgbproc exch def\n");
1618 fprintf (fp, " { bwproc } image\n");
1619 fprintf (fp, " } def\n");
1620 fprintf (fp, "} if\n");
1621 fprintf (fp, "/picstr %d string def\n", aImage.width() * aInColor);
1622 fprintf (fp, "%d %d scale\n", aImage.width(), aImage.height());
1623 fprintf (fp, "%d %d %d\n", aImage.width(), aImage.height(), 8);
1624 fprintf (fp, "[%d 0 0 %d 0 0]\n", aImage.width(), aImage.height());
1625 fprintf (fp, "{currentfile picstr readhexstring pop}\n");
1626 fprintf (fp, "false %d\n", aInColor);
1627 fprintf (fp, "colorimage\n");
1628
1629
1630 int width = aImage.width();
1631 int height = aImage.height();
1632 int depth = aImage.depth();
1633 int size = width*height;
1634
1635 if (depth == 1)
1636 size = (width+7)/8*height;
1637 else if (aInColor == 1)
1638 size = size*3;
1639
1640 int i = 0;
1641 // if ( aInColor ==1 ) {
1642 // FIXME : L. Garnier. For the moment 10 dec 2007, I could not find a way
1643 // to save correctly grayscale Image. I mean that color or grayscale image
1644 // have the same file save size !
1645
1646 /* } else*/ if (depth == 8) {
1647 for(int y=height-1; y >=0 ; y--) {
1648 const uchar * s = aImage.scanLine(y);
1649 for(int x=0; x <width; x++) {
1650 QRgb rgb = aImage.color(s[x]);
1651 if (aInColor == 1) {
1652 fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1653 i++;
1654 } else {
1655 fprintf (fp, " %02hx %02hx %02hx",
1656 (unsigned char) qRed(rgb),
1657 (unsigned char) qGreen(rgb),
1658 (unsigned char) qBlue(rgb));
1659 i += 3;
1660 }
1661 }
1662 fprintf (fp, "\n");
1663 }
1664 } else {
1665#if QT_VERSION < 0x040000
1666 bool alpha = aImage.hasAlphaBuffer();
1667#else
1668 bool alpha = aImage.hasAlphaChannel();
1669 for(int y=height-1; y >=0 ; y--) {
1670 QRgb * s = (QRgb*)(aImage.scanLine(y));
1671 for(int x=0; x <width; x++) {
1672 QRgb rgb = (*s++);
1673 if (alpha && qAlpha(rgb) < 0x40) // 25% alpha, convert to white -
1674 rgb = qRgb(0xff, 0xff, 0xff);
1675 if (aInColor == 1) {
1676 fprintf (fp, " %02hx ",(unsigned char)qGray(rgb));
1677 i++;
1678 } else {
1679 fprintf (fp, " %02hx %02hx %02hx",
1680 (unsigned char) qRed(rgb),
1681 (unsigned char) qGreen(rgb),
1682 (unsigned char) qBlue(rgb));
1683 i += 3;
1684 }
1685 }
1686 fprintf (fp, "\n");
1687 }
1688#endif
1689
1690 }
1691
1692 fprintf (fp, "grestore\n");
1693 fprintf (fp, "showpage\n");
1694 fclose (fp);
1695
1696 return true;
1697}
1698/**
1699 Generate Postscript or PDF form image
1700 @param aFilename : name of file
1701 @param aInColor : numbers of colors : 1->BW 2->RGB
1702 @param aImage : Image to print
1703*/
1704bool G4OpenGLQtViewer::generatePS_PDF (
1705 QString aFilename
1706,int aInColor
1707,QImage aImage
1708)
1709{
1710
1711#if QT_VERSION < 0x040000
1712#ifdef Q_WS_MAC || Q_WS_X11
1713 QPrinter printer;
1714 // printer.setPageSize(pageSize);
1715 if (aInColor == 1) {
1716 printer.setColorMode(QPrinter::GrayScale);
1717 } else {
1718 printer.setColorMode(QPrinter::Color);
1719 }
1720
1721 /* FIXME : I don't know which format it will save...
1722 if (aFilename.endsWith(".ps")) {
1723 printer.setOutputFormat(QPrinter::PostScriptFormat);
1724 } else {
1725 printer.setOutputFormat(QPrinter::PdfFormat);
1726 }
1727 */
1728 printer.setOutputFileName(aFilename);
1729 // printer.setFullPage ( true);
1730 QPainter paint(&printer);
1731 paint.drawImage (0,0,aImage );
1732 paint.end();
1733#else
1734 G4cerr << "This fonction is only supported on Mac OsX or X11 with Qt3. Full platform supported with Qt4\n" << G4endl;
1735#endif
1736#else
1737 QPrinter printer;
1738 // printer.setPageSize(pageSize);
1739
1740 // FIXME : L. Garnier 4/12/07
1741 // This is not working, it does nothing. Image is staying in color mode
1742 // So I have desactivate the B/W button in GUI
1743 if ((!aImage.isGrayscale ()) &&(aInColor ==1 )) {
1744#if QT_VERSION < 0x040000
1745 aImage = aImage.convertDepth(1,Qt::MonoOnly);
1746#else
1747 aImage = aImage.convertToFormat ( aImage.format(), Qt::MonoOnly);
1748#endif
1749 }
1750
1751
1752 if (aFilename.endsWith(".ps")) {
1753#if QT_VERSION > 0x040200
1754 printer.setOutputFormat(QPrinter::PostScriptFormat);
1755#endif
1756 } else {
1757#if QT_VERSION > 0x040100
1758 printer.setOutputFormat(QPrinter::PdfFormat);
1759#endif
1760 }
1761#if QT_VERSION > 0x040100
1762 printer.setOutputFileName(aFilename);
1763#endif
1764 // printer.setFullPage ( true);
1765 QPainter paint(&printer);
1766 paint.drawImage (0,0,aImage);
1767 paint.end();
1768#endif
1769 return true;
1770}
1771
1772
1773void G4OpenGLQtViewer::G4keyPressEvent (QKeyEvent * event)
1774{
1775 if (holdKeyEvent)
1776 return;
1777
1778 holdKeyEvent = true;
1779
1780 if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::AltModifier )) { // go backward
1781 G4MouseMoveEvent(0,0,1);
1782 }
1783 else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::AltModifier)) { // go forward
1784 G4MouseMoveEvent(0,0,-1);
1785 }
1786 if ((event->key() == Qt::Key_Down) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1787 G4MouseRotateEvent(0,-1);
1788 }
1789 else if ((event->key() == Qt::Key_Up) && (event->modifiers() & Qt::ShiftModifier)) { // rotate phi
1790 G4MouseRotateEvent(0,1);
1791 }
1792 if ((event->key() == Qt::Key_Left) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1793 G4MouseRotateEvent(1,0);
1794 }
1795 else if ((event->key() == Qt::Key_Right) && (event->modifiers() & Qt::ShiftModifier)) { // rotate theta
1796 G4MouseRotateEvent(-1,0);
1797 }
1798 if (event->key() == Qt::Key_Escape) { // escaped from full screen
1799 toggleFullScreen();
1800 }
1801 if (event->key() == Qt::Key_Plus) { // zoom in
1802 fVP.SetZoomFactor(fVP.GetZoomFactor()*fDeltaZoom);
1803 updateQWidget();
1804 }
1805 else if (event->key() == Qt::Key_Minus) { // zoom out
1806 fVP.SetZoomFactor(fVP.GetZoomFactor()/fDeltaZoom);
1807 updateQWidget();
1808 }
1809 // with no modifiers
1810 if (!event->modifiers()) {
1811 if (event->key() == Qt::Key_Down) { // go down
1812 G4MouseMoveEvent(0,1,0);
1813 }
1814 else if (event->key() == Qt::Key_Up) { // go up
1815 G4MouseMoveEvent(0,-1,0);
1816 }
1817 if (event->key() == Qt::Key_Left) { // go left
1818 G4MouseMoveEvent(-1,0,0);
1819 }
1820 else if (event->key() == Qt::Key_Right) { // go right
1821 G4MouseMoveEvent(1,0,0);
1822 }
1823 }
1824 holdKeyEvent = false;
1825}
1826
1827/*
1828
1829void MultiLayer::exportToSVG(const QString& fname)
1830{
1831 QPicture picture;
1832 QPainter p(&picture);
1833 for (int i=0;i<(int)graphsList->count();i++)
1834 {
1835 Graph *gr=(Graph *)graphsList->at(i);
1836 Plot *myPlot= (Plot *)gr->plotWidget();
1837
1838 QPoint pos=gr->pos();
1839
1840 int width=int(myPlot->frameGeometry().width());
1841 int height=int(myPlot->frameGeometry().height());
1842
1843 myPlot->print(&p, QRect(pos,QSize(width,height)));
1844 }
1845
1846 p.end();
1847 picture.save(fname, "svg");
1848}
1849*/
1850#endif
Note: See TracBrowser for help on using the repository browser.