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