VPTissue Reference Manual
TaskOverview.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "TaskOverview.h"
21 
23 #include "util/misc/StringUtils.h"
24 
25 #include <QDateTime>
26 #include <QHeaderView>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QSignalMapper>
30 #include <QStandardItemModel>
31 #include <QTableView>
32 #include <QTimer>
33 #include <QVBoxLayout>
34 
35 using namespace std;
37 
38 namespace SimPT_Parex {
39 namespace {
40 
41 QString RunningTimeToString(unsigned int time)
42 {
43  int seconds = time % 60;
44  time /= 60;
45  int minutes = time % 60;
46  time /= 60;
47  int hours = time % 24;
48  int days = time / 24;
49 
50  return QString::fromStdString(
51  ToString(days, 2, '0') + ":" +
52  ToString(hours, 2, '0') + ":" +
53  ToString(minutes, 2, '0') + ":" +
54  ToString(seconds, 2, '0'));
55 }
56 
57 QString StateToString(const ExplorationTask &task)
58 {
59  switch(task.GetState()) {
60  case TaskState::Waiting:
61  if (task.GetNumberOfTries() == 0)
62  return "Waiting";
63  else
64  return "Retrying ("
65  + QString::fromStdString(to_string(task.GetNumberOfTries())) + ")";
66  case TaskState::Running:
67  return "Running";
68  case TaskState::Finished:
69  return "Finished";
70  case TaskState::Cancelled:
71  return "Cancelled";
72  case TaskState::Failed:
73  return "Failed";
74  default:
75  assert(false && "This task is in an unknown state.");
76  return "Unknown";
77  }
78 }
79 
80 }
81 
82 TaskOverview::TaskOverview()
83  : m_exploration_progress(nullptr), m_table(nullptr), m_model(nullptr), m_refresh_timer(new QTimer(this))
84 {
85  setMinimumHeight(300);
86  setMinimumWidth(600);
87 
88  QVBoxLayout* layout = new QVBoxLayout;
89 
90  m_table = new QTableView(this);
91  m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
92  m_model = new QStandardItemModel(m_table);
93 
94  QPushButton* close_button = new QPushButton("Close");
95  connect(close_button, SIGNAL(clicked()), this, SLOT(close()));
96 
97  layout->addWidget(m_table);
98  layout->addWidget(close_button);
99 
100  setLayout(layout);
101 
102  UpdateExploration(m_exploration_progress);
103 
104  connect(m_refresh_timer, SIGNAL(timeout()), this, SLOT(Refresh()));
105  m_refresh_timer->setInterval(1000);
106 }
107 
109 {
110  m_refresh_timer->stop();
111  delete m_table;
112 }
113 
114 void TaskOverview::UpdateExploration(const std::shared_ptr<const ExplorationProgress>& explorationProgress)
115 {
116  QStringList header;
117  header << "Id" << "Status"<< "Action" << "Time" << "Node";
118  m_model->clear();
119 
120  m_exploration_progress = explorationProgress;
121  if (m_exploration_progress) {
122  const Exploration& exploration = m_exploration_progress->GetExploration();
123 
124  for (const auto& param : exploration.GetParameters()) {
125  header << QString::fromStdString(param).split('.').last();
126  }
127 
128  m_model->setHorizontalHeaderLabels(header);
129 
130  QSignalMapper* stop_signal = new QSignalMapper(this);
131  QSignalMapper* restart_signal = new QSignalMapper(this);
132  connect(stop_signal, SIGNAL(mapped(int)), this, SIGNAL(StopTask(int)));
133  connect(restart_signal, SIGNAL(mapped(int)), this, SIGNAL(StartTask(int)));
134 
135  for (unsigned int i = 0; i < exploration.GetNumberOfTasks(); i++) {
136  auto task = m_exploration_progress->GetTask(i);
137  auto state = task.GetState();
138  int j = 0;
139  m_model->setItem(i, j++, new QStandardItem(QString::number(i)));
140  m_model->setItem(i, j++, new QStandardItem(StateToString(task)));
141  m_model->setItem(i, j++, 0);
142  m_model->setItem(i, j++, new QStandardItem(RunningTimeToString(task.GetRunningTime())));
143 
144  if (state == TaskState::Running || state == TaskState::Waiting) {
145  QPushButton* button = new QPushButton("Stop");
146  connect(button, SIGNAL(clicked()), stop_signal, SLOT(map()));
147  stop_signal->setMapping(button, i);
148  m_table->setIndexWidget(m_model->index(i,2), button);
149  } else if (state == TaskState::Cancelled) {
150  QPushButton* button = new QPushButton("Restart");
151  connect(button, SIGNAL(clicked()), restart_signal, SLOT(map()));
152  restart_signal->setMapping(button, i);
153  m_table->setIndexWidget(m_model->index(i,2), button);
154  }
155  m_model->setItem(i, j++, new QStandardItem(QString::fromStdString(task.getNodeIp())+":"+QString::number(task.getNodePort())));
156 
157  for (const std::string &value : exploration.GetValues(i)) {
158  m_model->setItem(i, j++, new QStandardItem(QString::fromStdString(value)));
159  }
160  }
161  }
162 
163  m_model->setHorizontalHeaderLabels(header);
164  m_table->setModel(m_model);
165  m_table->resizeColumnsToContents();
166  m_table->horizontalHeader()->setStretchLastSection(true);
167 }
168 
169 void TaskOverview::showEvent(QShowEvent* event)
170 {
171  m_refresh_timer->start();
172  QWidget::showEvent(event);
173 }
174 
175 void TaskOverview::hideEvent(QHideEvent* event)
176 {
177  m_refresh_timer->stop();
178  QWidget::hideEvent(event);
179 }
180 
181 void TaskOverview::Refresh()
182 {
183  if (m_exploration_progress) {
184  auto num = m_exploration_progress->GetExploration().GetNumberOfTasks();
185  for (unsigned int i = 0; i < num; ++i) {
186  m_model->setItem(i, 3, new QStandardItem(
187  RunningTimeToString(m_exploration_progress->GetTask(i).GetRunningTime())));
188  }
189  }
190 }
191 
192 } // namespace
Interface for Exploration.
STL namespace.
Interface for TaskOverview.
The task is waiting to be sent for the first time.
string ToString(Type w)
Converts a WallType::Type value to corresponding name.
Definition: WallType.cpp:49
virtual unsigned int GetNumberOfTasks() const =0
Returns the number of tasks the exploration currently contains.
virtual std::vector< std::string > GetValues(unsigned int index) const =0
Return the values of a certain task.
virtual ~TaskOverview()
Destructor.
The task was finished.
virtual std::vector< std::string > GetParameters() const =0
Returns all the parameters in the exploration.
static std::string ToString(const T &value)
Builds a string representation of a value of type T.
Definition: StringUtils.h:70
void UpdateExploration(const std::shared_ptr< const ExplorationProgress > &explorationProgress)
Update the current exploration.
void StartTask(int task)
Send message to restart a stopped task.
virtual void showEvent(QShowEvent *event) override
Overriding QWidget::showEvent to start the refresh timer.
virtual void hideEvent(QHideEvent *event) override
Overriding QWidget::hideEvent to stop the refresh timer.
void StopTask(int task)
Send message to stop a certain task.
String manipulation utilities.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
Class describing a generic exploration.
Definition: Exploration.h:33