ThreadViewerModel.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2013
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "ThreadViewerModel.h"
24
25namespace armarx
26{
27 ThreadViewerModel::ThreadViewerModel(QObject* parent) : QAbstractTableModel(parent)
28 {
29 }
30
34
35 int
36 ThreadViewerModel::rowCount(const QModelIndex& parent) const
37 {
38 if (parent.isValid())
39 {
40 return 0;
41 }
42 return periodicTaskList.size();
43 }
44
45 int
46 ThreadViewerModel::columnCount(const QModelIndex& parent) const
47 {
48 if (parent.isValid())
49 {
50 return 0;
51 }
52 return 8;
53 }
54
55 QVariant
56 ThreadViewerModel::data(const QModelIndex& index, int role) const
57 {
58 const int row = index.row();
59 const int column = index.column();
60 std::unique_lock lock(taskListsMutex);
61
62 if (row < 0 || row >= static_cast<int>(periodicTaskList.size()))
63 {
64 return QVariant();
65 }
66
67 const PeriodicTaskIceBase& entry = periodicTaskList.at(row);
68
69 switch (role)
70 {
71 case Qt::DisplayRole:
72 case Qt::ToolTipRole:
73 switch (column)
74 {
75 case 0:
76 return QString::fromStdString(entry.name);
77 break;
78
79 case 1:
80 if (entry.running)
81 {
82 return "Yes";
83 }
84 else
85 {
86 return "No";
87 }
88
89 break;
90
91 case 2:
92 return entry.intervalMs;
93 break;
94
95 case 3:
96 // A PeriodicTask that has not completed a cycle yet has an empty
97 // workloadList; rbegin() on it would be undefined behaviour.
98 if (entry.workloadList.empty())
99 {
100 return 0.0f;
101 }
102 return *entry.workloadList.rbegin() * 100.0f;
103
104 case 4:
105 return QString::number(0.001 * entry.lastCycleDuration, 'f', 1);
106 break;
107
108 case 5:
109 {
110 QString date = QString::fromStdString(
111 IceUtil::Time::microSeconds(entry.startTime).toDateTime());
112 return date.remove(0, date.indexOf(' ') + 1); // remove date
113 }
114 break;
115
116 case 6:
117 {
118 QString date = QString::fromStdString(
119 IceUtil::Time::microSeconds(entry.lastCycleStartTime).toDateTime());
120 return date.remove(0, date.indexOf(' ') + 1); // remove date
121 }
122 break;
123
124 case 7:
125 {
126 return QString::number(entry.threadId);
127 }
128 break;
129
130 default:
131 return QVariant();
132 }
133
134 break;
135
136 default:
137
138 return QVariant();
139 }
140
141 return QVariant();
142 }
143
144 QVariant
145 ThreadViewerModel::headerData(int section, Qt::Orientation orientation, int role) const
146 {
147 switch (role)
148 {
149 case Qt::DisplayRole:
150 case Qt::ToolTipRole:
151 if (orientation == Qt::Horizontal)
152 {
153 switch (section)
154 {
155 case 0:
156 return "ThreadName";
157
158 case 1:
159 return "Running?";
160
161 case 2:
162 return "Interval (ms)";
163
164 case 3:
165 return "Workload";
166
167 case 4:
168 return "Last cycle duration (ms)";
169
170 case 5:
171 return "Starttime";
172
173 case 6:
174 return "Last cycle start";
175
176 case 7:
177 return "Thread Id";
178 }
179 }
180
181 break;
182 }
183
184 return QAbstractTableModel::headerData(section, orientation, role);
185 }
186
187 void
188 ThreadViewerModel::setPeriodicTaskListData(const PeriodicTaskList& periodicTaskList)
189 {
190 beginResetModel();
191 {
192 // Hold taskListsMutex only around the assignment. Holding it across
193 // endResetModel() (whose re-entrant data() relocks the non-recursive mutex)
194 // risks a self-deadlock now that the setter runs on the GUI thread
195 // (issues #64.12c/#64.14a). endResetModel() already notifies the views, so
196 // the out-of-range dataChanged() that used to follow it is gone (#64.13b).
197 std::unique_lock lock(taskListsMutex);
198 this->periodicTaskList = periodicTaskList;
199 }
200 endResetModel();
201 }
202} // namespace armarx
uint8_t index
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
ThreadViewerModel(QObject *parent=0)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void setPeriodicTaskListData(const PeriodicTaskList &periodicTaskList)
QVariant data(const QModelIndex &index, int role) const override
This file offers overloads of toIce() and fromIce() functions for STL container types.