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