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