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