Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef toolx_Qt_session 5 #define toolx_Qt_session 6 7 // pure Qt code, no GL. 8 9 #include "s2q" 10 11 #include <QtCore/qnamespace.h> 12 13 #if QT_VERSION < 0x050000 14 #include <QtGui/qapplication.h> 15 #include <QtGui/qwidget.h> 16 #else 17 #include <QtWidgets/qapplication.h> 18 #include <QtWidgets/qwidget.h> 19 #endif 20 21 #include <ostream> 22 #include <vector> 23 24 namespace toolx { 25 namespace Qt { 26 27 class session { 28 public: 29 session(std::ostream& a_out,int& a_argc,char 30 :m_out(a_out) 31 ,m_qapp(0) 32 ,m_own_qapp(false) 33 { 34 if(qApp) { 35 m_qapp = qApp; 36 m_own_qapp = false; 37 } else { 38 m_qapp = new QApplication(a_argc,a_argv) 39 m_own_qapp = true; 40 } 41 } 42 session(std::ostream& a_out,QApplication* a_ 43 :m_out(a_out) 44 ,m_qapp(a_qapp) 45 ,m_own_qapp(false) 46 {} 47 virtual ~session() { 48 if(m_own_qapp && m_qapp) delete m_qapp; 49 m_qapp = 0; 50 m_own_qapp = false; 51 } 52 protected: 53 session(const session& a_from) 54 :m_out(a_from.m_out) 55 ,m_qapp(0) 56 ,m_own_qapp(false) 57 {} 58 session& operator=(const session& a_from){ 59 if(&a_from==this) return *this; 60 return *this; 61 } 62 public: 63 std::ostream& out() const {return m_out;} 64 bool is_valid() const {return m_qapp?true:fa 65 bool steer() { 66 if(!m_qapp) return false; 67 m_qapp->exec(); 68 return true; 69 } 70 bool sync() { 71 if(!m_qapp) return false; 72 m_qapp->processEvents(); 73 return true; 74 } 75 public: 76 QApplication* qapp() const {return m_qapp;} 77 QWidget* create_window(const char* a_title,i 78 if(!m_qapp) return 0; 79 #if defined(_MSC_VER) || defined(__MINGW32__) 80 if(a_y<=0) a_y = 60; 81 #endif 82 QWidget* top = new QWidget(); 83 top->setWindowFlags(::Qt::CustomizeWindowH 84 | ::Qt::WindowTitleHint 85 | ::Qt::WindowMinMaxButt 86 // | ::Qt::WindowSystemMenu 87 // | ::Qt::WindowFullscreenButtonHint 88 ); 89 top->setGeometry(a_x,a_y,a_width,a_height) 90 top->setWindowTitle(s2q(a_title)); 91 //top->setAttribute(Qt::WA_DeleteOnClose,fal 92 return top; 93 } 94 void delete_window(QWidget* a_window) const 95 if(!m_qapp) return; 96 delete a_window; 97 } 98 protected: 99 std::ostream& m_out; 100 QApplication* m_qapp; 101 bool m_own_qapp; 102 }; 103 104 }} 105 106 107 #endif 108