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