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
43using namespace ScenarioManager;
44using namespace Controller;
45using namespace Data_Structure;
46using namespace Exec;
47using 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
102
103void
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
130void
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
160void
162{
163 packageAdderView.exec();
164}
165
166void
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
193void
194SettingsController::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
234void
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
284void
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.
298 model->setSourceModel(treemodel.get());
299 settingsDialog.setModel(model);
300}
301
302void
307
308void
313
314void
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
331 armarx::CMakePackageFinderCache::GlobalCache.findPackage(name.toStdString());
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}
void packageRemoved()
Emitted when a package gets removed.
void setExecutorState(int killMethodIndex, int delay, int stopStrategyIndex)
void setExecutorStopStrategy(std::string killMethod, int delay, std::string stopMethod)
Configures the Executor.
void showPackageAdderView()
Shows a view that allows the user to add new packages.
void updateModel()
Updates the packages displayed in the settingsview by reloading all packages into the model.
void removePackage(int row, int column, QModelIndex parent)
Removes a package from the settings.
void addPackage(std::string name)
Adds a new package to the settings.
void packageAdded(std::string name)
Emitted when a package gets added in the settings.
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...
static std::shared_ptr< StarterFactory > getFactory()
Returns the right StarterFactory for this operatin system.
static std::shared_ptr< StopperFactory > getFactory()
Returns the right StopperFactory for this operating system.
void closeUnavailablePackages()
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.
static CMakePackageFinderCache GlobalCache
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
bool packageFound() const
Returns whether or not this package was found with cmake.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_INFO_S
Definition Logging.h:202
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< ApplicationStopper > ApplicationStopperPtr
std::shared_ptr< Executor > ExecutorPtr
Definition Executor.h:175
std::shared_ptr< StopStrategy > StopStrategyPtr
Definition Executor.h:46
This file offers overloads of toIce() and fromIce() functions for STL container types.
@ SETTINGSITEMSOURCE
Definition treeitem.h:39