ScenarioManagerWidgetController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ScenarioManager::gui-plugins::ScenarioManagerWidgetController
17  * @author [Cedric Seehausen] ( [usdnr@student.kit.edu] )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <QCoreApplication>
26 #include <QMetaType>
27 #include <QProgressDialog>
28 #include <QToolBar>
29 #include <QToolTip>
30 
34 
36 
37 #include "gui/namelocationview.h"
38 #include <SimoxUtility/algorithm/string/string_tools.h>
39 
40 using namespace armarx;
41 using namespace ScenarioManager;
42 using namespace Exec;
43 using namespace Controller;
44 using namespace Data_Structure;
45 using namespace Parser;
46 
48  packages(new std::vector<PackagePtr>()),
49  executor(new Executor(
50  stopStrategyFactory.getStopStrategy(StopperFactory::getFactory()->getPidStopper()),
51  StarterFactory::getFactory()->getStarter())),
52  applicationController(packages, executor),
53  detailedApplicationController(executor),
54  scenarioListController(packages, executor),
55  settingsController(packages, executor),
56  openScenarioController(packages, executor),
57  settingsFile(armarx::ArmarXDataPath::GetDefaultUserConfigPath() + "/ScenarioManager.conf")
58 {
59  widget.setupUi(getWidget());
60  editModeAction = new QAction("Edit Mode", this);
61 
62  QSettings q_settings(QString(settingsFile.c_str()), QSettings::NativeFormat);
63  bool editModeState = q_settings.value("editMode", false).toBool();
64  editMode(editModeState);
65 }
66 
68 {
69 }
70 
71 bool ScenarioManagerWidgetController::isPackageAutoDiscoveryEnabled()
72 {
73  const std::string autodiscoverPropertyName = "AutodiscoverPackages";
74 
75  if(hasProperty(autodiscoverPropertyName))
76  {
77  return getProperty<bool>(autodiscoverPropertyName).getValue();
78  }
79 
80  ARMARX_INFO << autodiscoverPropertyName + " property not found";
81 
82  return true; // Property not available. Default: use autodiscovery
83 }
84 
85 
86 void ScenarioManagerWidgetController::autoDiscoverArmarXPackages()
87 {
88  QSettings autosettings(QString(settingsFile.c_str()), QSettings::NativeFormat);
89 
90 
92  std::sort(packagesStd.begin(), packagesStd.end(),
93  [](const std::string& lhs, const std::string& rhs) -> bool {
94  return simox::alg::to_lower(lhs) < simox::alg::to_lower(rhs);
95  });
96 
97  QStringList packages;
98  ARMARX_INFO << "Discovered the following ArmarX packages: ";
99  for(const auto& pkg : packagesStd)
100  {
101  packages.push_back(QString::fromStdString(pkg));
102  ARMARX_INFO << "- " << pkg;
103  }
104 
105  autosettings.setValue("packages", packages);
106 }
107 void
109 {
110  QSettings autosettings(QString(settingsFile.c_str()), QSettings::NativeFormat);
111  QStringList scenarios = settings->value("scenarios", QStringList()).toStringList();
112  scenarios.removeDuplicates();
113  if (not scenarios.empty())
114  {
115  autosettings.setValue("scenarios", scenarios);
116  }
117  QStringList packages = settings->value("packages", QStringList()).toStringList();
118  packages.removeDuplicates();
119  if (not packages.empty())
120  {
121  autosettings.setValue("packages", packages);
122  }
123 }
124 
125 void
127 {
128  QSettings autosettings(QString(settingsFile.c_str()), QSettings::NativeFormat);
129  auto scenarios = autosettings.value("scenarios", QStringList()).toStringList();
130  auto packages = autosettings.value("packages", QStringList()).toStringList();
131  scenarios.removeDuplicates();
132  packages.removeDuplicates();
133  settings->setValue("scenarios", scenarios);
134  settings->setValue("packages", packages);
135 }
136 
137 
139 {
140  QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
141 }
142 
143 void
144 ScenarioManagerWidgetController::init()
145 {
146  scenarioListController.setIceAdmin(
147  getArmarXManager()->getIceManager()->getIceGridSession()->getAdmin());
148  detailedApplicationController.setIceAdmin(
149  getArmarXManager()->getIceManager()->getIceGridSession()->getAdmin());
150  detailedApplicationController.setDetailedApplicationView(widget.detailedApplicationView);
151 
152  QObject::connect(widget.scenarioView,
153  SIGNAL(createScenario()),
154  &scenarioListController,
155  SLOT(createScenario()));
156 
157  QObject::connect(
158  widget.scenarioView, SIGNAL(createScenario()), widget.applicationDatabase, SLOT(show()));
159 
160  QObject::connect(widget.scenarioView,
161  SIGNAL(removeItem(QModelIndex)),
162  &scenarioListController,
163  SLOT(removeItem(QModelIndex)));
164 
165  QObject::connect(widget.scenarioView,
166  SIGNAL(itemClicked(QModelIndex)),
167  &scenarioListController,
168  SLOT(showApplication(QModelIndex)));
169 
170  QObject::connect(widget.scenarioView,
171  SIGNAL(startApplication(int, int, QModelIndex)),
172  &scenarioListController,
173  SLOT(start(int, int, QModelIndex)));
174 
175  QObject::connect(widget.scenarioView,
176  SIGNAL(stopApplication(int, int, QModelIndex)),
177  &scenarioListController,
178  SLOT(stop(int, int, QModelIndex)));
179 
180  QObject::connect(widget.scenarioView,
181  SIGNAL(restartApplication(int, int, QModelIndex)),
182  &scenarioListController,
183  SLOT(restart(int, int, QModelIndex)));
184 
185  QObject::connect(&settingsController, SIGNAL(packageRemoved()), this, SLOT(updateModels()));
186 
187  QObject::connect(&settingsController,
188  SIGNAL(packageAdded(std::string)),
189  this,
190  SLOT(reparsePackage(std::string)));
191 
192  QObject::connect(&scenarioListController, SIGNAL(updated()), this, SLOT(updateModels()));
193 
194  QObject::connect(
195  &scenarioListController,
196  SIGNAL(applicationInstanceClicked(Data_Structure::ApplicationInstancePtr, ScenarioItem*)),
197  &detailedApplicationController,
198  SLOT(showApplicationInstance(Data_Structure::ApplicationInstancePtr, ScenarioItem*)));
199 
200  QObject::connect(&scenarioListController,
201  SIGNAL(scenarioClicked(Data_Structure::ScenarioPtr)),
202  &detailedApplicationController,
203  SLOT(showScenario(Data_Structure::ScenarioPtr)));
204 
205  QObject::connect(&scenarioListController,
206  SIGNAL(statusUpdated()),
207  widget.detailedApplicationView,
208  SLOT(updateStatus()));
209 
210  QObject::connect(widget.applicationDatabase,
211  SIGNAL(openSettings()),
212  &settingsController,
213  SLOT(showSettings()));
214 
215  QObject::connect(widget.applicationDatabase,
216  SIGNAL(itemClicked(QModelIndex)),
217  &applicationController,
218  SLOT(on_itemClicked(QModelIndex)));
219 
220  QObject::connect(&applicationController,
221  SIGNAL(applicationClicked(Data_Structure::ApplicationPtr)),
222  &detailedApplicationController,
223  SLOT(showApplication(Data_Structure::ApplicationPtr)));
224 
225  QObject::connect(&applicationController,
226  SIGNAL(packageClicked(Data_Structure::PackagePtr)),
227  &detailedApplicationController,
228  SLOT(showPackage(Data_Structure::PackagePtr)));
229 
230  QObject::connect(&openScenarioController, SIGNAL(updated()), this, SLOT(updateModels()));
231 
232  QObject::connect(widget.scenarioView,
233  SIGNAL(showOpenDialog()),
234  &openScenarioController,
235  SLOT(showOpenScenarioView()));
236 
237  QObject::connect(widget.detailedApplicationView,
238  SIGNAL(startButtonClicked()),
239  &detailedApplicationController,
240  SLOT(start()));
241 
242  QObject::connect(widget.detailedApplicationView,
243  SIGNAL(stopButtonClicked()),
244  &detailedApplicationController,
245  SLOT(stop()));
246 
247  QObject::connect(widget.detailedApplicationView,
248  SIGNAL(restartButtonClicked()),
249  &detailedApplicationController,
250  SLOT(save()));
251 
252  QObject::connect(widget.detailedApplicationView,
253  SIGNAL(toolButtonClicked()),
254  &detailedApplicationController,
255  SLOT(showInStandardEditor()));
256 
257  QObject::connect(widget.detailedApplicationView,
258  SIGNAL(addParameterButtonClicked()),
259  &detailedApplicationController,
260  SLOT(showPropertyAddView()));
261 
262  QObject::connect(widget.detailedApplicationView,
263  SIGNAL(addIceEnvButtonClicked()),
264  &detailedApplicationController,
265  SLOT(showIceEnvVariableAddView()));
266 
267 
268  QObject::connect(widget.detailedApplicationView,
269  SIGNAL(applicationEnabledChange(bool)),
270  &scenarioListController,
271  SLOT(updateModel()));
272 
273  QObject::connect(&openScenarioController,
274  SIGNAL(showPackageDialog()),
275  &settingsController,
276  SLOT(showPackageAdderView()));
277 
278  QObject::connect(widget.detailedApplicationView,
280  &scenarioListController,
282 
283  QObject::connect(&applicationController,
284  SIGNAL(modelUpdated(FilterableTreeModelSortFilterProxyModelPtr)),
285  widget.applicationDatabase,
287 
288  if(isPackageAutoDiscoveryEnabled())
289  {
290  ARMARX_IMPORTANT << "ArmarX package autodiscovery enabled.";
291  autoDiscoverArmarXPackages();
292  }
293 
294  widget.scenarioView->setModel(scenarioListController.getTreeModel());
295 
296  connect(editModeAction, SIGNAL(toggled(bool)), this, SLOT(editMode(bool)));
297 
298  editModeAction->setCheckable(true);
299  editModeAction->setToolTip(
300  "If toggled the application database and the application property view will be shown.");
301 
302  QSettings settings(QString(settingsFile.c_str()), QSettings::NativeFormat);
303  QStringList packages = settings.value("packages").toStringList();
304  packages.removeDuplicates();
305 
306  if (packages.empty())
307  {
309  if (application.get() != nullptr)
310  {
311  for (const auto& package : application->getArmarXPackageNames())
312  {
313  packages << QString::fromStdString(package);
314  }
315  }
316  settings.setValue("packages", packages);
317  }
318 
319  bool editModeState = settings.value("editMode", false).toBool();
320  editModeAction->setChecked(editModeState);
321  editMode(editModeState);
322 
323  settingsController.init();
324 
325  reparsePackages();
326 }
327 
328 QPointer<QWidget>
330 {
331  if (customToolbar)
332  {
333  if (parent != customToolbar->parent())
334  {
335  customToolbar->setParent(parent);
336  }
337 
338  return customToolbar.data();
339  }
340 
341  customToolbar = new QToolBar(parent);
342  customToolbar->setIconSize(QSize(16, 16));
343  customToolbar->addAction(
344  QIcon(":/icons/configure-3.png"), "Configure", &settingsController, SLOT(showSettings()));
345  QToolTip::showText(customToolbar->mapToGlobal(QPoint(0, 0)),
346  "Additional Packages can be opened in the settings");
347  customToolbar->addAction(editModeAction);
348  {
349  QAction* killallAction = new QAction("Kill All", this);
350  customToolbar->addAction(killallAction);
351  killallAction->setToolTip(
352  "Kill all armarx applications except guis on this pc (executes 'armarx killAll')");
353  connect(killallAction,
354  &QAction::triggered,
355  [] { [[maybe_unused]] auto result = std::system("armarx killAll"); });
356  }
357  return customToolbar.data();
358 }
359 
360 void
362 {
363 }
364 
365 void
367 {
368  scenarioListController.stopUpdateTask();
369 }
370 
371 void
373 {
374  QSettings settings(QString(settingsFile.c_str()), QSettings::NativeFormat);
375  QStringList packages = settings.value("packages").toStringList();
376  packages.removeDuplicates();
377 
378  QProgressDialog progress("Loading scenarios from " + QString::number(packages.size()) +
379  " packages ...",
380  "",
381  0,
382  packages.size(),
383  getWidget());
384  progress.setWindowModality(Qt::WindowModal);
385 
386  PackageBuilder parser;
387  bool didNotFindSomePackages = false;
388  for (int i = 0; i < packages.size(); i++)
389  {
390  std::string name = packages.at(i).toStdString();
391  progress.setLabelText(QString("Loading scenarios from package ") + name.c_str());
392  progress.setValue(i + 1);
393  qApp->processEvents();
394 
395  QStringList openedScenarios = settings.value("scenarios").toStringList();
396  // remove duplicates
397  openedScenarios = openedScenarios.toSet().values();
398 
399  StringList openedList;
400  for (const QString& it : openedScenarios)
401  {
402  openedList.push_back(it.toStdString());
403  }
404 
405  PackagePtr package = parser.parsePackage(name, openedList);
406  if (package == nullptr)
407  {
408  didNotFindSomePackages = true;
409  continue;
410  }
411 
412  this->packages->push_back(package);
413  }
414 
415  if (didNotFindSomePackages)
416  {
417  std::stringstream ss;
418  ss << "To remove unavailable packages, "
419  << "open the configuration (wrench icon at the top of the ScenarioManager), "
420  << "switch to the 'Cache Settings' tab, "
421  << "and press 'Close unavailable packages'. Cheers!";
422  ARMARX_INFO << ss.str();
423  }
424 
425  updateModels();
426 }
427 
428 void
430 {
431  PackageBuilder parser;
432  PackagePtr package = parser.parsePackage(name, StringList());
433 
434  this->packages->push_back(package);
435 
436  updateModels();
437 }
438 
439 void
441 {
442  settingsController.updateModel();
443  openScenarioController.updateModel();
444  scenarioListController.updateModel();
445  applicationController.updatePackages();
446  widget.detailedApplicationView->updateStatus();
447 
448  //widget.applicationDatabase->setModel(applicationController.getModel());
449  //widget.scenarioView->setModel(scenarioListController.getTreeModel());
450 }
451 
452 void
454 {
455  QSettings settings(QString(settingsFile.c_str()), QSettings::NativeFormat);
456  settings.setValue("editMode", edit);
457  widget.detailedApplicationView->setVisible(edit);
458  widget.applicationDatabase->setVisible(edit);
459 }
ScenarioItem
TreeItem representing data contained in a Scenario or an Application.
Definition: scenarioitem.h:38
ScenarioManager::Controller::SettingsController::init
void init()
Definition: SettingsController.cpp:95
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
ScenarioManager::Data_Structure::ApplicationInstancePtr
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
Definition: ApplicationInstance.h:33
ScenarioManager::Data_Structure::ScenarioPtr
std::shared_ptr< Scenario > ScenarioPtr
Definition: Scenario.h:36
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_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
ScenarioManager::Controller::ApplicationDatabaseController::updatePackages
void updatePackages()
Updates the packages displayed in the ApplicationDatabaseView by reloading all packages and applicati...
Definition: ApplicationDatabaseController.cpp:65
ScenarioManager::Data_Structure::ApplicationPtr
std::shared_ptr< Application > ApplicationPtr
Definition: Application.h:119
armarx::CMakePackageFinder::FindAllArmarXSourcePackages
static std::vector< std::string > FindAllArmarXSourcePackages()
Definition: CMakePackageFinder.cpp:438
namelocationview.h
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
armarx::ScenarioManagerWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: ScenarioManagerWidgetController.cpp:108
ScenarioManager::Controller::ScenarioListController::setIceAdmin
void setIceAdmin(IceGrid::AdminPrx iceAdmin)
Set an IceAdmin for the controller.
Definition: ScenarioListController.cpp:993
armarx::ScenarioManagerWidgetController::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ScenarioManagerWidgetController.cpp:366
armarx::PropertyUser::hasProperty
bool hasProperty(const std::string &name)
Definition: PropertyUser.cpp:243
armarx::navigation::algorithms::save
bool save(const Costmap &costmap, const std::filesystem::path &directory)
Definition: persistence.cpp:101
PackageBuilder.h
ArmarXMainWindow.h
ScenarioManager::Data_Structure::PackagePtr
std::shared_ptr< Package > PackagePtr
Definition: Package.h:121
armarx::ScenarioManagerWidgetController::reparsePackages
void reparsePackages()
Definition: ScenarioManagerWidgetController.cpp:372
armarx::ScenarioManagerWidgetController::updateModels
void updateModels()
Definition: ScenarioManagerWidgetController.cpp:440
armarx::ArmarXDataPath
Definition: ArmarXDataPath.h:57
ScenarioManager::Controller::ScenarioListController::stopUpdateTask
void stopUpdateTask()
Definition: ScenarioListController.cpp:155
FilterableTreeModelSortFilterProxyModelPtr
std::shared_ptr< FilterableTreeModelSortFilterProxyModel > FilterableTreeModelSortFilterProxyModelPtr
Definition: filterabletreemodelsortfilterproxymodel.h:65
ScenarioManager::Controller::DetailedApplicationController::setDetailedApplicationView
void setDetailedApplicationView(DetailedApplicationView *ptr)
Sets the view this controller manages.
Definition: DetailedApplicationController.cpp:64
armarx::ScenarioManagerWidgetController::ScenarioManagerWidgetController
ScenarioManagerWidgetController()
Controller Constructor.
Definition: ScenarioManagerWidgetController.cpp:47
armarx::ScenarioManagerWidgetController::getCustomTitlebarWidget
QPointer< QWidget > getCustomTitlebarWidget(QWidget *parent) override
getTitleToolbar returns a pointer to the a toolbar widget of this controller.
Definition: ScenarioManagerWidgetController.cpp:329
ScenarioManager::Controller::OpenScenarioController::updateModel
void updateModel()
Reloads the packages into the model.
Definition: openscenariocontroller.cpp:195
XMLScenarioParser.h
ScenarioManager::Controller::DetailedApplicationController::setIceAdmin
void setIceAdmin(IceGrid::AdminPrx iceAdmin)
Set an IceAdmin for the controller.
Definition: DetailedApplicationController.cpp:307
armarx::Application::getInstance
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
Definition: Application.cpp:289
armarx::ScenarioManagerWidgetController::reparsePackage
void reparsePackage(std::string name)
Definition: ScenarioManagerWidgetController.cpp:429
IceStorm::parser
Parser * parser
Definition: Parser.cpp:33
ScenarioManager
Definition: Application.cpp:166
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
std
Definition: Application.h:66
ScenarioManagerWidgetController.h
IceUtil::Handle< Application >
armarx::ScenarioManagerWidgetController::onConnectComponent
void onConnectComponent() override
Definition: ScenarioManagerWidgetController.cpp:361
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
Logging.h
armarx::ScenarioManagerWidgetController::~ScenarioManagerWidgetController
~ScenarioManagerWidgetController() override
Controller destructor.
Definition: ScenarioManagerWidgetController.cpp:67
armarx::ScenarioManagerWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: ScenarioManagerWidgetController.cpp:126
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ScenarioManager::Controller::ScenarioListController::getTreeModel
FilterableTreeModelSortFilterProxyModelPtr getTreeModel()
Returns the model used by the ScenarioListView and managed by this controller.
Definition: ScenarioListController.cpp:110
ScenarioManager::Controller::ScenarioListController::updateModel
void updateModel()
Updates the model by reloading all scenarios and applications.
Definition: ScenarioListController.cpp:667
armarx::ScenarioManagerWidgetController::editMode
void editMode(bool edit)
Definition: ScenarioManagerWidgetController.cpp:453
ScenarioManager::Parser::StringList
std::vector< std::string > StringList
Definition: PackageBuilder.h:34
armarx::ScenarioManagerWidgetController::onInitComponent
void onInitComponent() override
Definition: ScenarioManagerWidgetController.cpp:138