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