view_manager.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <stdio.h>
00031 
00032 #include <sstream>
00033 
00034 #include "rviz/display_context.h"
00035 #include "rviz/failed_view_controller.h"
00036 #include "rviz/properties/enum_property.h"
00037 #include "rviz/properties/property_tree_model.h"
00038 #include "rviz/render_panel.h"
00039 #include "rviz/view_controller.h"
00040 
00041 #include "rviz/view_manager.h"
00042 
00043 namespace rviz
00044 {
00045 
00046 ViewManager::ViewManager( DisplayContext* context )
00047   : context_( context )
00048   , root_property_( new ViewControllerContainer )
00049   , property_model_( new PropertyTreeModel( root_property_ ))
00050   , factory_( new PluginlibFactory<ViewController>( "rviz", "rviz::ViewController" ))
00051   , current_( NULL )
00052   , render_panel_( NULL )
00053 {
00054   property_model_->setDragDropClass( "view-controller" );
00055   connect( property_model_, SIGNAL( configChanged() ), this, SIGNAL( configChanged() ));
00056 }
00057 
00058 ViewManager::~ViewManager()
00059 {
00060   delete property_model_;
00061   delete factory_;
00062 }
00063 
00064 void ViewManager::initialize()
00065 {
00066   setCurrent( create( "rviz/Orbit" ), false );
00067 }
00068 
00069 void ViewManager::update( float wall_dt, float ros_dt )
00070 {
00071   if( getCurrent() )
00072   {
00073     getCurrent()->update( wall_dt, ros_dt );
00074   }
00075 }
00076 
00077 ViewController* ViewManager::create( const QString& class_id )
00078 {
00079   QString error;
00080   ViewController* view = factory_->make( class_id, &error );
00081   if( !view )
00082   {
00083     view = new FailedViewController( class_id, error );
00084   }
00085   view->initialize( context_ );
00086 
00087   return view;
00088 }
00089 
00090 ViewController* ViewManager::getCurrent() const
00091 {
00092   return current_;
00093 }
00094 
00095 void ViewManager::setCurrentFrom( ViewController* source_view )
00096 {
00097   if( source_view == NULL )
00098   {
00099     return;
00100   }
00101 
00102   ViewController* previous = getCurrent();
00103   if( source_view != previous )
00104   {
00105     ViewController* new_current = copy( source_view );
00106 
00107     setCurrent( new_current, false );
00108     Q_EMIT configChanged();
00109   }
00110 }
00111 
00112 void ViewManager::onCurrentDestroyed( QObject* obj )
00113 {
00114   if( obj == current_ )
00115   {
00116     current_ = NULL;
00117   }
00118 }
00119 
00120 void ViewManager::setCurrent( ViewController* new_current, bool mimic_view )
00121 {
00122   ViewController* previous = getCurrent();
00123   if( previous )
00124   {
00125     if( mimic_view )
00126     {
00127       new_current->mimic( previous );
00128     }
00129     else
00130     {
00131       new_current->transitionFrom( previous );
00132     }
00133     disconnect( previous, SIGNAL( destroyed( QObject* )), this, SLOT( onCurrentDestroyed( QObject* )));
00134   }
00135   new_current->setName( "Current View" );
00136   connect( new_current, SIGNAL( destroyed( QObject* )), this, SLOT( onCurrentDestroyed( QObject* )));
00137   current_ = new_current;
00138   root_property_->addChildToFront( new_current );
00139   delete previous;
00140 
00141   if( render_panel_ )
00142   {
00143     // This setViewController() can indirectly call
00144     // ViewManager::update(), so make sure getCurrent() will return the
00145     // new one by this point.
00146     render_panel_->setViewController( new_current );
00147   }
00148   Q_EMIT currentChanged();
00149 }
00150 
00151 void ViewManager::setCurrentViewControllerType( const QString& new_class_id )
00152 {
00153   setCurrent( create( new_class_id ), true );
00154 }
00155 
00156 void ViewManager::copyCurrentToList()
00157 {
00158   ViewController* current = getCurrent();
00159   if( current )
00160   {
00161     ViewController* new_copy = copy( current );
00162     new_copy->setName( factory_->getClassName( new_copy->getClassId() ));
00163     root_property_->addChild( new_copy );
00164   }
00165 }
00166 
00167 ViewController* ViewManager::getViewAt( int index ) const
00168 {
00169   if( index < 0 )
00170   {
00171     index = 0;
00172   }
00173   return qobject_cast<ViewController*>( root_property_->childAt( index + 1 ));
00174 }
00175 
00176 int ViewManager::getNumViews() const
00177 {
00178   int count = root_property_->numChildren();
00179   if( count <= 0 )
00180   {
00181     return 0;
00182   }
00183   else
00184   {
00185     return count-1;
00186   }
00187 }
00188 
00189 void ViewManager::add( ViewController* view, int index )
00190 {
00191   if( index < 0 )
00192   {
00193     index = root_property_->numChildren();
00194   }
00195   else
00196   {
00197     index++;
00198   }
00199   property_model_->getRoot()->addChild( view, index );
00200 }
00201 
00202 ViewController* ViewManager::take( ViewController* view )
00203 {
00204   for( int i = 0; i < getNumViews(); i++ )
00205   {
00206     if( getViewAt( i ) == view )
00207     {
00208       return qobject_cast<ViewController*>( root_property_->takeChildAt( i + 1 ));
00209     }
00210   }
00211   return NULL;
00212 }
00213 
00214 ViewController* ViewManager::takeAt( int index )
00215 {
00216   if( index < 0 )
00217   {
00218     return NULL;
00219   }
00220   return qobject_cast<ViewController*>( root_property_->takeChildAt( index + 1 ));
00221 }
00222 
00223 void ViewManager::load( const Config& config )
00224 {
00225   Config current_config = config.mapGetChild( "Current" );
00226   QString class_id;
00227   if( current_config.mapGetString( "Class", &class_id ))
00228   {
00229     ViewController* new_current = create( class_id );
00230     new_current->load( current_config );
00231     setCurrent( new_current, false );
00232   }
00233 
00234   Config saved_views_config = config.mapGetChild( "Saved" );
00235   root_property_->removeChildren( 1 );
00236   int num_saved = saved_views_config.listLength();
00237   for( int i = 0; i < num_saved; i++ )
00238   {
00239     Config view_config = saved_views_config.listChildAt( i );
00240 
00241     if( view_config.mapGetString( "Class", &class_id ))
00242     {
00243       ViewController* view = create( class_id );
00244       view->load( view_config );
00245       add( view );
00246     }
00247   }
00248 }
00249 
00250 void ViewManager::save( Config config ) const
00251 {
00252   getCurrent()->save( config.mapMakeChild( "Current" ));
00253 
00254   Config saved_views_config = config.mapMakeChild( "Saved" );
00255   for( int i = 0; i < getNumViews(); i++ )
00256   {
00257     getViewAt( i )->save( saved_views_config.listAppendNew() );
00258   }
00259 }
00260 
00261 ViewController* ViewManager::copy( ViewController* source )
00262 {
00263   Config config;
00264   source->save( config );
00265 
00266   ViewController* copy_of_source = create( source->getClassId() );
00267   copy_of_source->load( config );
00268 
00269   return copy_of_source;
00270 }
00271 
00272 void ViewManager::setRenderPanel( RenderPanel* render_panel )
00273 {
00274   render_panel_ = render_panel;
00275 }
00276 
00277 Qt::ItemFlags ViewControllerContainer::getViewFlags( int column ) const
00278 {
00279   return Property::getViewFlags( column ) | Qt::ItemIsDropEnabled;
00280 }
00281 
00282 void ViewControllerContainer::addChild( Property* child, int index )
00283 {
00284   if( index == 0 )
00285   {
00286     index = 1;
00287   }
00288   Property::addChild( child, index );
00289 }
00290 
00291 void ViewControllerContainer::addChildToFront( Property* child )
00292 {
00293   Property::addChild( child, 0 );
00294 }
00295 
00296 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28