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