Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef toolx_Qt_glarea 5 #define toolx_Qt_glarea 6 7 #include "../sg/GL_viewer" 8 9 #include <QtCore/qglobal.h> //For QT_VERSION. 10 11 #if QT_VERSION < 0x060000 12 #include <QtOpenGL/qgl.h> 13 #else 14 #include <QtOpenGLWidgets/qopenglwidget.h> 15 #endif 16 17 #include <QtGui/qevent.h> 18 19 #include <tools/sg/device_interactor> 20 #ifdef TOOLS_MEM 21 #include <tools/mem> 22 #endif 23 24 namespace toolx { 25 namespace Qt { 26 27 class glarea 28 #if QT_VERSION < 0x060000 29 :public QGLWidget 30 #else 31 :public QOpenGLWidget 32 #endif 33 { 34 #if QT_VERSION < 0x060000 35 typedef QGLWidget parent; 36 #else 37 using parent = QOpenGLWidget; 38 #endif 39 TOOLS_SCLASS(toolx::Qt::glarea) 40 public: 41 virtual void initializeGL() {} 42 virtual void paintGL() { 43 #if QT_VERSION < 0x050000 44 m_viewer.set_size(width(),height()); 45 #else 46 m_viewer.set_size(devicePixelRatio()*width(),devicePixelRatio()*height()); 47 #endif 48 m_viewer.render(); 49 } 50 51 virtual void keyPressEvent(QKeyEvent* a_event) { 52 if(!m_interactor) return; 53 tools::sg::key_down_event _event(convert_key(a_event->key())); 54 m_interactor->key_press(_event); 55 } 56 virtual void keyReleaseEvent(QKeyEvent* a_event) { 57 if(!m_interactor) return; 58 tools::sg::key_up_event _event(convert_key(a_event->key())); 59 m_interactor->key_release(_event); 60 } 61 virtual void mousePressEvent(QMouseEvent* a_event) { 62 if(!m_interactor) return; 63 #if QT_VERSION < 0x060000 64 tools::sg::mouse_down_event _event(a_event->x(),a_event->y()); 65 #else 66 tools::sg::mouse_down_event _event(a_event->position().x(),a_event->position().y()); 67 #endif 68 m_interactor->mouse_press(_event); 69 } 70 virtual void mouseReleaseEvent(QMouseEvent* a_event) { 71 if(!m_interactor) return; 72 #if QT_VERSION < 0x060000 73 tools::sg::mouse_up_event _event(a_event->x(),a_event->y()); 74 #else 75 tools::sg::mouse_up_event _event(a_event->position().x(),a_event->position().y()); 76 #endif 77 m_interactor->mouse_release(_event); 78 } 79 virtual void mouseMoveEvent(QMouseEvent* a_event) { 80 if(!m_interactor) return; 81 #if QT_VERSION < 0x060000 82 tools::sg::mouse_move_event _event(a_event->x(),a_event->y(),0,0,false); 83 #else 84 tools::sg::mouse_move_event _event(a_event->position().x(),a_event->position().y(),0,0,false); 85 #endif 86 m_interactor->mouse_move(_event); 87 } 88 virtual void wheelEvent(QWheelEvent* a_event) { 89 if(!m_interactor) return; 90 tools::sg::wheel_rotate_event _event(a_event->angleDelta().y()); 91 m_interactor->wheel_rotate(_event); 92 } 93 94 public: 95 glarea(QWidget* a_parent,toolx::sg::GL_viewer& a_viewer):parent(a_parent),m_viewer(a_viewer),m_interactor(0){ 96 #ifdef TOOLS_MEM 97 tools::mem::increment(s_class().c_str()); 98 #endif 99 } 100 virtual ~glarea(){ 101 #ifdef TOOLS_MEM 102 tools::mem::decrement(s_class().c_str()); 103 #endif 104 } 105 public: 106 void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership. 107 protected: 108 tools::key_code convert_key(/*Qt::Key*/int a_key) {return a_key;} 109 protected: 110 toolx::sg::GL_viewer& m_viewer; 111 tools::sg::device_interactor* m_interactor; 112 }; 113 114 }} 115 116 117 #endif