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