scenariomodel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Cedric Seehausen (usdnr at kit dot edu)
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
26 #include "scenariomodel.h"
27 
28 #include <QColor>
29 #include <QDataStream>
30 #include <QIODevice>
31 #include <QMimeData>
32 #include <QStringList>
33 #include <QtGlobal>
34 
37 
38 using namespace ScenarioManager;
39 using namespace Data_Structure;
40 using namespace armarx;
41 
43 {
44  rootItem =
45  new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
46 }
47 
48 QVariant
49 ScenarioModel::data(const QModelIndex& index, int role) const
50 {
51  if (!index.isValid())
52  {
53  return QVariant();
54  }
55 
56  if (role == Qt::ForegroundRole)
57  {
58  ApplicationInstancePtr instance =
59  static_cast<ScenarioItem*>(index.internalPointer())->getApplicationInstance();
60  if (instance.get() != nullptr && !instance->getEnabled())
61  {
62  return QColor(Qt::lightGray);
63  }
64  if (index.column() == 4)
65  {
66  if (index.data().toString().toStdString() == ApplicationStatus::Missing)
67  {
68  return QColor(Qt::lightGray);
69  }
70  else if (index.data().toString().toStdString() == ApplicationStatus::Running)
71  {
72  return QColor(Qt::green);
73  }
74  else if (index.data().toString().toStdString() == ApplicationStatus::Stopped)
75  {
76  return QColor(Qt::red);
77  }
78  else if (index.data().toString().toStdString() == ApplicationStatus::Inactive)
79  {
80  return QColor(Qt::gray);
81  }
82  else if (index.data().toString().toStdString() == ApplicationStatus::Mixed)
83  {
84  return QColor(Qt::darkYellow);
85  }
86  else if (index.data().toString().toStdString() == ApplicationStatus::Waiting)
87  {
88  return QColor(Qt::blue);
89  }
90  else
91  {
92  return QColor(Qt::black);
93  }
94  }
95  }
96  else if (role == Qt::ToolTipRole)
97  {
98  const QAbstractItemModel* model = index.model();
99  QString appName =
100  model->data(model->index(index.row(), 0, index.parent()), Qt::DisplayRole).toString();
101 
102  if (index.column() == 1)
103  {
104  return QVariant(QString("Start ") + appName);
105  }
106  else if (index.column() == 2)
107  {
108  return QVariant(QString("Stop ") + appName);
109  }
110  else if (index.column() == 3)
111  {
112  return QVariant(QString("Restart ") + appName);
113  }
114  }
115  else if (role == SCENARIOITEMSOURCE)
116  {
117  return QVariant::fromValue(reinterpret_cast<ScenarioItem*>(index.internalPointer()));
118  }
119  else if (role != Qt::DisplayRole)
120  {
121  return QVariant();
122  }
123 
124  ScenarioItem* item = static_cast<ScenarioItem*>(index.internalPointer());
125  return item->data(index.column());
126 }
127 
128 void
130 {
131  delete rootItem;
132  rootItem =
133  new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
134  reset();
135 }
136 
137 Qt::DropActions
139 {
140  return Qt::CopyAction;
141 }
142 
143 Qt::ItemFlags
144 ScenarioModel::flags(const QModelIndex& index) const
145 {
146  Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
147 
148  // if (index.data().toString().compare("Start") == 0 && !index.parent().isValid())
149  // {
150  // return Qt::ItemIsDropEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | defaultFlags;
151  // }
152 
153  return Qt::ItemIsDropEnabled | defaultFlags;
154 }
155 
156 QStringList
158 {
159  QStringList types;
160  types << "application/pointer";
161  return types;
162 }
163 
164 bool
166  Qt::DropAction action,
167  int row,
168  int column,
169  const QModelIndex& parent)
170 {
171  if (action == Qt::IgnoreAction)
172  {
173  return true;
174  }
175 
176  if (!data->hasFormat("application/pointer"))
177  {
178  return false;
179  }
180 
181  if (column > 0)
182  {
183  return false;
184  }
185 
186  // int beginRow;
187 
188  // if (row != -1)
189  // beginRow = row;
190  // else if (parent.isValid())
191  // beginRow = parent.row();
192  // else
193  // beginRow = rowCount(QModelIndex());
194 
195  QByteArray encodedData = data->data("application/pointer");
196  QDataStream stream(&encodedData, QIODevice::ReadOnly);
197  QList<QPair<QString, ScenarioManager::Data_Structure::Application*>> result;
198  int rows = 0;
199 
200  while (!stream.atEnd())
201  {
202  quint64 holder;
203  QString instanceName;
204  stream >> holder >> instanceName;
205 
206  if (holder == 0)
207  {
208  continue;
209  }
210  result << qMakePair(instanceName,
211  reinterpret_cast<Data_Structure::Application*>(holder)); //
212  ++rows;
213  }
214 
215  emit applicationsDrop(result, row, parent);
216  return true;
217 }
218 
219 void
221 {
222  for (int i = 0; i < rootItem->childCount(); i++)
223  {
224  ScenarioItem* child = reinterpret_cast<ScenarioItem*>(rootItem->child(i));
225  child->update();
226  if (child->childCount() > 0)
227  {
228  treeUpdate(child);
229  }
230  }
231 
232  QModelIndex topLeft = index(0, 0);
233  QModelIndex bottomRight = index(rowCount() - 1, columnCount() - 1);
234 
235  emit dataChanged(topLeft, bottomRight);
236 }
237 
238 void
239 ScenarioModel::treeUpdate(ScenarioItem* item)
240 {
241  for (int i = 0; i < item->childCount(); i++)
242  {
243  ScenarioItem* child = reinterpret_cast<ScenarioItem*>(item->child(i));
244  child->update();
245  if (child->childCount() > 0)
246  {
247  treeUpdate(child);
248  }
249  }
250 }
251 
254 {
255  return static_cast<ScenarioItem*>(rootItem);
256 }
257 
259 ScenarioModel::FindScenario(const QAbstractItemModel* model, const QString& scenarioName)
260 {
261  ARMARX_CHECK_EXPRESSION(!scenarioName.isEmpty());
262  for (int i = 0; i < model->rowCount(); ++i)
263  {
264  auto index = model->index(i, 0);
265  auto currentScenarioName = index.data().toString();
266  if (currentScenarioName == scenarioName)
267  {
268  return index.data(SCENARIOITEMSOURCE).value<ScenarioItem*>();
269  break;
270  }
271  }
272  return NULL;
273 }
274 
275 int
276 ScenarioModel::FindScenarioIndex(const QAbstractItemModel* model, const QString& scenarioName)
277 {
278  ARMARX_CHECK_EXPRESSION(!scenarioName.isEmpty());
279  for (int i = 0; i < model->rowCount(); ++i)
280  {
281  auto index = model->index(i, 0);
282  auto currentScenarioName = index.data().toString();
283  if (currentScenarioName == scenarioName)
284  {
285  return i;
286  break;
287  }
288  }
289  return -1;
290 }
ScenarioItem
TreeItem representing data contained in a Scenario or an Application.
Definition: scenarioitem.h:40
scenariomodel.h
ScenarioModel::FindScenarioIndex
static int FindScenarioIndex(const QAbstractItemModel *model, const QString &scenarioName)
Definition: scenariomodel.cpp:276
ScenarioModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: scenariomodel.cpp:165
ScenarioModel::supportedDropActions
Qt::DropActions supportedDropActions() const override
Returns the drop-Actions supported by this model.
Definition: scenariomodel.cpp:138
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeItem::data
virtual QVariant data(int column) const
Definition: treeitem.cpp:69
ScenarioModel::ScenarioModel
ScenarioModel()
Sets up the model.
Definition: scenariomodel.cpp:42
ScenarioModel::data
QVariant data(const QModelIndex &index, int role) const override
Returns the data of the item at the given index based on the given role.
Definition: scenariomodel.cpp:49
SCENARIOITEMSOURCE
@ SCENARIOITEMSOURCE
Definition: treeitem.h:37
ScenarioManager::Data_Structure::ApplicationStatus::Stopped
static const std::string Stopped
Definition: ApplicationInstance.h:47
ScenarioModel::clear
void clear() override
Clears and resets this model.
Definition: scenariomodel.cpp:129
ScenarioModel::getRootItem
ScenarioItem * getRootItem()
Definition: scenariomodel.cpp:253
ScenarioManager::Data_Structure::ApplicationStatus::Missing
static const std::string Missing
Definition: ApplicationInstance.h:53
ScenarioModel::update
void update()
Updates this model.
Definition: scenariomodel.cpp:220
ScenarioManager::Data_Structure::ApplicationStatus::Running
static const std::string Running
Definition: ApplicationInstance.h:48
ApplicationInstancePtr
std::shared_ptr< ScenarioManager::Data_Structure::ApplicationInstance > ApplicationInstancePtr
Definition: StopStrategy.h:8
TreeItem::childCount
int childCount() const
Definition: treeitem.cpp:57
TreeItem::child
TreeItem * child(int row)
Definition: treeitem.cpp:51
ScenarioManager::Data_Structure::Application
Class containing data about an application Provides methods to get and set the date contained in the ...
Definition: Application.h:46
ScenarioModel::mimeTypes
QStringList mimeTypes() const override
Definition: scenariomodel.cpp:157
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ScenarioItem::update
void update()
Clears the item_data and parses the Scenario or Application for new data.
Definition: scenarioitem.cpp:72
ScenarioManager::Data_Structure::ApplicationStatus::Waiting
static const std::string Waiting
Definition: ApplicationInstance.h:49
ScenarioManager::Data_Structure::ApplicationStatus::Inactive
static const std::string Inactive
Definition: ApplicationInstance.h:52
ScenarioManager::Data_Structure::ApplicationStatus::Mixed
static const std::string Mixed
Definition: ApplicationInstance.h:50
armarx::red
QColor red()
Definition: StyleSheets.h:78
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ScenarioModel::FindScenario
static ScenarioItem * FindScenario(const QAbstractItemModel *model, const QString &scenarioName)
Definition: scenariomodel.cpp:259
ScenarioManager
Definition: Application.cpp:180
ScenarioModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags of the item at the given index.
Definition: scenariomodel.cpp:144
Logging.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::green
QColor green()
Definition: StyleSheets.h:72