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