openscenariocontroller.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
27
28#include <algorithm>
29
30#include <QList>
31#include <QMessageBox>
32#include <QSettings>
33#include <QVariant>
34
43
44
45using namespace ScenarioManager;
46using namespace Controller;
47using namespace Data_Structure;
48using namespace Parser;
49using namespace Exec;
50using namespace armarx;
51
54 QObject* parent) :
55 QObject(parent),
56 favlistSize(5),
57 packages(packages),
58 executor(exec),
59 treemodel(new OpenScenarioModel()),
61{
62 model->setSourceModel(treemodel.get());
63 // openScenarioView.setModel(model);
64
65 QObject::connect(&openScenarioView,
66 SIGNAL(openScenario(int, int, QModelIndex)),
67 this,
68 SLOT(on_openScenario(int, int, QModelIndex)));
69
70 QObject::connect(
71 &openScenarioView, SIGNAL(showAddPackageDialog()), this, SLOT(on_showAddPackageDialog()));
72}
73
77
78void
79OpenScenarioController::on_openScenario(int row, int column, QModelIndex parent)
80{
82 //ScenarioPtr scenario = parser.parseScenario(static_cast<OpenScenarioItem*>(treemodel->index(row, column, parent).internalPointer())->getScenarioPath());
83
84 OpenScenarioItem* openScenarioItem =
85 model->data(model->index(row, column, parent), OPENSCENARIOITEMSOURCE)
86 .value<OpenScenarioItem*>();
87 if (openScenarioItem == nullptr)
88 {
89 return;
90 }
91
92 //get package matching the scenario string
93 PackagePtr package;
94 for (unsigned int i = 0; i < packages->size(); i++)
95 {
96 if (openScenarioItem->getScenarioPackage().compare(packages->at(i)->getName()) == 0)
97 {
98 package = packages->at(i);
99 break;
100 }
101 }
102
103 if (package.get() == nullptr)
104 {
105 return;
106 }
107
108 ScenarioPtr scenario;
109 auto pos = openScenarioItem->getScenarioName().find("/");
110 if (pos != std::string::npos)
111 {
112 scenario = parser.parseScenario(package,
113 openScenarioItem->getScenarioName().substr(pos + 1),
114 openScenarioItem->getScenarioName().substr(0, pos));
115 }
116 else
117 {
118 scenario = parser.parseScenario(package, openScenarioItem->getScenarioName());
119 }
120
121 std::string cacheDir = Parser::IceParser::getCacheDir();
122
123
124 if (scenario.get() == nullptr)
125 {
126 QMessageBox message;
127 std::string messageStr;
128 messageStr << "Failed to load Scenario " << openScenarioItem->getScenarioName()
129 << " in Package " << openScenarioItem->getScenarioPackage() << "\n";
130 messageStr << "Either the File was not found or it has no valid scx formatting";
131 message.setText(QString::fromStdString(messageStr));
132 message.exec();
133 return;
134 }
135
136 for (const auto& it : *scenario->getApplications())
137 {
138 executor->loadAndSetCachedProperties(it, cacheDir, false, false);
139 }
140
141 scenario->reloadAppInstances();
142
143 package->addScenario(scenario);
144
145 const std::string file =
146 armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
147 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
148 //save to openend scenarios
149 QStringList settingsPackages = settings.value("scenarios").toStringList();
150 settingsPackages.removeDuplicates();
151
152 QString toAppend = QString::fromStdString(scenario->getName());
153 toAppend.append("::Package::");
154 toAppend.append(QString::fromStdString(scenario->getPackage()->getName()));
155
156 settingsPackages.append(toAppend);
157 settings.setValue("scenarios", settingsPackages);
158
159 //Save opencount for favourites
160 QMap<QString, int> favouriteCount;
161
162 settings.beginGroup("favourites");
163 QStringList keys = settings.childKeys();
164 foreach (QString key, keys)
165 {
166 favouriteCount[key] = settings.value(key).toInt();
167 }
168 settings.endGroup();
169
170 settings.beginGroup("favourites");
171 QMap<QString, int>::const_iterator i = favouriteCount.constBegin();
172 bool added = false;
173 while (i != favouriteCount.constEnd())
174 {
175 if (i.key().compare(QString::fromStdString(
176 scenario->getName() + "::Package::" + scenario->getPackage()->getName())) == 0)
177 {
178 settings.setValue(i.key(), i.value() + 1);
179 added = true;
180 }
181 else
182 {
183 settings.setValue(i.key(), i.value());
184 }
185 ++i;
186 }
187 //value not in map yet
188 if (!added)
189 {
190 settings.setValue(QString::fromStdString(scenario->getName() +
191 "::Package::" + scenario->getPackage()->getName()),
192 1);
193 }
194
195 settings.endGroup();
196
197 emit updated();
198}
199
200void
202{
203 openScenarioView.exec();
204}
205
206void
211
212void
214{
215 treemodel.reset(new OpenScenarioModel());
216
217 QStringList paths = getAllClosedScenarios();
218 QStringList favourites = getFavouriteScenarios(paths);
219
220 OpenScenarioItem* rootItem = treemodel->getRootItem();
221
222 OpenScenarioItem* favItem =
223 new OpenScenarioItem(QList<QVariant>({"Favourite Scenarios", "", "HIDE"}));
224 OpenScenarioItem* allItem =
225 new OpenScenarioItem(QList<QVariant>({"All Scenarios", "", "HIDE"}));
226
227 for (int i = 0; i < favourites.size(); i++)
228 {
229 QStringList split = favourites.at(i).split("::Package::");
230 OpenScenarioItem* scenarioItem =
231 new OpenScenarioItem(QVariant(split.at(0)), QVariant(split.at(1)));
232 favItem->appendChild(scenarioItem);
233 }
234
235 for (int i = 0; i < paths.size(); i++)
236 {
237 QStringList split = paths.at(i).split("::Package::");
238 OpenScenarioItem* scenarioItem =
239 new OpenScenarioItem(QVariant(split.at(0)), QVariant(split.at(1)));
240 allItem->appendChild(scenarioItem);
241 }
242
243 rootItem->appendChild(favItem);
244 rootItem->appendChild(allItem);
245
246 //treemodel->update();
247 model->setSourceModel(treemodel.get());
248 openScenarioView.setModel(model);
249}
250
251QStringList
252OpenScenarioController::getFavouriteScenarios(QStringList allClosedScenarios)
253{
254 const std::string file =
255 armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
256 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
257
258 QMap<QString, int> favouriteCount;
259
260 settings.beginGroup("favourites");
261 QStringList keys = settings.childKeys();
262 foreach (QString key, keys)
263 {
264 favouriteCount[key] = settings.value(key).toInt();
265 }
266 settings.endGroup();
267
268 if (favouriteCount.size() == 0)
269 {
270 return QStringList();
271 }
272
273 QList<QPair<QString, int>> favouriteSort;
274
275 for (const auto& key : favouriteCount.toStdMap())
276 {
277 favouriteSort.push_back(QPair<QString, int>(key.first, key.second));
278 }
279
280 std::sort(favouriteSort.begin(),
281 favouriteSort.end(),
282 [](const QPair<QString, int>& p1, const QPair<QString, int>& p2)
283 { return p1.second > p2.second; });
284
285 QStringList result;
286
287 for (int i = 0; i < favouriteSort.count(); ++i)
288 {
289 //filter - only show not openend scenarios in favourites
290 if (allClosedScenarios.contains(favouriteSort[i].first))
291 {
292 result.push_back(favouriteSort[i].first);
293 if ((size_t)result.size() == favlistSize)
294 {
295 break;
296 }
297 }
298 }
299
300 return result;
301}
302
303QStringList
304OpenScenarioController::getAllClosedScenarios()
305{
306 const std::string file =
307 armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
308 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
309 auto packages = settings.value("packages").toStringList().toSet();
310 auto openedScenarios = settings.value("scenarios").toStringList().toSet();
311
312 QStringList result;
313 for (const auto& package : packages)
314 // for (int i = 0; i < packages.size(); i++)
315 {
316
317 CMakePackageFinder parser =
319 XMLScenarioParser scenarioParser;
320 std::vector<std::string> packageScenarios =
321 scenarioParser.getScenariosFromFolder(parser.getScenariosDir());
322
323 for (const auto& str : packageScenarios)
324 {
325 bool contains = false;
326 for (const auto& opened : openedScenarios)
327 {
328 if (opened.compare("") == 0)
329 {
330 continue;
331 }
332 QString name = opened.split("::Package::").at(0);
333 QString p = opened.split("::Package::").at(1);
334 if (name.compare(QString::fromStdString(str)) == 0 &&
335 p.compare(QString::fromStdString(package.toStdString())) == 0)
336 {
337 contains = true;
338 break;
339 }
340 }
341 if (!contains)
342 {
343 QString resultStr(QString::fromStdString(str));
344 resultStr.append("::Package::");
345 resultStr.append(package);
346 result << resultStr;
347 }
348 }
349 }
350
351 return result;
352}
std::string str(const T &t)
Item in the TreeItemView.
std::string getScenarioPackage()
Returns the Name of the scenario package represented by this item.
std::string getScenarioName()
Returns the Name of the scenario represented by this item.
Model used by the OpenScenarioView.
OpenScenarioController(Data_Structure::PackageVectorPtr packages, Exec::ExecutorPtr exec, QObject *parent=0)
Constructor that sets the data structure and optionally the parent object.
void updated()
This signal gets emitted if the data structure has changed.
void showOpenScenarioView()
Shows the OpenScenarioView which allows the User to open and load in a Scenario.
void updateModel()
Reloads the packages into the model.
void on_openScenario(int row, int column, QModelIndex parent)
Finds a scenario in the model, based on the given data and updates the current model and data structu...
static std::string getCacheDir()
This class provides different methods to parse and save scenario data in XML-Files.
static std::vector< std::string > getScenariosFromFolder(const std::string &folder)
Finds all .xml scenario files in a folder and returns a list of paths to them.
void appendChild(TreeItem *child)
Definition treeitem.cpp:44
virtual QVariant data(int column) const
Definition treeitem.cpp:69
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
const CMakePackageFinder & findPackage(const std::string &packageName, const std::filesystem::path &packagePath="", bool suppressStdErr=false, bool usePackagePathOnlyAsHint=false)
static CMakePackageFinderCache GlobalCache
Parser * parser
Definition Parser.cpp:34
std::shared_ptr< Scenario > ScenarioPtr
Definition Scenario.h:35
std::shared_ptr< std::vector< ScenarioManager::Data_Structure::PackagePtr > > PackageVectorPtr
Definition Package.h:123
std::shared_ptr< Package > PackagePtr
Definition Package.h:122
std::shared_ptr< Executor > ExecutorPtr
Definition Executor.h:175
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition MemoryID.cpp:563
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
@ OPENSCENARIOITEMSOURCE
Definition treeitem.h:43