ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLQtViewer.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenGLQtViewer.hh
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 //
28 //
29 // G4OpenGLQtViewer : Class to provide WindowsNT specific
30 // functionality for OpenGL in GEANT4
31 //
32 // 30/06/2014 : M.Kelsey : Change QPixmap objects to pointers
33 
34 #if defined (G4VIS_BUILD_OPENGLQT_DRIVER) || defined (G4VIS_USE_OPENGLQT)
35 
36 #ifndef G4OPENGLQTVIEWER_HH
37 #define G4OPENGLQTVIEWER_HH
38 
39 #include "globals.hh"
40 
41 #include "G4OpenGLViewer.hh"
42 #include "G4PhysicalVolumeModel.hh"
43 #include "G4AutoLock.hh"
44 
45 #include <qobject.h>
46 #include <qpoint.h>
47 #include <qpixmap.h>
48 
49 class G4OpenGLSceneHandler;
50 class G4UImanager;
51 class G4Text;
52 class G4UIcommand;
53 
54 class QGLWidget;
55 class QDialog;
56 class QTextEdit;
57 class QContextMenuEvent;
58 class QMenu;
59 class QImage;
60 class QAction;
61 class QTabWidget;
62 class QMouseEvent;
63 class QKeyEvent;
64 class QWheelEvent;
65 class QProcess;
66 class QTime;
67 class QVBoxLayout;
68 class QPushButton;
69 class QSlider;
70 class QTreeWidgetItem;
71 class QTreeWidget;
72 class QColor;
73 class G4OpenGLSceneHandler;
74 class G4OpenGLQtMovieDialog;
75 class QLineEdit;
76 #if QT_VERSION < 0x050600
77 class QSignalMapper;
78 #endif
79 class G4UIQt;
80 class QTableWidget;
81 class QTableWidgetItem;
82 class QScrollArea;
83 class QSplitter;
84 
85 class G4OpenGLQtViewer: public QObject, virtual public G4OpenGLViewer {
86 
87  Q_OBJECT
88 
90  typedef std::vector<PVNodeID> PVPath;
91 
92 public:
93  G4OpenGLQtViewer (G4OpenGLSceneHandler& scene);
94  virtual ~G4OpenGLQtViewer ();
95 #ifdef G4MULTITHREADED
96  // In MT mode these functions are called in the following order for each run:
97  // Called on the master thread before starting the vis sub-thread.
98  virtual void DoneWithMasterThread ();
99  // Called on the master thread after starting the vis sub-thread.
100  virtual void MovingToVisSubThread ();
101  // Called on the vis sub-thread when waiting for events.
102  virtual void SwitchToVisSubThread ();
103  // Called on the vis sub-thread when all events have been processed.
104  virtual void DoneWithVisSubThread ();
105  // Called on the vis sub-thread when all events have been processed.
106  // virtual void MovingToMasterThread (); Not used in G4OpenGLQtViewer.
107  // Called on the master thread after the vis sub-thread has terminated.
108  virtual void SwitchToMasterThread ();
109 #endif
110 
111 private:
112  G4OpenGLQtViewer (const G4OpenGLQtViewer&);
113  G4OpenGLQtViewer& operator= (const G4OpenGLQtViewer&);
114 public:
115  virtual void updateQWidget()=0;
116  void updateSceneTreeWidget();
117  void updateViewerPropertiesTableWidget();
118  void updatePickInfosWidget(int, int);
119  QString setEncoderPath(QString path);
120  QString getEncoderPath();
121  QString setTempFolderPath(QString path);
122  QString getTempFolderPath();
123  QString setSaveFileName(QString path);
124  QString getSaveFileName();
125  bool isRecording();
126  bool isStopped();
127  bool isPaused();
128  bool isEncoding();
129  bool isWaiting();
130  bool isFailed();
131  void setWaiting();
132  bool isBadEncoder();
133  bool isBadOutput();
134  bool isBadTmp();
135  bool isSuccess();
136  void setBadTmp();
137  void setBadOutput();
138  void setBadEncoder();
139  bool isReadyToEncode();
140  void resetRecording();
141  void encodeVideo();
142  void stopVideo();
143  void saveVideo();
144  bool generateMpegEncoderParameters();
145  void displayRecordingStatus();
146  void DrawText(const G4Text&);
147  void ResetView ();
148  void addPVSceneTreeElement(const G4String& model,
149  G4PhysicalVolumeModel* pPVModel,
150  int currentPVPOIndex);
151  void addNonPVSceneTreeElement(const G4String& model,
152  int currentPVPOIndex,
153  const std::string& modelDescription,
154  const G4Visible& visible);
155  bool isTouchableVisible(int POindex);
156  void clearTreeWidget();
157  bool exportImage(std::string name="", int width=-1, int height=-1);
158 
159 public:
160  void G4MousePressEvent(QMouseEvent *event);
161  void G4wheelEvent (QWheelEvent * event);
162  void G4keyPressEvent (QKeyEvent * event);
163  void G4keyReleaseEvent (QKeyEvent * event);
164  void G4MouseDoubleClickEvent();
165  void G4MouseReleaseEvent(QMouseEvent *evnt);
166  void G4MouseMoveEvent(QMouseEvent *event);
167 
168 protected:
169  void CreateGLQtContext ();
170  virtual void CreateMainWindow (QGLWidget*,const QString&);
171  void G4manageContextMenuEvent(QContextMenuEvent *e);
172  void rotateQtScene(float, float);
173  void rotateQtSceneToggle(float, float);
174  void moveScene(float, float, float,bool);
175  void FinishView();
176  void updateKeyModifierState(const Qt::KeyboardModifiers&);
177  void displaySceneTreeComponent();
178  G4Colour getColorForPoIndex(int poIndex);
179 
180  // So that privately accumulated vis attributes modifiers may be
181  // concatenated with the standard vis attributes modifiers for commands
182  // such as /vis/viewer/set/all and /vis/viewer/save...
183  const std::vector<G4ModelingParameters::VisAttributesModifier>*
184  GetPrivateVisAttributesModifiers() const;
185  bool isCurrentWidget();
186 
187 protected:
188  QWidget* fGLWidget;
189  void savePPMToTemp();
190  int fRecordFrameNumber;
191 
192  bool fHasToRepaint;
193  bool fUpdateGLLock;
194  bool fQGLWidgetInitialiseCompleted;
195  bool fPaintEventLock;
196 
197  // Flag to indicate that action was initiated by interaction (mouse
198  // click) on the scene tree. It is used and reset in
199  // G4OpenGLStoredQtViewer::CompareForKernelVisit to prevent rebuild
200  // in this case.
201  bool fMouseOnSceneTree;
202 
203 private:
204  enum RECORDING_STEP {WAIT,START,PAUSE,CONTINUE,STOP,READY_TO_ENCODE,ENCODING,FAILED,SUCCESS,BAD_ENCODER,BAD_OUTPUT,BAD_TMP,SAVE};
205 
206  void createPopupMenu();
207  void createRadioAction(QAction *,QAction *, const std::string&,unsigned int a=1);
208  void rescaleImage(int, int);
209  bool printPDF(const std::string,int,QImage);
210  void showMovieParametersDialog();
211  void initMovieParameters();
212  QString createTempFolder();
213  QString removeTempFolder();
214  void setRecordingStatus(RECORDING_STEP);
215  void setRecordingInfos(const QString&);
216  QString getProcessErrorMsg();
217  QWidget* getParentWidget();
218  bool parseAndInsertInSceneTree(QTreeWidgetItem *,
219  G4PhysicalVolumeModel* pPVModel,
220  unsigned int fullPathIndex,
221  const QString& parentRoot,
222  unsigned int currentIndex,
223  int currentPVPOIndex);
224  void setCheckComponent(QTreeWidgetItem* item,bool check);
225  void createSceneTreeComponent();
226  void createSceneTreeWidget();
227  void createViewerPropertiesWidget();
228  void createPickInfosWidget();
229  bool parseAndCheckVisibility(QTreeWidgetItem * treeNode,int POindex);
230  QTreeWidgetItem* createTreeWidgetItem(const PVPath& fullPath,
231  const QString& name,
232  int copyNb,
233  int POIndex,
234  const QString& logicalName,
235  Qt::CheckState state,
236  QTreeWidgetItem * treeNode,
237  const G4Colour& color);
238  QString getModelShortName(const G4String& modelShortName);
239  void cloneSceneTree(QTreeWidgetItem* rootItem);
240  void changeDepthOnSceneTreeItem(double lookForDepth,double currentDepth,QTreeWidgetItem* item);
241  void updatePositivePoIndexSceneTreeWidgetQuickMap(int POindex,QTreeWidgetItem* item);
242  void changeQColorForTreeWidgetItem(QTreeWidgetItem* item, const QColor&);
243 
244  bool isSameSceneTreeElement(QTreeWidgetItem* parentOldItem,QTreeWidgetItem* parentNewItem);
245  void changeOpenCloseVisibleHiddenSelectedColorSceneTreeElement(QTreeWidgetItem* subItem);
246  bool isPVVolume(QTreeWidgetItem* item);
247  QTreeWidgetItem* cloneWidgetItem(QTreeWidgetItem* item);
248  void clearSceneTreeSelection(QTreeWidgetItem*);
249  void clearTreeWidgetElements(QTreeWidgetItem* item);
250 
251  // Get the tree wigdet item for POindex if exists
252  QTreeWidgetItem* getTreeWidgetItem(int POindex);
253 
254  // Get the old tree wigdet item for POindex if exists
255  QTreeWidgetItem* getOldTreeWidgetItem(int POindex);
256 
257 // parse the scene tree and return a string of status that can be saved
258  std::string parseSceneTreeAndSaveState();
259 
260  std::string parseSceneTreeElementAndSaveState(QTreeWidgetItem* item, unsigned int level);
261  QString GetCommandParameterList (const G4UIcommand *aCommand);
262  void changeColorAndTransparency(GLuint index, G4Color color);
263 
264 #ifdef G4MULTITHREADED
265  inline void SetQGLContextVisSubThread(QThread *th) {
266  fQGLContextVisSubThread = th;
267  }
268  inline void SetQGLContextMainThread(QThread *th) {
269  fQGLContextMainThread = th;
270  }
271 #endif
272 
273  QMenu *fContextMenu;
274  QPoint fLastPos1;
275  QPoint fLastPos2;
276  QPoint fLastPos3;
277  QPoint fLastPickPoint;
278 
279  // delta of depth move. This delta is put in % of the scene view
280  G4double fDeltaDepth;
281  // delta of zoom move. This delta is put in % of the scene view
282  G4double fDeltaZoom;
283  // To ensure key event are keep one by one
284  bool fHoldKeyEvent;
285  // To ensure move event are keep one by one
286  bool fHoldMoveEvent;
287  // To ensure rotate event are keep one by one
288  bool fHoldRotateEvent;
289  bool fAutoMove;
290  QString fEncoderPath;
291  QString fTempFolderPath;
292  QString fMovieTempFolderPath;
293  QString fSaveFileName;
294  QString fParameterFileName;
295  QAction *fMouseRotateAction;
296  QAction *fMouseMoveAction;
297  QAction *fMousePickAction;
298  QAction *fMouseZoomInAction;
299  QAction *fMouseZoomOutAction;
300  QAction *fFullScreenOn;
301  QAction *fFullScreenOff;
302  QAction *fDrawingWireframe;
303  QAction *fDrawingLineRemoval;
304  QAction *fDrawingSurfaceRemoval;
305  QAction *fDrawingLineSurfaceRemoval;
306  QAction *fProjectionOrtho;
307  QAction *fProjectionPerspective;
308  G4OpenGLQtMovieDialog* fMovieParametersDialog;
309  RECORDING_STEP fRecordingStep;
310  QProcess *fProcess;
311  QTime *fLastEventTime;
312  int fSpinningDelay;
313  int fNbMaxFramesPerSec;
314  float fNbMaxAnglePerSec;
315  int fLaunchSpinDelay;
316  QWidget* fUISceneTreeWidget;
317  QWidget* fUIViewerPropertiesWidget;
318  QWidget* fUIPickInfosWidget;
319  bool fNoKeyPress;
320  bool fAltKeyPress;
321  bool fControlKeyPress;
322  bool fShiftKeyPress;
323  bool fBatchMode;
324  bool fCheckSceneTreeComponentSignalLock;
325  bool fViewerPropertiesTableWidgetIsInit;
326  QTreeWidget* fSceneTreeComponentTreeWidget;
327  // This is only use to hold the old "expand" value, see file:///Developer/Documentation/Qt/html/qtreewidgetitem.html#setExpanded
328  QWidget* fSceneTreeWidget;
329  bool fPVRootNodeCreate;
330  QLineEdit* fFilterOutput;
331  QString fFileSavePath;
332  int fNbRotation ;
333  int fTimeRotation;
334  QString fTouchableVolumes;
335  QDialog* fShortcutsDialog;
336  QTableWidget *fViewerPropertiesTableWidget;
337  QWidget* fPickInfosWidget;
338  QScrollArea* fPickInfosScrollArea;
339  int fTreeWidgetInfosIgnoredCommands;
340  QPushButton * fSceneTreeButtonApply;
341  QTextEdit *fShortcutsDialogInfos;
342  QSlider* fSceneTreeDepthSlider;
343  std::map <int, PVPath > fTreeItemModels;
344  std::map <int, PVPath > fOldTreeItemModels;
345 
346  // quick scene tree map
347  std::map <int, QTreeWidgetItem*> fPositivePoIndexSceneTreeWidgetQuickMap;
348  // old scene tree map
349  std::map <int, QTreeWidgetItem*> fOldPositivePoIndexSceneTreeWidgetQuickMap;
350  std::vector <QTreeWidgetItem*> fOldNullPoIndexSceneTreeWidgetQuickVector;
351  // old vis attr color map
352  std::map <int, QColor> fOldVisAttrColorMap;
353 
354  unsigned int fSceneTreeDepth;
355  QTreeWidgetItem* fModelShortNameItem;
356  int fNumber;
357  int fMaxPOindexInserted;
358  G4UIQt* fUiQt;
359 #if QT_VERSION < 0x050600
360  QSignalMapper *fSignalMapperMouse;
361  QSignalMapper *fSignalMapperSurface;
362  QSignalMapper *fSignalMapperPicking;
363 #endif
364  // quick map index to find next item
365  std::map <int, QTreeWidgetItem*>::const_iterator fLastSceneTreeWidgetAskForIterator;
366  std::map <int, QTreeWidgetItem*>::const_iterator fLastSceneTreeWidgetAskForIteratorEnd;
367 
368  // quick map index to find next item
369  std::map <int, QTreeWidgetItem*>::const_iterator fOldLastSceneTreeWidgetAskForIterator;
370  std::map <int, QTreeWidgetItem*>::const_iterator fOldLastSceneTreeWidgetAskForIteratorEnd;
371 
372  // icons
373  QPixmap* fTreeIconOpen;
374  QPixmap* fTreeIconClosed;
375  QPixmap* fSearchIcon;
376 
377  int fLastExportSliderValue;
378  G4Color fLastHighlightColor;
379  GLuint fLastHighlightName;
380  bool fIsDeleting;
381 
382 #ifdef G4MULTITHREADED
383  QThread* fQGLContextVisSubThread;
384  QThread* fQGLContextMainThread;
385 #endif
386 
387  // safe to use in serial mode
388  G4AutoLock* lWaitForVisSubThreadQtOpenGLContextInitialized;
389  G4AutoLock* lWaitForVisSubThreadQtOpenGLContextMoved;
390 
391 public Q_SLOTS :
392  void startPauseVideo();
393 
394 protected Q_SLOTS :
395  void updateToolbarAndMouseContextMenu();
396 
397 private Q_SLOTS :
398  void actionSaveImage();
399  void actionChangeBackgroundColor();
400  void actionChangeTextColor();
401  void actionChangeDefaultColor();
402  void actionMovieParameters();
403 
404  void showShortcuts();
405  void toggleMouseAction(int);
406  void toggleSurfaceAction(int);
407  void toggleProjection(bool);
408  void toggleTransparency(bool);
409  void toggleAntialiasing(bool);
410  void toggleHaloing(bool);
411  void toggleAux(bool);
412  void toggleHiddenMarkers(bool);
413  void toggleFullScreen(bool);
414  void processEncodeFinished();
415  void processLookForFinished();
416  void processEncodeStdout();
417  void sceneTreeComponentItemChanged(QTreeWidgetItem* item, int id);
418  void toggleSceneTreeComponentPickingCout(int);
419  void togglePicking();
420  void currentTabActivated(int);
421 
422  // action trigger by a click on a component scene tree
423  void sceneTreeComponentSelected();
424  void changeDepthInSceneTree(int);
425  void changeSearchSelection();
426  void changeColorAndTransparency(QTreeWidgetItem* item,int val);
427  void tableWidgetViewerSetItemChanged(QTableWidgetItem *);
428 };
429 
430 #endif
431 
432 #endif