views_panel.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 <QLabel>
00031 #include <QListWidget>
00032 #include <QComboBox>
00033 #include <QPushButton>
00034 #include <QVBoxLayout>
00035 #include <QHBoxLayout>
00036 #include <QInputDialog>
00037 
00038 #include <boost/bind.hpp>
00039 
00040 #include "visualization_manager.h"
00041 #include "view_controller.h"
00042 #include "config.h"
00043 
00044 #include "views_panel.h"
00045 
00046 namespace rviz
00047 {
00048 
00049 ViewsPanel::ViewsPanel( QWidget* parent )
00050   : QWidget( parent )
00051   , manager_( NULL )
00052 {
00053   camera_type_selector_ = new QComboBox;
00054   views_list_ = new QListWidget;
00055 
00056   QPushButton* save_button = new QPushButton( "Save Current" );
00057   QPushButton* load_button = new QPushButton( "Load" );
00058   QPushButton* delete_button = new QPushButton( "Delete" );
00059   QPushButton* zero_button = new QPushButton( "Zero" );
00060   zero_button->setToolTip( "Jump to 0,0,0 with the current view controller. Shortcut: Z" );
00061 
00062   QHBoxLayout* top_layout = new QHBoxLayout;
00063   top_layout->addWidget( new QLabel( "Type:" ));
00064   top_layout->addWidget( camera_type_selector_ );
00065   top_layout->addStretch();
00066   top_layout->addWidget( zero_button );
00067 
00068   QHBoxLayout* button_layout = new QHBoxLayout;
00069   button_layout->addWidget( save_button );
00070   button_layout->addWidget( load_button );
00071   button_layout->addWidget( delete_button );
00072 
00073   QVBoxLayout* main_layout = new QVBoxLayout;
00074   main_layout->addLayout( top_layout );
00075   main_layout->addWidget( views_list_ );
00076   main_layout->addLayout( button_layout );
00077   setLayout( main_layout );
00078 
00079   connect( save_button, SIGNAL( clicked() ), this, SLOT( onSaveClicked() ));
00080   connect( load_button, SIGNAL( clicked() ), this, SLOT( loadSelected() ));
00081   connect( delete_button, SIGNAL( clicked() ), this, SLOT( onDeleteClicked() ));
00082   connect( zero_button, SIGNAL( clicked() ), this, SLOT( onZeroClicked() ));
00083 
00084   connect( camera_type_selector_, SIGNAL( activated( int )), this, SLOT( onCameraTypeSelected( int )));
00085   connect( views_list_, SIGNAL( itemActivated( QListWidgetItem* )), this, SLOT( loadSelected() ));
00086 }
00087 
00088 ViewsPanel::~ViewsPanel()
00089 {
00090 }
00091 
00092 void ViewsPanel::initialize( VisualizationManager* manager )
00093 {
00094   manager_ = manager;
00095 
00096   connect( manager_, SIGNAL( displaysConfigLoaded( const boost::shared_ptr<Config>& )),
00097            this, SLOT( readFromConfig( const boost::shared_ptr<Config>& )));
00098   connect( manager_, SIGNAL( displaysConfigSaved( const boost::shared_ptr<Config>& )),
00099            this, SLOT( writeToConfig( const boost::shared_ptr<Config>& )));
00100   connect( manager_, SIGNAL( viewControllerTypeAdded( const std::string&, const std::string& )),
00101            this, SLOT( onViewControllerTypeAdded( const std::string&, const std::string& )));
00102   connect( manager_, SIGNAL( viewControllerChanged( ViewController* )),
00103            this, SLOT( onViewControllerChanged( ViewController* )));
00104 }
00105 
00106 void ViewsPanel::loadSelected()
00107 {
00108   int index = views_list_->currentRow();
00109   if( index >= 0 && index < (int) views_.size() )
00110   {
00111     const View& view = views_[ index ];
00112     manager_->setTargetFrame( view.target_frame_ );
00113     manager_->setCurrentViewControllerType( view.controller_class_ );
00114     manager_->getCurrentViewController()->fromString( view.controller_config_ );
00115     manager_->queueRender();
00116   }
00117 }
00118 
00119 void ViewsPanel::addView( const View& view )
00120 {
00121   views_.push_back( view );
00122 
00123   std::stringstream ss;
00124   ss << view.name_
00125      << "; Target=[" << view.target_frame_
00126      << "] Type=[" << view.controller_class_
00127      << "] Config=[" << view.controller_config_ << "]";
00128 
00129   views_list_->addItem( QString::fromStdString( ss.str() ));
00130 }
00131 
00132 void ViewsPanel::save( const std::string& name )
00133 {
00134   View view;
00135   view.target_frame_ = manager_->getTargetFrame();
00136   view.controller_class_ = manager_->getCurrentViewControllerType();
00137   view.name_ = name;
00138   view.controller_config_ = manager_->getCurrentViewController()->toString();
00139 
00140   addView( view );
00141   Q_EMIT configChanged();
00142 }
00143 
00144 void ViewsPanel::onViewControllerTypeAdded( const std::string& class_name, const std::string& name )
00145 {
00146   camera_type_selector_->addItem( QString::fromStdString( name ), QString::fromStdString( class_name ));
00147 
00148   if( camera_type_selector_->count() == 1 )
00149   {
00150     camera_type_selector_->setCurrentIndex( 0 );
00151   }
00152 }
00153 
00154 void ViewsPanel::onViewControllerChanged( ViewController* controller )
00155 {
00156   int count = camera_type_selector_->count();
00157   for( int i = 0; i < count; ++i )
00158   {
00159     QVariant type_var = camera_type_selector_->itemData( i );
00160     if( type_var.isValid() && controller->getClassName() == type_var.toString().toStdString() )
00161     {
00162       camera_type_selector_->setCurrentIndex( i );
00163       break;
00164     }
00165   }
00166 }
00167 
00168 void ViewsPanel::onCameraTypeSelected( int index )
00169 {
00170   QVariant type_var = camera_type_selector_->itemData( index );
00171   if( type_var.isValid() )
00172   {
00173     manager_->setCurrentViewControllerType( type_var.toString().toStdString() );
00174   }
00175 }
00176 
00177 void ViewsPanel::onSaveClicked()
00178 {
00179   bool ok;
00180   QString q_name = QInputDialog::getText( this, "Name the View", "Name",
00181                                           QLineEdit::Normal,
00182                                           "My View", &ok );
00183   if( ok && !q_name.isEmpty() )
00184   {
00185     save( q_name.toStdString() );
00186   }
00187 }
00188 
00189 void ViewsPanel::onZeroClicked()
00190 {
00191   if( manager_->getCurrentViewController() )
00192   {
00193     manager_->getCurrentViewController()->reset();
00194   }
00195 }
00196 
00197 void ViewsPanel::onDeleteClicked()
00198 {
00199   int index = views_list_->currentRow();
00200   if( index >= 0 && index < views_list_->count() )
00201   {
00202     views_.erase( views_.begin() + index );
00203     delete views_list_->item( index );
00204     Q_EMIT configChanged();
00205   }
00206 }
00207 
00208 void ViewsPanel::clear()
00209 {
00210   views_.clear();
00211   views_list_->clear();
00212 }
00213 
00214 void ViewsPanel::readFromConfig( const boost::shared_ptr<Config>& config )
00215 {
00216   clear();
00217 
00218   int i = 0;
00219   while( 1 )
00220   {
00221     std::stringstream type, target, cam_config, name;
00222     type << "Views/" << i << "/Type";
00223     target << "Views/" << i << "/Target";
00224     cam_config << "Views/" << i << "/Config";
00225     name << "Views/" << i << "Name";
00226 
00227     std::string view_type, view_name, view_target, view_config;
00228     if( !config->get( type.str(), &view_type ))
00229     {
00230       break;
00231     }
00232 
00233     if( !config->get( name.str(), &view_name ))
00234     {
00235       break;
00236     }
00237 
00238     if( !config->get( target.str(), &view_target ))
00239     {
00240       break;
00241     }
00242 
00243     if( !config->get( cam_config.str(), &view_config ))
00244     {
00245       break;
00246     }
00247 
00248     View view;
00249     view.name_ = view_name;
00250     view.controller_class_ = view_type;
00251     view.target_frame_ = view_target;
00252     view.controller_config_ = view_config;
00253 
00254     addView( view );
00255 
00256     ++i;
00257   }
00258 }
00259 
00260 void ViewsPanel::writeToConfig( const boost::shared_ptr<Config>& config )
00261 {
00262   V_View::const_iterator it = views_.begin();
00263   V_View::const_iterator end = views_.end();
00264   uint32_t i = 0;
00265   for (; it != end; ++it, ++i)
00266   {
00267     const View& view = *it;
00268 
00269     std::stringstream type, target, cam_config, name;
00270     type << "Views/" << i << "/Type";
00271     target << "Views/" << i << "/Target";
00272     cam_config << "Views/" << i << "/Config";
00273     name << "Views/" << i << "Name";
00274 
00275     config->set( name.str(), view.name_ );
00276     config->set( type.str(), view.controller_class_ );
00277     config->set( target.str(), view.target_frame_ );
00278     config->set( cam_config.str(), view.controller_config_ );
00279   }
00280 }
00281 
00282 } // namespace rviz
00283 


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:33