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