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