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
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
101
102void
103gui::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
120void
121gui::GroupExplorerWidget::addSelectionToGroup()
122{
123 if (currentItem != NULL)
124 {
125 std::string groupName = currentItem->text().toStdString();
126 createAddToGroupOperations(groupName);
127 }
128}
129
130void
131gui::GroupExplorerWidget::addGroupToSelection()
132{
133 if (currentItem != NULL)
134 {
135 std::string groupName = currentItem->text().toStdString();
136 std::vector<scene3D::SceneObjectPtr> objectsInGroup;
137
138 if (controller::ControllerPtr controller = control.lock())
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
155 controller->execute(controller::Controller::EXECUTE_ON_SCENE |
158 operations);
159 }
160 }
161}
162
163void
164gui::GroupExplorerWidget::groupRemoveClicked()
165{
166 if (currentItem != NULL)
167 {
168 if (controller::ControllerPtr controller = control.lock())
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
193 controller::OperationPtr operation(new controller::DeleteGroupOperation(
194 controller->getMemoryXController(), controller->getScene(), groupName));
195 operations->push_back(operation);
196 controller->execute(controller::Controller::EXECUTE_ON_SCENE |
198 operations);
199 }
200 }
201}
202
203void
204gui::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
224void
225gui::GroupExplorerWidget::triggerOpenGroupEditor(QModelIndex index)
226{
227 if (index.column() == 0)
228 {
229 emit groupNameClicked();
230 }
231}
232
233void
234gui::GroupExplorerWidget::createAddToGroupOperations(std::string groupName)
235{
236 std::vector<scene3D::SceneObjectPtr> selectedObjects;
237
238 if (controller::ControllerPtr controller = control.lock())
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
255 controller->execute(controller::Controller::EXECUTE_ON_SCENE |
258 operations);
259 }
260}
261
262void
263gui::GroupExplorerWidget::openGroupEditorDialog()
264{
265 if (groupEditorDialog)
266 {
267 groupEditorDialog->setCurrentGroup(currentItem->text());
268 groupEditorDialog->exec();
269 }
270}
271
272void
273gui::GroupExplorerWidget::listAllGroups()
274{
275 ui->groupsTableWidget->clear();
276
277 std::vector<scene3D::SceneGroupPtr> allGroups;
278
279 if (controller::ControllerPtr controller = control.lock())
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
315void
316gui::GroupExplorerWidget::addSelectionToNewGroup()
317{
318 if (controller::ControllerPtr controller = control.lock())
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>());
333 controller::OperationPtr operation(new controller::CreateGroupOperation(
334 controller->getMemoryXController(), controller->getScene(), groupName));
335 operations->push_back(operation);
336 controller->execute(controller::Controller::EXECUTE_ON_SCENE |
338 operations);
339
340 createAddToGroupOperations(groupName);
341 }
342}
343
344void
346{
347 ui->retranslateUi(this);
348}
uint8_t index
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition Controller.h:80
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition Controller.h:66
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition Controller.h:73
void addToGroupClicked()
Signal emitted to add current selection in scene to selected group.
void groupNameClicked()
Signal emitted when click event on group name triggered.
GroupExplorerWidget(const controller::ControllerPtr &control, QPointer< dialog::GroupExplorerDialog > groupEditorDialog, QWidget *parent=0)
Constructor.
void retranslate()
Translates all translatable strings in this dialog.
void addToSelectionClicked()
Signal emitted to add selected group to current selection in the scene.
ArmarX Headers.
This file is part of ArmarX.
std::vector< std::string > vector_string
Definition Controller.h:47
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< Operation > OperationPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
std::shared_ptr< SceneGroup > SceneGroupPtr