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
38using namespace ScenarioManager;
39using namespace Data_Structure;
40using namespace armarx;
41
43{
44 rootItem =
45 new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
46}
47
48QVariant
49ScenarioModel::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
128void
130{
131 delete rootItem;
132 rootItem =
133 new ScenarioItem(QList<QVariant>({"Scenarios", "Start", "Stop", "Restart", "Status"}));
134 reset();
135}
136
137Qt::DropActions
139{
140 return Qt::CopyAction;
141}
142
143Qt::ItemFlags
144ScenarioModel::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
156QStringList
158{
159 QStringList types;
160 types << "application/pointer";
161 return types;
162}
163
164bool
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
219void
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
238void
239ScenarioModel::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
259ScenarioModel::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
275int
276ScenarioModel::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}
uint8_t index
TreeItem representing data contained in a Scenario or an Application.
ScenarioManager::Data_Structure::ApplicationInstancePtr getApplicationInstance()
If this item represents an Application, it is returned.
void update()
Clears the item_data and parses the Scenario or Application for new data.
Class containing data about an application Provides methods to get and set the date contained in the ...
Definition Application.h:47
QStringList mimeTypes() const override
void applicationsDrop(QList< QPair< QString, ScenarioManager::Data_Structure::Application * > > applications, int row, const QModelIndex &parent)
static ScenarioItem * FindScenario(const QAbstractItemModel *model, const QString &scenarioName)
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags of the item at the given index.
ScenarioItem * getRootItem()
static int FindScenarioIndex(const QAbstractItemModel *model, const QString &scenarioName)
void update()
Updates this model.
Qt::DropActions supportedDropActions() const override
Returns the drop-Actions supported by this model.
QVariant data(const QModelIndex &index, int role) const override
Returns the data of the item at the given index based on the given role.
void clear() override
Clears and resets this model.
ScenarioModel()
Sets up the model.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
int childCount() const
Definition treeitem.cpp:57
virtual QVariant data(int column) const
Definition treeitem.cpp:69
TreeItem * child(int row)
Definition treeitem.cpp:51
TreeItem * rootItem
Definition treemodel.h:69
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition treemodel.cpp:99
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition treemodel.cpp:45
QModelIndex parent(const QModelIndex &index) const override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
@ SCENARIOITEMSOURCE
Definition treeitem.h:37