$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 "new_display_dialog.h" 00031 00032 #include <sstream> 00033 00034 #include <wx/msgdlg.h> 00035 00036 namespace rviz 00037 { 00038 00039 class NewDisplayDialogTreeItemData : public wxTreeItemData 00040 { 00041 public: 00042 int32_t index; 00043 }; 00044 00045 struct DisplayTypeInfoComparator 00046 { 00047 bool operator()(const DisplayTypeInfoPtr& lhs, const DisplayTypeInfoPtr& rhs) const 00048 { 00049 return lhs->display_name < rhs->display_name; 00050 } 00051 }; 00052 00053 NewDisplayDialog::NewDisplayDialog( wxWindow* parent, const L_Plugin& plugins, const S_string& current_display_names ) 00054 : NewDisplayDialogGenerated( parent ) 00055 , current_display_names_(current_display_names) 00056 { 00057 wxTreeItemId root = types_->AddRoot(wxT("")); 00058 00059 L_Plugin::const_iterator pit = plugins.begin(); 00060 L_Plugin::const_iterator pend = plugins.end(); 00061 for (; pit != pend; ++pit) 00062 { 00063 const PluginPtr& plugin = *pit; 00064 00065 if (!plugin->isLoaded()) 00066 { 00067 continue; 00068 } 00069 00070 wxTreeItemId parent = types_->AppendItem(root, wxString::FromAscii(plugin->getName().c_str())); 00071 00072 L_DisplayTypeInfo typeinfo_list = plugin->getDisplayTypeInfoList(); 00073 typeinfo_list.sort(DisplayTypeInfoComparator()); 00074 00075 L_DisplayTypeInfo::const_iterator it = typeinfo_list.begin(); 00076 L_DisplayTypeInfo::const_iterator end = typeinfo_list.end(); 00077 for ( ; it != end; ++it ) 00078 { 00079 const DisplayTypeInfoPtr& info = *it; 00080 00081 NewDisplayDialogTreeItemData* data = new NewDisplayDialogTreeItemData; 00082 data->index = typeinfo_.size(); 00083 types_->AppendItem( parent, wxString::FromAscii( info->display_name.c_str() ), -1, -1, data ); 00084 00085 DisplayTypeInfoWithPlugin t; 00086 t.plugin = plugin; 00087 t.typeinfo = info; 00088 typeinfo_.push_back(t); 00089 } 00090 } 00091 00092 type_description_->Connect(wxEVT_COMMAND_HTML_LINK_CLICKED, wxHtmlLinkEventHandler(NewDisplayDialog::onLinkClicked), NULL, this); 00093 00094 types_->ExpandAll(); 00095 } 00096 00097 int32_t NewDisplayDialog::getSelectionIndex() 00098 { 00099 wxTreeItemId sel = types_->GetSelection(); 00100 if (!sel.IsOk()) 00101 { 00102 return -1; 00103 } 00104 00105 NewDisplayDialogTreeItemData* data = (NewDisplayDialogTreeItemData*)types_->GetItemData(sel); 00106 if (!data) 00107 { 00108 return -1; 00109 } 00110 00111 int32_t index = data->index; 00112 return index; 00113 } 00114 00115 void NewDisplayDialog::onDisplaySelected( wxTreeEvent& event ) 00116 { 00117 int32_t index = getSelectionIndex(); 00118 if (index < 0) 00119 { 00120 name_->SetValue(wxT("")); 00121 type_description_->SetPage(wxT("")); 00122 return; 00123 } 00124 00125 const DisplayTypeInfoPtr& info = typeinfo_[index].typeinfo; 00126 type_description_->SetPage( wxString::FromAscii( info->help_description.c_str() ) ); 00127 00128 int counter = 1; 00129 std::string name; 00130 do 00131 { 00132 std::stringstream ss; 00133 ss << info->display_name; 00134 00135 if (counter > 1) 00136 { 00137 ss << counter; 00138 } 00139 00140 ++counter; 00141 00142 name = ss.str(); 00143 } while(current_display_names_.find(name) != current_display_names_.end()); 00144 00145 name_->SetValue(wxString::FromAscii(name.c_str())); 00146 00147 Layout(); 00148 } 00149 00150 void NewDisplayDialog::onLinkClicked(wxHtmlLinkEvent& event) 00151 { 00152 wxLaunchDefaultBrowser(event.GetLinkInfo().GetHref()); 00153 } 00154 00155 void NewDisplayDialog::onDisplayDClick( wxMouseEvent& event ) 00156 { 00157 int32_t index = getSelectionIndex(); 00158 if (index < 0) 00159 { 00160 return; 00161 } 00162 00163 if ( name_->GetValue().IsEmpty() ) 00164 { 00165 wxMessageBox( wxT("You must enter a name!"), wxT("No name"), wxICON_ERROR | wxOK, this ); 00166 return; 00167 } 00168 00169 EndModal(wxOK); 00170 } 00171 00172 void NewDisplayDialog::onOK( wxCommandEvent& event ) 00173 { 00174 int32_t index = getSelectionIndex(); 00175 if (index < 0) 00176 { 00177 wxMessageBox( wxT("You must select a type!"), wxT("No selection"), wxICON_ERROR | wxOK, this ); 00178 return; 00179 } 00180 00181 if ( name_->GetValue().IsEmpty() ) 00182 { 00183 wxMessageBox( wxT("You must enter a name!"), wxT("No name"), wxICON_ERROR | wxOK, this ); 00184 return; 00185 } 00186 00187 std::string name = (const char*)name_->GetValue().fn_str(); 00188 if (current_display_names_.find(name) != current_display_names_.end()) 00189 { 00190 wxMessageBox( wxT("A display with that name already exists!"), wxT("Name conflict"), wxICON_ERROR | wxOK, this ); 00191 return; 00192 } 00193 00194 EndModal(wxOK); 00195 } 00196 00197 void NewDisplayDialog::onCancel( wxCommandEvent& event ) 00198 { 00199 EndModal(wxCANCEL); 00200 } 00201 00202 void NewDisplayDialog::onNameEnter( wxCommandEvent& event ) 00203 { 00204 onOK( event ); 00205 } 00206 00207 std::string NewDisplayDialog::getClassName() 00208 { 00209 int32_t index = getSelectionIndex(); 00210 if (index < 0) 00211 { 00212 return ""; 00213 } 00214 00215 return typeinfo_[index].typeinfo->class_name; 00216 } 00217 00218 std::string NewDisplayDialog::getDisplayName() 00219 { 00220 return (const char*)name_->GetValue().mb_str(); 00221 } 00222 00223 std::string NewDisplayDialog::getPackageName() 00224 { 00225 int32_t index = getSelectionIndex(); 00226 if (index < 0) 00227 { 00228 return ""; 00229 } 00230 00231 return typeinfo_[index].plugin->getPackageName(); 00232 } 00233 00234 } // rviz 00235