]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Client/src/principal.cpp
Se corrigen detalles en la documentacion.
[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                                 linea.is_tank = false;
498                                 p->out_lines.push_back(linea);
499                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
500                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
501                                 linea.is_tank = false;
502                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
503                                         linea.is_tank = true;
504                                         linea.is_tank_lower = false;
505                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
506                                                 linea.is_tank_lower = true;
507                                 }
508                                 p->in_lines.push_back(linea);
509                         }
510                 }
511                 nodo = nodo->next;
512         }
513         
514         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
515         p->set_position(x,y);
516         work_place->put(*p, x, y);
517         p->show();
518         // los agrego al hash
519         mapItems[name] = p;
520 }
521
522 void Principal::loadOr(xmlNodePtr nodo)
523 {
524         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
525         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
526         int orientacion=0, x, y;
527         t_Linea linea;
528
529         ViewOr *p;
530
531         nodo = nodo->children;
532         while (nodo != NULL) {
533                 if (nodo->type == XML_ELEMENT_NODE) {
534                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
535                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
536                                 p = new ViewOr(name, orientacion);
537                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
538                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
539                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
540                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
541                         } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
542                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
543                                 linea.is_tank = false;
544                                 p->out_lines.push_back(linea);
545                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
546                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
547                                 linea.is_tank = false;
548                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
549                                         linea.is_tank = true;
550                                         linea.is_tank_lower = false;
551                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
552                                                 linea.is_tank_lower = true;
553                                 }
554                                 p->in_lines.push_back(linea);
555                         }
556                 }
557                 nodo = nodo->next;
558         }
559
560         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
561         p->set_position(x,y);
562         work_place->put(*p, x, y);
563         p->show();
564         // los agrego al hash
565         mapItems[name] = p;
566 }
567
568 void Principal::loadAnd(xmlNodePtr nodo)
569 {
570         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
571         std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
572         int orientacion=0, x, y;
573         float flujo;
574         xmlNodePtr inicial = nodo;
575         t_Linea linea;
576         ViewAnd *p;
577         
578         nodo = nodo->children;
579         while (nodo != NULL) {
580                 if (nodo->type == XML_ELEMENT_NODE) {
581                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
582                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
583                                 p = new ViewAnd(name, orientacion);
584                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
585                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
586                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
587                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
588                         } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
589                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
590                                 p->out_lines.push_back(linea);
591                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
592                                 linea.dst = (char *)XML_GET_CONTENT(nodo->children);
593                                 linea.is_tank = false;
594                                 if (dynamic_cast<ViewTank *>(find_item(linea.dst)) != NULL) {
595                                         linea.is_tank = true;
596                                         linea.is_tank_lower = false;
597                                         if (xmlStrcmp(xmlGetProp(nodo, BAD_CAST"id"), BAD_CAST"inferior")==0)
598                                                 linea.is_tank_lower = true;
599                                 }
600                                 p->in_lines.push_back(linea);
601                         }
602                 }
603                 nodo = nodo->next;
604         }
605
606         p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
607         p->set_position(x,y);
608         work_place->put(*p, x, y);
609         p->show();
610         // los agrego al hash
611         mapItems[name] = p;
612 }
613
614 void Principal::loadBomba(xmlNodePtr nodo)
615 {
616         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
617         int orientacion=0, x, y;
618
619         nodo = nodo->children;
620         while (nodo != NULL) {
621                 if (nodo->type == XML_ELEMENT_NODE) {
622                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
623                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
624                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
625                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
626                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
627                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
628                         }
629                 }
630                 nodo = nodo->next;
631         }
632
633         // listo, ya recolecte todos los datos, ahora creo el objeto!
634         ViewPump *b = new ViewPump(name, orientacion);
635         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
636         b->set_position(x,y);
637         work_place->put(*b, x, y);
638         b->show();
639         // los agrego al hash
640         mapItems[name] = b;
641 }
642
643 void Principal::loadCodo(xmlNodePtr nodo)
644 {
645         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
646         int orientacion=0, x, y;
647
648         nodo = nodo->children;
649         while (nodo != NULL) {
650                 if (nodo->type == XML_ELEMENT_NODE) {
651                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
652                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
653                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
654                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
655                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
656                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
657                         }
658                 }
659                 nodo = nodo->next;
660         }
661
662         // listo, ya recolecte todos los datos, ahora creo el objeto!
663         ViewItem *b = new ViewCodo(name, orientacion);
664         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
665         b->set_position(x,y);
666         work_place->put(*b, x, y);
667         b->show();
668         // los agrego al hash
669         mapItems[name] = b;
670 }
671
672 void Principal::loadConduct(xmlNodePtr nodo)
673 {
674         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
675         int orientacion=0, x, y;
676
677         nodo = nodo->children;
678         while (nodo != NULL) {
679                 if (nodo->type == XML_ELEMENT_NODE) {
680                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
681                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
682                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
683                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
684                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
685                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
686                         }
687                 }
688                 nodo = nodo->next;
689         }
690
691         // listo, ya recolecte todos los datos, ahora creo el objeto!
692         ViewConduct *b = new ViewConduct(name, orientacion);
693         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
694         b->set_position(x,y);
695         work_place->put(*b, x, y);
696         b->show();
697         // los agrego al hash
698         mapItems[name] = b;
699 }
700
701 void Principal::loadExclusa(xmlNodePtr nodo)
702 {
703         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
704         int orientacion=0, x, y;
705
706         nodo = nodo->children;
707         while (nodo != NULL) {
708                 if (nodo->type == XML_ELEMENT_NODE) {
709                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
710                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
711                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
712                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
713                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
714                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
715                         }
716                 }
717                 nodo = nodo->next;
718         }
719
720         // listo, ya recolecte todos los datos, ahora creo el objeto!
721         ViewExclusa *b = new ViewExclusa(name, orientacion);
722         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
723         b->set_position(x,y);
724         work_place->put(*b, x, y);
725         b->show();
726         // los agrego al hash
727         mapItems[name] = b;
728 }
729
730 void Principal::loadTank(xmlNodePtr nodo)
731 {
732         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
733         int orientacion=0, x, y;
734
735         nodo = nodo->children;
736         while (nodo != NULL) {
737                 if (nodo->type == XML_ELEMENT_NODE) {
738                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
739                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
740                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
741                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
742                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
743                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
744                         }
745                 }
746                 nodo = nodo->next;
747         }
748
749         // listo, ya recolecte todos los datos, ahora creo el objeto!
750         ViewTank *b = new ViewTank(name, orientacion);
751         b->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
752         b->set_position(x,y);
753         work_place->put(*b, x, y);
754         b->show();
755         // los agrego al hash
756         mapItems[name] = b;
757 }
758
759 void Principal::loadUnion(xmlNodePtr nodo)
760 {
761         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
762         int orientacion=0, x, y;
763
764         nodo = nodo->children;
765         while (nodo != NULL) {
766                 if (nodo->type == XML_ELEMENT_NODE) {
767                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
768                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
769                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
770                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
771                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
772                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
773                         }
774                 }
775                 nodo = nodo->next;
776         }
777
778         // listo, ya recolecte todos los datos, ahora creo el objeto!
779         ViewUnion *b = new ViewUnion(name, orientacion);
780         b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
781         b->set_position(x,y);
782         work_place->put(*b, x, y);
783         b->show();
784         // los agrego al hash
785         mapItems[name] = b;
786 }
787
788 void Principal::loadDrain(xmlNodePtr nodo)
789 {
790         Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
791         int orientacion=0, x, y;
792
793         nodo = nodo->children;
794         while (nodo != NULL) {
795                 if (nodo->type == XML_ELEMENT_NODE) {
796                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
797                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
798                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
799                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
800                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
801                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
802                         }
803                 }
804                 nodo = nodo->next;
805         }
806
807         // listo, ya recolecte todos los datos, ahora creo el objeto!
808         ViewDrain *b = new ViewDrain(name, orientacion);
809         b->signal_button_release_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), b) );
810         b->set_position(x,y);
811         work_place->put(*b, x, y);
812         b->show();
813         // los agrego al hash
814         mapItems[name] = b;
815 }
816
817 void Principal::read_status_xml(const std::string &_frame)
818 {
819         std::string item_name;
820         std::string frame = _frame;
821         xmlDocPtr document;
822
823         // Cambio las , por . para evitar problemas de convercion 
824         for(int i=0; i<frame.size(); i++) {
825                 if (frame[i] == ',') {
826                         frame[i] = '.';
827                 }
828         }
829         document = xmlParseMemory(frame.c_str(),frame.size());
830         if (document == NULL) {
831                 txt_view->get_buffer()->insert(txt_view->get_buffer()->begin(), "ERROR : No se pudo leer el último frame!\n");
832                 return;
833         }
834         
835         xmlNodePtr nodo, items, props;
836         nodo = document->children;
837         float tmp;
838         bool tmp_b;
839
840         if (strcmp((char *)nodo->name, "plantstatus") == 0) {
841                 items = nodo->children;
842                 while (items != NULL) {
843                         if (items->type == XML_ELEMENT_NODE) {
844                                 tmp = -1;
845                                 item_name = "";
846                                 if (xmlStrcmp(items->name, BAD_CAST"float")==0) {
847                                         tmp = get_float_from_xml(items->children);
848                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
849                                         mapItems[item_name]->set_actual_flow(tmp);
850                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
851                                         tmp_b = get_bool_from_xml(items->children);
852                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
853                                         mapItems[item_name]->set_open(tmp_b);
854                                 } else if (xmlStrcmp(items->name, BAD_CAST"pump")==0) {
855                                         tmp_b = get_bool_from_xml(items->children);
856                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
857                                         mapItems[item_name]->set_open(tmp_b);
858                                 } else if (xmlStrcmp(items->name, BAD_CAST"logic")==0) {
859                                         tmp_b = get_bool_from_xml(items->children);
860                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
861                                         mapItems[item_name]->set_open(tmp_b);
862                                 } else if (xmlStrcmp(items->name, BAD_CAST"color")==0) {
863                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
864                                         mapItems[item_name]->set_color( get_rgb_from_xml(items->children) );
865                                 } else if (xmlStrcmp(items->name, BAD_CAST"tank")==0) {
866                                         xmlNodePtr nodo_tmp = items->children;
867                                         float cap, lit;
868                                         cap = lit = -1;
869                                         bool b_inf, b_sup;
870                                         while (nodo_tmp != NULL) {
871                                                 if (nodo_tmp->type == XML_ELEMENT_NODE) {
872                                                         if (xmlStrcmp(nodo_tmp->name, BAD_CAST"capacity")==0)
873                                                                 cap = atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
874                                                         else if (xmlStrcmp(nodo_tmp->name, BAD_CAST"litros")==0)
875                                                                 lit= atof( (char *)XML_GET_CONTENT(nodo_tmp->children) );
876                                                         else if (xmlStrcmp(nodo_tmp->name, BAD_CAST"salida")==0) {
877                                                                 if (xmlStrcmp(xmlGetProp(nodo_tmp, BAD_CAST"id"), BAD_CAST"inferior")==0) {
878                                                                         b_inf = get_bool_from_xml(nodo_tmp->children);
879                                                                 } else {
880                                                                         b_sup = get_bool_from_xml(nodo_tmp->children);
881                                                                 }
882                                                         }
883                                                 }
884                                                 nodo_tmp = nodo_tmp->next;
885                                         }
886                                         item_name = (char *)xmlGetProp(items, BAD_CAST"name");
887                                         mapItems[item_name]->set_actual_flow(cap);
888                                         mapItems[item_name]->set_extra(lit);
889                                         dynamic_cast<ViewTank *>(mapItems[item_name])->set_out_sup(b_sup);
890                                         dynamic_cast<ViewTank *>(mapItems[item_name])->set_out_inf(b_inf);
891                                 }
892                         }
893                         items = items->next;
894                 }
895
896                 xmlFreeDoc(document);
897                 // Actualizo la UI
898                 update_ui();
899         }
900 }
901
902 Gdk::Color Principal::get_rgb_from_xml(xmlNodePtr nodo)
903 {
904         gushort r,g,b;
905         while (nodo != NULL) {
906                 if (nodo->type == XML_ELEMENT_NODE) {
907                         if (xmlStrcmp(nodo->name, BAD_CAST"r")==0)
908                                 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
909                         if (xmlStrcmp(nodo->name, BAD_CAST"g")==0)
910                                 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
911                         if (xmlStrcmp(nodo->name, BAD_CAST"b")==0)
912                                 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
913                 }
914                 nodo = nodo->next;
915         }
916         r = static_cast<gushort>(65535 * (r / 255.0f));
917         g = static_cast<gushort>(65535 * (g / 255.0f));
918         b = static_cast<gushort>(65535 * (b / 255.0f));
919         Gdk::Color c;
920         c.set_rgb(r,g,b);
921
922         return c;
923 }
924
925 float Principal::get_float_from_xml(xmlNodePtr nodo)
926 {
927         float tmp = -1;
928         std::string s;
929         while (nodo != NULL) {
930                 if (nodo->type == XML_ELEMENT_NODE) {
931                         if (xmlStrcmp(nodo->name, BAD_CAST"actual_flow")==0) {
932                                 s = (char *)XML_GET_CONTENT(nodo->children);
933                                 break;
934                         }
935                 }
936                 nodo = nodo->next;
937         }
938         std::stringstream ss;
939         ss << s;
940         ss >> tmp;
941         if (tmp == INFINITO) tmp = 0;
942
943         return tmp;
944 }
945
946 bool Principal::get_bool_from_xml(xmlNodePtr nodo)
947 {
948         std::string tmp;
949         while (nodo != NULL) {
950                 if (nodo->type == XML_ELEMENT_NODE) {
951                         if (xmlStrcmp(nodo->name, BAD_CAST"active")==0) {
952                                 tmp = (char *)XML_GET_CONTENT(nodo->children);
953                                 break;
954                         }
955                 }
956                 nodo = nodo->next;
957         }
958         return tmp == "true";
959 }
960
961 void Principal::on_mnu_property()
962 {
963         dlg_property->show();
964 }
965
966 void Principal::on_dlg_property_ok()
967 {
968         if (conexion) {
969                 PlaQui::Server::Command c("plant", "set_frequency");
970                 c.add_arg("default");
971                 c.add_arg( dlg_property->get_velocity() );
972
973                 conexion->send(c);
974         }
975         dlg_property->hide();
976 }
977
978 void Principal::on_btn_simulate_clicked()
979 {
980         if (conexion == NULL) return;
981
982         PlaQui::Server::Command c("plant", "start");
983         c.add_arg("default");
984         conexion->send(c);
985         paused = false;
986 }
987
988 void Principal::on_btn_pause_clicked()
989 {
990         if (conexion == NULL) return;
991
992         PlaQui::Server::Command c("plant", "stop");
993         c.add_arg("default");
994         conexion->send(c);
995         paused = true;
996 }
997