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