GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
QUserPanel.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 
15 #include "QUserPanel.h"
16 
17 #include <cstdlib>
18 #include <cstdio>
19 #include <iostream>
20 #include <sstream>
21 
22 #ifndef _MSC_VER
23 #include <unistd.h>
24 #endif
25 
26 #include <QMimeData>
27 #include <QDropEvent>
28 
29 #include "TClass.h"
30 #include "TCanvas.h"
31 #include "TH1F.h"
32 #include "TGo4Proxy.h"
33 
34 // this function called by Go4 GUI to start user panel
35 extern "C" Q_DECL_EXPORT void *StartUserPanel(void *parent)
36 {
37 
38  return new QUserPanel((QWidget *) parent);
39 
40 // Use this code to hide main go4 window and show only user gui
41 
42 // QWidget *w = (QWidget *) parent;
43 // w->parentWidget()->hide();
44 // return new QUserPanel(0);
45 
46 }
47 
48 // *********************************************************
49 
50 /*
51  * Constructs a QUserPanel which is a child of 'parent', with the
52  * name 'name'.'
53  */
54 QUserPanel::QUserPanel( QWidget *parent, const char *name ) :
55  QGo4Widget( parent, name )
56 {
57  setupUi(this);
58 
59  DragLbl->setText("");
60  DragItemLbl->setText("");
61  DragClassLbl->setText("");
62  DragKindLbl->setText("");
63 
64  PrintLbl->setText("");
65 
66  TabWidget->setCurrentIndex(2);
67 
68  QObject::connect(InfoBtn, &QPushButton::clicked, this, &QUserPanel::InfoBtn_clicked);
69  QObject::connect(EditBtn, &QPushButton::clicked, this, &QUserPanel::EditBtn_clicked);
70  QObject::connect(DrawBtn, &QPushButton::clicked, this, &QUserPanel::DrawBtn_clicked);
71 
72  QObject::connect(EditorBtn, &QPushButton::clicked, [this]() { fxDrawCanvas->activateEditor(); });
73  QObject::connect(StatusBtn, &QPushButton::clicked, [this]() { fxDrawCanvas->activateStatusLine(); });
74 
75  QObject::connect(fxDrawCanvas, &QWebCanvas::CanvasDropEvent, this, &QUserPanel::CanvasDropEventSlot);
76 }
77 
79 {
80  fxDrawCanvas->getCanvas()->Clear();
81 }
82 
83 QString QUserPanel::kindString(int kind)
84 {
85  QString kndlabel;
86 
87  switch(kind) {
88  case TGo4Access::kndNone: kndlabel = "unknown"; break;
89  case TGo4Access::kndObject: kndlabel = "object"; break;
90  case TGo4Access::kndFolder: kndlabel = "folder"; break;
91  case TGo4Access::kndTreeBranch: kndlabel = "tree brunch"; break;
92  case TGo4Access::kndTreeLeaf: kndlabel = "tree leaf"; break;
93  case TGo4Access::kndGo4Param: kndlabel = "go4 parameter"; break;
94  case TGo4Access::kndDataMember: kndlabel = "event data member"; break;
95  case TGo4Access::kndEventElement: kndlabel = "event element"; break;
96  default: kndlabel = "undefined"; break;
97  }
98 
99  kndlabel = QString("Kind of draged item: ") + kndlabel +
100  " (" + QString::number(kind) + ")";
101 
102  return kndlabel;
103 }
104 
105 bool QUserPanel::IsAcceptDrag(const char *itemname, TClass *cl, int kind)
106 {
107  bool res = false;
108 
109  switch (TabWidget->currentIndex()) {
110  case 0:
111  DragLbl->setText(QString("Go4 GUI asks if widget accept dragged item "));
112  DragItemLbl->setText(itemname);
113  DragClassLbl->setText(QString("Class: ") + (!cl ? "not exists (known)" : cl->GetName()));
114  DragKindLbl->setText(kindString(kind));
115  res = cl != nullptr;
116  break;
117 
118  case 1:
119  PrintLbl->setText(QString("Class: ") + (!cl ? "not exists (known)" : cl->GetName()));
120  res = cl != nullptr;
121  break;
122  case 2:
123  res = cl != nullptr;
124  break;
125  }
126  // we will accept only items with known classes
127  return res;
128 }
129 
130 void QUserPanel::DropItem(const char *itemname, TClass *cl, int kind)
131 {
132  switch (TabWidget->currentIndex()) {
133  case 0:
134  DragLbl->setText("User dropped item");
135  DragItemLbl->setText(itemname);
136  DragClassLbl->setText(!cl ? "No class specified" : cl->GetName());
137  DragKindLbl->setText(kindString(kind));
138  break;
139  case 1:
140  if (!cl) {
141  PrintLbl->setText("Can not drop item of unknown class");
142  } else {
143  PrintLbl->setText(QString("Print item: ") + itemname);
144  RemoveLink("PrintItem");
145  AddLink(itemname, "PrintItem");
146  PrintObject(GetLinked("PrintItem", 2));
147  }
148 
149  break;
150  case 2:
151  DrawObjectOnCanvas(itemname);
152  break;
153  }
154 }
155 
156 void QUserPanel::linkedObjectUpdated(const char *linkname, TObject *obj)
157 {
158  if (strcmp(linkname, "PrintItem") == 0) {
159  PrintObject(obj);
160  } else if (strcmp(linkname, "DrawItem") == 0) {
161  fxDrawCanvas->getCanvas()->Clear();
162  fxDrawCanvas->getCanvas()->cd();
163  obj->Draw("");
164  fxDrawCanvas->getCanvas()->Update();
165  }
166 }
167 
168 void QUserPanel::linkedObjectRemoved(const char *linkname)
169 {
170  if (strcmp(linkname, "PrintItem") == 0) {
171  PrintObject(nullptr);
172  } else if (strcmp(linkname, "DrawItem") == 0) {
173  RemoveLink("DrawItem");
174  printf("Clear canvas and update\n");
175  fxDrawCanvas->getCanvas()->Clear();
176  fxDrawCanvas->getCanvas()->Update();
177  }
178 }
179 
181 {
182  ShowItemInfo(DragItemLbl->text());
183 }
184 
186 {
187  EditItem(DragItemLbl->text());
188 }
189 
191 {
192  DrawItem(DragItemLbl->text());
193 }
194 
195 void QUserPanel::PrintObject(TObject *obj)
196 {
197  PrintEdit->clear();
198  if (!obj) return;
199 
200 #ifndef _MSC_VER
201  int out_pipe[2];
202  int saved_stdout = dup(STDOUT_FILENO); /* save stdout for display later */
203 
204  if( pipe(out_pipe) != 0 ) return;
205 
206  dup2(out_pipe[1], STDOUT_FILENO); /* redirect stdout to the pipe */
207  ::close(out_pipe[1]);
208 
209  obj->Print("");
210  printf(" ");
211  std::cout.flush();
212  fflush(stdout);
213 
214  char sbuf[10000];
215  memset(sbuf,0, sizeof(sbuf));
216  read(out_pipe[0], sbuf, sizeof(sbuf)-1); /* read from pipe into buffer */
217 
218  dup2(saved_stdout, STDOUT_FILENO); /* reconnect stdout for testing */
219  ::close(out_pipe[0]);
220 
221  PrintEdit->setText(sbuf);
222 #else
223  PrintEdit->setText("<not supported (yet) under windows>");
224 #endif
225 }
226 
227 void QUserPanel::CanvasDropEventSlot(QDropEvent* event, TPad *pad)
228 {
229  if (!event->mimeData()->hasText()) return;
230  QString eventstr = event->mimeData()->text();
231  event->acceptProposedAction();
232  DrawObjectOnCanvas(eventstr.toLatin1().constData());
233 }
234 
235 void QUserPanel::DrawObjectOnCanvas(const char *itemname)
236 {
237  RemoveLink("DrawItem");
238  AddLink(itemname, "DrawItem");
239  TObject *obj = GetLinked("DrawItem", 2);
240  printf("Call draw object %s %p\n", itemname, obj);
241  if (obj) {
242  fxDrawCanvas->getCanvas()->Clear();
243  fxDrawCanvas->getCanvas()->cd();
244  obj->Draw("");
245  fxDrawCanvas->getCanvas()->Update();
246  }
247 }
248 
bool IsAcceptDrag(const char *itemname, TClass *cl, int kind) override
Definition: QUserPanel.cpp:101
void linkedObjectUpdated(const char *linkname, TObject *obj) override
Definition: QUserPanel.cpp:149
virtual void EditBtn_clicked()
Definition: QUserPanel.cpp:177
virtual void DrawObjectOnCanvas(const char *itemname)
Definition: QUserPanel.cpp:227
TGo4ViewPanel * DrawItem(const QString &itemname, TGo4ViewPanel *panel=nullptr, TPad *pad=nullptr, bool activate=true, int updatelevel=-1)
Definition: QGo4Widget.cpp:305
void EditItem(const QString &itemname)
Definition: QGo4Widget.cpp:345
TObject * GetLinked(const char *linkname, int updatelevel=0)
Definition: QGo4Widget.cpp:172
Q_DECL_EXPORT void * StartUserPanel(void *parent)
Definition: QUserPanel.cpp:36
void ShowItemInfo(const QString &itemname)
Definition: QGo4Widget.cpp:292
virtual void InfoBtn_clicked()
Definition: QUserPanel.cpp:172
virtual void CanvasDropEventSlot(QDropEvent *, TPad *)
Definition: QUserPanel.cpp:219
void RemoveLink(const char *linkname, bool blockreset=true)
Definition: QGo4Widget.cpp:188
virtual ~QUserPanel()
Definition: QUserPanel.cpp:74
QUserPanel(QWidget *parent=nullptr, const char *name=nullptr)
Definition: QUserPanel.cpp:55
void linkedObjectRemoved(const char *linkname) override
Definition: QUserPanel.cpp:161
void AddLink(const char *itemname, const char *linkname)
Definition: QGo4Widget.cpp:117
virtual void PrintObject(TObject *obj)
Definition: QUserPanel.cpp:187
virtual void DrawBtn_clicked()
Definition: QUserPanel.cpp:182
void DropItem(const char *itemname, TClass *cl, int kind) override
Definition: QUserPanel.cpp:123
void CanvasDropEvent(QDropEvent *, TPad *)
virtual QString kindString(int kind)
Definition: QUserPanel.cpp:79