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