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

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

correction ticket #107 et ajout du picking

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