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_pixwin 5 #define toolx_Qt_pixwin 6 7 #include <QtCore/qglobal.h> //For QT_VERSION. 8 9 #if QT_VERSION < 0x050000 10 #include <QtGui/qwidget.h> 11 #else 12 #include <QtWidgets/qwidget.h> 13 #endif 14 15 #include <QtGui/qevent.h> 16 #include <QtGui/qpainter.h> 17 18 #include <tools/sg/zb_viewer> 19 #include <tools/sg/device_interactor> 20 21 #ifdef TOOLS_MEM 22 #include <tools/mem> 23 #endif 24 25 namespace toolx { 26 namespace Qt { 27 28 class pixwin : public QWidget 29 { 30 typedef QWidget parent; 31 TOOLS_SCLASS(toolx::Qt::pixwin) 32 public: 33 virtual void resizeEvent(QResizeEvent* a_event) { 34 //::printf("debug : toolx::Qt::pixwin::resizeEvent : %d, %d\n",a_event->size().width(),a_event->size().height()); 35 parent::resizeEvent(a_event); 36 m_viewer.set_size(a_event->size().width(),a_event->size().height()); 37 } 38 virtual void paintEvent(QPaintEvent* a_event) { 39 parent::paintEvent(a_event); 40 if(!m_viewer.render(tools::sg::zb_viewer::get_rgbas,false)) return; //rgbas because Qt wants an image 32 bits aligned. 41 //static size_t s_count = 0;::printf("debug : toolx::Qt::pixwin::paintEvent %d %d : %lu\n",parent::m_ww,parent::m_wh,s_count);s_count++; 42 QImage image(tools::vec_data(m_viewer.out_buffer()),m_viewer.width(),m_viewer.height(),QImage::Format_RGBA8888); 43 QPixmap pixmap = QPixmap::fromImage(image); 44 QPainter painter(this); 45 painter.drawPixmap(0,0,pixmap); 46 //parent::after_render(); 47 m_viewer.out_buffer_clear(); 48 } 49 50 virtual void keyPressEvent(QKeyEvent* a_event) { 51 if(!m_interactor) return; 52 tools::sg::key_down_event _event(convert_key(a_event->key())); 53 m_interactor->key_press(_event); 54 } 55 virtual void keyReleaseEvent(QKeyEvent* a_event) { 56 if(!m_interactor) return; 57 tools::sg::key_up_event _event(convert_key(a_event->key())); 58 m_interactor->key_release(_event); 59 } 60 virtual void mousePressEvent(QMouseEvent* a_event) { 61 if(!m_interactor) return; 62 #if QT_VERSION < 0x060000 63 tools::sg::mouse_down_event _event(a_event->x(),a_event->y()); 64 #else 65 tools::sg::mouse_down_event _event(a_event->position().x(),a_event->position().y()); 66 #endif 67 m_interactor->mouse_press(_event); 68 } 69 virtual void mouseReleaseEvent(QMouseEvent* a_event) { 70 if(!m_interactor) return; 71 #if QT_VERSION < 0x060000 72 tools::sg::mouse_up_event _event(a_event->x(),a_event->y()); 73 #else 74 tools::sg::mouse_up_event _event(a_event->position().x(),a_event->position().y()); 75 #endif 76 m_interactor->mouse_release(_event); 77 } 78 virtual void mouseMoveEvent(QMouseEvent* a_event) { 79 if(!m_interactor) return; 80 #if QT_VERSION < 0x060000 81 tools::sg::mouse_move_event _event(a_event->x(),a_event->y(),0,0,false); 82 #else 83 tools::sg::mouse_move_event _event(a_event->position().x(),a_event->position().y(),0,0,false); 84 #endif 85 m_interactor->mouse_move(_event); 86 } 87 //virtual void mouseDoubleClickEvent(QMouseEvent* a_event) { 88 // if(!m_interactor) return; 89 // tools::sg::mouse_double_click_event _event(a_event->x(),a_event->y()); 90 // m_interactor->mouse_double_click(_event); 91 //} 92 virtual void wheelEvent(QWheelEvent* a_event) { 93 if(!m_interactor) return; 94 tools::sg::wheel_rotate_event _event(a_event->angleDelta().y()); 95 m_interactor->wheel_rotate(_event); 96 } 97 98 public: 99 pixwin(QWidget* a_parent,tools::sg::zb_viewer& a_viewer):parent(a_parent),m_viewer(a_viewer),m_interactor(0){ 100 #ifdef TOOLS_MEM 101 tools::mem::increment(s_class().c_str()); 102 #endif 103 } 104 virtual ~pixwin(){ 105 #ifdef TOOLS_MEM 106 tools::mem::decrement(s_class().c_str()); 107 #endif 108 } 109 public: 110 void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership. 111 protected: 112 tools::key_code convert_key(/*Qt::Key*/int a_key) {return a_key;} 113 protected: 114 tools::sg::zb_viewer& m_viewer; 115 tools::sg::device_interactor* m_interactor; 116 }; 117 118 }} 119 120 121 #endif