00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 }
00235