]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/constructor.cpp
2966528a44aef807761f8ab53377ac89cbec1cb0
[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         
9         
10         ico_canio = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/canio_n.png");
11         ico_y     = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_n.png");
12         ico_codo  = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_o.png");
13         ico_tanque  = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/tanque_e.png");
14         ico_exclusa = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
15         ico_drain = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/drain_n.png");
16         ico_bomba = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/bomba_e.png");
17         ico_and = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_e.png");
18         ico_or = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
19         ico_not = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_e.png");
20         
21         refGlade->get_widget("btn_canio", btn_canio);
22         refGlade->get_widget("btn_codo", btn_codo);
23         refGlade->get_widget("btn_y", btn_y);
24         refGlade->get_widget("btn_tanque",btn_tanque);
25         refGlade->get_widget("btn_bomba", btn_bomba);
26         refGlade->get_widget("btn_exclusa", btn_exclusa);
27         refGlade->get_widget("btn_drain", btn_drain);
28         refGlade->get_widget("btn_and", btn_and);
29         refGlade->get_widget("btn_or", btn_or);
30         refGlade->get_widget("btn_not", btn_not);
31         refGlade->get_widget("main_menu_quit",main_menu_quit);
32         refGlade->get_widget("main_menu_open",main_menu_open);
33         refGlade->get_widget("main_menu_save",main_menu_save);
34         refGlade->get_widget("edit_menu_del",edit_menu_del);
35         refGlade->get_widget("chk_btn_logica",chk_btn_logica);
36         refGlade->get_widget("file_open_selection",file_open_selection);
37         refGlade->get_widget("file_selection",file_selection);
38         refGlade->get_widget("combo_entry",combo_entry);
39         refGlade->get_widget("btn_check",btn_check);
40         refGlade->get_widget("dlg_connect", dlg_connect);
41         refGlade->get_widget("btn_dlg_close", btn_dlg_close);
42         refGlade->get_widget("dlg_label", dlg_label);
43         refGlade->get_widget("quick_btn_clear", quick_btn_clear);
44         refGlade->get_widget("quick_btn_save", quick_btn_save);
45         
46         refGlade->get_widget_derived("workplace", workplace);  //fixed
47         
48         //Targets
49         listTargets.push_back( Gtk::TargetEntry("STRING") );
50         listTargets.push_back( Gtk::TargetEntry("text/plain") );
51         listTargets.push_back( Gtk::TargetEntry("POINTER") );
52         listTargets.push_back( Gtk::TargetEntry("application/pointer") );
53         
54         btn_canio->drag_source_set(listTargets);
55         btn_y->drag_source_set(listTargets);
56         btn_codo->drag_source_set(listTargets);
57         btn_tanque->drag_source_set(listTargets);
58         btn_bomba->drag_source_set(listTargets);
59         btn_drain->drag_source_set(listTargets);
60         btn_exclusa->drag_source_set(listTargets);
61         btn_and->drag_source_set(listTargets);
62         btn_or->drag_source_set(listTargets);
63         btn_not->drag_source_set(listTargets);
64         
65         btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
66         btn_bomba->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_bomba_drag_get));
67         btn_exclusa->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_exclusa_drag_get));
68         btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
69         btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
70         btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
71         btn_drain->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_drain_drag_get));
72         btn_and->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_and_drag_get));
73         btn_or->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_or_drag_get));
74         btn_not->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_not_drag_get));
75         btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
76         btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
77         chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
78         quick_btn_clear->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_clear_clicked));
79         quick_btn_save->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_save_clicked));
80         
81         main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
82         main_menu_open->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_open));
83         main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
84         edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
85                         
86         btn_file_cancel = file_selection->get_cancel_button();
87         btn_file_ok = file_selection->get_ok_button();
88         btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
89         btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
90         
91         btn_open_cancel = file_open_selection->get_cancel_button();
92         btn_open_ok = file_open_selection->get_ok_button();
93         btn_open_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_load_from_xml));
94         btn_open_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_open_cancel_clicked));
95
96         // Señales para cambiar el icono cuando empieza el drag.
97         btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
98         btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
99         btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
100         btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
101         btn_bomba->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_bomba_drag_begin));
102         btn_exclusa->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_exclusa_drag_begin));
103         btn_drain->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_drain_drag_begin));
104         btn_and->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_and_drag_begin));
105         btn_or->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_or_drag_begin));
106         btn_not->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_not_drag_begin));
107         workplace->drag_dest_set(listTargets);
108         workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
109         workplace->listaItems = &listaItems;    
110         workplace->lista_logic_Items = &lista_logic_Items;      
111         logica = false;
112 }
113
114 Constructor::~Constructor()
115 {
116         Constructor::on_edit_menu_delete_all();
117 }
118
119 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
120 {
121         gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 12);
122 }
123
124 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
125 {
126         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",8);
127 }
128
129 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
130 {
131         gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",10);
132 }
133
134 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
135 {
136         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",12);
137 }
138
139 void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
140 {
141         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",11);
142 }
143
144 void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
145 {
146         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",13);
147 }
148
149 void Constructor::on_btn_drain_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
150 {
151         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"drain_n.png",11);
152 }
153
154 void Constructor::on_btn_and_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
155 {
156         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"and_e.png",9);
157 }
158
159 void Constructor::on_btn_or_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
160 {
161         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"or_e.png",8);
162 }
163
164 void Constructor::on_btn_not_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
165 {
166         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"not_e.png",9);
167 }
168
169
170 void Constructor::on_main_menu_quit()
171 {
172         delete this;
173         Gtk::Main::quit();
174         //hide();
175 }
176
177 void Constructor::on_main_menu_open()
178 {
179         // Conecto el boton OK para llamar al cargar del XML
180         file_open_selection->show();
181 }
182
183 void Constructor::on_main_menu_save()
184 {
185         // Conecto el boton OK para llamar al salvar
186         file_selection->show();
187 }
188
189 void Constructor::on_edit_menu_del()
190
191         workplace->delete_item(WorkPlace::pointed);
192 }
193
194 void Constructor::on_edit_menu_delete_all()
195 {
196         std::list<CItem *>::iterator i = listaItems.begin();
197         while ( i != listaItems.end() ) {
198                 (*i)->workplace->delete_item((*i)->get_id());
199                 i = listaItems.begin();
200         }
201         i = lista_logic_Items.begin();
202         while ( i != lista_logic_Items.end() ) {
203                 (*i)->workplace->delete_item((*i)->get_id());
204                 i=lista_logic_Items.begin();
205         }
206 }
207
208 void Constructor::on_chk_btn_clicked()
209 {
210         logica = !logica;
211         CItem::logic_connect = !CItem::logic_connect;
212 }
213
214 void Constructor::on_load_from_xml()
215 {
216         file_name = file_open_selection->get_filename();
217
218         /* Parseo de ejemplo de un XML desde archivo */
219         xmlDocPtr document;
220         document = xmlParseFile(file_name.c_str());
221         if (document == NULL) {
222                 // TODO : dar un aviso de que no se pudo abrir el archivo!!
223                 return;
224         }
225
226         /* bien, el archivo se parseo bien! */
227         xmlNodePtr nodo, items;
228         nodo = document->children;
229
230         // Recorro los items
231         CItem *current;
232         if (strcmp((char *)nodo->name, "planta") == 0) {
233                 items = nodo->children;
234                 while (items != NULL) {
235                         current = NULL;
236                         if (items->type == XML_ELEMENT_NODE) {
237                                 std::cout << "ITEM" << std::endl;
238                                 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
239                                         current = loadBomba(items);
240                                 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
241                                         current = loadConduct(items);
242                                 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
243                                         current = loadCodo(items);
244                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
245                                         current = loadExclusa(items);
246                                 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
247                                         current = loadTank(items);
248                                 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
249                                         current = loadUnion(items);
250                                 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
251                                         current = loadDrain(items);
252                                 }
253
254                                 if (current != NULL) {
255                                         // Agrego y conecto la bomba
256                                         listaItems.push_back(current);
257                                         current->drag_source_set(listTargets);
258                                         workplace->put(*current, current->get_position_x(), current->get_position_y());
259                                         //Apunto al workplace
260                                         current->workplace= workplace;
261                                         //Apunto a la lista.
262                                         current->combo_entry = combo_entry;
263                                         //Apunto a la listaItems.
264                                         current->listaItems = &listaItems;
265                                         //Apunto a la lista de items logicos
266                                         current->lista_logic_Items = &lista_logic_Items;
267                                         // Conecto las señales
268                                         current->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
269                                         current->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), current));
270                                         current->show();        
271                                 }
272                         }
273                         items = items->next;
274                 }
275         } else {
276                 // TODO : avisar que el XML no es valido!!
277         }
278         std::cout << "CARGA COMPLETA" << std::endl;
279         file_open_selection->hide();
280         id = listaItems.size()+lista_logic_Items.size()+1;
281 }
282
283 void Constructor::on_btn_file_ok_clicked()
284 {
285         std::list<CItem *>::iterator i = listaItems.begin();
286         file_name = file_selection->get_filename();
287         if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
288                 fprintf(archivo, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
289                 fprintf (archivo,"<planta>\n");
290                 while ( i != listaItems.end() ){
291                         (*i)->save(archivo);
292                         i++;
293                 }
294                 i = lista_logic_Items.begin();
295                 while ( i != lista_logic_Items.end() ) {
296                         (*i)->save(archivo);
297                         i++;
298                 }
299                 fprintf(archivo,"</planta>\n");
300                 saved = true;
301         } else 
302                 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
303         fclose(archivo);
304         file_selection->hide();
305 }
306
307 void Constructor::on_quick_btn_save_clicked()
308 {
309         if ( ! saved ) {
310                 on_main_menu_save();
311         } else {
312                 on_btn_file_ok_clicked();
313         }
314 }
315         
316
317 void Constructor::on_quick_btn_clear_clicked()
318 {
319         std::list<CItem *>::iterator i = listaItems.begin();
320         while ( i != listaItems.end() ){
321                 CItem *temp = *i;
322                 listaItems.erase(i);
323                 delete temp;
324                 i = listaItems.begin();
325         }
326         i = lista_logic_Items.begin();
327         while ( i != lista_logic_Items.end() ){
328                 CItem *temp2 = *i;
329                 lista_logic_Items.erase(i);
330                 delete temp2;
331                 i = lista_logic_Items.begin();
332         }
333 }       
334
335 void Constructor::on_btn_file_cancel_clicked()
336 {
337         file_selection->hide();
338 }
339
340 void Constructor::on_btn_open_cancel_clicked()
341 {
342         file_open_selection->hide();
343 }
344
345 void Constructor::on_btn_dlg_connect_clicked()
346 {
347         dlg_connect->hide();
348 }
349
350 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
351 {
352         context->set_icon(ico_canio, 5, 5); 
353 }
354
355 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
356 {
357         context->set_icon(ico_y, 5, 5); 
358 }
359
360 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
361 {
362         context->set_icon(ico_codo, 5, 5); 
363 }
364
365 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
366 {
367         context->set_icon(ico_tanque, 5, 5); 
368 }
369
370 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
371 {
372         context->set_icon(ico_bomba, 5, 5); 
373 }
374
375 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
376 {
377         context->set_icon(ico_exclusa, 5, 5); 
378 }
379
380 void Constructor::on_drain_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
381 {
382         context->set_icon(ico_drain, 5, 5); 
383 }
384
385 void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
386 {
387         context->set_icon(ico_and, 5, 5); 
388 }
389
390 void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
391 {
392         context->set_icon(ico_or, 5, 5); 
393 }
394
395 void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
396 {
397         context->set_icon(ico_not, 5, 5); 
398 }
399
400 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
401 {
402         context->set_icon(item->get_image(), 5, 5);
403 }
404
405 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
406 {       
407         gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
408 }
409
410 bool Constructor::can_drop(CItem *item, int _x, int _y)
411 {
412         std::list<CItem*>::iterator  i = listaItems.begin();
413         while( i != listaItems.end() ) {
414                 CItem *temp = *i;
415                 if ( temp->get_id() != item->get_id()   ) {
416                         if (  (temp->is_occupied_area(_x, _y))  ||  
417                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
418                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
419                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
420                                 return false;
421                         else i++;
422                 }
423                 else i++;
424         }
425         return true;
426 }
427
428 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
429 {
430         workplace->update_logic_position();
431         workplace->queue_draw();
432         if ( !logica ) {
433                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
434                 int i,j;
435                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
436                 // separacion de 2 cuadritos
437                 i = (x+1)/32;
438                 j = (y+1)/32;
439                 // El drag es de un item
440                 if (selection_data->format == 10) {
441                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
442                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
443                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
444                         }
445                 }
446
447                 // El Drag es desde la barra de tareas
448                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
449                         CItem *a;
450                         if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
451                         a = new Splitter();
452                         else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
453                         a = new Conduct();
454                         else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
455                         a = new Union();
456                         else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
457                         a = new Cistern();
458                         else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
459                         a =new Pump();
460                         else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
461                         a = new Exclusa();
462                         else if (strcmp((const char *)selection_data->data, "drain_n.png")==0) 
463                         a = new Drain();
464                         else if (strcmp((const char *)selection_data->data, "and_e.png")==0){ 
465                         a = new And(); a->is_logic = true;
466                         } else if (strcmp((const char *)selection_data->data, "or_e.png")==0) {
467                                 a = new Or(); a->is_logic = true;
468                                 } else if (strcmp((const char *)selection_data->data, "not_e.png")==0) {
469                                         a = new Not(); a->is_logic = true;
470                                         }
471                 
472                         char char_id[10];
473                         sprintf(char_id,"%d",id);
474                         a->set_id(id++);                
475                         a->set_name( a->get_name()+char_id );
476                         if ( can_drop(a, i*32, j*32) ) {
477                                 workplace->put(*a, i*32, j*32);
478                                 //Apunto al workplace
479                                 a->workplace= workplace;
480                                 //Apunto a la lista.
481                                 a->combo_entry = combo_entry;
482                                 //Apunto a la listaItems.
483                                 a->listaItems = &listaItems;
484                                 //Apunto a la lista de items logicos
485                                 a->lista_logic_Items = &lista_logic_Items;
486                                 //Seteo la posicion del item
487                                 a->set_position(i*32,j*32);
488                                 // Seteo la lista de tipos de drags 
489                                 a->drag_source_set(listTargets);
490                                 // Conecto las señales
491                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
492                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
493                                 // parametro extra, en este caso un CItem *. Esto se hace para
494                                 // que cuando el usuario quiera mover un item, saber que item es
495                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
496                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
497                                 // raro se vea el widget tal cual.
498                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
499                                 a->show();      
500                                 if (! a->is_logic )
501                                         listaItems.push_back(a);
502                                 else 
503                                         lista_logic_Items.push_back(a);
504                         } else {
505                                         id--;
506                                         delete a;
507                                 }
508                 }
509                 context->drag_finish(false, false, time);
510         }
511         workplace->update_logic_position();
512         workplace->queue_draw();
513 }
514
515 void Constructor::on_btn_check_clicked()
516 {
517         std::list<CItem *>::iterator i = listaItems.begin();
518         while ( i != listaItems.end() ){
519                 (*i)->set_default_connector();
520                 (*i)->is_connected = false;
521                 i++;
522         }
523         
524         i = listaItems.begin();
525         if ( !listaItems.empty() ) {
526                 while ( i != listaItems.end() ) {
527                         CItem *temp = *i;
528                         std::cout<< "item="<<temp->get_name()<<" "<<"check= "<<temp->check_connection()<<std::endl;
529                         if ( !temp->check_connection() ) {
530                                 dlg_connect->set_title("Error");
531                                 dlg_label->set_text("Los elementos no estan conectados\n\t\tcorrectamente");
532                                 dlg_connect->show();
533                                 return;
534                         }       
535                 i++;
536                 }
537                 dlg_connect->set_title("Conexion");
538                 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
539                 dlg_connect->show();    
540         }
541         std::cout<<"check_connection logic"<<std::endl;
542         std::list<CItem *>::iterator j = lista_logic_Items.begin();
543         while ( j != lista_logic_Items.end() ) {
544                 (*j)->check_connection();
545                 j++;
546         }
547
548
549 }
550
551 Pump *Constructor::loadBomba(xmlNodePtr nodo)
552 {
553         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
554         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
555         int orientacion=0, x, y;
556         float flujo;
557
558         nodo = nodo->children;
559         while (nodo != NULL) {
560                 if (nodo->type == XML_ELEMENT_NODE) {
561                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
562                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
563                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
564                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
565                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
566                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
567                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
568                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
569                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
570                                 // FIXME !
571                         }
572                 }
573                 nodo = nodo->next;
574         }
575
576         Pump *p = new Pump(orientacion);
577         p->set_position(x,y);
578         p->set_entrega(flujo);
579         p->set_id( atoi(id.c_str()) );
580         p->set_name(name);
581
582         return p;
583 }
584
585 Conduct *Constructor::loadConduct(xmlNodePtr nodo)
586 {
587         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
588         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
589         int orientacion=0, x, y;
590         float flujo;
591
592         nodo = nodo->children;
593         while (nodo != NULL) {
594                 if (nodo->type == XML_ELEMENT_NODE) {
595                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
596                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
597                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
598                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
599                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
600                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
601                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
602                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
603                         }
604                 }
605                 nodo = nodo->next;
606         }
607
608         Conduct *p = new Conduct(orientacion);
609         p->set_position(x,y);
610         p->set_caudal(flujo);
611         p->set_id( atoi(id.c_str()) );
612         p->set_name(name);
613
614         return p;
615 }
616
617 Exclusa *Constructor::loadExclusa(xmlNodePtr nodo)
618 {
619         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
620         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
621         std::string estado;
622         int orientacion=0, x, y;
623         float flujo;
624
625         nodo = nodo->children;
626         while (nodo != NULL) {
627                 if (nodo->type == XML_ELEMENT_NODE) {
628                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
629                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
630                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
631                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
632                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
633                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
634                         } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
635                                 estado = (char *)XML_GET_CONTENT(nodo->children);
636                         }
637                 }
638                 nodo = nodo->next;
639         }
640
641         Exclusa *p = new Exclusa(orientacion);
642         p->set_position(x,y);
643         p->set_id( atoi(id.c_str()) );
644         p->set_name(name);
645         p->set_estado( estado == "1" );
646
647         return p;
648 }
649
650 Cistern *Constructor::loadTank(xmlNodePtr nodo)
651 {
652         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
653         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
654         int orientacion=0, x, y;
655         float liquido,capacidad;
656
657         nodo = nodo->children;
658         while (nodo != NULL) {
659                 if (nodo->type == XML_ELEMENT_NODE) {
660                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
661                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
662                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
663                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
664                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
665                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
666                         } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
667                                 capacidad = atof ((char *)XML_GET_CONTENT(nodo->children) );
668                         } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
669                                 liquido = atof ((char *)XML_GET_CONTENT(nodo->children) );
670                         }
671                 }
672                 nodo = nodo->next;
673         }
674
675         Cistern *p = new Cistern(orientacion);
676         p->set_position(x,y);
677         p->set_id( atoi(id.c_str()) );
678         p->set_name(name);
679         p->set_capacidad(capacidad);
680         p->set_contenido_inicial(liquido);
681
682         return p;
683 }
684
685 Union *Constructor::loadUnion(xmlNodePtr nodo)
686 {
687         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
688         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
689         std::string tipo;
690         int orientacion=0, x, y;
691         float caudal;
692
693         nodo = nodo->children;
694         while (nodo != NULL) {
695                 if (nodo->type == XML_ELEMENT_NODE) {
696                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
697                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
698                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
699                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
700                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
701                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
702                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
703                                 caudal = atof ((char *)XML_GET_CONTENT(nodo->children) );
704                         } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
705                                 tipo = (char *)XML_GET_CONTENT(nodo->children);
706                         }
707                 }
708                 nodo = nodo->next;
709         }
710
711         Union *p = new Union(orientacion);
712         p->set_position(x,y);
713         p->set_id( atoi(id.c_str()) );
714         p->set_name(name);
715         p->set_caudal(caudal);
716         p->is_union = (tipo == "union");
717
718         return p;
719 }
720
721 Drain *Constructor::loadDrain(xmlNodePtr nodo)
722 {
723         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
724         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
725         int orientacion=0, x, y;
726
727         nodo = nodo->children;
728         while (nodo != NULL) {
729                 if (nodo->type == XML_ELEMENT_NODE) {
730                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
731                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
732                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
733                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
734                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
735                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
736                         }
737                 }
738                 nodo = nodo->next;
739         }
740
741         Drain *p = new Drain(orientacion);
742         p->set_position(x,y);
743         p->set_id( atoi(id.c_str()) );
744         p->set_name(name);
745
746         return p;
747 }
748
749 Splitter *Constructor::loadCodo(xmlNodePtr nodo)
750 {
751         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
752         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
753         int orientacion=0, x, y;
754         float caudal;
755
756         nodo = nodo->children;
757         while (nodo != NULL) {
758                 if (nodo->type == XML_ELEMENT_NODE) {
759                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
760                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
761                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
762                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
763                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
764                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
765                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
766                                 caudal = atof( (char *)XML_GET_CONTENT(nodo->children) );
767                         }
768                 }
769                 nodo = nodo->next;
770         }
771
772         Splitter *p = new Splitter(orientacion);
773         p->set_position(x,y);
774         p->set_id( atoi(id.c_str()) );
775         p->set_name(name);
776         p->set_caudal(caudal);
777
778         return p;
779 }