GroupExplorerWidget.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 /* Qt headers */
25 #include "GroupExplorerWidget.h"
26 
27 #include <QHeaderView>
28 #include <QMenu>
29 #include <QModelIndex>
30 #include <QStringList>
31 #include <QTableWidgetItem>
32 
33 #include "../controller/AddToGroupOperation.h"
34 #include "../controller/Controller.h"
35 #include "../controller/CreateGroupOperation.h"
36 #include "../controller/DeleteGroupOperation.h"
37 #include "../controller/RemoveFromGroupOperation.h"
38 #include "../controller/SelectOperation.h"
39 #include "../scene3D/SceneObject.h"
40 #include "OverrideAction.h"
41 #include "ShortcutController.h"
42 #include <MemoryX/gui-plugins/SceneEditor/ui_GroupExplorerWidget.h>
43 
46  QPointer<dialog::GroupExplorerDialog> groupEditorDialog,
47  QWidget* parent) :
48  QWidget(parent),
49  ui(new Ui::GroupExplorerWidget),
51  groupEditorDialog(groupEditorDialog)
52 {
53  ui->setupUi(this);
54  ui->newGroupButton->setIcon(QIcon(QString::fromStdString(":/icons/edit-add.ico")));
55 
56  // Register shortcuts
57  std::shared_ptr<gui::ShortcutController> shortcutController = control->getShortcutController();
58  QPointer<QAction> addToNewGroup(
59  new OverrideAction("Group Explorer: Add Selection to new group", this));
60  this->addAction(addToNewGroup);
61  shortcutController->registerAction(addToNewGroup->text(), addToNewGroup);
62 
63  setMinimumWidth(200);
64 
65  // Connections to add selection to new group
66  connect(ui->newGroupButton, SIGNAL(clicked()), this, SLOT(addSelectionToNewGroup()));
67  connect(addToNewGroup, SIGNAL(triggered()), this, SLOT(addSelectionToNewGroup()));
68 
69  // Connections for context menu
70  connect(ui->groupsTableWidget,
71  SIGNAL(customContextMenuRequested(QPoint)),
72  this,
73  SLOT(customContextMenuRequested(QPoint)));
74 
75  // Connections to open group editor
76  connect(ui->groupsTableWidget,
77  SIGNAL(doubleClicked(QModelIndex)),
78  this,
79  SLOT(triggerOpenGroupEditor(QModelIndex)));
80  connect(this, SIGNAL(groupNameClicked()), this, SLOT(openGroupEditorDialog()));
81 
82  // Connections to update group list
83  connect(control.get(),
84  SIGNAL(operationExecuted(controller::vector_string)),
85  this,
86  SLOT(listAllGroups()));
87 
88  // Connections for buttons in table
89  connect(ui->groupsTableWidget,
90  SIGNAL(itemClicked(QTableWidgetItem*)),
91  this,
92  SLOT(triggerOperation(QTableWidgetItem*)));
93  connect(this, SIGNAL(addToGroupClicked()), this, SLOT(addSelectionToGroup()));
94  connect(this, SIGNAL(addToSelectionClicked()), this, SLOT(addGroupToSelection()));
95 }
96 
98 {
99  delete ui;
100 }
101 
102 void
103 gui::GroupExplorerWidget::customContextMenuRequested(const QPoint& position)
104 {
105  currentItem = ui->groupsTableWidget->itemAt(position);
106  QModelIndex index = ui->groupsTableWidget->indexAt(position);
107 
108  // Position has to be in the first column where the group names are listed
109  if (index.isValid() && index.column() == 0)
110  {
111  QMenu* menu = new QMenu(this);
112  menu->addAction(tr("Add selection to group"), this, SLOT(addSelectionToGroup()));
113  menu->addAction(tr("Add group to selection"), this, SLOT(addGroupToSelection()));
114  menu->addAction(tr("Remove group"), this, SLOT(groupRemoveClicked()));
115  menu->addAction(tr("Show in Group Editor..."), this, SLOT(openGroupEditorDialog()));
116  menu->popup(ui->groupsTableWidget->viewport()->mapToGlobal(position));
117  }
118 }
119 
120 void
121 gui::GroupExplorerWidget::addSelectionToGroup()
122 {
123  if (currentItem != NULL)
124  {
125  std::string groupName = currentItem->text().toStdString();
126  createAddToGroupOperations(groupName);
127  }
128 }
129 
130 void
131 gui::GroupExplorerWidget::addGroupToSelection()
132 {
133  if (currentItem != NULL)
134  {
135  std::string groupName = currentItem->text().toStdString();
136  std::vector<scene3D::SceneObjectPtr> objectsInGroup;
137 
139  {
140  objectsInGroup =
141  controller->getScene()->getGroupManager()->getGroupById(groupName)->getAllObjects();
142  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
143  new std::vector<controller::OperationPtr>());
144 
145  // Create SelectOperation for each object
146  for (std::size_t i = 0; i < objectsInGroup.size(); ++i)
147  {
148  controller::OperationPtr operation(
149  new controller::SelectOperation(controller->getMemoryXController(),
150  controller->getScene(),
151  objectsInGroup[i]->getObjectId()));
152  operations->push_back(operation);
153  }
154 
158  operations);
159  }
160  }
161 }
162 
163 void
164 gui::GroupExplorerWidget::groupRemoveClicked()
165 {
166  if (currentItem != NULL)
167  {
169  {
170  std::string groupName = currentItem->text().toStdString();
171 
172  if (!controller->getScene()->getGroupManager()->getGroupById(groupName))
173  {
174  return;
175  }
176 
177  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
178  new std::vector<controller::OperationPtr>());
179 
180  for (scene3D::SceneObjectPtr object : controller->getScene()
181  ->getGroupManager()
182  ->getGroupById(groupName)
183  ->getAllObjects())
184  {
185  controller::OperationPtr operation(
186  new controller::RemoveFromGroupOperation(controller->getMemoryXController(),
187  controller->getScene(),
188  groupName,
189  object->getObjectId()));
190  operations->push_back(operation);
191  }
192 
194  controller->getMemoryXController(), controller->getScene(), groupName));
195  operations->push_back(operation);
198  operations);
199  }
200  }
201 }
202 
203 void
204 gui::GroupExplorerWidget::triggerOperation(QTableWidgetItem* item)
205 {
206  ui->groupsTableWidget->setCurrentItem(item);
207  currentItem = ui->groupsTableWidget->item(ui->groupsTableWidget->currentRow(), 0);
208 
209  if (currentItem != NULL)
210  {
211  // Icon to add selection to group has been clicked
212  if (item->column() == 1)
213  {
214  emit addToGroupClicked();
215  }
216  // Icon to add group to selection has been clicked
217  else if (item->column() == 2)
218  {
219  emit addToSelectionClicked();
220  }
221  }
222 }
223 
224 void
225 gui::GroupExplorerWidget::triggerOpenGroupEditor(QModelIndex index)
226 {
227  if (index.column() == 0)
228  {
229  emit groupNameClicked();
230  }
231 }
232 
233 void
234 gui::GroupExplorerWidget::createAddToGroupOperations(std::string groupName)
235 {
236  std::vector<scene3D::SceneObjectPtr> selectedObjects;
237 
239  {
240  selectedObjects = controller->getScene()->getSelectionManager()->getAllSelected();
241  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
242  new std::vector<controller::OperationPtr>());
243 
244  // Create SelectOperation for each object
245  for (std::size_t i = 0; i < selectedObjects.size(); ++i)
246  {
247  controller::OperationPtr operation(
248  new controller::AddToGroupOperation(controller->getMemoryXController(),
249  controller->getScene(),
250  groupName,
251  selectedObjects[i]->getObjectId()));
252  operations->push_back(operation);
253  }
254 
258  operations);
259  }
260 }
261 
262 void
263 gui::GroupExplorerWidget::openGroupEditorDialog()
264 {
265  if (groupEditorDialog)
266  {
267  groupEditorDialog->setCurrentGroup(currentItem->text());
268  groupEditorDialog->exec();
269  }
270 }
271 
272 void
273 gui::GroupExplorerWidget::listAllGroups()
274 {
275  ui->groupsTableWidget->clear();
276 
277  std::vector<scene3D::SceneGroupPtr> allGroups;
278 
280  {
281  allGroups = controller->getScene()->getGroupManager()->getAllGroups();
282  }
283 
284  ui->groupsTableWidget->setColumnCount(3);
285  ui->groupsTableWidget->setRowCount(allGroups.size());
286 
287  int index = 0;
288 
289  for (std::size_t i = 0; i < allGroups.size(); ++i)
290  {
291  scene3D::SceneGroupPtr group = allGroups.at(i);
292  QString groupName = QString::fromStdString(group->getGroupId());
293  QTableWidgetItem* item = new QTableWidgetItem(groupName);
294  QTableWidgetItem* addIcon =
295  new QTableWidgetItem(QIcon(QString::fromStdString(":/icons/edit-add.ico")), "");
296  QTableWidgetItem* selectIcon =
297  new QTableWidgetItem(QIcon(QString::fromStdString(":/images/Select.png")), "");
298  addIcon->setToolTip(tr("Add selection to group"));
299  selectIcon->setToolTip(tr("Add group to selection"));
300  ui->groupsTableWidget->setItem(index, 0, item);
301  ui->groupsTableWidget->setItem(index, 1, addIcon);
302  ui->groupsTableWidget->setItem(index, 2, selectIcon);
303  ++index;
304  }
305 
306  // To align the button columns on the right
307  QHeaderView* headerView = ui->groupsTableWidget->horizontalHeader();
308  headerView->setResizeMode(headerView->logicalIndexAt(0, 0), QHeaderView::Stretch);
309  headerView->setResizeMode(headerView->logicalIndexAt(1, 0), QHeaderView::Fixed);
310  headerView->setResizeMode(headerView->logicalIndexAt(2, 0), QHeaderView::Fixed);
311  headerView->resizeSection(1, 20);
312  headerView->resizeSection(2, 20);
313 }
314 
315 void
316 gui::GroupExplorerWidget::addSelectionToNewGroup()
317 {
319  {
320  int nameIndex = 0;
321 
322  do
323  {
324  nameIndex++;
325  } while (controller->getScene()->getGroupManager()->getGroupById(
326  tr("New Group ").toStdString() + std::to_string(nameIndex)) != NULL);
327 
328  std::string groupName = tr("New Group ").toStdString() + std::to_string(nameIndex);
329 
330  // Create CreateGroup operation
331  std::shared_ptr<std::vector<controller::OperationPtr>> operations(
332  new std::vector<controller::OperationPtr>());
334  controller->getMemoryXController(), controller->getScene(), groupName));
335  operations->push_back(operation);
338  operations);
339 
340  createAddToGroupOperations(groupName);
341  }
342 }
343 
344 void
346 {
347  ui->retranslateUi(this);
348 }
gui::GroupExplorerWidget
This class provides a QWidget which displays all existing groups in the local scene.
Definition: GroupExplorerWidget.h:46
gui::GroupExplorerWidget::GroupExplorerWidget
GroupExplorerWidget(const controller::ControllerPtr &control, QPointer< dialog::GroupExplorerDialog > groupEditorDialog, QWidget *parent=0)
Constructor.
Definition: GroupExplorerWidget.cpp:44
gui::GroupExplorerWidget::addToGroupClicked
void addToGroupClicked()
Signal emitted to add current selection in scene to selected group.
gui::OverrideAction
Definition: OverrideAction.h:31
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::GroupExplorerWidget::groupNameClicked
void groupNameClicked()
Signal emitted when click event on group name triggered.
gui::GroupExplorerWidget::addToSelectionClicked
void addToSelectionClicked()
Signal emitted to add selected group to current selection in the scene.
controller::vector_string
std::vector< std::string > vector_string
Definition: Controller.h:47
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
OverrideAction.h
controller::RemoveFromGroupOperation
A operation to removes a object from a existing group.
Definition: RemoveFromGroupOperation.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
ShortcutController.h
controller::SelectOperation
A operation to select a object.
Definition: SelectOperation.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::AddToGroupOperation
A operation to add a object to a existing group.
Definition: AddToGroupOperation.h:41
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
controller::Controller::EXECUTE_ON_WM
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition: Controller.h:66
gui::GroupExplorerWidget::retranslate
void retranslate()
Translates all translatable strings in this dialog.
Definition: GroupExplorerWidget.cpp:345
gui::GroupExplorerWidget::~GroupExplorerWidget
~GroupExplorerWidget() override
Definition: GroupExplorerWidget.cpp:97
control
This file is part of ArmarX.
GroupExplorerWidget.h