scenarioitem.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 "scenarioitem.h"
27
30
31using namespace armarx;
32using namespace ScenarioManager;
33using namespace Data_Structure;
34
35ScenarioItem::ScenarioItem(QList<QVariant> rootData)
36{
37 m_itemData = rootData.toVector();
38}
39
41{
42 this->scenario = scenario;
43
44 update();
45}
46
48{
49 this->application = application;
50 setEnabled(application->getEnabled());
51
52 update();
53}
54
55ScenarioItem::ScenarioItem(std::string name, std::vector<ApplicationInstancePtr> applications) :
56 applications(new std::vector<ApplicationInstancePtr>(applications))
57{
58 this->packageName = name;
59
60 for (const auto& appl : applications)
61 {
62 ScenarioItem* currentItem = new ScenarioItem(appl);
63 currentItem->setEnabled(appl->getEnabled());
64
65 this->appendChild(currentItem);
66 }
67
68 update();
69}
70
71bool
73{
74 const QVector<QVariant> previousData = m_itemData;
75 const Intent previousIntent = pendingIntent;
76
77 if (m_itemData.size() < 5)
78 {
79 m_itemData.resize(5);
80 m_itemData[1] = "Start";
81 m_itemData[2] = "Stop";
82 m_itemData[3] = "Restart";
83 }
84 if (scenario.get() != nullptr)
85 {
86 m_itemData[0] = scenario->getName().c_str();
87 m_itemData[4] = QString::fromStdString(scenario->getStatus());
88 pendingIntent = aggregateChildIntents();
89 }
90 else if (!packageName.empty())
91 {
92 m_itemData[0] = QString::fromStdString(packageName);
93
94 // The children have already been updated by ScenarioModel::update(), which walks the tree
95 // bottom-up. Do not update them again from here: that used to make every application item
96 // recompute itself twice per refresh.
97 std::vector<std::string> childStati;
98 childStati.reserve(childCount());
99 for (int i = 0; i < childCount(); i++)
100 {
102 if (!child(i)->isEnabled())
103 {
104 continue;
105 }
106 childStati.push_back(child(i)->data(4).toString().toStdString());
107 }
108
109 m_itemData[4] = QString::fromStdString(ApplicationStatus::Aggregate(childStati));
110 pendingIntent = aggregateChildIntents();
111 }
112 else
113 {
114 if (application->getInstanceName().empty())
115 {
116 m_itemData[0] = application->getName().c_str();
117 }
118 else
119 {
120 m_itemData[0] = (application->getInstanceName() + "." + application->getName()).c_str();
121 }
122
123 pendingIntent = application->getPendingIntent();
124
125 std::string status = application->getStatus();
126 if (!application->getFound())
127 {
128 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Missing));
129 }
131 {
132 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Running));
133 }
135 {
136 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Stopped));
137 }
139 {
140 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Inactive));
141 }
143 {
144 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Waiting));
145 }
146 else
147 {
148 m_itemData[4] = QVariant(QString::fromStdString(ApplicationStatus::Unknown));
149 }
150
151 if (m_itemData[4] == QString::fromStdString(ApplicationStatus::Missing) &&
152 previousData.value(4) != m_itemData[4])
153 {
154 // Status is shown as "Missing", no need for a warning, info is enough. Only logged on
155 // the transition: this runs on the GUI thread on every status refresh.
156 ARMARX_INFO << "Application '" + application->getExecutableAbsPath() + "' not found!";
157 }
158
159 setEnabled(application->getEnabled());
160 }
161
162 return m_itemData != previousData || pendingIntent != previousIntent;
163}
164
165Intent
167{
168 return pendingIntent;
169}
170
171Intent
172ScenarioItem::aggregateChildIntents()
173{
174 Intent shared = Intent::None;
175 for (int i = 0; i < childCount(); i++)
176 {
177 auto* item = dynamic_cast<ScenarioItem*>(child(i));
178 if (item == nullptr || !item->isEnabled())
179 {
180 continue;
181 }
182
183 const Intent childIntent = item->getPendingIntent();
184 if (childIntent == Intent::None)
185 {
186 continue;
187 }
188 if (shared == Intent::None)
189 {
190 shared = childIntent;
191 }
192 else if (shared != childIntent)
193 {
194 // The subtree is doing different things at once - do not claim any single one of them.
195 return Intent::None;
196 }
197 }
198 return shared;
199}
200
201std::string
203{
204 return packageName;
205}
206
209{
210 return scenario;
211}
212
215{
216 return application;
217}
218
221{
222 return applications;
223}
TreeItem representing data contained in a Scenario or an Application.
ScenarioManager::Data_Structure::ScenarioPtr getScenario()
If this item represents a Scenario, it is returned.
ScenarioManager::Data_Structure::ApplicationInstancePtr getApplicationInstance()
If this item represents an Application, it is returned.
std::string getPackageName()
ScenarioManager::Data_Structure::Intent getPendingIntent() const
The operation this row is currently moving towards, or Intent::None when idle.
bool update()
Clears the item_data and parses the Scenario or Application for new data.
ScenarioManager::Data_Structure::ApplicationInstanceVectorPtr getApplicationInstances()
ScenarioItem(QList< QVariant > rootData)
Constructor that sets the data contained in this item.
int childCount() const
Definition treeitem.cpp:57
void appendChild(TreeItem *child)
Definition treeitem.cpp:44
virtual QVariant data(int column) const
Definition treeitem.cpp:69
void setEnabled(bool enabled)
Definition treeitem.cpp:92
bool isEnabled()
Definition treeitem.cpp:98
TreeItem * child(int row)
Definition treeitem.cpp:51
QVector< QVariant > m_itemData
Definition treeitem.h:77
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:179
std::shared_ptr< Scenario > ScenarioPtr
Definition Scenario.h:35
Intent
What the ScenarioManager is currently trying to do with an application.
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
std::shared_ptr< std::vector< ApplicationInstancePtr > > ApplicationInstanceVectorPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
static std::string Aggregate(const std::vector< std::string > &stati)
Combines the stati of several applications into the status of their common parent (a scenario or the ...