SettingsController.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 "SettingsController.h"
27 
28 #include <QList>
29 #include <QMessageBox>
30 #include <QSet>
31 #include <QSettings>
32 #include <QStringList>
33 #include <QVariant>
34 
42 
43 using namespace ScenarioManager;
44 using namespace Controller;
45 using namespace Data_Structure;
46 using namespace Exec;
47 using namespace armarx;
48 
50  : QObject(parent), treemodel(new SettingsModel()),
52  packages(packages), executor(executor)
53 {
54  model->setSourceModel(treemodel.get());
55  settingsDialog.setModel(model);
56  stopperFactory = StopperFactory::getFactory();
57  starterFactory = StarterFactory::getFactory();
58 
59  using This = SettingsController;
60  QObject::connect(&settingsDialog, SIGNAL(openPackageChooser()),
61  this, SLOT(showPackageAdderView()));
62 
63  QObject::connect(&settingsDialog, SIGNAL(removePackage(int, int, QModelIndex)),
64  this, SLOT(removePackage(int, int, QModelIndex)));
65 
66  QObject::connect(&packageAdderView, SIGNAL(created(std::string)),
67  this, SLOT(addPackage(std::string)));
68 
69  QObject::connect(&settingsDialog, SIGNAL(changeExecutorSettings(std::string, int, std::string)),
70  this, SLOT(setExecutorStopStrategy(std::string, int, std::string)));
71 
72  QObject::connect(this, SIGNAL(setExecutorState(int, int, int)),
73  &settingsDialog, SLOT(setExecutorState(int, int, int)));
74 
75  QObject::connect(&settingsDialog, SIGNAL(clearPidCache()),
76  this, SLOT(clearPidCache()));
77 
78  QObject::connect(&settingsDialog, SIGNAL(clearXmlCache()),
79  this, SLOT(clearXmlCache()));
80 
81  QObject::connect(&settingsDialog, SIGNAL(XmlCache()),
82  this, SLOT(clearXmlCache()));
83 
84  QObject::connect(&settingsDialog, &SettingsView::closeUnavailablePackages,
85  this, &This::closeUnavailablePackages);
86 
87  //updateModel();
88 }
89 
91 {
92 
93 }
94 
96 {
97  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
98  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
99  QStringList execSettings = settings.value("execSettings").toStringList();
100 
101  if (execSettings.size() < 3)
102  {
103  execSettings.clear();
104  execSettings << "Stop & Kill" << "1000" << "By Id";
105  settings.setValue("execSettings", execSettings);
106  setExecutorStopStrategy(execSettings.at(0).toStdString(), execSettings.at(1).toInt(), execSettings.at(2).toStdString());
107  }
108  else
109  {
110  setExecutorStopStrategy(execSettings.at(0).toStdString(), execSettings.at(1).toInt(), execSettings.at(2).toStdString());
111  }
112 }
113 
115 {
116 
117  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
118  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
119  QStringList execSettings = settings.value("execSettings").toStringList();
120 
121  if (execSettings.size() < 3)
122  {
123  execSettings.clear();
124  execSettings << "Stop & Kill" << "1000" << "By Id";
125  settings.setValue("execSettings", execSettings);
126  setExecutorStopStrategy(execSettings.at(0).toStdString(), execSettings.at(1).toInt(), execSettings.at(2).toStdString());
127  }
128  else
129  {
130  setExecutorStopStrategy(execSettings.at(0).toStdString(), execSettings.at(1).toInt(), execSettings.at(2).toStdString());
131  }
132 
133  settingsDialog.exec();
134 }
135 
137 {
138  packageAdderView.exec();
139 }
140 
141 void SettingsController::addPackage(std::string name)
142 {
143  for (const auto& package : *packages)
144  {
145  if (package->getName().compare(name) == 0)
146  {
147  QMessageBox box;
148  QString message(QString::fromStdString("Package " + name + " is already open"));
149  box.setText(message);
150  box.exec();
151  return;
152  }
153  }
154 
155  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
156  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
157  QStringList packages = settings.value("packages").toStringList();
158  packages.removeDuplicates();
159  packages.append(QString::fromStdString(name));
160 
161  settings.setValue("packages", packages);
162 
163  emit packageAdded(name);
164 }
165 
166 void SettingsController::removePackage(int row, int column, QModelIndex parent)
167 {
168  //TODO finde index aus model heraus und loesche es aus qsettings
169  //PackagePtr package = static_cast<SettingsItem*>(treemodel->getRootItem()->child(row))->getPackage();
170 
171  SettingsItem* settingsItem = model->data(model->index(row, column, parent), SETTINGSITEMSOURCE).value<SettingsItem*>();
172  if (settingsItem == nullptr)
173  {
174  return;
175  }
176 
177  PackagePtr package = settingsItem->getPackage();
178 
179  //Remove Package from QSettings
180  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
181  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
182  QStringList settingsPackages = settings.value("packages").toStringList();
183  settingsPackages.removeDuplicates();
184 
185  settingsPackages.removeAt(settingsPackages.indexOf(QString::fromStdString(package->getName())));
186 
187  settings.setValue("packages", settingsPackages);
188 
189  //Remove Package from Data Structure
190  for (std::vector<PackagePtr>::iterator iter = packages->begin(); iter != packages->end(); ++iter)
191  {
192  if (*iter == package)
193  {
194  packages->erase(iter);
195  break;
196  }
197  }
198 
199  //emit model updates
200  emit packageRemoved();
201 }
202 
203 void SettingsController::setExecutorStopStrategy(std::string killMethod, int delay, std::string stopMethod)
204 {
205  StopStrategyPtr stopStrategy;
206  ApplicationStopperPtr applicationStopper;
207  int killIndex = 0;
208  int stopStrategyIndex = 0;
209  if (stopMethod.compare("By Id") == 0)
210  {
211  applicationStopper = stopperFactory->getPidStopper();
212  killIndex = 0;
213  }
214  else if (stopMethod.compare("By Name") == 0)
215  {
216  applicationStopper = stopperFactory->getByNameStopper();
217  killIndex = 1;
218  }
219 
220  if (killMethod.compare("Stop only") == 0)
221  {
222  stopStrategy = stopStrategyFactory.getStopStrategy(applicationStopper);
223  stopStrategyIndex = 1;
224  }
225  else if (killMethod.compare("Stop & Kill") == 0)
226  {
227  stopStrategy = stopStrategyFactory.getStopAndKillStrategy(applicationStopper, delay);
228  stopStrategyIndex = 0;
229  }
230 
231  if (stopStrategy.get() == nullptr)
232  {
233  ARMARX_INFO_S << "This should not happen";
234  return;
235  }
236  executor->setDefaultStopStrategy(stopStrategy);
237 
238  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
239  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
240  QStringList execSettings = settings.value("execSettings").toStringList();
241  execSettings.clear();
242 
243  execSettings << QString::fromStdString(killMethod) << QString::number(delay) << QString::fromStdString(stopMethod);
244  settings.setValue("execSettings", execSettings);
245  emit setExecutorState(killIndex, delay, stopStrategyIndex);
246 }
247 
249 {
250  treemodel->clear();
251  SettingsItem* rootItem = treemodel->getRootItem();
252  for (const auto& package : *packages)
253  {
254  SettingsItem* packageItem = new SettingsItem(package);
255  rootItem->appendChild(packageItem);
256  }
257 
258  treemodel->update();
259  // For some unknown reason, resetting the smart pointer is necessary to see an updated results list.
260  model.reset(new FilterableTreeModelSortFilterProxyModel());
261  model->setSourceModel(treemodel.get());
262  settingsDialog.setModel(model);
263 }
264 
266 {
268 }
269 
271 {
273 }
274 
275 
277 {
278  const std::string file = armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf";
279  QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
280  QStringList packages = settings.value("packages").toStringList();
281  packages.removeDuplicates();
282 
283  QStringList found;
284  std::vector<std::string> foundStd;
285  std::vector<std::string> unavailableStd;
286  for (int i = 0; i < packages.size(); i++)
287  {
288  const QString name = packages.at(i);
289 
292  if (pFinder.packageFound())
293  {
294  found.append(name);
295  foundStd.push_back(name.toStdString());
296  }
297  else
298  {
299  unavailableStd.push_back(name.toStdString());
300  }
301  }
302 
303  ARMARX_INFO << "Removed " << unavailableStd.size() << " from open packages: \n" << unavailableStd
304  << "\nFound available packages: \n" << foundStd;
305 
306  settings.setValue("packages", found);
307 }
ScenarioManager::Controller::SettingsController::init
void init()
Definition: SettingsController.cpp:95
SettingsController.h
iceparser.h
ScenarioManager::Controller::SettingsController::updateModel
void updateModel()
Updates the packages displayed in the settingsview by reloading all packages into the model.
Definition: SettingsController.cpp:248
armarx::CMakePackageFinder::packageFound
bool packageFound() const
Returns whether or not this package was found with cmake.
Definition: CMakePackageFinder.cpp:485
armarx::CMakePackageFinderCache::findPackage
const CMakePackageFinder & findPackage(const std::string &packageName, const std::filesystem::path &packagePath="", bool suppressStdErr=false, bool usePackagePathOnlyAsHint=false)
Definition: CMakePackageFinderCache.cpp:20
ScenarioManager::Controller::SettingsController::SettingsController
SettingsController(Data_Structure::PackageVectorPtr packages, Exec::ExecutorPtr executor, QObject *parent=0)
Constructor that sets the data structure this controller operates on, the executor which gets configu...
Definition: SettingsController.cpp:49
ScenarioManager::Exec::ApplicationStopperPtr
std::shared_ptr< ApplicationStopper > ApplicationStopperPtr
Definition: ApplicationStopper.h:55
ScenarioManager::Exec::StopStrategyPtr
std::shared_ptr< StopStrategy > StopStrategyPtr
Definition: Executor.h:46
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
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
SETTINGSITEMSOURCE
@ SETTINGSITEMSOURCE
Definition: treeitem.h:38
ScenarioManager::Data_Structure::PackageVectorPtr
std::shared_ptr< std::vector< ScenarioManager::Data_Structure::PackagePtr > > PackageVectorPtr
Definition: Package.h:122
ScenarioManager::Controller::SettingsController::showSettings
void showSettings()
Shows the settings dialog.
Definition: SettingsController.cpp:114
StatusManager.h
ScenarioManager::Controller::SettingsController::setExecutorState
void setExecutorState(int killMethodIndex, int delay, int stopStrategyIndex)
ScenarioManager::Controller::SettingsController::clearPidCache
void clearPidCache()
Definition: SettingsController.cpp:265
SettingsView::setModel
virtual void setModel(FilterableTreeModelSortFilterProxyModelPtr model)
Definition: settingsview.cpp:64
ScenarioManager::Data_Structure::PackagePtr
std::shared_ptr< Package > PackagePtr
Definition: Package.h:121
ScenarioManager::Controller::SettingsController::showPackageAdderView
void showPackageAdderView()
Shows a view that allows the user to add new packages.
Definition: SettingsController.cpp:136
ScenarioManager::Exec::StopStrategyFactory::getStopStrategy
StopStrategyPtr getStopStrategy(ApplicationStopperPtr appStopper)
Creates a StopStrategy that simply tries to stop the application.
Definition: StopStrategyFactory.cpp:35
SettingsModel
Definition: settingsmodel.h:32
FilterableTreeModelSortFilterProxyModel
Model of the FilterableTreeView.
Definition: filterabletreemodelsortfilterproxymodel.h:41
ScenarioManager::Controller::SettingsController::packageRemoved
void packageRemoved()
Emitted when a package gets removed.
ScenarioManager::Controller::SettingsController::removePackage
void removePackage(int row, int column, QModelIndex parent)
Removes a package from the settings.
Definition: SettingsController.cpp:166
ScenarioManager::Controller::SettingsController::setExecutorStopStrategy
void setExecutorStopStrategy(std::string killMethod, int delay, std::string stopMethod)
Configures the Executor.
Definition: SettingsController.cpp:203
ScenarioManager::Parser::IceParser::clearXmlCacheDir
static void clearXmlCacheDir()
Definition: iceparser.cpp:326
armarx::ArmarXDataPath::GetDefaultUserConfigPath
static std::string GetDefaultUserConfigPath()
The user config directory of ArmarX.
Definition: ArmarXDataPath.cpp:782
CMakePackageFinder.h
CMakePackageFinderCache.h
ScenarioManager::Controller::SettingsController::addPackage
void addPackage(std::string name)
Adds a new package to the settings.
Definition: SettingsController.cpp:141
ScenarioManager::Controller::SettingsController::~SettingsController
~SettingsController() override
Destructor.
Definition: SettingsController.cpp:90
ScenarioManager::Exec::StopStrategyFactory::getStopAndKillStrategy
StopStrategyPtr getStopAndKillStrategy(ApplicationStopperPtr appStopper, int delay)
Creates a StopStrategy that first tries to stop the application, the waits a certain amount of time,...
Definition: StopStrategyFactory.cpp:40
ScenarioManager
Definition: Application.cpp:166
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
ScenarioManager::Controller::SettingsController::packageAdded
void packageAdded(std::string name)
Emitted when a package gets added in the settings.
StopStrategyFactory.h
armarx::CMakePackageFinderCache::GlobalCache
static CMakePackageFinderCache GlobalCache
Definition: CMakePackageFinderCache.h:19
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
Logging.h
ScenarioManager::StatusManager::clearCache
static void clearCache()
Definition: StatusManager.cpp:86
ScenarioManager::Controller::SettingsController::clearXmlCache
void clearXmlCache()
Definition: SettingsController.cpp:270
ArmarXDataPath.h
SettingsItem
Definition: settingsitem.h:31
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ScenarioManager::Controller::SettingsController::closeUnavailablePackages
void closeUnavailablePackages()
Definition: SettingsController.cpp:276
SettingsView::closeUnavailablePackages
void closeUnavailablePackages()