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