GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
QGo4Widget.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 "QGo4Widget.h"
15 
16 #include <QtCore/QTimer>
17 #include <QAction>
18 #include <QMenu>
19 #include <QtCore/QSignalMapper>
20 
21 #include <QDragMoveEvent>
22 #include <QCloseEvent>
23 #include <QDragEnterEvent>
24 
25 QGo4Widget::QGo4Widget(QWidget *parent, const char *name, Qt::WindowFlags f) :
26  QWidget(parent, f),
27  fWaitsForObjectCreation(false),
28  fCanDestroyWidget(true),
29  fResetWidgetShooted(false),
30  fBlockUpdate(false),
31  fBrowserProxy(nullptr)
32 {
33  setObjectName(name);
34  setAcceptDrops(true);
35 }
36 
38 {
39  RemoveLinksMasked("complete");
40 }
41 
42 void QGo4Widget::ObjectCreatedByWidget(const char *itemname, TClass *cl)
43 {
44  // this method calls by maingui when object created by one of editor
45 
48  requestedObjectCreated(itemname, cl);
49  }
50 }
51 
53 {
54  // we set fResetWidgetShooted to prevent in any situation of ShootResetWidget() again
55  fResetWidgetShooted = true;
58  fResetWidgetShooted = false;
59 }
60 
62 {
63  if (fResetWidgetShooted) return;
64 
65  fResetWidgetShooted = true;
66 
67  QTimer::singleShot(0, this, &QGo4Widget::ResetWidget);
68 }
69 
70 void QGo4Widget::ShootCloseWidget(bool closeparent)
71 {
72  if (fResetWidgetShooted) return;
73 
74  fResetWidgetShooted = true;
75 
76  QTimer::singleShot(0, closeparent ? parentWidget() : this, &QWidget::close);
77 }
78 
80 {
81  ShootCloseWidget(true);
82 }
83 
84 bool QGo4Widget::IsAcceptDrag(const char *, TClass *, int)
85 {
86  return false;
87 }
88 
89 void QGo4Widget::DropItem(const char *, TClass *, int)
90 {
91 }
92 
93 void QGo4Widget::closeEvent(QCloseEvent *e)
94 {
95  if (fCanDestroyWidget) {
96  e->accept();
97  } else {
98  e->ignore();
99  }
100 }
101 
102 void QGo4Widget::dragEnterEvent(QDragEnterEvent *e)
103 {
104  emit widgetService(this, service_DragEnter, nullptr, dynamic_cast<QDragMoveEvent *> (e));
105 }
106 
107 void QGo4Widget::dragMoveEvent(QDragMoveEvent* e)
108 {
109  emit widgetService(this, service_DragEnter, "", e);
110 }
111 
112 void QGo4Widget::dropEvent(QDropEvent* e)
113 {
114  emit widgetService(this, service_DropEvent, nullptr, e);
115 }
116 
117 void QGo4Widget::AddLink(const char *itemname, const char *linkname)
118 {
119  emit widgetService(this, service_AddEditorLink, linkname, (void *) itemname);
120 }
121 
122 void QGo4Widget::AddLink(TGo4Slot *slot, const char *linkname)
123 {
124  emit widgetService(this, service_AddDirectLink, linkname, (void *) slot);
125 }
126 
127 TGo4Slot *QGo4Widget::AddLink(const char *itemname, TGo4Slot *parent)
128 {
129  TGo4Slot *res = parent;
130  emit widgetService(this, service_AddLinkInSlot, itemname, (void *) &res);
131  return res;
132 }
133 
135 {
136  TGo4Slot *res = nullptr;
137  emit widgetService(this, service_GetTopSlot, force ? "force" : "normal", (void *) &res);
138  return res;
139 }
140 
141 TGo4Slot *QGo4Widget::AddSlot(const char *slotname)
142 {
143  TGo4Slot *res = nullptr;
144  emit widgetService(this, service_AddEditorSlot, slotname, (void *) &res);
145  return res;
146 }
147 
148 void QGo4Widget::SetLinkedName(TGo4Slot *slot, const char *itemname)
149 {
150  emit widgetService(this, service_SetLinkedName, itemname, (void *) slot);
151 }
152 
153 const char *QGo4Widget::GetLinkedName(const char *linkname)
154 {
155  const char *res = nullptr;
156 
157  emit widgetService(this, service_GetLinkedName, linkname, (void *) &res);
158 
159  return res;
160 }
161 
163 {
164  void *res = slot;
165 
166  emit widgetService(this, service_GetLinkedName2, "", (void *) &res);
167 
168  return (const char *) res;
169 }
170 
171 
172 TObject *QGo4Widget::GetLinked(const char *linkname, int updatelevel)
173 {
174  TObject *res = nullptr;
175  int func = 0;
176  switch (updatelevel) {
177  case 0: func = service_GetLinked0; break;
178  case 1: func = service_GetLinked1; break;
179  case 2: func = service_GetLinked2; break;
180  default: func = service_GetLinked1; break;
181  }
182 
183  emit widgetService(this, func, linkname, (void *) &res);
184 
185  return res;
186 }
187 
188 void QGo4Widget::RemoveLink(const char *linkname, bool blockreset)
189 {
190  bool oldvalue = fBlockUpdate;
191  if (blockreset) fBlockUpdate = true;
192  emit widgetService(this, service_RemoveLink, linkname, nullptr);
193  fBlockUpdate = oldvalue;
194 }
195 
196 void QGo4Widget::RemoveAllLinks(bool blockreset)
197 {
198  bool oldvalue = fBlockUpdate;
199  if (blockreset) fBlockUpdate = true;
200  emit widgetService(this, service_RemoveAllLinks, nullptr, nullptr);
201  fBlockUpdate = oldvalue;
202 }
203 
204 void QGo4Widget::RemoveLinksMasked(const char *startedwith, bool blockreset)
205 {
206  bool oldvalue = fBlockUpdate;
207  if (blockreset) fBlockUpdate = true;
208  emit widgetService(this, service_RemoveAllLinks, startedwith, nullptr);
209  fBlockUpdate = oldvalue;
210 }
211 
212 bool QGo4Widget::BrowserItemRemote(const char *itemname)
213 {
214  bool isremote = false;
215  emit widgetService(this, service_BrowserItemRemote, itemname, (void *) &isremote);
216  return isremote;
217 }
218 
220 {
221  if (fBrowserProxy) return fBrowserProxy;
222 
223  TGo4BrowserProxy *br = nullptr;
224  emit widgetService(this, service_Browser, "", (void *) &br);
225  fBrowserProxy = br;
226  return br;
227 }
228 
229 void QGo4Widget::ConnectPad(TPad *pad)
230 {
231  emit widgetService(this, service_ConnectPad, "", (void *) pad);
232 }
233 
234 void QGo4Widget::CallPanelFunc(int func, TPad *pad)
235 {
236  emit widgetService(this, func, "", (void *) pad);
237 }
238 
239 void QGo4Widget::StatusMessage(const QString& message)
240 {
241  emit widgetService(this, service_StatusMessage, message.toLatin1().constData(), nullptr);
242 }
243 
244 void QGo4Widget::ProcessSignal(const char *linkname, bool assigned, TObject *obj, TGo4Slot *slot)
245 {
246  if (assigned) {
247  linkedUpdated(slot, obj);
248  linkedObjectUpdated(linkname, obj);
249  } else
250  // inform widget only if deletion caused not by widget itself by RemoveAllLinks() call
251  if (!IsUpdateBlocked()) {
252  linkedRemoved(slot, obj);
253  linkedObjectRemoved(linkname);
254  }
255 }
256 
257 void QGo4Widget::linkedObjectUpdated(const char */* linkname */, TObject */* obj */)
258 {
259 }
260 
261 void QGo4Widget::linkedObjectRemoved(const char */* linkname */)
262 {
263  // reset editor if any watched object is removed
265 }
266 
267 void QGo4Widget::linkedUpdated(TGo4Slot */* slot */, TObject */* obj */ )
268 {
269 }
270 
271 void QGo4Widget::linkedRemoved(TGo4Slot */* slot */, TObject */* obj */)
272 {
273 }
274 
275 void QGo4Widget::AskToCreateObject(TClass *cl, int isremote)
276 {
277  fWaitsForObjectCreation = (isremote >= 0);
278  QString str = QString::number(isremote);
279  emit widgetService(this, service_CreateItem, str.toLatin1().constData(), (void *) cl);
280 }
281 
282 void QGo4Widget::InformThatObjectCreated(const char *itemname, TClass *cl)
283 {
284  emit widgetService(this, service_ObjectCreated, itemname, cl);
285 }
286 
287 // this function should be reimplemented in editor that asks to create object
288 void QGo4Widget::requestedObjectCreated(const char * /* itemname */, TClass * /* cl */)
289 {
290 }
291 
292 void QGo4Widget::ShowItemInfo(const QString& itemname)
293 {
294  emit widgetService(this, service_ShowInfo, itemname.toLatin1().constData(), nullptr);
295 }
296 
298 {
299  QString str = QString::number(ndiv);
300  TGo4ViewPanel *res = nullptr;
301  emit widgetService(this, service_CreateViewPanel, str.toLatin1().constData(), (void *)&res);
302  return res;
303 }
304 
305 TGo4ViewPanel *QGo4Widget::DrawItem(const QString& itemname, TGo4ViewPanel *panel, TPad *pad, bool activate, int updatelevel)
306 {
307  void *res[4];
308  res[0] = panel;
309  res[1] = pad;
310  res[2] = &activate;
311  res[3] = &updatelevel;
312  emit widgetService(this, service_DrawItem, itemname.toLatin1().constData(), res);
313  return (TGo4ViewPanel *) res[0];
314 }
315 
317 {
318  TGo4ViewPanel *res = nullptr;
319  emit widgetService(this, service_WhereItemDrawn, itemname, &res);
320  return res;
321 }
322 
323 void QGo4Widget::UndrawItem(const char *itemname)
324 {
325  emit widgetService(this, service_UndrawItem, itemname, nullptr);
326 }
327 
328 void QGo4Widget::HelpWindow(const char *filename, const char *msg)
329 {
330  emit widgetService(this, service_HelpWindow, filename, (void *)msg);
331 }
332 
333 void QGo4Widget::StartHotstart(const char *filename)
334 {
335  emit widgetService(this, service_HotStart, filename, nullptr);
336 }
337 
339 {
340  TGo4ViewPanel *res = nullptr;
341  emit widgetService(this, service_LastActivePanel, "", (void *)&res);
342  return res;
343 }
344 
345 void QGo4Widget::EditItem(const QString& itemname)
346 {
347  emit widgetService(this, service_EditItem, itemname.toLatin1().constData(), nullptr);
348 }
349 
351 {
352  emit widgetService(this, service_EditInSlot, "", (void *) slot);
353 }
354 
355 QString QGo4Widget::SaveObjectInMemory(const char *foldername, TObject *obj)
356 {
357  void *par = obj;
358  emit widgetService(this, service_SaveToMemory, foldername, (void *) &par);
359  QString itemname = "";
360  if ((par != obj) && par) {
361  QString* res = (QString*) par;
362  itemname = *res;
363  delete res;
364  }
365  return itemname;
366 }
367 
368 bool QGo4Widget::SaveItemToFile(const char *itemname, const char *subfolder)
369 {
370  char buf[1000];
371  if (!subfolder)
372  strcpy(buf, "");
373  else
374  strncpy(buf, subfolder, sizeof(buf));
375  emit widgetService(this, service_SaveItem, itemname, (void *) buf);
376  return buf[0] != 0;
377 }
378 
379 bool QGo4Widget::UpdateItemInAnalysis(const char *itemname, TObject *obj)
380 {
381  TObject *res = obj;
382  emit widgetService(this, service_UpdateAnalysisItem, itemname, (void *) &res);
383  return res != nullptr;
384 }
385 
387 {
388  TGo4ServerProxy *res = nullptr;
389  emit widgetService(this, service_GetAnalysis, itemname, (void *) &res);
390  return res;
391 }
392 
393 void QGo4Widget::CallServiceFunc(int func, const char *str, void *par)
394 {
395  emit widgetService(this, func, str, par);
396 }
397 
398 void QGo4Widget::ServiceCall(const char *name, void *par)
399 {
400  CallServiceFunc(service_General, name, par);
401 }
402 
403 QAction* QGo4Widget::CreateChkAction(QMenu* menu, const QString& text, bool checked, bool enabled)
404 {
405  QAction* act = new QAction(text, menu);
406  act->setCheckable(true);
407  act->setChecked(checked);
408  act->setEnabled(enabled);
409  menu->addAction(act);
410  return act;
411 }
412 
413 QAction* QGo4Widget::AddIdAction(QMenu* menu, QSignalMapper* map,
414  const QString& text, int id, int enabled, int checked)
415 {
416  auto oldact = map->mapping(id);
417  if (oldact) map->removeMappings(oldact);
418 
419  QAction* act = new QAction(text, menu);
420  if (checked!=-1) {
421  act->setCheckable(true);
422  act->setChecked(checked > 0);
423  }
424  if (enabled!=-1)
425  act->setEnabled(enabled > 0);
426 
427  QObject::connect(act, &QAction::triggered, [id, map]() {
428 #if QT_VERSION < QT_VERSION_CHECK(5,15,0)
429  map->mapped(id);
430 #else
431  map->mappedInt(id);
432 #endif
433  });
434 
435  menu->addAction(act);
436  map->setMapping(act, id);
437  return act;
438 }
439 
440 QAction* QGo4Widget::SetIdAction(QSignalMapper* map, int id, int enabled, int checked)
441 {
442  QAction* act = (QAction*) map->mapping(id);
443  if (!act) return nullptr;
444  if (checked!=-1) {
445  act->setCheckable(true);
446  act->setChecked(checked > 0);
447  }
448  if (enabled!=-1)
449  act->setEnabled(enabled > 0);
450  return act;
451 }
452 
453 QAction* QGo4Widget::AddIdAction(QMenu* menu, QSignalMapper* map,
454  const QIcon& icon, const QString& text, int id, int enabled, int checked)
455 {
456  auto oldact = map->mapping(id);
457  if (oldact) map->removeMappings(oldact);
458 
459  QAction* act = new QAction(icon, text, menu);
460  if (checked != -1) {
461  act->setCheckable(true);
462  act->setChecked(checked > 0);
463  }
464  if (enabled!=-1)
465  act->setEnabled(enabled > 0);
466 
467  QObject::connect(act, &QAction::triggered, [id, map]() {
468 #if QT_VERSION < QT_VERSION_CHECK(5,15,0)
469  map->mapped(id);
470 #else
471  map->mappedInt(id);
472 #endif
473  });
474 
475  menu->addAction(act);
476  map->setMapping(act, id);
477  return act;
478 }
void StartHotstart(const char *filename)
Definition: QGo4Widget.cpp:333
virtual void ResetWidget()
Definition: QGo4Widget.cpp:52
virtual void linkedObjectRemoved(const char *linkname)
Definition: QGo4Widget.cpp:261
void EditObjectInSlot(TGo4Slot *slot)
Definition: QGo4Widget.cpp:350
void dragEnterEvent(QDragEnterEvent *e) override
Definition: QGo4Widget.cpp:102
void ShootCloseWidget(bool closeparent=false)
Definition: QGo4Widget.cpp:70
bool SaveItemToFile(const char *itemname, const char *subfolder=nullptr)
Definition: QGo4Widget.cpp:368
bool UpdateItemInAnalysis(const char *itemname, TObject *obj=nullptr)
Definition: QGo4Widget.cpp:379
virtual void linkedObjectUpdated(const char *linkname, TObject *obj)
Definition: QGo4Widget.cpp:257
bool fBlockUpdate
indicates that reset widget timer is shoot
Definition: QGo4Widget.h:48
virtual void linkedUpdated(TGo4Slot *slot, TObject *obj)
Definition: QGo4Widget.cpp:267
void ConnectPad(TPad *pad)
Definition: QGo4Widget.cpp:229
void CallPanelFunc(int id, TPad *pad=nullptr)
Definition: QGo4Widget.cpp:234
void SetLinkedName(TGo4Slot *slot, const char *itemname)
Definition: QGo4Widget.cpp:148
void ServiceCall(const char *name, void *par=nullptr)
Definition: QGo4Widget.cpp:398
void UndrawItem(const char *itemname)
Definition: QGo4Widget.cpp:323
void widgetService(QGo4Widget *editor, int serviceid, const char *str, void *par)
TGo4Slot * GetTopSlot(bool force=false)
Definition: QGo4Widget.cpp:134
const char * GetLinkedName(const char *linkname)
Definition: QGo4Widget.cpp:153
TGo4ViewPanel * DrawItem(const QString &itemname, TGo4ViewPanel *panel=nullptr, TPad *pad=nullptr, bool activate=true, int updatelevel=-1)
Definition: QGo4Widget.cpp:305
TGo4BrowserProxy * Browser()
Definition: QGo4Widget.cpp:219
void EditItem(const QString &itemname)
Definition: QGo4Widget.cpp:345
TObject * GetLinked(const char *linkname, int updatelevel=0)
Definition: QGo4Widget.cpp:172
void ProcessSignal(const char *linkname, bool assigned, TObject *obj, TGo4Slot *slot)
Definition: QGo4Widget.cpp:244
TGo4ViewPanel * CreateViewPanel(int ndiv=0)
Definition: QGo4Widget.cpp:297
void HelpWindow(const char *filename, const char *msg=nullptr)
Definition: QGo4Widget.cpp:328
virtual bool IsAcceptDrag(const char *itemname, TClass *cl, int kind)
Definition: QGo4Widget.cpp:84
void RemoveAllLinks(bool blockreset=true)
Definition: QGo4Widget.cpp:196
static QAction * SetIdAction(QSignalMapper *map, int id, int enabled=-1, int checked=-1)
Definition: QGo4Widget.cpp:440
bool fResetWidgetShooted
indicate that widget can be destroyed
Definition: QGo4Widget.h:47
bool BrowserItemRemote(const char *itemname)
Definition: QGo4Widget.cpp:212
void ShowItemInfo(const QString &itemname)
Definition: QGo4Widget.cpp:292
void StatusMessage(const QString &message)
Definition: QGo4Widget.cpp:239
bool fCanDestroyWidget
Definition: QGo4Widget.h:46
void ObjectCreatedByWidget(const char *itemname, TClass *cl)
Definition: QGo4Widget.cpp:42
virtual ~QGo4Widget()
Definition: QGo4Widget.cpp:37
void RemoveLink(const char *linkname, bool blockreset=true)
Definition: QGo4Widget.cpp:188
void InformThatObjectCreated(const char *itemname, TClass *cl)
Definition: QGo4Widget.cpp:282
bool fWaitsForObjectCreation
Definition: QGo4Widget.h:45
static QAction * CreateChkAction(QMenu *menu, const QString &text, bool checked, bool enabled=true)
Definition: QGo4Widget.cpp:403
virtual void requestedObjectCreated(const char *itemname, TClass *cl)
Definition: QGo4Widget.cpp:288
bool IsUpdateBlocked() const
Definition: QGo4Widget.h:208
QGo4Widget(QWidget *parent=nullptr, const char *name=nullptr, Qt::WindowFlags f=Qt::Widget)
Definition: QGo4Widget.cpp:25
TGo4ServerProxy * GetAnalysis(const char *itemname=nullptr)
Definition: QGo4Widget.cpp:386
TGo4ViewPanel * LastActivePanel()
Definition: QGo4Widget.cpp:338
TGo4BrowserProxy * fBrowserProxy
set when automatic reset must be blocked
Definition: QGo4Widget.h:50
static QAction * AddIdAction(QMenu *menu, QSignalMapper *map, const QString &text, int id, int enabled=-1, int checked=-1)
Definition: QGo4Widget.cpp:413
void AskToCreateObject(TClass *cl, int isremote)
Definition: QGo4Widget.cpp:275
TGo4ViewPanel * WhereItemDrawn(const char *itemname)
Definition: QGo4Widget.cpp:316
void closeEvent(QCloseEvent *e) override
Definition: QGo4Widget.cpp:93
void AddLink(const char *itemname, const char *linkname)
Definition: QGo4Widget.cpp:117
QString SaveObjectInMemory(const char *foldername, TObject *obj)
Definition: QGo4Widget.cpp:355
void dragMoveEvent(QDragMoveEvent *e) override
Definition: QGo4Widget.cpp:107
virtual void DropItem(const char *itemname, TClass *cl, int kind)
Definition: QGo4Widget.cpp:89
void dropEvent(QDropEvent *e) override
Definition: QGo4Widget.cpp:112
void RemoveLinksMasked(const char *startedwith=nullptr, bool blockreset=true)
Definition: QGo4Widget.cpp:204
virtual void linkedRemoved(TGo4Slot *slot, TObject *obj)
Definition: QGo4Widget.cpp:271
TGo4Slot * AddSlot(const char *slotname)
Definition: QGo4Widget.cpp:141
void ShootResetWidget()
Definition: QGo4Widget.cpp:61
void CloseMDIParentSlot()
Definition: QGo4Widget.cpp:79
string msg
Definition: go4init.py:11
void CallServiceFunc(int id, const char *str=nullptr, void *par=nullptr)
Definition: QGo4Widget.cpp:393