]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/constructor.cpp
aregle lo del id, pero seria conveniente que hagan la prueba, creo que anda bien
[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         
323         int id_1 = -1 , id_2 = -1;
324         std::list<CItem *>::iterator i = listaItems.begin();
325         while ( i != listaItems.end() ){
326                 if ( (*i)->get_id() >= id_1 ) 
327                         id_1 = (*i)->get_id();
328         i++;
329         }
330         id_1++;
331         
332         i = lista_logic_Items.begin();
333         while ( i != lista_logic_Items.end() ){
334                 if ( (*i)->get_id() >= id_2 ) 
335                         id_2 = (*i)->get_id();
336         i++;
337         }
338         id_2++;
339         if ( id_1 < id_2 ) id = id_2;
340         else id = id_1;
341 }
342
343 void Constructor::on_btn_file_ok_clicked()
344 {
345         std::list<CItem *>::iterator i = listaItems.begin();
346         file_name = file_selection->get_filename();
347         Glib::ustring name;
348         if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
349                 fprintf(archivo, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
350                 fprintf (archivo,"<planta>\n");
351                 while ( i != listaItems.end() ){
352                         (*i)->save(archivo);
353                         i++;
354                 }
355                 i = lista_logic_Items.begin();
356                 while ( i != lista_logic_Items.end() ) {
357                         (*i)->save(archivo);
358                         i++;
359                 }
360                 fprintf(archivo,"</planta>\n");
361                 saved = true;
362                 fclose(archivo);
363         } else 
364                 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
365         file_selection->hide();
366 }
367
368 void Constructor::on_quick_btn_save_clicked()
369 {
370         if ( ! saved ) {
371                 on_main_menu_save();
372         } else {
373                 on_btn_file_ok_clicked();
374         }
375 }
376         
377 void Constructor::on_quick_btn_new_clicked()
378 {
379         on_quick_btn_delete_all_clicked();
380         saved = false;
381 }
382
383 void Constructor::on_quick_btn_delete_all_clicked()
384 {
385         on_edit_menu_delete_all();
386 }       
387
388 void Constructor::on_btn_find_clicked()
389 {
390         CItem * temp = NULL;
391         std::list<CItem *>::iterator i = listaItems.begin();
392         while ( i !=listaItems.end() ) {
393                 if ( (*i)->get_name()  == combo_entry->get_entry()->get_text() ) {
394                         temp = *i;
395                         break;
396                 }
397                 i++;
398         }
399         
400         i = lista_logic_Items.begin();
401         while ( i !=lista_logic_Items.end() ) {
402                 if ( (*i)->get_name()  == combo_entry->get_entry()->get_text() ) {
403                         temp = *i;
404                         break;
405                 }
406                 i++;
407         }
408         if ( temp != NULL )
409         workplace->get_window()->draw_rectangle(workplace->get_style()->get_black_gc(), false , temp->get_position_x()-5, temp->get_position_y()-5,
410                                                                                                                                                 temp->get_image()->get_width()+10, temp->get_image()->get_height()+10);
411 }
412
413 void Constructor::on_btn_file_cancel_clicked()
414 {
415         file_selection->hide();
416 }
417
418 void Constructor::on_btn_open_cancel_clicked()
419 {
420         file_open_selection->hide();
421 }
422
423 void Constructor::on_btn_dlg_connect_clicked()
424 {
425         dlg_connect->hide();
426 }
427
428 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
429 {
430         context->set_icon(ico_canio, 5, 5); 
431 }
432
433 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
434 {
435         context->set_icon(ico_y, 5, 5); 
436 }
437
438 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
439 {
440         context->set_icon(ico_codo, 5, 5); 
441 }
442
443 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
444 {
445         context->set_icon(ico_tanque, 5, 5); 
446 }
447
448 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
449 {
450         context->set_icon(ico_bomba, 5, 5); 
451 }
452
453 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
454 {
455         context->set_icon(ico_exclusa, 5, 5); 
456 }
457
458 void Constructor::on_drain_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
459 {
460         context->set_icon(ico_drain, 5, 5); 
461 }
462
463 void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
464 {
465         context->set_icon(ico_and, 5, 5); 
466 }
467
468 void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
469 {
470         context->set_icon(ico_or, 5, 5); 
471 }
472
473 void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
474 {
475         context->set_icon(ico_not, 5, 5); 
476 }
477
478 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
479 {
480         context->set_icon(item->get_image(), 5, 5);
481 }
482
483 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
484 {       
485         gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
486 }
487
488 bool Constructor::can_drop(CItem *item, int _x, int _y)
489 {
490         std::list<CItem*>::iterator  i = listaItems.begin();
491         while( i != listaItems.end() ) {
492                 CItem *temp = *i;
493                 if ( temp->get_id() != item->get_id()   ) {
494                         if (  (temp->is_occupied_area(_x, _y))  ||  
495                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
496                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
497                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
498                                 return false;
499                         else i++;
500                 }
501                 else i++;
502         }
503         return true;
504 }
505
506 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
507 {
508         workplace->update_logic_position();
509         workplace->queue_draw();
510         if ( !logica ) {
511                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
512                 int i,j;
513                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
514                 // separacion de 2 cuadritos
515                 i = (x+1)/32;
516                 j = (y+1)/32;
517                 // El drag es de un item
518                 if (selection_data->format == 10) {
519                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
520                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
521                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
522                         }
523                 }
524
525                 // El Drag es desde la barra de tareas
526                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
527                         CItem *a;
528                         if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
529                         a = new Splitter();
530                         else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
531                         a = new Conduct();
532                         else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
533                         a = new Union();
534                         else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
535                         a = new Cistern();
536                         else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
537                         a =new Pump();
538                         else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
539                         a = new Exclusa();
540                         else if (strcmp((const char *)selection_data->data, "drain_n.png")==0) 
541                         a = new Drain();
542                         else if (strcmp((const char *)selection_data->data, "and_e.png")==0){ 
543                         a = new And(); a->is_logic = true;
544                         } else if (strcmp((const char *)selection_data->data, "or_e.png")==0) {
545                                 a = new Or(); a->is_logic = true;
546                                 } else if (strcmp((const char *)selection_data->data, "not_e.png")==0) {
547                                         a = new Not(); a->is_logic = true;
548                                         }
549                 
550                         char char_id[10];
551                         sprintf(char_id,"%d",id);
552                         a->set_id(id++);                
553                         a->set_name( a->get_name()+char_id );
554                         if ( can_drop(a, i*32, j*32) ) {
555                                 workplace->put(*a, i*32, j*32);
556                                 //Apunto al workplace
557                                 a->workplace = workplace;
558                                 //Apunto a la lista.
559                                 a->combo_entry = combo_entry;
560                                 //Apunto a la lista de apuntados
561                                 a->list_pointed = &list_pointed;
562                                 //Apunto a la listaItems.
563                                 a->listaItems = &listaItems;
564                                 //Apunto a la barra de estado
565                                 a->status_bar = status_bar;
566                                 //Apunto a la lista de items logicos
567                                 a->lista_logic_Items = &lista_logic_Items;
568                                 //Seteo la posicion del item
569                                 a->set_position(i*32,j*32);
570                                 // Seteo la lista de tipos de drags 
571                                 a->drag_source_set(listTargets);
572                                 // Conecto las señales
573                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
574                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
575                                 // parametro extra, en este caso un CItem *. Esto se hace para
576                                 // que cuando el usuario quiera mover un item, saber que item es
577                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
578                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
579                                 // raro se vea el widget tal cual.
580                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
581                                 a->show();      
582                                 if (! a->is_logic )
583                                         listaItems.push_back(a);
584                                 else 
585                                         lista_logic_Items.push_back(a);
586                         } else {
587                                         id--;
588                                         delete a;
589                                 }
590                 }
591                 context->drag_finish(false, false, time);
592         }
593         workplace->update_logic_position();
594         workplace->queue_draw();
595 }
596
597 void Constructor::on_btn_check_clicked()
598 {
599         Glib::ustring name;
600         if ( ! check_connection(name) ) {
601                 dlg_connect->set_title("Error");
602                 dlg_label->set_text("El elemento "+name+" no esta conectado\n\t\tcorrectamente");
603                 dlg_connect->show();
604         } else {
605                 dlg_connect->set_title("Conexion");
606                 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
607                 dlg_connect->show();
608         }
609 }
610
611
612 bool Constructor::check_connection(Glib::ustring& name)
613 {
614         std::list<CItem *>::iterator i = listaItems.begin();
615         while ( i != listaItems.end() ){
616                 (*i)->set_default_connector();
617                 (*i)->is_connected = false;
618                 i++;
619         }
620         i = listaItems.begin();
621         if ( !listaItems.empty() ) {
622                 while ( i != listaItems.end() ) {
623                         CItem *temp = *i;
624                         std::cout<< "item="<<temp->get_name()<<" "<<"check= "<<temp->check_connection()<<std::endl;
625                         if ( !temp->check_connection() ) {
626                                 name = temp->get_name();
627                                 return false;
628                         }       
629                 i++;
630                 }
631         }
632         std::cout<<"check_connection logic"<<std::endl;
633         std::list<CItem *>::iterator j = lista_logic_Items.begin();
634         while ( j != lista_logic_Items.end() ) {
635                 if ( !(*j)->check_connection() ) {
636                         name = (*j)->get_name();
637                         return false;
638                 }
639                 j++;
640         }
641         return true;
642 }       
643
644 Not *Constructor::loadNot(xmlNodePtr nodo)
645 {
646         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
647         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
648         int orientacion=0, x, y;
649
650         nodo = nodo->children;
651         while (nodo != NULL) {
652                 if (nodo->type == XML_ELEMENT_NODE) {
653                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
654                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
655                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
656                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
657                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
658                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
659                         }
660                 }
661                 nodo = nodo->next;
662         }
663
664         Not *p = new Not(orientacion);
665         p->set_position(x,y);
666         p->set_id( atoi(id.c_str()) );
667         p->set_name(name);
668
669         return p;
670 }
671
672 Or *Constructor::loadOr(xmlNodePtr nodo)
673 {
674         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
675         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
676         int orientacion=0, x, y;
677
678         nodo = nodo->children;
679         while (nodo != NULL) {
680                 if (nodo->type == XML_ELEMENT_NODE) {
681                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
682                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
683                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
684                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
685                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
686                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
687                         }
688                 }
689                 nodo = nodo->next;
690         }
691
692         Or *p = new Or(orientacion);
693         p->set_position(x,y);
694         p->set_id( atoi(id.c_str()) );
695         p->set_name(name);
696
697         return p;
698 }
699
700 And *Constructor::loadAnd(xmlNodePtr nodo)
701 {
702         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
703         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
704         int orientacion=0, x, y;
705         float flujo;
706         xmlNodePtr inicial = nodo;
707
708         nodo = nodo->children;
709         while (nodo != NULL) {
710                 if (nodo->type == XML_ELEMENT_NODE) {
711                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
712                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
713                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
714                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
715                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
716                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
717                         }
718                 }
719                 nodo = nodo->next;
720         }
721
722         And *p = new And(orientacion);
723         p->set_position(x,y);
724         p->set_id( atoi(id.c_str()) );
725         p->set_name(name);
726         return p;
727 }
728
729 Pump *Constructor::loadBomba(xmlNodePtr nodo)
730 {
731         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
732         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
733         int orientacion=0, x, y;
734         float flujo;
735
736         nodo = nodo->children;
737         while (nodo != NULL) {
738                 if (nodo->type == XML_ELEMENT_NODE) {
739                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
740                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
741                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
742                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
743                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
744                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
745                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
746                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
747                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
748                                 // FIXME !
749                         }
750                 }
751                 nodo = nodo->next;
752         }
753
754         Pump *p = new Pump(orientacion);
755         p->set_position(x,y);
756         p->set_entrega(flujo);
757         p->set_id( atoi(id.c_str()) );
758         p->set_name(name);
759
760         return p;
761 }
762
763 Conduct *Constructor::loadConduct(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 flujo;
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                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
781                         }
782                 }
783                 nodo = nodo->next;
784         }
785
786         Conduct *p = new Conduct(orientacion);
787         p->set_position(x,y);
788         p->set_caudal(flujo);
789         p->set_id( atoi(id.c_str()) );
790         p->set_name(name);
791
792         return p;
793 }
794
795 Exclusa *Constructor::loadExclusa(xmlNodePtr nodo)
796 {
797         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
798         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
799         std::string estado;
800         int orientacion=0, x, y;
801         float flujo;
802
803         nodo = nodo->children;
804         while (nodo != NULL) {
805                 if (nodo->type == XML_ELEMENT_NODE) {
806                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
807                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
808                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
809                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
810                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
811                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
812                         } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
813                                 estado = (char *)XML_GET_CONTENT(nodo->children);
814                         }
815                 }
816                 nodo = nodo->next;
817         }
818
819         Exclusa *p = new Exclusa(orientacion);
820         p->set_position(x,y);
821         p->set_id( atoi(id.c_str()) );
822         p->set_name(name);
823         p->set_estado( estado == "1" );
824
825         return p;
826 }
827
828 Cistern *Constructor::loadTank(xmlNodePtr nodo)
829 {
830         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
831         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
832         int orientacion=0, x, y;
833         float liquido,capacidad;
834
835         nodo = nodo->children;
836         while (nodo != NULL) {
837                 if (nodo->type == XML_ELEMENT_NODE) {
838                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
839                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
840                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
841                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
842                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
843                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
844                         } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
845                                 capacidad = atof ((char *)XML_GET_CONTENT(nodo->children) );
846                         } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
847                                 liquido = atof ((char *)XML_GET_CONTENT(nodo->children) );
848                         }
849                 }
850                 nodo = nodo->next;
851         }
852
853         Cistern *p = new Cistern(orientacion);
854         p->set_position(x,y);
855         p->set_id( atoi(id.c_str()) );
856         p->set_name(name);
857         p->set_capacidad(capacidad);
858         p->set_contenido_inicial(liquido);
859
860         return p;
861 }
862
863 Union *Constructor::loadUnion(xmlNodePtr nodo)
864 {
865         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
866         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
867         std::string tipo;
868         int orientacion=0, x, y;
869         float caudal;
870
871         nodo = nodo->children;
872         while (nodo != NULL) {
873                 if (nodo->type == XML_ELEMENT_NODE) {
874                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
875                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
876                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
877                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
878                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
879                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
880                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
881                                 caudal = atof ((char *)XML_GET_CONTENT(nodo->children) );
882                         } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
883                                 tipo = (char *)XML_GET_CONTENT(nodo->children);
884                         }
885                 }
886                 nodo = nodo->next;
887         }
888
889         Union *p = new Union(orientacion);
890         p->set_position(x,y);
891         p->set_id( atoi(id.c_str()) );
892         p->set_name(name);
893         p->set_caudal(caudal);
894         p->is_union = (tipo == "union");
895
896         return p;
897 }
898
899 Drain *Constructor::loadDrain(xmlNodePtr nodo)
900 {
901         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
902         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
903         int orientacion=0, x, y;
904
905         nodo = nodo->children;
906         while (nodo != NULL) {
907                 if (nodo->type == XML_ELEMENT_NODE) {
908                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
909                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
910                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
911                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
912                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
913                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
914                         }
915                 }
916                 nodo = nodo->next;
917         }
918
919         Drain *p = new Drain(orientacion);
920         p->set_position(x,y);
921         p->set_id( atoi(id.c_str()) );
922         p->set_name(name);
923
924         return p;
925 }
926
927 Splitter *Constructor::loadCodo(xmlNodePtr nodo)
928 {
929         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
930         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
931         int orientacion=0, x, y;
932         float caudal;
933
934         nodo = nodo->children;
935         while (nodo != NULL) {
936                 if (nodo->type == XML_ELEMENT_NODE) {
937                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
938                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
939                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
940                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
941                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
942                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
943                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
944                                 caudal = atof( (char *)XML_GET_CONTENT(nodo->children) );
945                         }
946                 }
947                 nodo = nodo->next;
948         }
949
950         Splitter *p = new Splitter(orientacion);
951         p->set_position(x,y);
952         p->set_id( atoi(id.c_str()) );
953         p->set_name(name);
954         p->set_caudal(caudal);
955
956         return p;
957 }
958
959 void Constructor::create_lines(xmlNodePtr nodo)
960 {
961         std::string name;
962         
963         nodo = nodo->children;
964         while (nodo != NULL) {
965                 if (nodo->type == XML_ELEMENT_NODE) {
966                         if (xmlStrcmp(nodo->name, BAD_CAST"and")==0) {
967                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
968                                 std::cout << name << std::endl;
969                                 create_line(nodo->children, workplace->get_logic_id(name));
970                         } else if (xmlStrcmp(nodo->name, BAD_CAST"not")==0) {
971                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
972                                 std::cout << name << std::endl;
973                                 create_line(nodo->children, workplace->get_logic_id(name));
974                         } else if (xmlStrcmp(nodo->name, BAD_CAST"or")==0) {
975                                 name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
976                                 std::cout << name << std::endl;
977                                 create_line(nodo->children, workplace->get_logic_id(name));
978                         }
979                 }
980                 nodo = nodo->next;
981         }
982 }
983
984 void Constructor::create_line(xmlNodePtr nodo, int logic_id)
985 {
986         std::string otro;
987         std::cout << "Buscando lineas ..." << std::endl;
988         while (nodo != NULL) {
989                 if (nodo->type == XML_ELEMENT_NODE) {
990                         if (xmlStrcmp(nodo->name, BAD_CAST"salida")==0) {
991                                 otro = (char *)XML_GET_CONTENT(nodo->children);
992                                 t_line tmp_line;
993                                 tmp_line.logic_id = logic_id;
994                                 //workplace->get_logic_item(logic_id)->set_out_connected(true);
995                                 tmp_line.store_id = workplace->get_item_id(otro);
996                                 std::cout << otro << " " << tmp_line.logic_id << " " << tmp_line.store_id << std::endl;
997                                 workplace->lista_lineas_in.push_back(tmp_line);
998                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada")==0) {
999                                 otro = (char *)XML_GET_CONTENT(nodo->children);
1000                                 t_line tmp_line;
1001                                 tmp_line.logic_id = logic_id;
1002                                 tmp_line.store_id = workplace->get_item_id(otro);
1003                                 workplace->lista_lineas_out.push_back(tmp_line);
1004                                 std::cout << otro << " " << tmp_line.logic_id << " " << tmp_line.store_id << std::endl;
1005                         }
1006                 }
1007                 nodo = nodo->next;
1008         }
1009 }