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