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"
29 #include <QMimeData>
30 #include <QStringList>
31 #include <QtGlobal>
32 #include <QColor>
33 #include <QIODevice>
34 #include <QDataStream>
35 
36 using namespace ScenarioManager;
37 using namespace Data_Structure;
38 using namespace armarx;
39 
40 
42 {
43  rootItem = new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
44 }
45 
46 QVariant ScenarioModel::data(const QModelIndex& index, int role) const
47 {
48  if (!index.isValid())
49  {
50  return QVariant();
51  }
52 
53  if (role == Qt::ForegroundRole)
54  {
55  ApplicationInstancePtr instance = static_cast<ScenarioItem*>(index.internalPointer())->getApplicationInstance();
56  if (instance.get() != nullptr && !instance->getEnabled())
57  {
58  return QColor(Qt::lightGray);
59  }
60  if (index.column() == 4)
61  {
62  if (index.data().toString().toStdString() == ApplicationStatus::Missing)
63  {
64  return QColor(Qt::lightGray);
65  }
66  else if (index.data().toString().toStdString() == ApplicationStatus::Running)
67  {
68  return QColor(Qt::green);
69  }
70  else if (index.data().toString().toStdString() == ApplicationStatus::Stopped)
71  {
72  return QColor(Qt::red);
73  }
74  else if (index.data().toString().toStdString() == ApplicationStatus::Inactive)
75  {
76  return QColor(Qt::gray);
77  }
78  else if (index.data().toString().toStdString() == ApplicationStatus::Mixed)
79  {
80  return QColor(Qt::darkYellow);
81  }
82  else if (index.data().toString().toStdString() == ApplicationStatus::Waiting)
83  {
84  return QColor(Qt::blue);
85  }
86  else
87  {
88  return QColor(Qt::black);
89  }
90  }
91  }
92  else if (role == Qt::ToolTipRole)
93  {
94  const QAbstractItemModel* model = index.model();
95  QString appName = model->data(model->index(index.row(), 0, index.parent()), Qt::DisplayRole).toString();
96 
97  if (index.column() == 1)
98  {
99  return QVariant(QString("Start ") + appName);
100  }
101  else if (index.column() == 2)
102  {
103  return QVariant(QString("Stop ") + appName);
104  }
105  else if (index.column() == 3)
106  {
107  return QVariant(QString("Restart ") + appName);
108  }
109  }
110  else if (role == SCENARIOITEMSOURCE)
111  {
112  return QVariant::fromValue(reinterpret_cast<ScenarioItem*>(index.internalPointer()));
113  }
114  else if (role != Qt::DisplayRole)
115  {
116  return QVariant();
117  }
118 
119  ScenarioItem* item = static_cast<ScenarioItem*>(index.internalPointer());
120  return item->data(index.column());
121 }
122 
123 
124 
126 {
127  delete rootItem;
128  rootItem = new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
129  reset();
130 }
131 
132 
133 
134 Qt::DropActions ScenarioModel::supportedDropActions() const
135 {
136  return Qt::CopyAction;
137 }
138 
139 Qt::ItemFlags ScenarioModel::flags(const QModelIndex& index) const
140 {
141  Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
142 
143  if (index.data().toString().compare("Start") == 0 && !index.parent().isValid())
144  {
145  return Qt::ItemIsDropEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | defaultFlags;
146  }
147 
148  return Qt::ItemIsDropEnabled | defaultFlags;
149 }
150 
151 QStringList ScenarioModel::mimeTypes() const
152 {
153  QStringList types;
154  types << "application/pointer";
155  return types;
156 }
157 
158 bool ScenarioModel::dropMimeData(const QMimeData* data, Qt::DropAction action,
159  int row, int column, const QModelIndex& parent)
160 {
161  if (action == Qt::IgnoreAction)
162  {
163  return true;
164  }
165 
166  if (!data->hasFormat("application/pointer"))
167  {
168  return false;
169  }
170 
171  if (column > 0)
172  {
173  return false;
174  }
175 
176  // int beginRow;
177 
178  // if (row != -1)
179  // beginRow = row;
180  // else if (parent.isValid())
181  // beginRow = parent.row();
182  // else
183  // beginRow = rowCount(QModelIndex());
184 
185  QByteArray encodedData = data->data("application/pointer");
186  QDataStream stream(&encodedData, QIODevice::ReadOnly);
187  QList<QPair<QString, ScenarioManager::Data_Structure::Application*>> result;
188  int rows = 0;
189 
190  while (!stream.atEnd())
191  {
192  quint64 holder;
193  QString instanceName;
194  stream >> holder >> instanceName;
195 
196  if (holder == 0)
197  {
198  continue;
199  }
200  result << qMakePair(instanceName, reinterpret_cast<Data_Structure::Application*>(holder)); //
201  ++rows;
202  }
203 
204  emit applicationsDrop(result, row, parent);
205  return true;
206 }
207 
209 {
210  for (int i = 0; i < rootItem->childCount(); i++)
211  {
212  ScenarioItem* child = reinterpret_cast<ScenarioItem*>(rootItem->child(i));
213  child->update();
214  if (child->childCount() > 0)
215  {
216  treeUpdate(child);
217  }
218  }
219 
220  QModelIndex topLeft = index(0, 0);
221  QModelIndex bottomRight = index(rowCount() - 1, columnCount() - 1);
222 
223  emit dataChanged(topLeft, bottomRight);
224 }
225 
226 void ScenarioModel::treeUpdate(ScenarioItem* item)
227 {
228  for (int i = 0; i < item->childCount(); i++)
229  {
230  ScenarioItem* child = reinterpret_cast<ScenarioItem*>(item->child(i));
231  child->update();
232  if (child->childCount() > 0)
233  {
234  treeUpdate(child);
235  }
236  }
237 }
238 
240 {
241  return static_cast<ScenarioItem*>(rootItem);
242 }
243 
244 ScenarioItem* ScenarioModel::FindScenario(const QAbstractItemModel* model, const QString& scenarioName)
245 {
246  ARMARX_CHECK_EXPRESSION(!scenarioName.isEmpty());
247  for (int i = 0; i < model->rowCount(); ++i)
248  {
249  auto index = model->index(i, 0);
250  auto currentScenarioName = index.data().toString();
251  if (currentScenarioName == scenarioName)
252  {
253  return index.data(SCENARIOITEMSOURCE).value<ScenarioItem*>();
254  break;
255  }
256  }
257  return NULL;
258 }
259 
260 int ScenarioModel::FindScenarioIndex(const QAbstractItemModel* model, const QString& scenarioName)
261 {
262  ARMARX_CHECK_EXPRESSION(!scenarioName.isEmpty());
263  for (int i = 0; i < model->rowCount(); ++i)
264  {
265  auto index = model->index(i, 0);
266  auto currentScenarioName = index.data().toString();
267  if (currentScenarioName == scenarioName)
268  {
269  return i;
270  break;
271  }
272  }
273  return -1;
274 }
ScenarioItem
TreeItem representing data contained in a Scenario or an Application.
Definition: scenarioitem.h:38
scenariomodel.h
ScenarioModel::FindScenarioIndex
static int FindScenarioIndex(const QAbstractItemModel *model, const QString &scenarioName)
Definition: scenariomodel.cpp:260
ScenarioModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: scenariomodel.cpp:158
ScenarioModel::supportedDropActions
Qt::DropActions supportedDropActions() const override
Returns the drop-Actions supported by this model.
Definition: scenariomodel.cpp:134
index
uint8_t index
Definition: EtherCATFrame.h:59
TreeItem::data
virtual QVariant data(int column) const
Definition: treeitem.cpp:64
ScenarioModel::ScenarioModel
ScenarioModel()
Sets up the model.
Definition: scenariomodel.cpp:41
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:46
SCENARIOITEMSOURCE
@ SCENARIOITEMSOURCE
Definition: treeitem.h:36
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:125
ScenarioModel::getRootItem
ScenarioItem * getRootItem()
Definition: scenariomodel.cpp:239
ScenarioManager::Data_Structure::ApplicationStatus::Missing
static const std::string Missing
Definition: ApplicationInstance.h:53
ScenarioModel::update
void update()
Updates this model.
Definition: scenariomodel.cpp:208
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:7
TreeItem::childCount
int childCount() const
Definition: treeitem.cpp:54
TreeItem::child
TreeItem * child(int row)
Definition: treeitem.cpp:49
ScenarioModel::mimeTypes
QStringList mimeTypes() const override
Definition: scenariomodel.cpp:151
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:71
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:76
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:244
ScenarioManager
Definition: Application.cpp:166
ScenarioModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags of the item at the given index.
Definition: scenariomodel.cpp:139
Logging.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::green
QColor green()
Definition: StyleSheets.h:72