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