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