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/nodes/SoSeparator.h>
28 
29 #include <Inventor/nodes/SoPickStyle.h>
30 #include <Inventor/nodes/SoMaterial.h>
31 #include <Inventor/nodes/SoDrawStyle.h>
32 #include <Inventor/nodes/SoVertexProperty.h>
33 #include <Inventor/nodes/SoLineSet.h>
34 #include <Inventor/events/SoMouseButtonEvent.h>
35 #include <Inventor/events/SoLocation2Event.h>
36 
37 namespace armarx
38 {
39  DisplayWidget::DisplayWidget(QWidget* parent) : QWidget(parent)
40  {
41  this->setContentsMargins(1, 1, 1, 1);
42 
43  QGridLayout* grid = new QGridLayout(this);
44  grid->setContentsMargins(0, 0, 0, 0);
45  this->setLayout(grid);
46  this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
47 
48  QWidget* view1 = new QWidget(this);
49  view1->setMinimumSize(100, 100);
50  view1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
51 
52  display = new Display(view1);
53  display->show();
54 
55  grid->addWidget(view1, 0, 0, 1, 2);
56  }
57 
59  {
60 
61  delete display;
62  }
63 
65  {
66  return display;
67  }
68 
69  Display::Display(QWidget* widget) : SoQtExaminerViewer(widget), sceneRootNode(new SoSeparator), contentRootNode(new SoSeparator), camera(new SoPerspectiveCamera)
70  {
71  this->setBackgroundColor(SbColor(80 / 255.0f, 80 / 255.0f, 80 / 255.0f));
72  this->setAccumulationBuffer(true);
73  this->setHeadlight(true);
74  this->setViewing(true);
75  this->setDecoration(false);
76 
77  this->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
78  this->setFeedbackVisibility(true);
79 
80  //Create scene root node
81  sceneRootNode->ref();
82  this->setSceneGraph(sceneRootNode);
83 
84  //Add camera to scene
85  sceneRootNode->addChild(camera);
86  this->setCamera(camera);
87 
88  //Give camera standard position
89  camera->position.setValue(SbVec3f(10, -10, 5));
90  camera->pointAt(SbVec3f(0, 0, 0), SbVec3f(0, 0, 1));
91 
92  //Add content node
93  sceneRootNode->addChild(contentRootNode);
94 
95  this->setViewing(false);
96  this->setViewing(true);
97  }
98 
100  {
101  if (sceneRootNode)
102  {
103  sceneRootNode->unref();
104  }
105  }
106 
107  SoSeparator* Display::getRootNode()
108  {
109  return contentRootNode;
110  }
111 
113  {
114  camera->viewAll(contentRootNode, SbViewportRegion());
115  }
116 
117  void Display::cameraViewNode(SoNode* node, const float slack)
118  {
119  camera->viewAll(node, SbViewportRegion(), slack);
120  }
121 
122  // //Override the default navigation behaviour of the SoQtExaminerViewer
123  // SbBool Display::processSoEvent(const SoEvent* const event)
124  // {
125  // const SoType type(event->getTypeId());
126 
127  // //Remapping mouse press events
128  // if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId()))
129  // {
130  // SoMouseButtonEvent* const ev = (SoMouseButtonEvent*) event;
131  // const int button = ev->getButton();
132  // const SbBool press = ev->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
133 
134  // //LEFT MOUSE BUTTON
135  // if (button == SoMouseButtonEvent::BUTTON1)
136  // {
137  // //Enable or disable viewing mode while BUTTON1 pressed
138  // if (press)
139  // {
140  // if (!this->isViewing())
141  // {
142  // this->setViewing(true);
143  // }
144  // }
145  // else
146  // {
147  // if (this->isViewing())
148  // {
149  // this->setViewing(false);
150  // }
151  // }
152 
153  // if (this->isViewing())
154  // {
155  // return SoQtExaminerViewer::processSoEvent(ev);
156  // }
157  // }
158 
159  // //MOUSE WHEEL UP AND DOWN
160  // if (button == SoMouseButtonEvent::BUTTON4 || button == SoMouseButtonEvent::BUTTON5)
161  // {
162  // //Zooming is allowed, so just use it. We have to temporarily turn viewing mode
163  // // on to make SoQtExaminerViewer allow zooming.
164 
165  // //Swap BUTTON4 and BUTTON5 because zooming out while scrolling up is just retarded
166  // ev->setButton(button == SoMouseButtonEvent::BUTTON4 ?
167  // SoMouseButtonEvent::BUTTON5 : SoMouseButtonEvent::BUTTON4);
168 
169  // //Zooming is allowed, so just pass it and temporarily set viewing mode on, if it is not already
170  // //(otherwise coin gives us warning messages...)
171  // if (!this->isViewing())
172  // {
173  // if (!this->isViewing())
174  // {
175  // this->setViewing(true);
176  // }
177 
178  // SoQtExaminerViewer::processSoEvent(ev);
179 
180  // if (this->isViewing())
181  // {
182  // this->setViewing(false);
183  // }
184  // }
185  // else
186  // {
187  // SoQtExaminerViewer::processSoEvent(ev);
188  // }
189 
190  // return TRUE;
191  // }
192  // }
193 
194  // // Keyboard handling
195  // if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId()))
196  // {
197  // const SoKeyboardEvent* const ev = (const SoKeyboardEvent*) event;
198 
199  // //The escape key and super key (windows key) is used to switch between
200  // // viewing modes. We need to disable this behaviour completely.
201 
202  // //65513 seems to be the super key, which is not available in the enum of keys in coin....
203  // if (ev->getKey() == SoKeyboardEvent::ESCAPE || ev->getKey() == 65513)
204  // {
205  // return TRUE;
206  // }
207  // else if (ev->getKey() == SoKeyboardEvent::S && ev->getState() == SoButtonEvent::DOWN)
208  // {
209  // if (!this->isSeekMode())
210  // {
211  // if (!this->isViewing())
212  // {
213  // this->setViewing(true);
214  // }
215 
216  // SoQtExaminerViewer::processSoEvent(ev);
217  // this->setSeekTime(0.5);
218  // this->seekToPoint(ev->getPosition());
219 
220  // if (this->isViewing())
221  // {
222  // this->setViewing(false);
223  // }
224  // }
225  // }
226  // else if (ev->getKey() == SoKeyboardEvent::R && ev->getState() == SoButtonEvent::DOWN)
227  // {
228  // camera->viewAll(this->contentRootNode, SbViewportRegion());
229  // }
230  // else
231  // {
232  // SoQtExaminerViewer::processSoEvent(ev);
233  // }
234 
235  // SoQtExaminerViewer::processSoEvent(ev);
236  // }
237 
238 
239  // if (type.isDerivedFrom(SoLocation2Event::getClassTypeId()))
240  // {
241  // return SoQtExaminerViewer::processSoEvent(event);
242  // }
243 
244  // //YOU SHALL NOT PASS!
245  // return TRUE;
246 
247  // return SoQtExaminerViewer::processSoEvent(event);
248  // }
249 }
armarx::Display::cameraViewAll
void cameraViewAll()
Definition: DisplayWidget.cpp:112
armarx::Display::~Display
~Display() override
Definition: DisplayWidget.cpp:99
armarx::DisplayWidget::DisplayWidget
DisplayWidget(QWidget *parent=0)
Definition: DisplayWidget.cpp:39
DisplayWidget.h
armarx::DisplayWidget::~DisplayWidget
~DisplayWidget() override
Definition: DisplayWidget.cpp:58
armarx::Display::cameraViewNode
void cameraViewNode(SoNode *node, const float slack=1.0)
Definition: DisplayWidget.cpp:117
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:28
armarx::Display::getRootNode
SoSeparator * getRootNode()
Definition: DisplayWidget.cpp:107