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