GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ParaEdit.cpp
Go to the documentation of this file.
1 // $Id: TGo4ParaEdit.cpp 1506 2015-06-10 08:27:32Z 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 #include "TGo4ParaEdit.h"
15 
16 #include <QMenu>
17 #include <QLabel>
18 #include <QMessageBox>
19 #include <QFileDialog>
20 #include <QToolTip>
21 #include <QHeaderView>
22 
23 #include "TROOT.h"
24 #include "TFile.h"
25 #include "TObjArray.h"
26 #include "TList.h"
27 #include "TClass.h"
28 #include "TDataMember.h"
29 #include "TDataType.h"
30 #include "TBaseClass.h"
31 #include "TGo4LockGuard.h"
32 #include "TGo4Parameter.h"
33 #include "TGo4ParameterMember.h"
34 #include "TGo4ParameterStatus.h"
35 #include "TGo4Fitter.h"
36 #include "TGo4Slot.h"
37 #include "TGo4BrowserProxy.h"
38 #include "TGo4ServerProxy.h"
39 
40 TGo4ParaEdit::TGo4ParaEdit(QWidget *parent, const char* name) :
41  QGo4Widget(parent,name)
42 {
43  setupUi(this);
44 
45  fItems = 0;
46  PleaseUpdateLabel->setVisible(false);
47  parentWidget()->adjustSize();
48 
49  ParamNameLbl->setText("");
50  ParamClassLbl->setText("");
51 
52  fItemName = "";
53  fFillingTable = false;
54  MemberTable->setContextMenuPolicy(Qt::CustomContextMenu);
55  MemberTable->horizontalHeader()->setStretchLastSection(true);
56 }
57 
59 {
60  if (fItems!=0) {
61  delete fItems;
62  fItems = 0;
63  }
64 }
65 
66 bool TGo4ParaEdit::IsAcceptDrag(const char* itemname, TClass* cl, int kind)
67 {
68  if (kind==TGo4Access::kndGo4Param) return true;
69 
70  return (cl==0) ? false : cl->InheritsFrom(TGo4Parameter::Class());
71 }
72 
73 void TGo4ParaEdit::DropItem(const char* itemname, TClass* cl, int kind)
74 {
75  if ((kind==TGo4Access::kndGo4Param) ||
76  ((cl!=0) && cl->InheritsFrom(TGo4Parameter::Class())))
77  WorkWithParameter(itemname, false);
78 }
79 
80 void TGo4ParaEdit::linkedObjectUpdated(const char* linkname, TObject* obj)
81 {
82  if (strcmp(linkname,"Parameter")==0)
83  RefreshWidget(dynamic_cast<TGo4Parameter*> (obj));
84  else
85  if (strcmp(linkname,"ParStatus")==0)
86  RefreshWidget(dynamic_cast<TGo4ParameterStatus*> (obj));
87 }
88 
89 void TGo4ParaEdit::linkedObjectRemoved( const char * linkname )
90 {
91  // if link removed while parameter is disappear, just close parameter editor
92 
93  if (strcmp(linkname,"Fitter")!=0)
94  ShootCloseWidget(true);
95 }
96 
97 void TGo4ParaEdit::WorkWithParameter(const char* itemname, bool isrefresh)
98 {
99  if (PleaseUpdateLabel->isVisible() && !isrefresh) {
100  TGo4Parameter* par = dynamic_cast<TGo4Parameter*> (GetLinked("Parameter",0));
101  const char* previtem = GetLinkedName("Parameter");
102  if ((par!=0) && (previtem!=0)) {
103  int res = QMessageBox::warning(this, "Parameter editor",
104  QString("Current parameter %1 is modified!\n"
105  "New parameter %2 is selected.").arg(previtem).arg(itemname),
106  QString("Continue with current"),
107  QString("Start with new"), QString::null, 0);
108 // (BrowserItemRemote(previtem) ? QString("Update current in analysis and start with new") : QString::null), 0);
109  if (res==0) return;
110 // if (res==2) UpdateItemInAnalysis(previtem);
111  }
112  }
113 
114  ResetWidget();
115 
116  ParamNameLbl->setText(itemname);
117 
118  fItemName = itemname;
119 
120  QString iconname = ":/icons/right.png";
121  QString tooltip = "Refresh parameter values from analysis";
122 
123  if (BrowserItemRemote(itemname)) {
124  TGo4Slot* tgtslot = AddSlot("ParStatus");
125  TGo4BrowserProxy* br = Browser();
126  if (br!=0) br->RequestObjectStatus(itemname, tgtslot);
127  // add dummy link to be informed when parameter is disappear
128  AddLink(itemname, "ParameterLock");
129  } else {
130  iconname = ":/icons/refresh.png";
131  tooltip = "Refresh parameter values from source";
132  AddLink(itemname, "Parameter");
133  TGo4Parameter* par = dynamic_cast<TGo4Parameter*> (GetLinked("Parameter",2));
134  if (par!=0) RefreshWidget(par);
135  }
136 
137  RefreshButton->setIcon(QIcon(iconname));
138  RefreshButton->setToolTip(tooltip);
139 
140  setFocus();
141 }
142 
144 {
146 
147  ParamNameLbl->setText("");
148  ParamClassLbl->setText("");
149 
150  fItemName = "";
151 
152  RefreshButton->setEnabled(false);
153  ApplyButton->setEnabled(false);
154 
155  delete fItems;
156  fItems = 0;
157 
159 
160  PleaseUpdateLabel->setVisible(false);
161 }
162 
164 {
165  RefreshButton->setEnabled(par!=0);
166  ApplyButton->setEnabled(par!=0);
167 
168  delete fItems;
169  fItems = new TObjArray(100);
170  fItems->SetOwner(kTRUE);
171 
172  if (par!=0) {
173  par->GetMemberValues(fItems);
174  ParamClassLbl->setText(QString(" - ")+par->ClassName());
175  }
176 
178 
179  PleaseUpdateLabel->setVisible(false);
180 }
181 
182 
184 {
185  RefreshButton->setEnabled(status!=0);
186 
187  TGo4BrowserProxy* br = Browser();
188  TGo4ServerProxy* serv = br ? br->DefineServerObject(fItemName.toLatin1().constData()) : 0;
189  ApplyButton->setEnabled(serv && serv->CanSubmitObjects());
190 
191  delete fItems;
192  fItems = 0;
193 
194  if (status!=0) {
195  fItems = status->GetMemberValues(kTRUE);
196  ParamClassLbl->setText(QString(" - ") + status->GetObjectClass());
197  }
198 
200 
201  PleaseUpdateLabel->setVisible(false);
202 }
203 
205 {
206  if (fItems==0) {
207  MemberTable->setRowCount(0);
208  return;
209  }
210 
211  fFillingTable = true;
212 
213  MemberTable->hide();
214 
215  MemberTable->horizontalHeaderItem(fiColType)->setText("Type");
216  MemberTable->horizontalHeaderItem(fiColName)->setText("Name");
217  MemberTable->horizontalHeaderItem(fiColValue)->setText("Value");
218  MemberTable->horizontalHeaderItem(fiColComment)->setText("Comments");
219 
220  int memnum = fItems->GetLast()+1;
221  MemberTable->setRowCount(memnum);
222  // MemberTable->setLeftMargin(0);
223  MemberTable->verticalHeader()->hide();
224  TString buf;
225  int ih=QApplication::fontMetrics().height();
226  // QApplication::font().pointSize()
227  for(int row=0; row<memnum; row++) {
228  MemberTable->setRowHeight(row, ih);
229  TGo4ParameterMember* info = (TGo4ParameterMember*) fItems->At(row);
230 
231  QTableWidgetItem* item = new QTableWidgetItem(info->GetTypeName());
232  item->setFlags(item->flags() & ~Qt::ItemIsEditable);
233  MemberTable->setItem(row, fiColType, item);
234 
235  item = new QTableWidgetItem(info->GetFullName(buf));
236  item->setFlags(item->flags() & ~Qt::ItemIsEditable);
237  MemberTable->setItem(row, fiColName, item);
238 
239  item = new QTableWidgetItem(info->GetStrValue());
240  if (info->CanStrEdit())
241  item->setFlags(item->flags() | Qt::ItemIsEditable);
242  else
243  item->setFlags(item->flags() & ~ Qt::ItemIsEditable);
244  MemberTable->setItem(row, fiColValue, item);
245 
246  item = new QTableWidgetItem(info->GetTitle());
247  item->setFlags(item->flags() & ~Qt::ItemIsEditable);
248  MemberTable->setItem(row, fiColComment, item);
249 
250  MemberTable->setRowHidden(row, !info->IsVisible());
251  }
252 
253  fFillingTable = false;
254 
255  MemberTable->show();
256  ensurePolished();
257  update();
258  show();
259  raise();
260  MemberTable->show();
261 }
262 
264 {
265  if (fItems==0) return;
266 
267  for(int n=0;n<=fItems->GetLast();n++) {
269  info->SetStrValue("");
270  }
271 
272  PleaseUpdateLabel->setVisible(true);
273 
275 }
276 
277 void TGo4ParaEdit::ChangedTable( int row, int col )
278 {
279  if ((col==fiColValue) && !fFillingTable) {
280  QString txt = MemberTable->item(row, col)->text();
281  TGo4ParameterMember* info = (TGo4ParameterMember*) fItems->At(row);
282  info->SetStrValue(txt.toLatin1().constData());
283  PleaseUpdateLabel->setVisible(true);
284  }
285 }
286 
287 void TGo4ParaEdit::TableDoubleClick( int row, int col )
288 {
289  if(col!=fiColName) return;
290 
291  TGo4ParameterMember* info = (TGo4ParameterMember*) fItems->At(row);
292 
293  if (info->IsArrayItem()) {
294  fiCurrentRow = row;
296  } else
297  if (info->IsFitterItem()) {
298  fiCurrentRow = row;
299  EditFitter();
300  }
301 }
302 
303 void TGo4ParaEdit::ContextMenu(const QPoint& pnt)
304 {
305  QTableWidgetItem* item = MemberTable->itemAt (pnt);
306 
307  if ((item==0) || (MemberTable->column(item) != fiColName)) return;
308 
309  int row = MemberTable->row(item);
310 
311  TGo4ParameterMember* info = (TGo4ParameterMember*) fItems->At(row);
312 
313  if (info->IsArrayItem()) {
314  fiCurrentRow = row;
315  QMenu contextMenu(this );
316  contextMenu.addAction("Array View");
317 
318  if (row<fItems->GetLast()) {
319  TGo4ParameterMember* next = (TGo4ParameterMember*) fItems->At(row+1);
320  if ((next->GetMemberId() == info->GetMemberId()) && !next->IsVisible())
321  contextMenu.addAction( "++ \t(next index)", this, SLOT(IncArrayIndex()));
322  }
323 
324  if (row>0) {
325  TGo4ParameterMember* prev = (TGo4ParameterMember*) fItems->At(row-1);
326  if ((prev->GetMemberId() == info->GetMemberId()) && !prev->IsVisible())
327  contextMenu.addAction( "-- \t(prev index)", this, SLOT(DecArrayIndex()));
328  }
329 
330  contextMenu.addAction( "Expand/Shrink", this, SLOT(ExpandShrinkArray()));
331  contextMenu.exec(QCursor::pos());
332  } else
333 
334  if (info->IsFitterItem()) {
335  fiCurrentRow = row;
336  QMenu contextMenu(this );
337  contextMenu.addAction("Modify Fitter");
338 
339  contextMenu.addAction( "Edit...", this, SLOT(EditFitter()));
340 
341  TGo4Fitter* fitter = 0;
342  ServiceCall("GetFitterFromFitPanel", &fitter);
343  if ((fitter!=0) && (info->GetObject()!=fitter))
344  contextMenu.addAction( "Get from FitPanel", this, SLOT(GetFitterFromEditor()));
345  contextMenu.exec(QCursor::pos());
346  }
347 }
348 
350 {
352 
353  Int_t memberid = info->GetMemberId();
354  for(int n=0;n<=fItems->GetLast();n++) {
356  if (item->GetMemberId()==memberid)
357  item->SetVisible(!item->IsVisible());
358  }
359  info->SetVisible(kTRUE);
361 }
362 
364 {
365  if (fiCurrentRow>fItems->GetLast()) return;
368  if (next->GetMemberId() == info->GetMemberId()) {
369  info->SetVisible(kFALSE);
370  next->SetVisible(kTRUE);
372  }
373 }
374 
376 {
377  if (fiCurrentRow==0) return;
380  if (prev->GetMemberId() == info->GetMemberId()) {
381  info->SetVisible(kFALSE);
382  prev->SetVisible(kTRUE);
384  }
385 }
386 
388 {
390  TGo4Fitter* fitter = dynamic_cast<TGo4Fitter*> (info->GetObject());
391 
392  if(fitter==0) {
393  fitter= new TGo4Fitter("Fitter","Fitter from parameter editor");
394  info->SetObject(fitter, kTRUE);
395  }
396 
397  TGo4Slot* fitterslot = AddSlot("Fitter");
398  fitterslot->AssignObject(fitter, kFALSE);
399  EditObjectInSlot(fitterslot);
400  PleaseUpdateLabel->setVisible(true);
401 }
402 
404 {
406  if ((info==0) || !info->IsFitterItem()) return;
407 
408  TGo4Fitter* fitter = 0;
409  ServiceCall("CloneFitterFromFitPanel", &fitter);
410 
411  if (fitter!=0) info->SetObject(fitter, kTRUE);
412 }
413 
415 {
416  TGo4LockGuard lock;
417 
418  if (fItems==0) return;
419 
420  if (GetLinkedName("Parameter")!=0) {
421  TGo4Parameter* par = dynamic_cast<TGo4Parameter*> (GetLinked("Parameter",0));
422  if (par==0) return;
423  if (par->SetMemberValues(fItems))
424  if (SaveItemToFile(GetLinkedName("Parameter"), "Parameters"))
425  PleaseUpdateLabel->setVisible(false);
426  } else
427  if (fItemName.length()>0) {
428  const char* parclass = Browser()->ItemClassName(fItemName.toLatin1().constData());
429  if (parclass==0) return;
430  if (gROOT->GetClass(parclass)==0) {
431  QMessageBox::warning(this, "Parameter editor",
432  QString("Parameter class ") + parclass + " not known to gui");
433  return;
434  }
435  TString foldname, parname;
436  TGo4Slot::ProduceFolderAndName(fItemName.toLatin1().constData(), foldname, parname);
437 
438  TGo4ParameterStatus status(parname, parclass, (TObjArray*)fItems->Clone());
439 
440  TGo4Parameter* par = status.CreateParameter();
441 
442  if (par==0) {
443  QMessageBox::warning(this, "Parameter editor",
444  QString("Can not crate parameter object of class ") + parclass);
445  return;
446  }
447 
448  QFileDialog fd(this, QString("Save ") + fItemName + " in root file",
449  QString(), "ROOT (*.root);;ROOT XML (*.xml)");
450  fd.setFileMode( QFileDialog::AnyFile);
451  fd.setAcceptMode(QFileDialog::AcceptSave);
452  fd.selectFile("file.root");
453  if (fd.exec() != QDialog::Accepted) return;
454 
455  QStringList flst = fd.selectedFiles();
456  if (flst.isEmpty()) return;
457 
458  TFile* f = TFile::Open(flst[0].toLatin1().constData(),"UPDATE");
459  if (f!=0) {
460  f->WriteTObject(par, par->GetName(), "WriteDelete") > 0;
461  delete f;
462  }
463  }
464 }
465 
467 {
468  WorkWithParameter(ParamNameLbl->text().toLatin1().constData(), true);
469 }
470 
472 {
473  TGo4LockGuard lock;
474 
475  if ((fItemName.length()==0) || (fItems==0)) return;
476 
477  TString foldname, parname;
478  TGo4Slot::ProduceFolderAndName(fItemName.toLatin1().constData(), foldname, parname);
479 
480  const char* parclass = Browser()->ItemClassName(fItemName.toLatin1().constData());
481  if (parclass==0) return;
482 
483  TGo4ParameterStatus status(parname, parclass, (TObjArray*)fItems->Clone());
484 
485  if (UpdateItemInAnalysis(fItemName.toLatin1().constData(), &status))
486  if (BrowserItemRemote(fItemName.toLatin1().constData()))
487  RefreshClicked();
488  else
489  PleaseUpdateLabel->setVisible(false);
490 }
virtual void ResetWidget()
Definition: QGo4Widget.cpp:51
TObject * GetObject() const
virtual ~TGo4ParaEdit()
virtual void linkedObjectRemoved(const char *linkname)
bool UpdateItemInAnalysis(const char *itemname, TObject *obj=0)
Definition: QGo4Widget.cpp:383
void EditObjectInSlot(TGo4Slot *slot)
Definition: QGo4Widget.cpp:356
void ShootCloseWidget(bool closeparent=false)
Definition: QGo4Widget.cpp:69
virtual void DropItem(const char *itemname, TClass *cl, int kind)
const char * GetFullName(TString &buf)
virtual void ChangedTable(int row, int col)
TObject * GetLinked(const char *linkname, int updatelevel)
Definition: QGo4Widget.cpp:176
virtual void GetFitterFromEditor()
TGo4ServerProxy * DefineServerObject(const char *itemname, TString *objname=0, Bool_t onlyanalysis=kTRUE)
virtual void ApplyClicked()
Bool_t IsVisible() const
virtual void WorkWithParameter(const char *itemname, bool isrefresh)
Bool_t CanStrEdit() const
virtual void RefreshWidget(TGo4Parameter *par)
void SetStrValue(const char *value)
const char * GetLinkedName(const char *linkname)
Definition: QGo4Widget.cpp:157
TGo4BrowserProxy * Browser()
Definition: QGo4Widget.cpp:223
TObjArray * GetMemberValues(Bool_t takeit=kFALSE)
QString fItemName
Definition: TGo4ParaEdit.h:70
virtual void linkedObjectUpdated(const char *linkname, TObject *obj)
bool BrowserItemRemote(const char *itemname)
Definition: QGo4Widget.cpp:216
void SetObject(TObject *obj, Bool_t owner)
Bool_t IsArrayItem() const
TGo4ParaEdit(QWidget *parent=0, const char *name=0)
bool fFillingTable
Definition: TGo4ParaEdit.h:71
void ServiceCall(const char *name, void *par=0)
Definition: QGo4Widget.cpp:402
void RequestObjectStatus(const char *name, TGo4Slot *tgtslot)
const char * ItemClassName(const char *name)
TObjArray * fItems
Definition: TGo4ParaEdit.h:69
virtual Bool_t CanSubmitObjects()
virtual void clearTextFields()
Bool_t SetMemberValues(TObjArray *fItems)
virtual void ExpandShrinkArray()
const char * GetObjectClass() const
const char * GetStrValue() const
static void ProduceFolderAndName(const char *fullname, TString &foldername, TString &objectname)
Definition: TGo4Slot.cxx:668
virtual void saveFile()
virtual void EditFitter()
virtual void IncArrayIndex()
Bool_t AssignObject(TObject *obj, Bool_t owner)
Definition: TGo4Slot.cxx:361
bool SaveItemToFile(const char *itemname, const char *subfolder=0)
Definition: QGo4Widget.cpp:374
void AddLink(const char *itemname, const char *linkname)
Definition: QGo4Widget.cpp:121
const char * GetTypeName() const
void SetVisible(Bool_t on=kTRUE)
virtual void TableDoubleClick(int row, int col)
virtual bool IsAcceptDrag(const char *itemname, TClass *cl, int kind)
Int_t GetMemberId() const
virtual void DecArrayIndex()
virtual void ShowVisibleItems()
virtual void RefreshClicked()
Bool_t IsFitterItem() const
virtual void ResetWidget()
virtual void ContextMenu(const QPoint &)
void GetMemberValues(TObjArray *fItems)
TGo4Slot * AddSlot(const char *slotname)
Definition: QGo4Widget.cpp:145