Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * retriever_config_plugin.cpp - Config plugin for the retriever plugin 00004 * 00005 * Created: Sun Mar 29 13:59:28 2009 00006 * Copyright 2009 Daniel Beck 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "retriever_config_plugin.h" 00024 00025 #include <config/config.h> 00026 #include <core/exceptions/software.h> 00027 #include <fvutils/system/camargp.h> 00028 00029 using namespace std; 00030 using namespace fawkes; 00031 using namespace firevision; 00032 00033 /** @class RetrieverConfigDialog "retriever_config_plugin.h" 00034 * Config dialog of the config editor plugin for the fvretriever. 00035 * @author Daniel Beck 00036 */ 00037 00038 /** Constructor. 00039 * Allows to construct a dialog by means of get_widget_derived( ... ). 00040 * @param cobject base object pointer 00041 * @param ref_xml Glade XML object representing the Glade input file 00042 */ 00043 RetrieverConfigDialog::RetrieverConfigDialog( BaseObjectType* cobject, 00044 const Glib::RefPtr< Gnome::Glade::Xml >& ref_xml ) 00045 : Gtk::Dialog( cobject ) 00046 { 00047 ref_xml->get_widget("trvCameras", m_trv_cameras); 00048 ref_xml->get_widget("btnAdd", m_btn_add_camera); 00049 ref_xml->get_widget("btnDelete", m_btn_delete_camera); 00050 00051 m_btn_add_camera->signal_clicked().connect( sigc::mem_fun( *this, &RetrieverConfigDialog::on_add_clicked ) ); 00052 m_btn_delete_camera->signal_clicked().connect( sigc::mem_fun( *this, &RetrieverConfigDialog::on_delete_clicked ) ); 00053 00054 m_camera_list = Gtk::ListStore::create( m_camera_record ); 00055 m_trv_cameras->set_model( m_camera_list ); 00056 m_trv_cameras->append_column_editable( "Name", m_camera_record.name ); 00057 m_trv_cameras->append_column_editable( "Type", m_camera_record.type ); 00058 m_trv_cameras->append_column_editable( "Id", m_camera_record.id ); 00059 m_trv_cameras->append_column_editable( "Parameter", m_camera_record.params ); 00060 m_trv_cameras->append_column_editable( "Record", m_camera_record.record_images ); 00061 m_trv_cameras->append_column_editable( "Save path", m_camera_record.save_path ); 00062 } 00063 00064 /** Destructor. */ 00065 RetrieverConfigDialog::~RetrieverConfigDialog() 00066 { 00067 } 00068 00069 /** Adds a camera to the list of cameras. 00070 * @param camera_name an arbitrary name to identify the camera 00071 * @param camera_string a camera string that can be parsed by a CameraArgumentParser 00072 * @param record_images if true the images of that camera are saved 00073 * @param save_path the directory where the images are saved 00074 */ 00075 void 00076 RetrieverConfigDialog::add_camera( string camera_name, 00077 string camera_string, 00078 bool record_images, 00079 string save_path ) 00080 { 00081 CameraArgumentParser* argp = new CameraArgumentParser( camera_string.c_str() ); 00082 00083 string cam_type = argp->cam_type(); 00084 string cam_id = argp->cam_id(); 00085 string params; 00086 std::map< string, string > param_map = argp->parameters(); 00087 00088 std::map< string, string >::iterator i = param_map.begin(); 00089 while ( i != param_map.end() ) 00090 { 00091 params += i->first + "=" + i->second; 00092 00093 if ( ++i != param_map.end() ) 00094 { params += ":"; } 00095 } 00096 00097 Gtk::TreeModel::Row row = *m_camera_list->append(); 00098 row[ m_camera_record.name ] = camera_name; 00099 row[ m_camera_record.type ] = cam_type; 00100 row[ m_camera_record.id ] = cam_id; 00101 row[ m_camera_record.params ] = params; 00102 row[ m_camera_record.record_images ] = record_images; 00103 row[ m_camera_record.save_path ] = save_path; 00104 00105 delete argp; 00106 } 00107 00108 /** Obtain the list of cameras shown in the dialog. 00109 * @return a map camera name => camera string 00110 */ 00111 std::map< string, string > 00112 RetrieverConfigDialog::get_cameras() const 00113 { 00114 std::map< string, string > cameras; 00115 00116 Gtk::TreeModel::Row row; 00117 Glib::ustring name; 00118 Glib::ustring cam_string; 00119 00120 for ( Gtk::TreeIter i = m_camera_list->children().begin(); 00121 i != m_camera_list->children().end(); 00122 ++i ) 00123 { 00124 row = *i; 00125 name = row[ m_camera_record.name ]; 00126 cam_string = row[ m_camera_record.type ] + ":" + 00127 row[ m_camera_record.id ] + ":" + 00128 row[ m_camera_record.params ]; 00129 00130 cameras[ name ] = cam_string; 00131 } 00132 00133 return cameras; 00134 } 00135 00136 void 00137 RetrieverConfigDialog::on_add_clicked() 00138 { 00139 // add empty row and select it 00140 Gtk::TreeIter iter = m_camera_list->append(); 00141 Gtk::TreeModel::Row row = *iter; 00142 row[ m_camera_record.name ] = ""; 00143 row[ m_camera_record.type ] = ""; 00144 row[ m_camera_record.id ] = ""; 00145 row[ m_camera_record.params ] = ""; 00146 row[ m_camera_record.record_images ] = false; 00147 row[ m_camera_record.save_path ] = ""; 00148 00149 m_trv_cameras->set_cursor( m_camera_list->get_path( iter ) ); 00150 } 00151 00152 void 00153 RetrieverConfigDialog::on_delete_clicked() 00154 { 00155 Gtk::TreeIter iter = m_trv_cameras->get_selection()->get_selected(); 00156 m_camera_list->erase( iter ); 00157 } 00158 00159 00160 /** @class RetrieverConfigPlugin tools/config_editor/retriever_config_plugin.h 00161 * Config editor plugin for the fvretriever plugin. 00162 * @author Daniel Beck 00163 */ 00164 00165 /** Constructor. 00166 * @param glade_path path to the Glade file for the plugin's dialog 00167 */ 00168 RetrieverConfigPlugin::RetrieverConfigPlugin( string glade_path ) 00169 : ConfigEditorPlugin( "/firevision/retriever", glade_path ) 00170 { 00171 } 00172 00173 /** Destructor. */ 00174 RetrieverConfigPlugin::~RetrieverConfigPlugin() 00175 { 00176 } 00177 00178 void 00179 RetrieverConfigPlugin::pre_run() 00180 { 00181 string prefix = m_config_path + "/camera/"; 00182 Configuration::ValueIterator* vit = m_config->search( prefix.c_str() ); 00183 00184 m_config->lock(); 00185 while ( vit->next() ) 00186 { 00187 if ( ! vit->is_string() ) 00188 { 00189 throw TypeMismatchException( "Only values of type string are valid for camera " 00190 "argument string, but got %s for %s", 00191 vit->type(), 00192 vit->path() ); 00193 } 00194 00195 string camera_name = string( vit->path() ).substr( prefix.length() ); 00196 00197 RetrieverConfigDialog* dlg = dynamic_cast< RetrieverConfigDialog* >( m_dialog ); 00198 dlg->add_camera( camera_name, vit->get_string() ); 00199 } 00200 m_config->unlock(); 00201 00202 delete vit; 00203 } 00204 00205 void 00206 RetrieverConfigPlugin::post_run( int response ) 00207 { 00208 switch( response ) 00209 { 00210 case ( Gtk::RESPONSE_OK ): 00211 { 00212 RetrieverConfigDialog* dlg = dynamic_cast< RetrieverConfigDialog* >( m_dialog ); 00213 std::map< string, string > cameras = dlg->get_cameras(); 00214 00215 Glib::ustring path; 00216 00217 for ( std::map< string, string >::iterator i = cameras.begin(); 00218 i != cameras.end(); 00219 ++i ) 00220 { 00221 path = m_config_path + "/camera/" + i->first; 00222 m_config->set_string( path.c_str(), i->second ); 00223 } 00224 00225 break; 00226 } 00227 case ( Gtk::RESPONSE_CANCEL ): 00228 break; 00229 00230 default: 00231 printf("unknonw response\n"); 00232 break; 00233 } 00234 } 00235 00236 Gtk::Dialog* 00237 RetrieverConfigPlugin::load_dialog() 00238 { 00239 RetrieverConfigDialog* dlg = NULL; 00240 m_ref_xml->get_widget_derived( "PluginDialog", dlg); 00241 00242 return dlg; 00243 }