ScenegraphWidget.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 "ScenegraphWidget.h"
25 #include <Inventor/actions/SoRayPickAction.h>
26 #include <QGridLayout>
27 #include <QDataStream>
28 #include <QByteArray>
29 #include <vector>
30 #include <math.h>
31 
32 #include "../controller/AddOperation.h"
33 #include "../controller/SelectOperation.h"
34 #include "../controller/DeselectOperation.h"
35 #include "../controller/RotateTranslateOperation.h"
36 #include "OverrideAction.h"
37 #include "QMimeData"
38 
39 
41  QWidget(parent),
43  camera(new SoPerspectiveCamera),
44  copyBuffer(new std::vector<controller::AddOperationPtr>())
45 {
46  //setPalette(Qt::transparent);
47  //setAttribute(Qt::WA_TransparentForMouseEvents);
48  mainController = control;
49 
50  this->setContentsMargins(1, 1, 1, 1);
51 
52  QGridLayout* grid = new QGridLayout();
53  grid->setContentsMargins(0, 0, 0, 0);
54  this->setLayout(grid);
55  this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
56 
57  QWidget* view1 = new QWidget(this);
58  view1->setMinimumSize(100, 100);
59  view1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
60  grid->addWidget(view1, 0, 0, 1, 2);
61 
62  viewer.reset(new scene3D::SceneViewer(control, view1));
63 
64  std::shared_ptr<gui::ShortcutController> shortcutController = control->getShortcutController();
65 
66  //Camera view all action
67  QPointer<QAction> cameraViewAllAction(new OverrideAction("Scene: Camera view all", this));
68  this->addAction(cameraViewAllAction);
69  connect(cameraViewAllAction, SIGNAL(triggered()), this, SLOT(cameraViewAll()));
70  shortcutController->registerAction(cameraViewAllAction->text(), cameraViewAllAction);
71 
72  //Camera view from X action
73  QPointer<QAction> cameraViewFromXAction(new OverrideAction("Scene: Camera view from X", this));
74  this->addAction(cameraViewFromXAction);
75  connect(cameraViewFromXAction, SIGNAL(triggered()), this, SLOT(cameraViewFromX()));
76  shortcutController->registerAction(cameraViewFromXAction->text(), cameraViewFromXAction);
77 
78  //Camera view from neg X action
79  QPointer<QAction> cameraViewFromNegXAction(new OverrideAction("Scene: Camera view from negative X", this));
80  this->addAction(cameraViewFromNegXAction);
81  connect(cameraViewFromNegXAction, SIGNAL(triggered()), this, SLOT(cameraViewFromNegX()));
82  shortcutController->registerAction(cameraViewFromNegXAction->text(), cameraViewFromNegXAction);
83 
84  //Camera view from Y action
85  QPointer<QAction> cameraViewFromYAction(new OverrideAction("Scene: Camera view from Y", this));
86  this->addAction(cameraViewFromYAction);
87  connect(cameraViewFromYAction, SIGNAL(triggered()), this, SLOT(cameraViewFromY()));
88  shortcutController->registerAction(cameraViewFromYAction->text(), cameraViewFromYAction);
89 
90  //Camera view from neg Y action
91  QPointer<QAction> cameraViewFromNegYAction(new OverrideAction("Scene: Camera view from negative Y", this));
92  this->addAction(cameraViewFromNegYAction);
93  connect(cameraViewFromNegYAction, SIGNAL(triggered()), this, SLOT(cameraViewFromNegY()));
94  shortcutController->registerAction(cameraViewFromNegYAction->text(), cameraViewFromNegYAction);
95 
96  //Camera view from Z action
97  QPointer<QAction> cameraViewFromZAction(new OverrideAction("Scene: Camera view from Z", this));
98  this->addAction(cameraViewFromZAction);
99  connect(cameraViewFromZAction, SIGNAL(triggered()), this, SLOT(cameraViewFromZ()));
100  shortcutController->registerAction(cameraViewFromZAction->text(), cameraViewFromZAction);
101 
102  //Camera view from neg Z action
103  QPointer<QAction> cameraViewFromNegZAction(new OverrideAction("Scene: Camera view from negative Z", this));
104  this->addAction(cameraViewFromNegZAction);
105  connect(cameraViewFromNegZAction, SIGNAL(triggered()), this, SLOT(cameraViewFromNegZ()));
106  shortcutController->registerAction(cameraViewFromNegZAction->text(), cameraViewFromNegZAction);
107 
108  //Select all or deselect all action
109  QPointer<QAction> selectToggleAllAction(new OverrideAction("Scene: Select all or deselect all", this));
110  this->addAction(selectToggleAllAction);
111  connect(selectToggleAllAction, SIGNAL(triggered()), this, SLOT(selectToggleAll()));
112  shortcutController->registerAction(selectToggleAllAction->text(), selectToggleAllAction);
113 
114  //Copy action
115  QPointer<QAction> copyAction(new OverrideAction("Scene: Copy selection", this));
116  this->addAction(copyAction);
117  connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
118  shortcutController->registerAction(copyAction->text(), copyAction);
119 
120  //Paste action
121  QPointer<QAction> pasteAction(new OverrideAction("Scene: Paste selection", this));
122  this->addAction(pasteAction);
123  connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
124  shortcutController->registerAction(pasteAction->text(), pasteAction);
125 
126  //Duplicate action
127  QPointer<QAction> duplicateAction(new OverrideAction("Scene: Duplicate selection", this));
128  this->addAction(duplicateAction);
129  connect(duplicateAction, SIGNAL(triggered()), this, SLOT(duplicate()));
130  shortcutController->registerAction(duplicateAction->text(), duplicateAction);
131 
132  //Reset rotation action
133  QPointer<QAction> resetRotationAction(new OverrideAction("Scene: Reset rotation", this));
134  this->addAction(resetRotationAction);
135  connect(resetRotationAction, SIGNAL(triggered()), this, SLOT(resetRotation()));
136  shortcutController->registerAction(resetRotationAction->text(), resetRotationAction);
137 
138  //Reset translation action
139  QPointer<QAction> resetTranslationAction(new OverrideAction("Scene: Reset translation", this));
140  this->addAction(resetTranslationAction);
141  connect(resetTranslationAction, SIGNAL(triggered()), this, SLOT(resetTranslation()));
142  shortcutController->registerAction(resetTranslationAction->text(), resetTranslationAction);
143 }
144 
146 {
147 }
148 
150 {
151  viewer->setSceneGraph(mainController->getScene()->registerCamera(camera));
152 
153  //Give camera standard position
154  camera->position.setValue(SbVec3f(10, -10, 5));
155  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
156 
157 
158  //Apply custom highlighting and make viewer redraw on selection changes
159  viewer->setGLRenderAction(new scene3D::SoGLHighlightRenderAction(viewer->getViewportRegion(), mainController->getScene().get()));
160  viewer->redrawOnSelectionChange(mainController->getScene()->selectionRootNode);
161 
162  // show everything
163  viewer->show();
164 
165  setAcceptDrops(true);
166 
167  connect(mainController.get(), SIGNAL(reloadScene()), this, SLOT(cameraViewAll()));
168  connect(mainController.get(), SIGNAL(minimapClicked()), this, SLOT(cameraViewFromZ()));
169 }
170 
172 {
174  {
175  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
176 
177  for (scene3D::SceneObjectPtr sceneobject : controller->getScene()->sceneSelectionManager->getAllSelected())
178  {
180  controller->getMemoryXController(),
181  controller->getScene(), sceneobject->getObjectId(),
182  sceneobject->getRotation(), SbRotation(SbVec3f(0, 0, 1), 0),
183  sceneobject->getTranslation(), sceneobject->getTranslation()));
184 
185  operations->push_back(operation);
186  }
187 
189  }
190 }
191 
193 {
195  {
196  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
197 
198  for (scene3D::SceneObjectPtr sceneobject : controller->getScene()->sceneSelectionManager->getAllSelected())
199  {
201  controller->getMemoryXController(),
202  controller->getScene(), sceneobject->getObjectId(),
203  sceneobject->getRotation(), sceneobject->getRotation(),
204  sceneobject->getTranslation(), SbVec3f(0, 0, 0)));
205 
206  operations->push_back(operation);
207  }
208 
210  }
211 }
213 {
215  {
216  //Clear copy buffer
217  copyBuffer->clear();
218 
219  for (scene3D::SceneObjectPtr sceneobject : controller->getScene()->sceneSelectionManager->getAllSelected())
220  {
222  controller->getMemoryXController(), controller->getScene(),
223  sceneobject->getClassId(), sceneobject->getCollection(),
224  sceneobject->getTranslation(), sceneobject->getRotation()));
225 
226  copyBuffer->push_back(addOperation);
227  }
228  }
229 }
230 
232 {
233  if (copyBuffer->size() != 0)
234  {
236  {
237  std::shared_ptr<std::vector<controller::OperationPtr>> operations(new std::vector<controller::OperationPtr>());
238 
239  // deselect all objects
240  for (scene3D::SceneObjectPtr object : controller->getScene()->getSelectionManager()->getAllSelected())
241  {
242  controller::OperationPtr deselect(new controller::DeselectOperation(controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
243  operations->push_back(deselect);
244  }
245 
246  // add new objects
247  for (auto it = copyBuffer->begin(); it != copyBuffer->end(); ++it)
248  {
250  operations->push_back(add);
251  controller::OperationPtr select(new controller::SelectOperation(controller->getMemoryXController(), controller->getScene(), add->getObjectId()));
252  operations->push_back(select);
253  }
254 
256  }
257  }
258 }
259 
261 {
263  {
264  std::shared_ptr<std::vector<controller::OperationPtr>> operations(new std::vector<controller::OperationPtr>());
265 
266  for (scene3D::SceneObjectPtr object : controller->getScene()->getSelectionManager()->getAllSelected())
267  {
268  controller::OperationPtr deselect(new controller::DeselectOperation(controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
269  operations->push_back(deselect);
271  controller->getMemoryXController(), controller->getScene(),
272  object->getClassId(), object->getCollection(),
273  object->getTranslation(), object->getRotation()));
274  operations->push_back(add);
275  controller::OperationPtr select(new controller::SelectOperation(controller->getMemoryXController(), controller->getScene(), add->getObjectId()));
276  operations->push_back(select);
277  }
278 
280  }
281 }
282 
284 {
286  {
287  controller->getScene()->makeCameraViewAll(camera, viewer->getViewportRegion());
288  }
289 }
290 
292 {
293  //Make sure we are on x axis
294  camera->position.setValue(1, 0, 0);
295  //Look at scene
296  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
297 
298  //View everything in bounding box
299  this->cameraViewAll();
300 }
301 
303 {
304  //Make sure we are on x axis
305  camera->position.setValue(-1, 0, 0);
306  //Look at scene
307  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
308 
309  //View everything in bounding box
310  this->cameraViewAll();
311 }
312 
314 {
315  //Make sure we are on x axis
316  camera->position.setValue(0, 1, 0);
317  //Look at scene
318  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
319 
320  //View everything in bounding box
321  this->cameraViewAll();
322 }
323 
325 {
326  //Make sure we are on x axis
327  camera->position.setValue(0, -1, 0);
328  //Look at scene
329  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
330 
331  //View everything in bounding box
332  this->cameraViewAll();
333 }
334 
336 {
337  //Make sure we are on x axis
338  camera->position.setValue(0, 0, 1);
339  //Look at scene
340  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 1, 0));
341 
342  //View everything in bounding box
343  this->cameraViewAll();
344 }
345 
347 {
348  //Make sure we are on x axis
349  camera->position.setValue(0, 0, -1);
350  //Look at scene
351  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 1, 0));
352 
353  //View everything in bounding box
354  this->cameraViewAll();
355 }
356 
358 {
360  {
361  //Check if all objects are selected
362  bool allObjectsAreSelected = true;
363 
364  for (scene3D::SceneObjectPtr object : controller->getScene()->getObjectManager()->getAllObjects())
365  {
366  if (!controller->getScene()->getSelectionManager()->isSelected(object))
367  {
368  allObjectsAreSelected = false;
369  break;
370  }
371  }
372 
373  //Prepare some operations
374  std::shared_ptr<std::vector<controller::OperationPtr>> operations(new std::vector<controller::OperationPtr>());
375 
376  if (allObjectsAreSelected)
377  {
378  //Deselect all
379  for (scene3D::SceneObjectPtr object : controller->getScene()->getSelectionManager()->getAllSelected())
380  {
381  controller::OperationPtr deselect(new controller::DeselectOperation(controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
382  operations->push_back(deselect);
383  }
384  }
385  else
386  {
387  //Select all
388  for (scene3D::SceneObjectPtr object : controller->getScene()->getObjectManager()->getAllObjects())
389  {
390  if (!controller->getScene()->getSelectionManager()->isSelected(object))
391  {
392  controller::OperationPtr select(new controller::SelectOperation(controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
393  operations->push_back(select);
394  }
395  }
396  }
397 
398  //Execute stuff
400  }
401 }
402 
403 void gui::ScenegraphWidget::dragMoveEvent(QDragMoveEvent* event)
404 {
405  // The event needs to be accepted here
406  event->accept();
407 }
408 
409 void gui::ScenegraphWidget::dragEnterEvent(QDragEnterEvent* event)
410 {
411  // Set the drop action to be the proposed action.
412  if (event->source())
413  {
414  event->acceptProposedAction();
415  }
416  else
417  {
418  event->ignore();
419  }
420 }
421 
422 void gui::ScenegraphWidget::dropEvent(QDropEvent* event)
423 {
424  if (!event->mimeData()->hasFormat("application/vnd.text.list"))
425  {
426  return;
427  }
428 
430  {
431  QByteArray encodedData = event->mimeData()->data("application/vnd.text.list");
432  QDataStream stream(&encodedData, QIODevice::ReadOnly);
433 
434  std::shared_ptr<std::vector<controller::OperationPtr>> operations(new std::vector<controller::OperationPtr>());
435 
436  // deselect all objects
437  for (scene3D::SceneObjectPtr object : controller->getScene()->getSelectionManager()->getAllSelected())
438  {
439  controller::OperationPtr deselect(new controller::DeselectOperation(controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
440  operations->push_back(deselect);
441  }
442 
443  // add new objects
444  while (!stream.atEnd())
445  {
446  QString collection, objectType;
447  stream >> collection;
448  stream >> objectType;
449 
450  // get needed information
451  SoCamera* pCamera = viewer->getCamera();
452  float aspect = viewer->getViewportRegion().getViewportAspectRatio();
453  SbViewVolume vvol = pCamera->getViewVolume(aspect);
454 
455  if (aspect < 1)
456  {
457  vvol.scale(1 / aspect);
458  }
459 
460  SbPlane ground(SbVec3f(0, 0, 1), SbVec3f(0, 0, 0));
461  SbVec2s clickPoint((short)event->pos().x(), (short)(this->height() - event->pos().y()));
462  SbVec2f clickPointNormalized(1.0f * event->pos().x() / this->width(), 1.0f - 1.0f * event->pos().y() / this->height());
463 
464  // calculate position to insert the object
465  SbVec3f insertPosition;
466  SbLine line;
467  vvol.projectPointToLine(clickPointNormalized, line);
468  SbVec3f pointOnCameraPlane;
469  vvol.getPlane(0.0).intersect(line, pointOnCameraPlane);
470  ground.intersect(line, insertPosition);
471 
472  if (pointOnCameraPlane.getValue()[2] > 0)
473  {
474  SoRayPickAction rp(viewer->getViewportRegion());
475  rp.setPickAll(false);
476  rp.setPoint(clickPoint);
477  rp.apply(viewer->getSceneGraph());
478  SoPickedPoint* picked = rp.getPickedPoint();
479 
480  if (picked != NULL)
481  {
482  insertPosition = picked->getPoint();
483  }
484  }
485 
486  // set the standard rotation
487  SbRotation insertRotation(SbVec3f(1, 0, 0), 0);
488 
489  // create operations
490  controller::OperationPtr add(new controller::AddOperation(controller->getMemoryXController(), controller->getScene(), objectType.toStdString(), collection.toStdString(), insertPosition, insertRotation));
491  operations->push_back(add);
492  controller::OperationPtr select(new controller::SelectOperation(controller->getMemoryXController(), controller->getScene(), add->getObjectId()));
493  operations->push_back(select);
494  }
495 
497  }
498 }
scene3D::SceneViewer
Definition: SceneViewer.h:32
gui::ScenegraphWidget::dropEvent
void dropEvent(QDropEvent *de) override
Reimplements the function Qt::QWidget:dropEvent().
Definition: ScenegraphWidget.cpp:422
gui::OverrideAction
Definition: OverrideAction.h:31
gui::ScenegraphWidget::cameraViewFromX
void cameraViewFromX()
Definition: ScenegraphWidget.cpp:291
controller::Controller::EXECUTE_ON_SCENE
static const int EXECUTE_ON_SCENE
A flag to execute operations on the Scene.
Definition: Controller.h:73
gui::ScenegraphWidget::cameraViewFromY
void cameraViewFromY()
Definition: ScenegraphWidget.cpp:313
gui::ScenegraphWidget::resetTranslation
void resetTranslation()
Definition: ScenegraphWidget.cpp:192
scene3D::SoGLHighlightRenderAction
Definition: SoGLHighlightRenderAction.h:57
gui::ScenegraphWidget::cameraViewFromNegX
void cameraViewFromNegX()
Definition: ScenegraphWidget.cpp:302
gui::ScenegraphWidget::cameraViewAll
void cameraViewAll()
Definition: ScenegraphWidget.cpp:283
gui::ScenegraphWidget::dragMoveEvent
void dragMoveEvent(QDragMoveEvent *de) override
Reimplements the function Qt::QWidget:dragMoveEvent();.
Definition: ScenegraphWidget.cpp:403
ScenegraphWidget.h
gui::ScenegraphWidget::cameraViewFromNegY
void cameraViewFromNegY()
Definition: ScenegraphWidget.cpp:324
gui::ScenegraphWidget::cameraViewFromNegZ
void cameraViewFromNegZ()
Definition: ScenegraphWidget.cpp:346
gui::ScenegraphWidget::selectToggleAll
void selectToggleAll()
Definition: ScenegraphWidget.cpp:357
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
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
OverrideAction.h
gui::ScenegraphWidget::paste
void paste()
Definition: ScenegraphWidget.cpp:231
gui::ScenegraphWidget::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event) override
Reimplements the function Qt::QWidget:dragEnterEvent();.
Definition: ScenegraphWidget.cpp:409
gui::ScenegraphWidget::cameraViewFromZ
void cameraViewFromZ()
Definition: ScenegraphWidget.cpp:335
gui::ScenegraphWidget::duplicate
void duplicate()
Definition: ScenegraphWidget.cpp:260
controller::DeselectOperation
A operation to deselect a object.
Definition: DeselectOperation.h:41
controller::SelectOperation
A operation to select a object.
Definition: SelectOperation.h:41
std
Definition: Application.h:66
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
gui::ScenegraphWidget::copy
void copy()
Definition: ScenegraphWidget.cpp:212
controller::RotateTranslateOperation
A operation to rotate and translate a object.
Definition: RotateTranslateOperation.h:46
controller::Controller::EXECUTE_ON_WM
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition: Controller.h:66
controller::AddOperationPtr
std::shared_ptr< AddOperation > AddOperationPtr
Definition: ClassDefinitions.h:32
gui::ScenegraphWidget::postDocking
void postDocking()
Definition: ScenegraphWidget.cpp:149
gui::ScenegraphWidget::~ScenegraphWidget
~ScenegraphWidget() override
Destructor.
Definition: ScenegraphWidget.cpp:145
control
This file is part of ArmarX.
gui::ScenegraphWidget::resetRotation
void resetRotation()
Definition: ScenegraphWidget.cpp:171
controller::AddOperation
A Operation to create a new object in the scene and the WorkingMemory.
Definition: AddOperation.h:46
gui::ScenegraphWidget::ScenegraphWidget
ScenegraphWidget(const controller::ControllerPtr control, QWidget *parent=0)
Constructor.
Definition: ScenegraphWidget.cpp:40