]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Constructor/src/constructor.cpp
-arreglo el bug de la union cuando se carga desde un archivo
[z.facultad/75.42/plaqui.git] / Constructor / src / constructor.cpp
index 1dcc0074b15771bb6bcd8fad69158c6aa7d8a55a..2966528a44aef807761f8ab53377ac89cbec1cb0 100644 (file)
@@ -3,6 +3,7 @@
 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
 {
        id = 0;
+       saved = false;
        set_title("Constructor");
        
        
@@ -28,15 +29,19 @@ Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glad
        refGlade->get_widget("btn_or", btn_or);
        refGlade->get_widget("btn_not", btn_not);
        refGlade->get_widget("main_menu_quit",main_menu_quit);
+       refGlade->get_widget("main_menu_open",main_menu_open);
        refGlade->get_widget("main_menu_save",main_menu_save);
        refGlade->get_widget("edit_menu_del",edit_menu_del);
        refGlade->get_widget("chk_btn_logica",chk_btn_logica);
+       refGlade->get_widget("file_open_selection",file_open_selection);
        refGlade->get_widget("file_selection",file_selection);
        refGlade->get_widget("combo_entry",combo_entry);
        refGlade->get_widget("btn_check",btn_check);
        refGlade->get_widget("dlg_connect", dlg_connect);
        refGlade->get_widget("btn_dlg_close", btn_dlg_close);
        refGlade->get_widget("dlg_label", dlg_label);
+       refGlade->get_widget("quick_btn_clear", quick_btn_clear);
+       refGlade->get_widget("quick_btn_save", quick_btn_save);
        
        refGlade->get_widget_derived("workplace", workplace);  //fixed
        
@@ -70,16 +75,24 @@ Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glad
        btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
        btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
        chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
+       quick_btn_clear->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_clear_clicked));
+       quick_btn_save->signal_clicked().connect(SigC::slot(*this, &Constructor::on_quick_btn_save_clicked));
        
        main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
+       main_menu_open->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_open));
        main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
        edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
                        
        btn_file_cancel = file_selection->get_cancel_button();
        btn_file_ok = file_selection->get_ok_button();
-       btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
        btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
+       btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
        
+       btn_open_cancel = file_open_selection->get_cancel_button();
+       btn_open_ok = file_open_selection->get_ok_button();
+       btn_open_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_load_from_xml));
+       btn_open_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_open_cancel_clicked));
+
        // Señales para cambiar el icono cuando empieza el drag.
        btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
        btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
@@ -100,13 +113,7 @@ Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glad
 
 Constructor::~Constructor()
 {
-       std::list<CItem *>::iterator i = listaItems.begin();
-       while ( i != listaItems.end() ){
-               CItem *temp = *i;
-               listaItems.erase(i);
-               delete temp;
-               i = listaItems.begin();
-       }
+       Constructor::on_edit_menu_delete_all();
 }
 
 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
@@ -167,54 +174,174 @@ void Constructor::on_main_menu_quit()
        //hide();
 }
 
+void Constructor::on_main_menu_open()
+{
+       // Conecto el boton OK para llamar al cargar del XML
+       file_open_selection->show();
+}
+
 void Constructor::on_main_menu_save()
 {
+       // Conecto el boton OK para llamar al salvar
        file_selection->show();
 }
 
 void Constructor::on_edit_menu_del()
-{  // OJO que borra al primero que encuentra con el mismo nombre!!!
+{ 
+       workplace->delete_item(WorkPlace::pointed);
+}
+
+void Constructor::on_edit_menu_delete_all()
+{
        std::list<CItem *>::iterator i = listaItems.begin();
-       Glib::ustring pointed = combo_entry->get_text();
-       while ( i != listaItems.end() ){
-               CItem *temp = *i;
-               if ( temp->get_name() == pointed ){ 
-                       workplace->delete_item(temp->get_id());
-                       break;
-               }
-       i++;
+       while ( i != listaItems.end() ) {
+               (*i)->workplace->delete_item((*i)->get_id());
+               i = listaItems.begin();
+       }
+       i = lista_logic_Items.begin();
+       while ( i != lista_logic_Items.end() ) {
+               (*i)->workplace->delete_item((*i)->get_id());
+               i=lista_logic_Items.begin();
        }
 }
 
 void Constructor::on_chk_btn_clicked()
 {
        logica = !logica;
+       CItem::logic_connect = !CItem::logic_connect;
+}
+
+void Constructor::on_load_from_xml()
+{
+       file_name = file_open_selection->get_filename();
+
+       /* Parseo de ejemplo de un XML desde archivo */
+       xmlDocPtr document;
+       document = xmlParseFile(file_name.c_str());
+       if (document == NULL) {
+               // TODO : dar un aviso de que no se pudo abrir el archivo!!
+               return;
+       }
+
+       /* bien, el archivo se parseo bien! */
+       xmlNodePtr nodo, items;
+       nodo = document->children;
+
+       // Recorro los items
+       CItem *current;
+       if (strcmp((char *)nodo->name, "planta") == 0) {
+               items = nodo->children;
+               while (items != NULL) {
+                       current = NULL;
+                       if (items->type == XML_ELEMENT_NODE) {
+                               std::cout << "ITEM" << std::endl;
+                               if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
+                                       current = loadBomba(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
+                                       current = loadConduct(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
+                                       current = loadCodo(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
+                                       current = loadExclusa(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
+                                       current = loadTank(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
+                                       current = loadUnion(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
+                                       current = loadDrain(items);
+                               }
+
+                               if (current != NULL) {
+                                       // Agrego y conecto la bomba
+                                       listaItems.push_back(current);
+                                       current->drag_source_set(listTargets);
+                                       workplace->put(*current, current->get_position_x(), current->get_position_y());
+                                       //Apunto al workplace
+                                       current->workplace= workplace;
+                                       //Apunto a la lista.
+                                       current->combo_entry = combo_entry;
+                                       //Apunto a la listaItems.
+                                       current->listaItems = &listaItems;
+                                       //Apunto a la lista de items logicos
+                                       current->lista_logic_Items = &lista_logic_Items;
+                                       // Conecto las señales
+                                       current->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
+                                       current->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), current));
+                                       current->show();        
+                               }
+                       }
+                       items = items->next;
+               }
+       } else {
+               // TODO : avisar que el XML no es valido!!
+       }
+       std::cout << "CARGA COMPLETA" << std::endl;
+       file_open_selection->hide();
+       id = listaItems.size()+lista_logic_Items.size()+1;
 }
 
 void Constructor::on_btn_file_ok_clicked()
 {
        std::list<CItem *>::iterator i = listaItems.begin();
-       std::string file_name = file_selection->get_filename();
+       file_name = file_selection->get_filename();
        if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
                fprintf(archivo, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
                fprintf (archivo,"<planta>\n");
                while ( i != listaItems.end() ){
-                       CItem *temp = *i;
-                       temp->save(archivo);
+                       (*i)->save(archivo);
+                       i++;
+               }
+               i = lista_logic_Items.begin();
+               while ( i != lista_logic_Items.end() ) {
+                       (*i)->save(archivo);
                        i++;
                }
                fprintf(archivo,"</planta>\n");
+               saved = true;
        } else 
                std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
        fclose(archivo);
        file_selection->hide();
 }
 
+void Constructor::on_quick_btn_save_clicked()
+{
+       if ( ! saved ) {
+               on_main_menu_save();
+       } else {
+               on_btn_file_ok_clicked();
+       }
+}
+       
+
+void Constructor::on_quick_btn_clear_clicked()
+{
+       std::list<CItem *>::iterator i = listaItems.begin();
+       while ( i != listaItems.end() ){
+               CItem *temp = *i;
+               listaItems.erase(i);
+               delete temp;
+               i = listaItems.begin();
+       }
+       i = lista_logic_Items.begin();
+       while ( i != lista_logic_Items.end() ){
+               CItem *temp2 = *i;
+               lista_logic_Items.erase(i);
+               delete temp2;
+               i = lista_logic_Items.begin();
+       }
+}      
+
 void Constructor::on_btn_file_cancel_clicked()
 {
        file_selection->hide();
 }
 
+void Constructor::on_btn_open_cancel_clicked()
+{
+       file_open_selection->hide();
+}
+
 void Constructor::on_btn_dlg_connect_clicked()
 {
        dlg_connect->hide();
@@ -272,14 +399,12 @@ void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& contex
 
 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
 {
-       GdkEventExpose e;
        context->set_icon(item->get_image(), 5, 5);
-       workplace->on_expose_event(&e);
 }
 
 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
 {      
-               gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
+       gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
 }
 
 bool Constructor::can_drop(CItem *item, int _x, int _y)
@@ -302,6 +427,8 @@ bool Constructor::can_drop(CItem *item, int _x, int _y)
 
 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
 {
+       workplace->update_logic_position();
+       workplace->queue_draw();
        if ( !logica ) {
                /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
                int i,j;
@@ -339,7 +466,7 @@ void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext
                        } else if (strcmp((const char *)selection_data->data, "or_e.png")==0) {
                                a = new Or(); a->is_logic = true;
                                } else if (strcmp((const char *)selection_data->data, "not_e.png")==0) {
-                                       a = new Not();  a->is_logic = true;
+                                       a = new Not(); a->is_logic = true;
                                        }
                
                        char char_id[10];
@@ -381,6 +508,8 @@ void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext
                }
                context->drag_finish(false, false, time);
        }
+       workplace->update_logic_position();
+       workplace->queue_draw();
 }
 
 void Constructor::on_btn_check_clicked()
@@ -409,4 +538,242 @@ void Constructor::on_btn_check_clicked()
                dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
                dlg_connect->show();    
        }
+       std::cout<<"check_connection logic"<<std::endl;
+       std::list<CItem *>::iterator j = lista_logic_Items.begin();
+       while ( j != lista_logic_Items.end() ) {
+               (*j)->check_connection();
+               j++;
+       }
+
+
+}
+
+Pump *Constructor::loadBomba(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;
+
+       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) );
+                       } 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"entrega") == 0) {
+                               flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
+                               // FIXME !
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Pump *p = new Pump(orientacion);
+       p->set_position(x,y);
+       p->set_entrega(flujo);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+
+       return p;
+}
+
+Conduct *Constructor::loadConduct(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;
+
+       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) );
+                       } 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"caudal") == 0) {
+                               flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Conduct *p = new Conduct(orientacion);
+       p->set_position(x,y);
+       p->set_caudal(flujo);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+
+       return p;
+}
+
+Exclusa *Constructor::loadExclusa(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       std::string estado;
+       int orientacion=0, x, y;
+       float flujo;
+
+       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) );
+                       } 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"estado") == 0) {
+                               estado = (char *)XML_GET_CONTENT(nodo->children);
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Exclusa *p = new Exclusa(orientacion);
+       p->set_position(x,y);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+       p->set_estado( estado == "1" );
+
+       return p;
+}
+
+Cistern *Constructor::loadTank(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 liquido,capacidad;
+
+       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) );
+                       } 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"capacidad") == 0) {
+                               capacidad = atof ((char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
+                               liquido = atof ((char *)XML_GET_CONTENT(nodo->children) );
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Cistern *p = new Cistern(orientacion);
+       p->set_position(x,y);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+       p->set_capacidad(capacidad);
+       p->set_contenido_inicial(liquido);
+
+       return p;
+}
+
+Union *Constructor::loadUnion(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       std::string tipo;
+       int orientacion=0, x, y;
+       float caudal;
+
+       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) );
+                       } 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"caudal") == 0) {
+                               caudal = atof ((char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
+                               tipo = (char *)XML_GET_CONTENT(nodo->children);
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Union *p = new Union(orientacion);
+       p->set_position(x,y);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+       p->set_caudal(caudal);
+       p->is_union = (tipo == "union");
+
+       return p;
+}
+
+Drain *Constructor::loadDrain(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;
+
+       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) );
+                       } 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) );
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Drain *p = new Drain(orientacion);
+       p->set_position(x,y);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+
+       return p;
+}
+
+Splitter *Constructor::loadCodo(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 caudal;
+
+       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) );
+                       } 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"caudal") == 0) {
+                               caudal = atof( (char *)XML_GET_CONTENT(nodo->children) );
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Splitter *p = new Splitter(orientacion);
+       p->set_position(x,y);
+       p->set_id( atoi(id.c_str()) );
+       p->set_name(name);
+       p->set_caudal(caudal);
+
+       return p;
 }