SceneEditorMainWindowController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 MemoryX::gui-plugins::SceneEditorMainWindow
19  * @author Adrian Knobloch ( adrian dot knobloch at student dot kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include <string>
26 
27 /* Qt headers */
28 #include <QComboBox>
29 #include <QDir>
30 #include <QHash>
31 #include <QString>
32 #include <QKeySequence>
33 
35 //#include <qinputcontext.h>
36 
37 /* SceneEditor plugin headers */
39 #include <MemoryX/gui-plugins/SceneEditor/gui/dialog/ui_SceneEditorConfigDialog.h>
40 #include "OverrideAction.h"
41 
43  mainWindow(NULL),
44  loadSnapshotAction(new OverrideAction(QIcon(":/icons/document-open-folder.png"), tr("Load Snapshot"), this)),
45  saveSnapshotAction(new OverrideAction(QIcon(":/icons/document-save-5.ico"), tr("Save Snapshot"), this)),
46  openShortcutDialogAction(new OverrideAction(QIcon(":/images/keys.png"), tr("Keyboard Shortcuts"), this)),
47  openGroupEditorAction(new OverrideAction(QIcon(":/images/chain-pencil.png"), tr("Group Editor"), this)),
48  clearWorkingMemoryAction(new OverrideAction(QIcon(":/icons/dialog-close.ico"), tr("Clear Scene"), this)),
49  control(controller::Controller::create()),
50  settingsPath()
51 {
52  shortcutDialog = new dialog::ShortcutDialog(control);
53  groupEditorDialog = new dialog::GroupExplorerDialog(control);
54  loadSnapshotDialog = new gui::dialog::LoadSnapshotDialog(control);
55  saveSnapshotDialog = new gui::dialog::SaveSnapshotDialog(control);
56  configDialog = NULL;
57  customToolbar = NULL;
58  workingMemoryName = "";
59  workingMemoryUpdatesTopic = "";
60  objectInstancesSegmentName = "";
61  priorMemoryName = "";
62 
63  connect(this, SIGNAL(disconnectComponent()), this, SLOT(onDisconnectComponentQt()));
64  connect(this, SIGNAL(connectComponent()), this, SLOT(onConnectComponentQt()));
65 
66  std::shared_ptr<gui::ShortcutController> shortcutController = control->getShortcutController();
67 
68  connect(loadSnapshotAction, SIGNAL(triggered()), this, SLOT(openLoadSnapshotDialog()));
69  shortcutController->registerAction("General: Load Snapshot", loadSnapshotAction);
70 
71  connect(saveSnapshotAction, SIGNAL(triggered()), this, SLOT(openSaveSnapshotDialog()));
72  shortcutController->registerAction("General: Save Snapshot", saveSnapshotAction);
73 
74  connect(openShortcutDialogAction, SIGNAL(triggered()), this, SLOT(openShortcutDialog()));
75  shortcutController->registerAction("General: Edit Shortcuts", openShortcutDialogAction);
76 
77  connect(openGroupEditorAction, SIGNAL(triggered()), this, SLOT(openGroupEditorDialog()));
78  shortcutController->registerAction("General: Edit Groups", openGroupEditorAction);
79 
80  connect(clearWorkingMemoryAction, SIGNAL(triggered()), this, SLOT(clearScene()));
81  shortcutController->registerAction("General: Clear Scene", clearWorkingMemoryAction);
82 
83  this->createLanguageMenu();
84 }
85 
87 {
88  qobject_cast<gui::SceneEditorMainWindow*>(getWidget())->postDocking();
89 }
90 
91 
93 {
94  if (!settingsPath.isEmpty())
95  {
96  QSettings settings(settingsPath, QSettings::IniFormat);
97  settings.clear();
98  QHash<QString, QKeySequence> shortcuts = control->getShortcutController()->getAllShortcuts();
99 
100  for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it)
101  {
102  settings.setValue("ShortcutController/shortcut/" + it.key(), it.value().toString());
103  }
104 
105  settings.setValue("SceneEditorMainWindowController/language", languageSelection->itemData(languageSelection->currentIndex()));
106  settings.sync();
107  }
108 }
109 
110 
112 {
113 
114 }
115 
117 {
118 
119 }
120 
121 
123 {
124  usingProxy(workingMemoryName);
125  usingProxy("LongtermMemory");
126  usingProxy(priorMemoryName);
127  usingProxy("CommonStorage");
128 }
129 
130 
132 {
133  memoryx::LongtermMemoryInterfacePrx longtermMemoryPrx = getProxy<memoryx::LongtermMemoryInterfacePrx>("LongtermMemory");
134  memoryx::WorkingMemoryInterfacePrx workingMemoryPrx = getProxy<memoryx::WorkingMemoryInterfacePrx>(workingMemoryName);
135  memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx = getProxy<memoryx::PriorKnowledgeInterfacePrx>(priorMemoryName);
136  this->control->getMemoryXController()->init(priorKnowledgePrx,
137  workingMemoryPrx,
138  workingMemoryUpdatesTopic,
139  objectInstancesSegmentName,
140  longtermMemoryPrx,
141  this->control);
142  getArmarXManager()->addObject(control->getMemoryXController()->getWorkingMemoryController());
143  control->reloadLocalScene();
144  enableMainWidgetAsync(true);
145  emit connectComponent();
146 }
147 
149 {
150  shortcutDialog->close();
151  groupEditorDialog->close();
152  loadSnapshotDialog->close();
153  saveSnapshotDialog->close();
154  enableMainWidgetAsync(false);
155  emit disconnectComponent();
156 }
157 
158 void gui::SceneEditorMainWindowController::onConnectComponentQt()
159 {
160  qobject_cast<gui::SceneEditorMainWindow*>(getWidget())->onConnect();
161  customToolbar->setEnabled(true);
162 }
163 
164 void gui::SceneEditorMainWindowController::onDisconnectComponentQt()
165 {
166  qobject_cast<gui::SceneEditorMainWindow*>(getWidget())->onDisconnect();
167  customToolbar->setDisabled(true);
168 }
169 
171 {
172  if (!mainWindow)
173  {
174  mainWindow = new SceneEditorMainWindow(control, groupEditorDialog);
175  connect(mainWindow, SIGNAL(languageChangedByUser()), this, SLOT(slotLanguageChangedByUser()));
176  }
177 
178  return qobject_cast<QWidget*>(mainWindow);
179 }
180 
182 {
183  if (customToolbar)
184  {
185  if (parent != customToolbar->parent())
186  {
187  customToolbar->setParent(parent);
188  }
189 
190  return qobject_cast<QWidget*>(customToolbar);
191  }
192 
193  customToolbar = new QToolBar(parent);
194  customToolbar->setIconSize(QSize(16, 16));
195 
196  customToolbar->addAction(loadSnapshotAction);
197  customToolbar->addAction(saveSnapshotAction);
198  customToolbar->addAction(openShortcutDialogAction);
199  customToolbar->addAction(openGroupEditorAction);
200  customToolbar->addAction(clearWorkingMemoryAction);
201  customToolbar->addWidget(languageSelection);
202  customToolbar->setDisabled(true);
203 
204  return qobject_cast<QWidget*>(customToolbar);
205 }
206 
208 {
209  if (!configDialog)
210  {
211  configDialog = new dialog::SceneEditorConfigDialog(parent);
212  configDialog->setIceObjectName();
213  }
214 
215  return qobject_cast<dialog::SceneEditorConfigDialog*>(configDialog);
216 }
217 
219 {
220  workingMemoryName = configDialog->getWorkingMemoryName();
221  workingMemoryUpdatesTopic = configDialog->getWorkingMemoryUpdatesTopic();
222  objectInstancesSegmentName = "objectInstances";
223  priorMemoryName = configDialog->getPriorMemoryName();
224 
225  settingsPath = configDialog->getSettingsFilePath();
226  QSettings settings(settingsPath, QSettings::IniFormat);
227  QHash<QString, QKeySequence> shortcuts;
228  QStringList allKeys = settings.allKeys();
229 
230  for (auto it = allKeys.begin(); it != allKeys.end(); ++it)
231  {
232  if ((*it).startsWith("ShortcutController/shortcut/", Qt::CaseSensitivity::CaseSensitive))
233  {
234  shortcuts.insert((*it).replace("ShortcutController/shortcut/", "", Qt::CaseSensitivity::CaseSensitive), QKeySequence(settings.value(*it).value<QString>()));
235  }
236  }
237 
238  QString language = settings.value("SceneEditorMainWindowController/language", QString()).value<QString>();
239 
240  for (int i = 0; i < languageSelection->count(); ++i)
241  {
242  if (languageSelection->itemData(i) == language)
243  {
244  loadLanguage(language);
245  languageSelection->setCurrentIndex(i);
246  break;
247  }
248  }
249 
250  if (shortcuts.isEmpty())
251  {
252  shortcuts.insert("General: Clear Scene", QKeySequence("Ctrl+Shift+Del"));
253  shortcuts.insert("General: Edit Groups", QKeySequence("Ctrl+G"));
254  shortcuts.insert("General: Edit Shortcuts", QKeySequence("Ctrl+K"));
255  shortcuts.insert("General: Load Snapshot", QKeySequence("Ctrl+O"));
256  shortcuts.insert("General: Save Snapshot", QKeySequence("Ctrl+S"));
257  shortcuts.insert("Group Explorer: Add Selection to new group", QKeySequence("Ctrl+Shift++"));
258  shortcuts.insert("Object Explorer: Set Focus on Search Field", QKeySequence("Ctrl+Space"));
259  shortcuts.insert("Scene: Camera view all", QKeySequence("Shift+5"));
260  shortcuts.insert("Scene: Camera view from X", QKeySequence("Shift+3"));
261  shortcuts.insert("Scene: Camera view from Y", QKeySequence("Shift+7"));
262  shortcuts.insert("Scene: Camera view from Z", QKeySequence("Shift+1"));
263  shortcuts.insert("Scene: Camera view from negative X", QKeySequence("Ctrl+Shift+3"));
264  shortcuts.insert("Scene: Camera view from negative Y", QKeySequence("Ctrl+Shift+7"));
265  shortcuts.insert("Scene: Camera view from negative Z", QKeySequence("Ctrl+Shift+1"));
266  shortcuts.insert("Scene: Copy selection", QKeySequence("Ctrl+C"));
267  shortcuts.insert("Scene: Duplicate selection", QKeySequence("Ctrl+D"));
268  shortcuts.insert("Scene: Paste selection", QKeySequence("Ctrl+V"));
269  shortcuts.insert("Scene: Redo last Action", QKeySequence("Ctrl+Shift+Z"));
270  shortcuts.insert("Scene: Remove the selected Objects", QKeySequence("Del"));
271  shortcuts.insert("Scene: Select all or deselect all", QKeySequence("Ctrl+A"));
272  shortcuts.insert("Scene: Switch between Editor and Viewer Mode", QKeySequence("Z"));
273  shortcuts.insert("Scene: Switch to Editor Mode", QKeySequence("Shift+E"));
274  shortcuts.insert("Scene: Switch to Viewer Mode", QKeySequence("Shift+V"));
275  shortcuts.insert("Scene: Toggle Collision Meshes", QKeySequence("Shift+C"));
276  shortcuts.insert("Scene: Toggle Rotation Manipulator", QKeySequence("Shift+R"));
277  shortcuts.insert("Scene: Toggle Translation Manipulator", QKeySequence("Shift+T"));
278  shortcuts.insert("Scene: Undo last Action", QKeySequence("Ctrl+Z"));
279  shortcuts.insert("Scene: Reset rotation", QKeySequence("Alt+R"));
280  shortcuts.insert("Scene: Reset translation", QKeySequence("Alt+T"));
281  }
282 
283  for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it)
284  {
285  control->getShortcutController()->updateShortcut(it.key(), it.value());
286  }
287 
288  configDialog->getArmarXManager()->removeObjectBlocking(configDialog->getName());
289 }
290 
291 void gui::SceneEditorMainWindowController::clearScene()
292 {
293  control->clearScene();
294 }
295 
296 void gui::SceneEditorMainWindowController::openLoadSnapshotDialog()
297 {
298  loadSnapshotDialog->exec();
299 }
300 
301 void gui::SceneEditorMainWindowController::openSaveSnapshotDialog()
302 {
303  saveSnapshotDialog->exec();
304 }
305 
306 void gui::SceneEditorMainWindowController::openShortcutDialog()
307 {
308  shortcutDialog->exec();
309 }
310 
311 void gui::SceneEditorMainWindowController::openGroupEditorDialog()
312 {
313  groupEditorDialog->exec();
314 }
315 
316 void gui::SceneEditorMainWindowController::createLanguageMenu()
317 {
318  languageSelection = new QComboBox();
319 
320  connect(languageSelection, SIGNAL(currentIndexChanged(int)), this, SLOT(slotLanguageChanged(int)));
321 
322  QDir dir(":/languages/");
323  QStringList fileNames = dir.entryList(QStringList("SceneEditorLanguages_*.qm"));
324 
325  for (int i = 0; i < fileNames.size(); ++i)
326  {
327  // get locale extracted by filename
328  QString language;
329  language = fileNames[i]; // "SceneEditorLanguages_de.qm"
330  language.truncate(language.lastIndexOf('.')); // "SceneEditorLanguages_de"
331  language.remove(0, language.indexOf('_') + 1); // "de"
332 
333  QString languageName = QLocale(language).nativeLanguageName();
334 
335  languageSelection->addItem(languageName, language);
336 
337  // Gets the local language and checks whether the added language is the same. If so it is set as the active language.
338  QString defaultLocale = QLocale::system().name(); // e.g. "de_DE"
339  defaultLocale.truncate(defaultLocale.lastIndexOf('_')); // e.g. "de"
340 
341  if (defaultLocale == language)
342  {
343  languageSelection->setCurrentIndex(languageSelection->count() - 1);
344  }
345  }
346 }
347 
348 void gui::SceneEditorMainWindowController::switchTranslator(QTranslator& translator, const QString& filename, const QString& directory)
349 {
350  // remove the old translator
351  qApp->removeTranslator(&translator);
352 
353  // load the new translator
354  if (translator.load(filename, directory))
355  {
356  qApp->installTranslator(&translator);
357  }
358 }
359 
360 void gui::SceneEditorMainWindowController::loadLanguage(const QString& rLanguage)
361 {
362  if (m_currLang != rLanguage)
363  {
364  m_currLang = rLanguage;
365  QString languageFilePath = QString::fromStdString(":/languages/");
366 
367  switchTranslator(m_translator, QString("SceneEditorLanguages_%1.qm").arg(rLanguage), languageFilePath);
368  // switchTranslator(m_translatorQt, QString("qt_%1.qm").arg(rLanguage), languageFilePath);
369  }
370 }
371 
372 // Called every time, when a menu entry of the language menu is called
373 void gui::SceneEditorMainWindowController::slotLanguageChanged(const int languageIndex)
374 {
375  if (languageIndex != -1)
376  {
377  QString language = this->languageSelection->itemData(languageIndex).toString();
378  this->loadLanguage(language);
379  }
380 }
381 
382 // Called every time, when the main window gets a language change event
383 void gui::SceneEditorMainWindowController::slotLanguageChangedByUser()
384 {
385  this->saveSnapshotDialog->retranslate();
386  this->loadSnapshotDialog->retranslate();
387  this->groupEditorDialog->retranslate();
388  this->shortcutDialog->retranslate();
389 
390  this->openGroupEditorAction->setText(tr("Group Editor"));
391  this->openShortcutDialogAction->setText(tr("Keyboard Shortcuts"));
392  this->saveSnapshotAction->setText(tr("Save Snapshot"));
393  this->loadSnapshotAction->setText(tr("Load Snapshot"));
394  this->clearWorkingMemoryAction->setText(tr("Clear Scene"));
395 }
gui::SceneEditorMainWindowController::disconnectComponent
void disconnectComponent()
gui::dialog::LoadSnapshotDialog
Definition: LoadSnapshotDialog.h:37
SceneEditorMainWindowController.h
gui::OverrideAction
Definition: OverrideAction.h:31
gui::SceneEditorMainWindowController::loadSettings
void loadSettings(QSettings *settings) override
Reimplemented ArmarXWidgetController:loadSettings().
Definition: SceneEditorMainWindowController.cpp:111
ArmarXManager.h
gui::dialog::SaveSnapshotDialog
Definition: SaveSnapshotDialog.h:37
gui::dialog::GroupExplorerDialog
Definition: GroupExplorerDialog.h:43
gui::SceneEditorMainWindowController::SceneEditorMainWindowController
SceneEditorMainWindowController()
Constructor.
Definition: SceneEditorMainWindowController.cpp:42
gui::dialog::ShortcutDialog
Definition: ShortcutDialog.h:41
gui::SceneEditorMainWindowController::getCustomTitlebarWidget
QPointer< QWidget > getCustomTitlebarWidget(QWidget *parent) override
Reimplemented ArmarXWidgetController:getCustomTitlebarWidget().
Definition: SceneEditorMainWindowController.cpp:181
gui::SceneEditorMainWindowController::configured
void configured() override
Reimplemented ArmarXWidgetController:configured().
Definition: SceneEditorMainWindowController.cpp:218
gui::SceneEditorMainWindowController::~SceneEditorMainWindowController
~SceneEditorMainWindowController() override
Destructor.
Definition: SceneEditorMainWindowController.cpp:92
gui::SceneEditorMainWindowController::connectComponent
void connectComponent()
gui::dialog::SceneEditorConfigDialog
This class provides a dialog derived from Qt::QDialog and armarx::ManagedIceObject.
Definition: SceneEditorConfigDialog.h:50
gui::SceneEditorMainWindowController::onDisconnectComponent
void onDisconnectComponent() override
Definition: SceneEditorMainWindowController.cpp:148
controller
Definition: AddOperation.h:39
gui::SceneEditorMainWindowController::getConfigDialog
QPointer< QDialog > getConfigDialog(QWidget *parent) override
Reimplemented ArmarXWidgetController:getConfigDialog().
Definition: SceneEditorMainWindowController.cpp:207
OverrideAction.h
gui::SceneEditorMainWindowController::getWidget
QPointer< QWidget > getWidget() override
getWidget returns a pointer to the a widget of this controller.
Definition: SceneEditorMainWindowController.cpp:170
filename
std::string filename
Definition: VisualizationRobot.cpp:83
gui::SceneEditorMainWindowController::saveSettings
void saveSettings(QSettings *settings) override
Reimplemented ArmarXWidgetController:saveSettings().
Definition: SceneEditorMainWindowController.cpp:116
gui::SceneEditorMainWindowController::postDocking
void postDocking() override
postDocking is called after the widget has been docked into the main window.
Definition: SceneEditorMainWindowController.cpp:86
gui::SceneEditorMainWindowController::onInitComponent
void onInitComponent() override
Reimplemented armarx::Component:onInitComponent().
Definition: SceneEditorMainWindowController.cpp:122
gui::SceneEditorMainWindowController::onConnectComponent
void onConnectComponent() override
Reimplementiert armarx::Component:onConnectComponent().
Definition: SceneEditorMainWindowController.cpp:131
gui::SceneEditorMainWindow
This class provides the main window of the plugin.
Definition: SceneEditorMainWindow.h:53
control
This file is part of ArmarX.