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

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

update sur cvs

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