]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Client/src/principal.cpp
Se agrega la consante INFINITO pendiente desde el primer commit :-)
[z.facultad/75.42/plaqui.git] / Client / src / principal.cpp
index 0b59c0b8040470fa36ec54d6cd17b0ef14e9112d..7b441eec9ef970bbe0a7e79c557ca59de00b7134 100644 (file)
@@ -3,7 +3,6 @@
 #include <iostream>
 #include <sstream>
 #include <string>
-#include "plaqui/server/string.h"
 #include "item_codo.h"
 #include "item_conduct.h"
 #include "item_exclusa.h"
 #include "item_pump.h"
 #include "item_union.h"
 #include "item_drain.h"
+#include "item_not.h"
+#include "item_or.h"
+#include "item_and.h"
 #include <unistd.h>
+#include <glibmm/thread.h>
+
+#define INFINITO 99999999
 
 Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &rg):Gtk::Window(co),refXml(rg)
 {
@@ -21,7 +26,10 @@ Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &
        txt_target = txt_command = txt_args = 0;
        work_place = 0;
        lbl_cap_flujo = lbl_cap_extra = lbl_extra = lbl_nombre = lbl_color = lbl_flujo = 0;
+       btn_simulate = btn_pause = 0;
 
+       rg->get_widget("btn_pausa", btn_pause);
+       rg->get_widget("btn_simular", btn_simulate);
        rg->get_widget("btn_activar", btn_activar);
        rg->get_widget("color_preview", color_preview);
        rg->get_widget("lbl_nombre", lbl_nombre);
@@ -34,6 +42,7 @@ Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &
        rg->get_widget("mnu_file_exit", exit);
        rg->get_widget("mnu_help_about", about);
        rg->get_widget("mnu_prop", mnu_prop);
+       rg->get_widget_derived("dlg_property", dlg_property);
        rg->get_widget_derived("dlgConectar", dlg_conectar);
        rg->get_widget("btn_get", btn_get);
        rg->get_widget("txt_view", txt_view);
@@ -44,7 +53,10 @@ Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &
        rg->get_widget("work_place", work_place);
        rg->get_widget("ico_conected", ico_conected);
 
+       work_place->signal_expose_event().connect( SigC::slot(*this, &Principal::on_workplace_expose_event) );
+       dlg_property->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_property_ok) );
        dlg_conectar->get_ok_button()->signal_clicked().connect( SigC::slot(*this, &Principal::on_dlg_connect_ok) );
+       mnu_prop->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_property));
        mnu_disconnect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_disconnect));
        conect->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
        bar_connect->signal_clicked().connect( SigC::slot(*this, &Principal::on_mnu_file_connect));
@@ -52,18 +64,94 @@ Principal::Principal(BaseObjectType *co, const Glib::RefPtr<Gnome::Glade::Xml> &
        about->signal_activate().connect( SigC::slot(*this, &Principal::on_mnu_help_about));
        btn_get->signal_clicked().connect( SigC::slot(*this, &Principal::on_get_clicked) );
        btn_activar->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_activar_clicked) );
-
+       btn_simulate->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_simulate_clicked) );
+       btn_pause->signal_clicked().connect( SigC::slot(*this, &Principal::on_btn_pause_clicked) );
+       
        conexion = NULL;
        is_xml_loaded = false;
        last_selected = NULL;
        update_ui.connect( SigC::slot(*this, &Principal::update_items_prop ) );
        load_xml_dispatch.connect( SigC::slot(*this, &Principal::loadXML ) );
+       gc = Gdk::GC::create(get_window());
+       color_low = Gdk::Color("blue");
+       color_high = Gdk::Color("red");
+       Gtk::Widget::get_default_colormap()->alloc_color(color_low);
+       Gtk::Widget::get_default_colormap()->alloc_color(color_high);
 }
 
 Principal::~Principal()
 {
-       if (conexion != NULL)
-               delete conexion;
+       if (conexion) {
+               conexion->finish();
+       }
+       // Espera a que termine realmente.
+       while (conexion) {
+               Glib::usleep(100000); // 0,1 segundo
+       }
+}
+
+void Principal::on_realize()
+{
+       Gtk::Window::on_realize();
+}
+
+bool Principal::on_workplace_expose_event(GdkEventExpose *e)
+{
+       Glib::RefPtr<Gdk::Window> window = work_place->get_window();
+
+       int x1, y1, x2, y2;
+       // Dibujo las lineas
+       std::map<const std::string, ViewItem *>::iterator i;
+       for(i=mapItems.begin(); i!=mapItems.end(); i++) {
+               if (dynamic_cast<ViewAnd *>(i->second) || dynamic_cast<ViewOr *>(i->second) || dynamic_cast<ViewNot *>(i->second)) {
+                       x1 = i->second->x + i->second->in_x;
+                       y1 = i->second->y + i->second->in_y;
+                       std::list<std::string>::iterator linea;
+                       for(linea=i->second->in_lines.begin(); linea!=i->second->in_lines.end(); linea++) {
+                               ViewItem *tmp = find_item(*linea);
+                               x2 = tmp->x + tmp->out_x;
+                               y2 = tmp->y + tmp->out_y;
+                               if (tmp->get_open()) {
+                                       gc->set_foreground(color_high);
+                               } else {
+                                       gc->set_foreground(color_low);
+                               }
+                               gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
+                               window->draw_line(gc, x2+tmp->item_offset_x, y2+tmp->item_offset_y, x2+tmp->item_offset_x, y1+i->second->offset_y);
+                               window->draw_line(gc, x2+tmp->item_offset_x, y1+i->second->offset_y, x1+i->second->offset_x, y1+i->second->offset_y);
+                               window->draw_line(gc, x1, y1, x1+i->second->offset_x, y1+i->second->offset_y);
+                               window->draw_line(gc, x2, y2, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
+                       }
+                       x1 = i->second->x + i->second->out_x;
+                       y1 = i->second->y + i->second->out_y;
+                       for(linea=i->second->out_lines.begin(); linea!=i->second->out_lines.end(); linea++) {
+                               ViewItem *tmp = find_item(*linea);
+                               x2 = tmp->x + tmp->in_x;
+                               y2 = tmp->y + tmp->in_y;
+                               if (i->second->get_open()) {
+                                       gc->set_foreground(color_high);
+                               } else {
+                                       gc->set_foreground(color_low);
+                               }
+                               gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
+                               window->draw_line(gc, x1+i->second->offset_x, y1+i->second->offset_y, x1+i->second->offset_x, y2+tmp->item_offset_y);
+                               window->draw_line(gc, x1+i->second->offset_x, y2+tmp->item_offset_y, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
+                               window->draw_line(gc, x1, y1, x1+i->second->offset_x, y1+i->second->offset_y);
+                               window->draw_line(gc, x2, y2, x2+tmp->item_offset_x, y2+tmp->item_offset_y);
+                       }
+               }
+       }
+}
+
+ViewItem *Principal::find_item(std::string &_name)
+{
+       std::map<const std::string, ViewItem *>::iterator i;
+       for(i=mapItems.begin(); i!=mapItems.end(); i++) {
+               if (i->second->get_name() == _name) {
+                       return i->second;
+               }
+       }
+       return NULL;
 }
 
 void Principal::on_dlg_connect_ok()
@@ -75,30 +163,43 @@ void Principal::on_dlg_connect_ok()
                }
                catch (...) {
                        txt_view->get_buffer()->insert_at_cursor("NO SE PUDO CREAR OBJETO\n");
-                       delete conexion;
-                       conexion == NULL;
+                       //delete conexion; XXX Si no me equivoco, si falla el
+                       //constructor, no se reserva la memoria (el delete no va).
+                       conexion = NULL;
                        return;
                }
 
                // Conecto las señales
                conexion->signal_ok_received().connect( SigC::slot(*this, &Principal::on_conexion_ok) );
+               conexion->signal_error().connect( SigC::slot(*this, &Principal::on_conexion_fatal_error) );
                conexion->signal_error_received().connect( SigC::slot(*this, &Principal::on_conexion_error) );
-               conexion->signal_connected().connect( SigC::slot(*this, &Principal::on_conexion_connected) );
                conexion->signal_finished().connect( SigC::slot(*this, &Principal::on_conexion_finished) );
                conexion->signal_frame_received().connect(SigC::slot(*this, &Principal::on_conexion_frame));
                // Lanzo la conexion!
                conexion->run();
-       } else {
-               txt_view->get_buffer()->insert_at_cursor("YA ESTAS CONECTADO\n");
+               txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
+               ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
+
+               // Pido la planta por defecto
+               PlaQui::Server::Command c("plant", "get");
+               c.add_arg("default");
+               conexion->send(c);
        }
        dlg_conectar->hide();
 }
 
 void Principal::on_mnu_file_exit()
 {
+       on_mnu_file_disconnect();
        Gtk::Main::quit();
 }
 
+bool Principal::on_delete_event(GdkEventAny *e)
+{
+       on_mnu_file_exit();
+       return false;
+}
+
 void Principal::on_btn_activar_clicked()
 {
        if ((conexion == NULL) || (last_selected == NULL)) return;
@@ -120,10 +221,15 @@ void Principal::on_mnu_file_disconnect()
 {
        if (conexion == NULL) return;
 
-       PlaQui::Server::Command c("connection", "stop");
+       PlaQui::Server::Command c("transmission", "stop");
        c.add_arg(conexion->get_host());
-       c.add_arg("7522");
+       c.add_arg("7528");
        conexion->send(c);
+       PlaQui::Server::Command c2("connection", "stop");
+       c2.add_arg(conexion->get_host());
+       c2.add_arg(conexion->get_port());
+       conexion->send(c2);
+
 }
 
 void Principal::on_mnu_file_connect()
@@ -187,19 +293,13 @@ void Principal::update_items_prop()
 
        color_preview->modify_bg(Gtk::STATE_NORMAL, last_selected->get_color());
        color_preview->queue_draw();
-}
-
-void Principal::on_conexion_connected()
-{
-       txt_view->get_buffer()->insert_at_cursor("CONNECTED\n");
-       ico_conected->set( Gtk::Stock::YES , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
 
-       // Pido la planta por defecto
-       if (conexion != NULL) {
-               PlaQui::Server::Command c("plant", "get");
-               c.add_arg("default");
-               conexion->send(c);
+       // Mando a redibujar a todos los items
+       std::map<const std::string, ViewItem *>::iterator i;
+       for(i=mapItems.begin(); i!=mapItems.end(); i++) {
+               i->second->queue_draw();
        }
+       work_place->queue_draw();
 }
 
 void Principal::on_conexion_frame(const std::string &frame)
@@ -214,6 +314,12 @@ void Principal::on_conexion_finished()
        txt_view->get_buffer()->insert_at_cursor("HANG UP\n");
        ico_conected->set( Gtk::Stock::NO , Gtk::IconSize(Gtk::ICON_SIZE_LARGE_TOOLBAR));
        conexion = NULL;
+       // Elimino la planta    
+       std::map<const std::string, ViewItem *>::iterator i;
+       for(i=mapItems.begin(); i!=mapItems.end(); i++) {
+               delete i->second;
+       }
+       mapItems.clear();
 }
 
 void Principal::on_conexion_ok(const std::string &body)
@@ -232,14 +338,29 @@ void Principal::on_conexion_ok(const std::string &body)
        }
 }
 
-void Principal::on_conexion_error(unsigned code)
+void Principal::on_conexion_fatal_error(const PlaQui::Server::ControlClient::Error& code, const std::string& desc)
+{
+       std::stringstream a;
+       std::string s;
+       a << code;
+       a >> s;
+       txt_view->get_buffer()->insert_at_cursor("Error de red nro. ");
+       txt_view->get_buffer()->insert_at_cursor(s);
+       txt_view->get_buffer()->insert_at_cursor(": ");
+       txt_view->get_buffer()->insert_at_cursor(desc);
+       txt_view->get_buffer()->insert_at_cursor("\n");
+}
+
+void Principal::on_conexion_error(unsigned code, const std::string& desc)
 {
        std::stringstream a;
        std::string s;
        a << code;
        a >> s;
-       txt_view->get_buffer()->insert_at_cursor("El server dice que hay error : ");
+       txt_view->get_buffer()->insert_at_cursor("El server dice que hay error nro. ");
        txt_view->get_buffer()->insert_at_cursor(s);
+       txt_view->get_buffer()->insert_at_cursor(": ");
+       txt_view->get_buffer()->insert_at_cursor(desc);
        txt_view->get_buffer()->insert_at_cursor("\n");
 }
 
@@ -297,6 +418,12 @@ void Principal::loadXML()
                                        loadUnion(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
                                        loadDrain(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"and")==0) {
+                                       loadAnd(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"or")==0) {
+                                       loadOr(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"not")==0) {
+                                       loadNot(items);
                                }
 
                        }
@@ -304,6 +431,8 @@ void Principal::loadXML()
                }
        }
 
+       xmlFreeDoc(document);
+
        // Ya cargado el XML, mando un msg para empezar a recibir los frames!
        PlaQui::Server::Command c("transmission", "start");
        c.add_arg("default");
@@ -312,6 +441,112 @@ void Principal::loadXML()
        conexion->send(c);
 }
 
+void Principal::loadNot(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       int orientacion=0, x, y;
+
+       ViewNot *p;
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+                               orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                               p = new ViewNot(name, orientacion);
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+                               x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+                               y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                               p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                               p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+       
+       p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
+       p->set_position(x,y);
+       work_place->put(*p, x, y);
+       p->show();
+       // los agrego al hash
+       mapItems[name] = p;
+}
+
+void Principal::loadOr(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       int orientacion=0, x, y;
+
+       ViewOr *p;
+
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+                               orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                               p = new ViewOr(name, orientacion);
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+                               x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+                               y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                               p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                               p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
+       p->set_position(x,y);
+       work_place->put(*p, x, y);
+       p->show();
+       // los agrego al hash
+       mapItems[name] = p;
+}
+
+void Principal::loadAnd(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       int orientacion=0, x, y;
+       float flujo;
+       xmlNodePtr inicial = nodo;
+
+       ViewAnd *p;
+       
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+                               orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                               p = new ViewAnd(name, orientacion);
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+                               x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+                               y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                               p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                               p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       p->signal_button_press_event().connect(SigC::bind( SigC::slot(*this, &Principal::on_item_clicked), p) );
+       p->set_position(x,y);
+       work_place->put(*p, x, y);
+       p->show();
+       // los agrego al hash
+       mapItems[name] = p;
+}
+
 void Principal::loadBomba(xmlNodePtr nodo)
 {
        Glib::ustring name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
@@ -548,7 +783,11 @@ void Principal::read_status_xml(const std::string &frame)
                                        tmp_b = get_bool_from_xml(items->children);
                                        item_name = (char *)xmlGetProp(items, BAD_CAST"name");
                                        mapItems[item_name]->set_open(tmp_b);
-                               }  else if (xmlStrcmp(items->name, BAD_CAST"color")==0) {
+                               } else if (xmlStrcmp(items->name, BAD_CAST"logic")==0) {
+                                       tmp_b = get_bool_from_xml(items->children);
+                                       item_name = (char *)xmlGetProp(items, BAD_CAST"name");
+                                       mapItems[item_name]->set_open(tmp_b);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"color")==0) {
                                        item_name = (char *)xmlGetProp(items, BAD_CAST"name");
                                        mapItems[item_name]->set_color( get_rgb_from_xml(items->children) );
                                } else if (xmlStrcmp(items->name, BAD_CAST"tank")==0) {
@@ -572,6 +811,7 @@ void Principal::read_status_xml(const std::string &frame)
                        items = items->next;
                }
 
+               xmlFreeDoc(document);
                // Actualizo la UI
                update_ui();
        }
@@ -579,7 +819,7 @@ void Principal::read_status_xml(const std::string &frame)
 
 Gdk::Color Principal::get_rgb_from_xml(xmlNodePtr nodo)
 {
-       unsigned r,g,b;
+       gushort r,g,b;
        while (nodo != NULL) {
                if (nodo->type == XML_ELEMENT_NODE) {
                        if (xmlStrcmp(nodo->name, BAD_CAST"r")==0)
@@ -591,14 +831,15 @@ Gdk::Color Principal::get_rgb_from_xml(xmlNodePtr nodo)
                }
                nodo = nodo->next;
        }
-       r = 65535 * r / 255;
-       g = 65535 * g / 255;
-       b = 65535 * b / 255;
+       r = static_cast<gushort>(65535 * (r / 255.0f));
+       g = static_cast<gushort>(65535 * (g / 255.0f));
+       b = static_cast<gushort>(65535 * (b / 255.0f));
        Gdk::Color c;
        c.set_rgb(r,g,b);
 
        return c;
 }
+
 float Principal::get_float_from_xml(xmlNodePtr nodo)
 {
        float tmp = -1;
@@ -611,6 +852,7 @@ float Principal::get_float_from_xml(xmlNodePtr nodo)
                }
                nodo = nodo->next;
        }
+       if (tmp == INFINITO) tmp = 0;
        return tmp;
 }
 
@@ -629,3 +871,38 @@ bool Principal::get_bool_from_xml(xmlNodePtr nodo)
        return tmp == "true";
 }
 
+void Principal::on_mnu_property()
+{
+       dlg_property->show();
+}
+
+void Principal::on_dlg_property_ok()
+{
+       if (conexion) {
+               PlaQui::Server::Command c("plant", "set_frequency");
+               c.add_arg("default");
+               c.add_arg( dlg_property->get_velocity() );
+
+               conexion->send(c);
+       }
+       dlg_property->hide();
+}
+
+void Principal::on_btn_simulate_clicked()
+{
+       if (conexion == NULL) return;
+
+       PlaQui::Server::Command c("plant", "start");
+       c.add_arg("default");
+       conexion->send(c);
+}
+
+void Principal::on_btn_pause_clicked()
+{
+       if (conexion == NULL) return;
+
+       PlaQui::Server::Command c("plant", "stop");
+       c.add_arg("default");
+       conexion->send(c);
+}
+