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 <IceGrid/Admin.h>
27 
28 #include <QBrush>
29 
30 #include <mutex>
31 
32 
34  public QAbstractTableModel
35 {
36  Q_OBJECT
37 public:
39  {
40  }
41 
42 
43  ServerInfoModel(IceGrid::ServerDynamicInfoSeq serverInfo)
44  {
46  }
47 
49  {
54  };
55 
56 
57  int rowCount(const QModelIndex& parent = QModelIndex()) const override
58  {
59  return serverInfo.size();
60  }
61 
62 
63  int columnCount(const QModelIndex& parent = QModelIndex()) const override
64  {
65  return 4;
66  }
67 
68 
69  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
70  {
71  if (!index.isValid())
72  {
73  return QVariant();
74  }
75 
76  if (index.row() >= (int)serverInfo.size())
77  {
78  return QVariant();
79  }
80 
81  if (role == Qt::DisplayRole)
82  {
83  std::unique_lock guard(serverInfoMutex);
84 
85  switch (index.column())
86  {
87  case eServerInfo_Id:
88  return QString::fromStdString(serverInfo.at(index.row()).id);
89  break;
90 
91  case eServerInfo_Status:
92  {
93  switch (serverInfo.at(index.row()).state)
94  {
95  case IceGrid::Inactive:
96  return QString("Inactive");
97  break;
98 
99  case IceGrid::Activating:
100  return QString("Activating");
101  break;
102 
103  case IceGrid::ActivationTimedOut:
104  return QString("ActivationTimedOut");
105  break;
106 
107  case IceGrid::Active:
108  return QString("Active");
109  break;
110 
111  case IceGrid::Deactivating:
112  return QString("Deactivating");
113  break;
114 
115  case IceGrid::Destroying:
116  return QString("Destroying");
117  break;
118 
119  case IceGrid::Destroyed:
120  return QString("Destroyed");
121  break;
122  };
123  [[fallthrough]];
124  }
125 
126  case eServerInfo_Pid:
127  return QString::number(serverInfo.at(index.row()).pid);
128  break;
129 
130  default:
131  return QVariant();
132  }
133  }
134  else if (role == Qt::CheckStateRole)
135  {
136  if (index.column() == eServerInfo_Enabled)
137  {
138  return (serverInfo.at(index.row()).enabled) ? Qt::Checked : Qt::Unchecked;
139  }
140  }
141  else if (role == Qt::BackgroundRole)
142  {
143  return (serverInfo.at(index.row()).enabled) ? QBrush(Qt::green) : QBrush(Qt::lightGray);
144  }
145  else if (role == Qt::EditRole)
146  {
147  return (index.column() == 0);
148  }
149 
150  return QVariant();
151  }
152 
153 
154  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
155  {
156  if (role != Qt::DisplayRole)
157  {
158  return QVariant();
159  }
160 
161  if (orientation == Qt::Horizontal)
162  {
163  switch (section)
164  {
165  case eServerInfo_Enabled:
166  return QString("Enabled");
167  break;
168 
169  case eServerInfo_Id:
170  return QString("ID");
171  break;
172 
173  case eServerInfo_Status:
174  return QString("Status");
175  break;
176 
177  case eServerInfo_Pid:
178  return QString("PID");
179  break;
180 
181  default:
182  return QString("");
183  break;
184  }
185  }
186 
187  return QVariant();
188  }
189 
190 
191  Qt::ItemFlags flags(const QModelIndex& index) const override
192  {
193  return (index.column() == eServerInfo_Enabled) ? Qt::ItemFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable) : Qt::ItemFlags(Qt::ItemIsEnabled);
194  }
195 
196 
197  bool setData(const QModelIndex& index, const QVariant& value, int role) override
198  {
199  if ((role != Qt::CheckStateRole) || (index.column() != eServerInfo_Enabled))
200  {
201  return false;
202  }
203 
204  serverInfo.at(index.row()).enabled = ((Qt::CheckState)value.toInt() == Qt::Checked);
205  emit dataChanged(index, index);
206  emit serverInfoChanged(serverInfo.at(index.row()));
207  return true;
208  }
209 
210 
211  bool setData(IceGrid::ServerDynamicInfoSeq newInfo)
212  {
213  std::unique_lock guard(serverInfoMutex);
214  beginResetModel();
215  serverInfo = newInfo;
216  endResetModel();
217  emit dataChanged(index(0, 0), index(rowCount(), columnCount()));
218  return true;
219  }
220 
221 signals:
222  void serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo);
223 
224 protected:
225  IceGrid::ServerDynamicInfoSeq serverInfo;
226  mutable std::mutex serverInfoMutex;
227 };
228 
ServerInfoModel::COLUMN_INDEX
COLUMN_INDEX
Definition: ServerInfoModel.h:48
ServerInfoModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: ServerInfoModel.h:69
GfxTL::Orientation
ScalarT Orientation(const VectorXD< 2, ScalarT > &p1, const VectorXD< 2, ScalarT > &p2, const VectorXD< 2, ScalarT > &c)
Definition: Orientation.h:9
index
uint8_t index
Definition: EtherCATFrame.h:59
ServerInfoModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: ServerInfoModel.h:63
ServerInfoModel::serverInfo
IceGrid::ServerDynamicInfoSeq serverInfo
Definition: ServerInfoModel.h:225
ServerInfoModel
Definition: ServerInfoModel.h:33
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ServerInfoModel::serverInfoChanged
void serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo)
ServerInfoModel::ServerInfoModel
ServerInfoModel()
Definition: ServerInfoModel.h:38
ServerInfoModel::ServerInfoModel
ServerInfoModel(IceGrid::ServerDynamicInfoSeq serverInfo)
Definition: ServerInfoModel.h:43
ServerInfoModel::setData
bool setData(IceGrid::ServerDynamicInfoSeq newInfo)
Definition: ServerInfoModel.h:211
ServerInfoModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: ServerInfoModel.h:57
ServerInfoModel::eServerInfo_Status
@ eServerInfo_Status
Definition: ServerInfoModel.h:52
ServerInfoModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: ServerInfoModel.h:154
ServerInfoModel::eServerInfo_Pid
@ eServerInfo_Pid
Definition: ServerInfoModel.h:53
ServerInfoModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: ServerInfoModel.h:197
ServerInfoModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: ServerInfoModel.h:191
ServerInfoModel::serverInfoMutex
std::mutex serverInfoMutex
Definition: ServerInfoModel.h:226
ServerInfoModel::eServerInfo_Id
@ eServerInfo_Id
Definition: ServerInfoModel.h:51
armarx::green
QColor green()
Definition: StyleSheets.h:72
ServerInfoModel::eServerInfo_Enabled
@ eServerInfo_Enabled
Definition: ServerInfoModel.h:50