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