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 {
55 
57  : context_( NULL )
58  , camera_( NULL )
59  , is_active_( false )
60  , type_property_( NULL )
61 {
62  near_clip_property_ = new FloatProperty( "Near Clip Distance", 0.01f,
63  "Anything closer to the camera than this threshold will not get rendered.",
64  this, SLOT( updateNearClipDistance() ) );
65  near_clip_property_->setMin( 0.001 );
66  near_clip_property_->setMax( 10000 );
67 
68  stereo_enable_ = new BoolProperty( "Enable Stereo Rendering", true,
69  "Render the main view in stereo if supported."
70  " On Linux this requires a recent version of Ogre and"
71  " an NVIDIA Quadro card with 3DVision glasses.",
72  this, SLOT( updateStereoProperties() ) );
73  stereo_eye_swap_ = new BoolProperty( "Swap Stereo Eyes", false,
74  "Swap eyes if the monitor shows the left eye on the right.",
75  stereo_enable_, SLOT( updateStereoProperties() ), this );
76  stereo_eye_separation_ = new FloatProperty( "Stereo Eye Separation", 0.06f,
77  "Distance between eyes for stereo rendering.",
78  stereo_enable_, SLOT( updateStereoProperties() ), this );
79  stereo_focal_distance_ = new FloatProperty( "Stereo Focal Distance", 1.0f,
80  "Distance from eyes to screen. For stereo rendering.",
81  stereo_enable_, SLOT( updateStereoProperties() ), this );
82  invert_z_ = new BoolProperty( "Invert Z Axis", false,
83  "Invert camera's Z axis for Z-down environments/models.",
84  this, SLOT( updateStereoProperties() ) );
85 }
86 
88 {
89  context_ = context;
90 
91  std::stringstream ss;
92  static int count = 0;
93  ss << "ViewControllerCamera" << count++;
94  camera_ = context_->getSceneManager()->createCamera( ss.str() );
95  context_->getSceneManager()->getRootSceneNode()->attachObject( camera_ );
96 
98  setReadOnly( true );
99 
100  // Do subclass initialization.
101  onInitialize();
102 
104 
106  standard_cursors_[Rotate2D] = makeIconCursor( "package://rviz/icons/rotate.svg" );
107  standard_cursors_[Rotate3D] = makeIconCursor( "package://rviz/icons/rotate_cam.svg" );
108  standard_cursors_[MoveXY] = makeIconCursor( "package://rviz/icons/move2d.svg" );
109  standard_cursors_[MoveZ] = makeIconCursor( "package://rviz/icons/move_z.svg" );
110  standard_cursors_[Zoom] = makeIconCursor( "package://rviz/icons/zoom.svg" );
111  standard_cursors_[Crosshair] = makeIconCursor( "package://rviz/icons/crosshair.svg" );
112 
115 
116  if (!RenderSystem::get()->isStereoSupported())
117  {
118  stereo_enable_->setBool(false);
119  stereo_enable_->hide();
120  }
121 }
122 
124 {
125  context_->getSceneManager()->destroyCamera( camera_ );
126 }
127 
128 QString ViewController::formatClassId( const QString& class_id )
129 {
130  QStringList id_parts = class_id.split( "/" );
131  if( id_parts.size() != 2 )
132  {
133  // Should never happen with pluginlib class ids, which are
134  // formatted like "package_name/class_name". Not worth crashing
135  // over though.
136  return class_id;
137  }
138  else
139  {
140  return id_parts[ 1 ] + " (" + id_parts[ 0 ] + ")";
141  }
142 }
143 
144 QVariant ViewController::getViewData( int column, int role ) const
145 {
146  if ( role == Qt::TextColorRole )
147  {
148  return QVariant();
149  }
150 
151  if( is_active_ )
152  {
153  switch( role )
154  {
155  case Qt::BackgroundRole:
156  {
157  return QColor( 0xba, 0xad, 0xa4 );
158  }
159  case Qt::FontRole:
160  {
161  QFont font;
162  font.setBold( true );
163  return font;
164  }
165  }
166  }
167  return Property::getViewData( column, role );
168 }
169 
170 Qt::ItemFlags ViewController::getViewFlags( int column ) const
171 {
172  if( is_active_ )
173  {
174  return Property::getViewFlags( column );
175  }
176  else
177  {
178  return Property::getViewFlags( column ) | Qt::ItemIsDragEnabled;
179  }
180 }
181 
183 {
184  is_active_ = true;
185  onActivate();
186 }
187 
189 {
190  Q_EMIT configChanged();
191 }
192 
193 void ViewController::load( const Config& config )
194 {
195  // Load the name by hand.
196  QString name;
197  if( config.mapGetString( "Name", &name ))
198  {
199  setName( name );
200  }
201  // Load all sub-properties the same way the base class does.
202  Property::load( config );
203 }
204 
205 void ViewController::save( Config config ) const
206 {
207  config.mapSetValue( "Class", getClassId() );
208  config.mapSetValue( "Name", getName() );
209 
210  Property::save( config );
211 }
212 
213 void ViewController::handleKeyEvent( QKeyEvent* event, RenderPanel* panel )
214 {
215  if( event->key() == Qt::Key_F &&
216  panel->getViewport() &&
218  {
219  QPoint mouse_rel_panel = panel->mapFromGlobal( QCursor::pos() );
220  Ogre::Vector3 point_rel_world; // output of get3DPoint().
222  mouse_rel_panel.x(), mouse_rel_panel.y(),
223  point_rel_world ))
224  {
225  lookAt( point_rel_world );
226  }
227  }
228 
229  if( event->key() == Qt::Key_Z )
230  {
231  reset();
232  }
233 }
234 
236 {
237  cursor_=standard_cursors_[cursor_type];
238 }
239 
240 void ViewController::lookAt( float x, float y, float z )
241 {
242  Ogre::Vector3 point( x, y, z );
243  lookAt( point );
244 }
245 
246 void ViewController::setStatus( const QString & message )
247 {
248  if ( context_ )
249  {
250  context_->setStatus( message );
251  }
252 }
253 
255 {
256  float n = near_clip_property_->getFloat();
257  camera_->setNearClipDistance( n );
258 }
259 
261 {
262  if (stereo_enable_->getBool())
263  {
264  float focal_dist = stereo_focal_distance_->getFloat();
265  float eye_sep = stereo_eye_swap_->getBool() ?
268  camera_->setFrustumOffset(0.5f * eye_sep, 0.0f);
269  camera_->setFocalLength(focal_dist);
273  }
274  else
275  {
276  camera_->setFrustumOffset(0.0f,0.0f);
277  camera_->setFocalLength(1.0f);
281  }
282 }
283 
285 {
286  // We don't seem to need to do anything here.
287 }
288 
289 } // end namespace rviz
virtual void setStatus(const QString &message)=0
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
#define NULL
Definition: global.h:37
QMap< CursorType, QCursor > standard_cursors_
void setMin(float min)
void setMax(float max)
f
QCursor makeIconCursor(QString url, bool fill_cache)
static RenderSystem * get()
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
Definition: property.cpp:426
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:130
void emitConfigChanged()
Subclasses should call this whenever a change is made which would change the results of toString()...
void setCursor(CursorType cursor_type)
virtual float getFloat() const
void lookAt(float x, float y, float z)
Convenience function which calls lookAt(Ogre::Vector3).
FloatProperty * stereo_focal_distance_
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:236
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:150
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
static QString formatClassId(const QString &class_id)
BoolProperty * invert_z_
Ogre::Camera * camera_
Property specialized to enforce floating point max/min.
virtual Qt::ItemFlags getViewFlags(int column) const
Overridden from Property to make this draggable if it is not active.
bool mapGetString(const QString &key, QString *value_out) const
Convenience function for looking up a named string.
Definition: config.cpp:281
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
void mapSetValue(const QString &key, QVariant value)
Set a named child to the given value.
Definition: config.cpp:185
Configuration data storage class.
Definition: config.h:125
virtual bool getBool() const
Pure-virtual base class for objects which give Display subclasses context in which to work...
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
Definition: property.cpp:467
void show()
Show this Property in any PropertyTreeWidgets.
Definition: property.h:377
FloatProperty * stereo_eye_separation_
void setStatus(const QString &message)
QCursor getDefaultCursor(bool fill_cache)
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_ ...
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
Ogre::Viewport * getViewport() const
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
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.
void hide()
Hide this Property in any PropertyTreeWidgets.
Definition: property.h:371
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 void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:397
DisplayContext * context_
bool setBool(bool value)
Definition: bool_property.h:62
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:159
virtual QVariant getViewData(int column, int role) const
Overridden from Property to give a different background color and bold font if this view is active...
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:285


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:42