]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/constructor.cpp
Mini bugfix.
[z.facultad/75.42/plaqui.git] / Constructor / src / constructor.cpp
1 #include "constructor.h"
2 #include <iostream>
3
4 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
5 {
6         id = 0;
7         saved = false;
8         set_title("Constructor");
9         Gtk::Label *lbl_plaqui_version;
10         Gtk::Image *plaqui_logo;
11         Gtk::MenuItem *mnu_about;
12         Gtk::Button *close_about;
13         
14         plaqui_logo = 0;
15         lbl_plaqui_version = 0;
16         refGlade->get_widget("lbl_plaqui_version", lbl_plaqui_version);
17         refGlade->get_widget("plaqui_logo", plaqui_logo);
18         plaqui_logo->set(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/logo.png");
19
20         refGlade->get_widget("plaqui_logo", plaqui_logo);
21         // Calculo número de revisión.
22         std::string rev = "$Rev$";
23         rev = rev.substr(6, rev.length() - 8);
24         std::string s = "PlaQui Constructor versión " VERSION " (revisión ";
25         s += rev + ")"; 
26         lbl_plaqui_version->set_text(Glib::convert(s, "UTF-8", "ISO-8859-1"));
27         
28         refGlade->get_widget("dlgAbout", dlg_about);
29         refGlade->get_widget("close_about", close_about);
30         refGlade->get_widget("mnu_about", mnu_about);
31         
32         // Cargo todas las imagenes de los iconos de los botones
33         ico_canio = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/canio_n.png");
34         ico_y     = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_n.png");
35         ico_codo  = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_o.png");
36         ico_tanque  = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/tanque_e.png");
37         ico_exclusa = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
38         ico_drain = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/drain_n.png");
39         ico_bomba = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/bomba_e.png");
40         ico_and = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_e.png");
41         ico_or = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
42         ico_not = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_e.png");
43         
44         //Obtengo todos los widgets de la ventana principal.
45         refGlade->get_widget("btn_canio", btn_canio);
46         refGlade->get_widget("btn_codo", btn_codo);
47         refGlade->get_widget("btn_y", btn_y);
48         refGlade->get_widget("btn_tanque",btn_tanque);
49         refGlade->get_widget("btn_bomba", btn_bomba);
50         refGlade->get_widget("btn_exclusa", btn_exclusa);
51         refGlade->get_widget("btn_drain", btn_drain);
52         refGlade->get_widget("btn_and", btn_and);
53         refGlade->get_widget("btn_or", btn_or);
54         refGlade->get_widget("btn_not", btn_not);
55         refGlade->get_widget("main_menu_quit",main_menu_quit);
56         refGlade->get_widget("main_menu_new",main_menu_new);
57         refGlade->get_widget("main_menu_open",main_menu_open);
58         refGlade->get_widget("main_menu_save",main_menu_save);
59         refGlade->get_widget("edit_menu_del",edit_menu_del);
60         refGlade->get_widget("chk_btn_logica",chk_btn_logica);
61         refGlade->get_widget("file_open_selection",file_open_selection);
62         refGlade->get_widget("file_selection",file_selection);
63         refGlade->get_widget("combo_entry",combo_entry);
64         refGlade->get_widget("btn_check",btn_check);
65         refGlade->get_widget("dlg_connect", dlg_connect);
66         refGlade->get_widget("btn_dlg_close", btn_dlg_close);
67         refGlade->get_widget("dlg_label", dlg_label);
68         refGlade->get_widget("quick_btn_new", quick_btn_new);
69         refGlade->get_widget("quick_btn_save", quick_btn_save);
70         refGlade->get_widget("quick_btn_open", quick_btn_open);
71         refGlade->get_widget("edit_menu_delete_all", edit_menu_delete_all);
72         refGlade->get_widget("btn_find", btn_find);
73         refGlade->get_widget("status_bar", status_bar);
74         refGlade->get_widget("dlg_quit", dlg_quit);
75         refGlade->get_widget("btn_dlg_quit_ok", btn_dlg_quit_ok);
76         refGlade->get_widget("btn_dlg_quit_cancel", btn_dlg_quit_cancel);
77         
78         //Obtengo el area de trabajo, la cual tiene definida su propia clase.
79         refGlade->get_widget_derived("workplace", workplace);  //fixed
80         
81         //Targets
82         listTargets.push_back( Gtk::TargetEntry("STRING") );
83         listTargets.push_back( Gtk::TargetEntry("text/plain") );
84         listTargets.push_back( Gtk::TargetEntry("POINTER") );
85         listTargets.push_back( Gtk::TargetEntry("application/pointer") );
86         
87         // Le indico cuales son los botones  desde los cuales se puede hacer un drag.
88         mnu_about->signal_activate().connect( SigC::slot(*dlg_about, &Gtk::Widget::show) );
89         close_about->signal_clicked().connect( SigC::slot(*dlg_about, &Gtk::Widget::hide) );
90         btn_canio->drag_source_set(listTargets);
91         btn_y->drag_source_set(listTargets);
92         btn_codo->drag_source_set(listTargets);
93         btn_tanque->drag_source_set(listTargets);
94         btn_bomba->drag_source_set(listTargets);
95         btn_drain->drag_source_set(listTargets);
96         btn_exclusa->drag_source_set(listTargets);
97         btn_and->drag_source_set(listTargets);
98         btn_or->drag_source_set(listTargets);
99         btn_not->drag_source_set(listTargets);
100         
101         //Conecto las señales de cada boton con su correspondiente metodo.
102         btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
103         btn_bomba->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_bomba_drag_get));
104         btn_exclusa->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_exclusa_drag_get));
105         btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
106         btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
107         btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
108         btn_drain->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_drain_drag_get));
109         btn_and->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_and_drag_get));
110         btn_or->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_or_drag_get));
111         btn_not->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_not_drag_get));
112         btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
113         btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
114         btn_find->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_find_clicked));        
115         chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_logica_clicked));
116         quick_btn_new->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_new_clicked));
117         quick_btn_save->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_save_clicked));
118         quick_btn_open->signal_clicked().connect(SigC::slot(*this, &Constructor::on_main_menu_open));
119         btn_dlg_quit_ok->signal_clicked().connect(SigC::slot(*this, &Constructor::on_btn_dlg_quit_ok_clicked));
120         btn_dlg_quit_cancel->signal_clicked().connect(SigC::slot(*this, &Constructor::on_btn_dlg_quit_cancel_clicked));
121         
122         main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
123         main_menu_new->signal_activate().connect(SigC::slot(*this, &Constructor::on_quick_btn_new_clicked));
124         main_menu_open->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_open));
125         main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
126         edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
127         edit_menu_delete_all->signal_activate().connect(SigC::slot(*this,&Constructor::on_quick_btn_delete_all_clicked));
128         
129         //Obtengo y conecto los botones del dialogo de Salvar/Cargar un archivo.
130         btn_file_cancel = file_selection->get_cancel_button();
131         btn_file_ok = file_selection->get_ok_button();
132         btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
133         btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
134         btn_open_cancel = file_open_selection->get_cancel_button();
135         btn_open_ok = file_open_selection->get_ok_button();
136         btn_open_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_load_from_xml));
137         btn_open_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_open_cancel_clicked));
138
139         // Señales para cambiar el icono cuando empieza el drag.
140         btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
141         btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
142         btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
143         btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
144         btn_bomba->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_bomba_drag_begin));
145         btn_exclusa->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_exclusa_drag_begin));
146         btn_drain->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_drain_drag_begin));
147         btn_and->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_and_drag_begin));
148         btn_or->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_or_drag_begin));
149         btn_not->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_not_drag_begin));
150         workplace->drag_dest_set(listTargets);
151         workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
152         // Apunto la lista del area de trabajo a la lista de items en el Constructor
153         workplace->listaItems = &listaItems;    
154         workplace->lista_logic_Items = &lista_logic_Items;      
155         workplace->logica = &logica;
156         logica = false;
157 }
158
159 Constructor::~Constructor()
160 {
161         // elimina todos los items 
162         Constructor::on_edit_menu_delete_all();
163 }
164
165 // Definicion de los metodos para obtener el icono al realizar un drag.
166 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
167 {
168         gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 12);
169 }
170
171 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
172 {
173         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",8);
174 }
175
176 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
177 {
178         gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",10);
179 }
180
181 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
182 {
183         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",12);
184 }
185
186 void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
187 {
188         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",11);
189 }
190
191 void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
192 {
193         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",13);
194 }
195
196 void Constructor::on_btn_drain_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
197 {
198         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"drain_n.png",11);
199 }
200
201 void Constructor::on_btn_and_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
202 {
203         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"and_e.png",9);
204 }
205
206 void Constructor::on_btn_or_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
207 {
208         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"or_e.png",8);
209 }
210
211 void Constructor::on_btn_not_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
212 {
213         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"not_e.png",9);
214 }
215
216 //Sale del programa. Pregunta si desea salvar antes de salir.
217 void Constructor::on_main_menu_quit()
218 {
219         if ( !saved ) 
220                 dlg_quit->show();
221         else 
222                 Gtk::Main::quit();
223 }
224 bool Constructor::on_delete_event(GdkEventAny *e)
225 {
226         std::cout<<"terminando constructor"<<std::endl;
227         on_main_menu_quit();
228         return true;
229 }
230 //Sale del programa guardando el archivo.
231 void Constructor::on_btn_dlg_quit_ok_clicked()
232 {
233         dlg_quit->hide();
234         on_quick_btn_save_clicked();
235 }
236
237 //Sale del programa sin guardar el archivo.
238 void Constructor::on_btn_dlg_quit_cancel_clicked()
239 {
240         dlg_quit->hide();
241         Gtk::Main::quit();
242 }
243
244
245 // Carga de un archivo XML
246 void Constructor::on_main_menu_open()
247 {
248         // Conecto el boton OK para llamar al cargar del XML
249         file_open_selection->show();
250 }
251
252 // Salva un archivo XML
253 void Constructor::on_main_menu_save()
254 {
255         // Conecto el boton OK para llamar al salvar
256         Glib::ustring name;
257         if ( ! check_connection(name) ) {
258                 dlg_connect->set_title("Error");
259                 dlg_label->set_text("El elemento "+name+" no esta conectado\n\t\tcorrectamente");
260                 dlg_connect->show();
261         } else  
262                 file_selection->show();
263 }
264
265 // Elimina el item apuntado desde el menu principal
266 void Constructor::on_edit_menu_del()
267
268         workplace->delete_item(WorkPlace::pointed);
269 }
270
271 // Elimina todos lo items
272 void Constructor::on_edit_menu_delete_all()
273 {
274         std::list<CItem *>::iterator i = listaItems.begin();
275         while ( i != listaItems.end() ) {
276                 (*i)->workplace->delete_item((*i)->get_id());
277                 i = listaItems.begin();
278         }
279         i = lista_logic_Items.begin();
280         while ( i != lista_logic_Items.end() ) {
281                 (*i)->workplace->delete_item((*i)->get_id());
282                 i=lista_logic_Items.begin();
283         }
284         id = 0;
285         list_pointed.clear();
286         list_pointed.push_back(" ");
287         combo_entry->set_popdown_strings(list_pointed);
288         combo_entry->get_entry()->set_text("");
289 }
290
291 // Al presionarse el check button "logica"
292 void Constructor::on_chk_btn_logica_clicked()
293 {
294         logica = !logica;
295         CItem::logic_connect = !CItem::logic_connect;
296         workplace->queue_draw();
297 }
298
299 // Metodo que levanta el archivo XML y crea todos los items agregandolos a las listas correspondientes
300 void Constructor::on_load_from_xml()
301 {
302         on_edit_menu_delete_all();
303         file_name = file_open_selection->get_filename();
304
305         /* Parseo de ejemplo de un XML desde archivo */
306         xmlDocPtr document;
307         document = xmlParseFile(file_name.c_str());
308         if (document == NULL) {
309                 std::cout<<"NO SE PUDO CARGAR EL ARCHIVO"<<std::endl;
310                 // TODO : dar un aviso de que no se pudo abrir el archivo!!
311                 return;
312         }
313
314         /* bien, el archivo se parseo bien! */
315         xmlNodePtr nodo, items;
316         nodo = document->children;
317
318         // Recorro los items
319         CItem *current;
320         if (strcmp((char *)nodo->name, "planta") == 0) {
321                 items = nodo->children;
322                 while (items != NULL) {
323                         current = NULL;
324                         if (items->type == XML_ELEMENT_NODE) {
325                                 std::cout << "ITEM" << std::endl;
326                                 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
327                                         current = loadBomba(items);
328                                 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
329                                         current = loadConduct(items);
330                                 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
331                                         current = loadCodo(items);
332                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
333                                         current = loadExclusa(items);
334                                 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
335                                         current = loadTank(items);
336                                 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
337                                         current = loadUnion(items);
338                                 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
339                                         current = loadDrain(items);
340                                 } else if (xmlStrcmp(items->name, BAD_CAST"and")==0) {
341                                         current = loadAnd(items);
342                                 } else if (xmlStrcmp(items->name, BAD_CAST"not")==0) {
343                                         current = loadNot(items);
344                                 } else if (xmlStrcmp(items->name, BAD_CAST"or")==0) {
345                                         current = loadOr(items);
346                                 }
347
348                                 if (current != NULL) {
349                                         if (! current->is_logic )
350                                                 listaItems.push_back(current);
351                                         else {
352                                                 lista_logic_Items.push_back(current);
353                                         }
354                                         // Agrego y conecto la bomba
355                                         current->drag_source_set(listTargets);
356                                         workplace->put(*current, current->get_position_x(), current->get_position_y());
357                                         //Apunto al workplace
358                                         current->workplace= workplace;
359                                         //Apunto a la lista.
360                                         current->combo_entry = combo_entry;
361                                         //Apunto a la lista de apuntados
362                                         current->list_pointed = &list_pointed;
363                                         //Apunto a la listaItems.
364                                         current->listaItems = &listaItems;
365                                         //Apunto a la lista de items logicos
366                                         current->lista_logic_Items = &lista_logic_Items;
367                                         //Apunto a la barra de estado
368                                         current->status_bar = status_bar;
369                                         // Conecto las señales
370                                         current->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
371                                         current->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), current));
372                                         current->show();        
373                                 }
374                         }
375                         items = items->next;
376                 }
377                 
378                 // Crea las lineas que conectan la parte logica.
379                 create_lines(document->children);
380                 xmlFreeDoc(document);
381                 workplace->update_logic_position();
382                 workplace->queue_draw();
383         } else {
384                 std::cout<< "EL XML NO ES VALIDO" << std::endl;
385                 // TODO : avisar que el XML no es valido!!
386         }
387         std::cout << "CARGA COMPLETA" << std::endl;
388         file_open_selection->hide();
389         
390         // Seteo el id en el maximo mas uno de todos los id cargados, para que quede bien cuando se agregan nuevos items en un proyecto 
391         // cargado desde un archivo.
392         int id_1 = -1 , id_2 = -1;
393         std::list<CItem *>::iterator i = listaItems.begin();
394         while ( i != listaItems.end() ){
395                 if ( (*i)->get_id() >= id_1 ) 
396                         id_1 = (*i)->get_id();
397         i++;
398         }
399         id_1++;
400         
401         i = lista_logic_Items.begin();
402         while ( i != lista_logic_Items.end() ){
403                 if ( (*i)->get_id() >= id_2 ) 
404                         id_2 = (*i)->get_id();
405         i++;
406         }
407         id_2++;
408         if ( id_1 < id_2 ) id = id_2;
409         else id = id_1;
410 }
411
412 // Recorre todo los items los cuales conocen la manera de salvarse en un archivo XML.
413 void Constructor::on_btn_file_ok_clicked()
414 {
415         std::list<CItem *>::iterator i = listaItems.begin();
416         file_name = file_selection->get_filename();
417         Glib::ustring name;
418         if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
419                 fprintf(archivo, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
420                 fprintf (archivo,"<planta>\n");
421                 while ( i != listaItems.end() ){
422                         (*i)->save(archivo);
423                         i++;
424                 }
425                 i = lista_logic_Items.begin();
426                 while ( i != lista_logic_Items.end() ) {
427                         (*i)->save(archivo);
428                         i++;
429                 }
430                 fprintf(archivo,"</planta>\n");
431                 saved = true;
432                 fclose(archivo);
433         } else 
434                 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
435         file_selection->hide();
436 }
437
438 // Salva el archivo, si no estaba salvado muestra el dialogo, si no sobreescribe el ultimo archivo salvado.
439 void Constructor::on_quick_btn_save_clicked()
440 {
441         if ( ! saved ) {
442                 on_main_menu_save();
443         } else {
444                 on_btn_file_ok_clicked();
445         }
446 }
447         
448 // Limpia la pantalla para comenzar un nuevo proyecto.
449 void Constructor::on_quick_btn_new_clicked()
450 {
451         on_quick_btn_delete_all_clicked();
452         saved = false;
453 }
454
455 // Borra todos los items pero desde otro boton.
456 void Constructor::on_quick_btn_delete_all_clicked()
457 {
458         on_edit_menu_delete_all();
459 }       
460
461 // busca en la lista el nombre del item que aparece en el cuadro de texto y si lo encuentra dibuja un recuadro para mostrarlo.
462 void Constructor::on_btn_find_clicked()
463 {
464         CItem * temp = NULL;
465         std::list<CItem *>::iterator i = listaItems.begin();
466         while ( i !=listaItems.end() ) {
467                 if ( (*i)->get_name()  == combo_entry->get_entry()->get_text() ) {
468                         temp = *i;
469                         break;
470                 }
471                 i++;
472         }
473         
474         i = lista_logic_Items.begin();
475         while ( i !=lista_logic_Items.end() ) {
476                 if ( (*i)->get_name()  == combo_entry->get_entry()->get_text() ) {
477                         temp = *i;
478                         break;
479                 }
480                 i++;
481         }
482         if ( temp != NULL )
483         workplace->get_window()->draw_rectangle(workplace->get_style()->get_black_gc(), false , temp->get_position_x()-5, temp->get_position_y()-5,
484                                                                                                                                                 temp->get_image()->get_width()+10, temp->get_image()->get_height()+10);
485 }
486
487 // Oculta el dialogo.
488 void Constructor::on_btn_file_cancel_clicked()
489 {
490         file_selection->hide();
491 }
492
493 //Muestra el dialogo para seleccionar archivo
494 void Constructor::on_btn_open_cancel_clicked()
495 {
496         file_open_selection->hide();
497 }
498
499 // Oculta el dialogo
500 void Constructor::on_btn_dlg_connect_clicked()
501 {
502         dlg_connect->hide();
503 }
504
505 //Metodos que permiten que el icono sea el del items durante el drag 
506 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
507 {
508         context->set_icon(ico_canio, 5, 5); 
509 }
510
511 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
512 {
513         context->set_icon(ico_y, 5, 5); 
514 }
515
516 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
517 {
518         context->set_icon(ico_codo, 5, 5); 
519 }
520
521 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
522 {
523         context->set_icon(ico_tanque, 5, 5); 
524 }
525
526 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
527 {
528         context->set_icon(ico_bomba, 5, 5); 
529 }
530
531 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
532 {
533         context->set_icon(ico_exclusa, 5, 5); 
534 }
535
536 void Constructor::on_drain_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
537 {
538         context->set_icon(ico_drain, 5, 5); 
539 }
540
541 void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
542 {
543         context->set_icon(ico_and, 5, 5); 
544 }
545
546 void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
547 {
548         context->set_icon(ico_or, 5, 5); 
549 }
550
551 void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
552 {
553         context->set_icon(ico_not, 5, 5); 
554 }
555
556 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
557 {
558         context->set_icon(item->get_image(), 5, 5);
559 }
560
561 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
562 {       
563         gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
564 }
565
566 // Si un item es movido encima de otro este metodo devueve falso. De esta manera no podran encimarse items.
567 bool Constructor::can_drop(CItem *item, int _x, int _y)
568 {
569         std::list<CItem*>::iterator  i = listaItems.begin();
570         while( i != listaItems.end() ) {
571                 CItem *temp = *i;
572                 if ( temp->get_id() != item->get_id()   ) {
573                         if (  (temp->is_occupied_area(_x, _y))  ||  
574                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
575                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
576                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
577                                 return false;
578                         else i++;
579                 }
580                 else i++;
581         }
582         return true;
583 }
584
585 //Cada vez que se realiza un drag o un drop este metodo es invocado.
586 //Aca se validan las posiciones de los items y se crean si no estaban en el area de trabajo.
587 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
588 {
589         workplace->update_logic_position();
590         workplace->queue_draw();
591         if ( !logica ) {
592                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
593                 int i,j;
594                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
595                 // separacion de 2 cuadritos
596                 i = (x+1)/32;
597                 j = (y+1)/32;
598                 // El drag es de un item
599                 if (selection_data->format == 10) {
600                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
601                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
602                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
603                         }
604                 }
605
606                 // El Drag es desde la barra de tareas
607                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
608                         CItem *a;
609                         if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
610                         a = new Splitter();
611                         else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
612                         a = new Conduct();
613                         else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
614                         a = new Union();
615                         else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
616                         a = new Cistern();
617                         else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
618                         a =new Pump();
619                         else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
620                         a = new Exclusa();
621                         else if (strcmp((const char *)selection_data->data, "drain_n.png")==0) 
622                         a = new Drain();
623                         else if (strcmp((const char *)selection_data->data, "and_e.png")==0){ 
624                         a = new And(); a->is_logic = true;
625                         } else if (strcmp((const char *)selection_data->data, "or_e.png")==0) {
626                                 a = new Or(); a->is_logic = true;
627                                 } else if (strcmp((const char *)selection_data->data, "not_e.png")==0) {
628                                         a = new Not(); a->is_logic = true;
629                                         }
630                 
631                         char char_id[10];
632                         sprintf(char_id,"%d",id);
633                         a->set_id(id++);                
634                         a->set_name( a->get_name()+char_id );
635                         if ( can_drop(a, i*32, j*32) ) {
636                                 workplace->put(*a, i*32, j*32);
637                                 //Apunto al workplace
638                                 a->workplace = workplace;
639                                 //Apunto a la lista.
640                                 a->combo_entry = combo_entry;
641                                 //Apunto a la lista de apuntados
642                                 a->list_pointed = &list_pointed;
643                                 //Apunto a la listaItems.
644                                 a->listaItems = &listaItems;
645                                 //Apunto a la barra de estado
646                                 a->status_bar = status_bar;
647                                 //Apunto a la lista de items logicos
648                                 a->lista_logic_Items = &lista_logic_Items;
649                                 //Seteo la posicion del item
650                                 a->set_position(i*32,j*32);
651                                 // Seteo la lista de tipos de drags 
652                                 a->drag_source_set(listTargets);
653                                 // Conecto las señales
654                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
655                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
656                                 // parametro extra, en este caso un CItem *. Esto se hace para
657                                 // que cuando el usuario quiera mover un item, saber que item es
658                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
659                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
660                                 // raro se vea el widget tal cual.
661                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
662                                 a->show();      
663                                 if (! a->is_logic )
664                                         listaItems.push_back(a);
665                                 else 
666                                         lista_logic_Items.push_back(a);
667                         } else {
668                                         id--;
669                                         delete a;
670                                 }
671                 }
672                 context->drag_finish(false, false, time);
673         }
674         workplace->update_logic_position();
675         workplace->queue_draw();
676 }
677
678 // Cuando se presiona el boton "Verificar Conexiones" esta funcion es invocada
679 //Muestra un dialogo de error en caso de que no esten bien conectados y un dialogo
680 //de exito si lo estan.
681 void Constructor::on_btn_check_clicked()
682 {
683         Glib::ustring name;
684         if ( ! check_connection(name) ) {
685                 dlg_connect->set_title("Error");
686                 dlg_label->set_text("El elemento "+name+" no esta conectado\n\t\tcorrectamente");
687                 dlg_connect->show();
688         } else {
689                 dlg_connect->set_title("Conexion");
690                 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
691                 dlg_connect->show();
692         }
693 }
694
695 //Verifica que todos los items esten correctamente conectados en sus extremos.
696 //Cada item verifica su conexion en forma independiente.
697 bool Constructor::check_connection(Glib::ustring& name)
698 {
699         std::list<CItem *>::iterator i = listaItems.begin();
700         while ( i != listaItems.end() ){
701                 (*i)->set_default_connector();
702                 (*i)->is_connected = false;
703                 i++;
704         }
705         i = listaItems.begin();
706         if ( !listaItems.empty() ) {
707                 while ( i != listaItems.end() ) {
708                         CItem *temp = *i;
709                         std::cout<< "item="<<temp->get_name()<<" "<<"check= "<<temp->check_connection()<<std::endl;
710                         if ( !temp->check_connection() ) {
711                                 name = temp->get_name();
712                                 return false;
713                         }       
714                 i++;
715                 }
716         }
717         std::cout<<"check_connection logic"<<std::endl;
718         std::list<CItem *>::iterator j = lista_logic_Items.begin();
719         while ( j != lista_logic_Items.end() ) {
720                 if ( !(*j)->check_connection() ) {
721                         name = (*j)->get_name();
722                         return false;
723                 }
724                 j++;
725         }
726         return true;
727 }       
728
729
730 //Los siguientes metodos son utilizados para levantar cada item desde el XML
731
732 Not *Constructor::loadNot(xmlNodePtr nodo)
733 {
734         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
735         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
736         int orientacion=0, x, y;
737
738         nodo = nodo->children;
739         while (nodo != NULL) {
740                 if (nodo->type == XML_ELEMENT_NODE) {
741                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
742                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
743                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
744                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
745                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
746                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
747                         }
748                 }
749                 nodo = nodo->next;
750         }
751
752         Not *p = new Not(orientacion);
753         p->set_position(x,y);
754         p->set_id( atoi(id.c_str()) );
755         p->set_name(name);
756
757         return p;
758 }
759
760 Or *Constructor::loadOr(xmlNodePtr nodo)
761 {
762         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
763         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
764         int orientacion=0, x, y;
765
766         nodo = nodo->children;
767         while (nodo != NULL) {
768                 if (nodo->type == XML_ELEMENT_NODE) {
769                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
770                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
771                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
772                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
773                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
774                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
775                         }
776                 }
777                 nodo = nodo->next;
778         }
779
780         Or *p = new Or(orientacion);
781         p->set_position(x,y);
782         p->set_id( atoi(id.c_str()) );
783         p->set_name(name);
784
785         return p;
786 }
787
788 And *Constructor::loadAnd(xmlNodePtr nodo)
789 {
790         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
791         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
792         int orientacion=0, x, y;
793         float flujo;
794         xmlNodePtr inicial = nodo;
795
796         nodo = nodo->children;
797         while (nodo != NULL) {
798                 if (nodo->type == XML_ELEMENT_NODE) {
799                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
800                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
801                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
802                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
803                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
804                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
805                         }
806                 }
807                 nodo = nodo->next;
808         }
809
810         And *p = new And(orientacion);
811         p->set_position(x,y);
812         p->set_id( atoi(id.c_str()) );
813         p->set_name(name);
814         return p;
815 }
816
817 Pump *Constructor::loadBomba(xmlNodePtr nodo)
818 {
819         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
820         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
821         int orientacion=0, x, y;
822         float flujo;
823         Gdk::Color color;
824
825         nodo = nodo->children;
826         while (nodo != NULL) {
827                 if (nodo->type == XML_ELEMENT_NODE) {
828                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
829                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
830                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
831                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
832                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
833                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
834                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
835                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
836                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
837                                 color = loadColor(nodo->children);
838                         }
839                 }
840                 nodo = nodo->next;
841         }
842
843         Pump *p = new Pump(orientacion);
844         p->set_position(x,y);
845         p->set_entrega(flujo);
846         p->set_id( atoi(id.c_str()) );
847         p->set_name(name);
848         p->set_liquid_color(color);
849
850         return p;
851 }
852
853 Gdk::Color Constructor::loadColor(xmlNodePtr nodo)
854 {
855         gushort r,g,b;
856         while (nodo != NULL) {
857                 if (nodo->type == XML_ELEMENT_NODE) {
858                         if (xmlStrcmp(nodo->name, BAD_CAST"rojo")==0)
859                                 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
860                         if (xmlStrcmp(nodo->name, BAD_CAST"verde")==0)
861                                 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
862                         if (xmlStrcmp(nodo->name, BAD_CAST"azul")==0)
863                                 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
864                 }
865                 nodo = nodo->next;
866         }
867         Gdk::Color c;
868         c.set_rgb(r,g,b);
869         std::cout << r << "  " << g << " " << b << std::endl;
870         return c;
871 }
872
873 Conduct *Constructor::loadConduct(xmlNodePtr nodo)
874 {
875         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
876         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
877         int orientacion=0, x, y;
878         float flujo;
879
880         nodo = nodo->children;
881         while (nodo != NULL) {
882                 if (nodo->type == XML_ELEMENT_NODE) {
883                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
884                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
885                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
886                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
887                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
888                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
889                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
890                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
891                         }
892                 }
893                 nodo = nodo->next;
894         }
895
896         Conduct *p = new Conduct(orientacion);
897         p->set_position(x,y);
898         p->set_caudal(flujo);
899         p->set_id( atoi(id.c_str()) );
900         p->set_name(name);
901
902         return p;
903 }
904
905 Exclusa *Constructor::loadExclusa(xmlNodePtr nodo)
906 {
907         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
908         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
909         std::string estado;
910         int orientacion=0, x, y;
911         float flujo;
912
913         nodo = nodo->children;
914         while (nodo != NULL) {
915                 if (nodo->type == XML_ELEMENT_NODE) {
916                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
917                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
918                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
919                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
920                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
921                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
922                         } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
923                                 estado = (char *)XML_GET_CONTENT(nodo->children);
924                         }
925                 }
926                 nodo = nodo->next;
927         }
928
929         Exclusa *p = new Exclusa(orientacion);
930         p->set_position(x,y);
931         p->set_id( atoi(id.c_str()) );
932         p->set_name(name);
933         p->set_estado( estado == "1" );
934
935         return p;
936 }
937
938 Cistern *Constructor::loadTank(xmlNodePtr nodo)
939 {
940         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
941         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
942         int orientacion=0, x, y;
943         float liquido,capacidad;
944         Gdk::Color color;
945
946         nodo = nodo->children;
947         while (nodo != NULL) {
948                 if (nodo->type == XML_ELEMENT_NODE) {
949                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
950                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
951                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
952                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
953                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
954                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
955                         } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
956                                 capacidad = atof ((char *)XML_GET_CONTENT(nodo->children) );
957                         } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
958                                 liquido = atof ((char *)XML_GET_CONTENT(nodo->children) );
959                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
960                                 color = loadColor(nodo->children);
961                         }
962                 }
963                 nodo = nodo->next;
964         }
965
966         Cistern *p = new Cistern(orientacion);
967         p->set_position(x,y);
968         p->set_id( atoi(id.c_str()) );
969         p->set_name(name);
970         p->set_capacidad(capacidad);
971         p->set_contenido_inicial(liquido);
972         p->set_liquid_color(color);
973         return p;
974 }
975
976 Union *Constructor::loadUnion(xmlNodePtr nodo)
977 {
978         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
979         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
980         std::string tipo;
981         int orientacion=0, x, y;
982         float caudal;
983
984         nodo = nodo->children;
985         while (nodo != NULL) {
986                 if (nodo->type == XML_ELEMENT_NODE) {
987                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
988                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
989                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
990                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
991                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
992                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
993                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
994                                 caudal = atof ((char *)XML_GET_CONTENT(nodo->children) );
995                         } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
996                                 tipo = (char *)XML_GET_CONTENT(nodo->children);
997                         }
998                 }
999                 nodo = nodo->next;
1000         }
1001
1002         Union *p = new Union(orientacion);
1003         p->set_position(x,y);
1004         p->set_id( atoi(id.c_str()) );
1005         p->set_name(name);
1006         p->set_caudal(caudal);
1007         p->is_union = (tipo == "union");
1008
1009         return p;
1010 }
1011
1012 Drain *Constructor::loadDrain(xmlNodePtr nodo)
1013 {
1014         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
1015         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
1016         int orientacion=0, x, y;
1017
1018         nodo = nodo->children;
1019         while (nodo != NULL) {
1020                 if (nodo->type == XML_ELEMENT_NODE) {
1021                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
1022                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1023                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
1024                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1025                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
1026                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1027                         }
1028                 }
1029                 nodo = nodo->next;
1030         }
1031
1032         Drain *p = new Drain(orientacion);
1033         p->set_position(x,y);
1034         p->set_id( atoi(id.c_str()) );
1035         p->set_name(name);
1036
1037         return p;
1038 }
1039
1040 Splitter *Constructor::loadCodo(xmlNodePtr nodo)
1041 {
1042         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
1043         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
1044         int orientacion=0, x, y;
1045         float caudal;
1046
1047         nodo = nodo->children;
1048         while (nodo != NULL) {
1049                 if (nodo->type == XML_ELEMENT_NODE) {
1050                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
1051                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1052                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
1053                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1054                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
1055                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
1056                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
1057                                 caudal = atof( (char *)XML_GET_CONTENT(nodo->children) );
1058                         }
1059                 }
1060                 nodo = nodo->next;
1061         }
1062
1063         Splitter *p = new Splitter(orientacion);
1064         p->set_position(x,y);
1065         p->set_id( atoi(id.c_str()) );
1066         p->set_name(name);
1067         p->set_caudal(caudal);
1068
1069         return p;
1070 }
1071
1072 void Constructor::create_lines(xmlNodePtr nodo)
1073 {
1074         std::string name;
1075         
1076         nodo = nodo->children;
1077         while (nodo != NULL) {
1078                 if (nodo->type == XML_ELEMENT_NODE) {
1079                         if (xmlStrcmp(nodo->name, BAD_CAST"and")==0) {
1080                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
1081                                 std::cout << name << std::endl;
1082                                 create_line(nodo->children, workplace->get_logic_id(name));
1083                         } else if (xmlStrcmp(nodo->name, BAD_CAST"not")==0) {
1084                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
1085                                 std::cout << name << std::endl;
1086                                 create_line(nodo->children, workplace->get_logic_id(name));
1087                         } else if (xmlStrcmp(nodo->name, BAD_CAST"or")==0) {
1088                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
1089                                 std::cout << name << std::endl;
1090                                 create_line(nodo->children, workplace->get_logic_id(name));
1091                         }
1092                 }
1093                 nodo = nodo->next;
1094         }
1095 }
1096
1097 //Levanta las lineas desde el archvo XML 
1098 void Constructor::create_line(xmlNodePtr nodo, int logic_id)
1099 {
1100         std::string otro;
1101         std::cout << "Buscando lineas ..." << std::endl;
1102         while (nodo != NULL) {
1103                 if (nodo->type == XML_ELEMENT_NODE) {
1104                         if (xmlStrcmp(nodo->name, BAD_CAST"salida")==0) {
1105                                 otro = (char *)XML_GET_CONTENT(nodo->children);
1106                                 t_line tmp_line;
1107                                 tmp_line.logic_id = logic_id;
1108                                 //workplace->get_logic_item(logic_id)->set_out_connected(true);
1109                                 if (workplace->get_item_id(otro) != -1) {
1110                                         tmp_line.store_id = workplace->get_item_id(otro);
1111                                         std::cout << otro << " se conecta a una compuerta" << std::endl;
1112                                         workplace->lista_lineas_in.push_back(tmp_line);
1113                                 } else {
1114                                         /* Como no era un item, debe ser una compuerta */
1115                                         tmp_line.store_id = workplace->get_logic_id(otro);
1116                                         workplace->lista_lineas_logic.push_back(tmp_line);
1117                                 }
1118                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada")==0) {
1119                                 otro = (char *)XML_GET_CONTENT(nodo->children);
1120                                 t_line tmp_line;
1121                                 tmp_line.logic_id = logic_id;
1122                                 if (workplace->get_item_id(otro) != -1) {
1123                                         tmp_line.store_id = workplace->get_item_id(otro);
1124                                         /* Tengo que ver si es un tanque, para setearlo correctamente */
1125                                         if (dynamic_cast<Cistern *>(workplace->get_item(workplace->get_item_id(otro)))) {
1126                                                 std::string donde = (char *)xmlGetProp(nodo, BAD_CAST"id");
1127                                                 tmp_line.cistern_out1 = true;
1128                                                 if (donde == "inferior") {
1129                                                         tmp_line.cistern_out1 = false;
1130                                                 }
1131                                         }
1132                                         workplace->lista_lineas_out.push_back(tmp_line);
1133                                         std::cout << otro << " se conecta a una compuerta" << std::endl;
1134                                 } else {
1135                                         /* Como no era un item, debe ser una compuerta */
1136                                         tmp_line.store_id = workplace->get_logic_id(otro);
1137                                         workplace->lista_lineas_logic.push_back(tmp_line);
1138                                 }
1139                         }
1140                 }
1141                 nodo = nodo->next;
1142         }
1143 }