GroupExplorerDialog.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 "GroupExplorerDialog.h"
25 #include <MemoryX/gui-plugins/SceneEditor/ui_GroupExplorerDialog.h>
26 #include "../../scene3D/SceneGroupManager.h"
27 #include <QStandardItem>
28 #include <QList>
29 #include <Inventor/nodes/SoSeparator.h>
30 #include <Inventor/nodes/SoCone.h>
31 #include <Inventor/SbRotation.h>
32 #include <QInputDialog>
33 
34 #include "../../controller/CreateGroupOperation.h"
35 #include "../../controller/DeleteGroupOperation.h"
36 #include "../../controller/RenameGroupOperation.h"
37 #include "../../controller/RemoveFromGroupOperation.h"
38 
40  QDialog(parent),
42  ui(new Ui::GroupExplorerDialog)
43 {
44  ui->setupUi(this);
45 
46  viewer.reset(new SoQtExaminerViewer(ui->viewInSceneWidget));
47  viewer->setDecoration(false);
48  viewer->setHeadlight(true);
49  viewer->setBackgroundColor(SbColor(100 / 255.0f, 100 / 255.0f, 100 / 255.0f));
50  viewer->setSceneGraph(new SoSeparator());
51 
52  connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(close()));
53  connect(ui->groupListWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(groupClicked(QModelIndex)));
54  connect(ui->objectListWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(objectClicked(QModelIndex)));
55  connect(ui->objectListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(checkButtons()));
56  connect(ui->addGroupButton, SIGNAL(clicked()), this, SLOT(groupAddClicked()));
57  connect(ui->deleteGroupButton, SIGNAL(clicked()), this, SLOT(groupRemoveClicked()));
58  connect(ui->changeNameButton, SIGNAL(clicked()), this, SLOT(groupRenameClicked()));
59  connect(ui->deleteObjectButton, SIGNAL(clicked()), this, SLOT(objectRemoveClicked()));
60  connect(ui->groupNameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkButtons()));
61 }
62 
64 {
65  delete ui;
66 }
67 
68 // Triggered when opened
69 void gui::dialog::GroupExplorerDialog::showEvent(QShowEvent* event)
70 {
71  reloadGroups();
72  reloadObjects();
73  reloadProperties();
74  checkButtons();
75 }
76 
77 void gui::dialog::GroupExplorerDialog::groupClicked(QModelIndex index)
78 {
79  if (ui->groupListWidget->currentItem()->text() != activeGroup)
80  {
81  activeObject = "";
82  }
83 
84  activeGroup = ui->groupListWidget->currentItem()->text();
85 
86  reloadObjects();
87  reloadProperties();
88  checkButtons();
89 }
90 
91 void gui::dialog::GroupExplorerDialog::objectClicked(QModelIndex index)
92 {
93  activeObject = ui->objectListWidget->currentItem()->toolTip();
94 
95  reloadProperties();
96  checkButtons();
97 }
98 
99 void gui::dialog::GroupExplorerDialog::groupAddClicked()
100 {
102  {
103  int nameIndex = 0;
104 
105  do
106  {
107  nameIndex++;
108  }
109  while (controller->getScene()->getGroupManager()->getGroupById(tr("New Group ").toStdString() + std::to_string(nameIndex)) != NULL);
110 
111  std::string groupName = tr("New Group ").toStdString() + std::to_string(nameIndex);
112 
113  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
114 
115  controller::OperationPtr operation(new controller::CreateGroupOperation(controller->getMemoryXController(),
116  controller->getScene(),
117  groupName));
118  operations->push_back(operation);
120 
121  activeGroup = QString::fromStdString(groupName);
122  activeObject = "";
123 
124  reloadGroups();
125  reloadObjects();
126  reloadProperties();
127  checkButtons();
128 
129  ui->groupNameLineEdit->setFocus();
130  ui->groupNameLineEdit->selectAll();
131  ui->changeNameButton->setDefault(true);
132  }
133 }
134 
135 void gui::dialog::GroupExplorerDialog::groupRemoveClicked()
136 {
138  {
139  if (!controller->getScene()->getGroupManager()->getGroupById(activeGroup.toStdString()))
140  {
141  return;
142  }
143 
144  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
145 
146  for (scene3D::SceneObjectPtr object : controller->getScene()->getGroupManager()->getGroupById(activeGroup.toStdString())->getAllObjects())
147  {
148  controller::OperationPtr operation(new controller::RemoveFromGroupOperation(controller->getMemoryXController(),
149  controller->getScene(),
150  activeGroup.toStdString(),
151  object->getObjectId()));
152  operations->push_back(operation);
153  }
154 
155  controller::OperationPtr operation(new controller::DeleteGroupOperation(controller->getMemoryXController(),
156  controller->getScene(),
157  activeGroup.toStdString()));
158  operations->push_back(operation);
160 
161  activeGroup = "";
162  activeObject = "";
163 
164  reloadGroups();
165  reloadObjects();
166  reloadProperties();
167  checkButtons();
168  }
169 }
170 
171 void gui::dialog::GroupExplorerDialog::objectRemoveClicked()
172 {
174  {
175  QList<QListWidgetItem*> allItemsToDelete = ui->objectListWidget->selectedItems();
176  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
177 
178  for (QList<QListWidgetItem*>::iterator it = allItemsToDelete.begin(); it != allItemsToDelete.end(); ++it)
179  {
180  controller::OperationPtr operation(new controller::RemoveFromGroupOperation(controller->getMemoryXController(),
181  controller->getScene(),
182  activeGroup.toStdString(),
183  (*it)->toolTip().toStdString()));
184  operations->push_back(operation);
185  }
186 
188 
189  activeObject = "";
190 
191  reloadObjects();
192  reloadProperties();
193  checkButtons();
194  }
195 }
196 
197 void gui::dialog::GroupExplorerDialog::groupRenameClicked()
198 {
200  {
201  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
202 
203  controller::OperationPtr operation(new controller::RenameGroupOperation(controller->getMemoryXController(),
204  controller->getScene(),
205  activeGroup.toStdString(),
206  ui->groupNameLineEdit->text().toStdString()));
207  operations->push_back(operation);
209 
210  activeGroup = ui->groupNameLineEdit->text();
211 
212  reloadGroups();
213  checkButtons();
214  }
215 }
216 
218 {
220  {
221  if (controller->getScene()->getGroupManager()->getGroupById(groupId.toStdString()))
222  {
223  activeGroup = groupId;
224  }
225 
226  reloadGroups();
227  reloadObjects();
228  reloadProperties();
229  checkButtons();
230 
231  // To force active highlightening in group list
232  ui->groupListWidget->setFocus();
233  }
234 }
235 
236 void gui::dialog::GroupExplorerDialog::reloadGroups()
237 {
239  {
240  // Reload Groups
241  ui->groupListWidget->clear();
242  allGroups = controller->getScene()->getGroupManager()->getAllGroups();
243 
244  for (std::vector<scene3D::SceneGroupPtr>::iterator it = allGroups.begin(); it != allGroups.end(); ++it)
245  {
246  ui->groupListWidget->addItem(QString::fromStdString((*it)->getGroupId()));
247  }
248 
249  if (!activeGroup.isNull() && !activeGroup.isEmpty())
250  {
251  // Set current group
252  ui->groupListWidget->setCurrentItem(ui->groupListWidget->findItems(activeGroup, Qt::MatchFixedString).first());
253  }
254  }
255 }
256 
257 void gui::dialog::GroupExplorerDialog::reloadObjects()
258 {
260  {
261  // Reload Objects
262  ui->objectListWidget->clear();
263  scene3D::SceneGroupPtr clickedGroup = controller->getScene()->getGroupManager()->getGroupById(activeGroup.toStdString());
264 
265  if (clickedGroup != NULL)
266  {
267  std::vector<scene3D::SceneObjectPtr> allObjects = clickedGroup->getAllObjects();
268 
269  for (std::vector<scene3D::SceneObjectPtr>::iterator it = allObjects.begin(); it != allObjects.end(); ++it)
270  {
271  std::string objectId = (*it)->getObjectId();
272  std::string classId = (*it)->getClassId();
273  QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(classId));
274  item->setToolTip(QString::fromStdString(objectId));
275  ui->objectListWidget->insertItem(ui->objectListWidget->count(), item);
276  }
277  }
278 
279  ui->groupNameLineEdit->setText(activeGroup);
280  }
281 }
282 
283 void gui::dialog::GroupExplorerDialog::reloadProperties()
284 {
286  {
287  // Reload Object Properties
288  ui->propertiesTableWidget->clear();
289  ui->propertiesTableWidget->reset();
290  viewer->setSceneGraph(new SoSeparator());
291  scene3D::SceneObjectPtr sceneObject = controller->getScene()->getObjectManager()->getObjectById(activeObject.toStdString());
292 
293  if (sceneObject)
294  {
295  std::map<std::string, std::string> attributes = sceneObject->getAllAttributes();
296  // Basic Properties
297  ui->propertiesTableWidget->setRowCount(3);
298  ui->propertiesTableWidget->setItem(0, 0, new QTableWidgetItem(tr("Object ID")));
299  ui->propertiesTableWidget->setItem(0, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getObjectId())));
300  ui->propertiesTableWidget->setItem(1, 0, new QTableWidgetItem(tr("Class ID")));
301  ui->propertiesTableWidget->setItem(1, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getClassId())));
302  ui->propertiesTableWidget->setItem(2, 0, new QTableWidgetItem(tr("Collection ID")));
303  ui->propertiesTableWidget->setItem(2, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getCollection())));
304 
305  // All other generic properties
306  int row;
307 
308  for (std::map<std::string, std::string>::iterator it = attributes.begin(); it != attributes.end(); ++it)
309  {
310  row = ui->propertiesTableWidget->rowCount();
311  QTableWidgetItem* key = new QTableWidgetItem(QString::fromStdString(it->first));
312  QTableWidgetItem* value = new QTableWidgetItem(QString::fromStdString(it->second));
313  ui->propertiesTableWidget->insertRow(row);
314  ui->propertiesTableWidget->setItem(row, 0, key);
315  ui->propertiesTableWidget->setItem(row, 1, value);
316  ui->propertiesTableWidget->resizeRowToContents(row);
317  }
318 
319  ui->propertiesTableWidget->resizeColumnToContents(1);
320 
321  showPreviewImage(controller->getMemoryXController()->getPriorKnowlegdeController()->getObjectClassPtr(sceneObject->getClassId(), sceneObject->getCollection()));
322  }
323  }
324 }
325 
326 void gui::dialog::GroupExplorerDialog::checkButtons()
327 {
329  {
330  ui->changeNameButton->setEnabled(!(
331  ui->groupNameLineEdit->text().isEmpty() ||
332  ui->groupNameLineEdit->text() == activeGroup ||
333  controller->getScene()->getGroupManager()->getGroupById(ui->groupNameLineEdit->text().toStdString()) != NULL));
334  ui->deleteGroupButton->setEnabled(activeGroup != "");
335  ui->deleteObjectButton->setEnabled(activeObject != "");
336  ui->groupNameLineEdit->setEnabled(activeGroup != "");
337  }
338 }
339 
340 void gui::dialog::GroupExplorerDialog::showPreviewImage(const memoryx::ObjectClassPtr& objectClass)
341 {
343  {
344  //Get geometry for object
345  SoNode* visualisation = controller->getMemoryXController()->getPriorKnowlegdeController()->getCoinVisualisation(objectClass, false);
346 
347  //Create basic scene with rotation around Y
348  SoSeparator* root = controller->getScene()->getPreviewGenerator()->createAnimatedPreview(visualisation);
349 
350  viewer->setSceneGraph(root);
351  viewer->getCamera()->viewAll(root, viewer->getViewportRegion());
352  }
353 }
354 
356 {
357  this->ui->retranslateUi(this);
358 }
index
uint8_t index
Definition: EtherCATFrame.h:59
controller::Controller::EXECUTE_ON_SCENE
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition: Controller.h:73
gui::dialog::GroupExplorerDialog::retranslate
void retranslate()
Translates all translatable strings in this dialog.
Definition: GroupExplorerDialog.cpp:355
GroupExplorerDialog.h
gui::dialog::GroupExplorerDialog
Definition: GroupExplorerDialog.h:43
gui::dialog::GroupExplorerDialog::~GroupExplorerDialog
~GroupExplorerDialog() override
Destructor.
Definition: GroupExplorerDialog.cpp:63
IceInternal::Handle< ObjectClass >
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
controller::Controller::UNDOABLE
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition: Controller.h:80
controller
Definition: AddOperation.h:39
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:58
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
controller::RemoveFromGroupOperation
A operation to removes a object from a existing group.
Definition: RemoveFromGroupOperation.h:41
controller::RenameGroupOperation
A operation to rename a group.
Definition: RenameGroupOperation.h:41
scene3D::SceneGroupPtr
std::shared_ptr< SceneGroup > SceneGroupPtr
Definition: PointerDefinitions.h:53
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
controller::CreateGroupOperation
A operation to create a new group.
Definition: CreateGroupOperation.h:41
controller::DeleteGroupOperation
A operation to delete a group.
Definition: DeleteGroupOperation.h:41
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
gui::dialog::GroupExplorerDialog::setCurrentGroup
void setCurrentGroup(QString groupId)
Sets the currently selected Group.
Definition: GroupExplorerDialog.cpp:217
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
gui::dialog::GroupExplorerDialog::GroupExplorerDialog
GroupExplorerDialog(controller::ControllerPtr control, QWidget *parent=0)
Constructor.
Definition: GroupExplorerDialog.cpp:39
control
This file is part of ArmarX.