DisplayWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package <PACKAGE_NAME>::<CATEGORY>::DisplayWidget
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "DisplayWidget.h"
24 
25 #include <QGridLayout>
26 
27 #include <Inventor/events/SoLocation2Event.h>
28 #include <Inventor/events/SoMouseButtonEvent.h>
29 #include <Inventor/nodes/SoDrawStyle.h>
30 #include <Inventor/nodes/SoLineSet.h>
31 #include <Inventor/nodes/SoMaterial.h>
32 #include <Inventor/nodes/SoPickStyle.h>
33 #include <Inventor/nodes/SoSeparator.h>
34 #include <Inventor/nodes/SoVertexProperty.h>
35 
36 namespace armarx
37 {
38  DisplayWidget::DisplayWidget(QWidget* parent) : QWidget(parent)
39  {
40  this->setContentsMargins(1, 1, 1, 1);
41 
42  QGridLayout* grid = new QGridLayout(this);
43  grid->setContentsMargins(0, 0, 0, 0);
44  this->setLayout(grid);
45  this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
46 
47  QWidget* view1 = new QWidget(this);
48  view1->setMinimumSize(100, 100);
49  view1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
50 
51  display = new Display(view1);
52  display->show();
53 
54  grid->addWidget(view1, 0, 0, 1, 2);
55  }
56 
58  {
59 
60  delete display;
61  }
62 
63  Display*
65  {
66  return display;
67  }
68 
69  Display::Display(QWidget* widget) :
70  SoQtExaminerViewer(widget),
71  sceneRootNode(new SoSeparator),
72  contentRootNode(new SoSeparator),
73  camera(new SoPerspectiveCamera)
74  {
75  this->setBackgroundColor(SbColor(80 / 255.0f, 80 / 255.0f, 80 / 255.0f));
76  this->setAccumulationBuffer(true);
77  this->setHeadlight(true);
78  this->setViewing(true);
79  this->setDecoration(false);
80 
81  this->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
82  this->setFeedbackVisibility(true);
83 
84  //Create scene root node
85  sceneRootNode->ref();
86  this->setSceneGraph(sceneRootNode);
87 
88  //Add camera to scene
89  sceneRootNode->addChild(camera);
90  this->setCamera(camera);
91 
92  //Give camera standard position
93  camera->position.setValue(SbVec3f(10, -10, 5));
94  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
95 
96  //Add content node
97  sceneRootNode->addChild(contentRootNode);
98 
99  this->setViewing(false);
100  this->setViewing(true);
101  }
102 
104  {
105  if (sceneRootNode)
106  {
107  sceneRootNode->unref();
108  }
109  }
110 
111  SoSeparator*
113  {
114  return contentRootNode;
115  }
116 
117  void
119  {
120  camera->viewAll(contentRootNode, SbViewportRegion());
121  }
122 
123  void
124  Display::cameraViewNode(SoNode* node, const float slack)
125  {
126  camera->viewAll(node, SbViewportRegion(), slack);
127  }
128 
129  // //Override the default navigation behaviour of the SoQtExaminerViewer
130  // SbBool Display::processSoEvent(const SoEvent* const event)
131  // {
132  // const SoType type(event->getTypeId());
133 
134  // //Remapping mouse press events
135  // if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
136  // {
137  // SoMouseButtonEvent* const ev = (SoMouseButtonEvent*) event;
138  // const int button = ev->getButton();
139  // const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
140 
141  // //LEFT MOUSE BUTTON
142  // if (button == SoMouseButtonEvent::BUTTON1)
143  // {
144  // //Enable or disable viewing mode while BUTTON1 pressed
145  // if (press)
146  // {
147  // if (!this->isViewing())
148  // {
149  // this->setViewing(true);
150  // }
151  // }
152  // else
153  // {
154  // if (this->isViewing())
155  // {
156  // this->setViewing(false);
157  // }
158  // }
159 
160  // if (this->isViewing())
161  // {
162  // return SoQtExaminerViewer::processSoEvent(ev);
163  // }
164  // }
165 
166  // //MOUSE WHEEL UP AND DOWN
167  // if (button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5)
168  // {
169  // //Zooming is allowed, so just use it. We have to temporarily turn viewing mode
170  // // on to make SoQtExaminerViewer allow zooming.
171 
172  // //Swap BUTTON4 and BUTTON5 because zooming out while scrolling up is just retarded
173  // ev->setButton(button == SoMouseButtonEvent::BUTTON4 ?
174  // SoMouseButtonEvent::BUTTON5 : SoMouseButtonEvent::BUTTON4);
175 
176  // //Zooming is allowed, so just pass it and temporarily set viewing mode on, if it is not already
177  // //(otherwise coin gives us warning messages...)
178  // if (!this->isViewing())
179  // {
180  // if (!this->isViewing())
181  // {
182  // this->setViewing(true);
183  // }
184 
185  // SoQtExaminerViewer::processSoEvent(ev);
186 
187  // if (this->isViewing())
188  // {
189  // this->setViewing(false);
190  // }
191  // }
192  // else
193  // {
194  // SoQtExaminerViewer::processSoEvent(ev);
195  // }
196 
197  // return TRUE;
198  // }
199  // }
200 
201  // // Keyboard handling
202  // if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
203  // {
204  // const SoKeyboardEvent* const ev = (const SoKeyboardEvent*) event;
205 
206  // //The escape key and super key (windows key) is used to switch between
207  // // viewing modes. We need to disable this behaviour completely.
208 
209  // //65513 seems to be the super key, which is not available in the enum of keys in coin....
210  // if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513)
211  // {
212  // return TRUE;
213  // }
214  // else if (ev->getKey() == SoKeyboardEvent::S && ev->getState() == SoButtonEvent::DOWN)
215  // {
216  // if (!this->isSeekMode())
217  // {
218  // if (!this->isViewing())
219  // {
220  // this->setViewing(true);
221  // }
222 
223  // SoQtExaminerViewer::processSoEvent(ev);
224  // this->setSeekTime(0.5);
225  // this->seekToPoint(ev->getPosition());
226 
227  // if (this->isViewing())
228  // {
229  // this->setViewing(false);
230  // }
231  // }
232  // }
233  // else if (ev->getKey() == SoKeyboardEvent::R && ev->getState() == SoButtonEvent::DOWN)
234  // {
235  // camera->viewAll(this->contentRootNode, SbViewportRegion());
236  // }
237  // else
238  // {
239  // SoQtExaminerViewer::processSoEvent(ev);
240  // }
241 
242  // SoQtExaminerViewer::processSoEvent(ev);
243  // }
244 
245 
246  // if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
247  // {
248  // return SoQtExaminerViewer::processSoEvent(event);
249  // }
250 
251  // //YOU SHALL NOT PASS!
252  // return TRUE;
253 
254  // return SoQtExaminerViewer::processSoEvent(event);
255  // }
256 } // namespace armarx
armarx::Display::cameraViewAll
void cameraViewAll()
Definition: DisplayWidget.cpp:118
armarx::Display::~Display
~Display() override
Definition: DisplayWidget.cpp:103
armarx::DisplayWidget::DisplayWidget
DisplayWidget(QWidget *parent=0)
Definition: DisplayWidget.cpp:38
DisplayWidget.h
armarx::DisplayWidget::~DisplayWidget
~DisplayWidget() override
Definition: DisplayWidget.cpp:57
armarx::Display::cameraViewNode
void cameraViewNode(SoNode *node, const float slack=1.0)
Definition: DisplayWidget.cpp:124
armarx::DisplayWidget::getDisplay
Display * getDisplay()
Definition: DisplayWidget.cpp:64
armarx::Display::Display
Display(QWidget *widget)
Definition: DisplayWidget.cpp:69
armarx::Display
Definition: DisplayWidget.h:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Display::getRootNode
SoSeparator * getRootNode()
Definition: DisplayWidget.cpp:112