Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
retriever_config_plugin.cpp
1 
2 /***************************************************************************
3  * retriever_config_plugin.cpp - Config plugin for the retriever plugin
4  *
5  * Created: Sun Mar 29 13:59:28 2009
6  * Copyright 2009 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "retriever_config_plugin.h"
24 
25 #include <config/config.h>
26 #include <core/exceptions/software.h>
27 #include <fvutils/system/camargp.h>
28 
29 using namespace std;
30 using namespace fawkes;
31 using namespace firevision;
32 
33 /** @class RetrieverConfigDialog "retriever_config_plugin.h"
34  * Config dialog of the config editor plugin for the fvretriever.
35  * @author Daniel Beck
36  */
37 
38 /** Constructor.
39  * Allows to construct a dialog by means of get_widget_derived( ... ).
40  * @param cobject base object pointer
41  * @param builder Gtk builder
42  */
44  const Glib::RefPtr<Gtk::Builder> &builder)
45  : Gtk::Dialog( cobject )
46 {
47  builder->get_widget("trvCameras", m_trv_cameras);
48  builder->get_widget("btnAdd", m_btn_add_camera);
49  builder->get_widget("btnDelete", m_btn_delete_camera);
50 
51  m_btn_add_camera->signal_clicked().connect( sigc::mem_fun( *this, &RetrieverConfigDialog::on_add_clicked ) );
52  m_btn_delete_camera->signal_clicked().connect( sigc::mem_fun( *this, &RetrieverConfigDialog::on_delete_clicked ) );
53 
54  m_camera_list = Gtk::ListStore::create( m_camera_record );
55  m_trv_cameras->set_model( m_camera_list );
56  m_trv_cameras->append_column_editable( "Name", m_camera_record.name );
57  m_trv_cameras->append_column_editable( "Type", m_camera_record.type );
58  m_trv_cameras->append_column_editable( "Id", m_camera_record.id );
59  m_trv_cameras->append_column_editable( "Parameter", m_camera_record.params );
60  m_trv_cameras->append_column_editable( "Record", m_camera_record.record_images );
61  m_trv_cameras->append_column_editable( "Save path", m_camera_record.save_path );
62 }
63 
64 /** Destructor. */
66 {
67 }
68 
69 /** Adds a camera to the list of cameras.
70  * @param camera_name an arbitrary name to identify the camera
71  * @param camera_string a camera string that can be parsed by a CameraArgumentParser
72  * @param record_images if true the images of that camera are saved
73  * @param save_path the directory where the images are saved
74  */
75 void
77  string camera_string,
78  bool record_images,
79  string save_path )
80 {
81  CameraArgumentParser* argp = new CameraArgumentParser( camera_string.c_str() );
82 
83  string cam_type = argp->cam_type();
84  string cam_id = argp->cam_id();
85  string params;
86  std::map< string, string > param_map = argp->parameters();
87 
88  std::map< string, string >::iterator i = param_map.begin();
89  while ( i != param_map.end() )
90  {
91  params += i->first + "=" + i->second;
92 
93  if ( ++i != param_map.end() )
94  { params += ":"; }
95  }
96 
97  Gtk::TreeModel::Row row = *m_camera_list->append();
98  row[ m_camera_record.name ] = camera_name;
99  row[ m_camera_record.type ] = cam_type;
100  row[ m_camera_record.id ] = cam_id;
101  row[ m_camera_record.params ] = params;
102  row[ m_camera_record.record_images ] = record_images;
103  row[ m_camera_record.save_path ] = save_path;
104 
105  delete argp;
106 }
107 
108 /** Obtain the list of cameras shown in the dialog.
109  * @return a map camera name => camera string
110  */
111 std::map< string, string >
113 {
114  std::map< string, string > cameras;
115 
116  Gtk::TreeModel::Row row;
117  Glib::ustring name;
118  Glib::ustring cam_string;
119 
120  for ( Gtk::TreeIter i = m_camera_list->children().begin();
121  i != m_camera_list->children().end();
122  ++i )
123  {
124  row = *i;
125  name = row[ m_camera_record.name ];
126  cam_string = row[ m_camera_record.type ] + ":" +
127  row[ m_camera_record.id ] + ":" +
128  row[ m_camera_record.params ];
129 
130  cameras[ name ] = cam_string;
131  }
132 
133  return cameras;
134 }
135 
136 void
137 RetrieverConfigDialog::on_add_clicked()
138 {
139  // add empty row and select it
140  Gtk::TreeIter iter = m_camera_list->append();
141  Gtk::TreeModel::Row row = *iter;
142  row[ m_camera_record.name ] = "";
143  row[ m_camera_record.type ] = "";
144  row[ m_camera_record.id ] = "";
145  row[ m_camera_record.params ] = "";
146  row[ m_camera_record.record_images ] = false;
147  row[ m_camera_record.save_path ] = "";
148 
149  m_trv_cameras->set_cursor( m_camera_list->get_path( iter ) );
150 }
151 
152 void
153 RetrieverConfigDialog::on_delete_clicked()
154 {
155  Gtk::TreeIter iter = m_trv_cameras->get_selection()->get_selected();
156  m_camera_list->erase( iter );
157 }
158 
159 
160 /** @class RetrieverConfigPlugin tools/config_editor/retriever_config_plugin.h
161  * Config editor plugin for the fvretriever plugin.
162  * @author Daniel Beck
163  */
164 
165 /** Constructor.
166  * @param ui_path path to the UI file for the plugin's dialog
167  */
169  : ConfigEditorPlugin( "/firevision/retriever", ui_path )
170 {
171 }
172 
173 /** Destructor. */
175 {
176 }
177 
178 void
180 {
181  string prefix = m_config_path + "/camera/";
182  Configuration::ValueIterator* vit = m_config->search( prefix.c_str() );
183 
184  m_config->lock();
185  while ( vit->next() )
186  {
187  if ( ! vit->is_string() )
188  {
189  throw TypeMismatchException( "Only values of type string are valid for camera "
190  "argument string, but got %s for %s",
191  vit->type(),
192  vit->path() );
193  }
194 
195  string camera_name = string( vit->path() ).substr( prefix.length() );
196 
197  RetrieverConfigDialog* dlg = dynamic_cast< RetrieverConfigDialog* >( m_dialog );
198  dlg->add_camera( camera_name, vit->get_string() );
199  }
200  m_config->unlock();
201 
202  delete vit;
203 }
204 
205 void
207 {
208  switch( response )
209  {
210  case ( Gtk::RESPONSE_OK ):
211  {
212  RetrieverConfigDialog* dlg = dynamic_cast< RetrieverConfigDialog* >( m_dialog );
213  std::map< string, string > cameras = dlg->get_cameras();
214 
215  Glib::ustring path;
216 
217  for ( std::map< string, string >::iterator i = cameras.begin();
218  i != cameras.end();
219  ++i )
220  {
221  path = m_config_path + "/camera/" + i->first;
222  m_config->set_string( path.c_str(), i->second );
223  }
224 
225  break;
226  }
227  case ( Gtk::RESPONSE_CANCEL ):
228  break;
229 
230  default:
231  printf("unknonw response\n");
232  break;
233  }
234 }
235 
236 Gtk::Dialog*
238 {
239  RetrieverConfigDialog* dlg = NULL;
240  m_builder->get_widget_derived( "PluginDialog", dlg);
241 
242  return dlg;
243 }