view_controller.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QColor>
31 #include <QFont>
32 #include <QKeyEvent>
33 
34 #include <OgreCamera.h>
35 #include <OgreSceneManager.h>
36 #include <OgreSceneNode.h>
37 
38 #include "rviz/display_context.h"
39 #include "rviz/frame_manager.h"
40 #include "rviz/load_resource.h"
44 #include "rviz/render_panel.h"
46 #include "rviz/view_manager.h"
50 
51 #include "rviz/view_controller.h"
52 
53 namespace rviz
54 {
56  : context_(nullptr), camera_(nullptr), is_active_(false), type_property_(nullptr)
57 {
59  new FloatProperty("Near Clip Distance", 0.01f,
60  "Anything closer to the camera than this threshold will not get rendered.", this,
61  SLOT(updateNearClipDistance()));
64 
65  stereo_enable_ = new BoolProperty("Enable Stereo Rendering", true,
66  "Render the main view in stereo if supported."
67  " On Linux this requires a recent version of Ogre and"
68  " an NVIDIA Quadro card with 3DVision glasses.",
69  this, SLOT(updateStereoProperties()));
70  stereo_eye_swap_ = new BoolProperty("Swap Stereo Eyes", false,
71  "Swap eyes if the monitor shows the left eye on the right.",
72  stereo_enable_, SLOT(updateStereoProperties()), this);
74  new FloatProperty("Stereo Eye Separation", 0.06f, "Distance between eyes for stereo rendering.",
75  stereo_enable_, SLOT(updateStereoProperties()), this);
76  stereo_focal_distance_ = new FloatProperty("Stereo Focal Distance", 1.0f,
77  "Distance from eyes to screen. For stereo rendering.",
78  stereo_enable_, SLOT(updateStereoProperties()), this);
79  invert_z_ =
80  new BoolProperty("Invert Z Axis", false, "Invert camera's Z axis for Z-down environments/models.",
81  this, SLOT(updateStereoProperties()));
82 }
83 
85 {
86  context_ = context;
87 
88  std::stringstream ss;
89  static int count = 0;
90  ss << "ViewControllerCamera" << count++;
91  camera_ = context_->getSceneManager()->createCamera(ss.str());
92  context_->getSceneManager()->getRootSceneNode()->attachObject(camera_);
93 
95  setReadOnly(true);
96 
97  // Do subclass initialization.
98  onInitialize();
99 
101 
103  standard_cursors_[Rotate2D] = makeIconCursor("package://rviz/icons/rotate.svg");
104  standard_cursors_[Rotate3D] = makeIconCursor("package://rviz/icons/rotate_cam.svg");
105  standard_cursors_[MoveXY] = makeIconCursor("package://rviz/icons/move2d.svg");
106  standard_cursors_[MoveZ] = makeIconCursor("package://rviz/icons/move_z.svg");
107  standard_cursors_[Zoom] = makeIconCursor("package://rviz/icons/zoom.svg");
108  standard_cursors_[Crosshair] = makeIconCursor("package://rviz/icons/crosshair.svg");
109 
112 
113  if (!RenderSystem::get()->isStereoSupported())
114  {
115  stereo_enable_->setBool(false);
116  stereo_enable_->hide();
117  }
118 }
119 
121 {
122  context_->getSceneManager()->destroyCamera(camera_);
123 }
124 
125 QString ViewController::formatClassId(const QString& class_id)
126 {
127  QStringList id_parts = class_id.split("/");
128  if (id_parts.size() != 2)
129  {
130  // Should never happen with pluginlib class ids, which are
131  // formatted like "package_name/class_name". Not worth crashing
132  // over though.
133  return class_id;
134  }
135  else
136  {
137  return id_parts[1] + " (" + id_parts[0] + ")";
138  }
139 }
140 
141 QVariant ViewController::getViewData(int column, int role) const
142 {
143  if (role == Qt::TextColorRole)
144  return QVariant();
145 
146  if (is_active_)
147  {
148  switch (role)
149  {
150  case Qt::FontRole:
151  {
152  QFont font;
153  font.setBold(true);
154  return font;
155  }
156  }
157  }
158  return Property::getViewData(column, role);
159 }
160 
161 Qt::ItemFlags ViewController::getViewFlags(int column) const
162 {
163  if (is_active_)
164  {
165  return Property::getViewFlags(column);
166  }
167  else
168  {
169  return Property::getViewFlags(column) | Qt::ItemIsDragEnabled;
170  }
171 }
172 
174 {
175  is_active_ = true;
176  onActivate();
177 }
178 
180 {
181  Q_EMIT configChanged();
182 }
183 
184 void ViewController::load(const Config& config)
185 {
186  // Load the name by hand.
187  QString name;
188  if (config.mapGetString("Name", &name))
189  {
190  setName(name);
191  }
192  // Load all sub-properties the same way the base class does.
193  Property::load(config);
194 }
195 
196 void ViewController::save(Config config) const
197 {
198  config.mapSetValue("Class", getClassId());
199  config.mapSetValue("Name", getName());
200 
201  Property::save(config);
202 }
203 
204 void ViewController::handleKeyEvent(QKeyEvent* event, RenderPanel* panel)
205 {
206  if (event->key() == Qt::Key_F && panel->getViewport() && context_->getSelectionManager())
207  {
208  QPoint mouse_rel_panel = panel->mapFromGlobal(QCursor::pos());
209  Ogre::Vector3 point_rel_world; // output of get3DPoint().
210  if (context_->getSelectionManager()->get3DPoint(panel->getViewport(), mouse_rel_panel.x(),
211  mouse_rel_panel.y(), point_rel_world))
212  {
213  lookAt(point_rel_world);
214  }
215  }
216 
217  if (event->key() == Qt::Key_Z)
218  {
219  reset();
220  }
221 }
222 
224 {
225  cursor_ = standard_cursors_[cursor_type];
226 }
227 
228 void ViewController::lookAt(float x, float y, float z)
229 {
230  Ogre::Vector3 point(x, y, z);
231  lookAt(point);
232 }
233 
234 void ViewController::setStatus(const QString& message)
235 {
236  if (context_)
237  {
238  context_->setStatus(message);
239  }
240 }
241 
243 {
244  float n = near_clip_property_->getFloat();
245  camera_->setNearClipDistance(n);
246 }
247 
249 {
250  if (stereo_enable_->getBool())
251  {
252  float focal_dist = stereo_focal_distance_->getFloat();
253  float eye_sep = stereo_eye_swap_->getBool() ? -stereo_eye_separation_->getFloat() :
255  camera_->setFrustumOffset(0.5f * eye_sep, 0.0f);
256  camera_->setFocalLength(focal_dist);
260  }
261  else
262  {
263  camera_->setFrustumOffset(0.0f, 0.0f);
264  camera_->setFocalLength(1.0f);
268  }
269 }
270 
272 {
273  // We don't seem to need to do anything here.
274 }
275 
276 } // end namespace rviz
virtual void setStatus(const QString &message)=0
void setMin(float min)
void setMax(float max)
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
Definition: property.cpp:490
f
QCursor makeIconCursor(QString url, bool fill_cache)
static RenderSystem * get()
QVariant getViewData(int column, int role) const override
Overridden from Property to give a different background color and bold font if this view is active...
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
Definition: property.cpp:440
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
Definition: property.cpp:134
void emitConfigChanged()
Subclasses should call this whenever a change is made which would change the results of toString()...
Qt::ItemFlags getViewFlags(int column) const override
Overridden from Property to make this draggable if it is not active.
void setCursor(CursorType cursor_type)
void lookAt(float x, float y, float z)
Convenience function which calls lookAt(Ogre::Vector3).
FloatProperty * stereo_focal_distance_
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
QCursor getDefaultCursor(bool)
static QString formatClassId(const QString &class_id)
BoolProperty * invert_z_
Ogre::Camera * camera_
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:293
Property specialized to enforce floating point max/min.
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:196
Configuration data storage class.
Definition: config.h:124
Pure-virtual base class for objects which give Display subclasses context in which to work...
virtual QVariant getViewData(int column, int role) const
Return data appropriate for the given column (0 or 1) and role for this Property. ...
Definition: property.cpp:241
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:289
void show()
Show this Property in any PropertyTreeWidgets.
Definition: property.h:410
FloatProperty * stereo_eye_separation_
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:164
void setStatus(const QString &message)
Ogre::Viewport * getViewport() const
virtual SelectionManager * getSelectionManager() const =0
Return a pointer to the SelectionManager.
BoolProperty * stereo_eye_swap_
virtual void onInitialize()
Do subclass-specific initialization. Called by ViewController::initialize after context_ and camera_ ...
void load(const Config &config) override
Load the value of this property and/or its children from the given Config reference.
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
virtual float getFloat() const
virtual void onActivate()
called by activate().
FloatProperty * near_clip_property_
virtual void reset()=0
BoolProperty * stereo_enable_
virtual void handleKeyEvent(QKeyEvent *event, RenderPanel *panel)
Called by MoveTool and InteractionTool when keyboard events are passed to them.
QMap< CursorType, QCursor > standard_cursors_
void hide()
Hide this Property in any PropertyTreeWidgets.
Definition: property.h:401
void initialize(DisplayContext *context)
Do all setup that can&#39;t be done in the constructor.
void activate()
Called by RenderPanel when this view controller is about to be used.
bool get3DPoint(Ogre::Viewport *viewport, const int x, const int y, Ogre::Vector3 &result_point)
virtual bool getBool() const
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:436
DisplayContext * context_
bool setBool(bool value)
Definition: bool_property.h:62


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25