ServerInfoModel.h
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 Manfred Kroehnert ( manfred.kroehnert 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#pragma once
24
25#include <QAbstractTableModel>
26#include <QBrush>
27
28#include <IceGrid/Admin.h>
29
30class ServerInfoModel : public QAbstractTableModel
31{
32 Q_OBJECT
33public:
35 {
36 }
37
38 ServerInfoModel(IceGrid::ServerDynamicInfoSeq serverInfo)
39 {
41 }
42
50
51 int
52 rowCount(const QModelIndex& parent = QModelIndex()) const override
53 {
54 return serverInfo.size();
55 }
56
57 int
58 columnCount(const QModelIndex& parent = QModelIndex()) const override
59 {
60 return 4;
61 }
62
63 QVariant
64 data(const QModelIndex& index, int role = Qt::DisplayRole) const override
65 {
66 if (!index.isValid())
67 {
68 return QVariant();
69 }
70
71 if (index.row() >= (int)serverInfo.size())
72 {
73 return QVariant();
74 }
75
76 if (role == Qt::DisplayRole)
77 {
78 switch (index.column())
79 {
80 case eServerInfo_Id:
81 return QString::fromStdString(serverInfo.at(index.row()).id);
82 break;
83
85 {
86 switch (serverInfo.at(index.row()).state)
87 {
88 case IceGrid::Inactive:
89 return QString("Inactive");
90 break;
91
92 case IceGrid::Activating:
93 return QString("Activating");
94 break;
95
96 case IceGrid::ActivationTimedOut:
97 return QString("ActivationTimedOut");
98 break;
99
100 case IceGrid::Active:
101 return QString("Active");
102 break;
103
104 case IceGrid::Deactivating:
105 return QString("Deactivating");
106 break;
107
108 case IceGrid::Destroying:
109 return QString("Destroying");
110 break;
111
112 case IceGrid::Destroyed:
113 return QString("Destroyed");
114 break;
115 }
116 // Unknown / future ServerState: do not fall through into the PID
117 // column (issue #64.12a).
118 return QVariant();
119 }
120
121 case eServerInfo_Pid:
122 return QString::number(serverInfo.at(index.row()).pid);
123 break;
124
125 default:
126 return QVariant();
127 }
128 }
129 else if (role == Qt::CheckStateRole)
130 {
131 if (index.column() == eServerInfo_Enabled)
132 {
133 return (serverInfo.at(index.row()).enabled) ? Qt::Checked : Qt::Unchecked;
134 }
135 }
136 else if (role == Qt::BackgroundRole)
137 {
138 return (serverInfo.at(index.row()).enabled) ? QBrush(Qt::green) : QBrush(Qt::lightGray);
139 }
140 // No Qt::EditRole: the Enabled column is edited via Qt::CheckStateRole. The old
141 // code returned a bool here (a confusion with flags()) - issue #64.12e.
142
143 return QVariant();
144 }
145
146 QVariant
147 headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
148 {
149 if (role != Qt::DisplayRole)
150 {
151 return QVariant();
152 }
153
154 if (orientation == Qt::Horizontal)
155 {
156 switch (section)
157 {
159 return QString("Enabled");
160 break;
161
162 case eServerInfo_Id:
163 return QString("ID");
164 break;
165
167 return QString("Status");
168 break;
169
170 case eServerInfo_Pid:
171 return QString("PID");
172 break;
173
174 default:
175 return QString("");
176 break;
177 }
178 }
179
180 return QVariant();
181 }
182
183 Qt::ItemFlags
184 flags(const QModelIndex& index) const override
185 {
186 return (index.column() == eServerInfo_Enabled)
187 ? Qt::ItemFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable)
188 : Qt::ItemFlags(Qt::ItemIsEnabled);
189 }
190
191 bool
192 setData(const QModelIndex& index, const QVariant& value, int role) override
193 {
194 if ((role != Qt::CheckStateRole) || (index.column() != eServerInfo_Enabled))
195 {
196 return false;
197 }
198 if (index.row() < 0 || index.row() >= (int)serverInfo.size())
199 {
200 // Stale index: .at() would throw std::out_of_range into the Qt event loop
201 // (issue #64.12d).
202 return false;
203 }
204
205 serverInfo.at(index.row()).enabled = ((Qt::CheckState)value.toInt() == Qt::Checked);
206 emit dataChanged(index, index);
207 emit serverInfoChanged(serverInfo.at(index.row()));
208 return true;
209 }
210
211 bool
212 setData(IceGrid::ServerDynamicInfoSeq newInfo)
213 {
214 // Runs on the GUI thread (via IceGridViewer::selectGridNode). No mutex needed,
215 // and holding one across endResetModel() risked a re-entrant self-deadlock
216 // (issue #64.12b/#64.12c). endResetModel() already notifies the views, so the
217 // out-of-range dataChanged() that used to follow it is gone too (#64.13b).
218 beginResetModel();
219 serverInfo = newInfo;
220 endResetModel();
221 return true;
222 }
223
224signals:
225 void serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo);
226
227protected:
228 IceGrid::ServerDynamicInfoSeq serverInfo;
229};
uint8_t index
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
IceGrid::ServerDynamicInfoSeq serverInfo
ServerInfoModel(IceGrid::ServerDynamicInfoSeq serverInfo)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool setData(IceGrid::ServerDynamicInfoSeq newInfo)
void serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo)