$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 "views_panel.h" 00031 #include "render_panel.h" 00032 #include "visualization_manager.h" 00033 #include "view_controller.h" 00034 00035 #include <wx/textdlg.h> 00036 #include <wx/confbase.h> 00037 00038 #include <boost/bind.hpp> 00039 00040 namespace rviz 00041 { 00042 00043 class StringClientData : public wxClientData 00044 { 00045 public: 00046 StringClientData(const std::string s) 00047 : str(s) 00048 {} 00049 00050 std::string str; 00051 }; 00052 00053 ViewsPanel::ViewsPanel( wxWindow* parent ) 00054 : ViewsPanelGenerated( parent ) 00055 , manager_(NULL) 00056 { 00057 } 00058 00059 ViewsPanel::~ViewsPanel() 00060 { 00061 00062 } 00063 00064 void ViewsPanel::initialize(VisualizationManager* manager) 00065 { 00066 manager_ = manager; 00067 00068 manager_->getGeneralConfigLoadedSignal().connect( boost::bind( &ViewsPanel::onGeneralConfigLoaded, this, _1 ) ); 00069 manager_->getGeneralConfigSavingSignal().connect( boost::bind( &ViewsPanel::onGeneralConfigSaving, this, _1 ) ); 00070 manager_->getViewControllerTypeAddedSignal().connect( boost::bind( &ViewsPanel::onViewControllerTypeAdded, this, _1, _2 ) ); 00071 manager_->getViewControllerTypeChangedSignal().connect( boost::bind( &ViewsPanel::onViewControllerTypeChanged, this, _1 ) ); 00072 } 00073 00074 void ViewsPanel::loadSelected() 00075 { 00076 int index = views_list_->GetSelection(); 00077 if (index != wxNOT_FOUND) 00078 { 00079 const View& view = views_[index]; 00080 manager_->setTargetFrame(view.target_frame_); 00081 manager_->setCurrentViewControllerType(view.controller_class_); 00082 manager_->getCurrentViewController()->fromString(view.controller_config_); 00083 manager_->queueRender(); 00084 } 00085 } 00086 00087 void ViewsPanel::addView(const View& view) 00088 { 00089 views_.push_back(view); 00090 00091 std::stringstream ss; 00092 ss << view.name_ << "; Target=[" << view.target_frame_ << "] Type=[" << view.controller_class_ << "] Config=[" << view.controller_config_ << "]"; 00093 00094 views_list_->Append(wxString::FromAscii(ss.str().c_str())); 00095 } 00096 00097 void ViewsPanel::save(const std::string& name) 00098 { 00099 View view; 00100 view.target_frame_ = manager_->getTargetFrame(); 00101 view.controller_class_ = manager_->getCurrentViewControllerType(); 00102 view.name_ = name; 00103 view.controller_config_ = manager_->getCurrentViewController()->toString(); 00104 00105 addView(view); 00106 } 00107 00108 void ViewsPanel::onViewControllerTypeAdded(const std::string& class_name, const std::string& name) 00109 { 00110 camera_types_->Append( wxString::FromAscii(name.c_str()) ); 00111 camera_types_->SetClientObject(camera_types_->GetCount() - 1, new StringClientData(class_name)); 00112 00113 if (camera_types_->GetCount() == 1) 00114 { 00115 camera_types_->SetSelection( 0 ); 00116 } 00117 } 00118 00119 void ViewsPanel::onViewControllerTypeChanged(ViewController* controller) 00120 { 00121 int count = camera_types_->GetCount(); 00122 for (int i = 0; i < count; ++i) 00123 { 00124 StringClientData* data = (StringClientData*)camera_types_->GetClientObject(i); 00125 if (data->str == controller->getClassName()) 00126 { 00127 camera_types_->SetSelection(i); 00128 break; 00129 } 00130 } 00131 } 00132 00133 void ViewsPanel::onCameraTypeSelected( wxCommandEvent& event ) 00134 { 00135 StringClientData* data = (StringClientData*)camera_types_->GetClientObject(camera_types_->GetSelection()); 00136 manager_->setCurrentViewControllerType(data->str); 00137 } 00138 00139 void ViewsPanel::onViewsClicked( wxCommandEvent& event ) 00140 { 00141 } 00142 00143 void ViewsPanel::onViewsDClicked( wxCommandEvent& event ) 00144 { 00145 loadSelected(); 00146 } 00147 00148 void ViewsPanel::onLoadClicked( wxCommandEvent& event ) 00149 { 00150 loadSelected(); 00151 } 00152 00153 void ViewsPanel::onSaveClicked( wxCommandEvent& event ) 00154 { 00155 wxString name = wxGetTextFromUser(wxT("Name the View"), wxT("Name"), wxT("My View"), this); 00156 00157 if (!name.IsEmpty()) 00158 { 00159 save((const char*)name.fn_str()); 00160 } 00161 } 00162 00163 void ViewsPanel::onDeleteClicked( wxCommandEvent& event ) 00164 { 00165 int index = views_list_->GetSelection(); 00166 if (index != wxNOT_FOUND) 00167 { 00168 views_.erase(views_.begin() + index); 00169 views_list_->Delete(index); 00170 } 00171 } 00172 00173 void ViewsPanel::onGeneralConfigLoaded(const boost::shared_ptr<wxConfigBase>& config) 00174 { 00175 int i = 0; 00176 while (1) 00177 { 00178 wxString type, target, cam_config, name; 00179 type.Printf( wxT("Views/%d/Type"), i ); 00180 target.Printf( wxT("Views/%d/Target"), i ); 00181 cam_config.Printf( wxT("Views/%d/Config"), i ); 00182 name.Printf( wxT("Views/%d/Name"), i ); 00183 00184 wxString view_type, view_name, view_target, view_config; 00185 if ( !config->Read( type, &view_type ) ) 00186 { 00187 break; 00188 } 00189 00190 if ( !config->Read( name, &view_name ) ) 00191 { 00192 break; 00193 } 00194 00195 if ( !config->Read( target, &view_target ) ) 00196 { 00197 break; 00198 } 00199 00200 if ( !config->Read( cam_config, &view_config ) ) 00201 { 00202 break; 00203 } 00204 00205 View view; 00206 view.name_ = (const char*)view_name.fn_str(); 00207 view.controller_class_ = (const char*)view_type.fn_str(); 00208 view.target_frame_ = (const char*)view_target.fn_str(); 00209 view.controller_config_ = (const char*)view_config.fn_str(); 00210 00211 addView(view); 00212 00213 ++i; 00214 } 00215 } 00216 00217 void ViewsPanel::onGeneralConfigSaving(const boost::shared_ptr<wxConfigBase>& config) 00218 { 00219 V_View::const_iterator it = views_.begin(); 00220 V_View::const_iterator end = views_.end(); 00221 uint32_t i = 0; 00222 for (; it != end; ++it, ++i) 00223 { 00224 const View& view = *it; 00225 00226 wxString type, target, cam_config, name; 00227 type.Printf( wxT("Views/%d/Type"), i ); 00228 target.Printf( wxT("Views/%d/Target"), i ); 00229 cam_config.Printf( wxT("Views/%d/Config"), i ); 00230 name.Printf( wxT("Views/%d/Name"), i ); 00231 00232 config->Write(name, wxString::FromAscii(view.name_.c_str())); 00233 config->Write(type, wxString::FromAscii(view.controller_class_.c_str())); 00234 config->Write(target, wxString::FromAscii(view.target_frame_.c_str())); 00235 config->Write(cam_config, wxString::FromAscii(view.controller_config_.c_str())); 00236 } 00237 } 00238 00239 } // namespace rviz 00240