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