GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QUserPanel.cpp
Go to the documentation of this file.
1 // $Id: QUserPanel.cpp 3341 2021-10-18 13:18:09Z 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 
15 #include "QUserPanel.h"
16 
17 #include <cstdlib>
18 #include <cstdio>
19 #include <iostream>
20 #include <sstream>
21 
22 #ifndef WIN32
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, SIGNAL(clicked()), this, SLOT(InfoBtn_clicked()));
69  QObject::connect(EditBtn, SIGNAL(clicked()), this, SLOT(EditBtn_clicked()));
70  QObject::connect(DrawBtn, SIGNAL(clicked()), this, SLOT(DrawBtn_clicked()));
71 
72  QObject::connect(EditorBtn, SIGNAL(clicked()), fxDrawCanvas, SLOT(activateEditor()));
73  QObject::connect(StatusBtn, SIGNAL(clicked()), fxDrawCanvas, SLOT(activateStatusLine()));
74 
75  QObject::connect(fxDrawCanvas, SIGNAL(CanvasDropEvent(QDropEvent*,TPad*)), this, SLOT(CanvasDropEventSlot(QDropEvent*,TPad*)));
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!=0;
116  break;
117 
118  case 1:
119  PrintLbl->setText(QString("Class: ") + (cl==0 ? "not exists (known)" : cl->GetName()));
120  res = cl!=0;
121  break;
122  case 2:
123  res = cl!=0;
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==0 ? "No class specified" : cl->GetName());
137  DragKindLbl->setText(kindString(kind));
138  break;
139  case 1:
140  if (cl==0) {
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
161  if (strcmp(linkname, "DrawItem")==0) {
162  fxDrawCanvas->getCanvas()->Clear();
163  fxDrawCanvas->getCanvas()->cd();
164  obj->Draw("");
165  fxDrawCanvas->getCanvas()->Update();
166  }
167 }
168 
169 void QUserPanel::linkedObjectRemoved(const char* linkname)
170 {
171  if (strcmp(linkname, "PrintItem")==0)
172  PrintObject(0);
173  else
174  if (strcmp(linkname, "DrawItem")==0) {
175  RemoveLink("DrawItem");
176  printf("Clear canvas and update\n");
177  fxDrawCanvas->getCanvas()->Clear();
178  fxDrawCanvas->getCanvas()->Update();
179  }
180 }
181 
183 {
184  ShowItemInfo(DragItemLbl->text());
185 }
186 
188 {
189  EditItem(DragItemLbl->text());
190 }
191 
193 {
194  DrawItem(DragItemLbl->text());
195 }
196 
197 void QUserPanel::PrintObject(TObject* obj)
198 {
199  PrintEdit->clear();
200  if (obj==0) return;
201 
202 #ifndef WIN32
203  int out_pipe[2];
204  int saved_stdout = dup(STDOUT_FILENO); /* save stdout for display later */
205 
206  if( pipe(out_pipe) != 0 ) return;
207 
208  dup2(out_pipe[1], STDOUT_FILENO); /* redirect stdout to the pipe */
209  ::close(out_pipe[1]);
210 
211  obj->Print("");
212  printf(" ");
213  std::cout.flush();
214  fflush(stdout);
215 
216  char sbuf[10000];
217  memset(sbuf,0, sizeof(sbuf));
218  read(out_pipe[0], sbuf, sizeof(sbuf)-1); /* read from pipe into buffer */
219 
220  dup2(saved_stdout, STDOUT_FILENO); /* reconnect stdout for testing */
221  ::close(out_pipe[0]);
222 
223  PrintEdit->setText(sbuf);
224 #else
225  PrintEdit->setText("<not supported (yet) under windows>");
226 #endif
227 }
228 
229 
230 void QUserPanel::CanvasDropEventSlot(QDropEvent* event, TPad* pad)
231 {
232  if (!event->mimeData()->hasText()) return;
233  QString eventstr = event->mimeData()->text();
234  event->acceptProposedAction();
235  DrawObjectOnCanvas(eventstr.toLatin1().constData());
236 }
237 
238 void QUserPanel::DrawObjectOnCanvas(const char* itemname)
239 {
240  RemoveLink("DrawItem");
241  AddLink(itemname, "DrawItem");
242  TObject* obj = GetLinked("DrawItem", 2);
243  printf("Call draw object %s %p\n", itemname, obj);
244  if (obj!=0) {
245  fxDrawCanvas->getCanvas()->Clear();
246  fxDrawCanvas->getCanvas()->cd();
247  obj->Draw("");
248  fxDrawCanvas->getCanvas()->Update();
249  }
250 }
251 
QUserPanel(QWidget *parent=0, const char *name=0)
Definition: QUserPanel.cpp:60
TObject * GetLinked(const char *linkname, int updatelevel)
Definition: QGo4Widget.cpp:177
virtual bool IsAcceptDrag(const char *itemname, TClass *cl, int kind)
Definition: QUserPanel.cpp:105
virtual void EditBtn_clicked()
Definition: QUserPanel.cpp:183
virtual void DrawObjectOnCanvas(const char *itemname)
Definition: QUserPanel.cpp:234
void EditItem(const QString &itemname)
Definition: QGo4Widget.cpp:352
virtual void linkedObjectRemoved(const char *linkname)
Definition: QUserPanel.cpp:166
Q_DECL_EXPORT void * StartUserPanel(void *parent)
Definition: QUserPanel.cpp:36
void ShowItemInfo(const QString &itemname)
Definition: QGo4Widget.cpp:299
virtual void InfoBtn_clicked()
Definition: QUserPanel.cpp:178
virtual void DropItem(const char *itemname, TClass *cl, int kind)
Definition: QUserPanel.cpp:127
virtual void CanvasDropEventSlot(QDropEvent *, TPad *)
Definition: QUserPanel.cpp:225
void RemoveLink(const char *linkname, bool blockreset=true)
Definition: QGo4Widget.cpp:193
TGo4ViewPanel * DrawItem(const QString &itemname, TGo4ViewPanel *panel=0, TPad *pad=0, bool activate=true, int updatelevel=-1)
Definition: QGo4Widget.cpp:312
virtual void linkedObjectUpdated(const char *linkname, TObject *obj)
Definition: QUserPanel.cpp:153
virtual ~QUserPanel()
Definition: QUserPanel.cpp:78
void AddLink(const char *itemname, const char *linkname)
Definition: QGo4Widget.cpp:122
virtual void PrintObject(TObject *obj)
Definition: QUserPanel.cpp:193
virtual void DrawBtn_clicked()
Definition: QUserPanel.cpp:188
virtual QString kindString(int kind)
Definition: QUserPanel.cpp:83