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 
26 #include <QInputDialog>
27 #include <QList>
28 #include <QStandardItem>
29 
30 #include "../../controller/CreateGroupOperation.h"
31 #include "../../controller/DeleteGroupOperation.h"
32 #include "../../controller/RemoveFromGroupOperation.h"
33 #include "../../controller/RenameGroupOperation.h"
34 #include "../../scene3D/SceneGroupManager.h"
35 #include <Inventor/SbRotation.h>
36 #include <Inventor/nodes/SoCone.h>
37 #include <Inventor/nodes/SoSeparator.h>
38 #include <MemoryX/gui-plugins/SceneEditor/ui_GroupExplorerDialog.h>
39 
41  QWidget* parent) :
42  QDialog(parent), control(control), 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(
54  ui->groupListWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(groupClicked(QModelIndex)));
55  connect(
56  ui->objectListWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(objectClicked(QModelIndex)));
57  connect(ui->objectListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(checkButtons()));
58  connect(ui->addGroupButton, SIGNAL(clicked()), this, SLOT(groupAddClicked()));
59  connect(ui->deleteGroupButton, SIGNAL(clicked()), this, SLOT(groupRemoveClicked()));
60  connect(ui->changeNameButton, SIGNAL(clicked()), this, SLOT(groupRenameClicked()));
61  connect(ui->deleteObjectButton, SIGNAL(clicked()), this, SLOT(objectRemoveClicked()));
62  connect(ui->groupNameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkButtons()));
63 }
64 
66 {
67  delete ui;
68 }
69 
70 // Triggered when opened
71 void
72 gui::dialog::GroupExplorerDialog::showEvent(QShowEvent* event)
73 {
74  reloadGroups();
75  reloadObjects();
76  reloadProperties();
77  checkButtons();
78 }
79 
80 void
81 gui::dialog::GroupExplorerDialog::groupClicked(QModelIndex index)
82 {
83  if (ui->groupListWidget->currentItem()->text() != activeGroup)
84  {
85  activeObject = "";
86  }
87 
88  activeGroup = ui->groupListWidget->currentItem()->text();
89 
90  reloadObjects();
91  reloadProperties();
92  checkButtons();
93 }
94 
95 void
96 gui::dialog::GroupExplorerDialog::objectClicked(QModelIndex index)
97 {
98  activeObject = ui->objectListWidget->currentItem()->toolTip();
99 
100  reloadProperties();
101  checkButtons();
102 }
103 
104 void
105 gui::dialog::GroupExplorerDialog::groupAddClicked()
106 {
108  {
109  int nameIndex = 0;
110 
111  do
112  {
113  nameIndex++;
114  } while (controller->getScene()->getGroupManager()->getGroupById(
115  tr("New Group ").toStdString() + std::to_string(nameIndex)) != NULL);
116 
117  std::string groupName = tr("New Group ").toStdString() + std::to_string(nameIndex);
118 
119  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
120  new std::vector<controller::OperationPtr>());
121 
123  controller->getMemoryXController(), controller->getScene(), groupName));
124  operations->push_back(operation);
127  operations);
128 
129  activeGroup = QString::fromStdString(groupName);
130  activeObject = "";
131 
132  reloadGroups();
133  reloadObjects();
134  reloadProperties();
135  checkButtons();
136 
137  ui->groupNameLineEdit->setFocus();
138  ui->groupNameLineEdit->selectAll();
139  ui->changeNameButton->setDefault(true);
140  }
141 }
142 
143 void
144 gui::dialog::GroupExplorerDialog::groupRemoveClicked()
145 {
147  {
148  if (!controller->getScene()->getGroupManager()->getGroupById(activeGroup.toStdString()))
149  {
150  return;
151  }
152 
153  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
154  new std::vector<controller::OperationPtr>());
155 
156  for (scene3D::SceneObjectPtr object : controller->getScene()
157  ->getGroupManager()
158  ->getGroupById(activeGroup.toStdString())
159  ->getAllObjects())
160  {
161  controller::OperationPtr operation(
162  new controller::RemoveFromGroupOperation(controller->getMemoryXController(),
163  controller->getScene(),
164  activeGroup.toStdString(),
165  object->getObjectId()));
166  operations->push_back(operation);
167  }
168 
170  controller->getMemoryXController(), controller->getScene(), activeGroup.toStdString()));
171  operations->push_back(operation);
174  operations);
175 
176  activeGroup = "";
177  activeObject = "";
178 
179  reloadGroups();
180  reloadObjects();
181  reloadProperties();
182  checkButtons();
183  }
184 }
185 
186 void
187 gui::dialog::GroupExplorerDialog::objectRemoveClicked()
188 {
190  {
191  QList<QListWidgetItem*> allItemsToDelete = ui->objectListWidget->selectedItems();
192  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
193  new std::vector<controller::OperationPtr>());
194 
195  for (QList<QListWidgetItem*>::iterator it = allItemsToDelete.begin();
196  it != allItemsToDelete.end();
197  ++it)
198  {
199  controller::OperationPtr operation(
200  new controller::RemoveFromGroupOperation(controller->getMemoryXController(),
201  controller->getScene(),
202  activeGroup.toStdString(),
203  (*it)->toolTip().toStdString()));
204  operations->push_back(operation);
205  }
206 
209  operations);
210 
211  activeObject = "";
212 
213  reloadObjects();
214  reloadProperties();
215  checkButtons();
216  }
217 }
218 
219 void
220 gui::dialog::GroupExplorerDialog::groupRenameClicked()
221 {
223  {
224  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
225  new std::vector<controller::OperationPtr>());
226 
227  controller::OperationPtr operation(
228  new controller::RenameGroupOperation(controller->getMemoryXController(),
229  controller->getScene(),
230  activeGroup.toStdString(),
231  ui->groupNameLineEdit->text().toStdString()));
232  operations->push_back(operation);
235  operations);
236 
237  activeGroup = ui->groupNameLineEdit->text();
238 
239  reloadGroups();
240  checkButtons();
241  }
242 }
243 
244 void
246 {
248  {
249  if (controller->getScene()->getGroupManager()->getGroupById(groupId.toStdString()))
250  {
251  activeGroup = groupId;
252  }
253 
254  reloadGroups();
255  reloadObjects();
256  reloadProperties();
257  checkButtons();
258 
259  // To force active highlightening in group list
260  ui->groupListWidget->setFocus();
261  }
262 }
263 
264 void
265 gui::dialog::GroupExplorerDialog::reloadGroups()
266 {
268  {
269  // Reload Groups
270  ui->groupListWidget->clear();
271  allGroups = controller->getScene()->getGroupManager()->getAllGroups();
272 
273  for (std::vector<scene3D::SceneGroupPtr>::iterator it = allGroups.begin();
274  it != allGroups.end();
275  ++it)
276  {
277  ui->groupListWidget->addItem(QString::fromStdString((*it)->getGroupId()));
278  }
279 
280  if (!activeGroup.isNull() && !activeGroup.isEmpty())
281  {
282  // Set current group
283  ui->groupListWidget->setCurrentItem(
284  ui->groupListWidget->findItems(activeGroup, Qt::MatchFixedString).first());
285  }
286  }
287 }
288 
289 void
290 gui::dialog::GroupExplorerDialog::reloadObjects()
291 {
293  {
294  // Reload Objects
295  ui->objectListWidget->clear();
296  scene3D::SceneGroupPtr clickedGroup =
297  controller->getScene()->getGroupManager()->getGroupById(activeGroup.toStdString());
298 
299  if (clickedGroup != NULL)
300  {
301  std::vector<scene3D::SceneObjectPtr> allObjects = clickedGroup->getAllObjects();
302 
303  for (std::vector<scene3D::SceneObjectPtr>::iterator it = allObjects.begin();
304  it != allObjects.end();
305  ++it)
306  {
307  std::string objectId = (*it)->getObjectId();
308  std::string classId = (*it)->getClassId();
309  QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(classId));
310  item->setToolTip(QString::fromStdString(objectId));
311  ui->objectListWidget->insertItem(ui->objectListWidget->count(), item);
312  }
313  }
314 
315  ui->groupNameLineEdit->setText(activeGroup);
316  }
317 }
318 
319 void
320 gui::dialog::GroupExplorerDialog::reloadProperties()
321 {
323  {
324  // Reload Object Properties
325  ui->propertiesTableWidget->clear();
326  ui->propertiesTableWidget->reset();
327  viewer->setSceneGraph(new SoSeparator());
328  scene3D::SceneObjectPtr sceneObject =
329  controller->getScene()->getObjectManager()->getObjectById(activeObject.toStdString());
330 
331  if (sceneObject)
332  {
333  std::map<std::string, std::string> attributes = sceneObject->getAllAttributes();
334  // Basic Properties
335  ui->propertiesTableWidget->setRowCount(3);
336  ui->propertiesTableWidget->setItem(0, 0, new QTableWidgetItem(tr("Object ID")));
337  ui->propertiesTableWidget->setItem(
338  0, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getObjectId())));
339  ui->propertiesTableWidget->setItem(1, 0, new QTableWidgetItem(tr("Class ID")));
340  ui->propertiesTableWidget->setItem(
341  1, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getClassId())));
342  ui->propertiesTableWidget->setItem(2, 0, new QTableWidgetItem(tr("Collection ID")));
343  ui->propertiesTableWidget->setItem(
344  2, 1, new QTableWidgetItem(QString::fromStdString(sceneObject->getCollection())));
345 
346  // All other generic properties
347  int row;
348 
349  for (std::map<std::string, std::string>::iterator it = attributes.begin();
350  it != attributes.end();
351  ++it)
352  {
353  row = ui->propertiesTableWidget->rowCount();
354  QTableWidgetItem* key = new QTableWidgetItem(QString::fromStdString(it->first));
355  QTableWidgetItem* value = new QTableWidgetItem(QString::fromStdString(it->second));
356  ui->propertiesTableWidget->insertRow(row);
357  ui->propertiesTableWidget->setItem(row, 0, key);
358  ui->propertiesTableWidget->setItem(row, 1, value);
359  ui->propertiesTableWidget->resizeRowToContents(row);
360  }
361 
362  ui->propertiesTableWidget->resizeColumnToContents(1);
363 
364  showPreviewImage(
365  controller->getMemoryXController()
366  ->getPriorKnowlegdeController()
367  ->getObjectClassPtr(sceneObject->getClassId(), sceneObject->getCollection()));
368  }
369  }
370 }
371 
372 void
373 gui::dialog::GroupExplorerDialog::checkButtons()
374 {
376  {
377  ui->changeNameButton->setEnabled(
378  !(ui->groupNameLineEdit->text().isEmpty() ||
379  ui->groupNameLineEdit->text() == activeGroup ||
380  controller->getScene()->getGroupManager()->getGroupById(
381  ui->groupNameLineEdit->text().toStdString()) != NULL));
382  ui->deleteGroupButton->setEnabled(activeGroup != "");
383  ui->deleteObjectButton->setEnabled(activeObject != "");
384  ui->groupNameLineEdit->setEnabled(activeGroup != "");
385  }
386 }
387 
388 void
389 gui::dialog::GroupExplorerDialog::showPreviewImage(const memoryx::ObjectClassPtr& objectClass)
390 {
392  {
393  //Get geometry for object
394  SoNode* visualisation =
395  controller->getMemoryXController()->getPriorKnowlegdeController()->getCoinVisualisation(
396  objectClass, false);
397 
398  //Create basic scene with rotation around Y
399  SoSeparator* root =
400  controller->getScene()->getPreviewGenerator()->createAnimatedPreview(visualisation);
401 
402  viewer->setSceneGraph(root);
403  viewer->getCamera()->viewAll(root, viewer->getViewportRegion());
404  }
405 }
406 
407 void
409 {
410  this->ui->retranslateUi(this);
411 }
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:408
GroupExplorerDialog.h
gui::dialog::GroupExplorerDialog
Definition: GroupExplorerDialog.h:43
gui::dialog::GroupExplorerDialog::~GroupExplorerDialog
~GroupExplorerDialog() override
Destructor.
Definition: GroupExplorerDialog.cpp:65
IceInternal::Handle< ObjectClass >
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
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:54
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:55
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
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:245
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:40
control
This file is part of ArmarX.