]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/constructor.cpp
87a943bb72e924fcc54aab2949dc7b1f104e8f62
[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         file_selection->show();
193 }
194
195 void Constructor::on_edit_menu_del()
196
197         workplace->delete_item(WorkPlace::pointed);
198 }
199
200 void Constructor::on_edit_menu_delete_all()
201 {
202         std::list<CItem *>::iterator i = listaItems.begin();
203         while ( i != listaItems.end() ) {
204                 (*i)->workplace->delete_item((*i)->get_id());
205                 i = listaItems.begin();
206         }
207         i = lista_logic_Items.begin();
208         while ( i != lista_logic_Items.end() ) {
209                 (*i)->workplace->delete_item((*i)->get_id());
210                 i=lista_logic_Items.begin();
211         }
212         id = 0;
213 }
214
215 void Constructor::on_chk_btn_clicked()
216 {
217         logica = !logica;
218         CItem::logic_connect = !CItem::logic_connect;
219 }
220
221 void Constructor::on_load_from_xml()
222 {
223         on_edit_menu_delete_all();
224         file_name = file_open_selection->get_filename();
225
226         /* Parseo de ejemplo de un XML desde archivo */
227         xmlDocPtr document;
228         document = xmlParseFile(file_name.c_str());
229         if (document == NULL) {
230                 // TODO : dar un aviso de que no se pudo abrir el archivo!!
231                 return;
232         }
233
234         /* bien, el archivo se parseo bien! */
235         xmlNodePtr nodo, items;
236         nodo = document->children;
237
238         // Recorro los items
239         CItem *current;
240         if (strcmp((char *)nodo->name, "planta") == 0) {
241                 items = nodo->children;
242                 while (items != NULL) {
243                         current = NULL;
244                         if (items->type == XML_ELEMENT_NODE) {
245                                 std::cout << "ITEM" << std::endl;
246                                 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
247                                         current = loadBomba(items);
248                                 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
249                                         current = loadConduct(items);
250                                 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
251                                         current = loadCodo(items);
252                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
253                                         current = loadExclusa(items);
254                                 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
255                                         current = loadTank(items);
256                                 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
257                                         current = loadUnion(items);
258                                 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
259                                         current = loadDrain(items);
260                                 }
261
262                                 if (current != NULL) {
263                                         // Agrego y conecto la bomba
264                                         listaItems.push_back(current);
265                                         current->drag_source_set(listTargets);
266                                         workplace->put(*current, current->get_position_x(), current->get_position_y());
267                                         //Apunto al workplace
268                                         current->workplace= workplace;
269                                         //Apunto a la lista.
270                                         current->combo_entry = combo_entry;
271                                         //Apunto a la listaItems.
272                                         current->listaItems = &listaItems;
273                                         //Apunto a la lista de items logicos
274                                         current->lista_logic_Items = &lista_logic_Items;
275                                         // Conecto las señales
276                                         current->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
277                                         current->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), current));
278                                         current->show();        
279                                 }
280                         }
281                         items = items->next;
282                 }
283         } else {
284                 // TODO : avisar que el XML no es valido!!
285         }
286         std::cout << "CARGA COMPLETA" << std::endl;
287         file_open_selection->hide();
288         id = listaItems.size()+lista_logic_Items.size()+1;
289 }
290
291 void Constructor::on_btn_file_ok_clicked()
292 {
293         std::list<CItem *>::iterator i = listaItems.begin();
294         file_name = file_selection->get_filename();
295         if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
296                 fprintf(archivo, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
297                 fprintf (archivo,"<planta>\n");
298                 while ( i != listaItems.end() ){
299                         (*i)->save(archivo);
300                         i++;
301                 }
302                 i = lista_logic_Items.begin();
303                 while ( i != lista_logic_Items.end() ) {
304                         (*i)->save(archivo);
305                         i++;
306                 }
307                 fprintf(archivo,"</planta>\n");
308                 saved = true;
309         } else 
310                 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
311         fclose(archivo);
312         file_selection->hide();
313 }
314
315 void Constructor::on_quick_btn_save_clicked()
316 {
317         if ( ! saved ) {
318                 on_main_menu_save();
319         } else {
320                 on_btn_file_ok_clicked();
321         }
322 }
323         
324 void Constructor::on_quick_btn_new_clicked()
325 {
326         on_quick_btn_delete_all_clicked();
327         saved = false;
328 }
329
330 void Constructor::on_quick_btn_delete_all_clicked()
331 {
332         std::list<CItem *>::iterator i = listaItems.begin();
333         while ( i != listaItems.end() ){
334                 CItem *temp = *i;
335                 listaItems.erase(i);
336                 delete temp;
337                 i = listaItems.begin();
338         }
339         i = lista_logic_Items.begin();
340         while ( i != lista_logic_Items.end() ){
341                 CItem *temp2 = *i;
342                 lista_logic_Items.erase(i);
343                 delete temp2;
344                 i = lista_logic_Items.begin();
345         }
346         id = 0;
347 }       
348
349 void Constructor::on_btn_file_cancel_clicked()
350 {
351         file_selection->hide();
352 }
353
354 void Constructor::on_btn_open_cancel_clicked()
355 {
356         file_open_selection->hide();
357 }
358
359 void Constructor::on_btn_dlg_connect_clicked()
360 {
361         dlg_connect->hide();
362 }
363
364 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
365 {
366         context->set_icon(ico_canio, 5, 5); 
367 }
368
369 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
370 {
371         context->set_icon(ico_y, 5, 5); 
372 }
373
374 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
375 {
376         context->set_icon(ico_codo, 5, 5); 
377 }
378
379 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
380 {
381         context->set_icon(ico_tanque, 5, 5); 
382 }
383
384 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
385 {
386         context->set_icon(ico_bomba, 5, 5); 
387 }
388
389 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
390 {
391         context->set_icon(ico_exclusa, 5, 5); 
392 }
393
394 void Constructor::on_drain_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
395 {
396         context->set_icon(ico_drain, 5, 5); 
397 }
398
399 void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
400 {
401         context->set_icon(ico_and, 5, 5); 
402 }
403
404 void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
405 {
406         context->set_icon(ico_or, 5, 5); 
407 }
408
409 void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
410 {
411         context->set_icon(ico_not, 5, 5); 
412 }
413
414 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
415 {
416         context->set_icon(item->get_image(), 5, 5);
417 }
418
419 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
420 {       
421         gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
422 }
423
424 bool Constructor::can_drop(CItem *item, int _x, int _y)
425 {
426         std::list<CItem*>::iterator  i = listaItems.begin();
427         while( i != listaItems.end() ) {
428                 CItem *temp = *i;
429                 if ( temp->get_id() != item->get_id()   ) {
430                         if (  (temp->is_occupied_area(_x, _y))  ||  
431                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
432                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
433                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
434                                 return false;
435                         else i++;
436                 }
437                 else i++;
438         }
439         return true;
440 }
441
442 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
443 {
444         workplace->update_logic_position();
445         workplace->queue_draw();
446         if ( !logica ) {
447                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
448                 int i,j;
449                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
450                 // separacion de 2 cuadritos
451                 i = (x+1)/32;
452                 j = (y+1)/32;
453                 // El drag es de un item
454                 if (selection_data->format == 10) {
455                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
456                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
457                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
458                         }
459                 }
460
461                 // El Drag es desde la barra de tareas
462                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
463                         CItem *a;
464                         if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
465                         a = new Splitter();
466                         else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
467                         a = new Conduct();
468                         else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
469                         a = new Union();
470                         else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
471                         a = new Cistern();
472                         else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
473                         a =new Pump();
474                         else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
475                         a = new Exclusa();
476                         else if (strcmp((const char *)selection_data->data, "drain_n.png")==0) 
477                         a = new Drain();
478                         else if (strcmp((const char *)selection_data->data, "and_e.png")==0){ 
479                         a = new And(); a->is_logic = true;
480                         } else if (strcmp((const char *)selection_data->data, "or_e.png")==0) {
481                                 a = new Or(); a->is_logic = true;
482                                 } else if (strcmp((const char *)selection_data->data, "not_e.png")==0) {
483                                         a = new Not(); a->is_logic = true;
484                                         }
485                 
486                         char char_id[10];
487                         sprintf(char_id,"%d",id);
488                         a->set_id(id++);                
489                         a->set_name( a->get_name()+char_id );
490                         if ( can_drop(a, i*32, j*32) ) {
491                                 workplace->put(*a, i*32, j*32);
492                                 //Apunto al workplace
493                                 a->workplace= workplace;
494                                 //Apunto a la lista.
495                                 a->combo_entry = combo_entry;
496                                 //Apunto a la listaItems.
497                                 a->listaItems = &listaItems;
498                                 //Apunto a la lista de items logicos
499                                 a->lista_logic_Items = &lista_logic_Items;
500                                 //Seteo la posicion del item
501                                 a->set_position(i*32,j*32);
502                                 // Seteo la lista de tipos de drags 
503                                 a->drag_source_set(listTargets);
504                                 // Conecto las señales
505                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
506                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
507                                 // parametro extra, en este caso un CItem *. Esto se hace para
508                                 // que cuando el usuario quiera mover un item, saber que item es
509                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
510                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
511                                 // raro se vea el widget tal cual.
512                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
513                                 a->show();      
514                                 if (! a->is_logic )
515                                         listaItems.push_back(a);
516                                 else 
517                                         lista_logic_Items.push_back(a);
518                         } else {
519                                         id--;
520                                         delete a;
521                                 }
522                 }
523                 context->drag_finish(false, false, time);
524         }
525         workplace->update_logic_position();
526         workplace->queue_draw();
527 }
528
529 void Constructor::on_btn_check_clicked()
530 {
531         std::list<CItem *>::iterator i = listaItems.begin();
532         while ( i != listaItems.end() ){
533                 (*i)->set_default_connector();
534                 (*i)->is_connected = false;
535                 i++;
536         }
537         
538         i = listaItems.begin();
539         if ( !listaItems.empty() ) {
540                 while ( i != listaItems.end() ) {
541                         CItem *temp = *i;
542                         std::cout<< "item="<<temp->get_name()<<" "<<"check= "<<temp->check_connection()<<std::endl;
543                         if ( !temp->check_connection() ) {
544                                 dlg_connect->set_title("Error");
545                                 dlg_label->set_text("El elemento "+temp->get_name()+" no esta conectado\n\t\tcorrectamente");
546                                 dlg_connect->show();
547                                 return;
548                         }       
549                 i++;
550                 }
551                 dlg_connect->set_title("Conexion");
552                 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
553                 dlg_connect->show();    
554         }
555         std::cout<<"check_connection logic"<<std::endl;
556         std::list<CItem *>::iterator j = lista_logic_Items.begin();
557         while ( j != lista_logic_Items.end() ) {
558                 (*j)->check_connection();
559                 j++;
560         }
561
562
563 }
564
565 Pump *Constructor::loadBomba(xmlNodePtr nodo)
566 {
567         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
568         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
569         int orientacion=0, x, y;
570         float flujo;
571
572         nodo = nodo->children;
573         while (nodo != NULL) {
574                 if (nodo->type == XML_ELEMENT_NODE) {
575                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
576                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
577                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
578                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
579                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
580                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
581                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
582                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
583                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
584                                 // FIXME !
585                         }
586                 }
587                 nodo = nodo->next;
588         }
589
590         Pump *p = new Pump(orientacion);
591         p->set_position(x,y);
592         p->set_entrega(flujo);
593         p->set_id( atoi(id.c_str()) );
594         p->set_name(name);
595
596         return p;
597 }
598
599 Conduct *Constructor::loadConduct(xmlNodePtr nodo)
600 {
601         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
602         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
603         int orientacion=0, x, y;
604         float flujo;
605
606         nodo = nodo->children;
607         while (nodo != NULL) {
608                 if (nodo->type == XML_ELEMENT_NODE) {
609                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
610                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
611                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
612                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
613                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
614                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
615                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
616                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
617                         }
618                 }
619                 nodo = nodo->next;
620         }
621
622         Conduct *p = new Conduct(orientacion);
623         p->set_position(x,y);
624         p->set_caudal(flujo);
625         p->set_id( atoi(id.c_str()) );
626         p->set_name(name);
627
628         return p;
629 }
630
631 Exclusa *Constructor::loadExclusa(xmlNodePtr nodo)
632 {
633         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
634         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
635         std::string estado;
636         int orientacion=0, x, y;
637         float flujo;
638
639         nodo = nodo->children;
640         while (nodo != NULL) {
641                 if (nodo->type == XML_ELEMENT_NODE) {
642                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
643                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
644                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
645                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
646                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
647                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
648                         } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
649                                 estado = (char *)XML_GET_CONTENT(nodo->children);
650                         }
651                 }
652                 nodo = nodo->next;
653         }
654
655         Exclusa *p = new Exclusa(orientacion);
656         p->set_position(x,y);
657         p->set_id( atoi(id.c_str()) );
658         p->set_name(name);
659         p->set_estado( estado == "1" );
660
661         return p;
662 }
663
664 Cistern *Constructor::loadTank(xmlNodePtr nodo)
665 {
666         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
667         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
668         int orientacion=0, x, y;
669         float liquido,capacidad;
670
671         nodo = nodo->children;
672         while (nodo != NULL) {
673                 if (nodo->type == XML_ELEMENT_NODE) {
674                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
675                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
676                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
677                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
678                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
679                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
680                         } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
681                                 capacidad = atof ((char *)XML_GET_CONTENT(nodo->children) );
682                         } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
683                                 liquido = atof ((char *)XML_GET_CONTENT(nodo->children) );
684                         }
685                 }
686                 nodo = nodo->next;
687         }
688
689         Cistern *p = new Cistern(orientacion);
690         p->set_position(x,y);
691         p->set_id( atoi(id.c_str()) );
692         p->set_name(name);
693         p->set_capacidad(capacidad);
694         p->set_contenido_inicial(liquido);
695
696         return p;
697 }
698
699 Union *Constructor::loadUnion(xmlNodePtr nodo)
700 {
701         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
702         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
703         std::string tipo;
704         int orientacion=0, x, y;
705         float caudal;
706
707         nodo = nodo->children;
708         while (nodo != NULL) {
709                 if (nodo->type == XML_ELEMENT_NODE) {
710                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
711                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
712                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
713                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
714                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
715                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
716                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
717                                 caudal = atof ((char *)XML_GET_CONTENT(nodo->children) );
718                         } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
719                                 tipo = (char *)XML_GET_CONTENT(nodo->children);
720                         }
721                 }
722                 nodo = nodo->next;
723         }
724
725         Union *p = new Union(orientacion);
726         p->set_position(x,y);
727         p->set_id( atoi(id.c_str()) );
728         p->set_name(name);
729         p->set_caudal(caudal);
730         p->is_union = (tipo == "union");
731
732         return p;
733 }
734
735 Drain *Constructor::loadDrain(xmlNodePtr nodo)
736 {
737         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
738         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
739         int orientacion=0, x, y;
740
741         nodo = nodo->children;
742         while (nodo != NULL) {
743                 if (nodo->type == XML_ELEMENT_NODE) {
744                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
745                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
746                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
747                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
748                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
749                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
750                         }
751                 }
752                 nodo = nodo->next;
753         }
754
755         Drain *p = new Drain(orientacion);
756         p->set_position(x,y);
757         p->set_id( atoi(id.c_str()) );
758         p->set_name(name);
759
760         return p;
761 }
762
763 Splitter *Constructor::loadCodo(xmlNodePtr nodo)
764 {
765         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
766         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
767         int orientacion=0, x, y;
768         float caudal;
769
770         nodo = nodo->children;
771         while (nodo != NULL) {
772                 if (nodo->type == XML_ELEMENT_NODE) {
773                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
774                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
775                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
776                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
777                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
778                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
779                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
780                                 caudal = atof( (char *)XML_GET_CONTENT(nodo->children) );
781                         }
782                 }
783                 nodo = nodo->next;
784         }
785
786         Splitter *p = new Splitter(orientacion);
787         p->set_position(x,y);
788         p->set_id( atoi(id.c_str()) );
789         p->set_name(name);
790         p->set_caudal(caudal);
791
792         return p;
793 }