GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4LogInfo.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 #include "TGo4LogInfo.h"
15 
16 #include "TList.h"
17 
18 #include <QFileDialog>
19 #include <QTextStream>
20 #include <QHeaderView>
21 
22 TGo4LogInfo::TGo4LogInfo(QWidget *parent, const char *name) :
23  QGo4Widget(parent, name)
24 {
25  setupUi(this);
26 
27  setAcceptDrops(false);
28 
29  LogText->sortByColumn(1, Qt::DescendingOrder);
30 
31  LogText->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
32  LogText->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
33  LogText->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
34  LogText->header()->setStretchLastSection(true);
35 }
36 
38 {
39  ResetWidget();
40  AddLink(slot, "Loginfo");
41 }
42 
43 void TGo4LogInfo::AddMessage(const QDateTime& dt, int level, QString msg)
44 {
45  QString Date = dt.toString("dd.MM.yy ");
46  QString Time = dt.toString("hh.mm.ss ");
47 
48  QTreeWidgetItem* Item = new QTreeWidgetItem();
49  Item->setText(0, Date);
50  Item->setText(1, Time);
51 
52  if (level == 0) {
53  if(msg.contains("GO4-*")) level=1; else
54  if(msg.contains("GO4-#")) level=2; else
55  if(msg.contains("GO4-!")) level=3;
56  msg.remove(0, 6);
57  }
58 
59  switch(level) {
60  case 2:
61  Item->setText(2, "Warning");
62  Item->setIcon(0, QIcon(":/icons/warn.png"));
63  break;
64  case 3:
65  Item->setText(2, "Error");
66  Item->setIcon(0, QIcon(":/icons/error.png"));
67  break;
68  default:
69  Item->setText(2, "Info");
70  Item->setIcon(0, QIcon(":/icons/info.png"));
71  break;
72  }
73 
74  Item->setText(3, msg);
75 
76  LogText->addTopLevelItem(Item);
77  Item->setSelected(true);
78  LogText->setCurrentItem(Item);
79  LogText->scrollToItem(Item);
80 }
81 
82 void TGo4LogInfo::linkedObjectUpdated(const char *linkname, TObject *linkobj)
83 {
84  TList *lst = dynamic_cast<TList *>(linkobj);
85  if (lst) {
86  TListIter iter(lst, kFALSE);
87  while (auto obj = iter()) {
88  // first item is id of current status message, used to submit next request
89  if (obj == lst->First()) continue;
90 
91  const char *msg = obj->GetName();
92 
93  const char *separ = strchr(msg,':');
94  if (!separ || (strlen(separ) < 3)) continue;
95 
96  Long64_t tm = TString(msg, separ-msg).Atoll();
97 
98  QDateTime dt;
99  dt.setMSecsSinceEpoch(tm*1000);
100 
101  separ++;
102  int level = 1;
103  if (*separ=='2') level=2; else
104  if (*separ=='3') level=3;
105 
106  AddMessage(dt, level, separ+2);
107  }
108  } else if (linkobj) {
109  AddMessage(QDateTime::currentDateTime(), 0, linkobj->GetName());
110  }
111 }
112 
114 {
115  LogText->clear();
116 }
117 
119 {
120  QString TextToSave;
121  QFileDialog fd( this, "Save analysis log window", QString(),
122  "Plain text (*.txt)");
123  fd.setFileMode( QFileDialog::AnyFile );
124  fd.setAcceptMode(QFileDialog::AcceptSave);
125 
126  if (fd.exec() != QDialog::Accepted) return;
127 
128  QStringList flst = fd.selectedFiles();
129  if (flst.isEmpty()) return;
130 
131  QString fileName = flst[0];
132  if(!fileName.endsWith(".txt")) fileName.append(".txt");
133  QFile NewFile(fileName);
134  NewFile.open( QIODevice::ReadWrite | QIODevice::Append );
135  QTextStream t( &NewFile );
136 
137  QTreeWidgetItemIterator it(LogText);
138  while (*it) {
139  QTreeWidgetItem* itm = *it++;
140  t << itm->text(0) << " " << itm->text(1) << " " << itm->text(3) << "\n";
141  }
142  NewFile.close();
143 }
virtual void ResetWidget()
Definition: QGo4Widget.cpp:52
virtual void ClearLogInfo()
void AddMessage(const QDateTime &dt, int level, QString msg)
Definition: TGo4LogInfo.cpp:43
virtual void SaveLogInfo()
void WorkWithInfo(TGo4Slot *slot)
Definition: TGo4LogInfo.cpp:37
TGo4LogInfo(QWidget *parent=nullptr, const char *name=nullptr)
Definition: TGo4LogInfo.cpp:22
void linkedObjectUpdated(const char *linkname, TObject *obj) override
Definition: TGo4LogInfo.cpp:82
void AddLink(const char *itemname, const char *linkname)
Definition: QGo4Widget.cpp:117
string msg
Definition: go4init.py:11