GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QUserPanel.cpp
Go to the documentation of this file.
1 // $Id: QUserPanel.cpp 2858 2020-05-18 14:01:49Z 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 "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 
39  return new QUserPanel((QWidget*) parent);
40 
41 // Use this code to hide main go4 window and show only user gui
42 
43 // QWidget* w = (QWidget*) parent;
44 // w->parentWidget()->hide();
45 // return new QUserPanel(0);
46 
47 }
48 
49 
50 
51 
52 
53 // *********************************************************
54 
55 
56 /*
57  * Constructs a QUserPanel which is a child of 'parent', with the
58  * name 'name'.'
59  */
60 QUserPanel::QUserPanel( QWidget* parent, const char* name ) :
61  QGo4Widget( parent, name )
62 {
63  setupUi(this);
64 
65  DragLbl->setText("");
66  DragItemLbl->setText("");
67  DragClassLbl->setText("");
68  DragKindLbl->setText("");
69 
70  PrintLbl->setText("");
71 
72  QObject::connect(InfoBtn, SIGNAL(clicked()), this, SLOT(InfoBtn_clicked()));
73  QObject::connect(EditBtn, SIGNAL(clicked()), this, SLOT(EditBtn_clicked()));
74  QObject::connect(DrawBtn, SIGNAL(clicked()), this, SLOT(DrawBtn_clicked()));
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("Go4 GUI asks if widget accept dragged item");
112  DragItemLbl->setText(itemname);
113  DragClassLbl->setText(QString("Class: ") + (cl==0 ? "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  }
123  // we will accept only items with known classes
124  return res;
125 }
126 
127 void QUserPanel::DropItem(const char* itemname, TClass* cl, int kind)
128 {
129  switch (TabWidget->currentIndex()) {
130  case 0:
131  DragLbl->setText("User dropped item");
132  DragItemLbl->setText(itemname);
133  DragClassLbl->setText(cl==0 ? "No class specified" : cl->GetName());
134  DragKindLbl->setText(kindString(kind));
135  break;
136  case 1:
137  if (cl==0) {
138  PrintLbl->setText("Can not drop item of unknown class");
139  } else {
140  PrintLbl->setText(QString("Print item: ") + itemname);
141  RemoveLink("PrintItem");
142  AddLink(itemname, "PrintItem");
143  PrintObject(GetLinked("PrintItem", 2));
144  }
145 
146  break;
147  case 2:
148  DrawObjectOnCanvas(itemname);
149  break;
150  }
151 }
152 
153 void QUserPanel::linkedObjectUpdated(const char* linkname, TObject* obj)
154 {
155  if (strcmp(linkname, "PrintItem")==0)
156  PrintObject(obj);
157  else
158  if (strcmp(linkname, "DrawItem")==0) {
159  fxDrawCanvas->getCanvas()->Clear();
160  fxDrawCanvas->getCanvas()->cd();
161  obj->Draw("");
162  fxDrawCanvas->getCanvas()->Update();
163  }
164 }
165 
166 void QUserPanel::linkedObjectRemoved(const char* linkname)
167 {
168  if (strcmp(linkname, "PrintItem")==0)
169  PrintObject(0);
170  else
171  if (strcmp(linkname, "DrawItem")==0) {
172  RemoveLink("DrawItem");
173  fxDrawCanvas->getCanvas()->Clear();
174  fxDrawCanvas->getCanvas()->Update();
175  }
176 }
177 
179 {
180  ShowItemInfo(DragItemLbl->text());
181 }
182 
184 {
185  EditItem(DragItemLbl->text());
186 }
187 
189 {
190  DrawItem(DragItemLbl->text());
191 }
192 
193 void QUserPanel::PrintObject(TObject* obj)
194 {
195  PrintEdit->clear();
196  if (obj==0) return;
197 
198 #ifndef WIN32
199  int out_pipe[2];
200  int saved_stdout = dup(STDOUT_FILENO); /* save stdout for display later */
201 
202  if( pipe(out_pipe) != 0 ) return;
203 
204  dup2(out_pipe[1], STDOUT_FILENO); /* redirect stdout to the pipe */
205  ::close(out_pipe[1]);
206 
207  obj->Print("");
208  printf(" ");
209  std::cout.flush();
210  fflush(stdout);
211 
212  char sbuf[10000];
213  memset(sbuf,0, sizeof(sbuf));
214  read(out_pipe[0], sbuf, sizeof(sbuf)-1); /* read from pipe into buffer */
215 
216  dup2(saved_stdout, STDOUT_FILENO); /* reconnect stdout for testing */
217  ::close(out_pipe[0]);
218 
219  PrintEdit->setText(sbuf);
220 #else
221  PrintEdit->setText("<not supported (yet) under windows>");
222 #endif
223 }
224 
225 void QUserPanel::CanvasDropEventSlot(QDropEvent* event, TPad* pad)
226 {
227 
228  if (!event->mimeData()->hasText()) return;
229  QString eventstr = event->mimeData()->text();
230  event->acceptProposedAction();
231  DrawObjectOnCanvas(eventstr.toLatin1().constData());
232 }
233 
234 void QUserPanel::DrawObjectOnCanvas(const char* itemname)
235 {
236  RemoveLink("DrawItem");
237  AddLink(itemname, "DrawItem");
238  TObject* obj = GetLinked("DrawItem", 2);
239  if (obj!=0) {
240  fxDrawCanvas->getCanvas()->Clear();
241  fxDrawCanvas->getCanvas()->cd();
242  obj->Draw("");
243  fxDrawCanvas->getCanvas()->Update();
244  }
245 }
246 
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