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