GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
QRootWindow.cpp
Go to the documentation of this file.
1 // $Id$
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(nullptr),
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  fxRootwindow = new TQRootFrame(gVirtualX->GetWindowID(fiWinid));
83  fxRootwindow->Resize();
84  if (parent) parent->installEventFilter( this );
85  // JAM the following is pure empiric. hopefully default denominator won't change in future qt?
86  fQtScalingfactor = (double) metric(QPaintDevice::PdmDevicePixelRatioScaled)/65536.;
87  }
88 }
89 
90 void QRootWindow::AddSubFrame(TGFrame* f, TGLayoutHints* l)
91 {
92  fxRootwindow->AddFrame(f,l);
93  fxRootwindow->MapWindow();
94  fxRootwindow->MapSubwindows();
95  fxRootwindow->Resize();
96  ensurePolished();
97  update();
98  show();
99 }
100 
102 {
103  fxRootwindow->SetEditable(on);
104 }
105 
106 Bool_t QRootWindow::MapQMouseEvent(QMouseEvent *e, Event_t* rev)
107 {
108  if(!e || !rev) return kFALSE;
109 
110 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
111  rev->fX = e->x();
112  rev->fY = e->y();
113  rev->fXRoot = e->globalX();
114  rev->fYRoot = e->globalY();
115 #else
116  rev->fX = e->position().x();
117  rev->fY = e->position().y();
118  rev->fXRoot = e->globalPosition().x();
119  rev->fYRoot = e->globalPosition().y();
120 #endif
121 
122  // translate Qt event type:
123  if(e->type() == QEvent::MouseButtonPress) rev->fType = kButtonPress;
124  else if(e->type() == QEvent::MouseButtonRelease) rev->fType = kButtonRelease;
125  else if(e->type() == QEvent::MouseButtonDblClick) rev->fType = kButtonDoubleClick;
126  else if(e->type() == QEvent::MouseMove) rev->fType = kMotionNotify;
127  else if(e->type() == QEvent::KeyPress) rev->fType = kGKeyPress;
128  else if(e->type() == QEvent::KeyRelease) rev->fType = kKeyRelease;
129  else rev->fType = kOtherEvent;
130 
131  // translate Qt state bits:
132  rev->fState = 0;
133  if(e->buttons() & Qt::LeftButton)
134  rev->fState |= kButton1Mask;
135  if(e->buttons() & Qt::RightButton)
136  rev->fState |= kButton3Mask;
137  if(e->buttons() & Qt::MiddleButton)
138  rev->fState |= kButton2Mask;
139  if(e->buttons() & Qt::MouseButtonMask)
140  rev->fState |= kButton1Mask;
141 
142  if(e->modifiers() & Qt::ShiftModifier)
143  rev->fState |= kKeyShiftMask;
144  if(e->modifiers() & Qt::ControlModifier)
145  rev->fState |= kKeyControlMask;
146  if(e->modifiers() & Qt::AltModifier)
147  rev->fState |= kKeyMod1Mask;
148  if(e->modifiers() & Qt::MetaModifier)
149  rev->fState |= kKeyMod1Mask;
150  //if(e->buttons() & KeyButtonMask)
151  // rev->fState |= ShiftMask;
152  //if(e->buttons() & Keypad)
153  // rev->fState |= ShiftMask;
154 
155  rev->fCode = Qt::NoButton; // button code
156  if(e->button() == Qt::LeftButton)
157  rev->fCode |= kButton1Mask;
158  if(e->button() == Qt::RightButton)
159  rev->fCode |= kButton3Mask;
160  if(e->button() == Qt::MiddleButton)
161  rev->fCode |= kButton2Mask;
162 
163  rev->fUser[0] = 0;
164  rev->fWindow = gVirtualX->GetWindowID(fiWinid); // we refer signals to this window
165  rev->fSendEvent = 0;
166  rev->fTime = 0; // this might cause problems with root doubleclick treatment?
167 
168  return kTRUE;
169 }
170 
171 
172 
173 void QRootWindow::paintEvent( QPaintEvent * e)
174 {
175  //TGo4LockGuard threadlock;
176  //(void) threadlock; // suppress compiler warnings
177 
178  if(fxRootwindow) {
179  WId nxid = winId();
180  if(fQtWinId != nxid) {
181  TGo4LockGuard threadlock;
182  (void) threadlock; // suppress compiler warnings
183  // may happen when this window is embedded to Qt workspace...
184  std::cout <<"Warning: QRootWindow::paintEvent finds changed X window id: "<< (ULong_t) winId()<<", previous:"<<fQtWinId << std::endl;
185  delete fxRootwindow; // should also remove old x windows!
186  fQtWinId = nxid;
187  fiWinid = gVirtualX->AddWindow((ULong_t) fQtWinId, width(), height());
188  fxRootwindow = new TQRootFrame(gVirtualX->GetWindowID(fiWinid));
189  }
190  if(fbResizeOnPaint) {
191  TGo4LockGuard threadlock;
192  (void) threadlock; // suppress compiler warnings
193  fxRootwindow->Resize(width(),height());
194  gVirtualX->Update(1); // Xsync/flus
195  }
196  }
197  QWidget::paintEvent(e);
198 }
199 
200 bool QRootWindow ::eventFilter( QObject *o, QEvent *e )
201 {
202  TGo4LockGuard threadlock;
203  (void) threadlock; // suppress compiler warnings
204 
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 = nullptr;
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 = nullptr;
229  }
230  e->accept();
231 }
232 
234 {
235  if (fxRootwindow) {
236  delete fxRootwindow;
237  fxRootwindow = nullptr;
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
double ScaledHeight()
Int_t fiWinid
Definition: QRootWindow.h:80
bool eventFilter(QObject *, QEvent *) override
double ScaledWidth()
virtual ~QRootWindow()
void SetEditable(bool on=true)
int l
Definition: f_mbs_status.c:31
Bool_t fbResizeOnPaint
Definition: QRootWindow.h:87
virtual ~TQRootFrame()
Definition: QRootWindow.cpp:41
void AddSubFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Definition: QRootWindow.cpp:90
double fQtScalingfactor
Definition: QRootWindow.h:89
void closeEvent(QCloseEvent *e) override
TGCompositeFrame * GetRootFrame()
TQRootFrame * fxRootwindow
Definition: QRootWindow.h:77
void paintEvent(QPaintEvent *e) override
QRootWindow(QWidget *parent=nullptr, const char *name=nullptr, bool designermode=false)
Definition: QRootWindow.cpp:64
Bool_t MapQMouseEvent(QMouseEvent *e, Event_t *rev)