GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QRootWindow.cpp
Go to the documentation of this file.
1 // $Id: QRootWindow.cpp 1950 2016-09-12 12:43:45Z adamczew $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "QRootWindow.h"
15 
16 #include "TSystem.h"
17 #include "GuiTypes.h"
18 #include "TGFrame.h"
19 #include "Riostream.h"
20 
21 #include <QPainter>
22 #include <QMouseEvent>
23 #include <QCloseEvent>
24 #include <QPaintEvent>
25 
26 #include "TGo4LockGuard.h"
27 
29 class TQRootFrame: public TGCompositeFrame {
30  public:
32  TQRootFrame(Window_t id) :
33  TGCompositeFrame(gClient, id, new TGMainFrame(gClient->GetDefaultRoot(),100,100))
34  {
35  // we have to offer a real mainframe as top parent for root internal lists:
36  // fParent=new TGMainFrame(gClient->GetDefaultRoot(),100,100);
37  }
38 
39  virtual ~TQRootFrame()
40  {
41  delete fParent;
42  }
43 
44  virtual void CloseWindow()
45  {
46  UnmapWindow(); // capture the close window
47  }
48 
49  /* virtual Bool_t HandleEvent(Event_t *event)
50  {
51  return TGCompositeFrame::HandleEvent(event);
52  }
53  */
54 
55 };
56 
57 
58 
60 // This is the widget to be embedded in qt:
61 
62 QRootWindow::QRootWindow( QWidget *parent, const char *name, bool designermode) :
63  QWidget(parent),
64  fxRootwindow(0),
65  fbResizeOnPaint(kTRUE),fQtScalingfactor(1.0)
66 {
67  setObjectName( name ? name : "QRootWindow");
68 
69  // set defaults
70  setUpdatesEnabled( true );
71  setMouseTracking(true);
72 
73  setFocusPolicy( Qt::TabFocus );
74  setCursor( Qt::CrossCursor );
75 
76  if(!designermode) {
77  // add the Qt::WinId to TGX11 interface
78  fQtWinId = winId();
79  fiWinid = gVirtualX->AddWindow((ULong_t)fQtWinId,145,400);
80  //std::cout <<"QRootWindow ctor added window for "<<fQtWinId<<" with ROOT wid:"<<fiWinid<< std::endl;
81  fxRootwindow = new TQRootFrame(gVirtualX->GetWindowID(fiWinid));
82  fxRootwindow->Resize();
83  if ( parent ) parent->installEventFilter( this );
84 #if QT_VERSION > QT_VERSION_CHECK(5,6,0)
85  // JAM the following is pure empiric. hopefully default denominator won't change in future qt?
86  fQtScalingfactor=(double) metric(QPaintDevice::PdmDevicePixelRatioScaled)/65536.;
87 #endif
88 
89  }
90 }
91 
92 void QRootWindow::AddSubFrame(TGFrame* f, TGLayoutHints* l)
93 {
94  fxRootwindow->AddFrame(f,l);
95  fxRootwindow->MapWindow();
96  fxRootwindow->MapSubwindows();
97  fxRootwindow->Resize();
98  ensurePolished();
99  update();
100  show();
101 }
102 
104 {
105  fxRootwindow->SetEditable(on);
106 }
107 
108 Bool_t QRootWindow::MapQMouseEvent(QMouseEvent *e, Event_t* rev)
109 {
110  if((e==0) || (rev==0)) return kFALSE;
111 
112  rev->fX=e->x();
113  rev->fY=e->y();
114  rev->fXRoot= e->globalX();
115  rev->fYRoot= e->globalY();
116 
117  // translate Qt event type:
118  if(e->type() == QEvent::MouseButtonPress) rev->fType = kButtonPress;
119  else if(e->type() == QEvent::MouseButtonRelease) rev->fType = kButtonRelease;
120  else if(e->type() == QEvent::MouseButtonDblClick) rev->fType = kButtonDoubleClick;
121  else if(e->type() == QEvent::MouseMove) rev->fType = kMotionNotify;
122  else if(e->type() == QEvent::KeyPress) rev->fType = kGKeyPress;
123  else if(e->type() == QEvent::KeyRelease) rev->fType = kKeyRelease;
124  else rev->fType = kOtherEvent;
125 
126  // translate Qt state bits:
127  rev->fState=0;
128  if(e->buttons() & Qt::LeftButton)
129  rev->fState |= kButton1Mask;
130  if(e->buttons() & Qt::RightButton)
131  rev->fState |= kButton3Mask;
132  if(e->buttons() & Qt::MidButton)
133  rev->fState |= kButton2Mask;
134  if(e->buttons() & Qt::MouseButtonMask)
135  rev->fState |= kButton1Mask;
136 
137  if(e->modifiers() & Qt::ShiftModifier)
138  rev->fState |= kKeyShiftMask;
139  if(e->modifiers() & Qt::ControlModifier)
140  rev->fState |= kKeyControlMask;
141  if(e->modifiers() & Qt::AltModifier)
142  rev->fState |= kKeyMod1Mask;
143  if(e->modifiers() & Qt::MetaModifier)
144  rev->fState |= kKeyMod1Mask;
145  //if(e->buttons() & KeyButtonMask)
146  // rev->fState |= ShiftMask;
147  //if(e->buttons() & Keypad)
148  // rev->fState |= ShiftMask;
149 
150  rev->fCode = Qt::NoButton; // button code
151  if(e->button() == Qt::LeftButton)
152  rev->fCode |= kButton1Mask;
153  if(e->button() == Qt::RightButton)
154  rev->fCode |= kButton3Mask;
155  if(e->button() == Qt::MidButton)
156  rev->fCode |= kButton2Mask;
157 
158  rev->fUser[0]=0;
159  rev->fWindow = gVirtualX->GetWindowID(fiWinid); // we refer signals to this window
160  rev->fSendEvent = 0;
161  rev->fTime = 0; // this might cause problems with root doubleclick treatment?
162 
163  return kTRUE;
164 }
165 
166 
167 
168 void QRootWindow::paintEvent( QPaintEvent * e)
169 {
170  //TGo4LockGuard threadlock;
171 
172  if(fxRootwindow) {
173  WId nxid = winId();
174  if(fQtWinId!=nxid) {
175  TGo4LockGuard threadlock;
176  // may happen when this window is embedded to Qt workspace...
177  std::cout <<"Warning: QRootWindow::paintEvent finds changed X window id: "<< (ULong_t) winId()<<", previous:"<<fQtWinId << std::endl;
178  delete fxRootwindow; // should also remove old x windows!
179  fQtWinId = nxid;
180  fiWinid = gVirtualX->AddWindow((ULong_t) fQtWinId, width(), height());
181  fxRootwindow = new TQRootFrame(gVirtualX->GetWindowID(fiWinid));
182  }
183  if(fbResizeOnPaint) {
184  TGo4LockGuard threadlock;
185  //std::cout <<"QRootWindow::paintEvent does TGCompositeFrame Resize ..." << std::endl;
186  fxRootwindow->Resize(width(),height());
187  gVirtualX->Update(1); // Xsync/flus
188  }
189  }
190  QWidget::paintEvent(e);
191 }
192 
193 bool QRootWindow ::eventFilter( QObject *o, QEvent *e )
194 {
195  TGo4LockGuard threadlock;
196  QMouseEvent* me = dynamic_cast<QMouseEvent*>(e);
197 
198  Event_t root_evnt;
199 
200  if (MapQMouseEvent(me, &root_evnt)) {
201  if(fxRootwindow) fxRootwindow->HandleEvent(&root_evnt);
202  return false;
203  }
204 
205  if ( e->type() == QEvent::Close) { // close
206  delete fxRootwindow;
207  fxRootwindow = 0;
208  return false;
209  }
210 
211  // standard event processing
212  return QWidget::eventFilter( o, e );
213 }
214 
215 void QRootWindow::closeEvent( QCloseEvent * e)
216 {
217  if (fxRootwindow) {
218  delete fxRootwindow;
219  fxRootwindow = 0;
220  }
221  e->accept();
222 }
223 
225 {
226  if (fxRootwindow) {
227  delete fxRootwindow;
228  fxRootwindow = 0;
229  }
230 }
231 
232 TGCompositeFrame* QRootWindow::GetRootFrame()
233 {
234  return fxRootwindow;
235 }
236 
237 
238 
240 {
241  return fQtScalingfactor * width();
242 }
243 
245 {
246  return fQtScalingfactor * height();
247 }
248 
virtual void CloseWindow()
Definition: QRootWindow.cpp:44
TQRootFrame(Window_t id)
Definition: QRootWindow.cpp:32
virtual void closeEvent(QCloseEvent *e)
double ScaledHeight()
void AddSubFrame(TGFrame *f, TGLayoutHints *l=0)
Definition: QRootWindow.cpp:92
Int_t fiWinid
Definition: QRootWindow.h:80
double ScaledWidth()
virtual ~QRootWindow()
void SetEditable(bool on=true)
int l
Definition: f_mbs_status.c:34
Bool_t fbResizeOnPaint
Definition: QRootWindow.h:87
virtual ~TQRootFrame()
Definition: QRootWindow.cpp:39
virtual void paintEvent(QPaintEvent *e)
virtual bool eventFilter(QObject *, QEvent *)
double fQtScalingfactor
Definition: QRootWindow.h:89
TGCompositeFrame * GetRootFrame()
TQRootFrame * fxRootwindow
Definition: QRootWindow.h:77
QRootWindow(QWidget *parent=0, const char *name=0, bool designermode=false)
Definition: QRootWindow.cpp:62
Bool_t MapQMouseEvent(QMouseEvent *e, Event_t *rev)