SaveSnapshotDialog.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::SceneEditor
19 * @date 2015
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "SaveSnapshotDialog.h"
25
26#include <QPushButton>
27
28#include <MemoryX/gui-plugins/SceneEditor/ui_SaveSnapshotDialog.h>
29
31 QWidget* parent) :
32 QDialog(parent), ui(new Ui::SaveSnapshotDialog), control(control)
33{
34 ui->setupUi(this);
35 connectSlots();
36 newSnapshot = false;
37 allObjects = true;
38}
39
44
45void
46gui::dialog::SaveSnapshotDialog::connectSlots()
47{
48 connect(
49 ui->newSnapshotRadioButton, SIGNAL(toggled(bool)), this, SLOT(enableSaveNewSnapshot(bool)));
50 connect(ui->replaceSnapshotRadioButton,
51 SIGNAL(toggled(bool)),
52 this,
53 SLOT(enableReplaceSnapshot(bool)));
54 connect(ui->saveAllRadioButton, SIGNAL(toggled(bool)), this, SLOT(enableSaveAllObjects(bool)));
55 connect(ui->saveGroupsRadioButton, SIGNAL(toggled(bool)), this, SLOT(enableSaveGroups(bool)));
56 connect(
57 ui->snapshotNameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkOkButtonStatus()));
58 connect(
59 ui->groupsListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(checkOkButtonStatus()));
60}
61
62void
63gui::dialog::SaveSnapshotDialog::showEvent(QShowEvent*)
64{
66 {
67 // Load Existing Snapshots
68 ui->existingSnapshotsComboBox->clear();
69 std::vector<std::string> allSnapshotsVector =
70 controller->getMemoryXController()->getAllSnapshots();
71
72 for (std::vector<std::string>::iterator it = allSnapshotsVector.begin();
73 it != allSnapshotsVector.end();
74 ++it)
75 {
76 ui->existingSnapshotsComboBox->insertItem(ui->existingSnapshotsComboBox->count(),
77 QString::fromStdString(*it));
78 }
79
80 // Load Existing Groups
81 ui->groupsListWidget->clear();
82 std::vector<scene3D::SceneGroupPtr> allGroupsVector =
83 controller->getScene()->getGroupManager()->getAllGroups();
84
85 for (std::vector<scene3D::SceneGroupPtr>::iterator it = allGroupsVector.begin();
86 it != allGroupsVector.end();
87 ++it)
88 {
89 ui->groupsListWidget->insertItem(ui->groupsListWidget->count(),
90 QString::fromStdString((*it)->getGroupId()));
91 }
92
93 checkOkButtonStatus();
94 }
95}
96
97void
98gui::dialog::SaveSnapshotDialog::accept()
99{
100 if (controller::ControllerPtr controller = control.lock())
101 {
102 QString snapshotName = newSnapshot ? ui->snapshotNameLineEdit->text()
103 : ui->existingSnapshotsComboBox->currentText();
104 std::string snapshotNameStr = snapshotName.toStdString();
105
106 if (allObjects)
107 {
108 controller->saveSnapshotAsJSON(snapshotNameStr);
109 controller->getMemoryXController()->saveSceneInSnapshot(snapshotNameStr);
110 }
111 //std::cout << "Saving all Objects as snapshot " << snapshotName.toStdString() << std::endl;
112 else
113 {
114 std::vector<std::string> allObjects;
115 QList<QListWidgetItem*> allSelectedGroups = ui->groupsListWidget->selectedItems();
116
117 for (QList<QListWidgetItem*>::iterator it = allSelectedGroups.begin();
118 it != allSelectedGroups.end();
119 ++it)
120 {
121 scene3D::SceneGroupPtr selectedGroup =
122 controller->getScene()->getGroupManager()->getGroupById(
123 (*it)->text().toStdString());
124 std::vector<scene3D::SceneObjectPtr> allObjectsInGroup =
125 selectedGroup->getAllObjects();
126
127 for (std::vector<scene3D::SceneObjectPtr>::iterator it = allObjectsInGroup.begin();
128 it != allObjectsInGroup.end();
129 ++it)
130 {
131 allObjects.insert(allObjects.end(), (*it)->getObjectId());
132 }
133 }
134
135 controller->getMemoryXController()->saveObjectsInSnapshot(snapshotNameStr, allObjects);
136 //std::cout << "Saving " << allObjects.size() << " Objects as snapshot " << snapshotName.toStdString() << std::endl;
137 }
138
139 close();
140 }
141}
142
143void
144gui::dialog::SaveSnapshotDialog::checkOkButtonStatus()
145{
146 bool inputIsValid = (!ui->snapshotNameLineEdit->text().isEmpty() || !newSnapshot) &&
147 (ui->groupsListWidget->selectedItems().count() > 0 || allObjects);
148 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(inputIsValid);
149}
150
151void
152gui::dialog::SaveSnapshotDialog::enableSaveNewSnapshot(bool enabled)
153{
154 if (enabled)
155 {
156 ui->snapshotNameLineEdit->setEnabled(enabled);
157 ui->existingSnapshotsComboBox->setDisabled(enabled);
158 newSnapshot = true;
159 checkOkButtonStatus();
160 }
161}
162
163void
164gui::dialog::SaveSnapshotDialog::enableReplaceSnapshot(bool enabled)
165{
166 if (enabled)
167 {
168 ui->existingSnapshotsComboBox->setEnabled(enabled);
169 ui->snapshotNameLineEdit->setDisabled(enabled);
170 newSnapshot = false;
171 checkOkButtonStatus();
172 }
173}
174
175void
176gui::dialog::SaveSnapshotDialog::enableSaveAllObjects(bool enabled)
177{
178 if (enabled)
179 {
180 ui->groupsListWidget->setDisabled(enabled);
181 allObjects = true;
182 checkOkButtonStatus();
183 }
184}
185
186void
187gui::dialog::SaveSnapshotDialog::enableSaveGroups(bool enabled)
188{
189 if (enabled)
190 {
191 ui->groupsListWidget->setEnabled(enabled);
192 allObjects = false;
193 checkOkButtonStatus();
194 }
195}
196
197void
199{
200 this->ui->retranslateUi(this);
201}
~SaveSnapshotDialog() override
Destructor.
void retranslate()
Translates all translatable strings in this dialog.
SaveSnapshotDialog(controller::ControllerPtr control, QWidget *parent=0)
Constructor.
ArmarX Headers.
This file is part of ArmarX.
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< SceneGroup > SceneGroupPtr