]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Client/src/principal.cpp
* Se modifica el modelo para que el tanque mando en el XML el estado logico de sus...
[z.facultad/75.42/plaqui.git] / Client / src / principal.cpp
1
2 #include "principal.h"
3 #include <iostream>
4 #include <sstream>
5 #include <string>
6 #include "item_codo.h"
7 #include "item_conduct.h"
8 #include "item_exclusa.h"
9 #include "item_tank.h"
10 #include "item_pump.h"
11 #include "item_union.h"
12 #include "item_drain.h"
13 #include "item_not.h"
14 #include "item_or.h"
15 #include "item_and.h"
16 #include <unistd.h>
17 #include <glibmm/thread.h>
18
19 #define INFINITO 99999999
20
21 Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &rg):Gtk::Window(co),refXml(rg)
22 {
23         Gtk::MenuItem *conect=0, *exit=0, *about=0, *mnu_prop=0, *mnu_disconnect=0, *server_stop=0;
24         Gtk::Button *bar_connect=0, *close_about=0;
25         Gtk::Image *plaqui_logo;
26         txt_view = 0;
27         work_place = 0;
28         lbl_cap_flujo = lbl_cap_extra = lbl_extra = lbl_nombre = lbl_color = lbl_flujo = 0;
29         btn_simulate = btn_pause = 0;
30
31         anim_frames[0] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_0.png");
32         anim_frames[1] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_1.png");
33         anim_frames[2] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_2.png");
34         anim_frames[3] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_3.png");
35         anim_frames[4] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_4.png");
36         anim_frames[5] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_5.png");
37         anim_frames[6] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_6.png");
38         anim_frames[7] = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/anim_7.png");
39         current_frame = 0;
40
41         plaqui_logo = anim = 0;
42         rg->get_widget("plaqui_logo", plaqui_logo);
43         plaqui_logo->set(PACKAGE_DATA_DIR"/plaqui-client/pixmaps/logo.png");
44
45         rg->get_widget("anim_frames", anim);
46         anim->set(anim_frames[current_frame]);
47
48         rg->get_widget("close_about", close_about);
49         rg->get_widget("dlgAbout", dlg_about);
50         rg->get_widget("btn_pausa", btn_pause);
51         rg->get_widget("btn_simular", btn_simulate);
52         rg->get_widget("btn_activar", btn_activar);
53         rg->get_widget("color_preview", color_preview);
54         rg->get_widget("lbl_nombre", lbl_nombre);
55         rg->get_widget("lbl_extra", lbl_extra);
56         rg->get_widget("lbl_cap_extra", lbl_cap_extra);
57         rg->get_widget("lbl_cap_flujo", lbl_cap_flujo);
58         rg->get_widget("lbl_flujo", lbl_flujo);
59         rg->get_widget("mnu_file_connect", conect);
60         rg->get_widget("mnu_file_disconnect", mnu_disconnect);
61         rg->get_widget("mnu_file_exit", exit);
62         rg->get_widget("mnu_help_about", about);
63         rg->get_widget("mnu_server_stop", server_stop);
64         rg->get_widget("mnu_prop", mnu_prop);
65         rg->get_widget_derived("dlg_property", dlg_property);
66         rg->get_widget_derived("dlgConectar", dlg_conectar);
67         rg->get_widget("txt_view", txt_view);
68         rg->get_widget("bar_connect", bar_connect);
69         rg->get_widget("work_place", work_place);
70         rg->get_widget("ico_conected", ico_conected);
71
72         work_place->signal_expose_event().connect( SigC::slot(*this, &Principal::on_workplace_expose_event) );
73         dlg_property->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_property_ok) );
74         dlg_conectar->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_connect_ok) );
75         mnu_prop->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_property));
76         mnu_disconnect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_disconnect));
77         server_stop->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_server_stop_activate));
78         conect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
79         bar_connect->signal_clicked().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
80         exit->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_exit));
81         about->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_help_about));
82         btn_activar->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_activar_clicked) );
83         btn_simulate->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_simulate_clicked) );
84         btn_pause->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_pause_clicked) );
85         close_about->signal_clicked().connect( SigC::slot(*dlg_about, &Gtk::Widget::hide) );
86         
87         conexion = NULL;
88         is_xml_loaded = false;
89         last_selected = NULL;
90         paused = true;
91         update_ui.connect( SigC::slot(*this, &Principal::update_items_prop ) );
92         load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
93         gc = Gdk::GC::create(get_window());
94         color_low = Gdk::Color("blue");
95         color_high = Gdk::Color("red");
96         Gtk::Widget::get_default_colormap()->alloc_color(color_low);
97         Gtk::Widget::get_default_colormap()->alloc_color(color_high);
98 }
99
100 Principal::~Principal()
101 {
102 }
103
104 void Principal::on_realize()
105 {
106         Gtk::Window::on_realize();
107 }
108
109 bool Principal::on_workplace_expose_event(GdkEventExpose *e)
110 {
111         Glib::RefPtr<Gdk::Window> window = work_place->get_window();
112
113         int x1, y1, x2, y2;
114         // Dibujo las lineas
115         std::map<const std::string, ViewItem *>::iterator i;
116         for(i=mapItems.begin(); i!=mapItems.end(); i++) {
117                 if (dynamic_cast<ViewAnd *>(i->second) || dynamic_cast<ViewOr *>(i->second) || dynamic_cast<ViewNot *>(i->second)) {
118                         x1 = i->second->x + i->second->in_x;
119                         y1 = i->second->y + i->second->in_y;
120                         std::list<t_Linea>::iterator linea;
121                         for(linea=i->second->in_lines.begin(); linea!=i->second->in_lines.end(); linea++) {
122                                 ViewItem *tmp = find_item(linea->dst);
123                                 /* Si este item es un tanque */
124                                 if (linea->is_tank) {
125                                         if (linea->is_tank_lower) {
126                                                 x2 = tmp->x + tmp->out_x;
127                                                 y2 = tmp->y + tmp->out_y;
128                                                 if (dynamic_cast<ViewTank *>(tmp)->get_out_inf()) {
129                                                         gc->set_foreground(color_high);
130                                                 } else {
131                                                         gc->set_foreground(color_low);
132                                                 }
133                                         } else {
134                                                 x2 = tmp->x + tmp->in_x;
135                                                 y2 = tmp->y + tmp->in_y;
136                                                 if (dynamic_cast<ViewTank *>(tmp)->get_out_sup()) {
137                                                         gc->set_foreground(color_high);
138                                                 } else {
139                                                         gc->set_foreground(color_low);
140                                                 }
141                                         }
142                                 } else {
143                                         x2 = tmp->x + tmp->out_x;
144                                         y2 = tmp->y + tmp->out_y;
145                                         if (tmp->get_open()) {
146                                                 gc->set_foreground(color_high);
147                                         } else {
148                                                 gc->set_foreground(color_low);
149                                         }
150                                 }
151                                 gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
152                                 window->draw_line(gc, x2+tmp->item_offset_x, y2+tmp->item_offset_y, x2+tmp->item_offset_x, y1+i->second->offset_y);
153                                 window->draw_line(gc, x2+tmp->item_offset_x, y1+i->second->offset_y, x1+i->second->offset_x, y1+i->second->offset_y);
154                                 window->draw_line(gc, x1, y1, x1+i->second->offset_x, y1+i->second->offset_y);
155                                 window->draw_line(gc, x2, y2, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
156                         }
157                         x1 = i->second->x + i->second->out_x;
158                         y1 = i->second->y + i->second->out_y;
159                         for(linea=i->second->out_lines.begin(); linea!=i->second->out_lines.end(); linea++) {
160                                 ViewItem *tmp = find_item(linea->dst);
161                                 x2 = tmp->x + tmp->in_x;
162                                 y2 = tmp->y + tmp->in_y;
163                                 if (i->second->get_open()) {
164                                         gc->set_foreground(color_high);
165                                 } else {
166                                         gc->set_foreground(color_low);
167                                 }
168                                 gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
169                                 window->draw_line(gc, x1+i->second->offset_x, y1+i->second->offset_y, x1+i->second->offset_x, y2+tmp->item_offset_y);
170                                 window->draw_line(gc, x1+i->second->offset_x, y2+tmp->item_offset_y, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
171                                 window->draw_line(gc, x1, y1, x1+i->second->offset_x, y1+i->second->offset_y);
172                                 window->draw_line(gc, x2, y2, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
173                         }
174                 }
175         }
176 }
177
178 ViewItem *Principal::find_item(std::string &_name)
179 {
180         std::map<const std::string, ViewItem *>::iterator i;
181         for(i=mapItems.begin(); i!=mapItems.end(); i++) {
182                 if (i->second->get_name() == _name) {
183                         return i->second;
184                 }
185         }
186         return NULL;
187 }
188
189 void Principal::on_dlg_connect_ok()
190 {
191         if (conexion == NULL) {
192                 // Creo la conexion
193                 try {
194                         conexion = new PlaQui::Server::ControlClient(dlg_conectar->get_server_name(), dlg_conectar->get_server_port());
195                 }
196                 catch (...) {
197                         txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
198                         //delete conexion; XXX Si no me equivoco, si falla el
199                         //constructor, no se reserva la memoria (el delete no va).
200                         conexion = NULL;
201                         return;
202                 }
203
204                 // Conecto las señales
205                 conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
206                 conexion->signal_error().connect( SigC::slot(*this, &Principal::on_conexion_fatal_error) );
207                 conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
208                 conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
209                 conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
210                 // Lanzo la conexion!
211                 conexion->run();
212                 txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
213                 ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
214
215                 // Pido la planta por defecto
216                 PlaQui::Server::Command c("plant", "get");
217                 c.add_arg("default");
218                 conexion->send(c);
219         }
220         dlg_conectar->hide();
221 }
222
223 void Principal::on_mnu_file_exit()
224 {
225         on_mnu_file_disconnect();
226         if (conexion) {
227                 conexion->finish();
228         }
229         // Espera a que termine realmente.
230         while (conexion) {
231                 Glib::usleep(100000); // 0,1 segundo
232         }
233         Gtk::Main::quit();
234 }
235
236 bool Principal::on_delete_event(GdkEventAny *e)
237 {
238         on_mnu_file_exit();
239         return false;
240 }
241
242 void Principal::on_mnu_server_stop_activate()
243 {
244         if (conexion == NULL) return;
245
246         PlaQui::Server::Command c("server", "stop");
247         conexion->send(c);
248 }
249
250 void Principal::on_btn_activar_clicked()
251 {
252         if ((conexion == NULL) || (last_selected == NULL)) return;
253
254         PlaQui::Server::Command c("plant", "set");
255         c.add_arg("default");
256         c.add_arg(last_selected->get_name());
257         c.add_arg("open");
258         
259         if (last_selected->get_open())
260                 c.add_arg("false");
261         else
262                 c.add_arg("true");
263
264         conexion->send(c);
265 }
266
267 void Principal::on_mnu_file_disconnect()
268 {
269         if (conexion == NULL) return;
270
271         PlaQui::Server::Command c("transmission", "stop");
272         c.add_arg(conexion->get_host());
273         c.add_arg("7528");
274         conexion->send(c);
275         PlaQui::Server::Command c2("connection", "stop");
276         c2.add_arg(conexion->get_host());
277         c2.add_arg(conexion->get_port());
278         conexion->send(c2);
279 }
280
281 void Principal::on_mnu_file_connect()
282 {
283         dlg_conectar->show();
284 }
285
286 void Principal::on_mnu_help_about()
287 {
288         dlg_about->run();
289 }
290
291 bool Principal::on_item_clicked(GdkEventButton *e, ViewItem *i)
292 {
293         
294         txt_view->get_buffer()->insert_at_cursor("Selecciono ");
295         txt_view->get_buffer()->insert_at_cursor(i->get_name());
296         txt_view->get_buffer()->insert_at_cursor("\n");
297
298         last_selected = i;
299         update_items_prop();
300 }
301
302 void Principal::update_items_prop()
303 {
304         if (last_selected != NULL) {
305                 lbl_nombre->set_text(last_selected->get_name());
306                 lbl_flujo->set_text(last_selected->get_actual_flow());
307                 lbl_extra->set_text(last_selected->get_extra());
308
309                 lbl_cap_flujo->set_text(last_selected->get_cap_flow());
310                 lbl_cap_extra->set_text(last_selected->get_cap_extra());
311
312                 color_preview->modify_bg(Gtk::STATE_NORMAL, last_selected->get_color());
313                 color_preview->queue_draw();
314         }
315
316         if (!paused) {
317                 current_frame++;
318                 if (current_frame > 7) {
319                         current_frame = 0;
320                 }
321                 anim->set(anim_frames[current_frame]);
322         }
323
324         // Mando a redibujar a todos los items
325         std::map<const std::string, ViewItem *>::iterator i;
326         for(i=mapItems.begin(); i!=mapItems.end(); i++) {
327                 i->second->queue_draw();
328         }
329         work_place->queue_draw();
330 }
331
332 void Principal::on_conexion_frame(const std::string &frame)
333 {
334         if (conexion != NULL) {
335                 read_status_xml(frame);
336         }
337 }
338
339 void Principal::on_conexion_finished()
340 {
341         txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
342         ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
343         conexion = NULL;
344         // Elimino la planta    
345         std::map<const std::string, ViewItem *>::iterator i;
346         for(i=mapItems.begin(); i!=mapItems.end(); i++) {
347                 delete i->second;
348         }
349         mapItems.clear();
350         is_xml_loaded = false;
351 }
352
353 void Principal::on_conexion_ok(const std::string &body)
354 {
355         /* lo paso a la carga del XML */
356         /* verifico que body este completo */
357         if ((body.find("</planta>")>0) && (body.find("<planta>")>0)) {
358                 //loadXML(body);
359                 xml_body = body;
360                 load_xml_dispatch();
361         } else {
362                 std::cout << body << std::endl;
363                 txt_view->get_buffer()->insert_at_cursor("<IN>\n");
364                 txt_view->get_buffer()->insert_at_cursor(Glib::locale_to_utf8(body));
365                 txt_view->get_buffer()->insert_at_cursor("</IN>\n");
366         }
367 }
368
369 void Principal::on_conexion_fatal_error(const PlaQui::Server::ControlClient::Error& code, const std::string& desc)
370 {
371         std::stringstream a;
372         std::string s;
373         a << code;
374         a >> s;
375         txt_view->get_buffer()->insert_at_cursor("Error de red nro. ");
376         txt_view->get_buffer()->insert_at_cursor(s);
377         txt_view->get_buffer()->insert_at_cursor(": ");
378         txt_view->get_buffer()->insert_at_cursor(desc);
379         txt_view->get_buffer()->insert_at_cursor("\n");
380 }
381
382 void Principal::on_conexion_error(unsigned code, const std::string& desc)
383 {
384         std::stringstream a;
385         std::string s;
386         a << code;
387         a >> s;
388         txt_view->get_buffer()->insert_at_cursor("El server dice que hay error nro. ");
389         txt_view->get_buffer()->insert_at_cursor(s);
390         txt_view->get_buffer()->insert_at_cursor(": ");
391         txt_view->get_buffer()->insert_at_cursor(desc);
392         txt_view->get_buffer()->insert_at_cursor("\n");
393 }
394
395 void Principal::loadXML()
396 {
397         // ya lo cargue
398         if (is_xml_loaded) return;
399
400         /* Parseo de ejemplo de un XML desde archivo */
401         xmlDocPtr document;
402         document = xmlParseMemory(xml_body.c_str(),xml_body.size());
403         if (document == NULL) {
404                 std::cout << "EEERRRRRRROOOOOOOOOO" << std::endl;
405                 return;
406         }
407         is_xml_loaded = true;
408         /* bien, el archivo se parseo bien! */
409         xmlNodePtr nodo, items;
410         nodo = document->children;
411
412         if (strcmp((char *)nodo->name, "planta") == 0) {
413                 items = nodo->children;
414                 while (items != NULL) {
415                         if (items->type == XML_ELEMENT_NODE) {
416                                 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
417                                         loadBomba(items);
418                                 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
419                                         loadCodo(items);
420                                 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
421                                         loadConduct(items);
422                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
423                                         loadExclusa(items);
424                                 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
425                                         loadTank(items);
426                                 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
427                                         loadUnion(items);
428                                 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
429                                         loadDrain(items);
430                                 } else if (xmlStrcmp(items->name, BAD_CAST"and")==0) {
431                                         loadAnd(items);
432                                 } else if (xmlStrcmp(items->name, BAD_CAST"or")==0) {
433                                         loadOr(items);
434                                 } else if (xmlStrcmp(items->name, BAD_CAST"not")==0) {
435                                         loadNot(items);
436                                 }
437
438                         }
439                         items = items->next;
440                 }
441         }
442
443         xmlFreeDoc(document);
444
445         // Ya cargado el XML, mando un msg para empezar a recibir los frames!
446         PlaQui::Server::Command c("transmission", "start");
447         c.add_arg("default");
448         c.add_arg(conexion->get_host());
449         c.add_arg("7528");
450         conexion->send(c);
451 }
452
453 void Principal::loadNot(xmlNodePtr nodo)
454 {
455         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
456         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
457         int orientacion=0, x, y;
458         t_Linea linea;
459
460         ViewNot *p;
461         nodo = nodo->children;
462         while (nodo != NULL) {
463                 if (nodo->type == XML_ELEMENT_NODE) {
464                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
465                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
466                                 p = new ViewNot(name, orientacion);
467                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
468                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
469                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
470                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
471                         } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
472                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
473                                 p->out_lines.push_back(linea);
474                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
475                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
476                                 linea.is_tank = false;
477                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
478                                         linea.is_tank = true;
479                                         linea.is_tank_lower = false;
480                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
481                                                 linea.is_tank_lower = true;
482                                 }
483                                 p->in_lines.push_back(linea);
484                         }
485                 }
486                 nodo = nodo->next;
487         }
488         
489         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
490         p->set_position(x,y);
491         work_place->put(*p, x, y);
492         p->show();
493         // los agrego al hash
494         mapItems[name] = p;
495 }
496
497 void Principal::loadOr(xmlNodePtr nodo)
498 {
499         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
500         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
501         int orientacion=0, x, y;
502         t_Linea linea;
503
504         ViewOr *p;
505
506         nodo = nodo->children;
507         while (nodo != NULL) {
508                 if (nodo->type == XML_ELEMENT_NODE) {
509                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
510                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
511                                 p = new ViewOr(name, orientacion);
512                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
513                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
514                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
515                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
516                         } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
517                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
518                                 p->out_lines.push_back(linea);
519                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
520                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
521                                 linea.is_tank = false;
522                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
523                                         linea.is_tank = true;
524                                         linea.is_tank_lower = false;
525                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
526                                                 linea.is_tank_lower = true;
527                                 }
528                                 p->in_lines.push_back(linea);
529                         }
530                 }
531                 nodo = nodo->next;
532         }
533
534         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
535         p->set_position(x,y);
536         work_place->put(*p, x, y);
537         p->show();
538         // los agrego al hash
539         mapItems[name] = p;
540 }
541
542 void Principal::loadAnd(xmlNodePtr nodo)
543 {
544         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
545         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
546         int orientacion=0, x, y;
547         float flujo;
548         xmlNodePtr inicial = nodo;
549         t_Linea linea;
550         ViewAnd *p;
551         
552         nodo = nodo->children;
553         while (nodo != NULL) {
554                 if (nodo->type == XML_ELEMENT_NODE) {
555                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
556                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
557                                 p = new ViewAnd(name, orientacion);
558                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
559                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
560                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
561                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
562                         } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
563                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
564                                 p->out_lines.push_back(linea);
565                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
566                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
567                                 linea.is_tank = false;
568                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
569                                         linea.is_tank = true;
570                                         linea.is_tank_lower = false;
571                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
572                                                 linea.is_tank_lower = true;
573                                 }
574                                 p->in_lines.push_back(linea);
575                         }
576                 }
577                 nodo = nodo->next;
578         }
579
580         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
581         p->set_position(x,y);
582         work_place->put(*p, x, y);
583         p->show();
584         // los agrego al hash
585         mapItems[name] = p;
586 }
587
588 void Principal::loadBomba(xmlNodePtr nodo)
589 {
590         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
591         int orientacion=0, x, y;
592
593         nodo = nodo->children;
594         while (nodo != NULL) {
595                 if (nodo->type == XML_ELEMENT_NODE) {
596                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
597                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
598                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
599                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
600                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
601                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
602                         }
603                 }
604                 nodo = nodo->next;
605         }
606
607         // listo, ya recolecte todos los datos, ahora creo el objeto!
608         ViewPump *b = new ViewPump(name, orientacion);
609         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
610         b->set_position(x,y);
611         work_place->put(*b, x, y);
612         b->show();
613         // los agrego al hash
614         mapItems[name] = b;
615 }
616
617 void Principal::loadCodo(xmlNodePtr nodo)
618 {
619         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
620         int orientacion=0, x, y;
621
622         nodo = nodo->children;
623         while (nodo != NULL) {
624                 if (nodo->type == XML_ELEMENT_NODE) {
625                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
626                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
627                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
628                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
629                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
630                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
631                         }
632                 }
633                 nodo = nodo->next;
634         }
635
636         // listo, ya recolecte todos los datos, ahora creo el objeto!
637         ViewItem *b = new ViewCodo(name, orientacion);
638         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
639         b->set_position(x,y);
640         work_place->put(*b, x, y);
641         b->show();
642         // los agrego al hash
643         mapItems[name] = b;
644 }
645
646 void Principal::loadConduct(xmlNodePtr nodo)
647 {
648         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
649         int orientacion=0, x, y;
650
651         nodo = nodo->children;
652         while (nodo != NULL) {
653                 if (nodo->type == XML_ELEMENT_NODE) {
654                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
655                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
656                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
657                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
658                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
659                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
660                         }
661                 }
662                 nodo = nodo->next;
663         }
664
665         // listo, ya recolecte todos los datos, ahora creo el objeto!
666         ViewConduct *b = new ViewConduct(name, orientacion);
667         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
668         b->set_position(x,y);
669         work_place->put(*b, x, y);
670         b->show();
671         // los agrego al hash
672         mapItems[name] = b;
673 }
674
675 void Principal::loadExclusa(xmlNodePtr nodo)
676 {
677         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
678         int orientacion=0, x, y;
679
680         nodo = nodo->children;
681         while (nodo != NULL) {
682                 if (nodo->type == XML_ELEMENT_NODE) {
683                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
684                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
685                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
686                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
687                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
688                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
689                         }
690                 }
691                 nodo = nodo->next;
692         }
693
694         // listo, ya recolecte todos los datos, ahora creo el objeto!
695         ViewExclusa *b = new ViewExclusa(name, orientacion);
696         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
697         b->set_position(x,y);
698         work_place->put(*b, x, y);
699         b->show();
700         // los agrego al hash
701         mapItems[name] = b;
702 }
703
704 void Principal::loadTank(xmlNodePtr nodo)
705 {
706         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
707         int orientacion=0, x, y;
708
709         nodo = nodo->children;
710         while (nodo != NULL) {
711                 if (nodo->type == XML_ELEMENT_NODE) {
712                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
713                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
714                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
715                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
716                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
717                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
718                         }
719                 }
720                 nodo = nodo->next;
721         }
722
723         // listo, ya recolecte todos los datos, ahora creo el objeto!
724         ViewTank *b = new ViewTank(name, orientacion);
725         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
726         b->set_position(x,y);
727         work_place->put(*b, x, y);
728         b->show();
729         // los agrego al hash
730         mapItems[name] = b;
731 }
732
733 void Principal::loadUnion(xmlNodePtr nodo)
734 {
735         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
736         int orientacion=0, x, y;
737
738         nodo = nodo->children;
739         while (nodo != NULL) {
740                 if (nodo->type == XML_ELEMENT_NODE) {
741                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
742                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
743                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
744                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
745                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
746                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
747                         }
748                 }
749                 nodo = nodo->next;
750         }
751
752         // listo, ya recolecte todos los datos, ahora creo el objeto!
753         ViewUnion *b = new ViewUnion(name, orientacion);
754         b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
755         b->set_position(x,y);
756         work_place->put(*b, x, y);
757         b->show();
758         // los agrego al hash
759         mapItems[name] = b;
760 }
761
762 void Principal::loadDrain(xmlNodePtr nodo)
763 {
764         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
765         int orientacion=0, x, y;
766
767         nodo = nodo->children;
768         while (nodo != NULL) {
769                 if (nodo->type == XML_ELEMENT_NODE) {
770                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
771                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
772                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
773                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
774                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
775                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
776                         }
777                 }
778                 nodo = nodo->next;
779         }
780
781         // listo, ya recolecte todos los datos, ahora creo el objeto!
782         ViewDrain *b = new ViewDrain(name, orientacion);
783         b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
784         b->set_position(x,y);
785         work_place->put(*b, x, y);
786         b->show();
787         // los agrego al hash
788         mapItems[name] = b;
789 }
790
791 void Principal::read_status_xml(const std::string &frame)
792 {
793         std::string item_name;
794         xmlDocPtr document;
795         document = xmlParseMemory(frame.c_str(),frame.size());
796         if (document == NULL) {
797                 std::cout << "read_status_xml::no se creo documento" << std::endl;
798                 return;
799         }
800         
801         xmlNodePtr nodo, items, props;
802         nodo = document->children;
803         float tmp;
804         bool tmp_b;
805
806         if (strcmp((char *)nodo->name, "plantstatus") == 0) {
807                 items = nodo->children;
808                 while (items != NULL) {
809                         if (items->type == XML_ELEMENT_NODE) {
810                                 tmp = -1;
811                                 item_name = "";
812                                 if (xmlStrcmp(items->name, BAD_CAST"float")==0) {
813                                         tmp = get_float_from_xml(items->children);
814                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
815                                         mapItems[item_name]->set_actual_flow(tmp);
816                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
817                                         tmp_b = get_bool_from_xml(items->children);
818                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
819                                         mapItems[item_name]->set_open(tmp_b);
820                                 } else if (xmlStrcmp(items->name, BAD_CAST"pump")==0) {
821                                         tmp_b = get_bool_from_xml(items->children);
822                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
823                                         mapItems[item_name]->set_open(tmp_b);
824                                 } else if (xmlStrcmp(items->name, BAD_CAST"logic")==0) {
825                                         tmp_b = get_bool_from_xml(items->children);
826                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
827                                         mapItems[item_name]->set_open(tmp_b);
828                                 } else if (xmlStrcmp(items->name, BAD_CAST"color")==0) {
829                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
830                                         mapItems[item_name]->set_color( get_rgb_from_xml(items->children) );
831                                 } else if (xmlStrcmp(items->name, BAD_CAST"tank")==0) {
832                                         xmlNodePtr nodo_tmp = items->children;
833                                         float cap, lit;
834                                         cap = lit = -1;
835                                         bool b_inf, b_sup;
836                                         while (nodo_tmp != NULL) {
837                                                 if (nodo_tmp->type == XML_ELEMENT_NODE) {
838                                                         if (xmlStrcmp(nodo_tmp->name, BAD_CAST"capacity")==0)
839                                                                 cap = atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
840                                                         else if (xmlStrcmp(nodo_tmp->name, BAD_CAST"litros")==0)
841                                                                 lit= atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
842                                                         else if (xmlStrcmp(nodo_tmp->name, BAD_CAST"salida")==0) {
843                                                                 if (xmlStrcmp(xmlGetProp(items, BAD_CAST"id"), BAD_CAST"inferior")==0) {
844                                                                         b_inf = get_bool_from_xml(nodo_tmp->children);
845                                                                 } else {
846                                                                         b_sup = get_bool_from_xml(nodo_tmp->children);
847                                                                 }
848                                                         }
849                                                 }
850                                                 nodo_tmp = nodo_tmp->next;
851                                         }
852                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
853                                         mapItems[item_name]->set_actual_flow(cap);
854                                         mapItems[item_name]->set_extra(lit);
855                                         dynamic_cast<ViewTank *>(mapItems[item_name])->set_out_sup(b_sup);
856                                         dynamic_cast<ViewTank *>(mapItems[item_name])->set_out_inf(b_inf);
857                                 }
858                         }
859                         items = items->next;
860                 }
861
862                 xmlFreeDoc(document);
863                 // Actualizo la UI
864                 update_ui();
865         }
866 }
867
868 Gdk::Color Principal::get_rgb_from_xml(xmlNodePtr nodo)
869 {
870         gushort r,g,b;
871         while (nodo != NULL) {
872                 if (nodo->type == XML_ELEMENT_NODE) {
873                         if (xmlStrcmp(nodo->name, BAD_CAST"r")==0)
874                                 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
875                         if (xmlStrcmp(nodo->name, BAD_CAST"g")==0)
876                                 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
877                         if (xmlStrcmp(nodo->name, BAD_CAST"b")==0)
878                                 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
879                 }
880                 nodo = nodo->next;
881         }
882         r = static_cast<gushort>(65535 * (r / 255.0f));
883         g = static_cast<gushort>(65535 * (g / 255.0f));
884         b = static_cast<gushort>(65535 * (b / 255.0f));
885         Gdk::Color c;
886         c.set_rgb(r,g,b);
887
888         return c;
889 }
890
891 float Principal::get_float_from_xml(xmlNodePtr nodo)
892 {
893         float tmp = -1;
894         while (nodo != NULL) {
895                 if (nodo->type == XML_ELEMENT_NODE) {
896                         if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
897                                 tmp = atof( (char *)XML_GET_CONTENT(nodo->children) );
898                                 break;
899                         }
900                 }
901                 nodo = nodo->next;
902         }
903         if (tmp == INFINITO) tmp = 0;
904         return tmp;
905 }
906
907 bool Principal::get_bool_from_xml(xmlNodePtr nodo)
908 {
909         std::string tmp;
910         while (nodo != NULL) {
911                 if (nodo->type == XML_ELEMENT_NODE) {
912                         if (xmlStrcmp(nodo->name, BAD_CAST"active")==0) {
913                                 tmp = (char *)XML_GET_CONTENT(nodo->children);
914                                 break;
915                         }
916                 }
917                 nodo = nodo->next;
918         }
919         return tmp == "true";
920 }
921
922 void Principal::on_mnu_property()
923 {
924         dlg_property->show();
925 }
926
927 void Principal::on_dlg_property_ok()
928 {
929         if (conexion) {
930                 PlaQui::Server::Command c("plant", "set_frequency");
931                 c.add_arg("default");
932                 c.add_arg( dlg_property->get_velocity() );
933
934                 conexion->send(c);
935         }
936         dlg_property->hide();
937 }
938
939 void Principal::on_btn_simulate_clicked()
940 {
941         if (conexion == NULL) return;
942
943         PlaQui::Server::Command c("plant", "start");
944         c.add_arg("default");
945         conexion->send(c);
946         paused = false;
947 }
948
949 void Principal::on_btn_pause_clicked()
950 {
951         if (conexion == NULL) return;
952
953         PlaQui::Server::Command c("plant", "stop");
954         c.add_arg("default");
955         conexion->send(c);
956         paused = true;
957 }
958