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