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

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

r629@mac-90108: laurentgarnier | 2007-11-09 15:34:57 +0100
mise a jour

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