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