]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Constructor/constructor.cpp
Se agrega generacion de XML para dar el estado actual de la planta simulada.
[z.facultad/75.42/plaqui.git] / Constructor / constructor.cpp
index 2f1122177f0a6a3b635d52ea0b49e25fc3b2c252..aa162e92f67c90acd0de27a4a7de06fbc7a1bb9c 100644 (file)
@@ -1,4 +1,3 @@
-//constructor.cpp
 #include "constructor.h"
 
 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
@@ -7,22 +6,40 @@ Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glad
        set_title("Constructor");
        
        
-       ico_canio = Gdk::Pixbuf::create_from_file("item_canio.png");
-       ico_y     = Gdk::Pixbuf::create_from_file("item_y.png");
-       ico_codo  = Gdk::Pixbuf::create_from_file("item_codo.png");
-       ico_tanque  = Gdk::Pixbuf::create_from_file("item_tanque.png");
+       ico_canio = Gdk::Pixbuf::create_from_file("canio_n.png");
+       ico_y     = Gdk::Pixbuf::create_from_file("y_n.png");
+       ico_codo  = Gdk::Pixbuf::create_from_file("codo_o.png");
+       ico_tanque  = Gdk::Pixbuf::create_from_file("tanque_e.png");
+       ico_exclusa = Gdk::Pixbuf::create_from_file("exclusa_h.png");
+       ico_drain = Gdk::Pixbuf::create_from_file("drain_n.png");
+       ico_bomba = Gdk::Pixbuf::create_from_file("bomba_e.png");
+       ico_and = Gdk::Pixbuf::create_from_file("and_e.png");
+       ico_or = Gdk::Pixbuf::create_from_file("or_e.png");
+       ico_not = Gdk::Pixbuf::create_from_file("not_e.png");
        
        refGlade->get_widget("btn_canio", btn_canio);
        refGlade->get_widget("btn_codo", btn_codo);
        refGlade->get_widget("btn_y", btn_y);
        refGlade->get_widget("btn_tanque",btn_tanque);
+       refGlade->get_widget("btn_bomba", btn_bomba);
+       refGlade->get_widget("btn_exclusa", btn_exclusa);
+       refGlade->get_widget("btn_drain", btn_drain);
+       refGlade->get_widget("btn_and", btn_and);
+       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_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_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_derived("workplace", workplace);  //fixed
        
-       
-       
        //Targets
        listTargets.push_back( Gtk::TargetEntry("STRING") );
        listTargets.push_back( Gtk::TargetEntry("text/plain") );
@@ -33,35 +50,59 @@ Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glad
        btn_y->drag_source_set(listTargets);
        btn_codo->drag_source_set(listTargets);
        btn_tanque->drag_source_set(listTargets);
+       btn_bomba->drag_source_set(listTargets);
+       btn_drain->drag_source_set(listTargets);
+       btn_exclusa->drag_source_set(listTargets);
+       btn_and->drag_source_set(listTargets);
+       btn_or->drag_source_set(listTargets);
+       btn_not->drag_source_set(listTargets);
        
        btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
+       btn_bomba->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_bomba_drag_get));
+       btn_exclusa->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_exclusa_drag_get));
        btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
        btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
        btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
+       btn_drain->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_drain_drag_get));
+       btn_and->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_and_drag_get));
+       btn_or->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_or_drag_get));
+       btn_not->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_not_drag_get));
+       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));
        
        main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
+       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));
        
        // 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));
        btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
        btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
-       
-       
+       btn_bomba->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_bomba_drag_begin));
+       btn_exclusa->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_exclusa_drag_begin));
+       btn_drain->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_drain_drag_begin));
+       btn_and->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_and_drag_begin));
+       btn_or->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_or_drag_begin));
+       btn_not->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_not_drag_begin));
        workplace->drag_dest_set(listTargets);
        workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
        workplace->listaItems = &listaItems;    
+       logica = false;
 }
 
 Constructor::~Constructor()
 {
        std::list<CItem *>::iterator i = listaItems.begin();
-       std::cout << "ok" << std::endl;
        while ( i != listaItems.end() ){
                CItem *temp = *i;
                listaItems.erase(i);
-               std::cout << "Elimnando ... " << std::endl;
                delete temp;
                i = listaItems.begin();
        }
@@ -69,34 +110,115 @@ Constructor::~Constructor()
 
 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
 {
-       gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"item_canio.png", 14);
+       gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 12);
 }
 
 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
 {
-       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_y.png",10);
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",8);
 }
 
 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
 {
-       gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"item_codo.png",13);
+       gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",10);
 }
 
 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
 {
-       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_tanque.png",15);
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",12);
+}
+
+void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",11);
+}
+
+void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",13);
+}
+
+void Constructor::on_btn_drain_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"drain_n.png",11);
+}
+
+void Constructor::on_btn_and_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"and_e.png",9);
+}
+
+void Constructor::on_btn_or_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"or_e.png",8);
 }
 
+void Constructor::on_btn_not_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
+{
+       gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"not_e.png",9);
+}
+
+
 void Constructor::on_main_menu_quit()
 {
-       //Gtk::Main::quit();
-       hide();
+       delete this;
+       Gtk::Main::quit();
+       //hide();
+}
+
+void Constructor::on_main_menu_save()
+{
+       file_selection->show();
 }
 
 void Constructor::on_edit_menu_del()
+{  // OJO que borra al primero que encuentra con el mismo nombre!!!
+       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++;
+       }
+}
+
+void Constructor::on_chk_btn_clicked()
 {
-       // hay que meter algo aca.
+       logica = !logica;
 }
+
+void Constructor::on_btn_file_ok_clicked()
+{
+       std::list<CItem *>::iterator i = listaItems.begin();
+       std::string 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++;
+               }
+               fprintf(archivo,"</planta>\n");
+       } else 
+               std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
+       fclose(archivo);
+       file_selection->hide();
+}
+
+void Constructor::on_btn_file_cancel_clicked()
+{
+       file_selection->hide();
+}
+
+void Constructor::on_btn_dlg_connect_clicked()
+{
+       dlg_connect->hide();
+}
+
 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
 {
        context->set_icon(ico_canio, 5, 5); 
@@ -117,9 +239,44 @@ void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& con
        context->set_icon(ico_tanque, 5, 5); 
 }
 
+void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_bomba, 5, 5); 
+}
+
+void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_exclusa, 5, 5); 
+}
+
+void Constructor::on_drain_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_drain, 5, 5); 
+}
+
+void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_and, 5, 5); 
+}
+
+void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_or, 5, 5); 
+}
+
+void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
+{
+       context->set_icon(ico_not, 5, 5); 
+}
+
 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
 {
        context->set_icon(item->get_image(), 5, 5);
+       std::list<CItem *>::iterator i = listaItems.begin();
+       while ( i != listaItems.end() ){
+               (*i)->set_default_connector();
+               i++;
+       }
 }
 
 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
@@ -127,16 +284,16 @@ void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& co
                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)
+bool Constructor::can_drop(CItem *item, int _x, int _y)
 {
        std::list<CItem*>::iterator  i = listaItems.begin();
        while( i != listaItems.end() ) {
                CItem *temp = *i;
                if ( temp->get_id() != item->get_id()   ) {
-                       if (  (temp->is_occupied_area(x, y))  ||  
-                               ( temp->is_occupied_area(x+item->get_image()->get_width()-1, y+item->get_image()->get_height()-1))  ||
-                               ( temp->is_occupied_area(x, y+item->get_image()->get_height()-1)) ||
-                               ( temp->is_occupied_area(x+item->get_image()->get_width()-1, y) ) ) 
+                       if (  (temp->is_occupied_area(_x, _y))  ||  
+                               ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
+                               ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
+                               ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
                                return false;
                        else i++;
                }
@@ -147,61 +304,97 @@ 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)
 {
+       if ( !logica ) {
                /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
-       int i,j;
-       // el +1 es para evitar un bug cuando se selecciona muy cerce de la
-       // separacion de 2 cuadritos
-       i = (x+1)/32;
-       j = (y+1)/32;
-       // El drag es de un item
-       if (selection_data->format == 10) {
-               if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
-                       ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
-                       workplace->move(*drag_get_source_widget(context), i*32, j*32);
+               int i,j;
+               // el +1 es para evitar un bug cuando se selecciona muy cerce de la
+               // separacion de 2 cuadritos
+               i = (x+1)/32;
+               j = (y+1)/32;
+               // El drag es de un item
+               if (selection_data->format == 10) {
+                       if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
+                               ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
+                               workplace->move(*drag_get_source_widget(context), i*32, j*32);
+                       }
                }
+
+               // El Drag es desde la barra de tareas
+               if ((selection_data->length >= 0) && (selection_data->format == 8))     {
+                       CItem *a;
+                       if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
+                               a = new Splitter();
+                                       else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
+                                               a = new Conduct();
+                                                       else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
+                                                               a = new Union();
+                                                                       else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
+                                                                               a = new Cistern();
+                                                                                       else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
+                                                                                               a =new Pump();
+                                                                                                       else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
+                                                                                                               a = new Exclusa();
+                                                                                                                       else if (strcmp((const char *)selection_data->data, "drain_n.png")==0) 
+                                                                                                                               a = new Drain();
+                                                                                                                                       else if (strcmp((const char *)selection_data->data, "and_e.png")==0) 
+                                                                                                                                               a = new And();
+                                                                                                                                                       else if (strcmp((const char *)selection_data->data, "or_e.png")==0) 
+                                                                                                                                                               a = new Or();
+                                                                                                                                                                       else if (strcmp((const char *)selection_data->data, "not_e.png")==0) 
+                                                                                                                                                                               a = new Not();  
+               
+                       char char_id[10];
+                       sprintf(char_id,"%d",id);
+                       a->set_id(++id);                
+                       a->set_name( a->get_name()+char_id );
+                       if ( can_drop(a, i*32, j*32) ) {
+                               workplace->put(*a, i*32, j*32);
+                               //Apunto al workplace
+                               a->workplace= workplace;
+                               //Apunto a la lista.
+                               a->combo_entry = combo_entry;
+                               //Apunto a la listaItems.
+                               a->listaItems = &listaItems;
+                               //Seteo la posicion del item
+                               a->set_position(i*32,j*32);
+                               // Seteo la lista de tipos de drags 
+                               a->drag_source_set(listTargets);
+                               // Conecto las señales
+                               a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
+                               // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
+                               // parametro extra, en este caso un CItem *. Esto se hace para
+                               // que cuando el usuario quiera mover un item, saber que item es
+                               // y pedirle su ícono para mostrar cono icono durante la operacion,
+                               // Esto va a permitir, que si un widget tiene una imagen rotara o algo
+                               // raro se vea el widget tal cual.
+                               a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
+                               a->show();      
+                               listaItems.push_back(a);
+                       } else {
+                                       id--;
+                                       delete a;
+                               }
+               }
+               context->drag_finish(false, false, time);
        }
+}
 
-       // El Drag es desde la barra de tareas
-  if ((selection_data->length >= 0) && (selection_data->format == 8))
-  {
-       CItem *a;
-       if (strcmp((const char *)selection_data->data, "item_codo.png")==0) 
-               a = new Splitter();//Gtk::manage( new Splitter() );
-       else    if (strcmp((const char *)selection_data->data, "item_canio.png")==0) 
-                               a = new Conduct();//Gtk::manage( new Conduct() );
-                       else if (strcmp((const char *)selection_data->data, "item_y.png")==0) 
-                                               a = new Union();//Gtk::manage( new Union() );
-                                       else if (strcmp((const char *)selection_data->data, "item_tanque.png")==0) 
-                                                               a = new Cistern();//Gtk::manage( new Cistern() );
-                                                       else
-                                                               a = new CItem();//Gtk::manage( new CItem((const char *)selection_data->data) );
-       //Seteo el ID del item
-       a->set_id(++id);                
-       std::cout << can_drop(a, i*32, j*32) <<" ---  tamanio lista = "<< listaItems.size()<< "item = "<<a->get_id()<<std::endl;
-       if ( can_drop(a, i*32, j*32) ) {
-               workplace->put(*a, i*32, j*32);
-               //Apunto al workplace
-               a->workplace= workplace;
-               //Seteo la posicion del item
-               a->set_position(i*32,j*32);
-               // Seteo la lista de tipos de drags 
-               a->drag_source_set(listTargets);
-               // Conecto las señales
-               a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
-
-               // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
-               // parametro extra, en este caso un CItem *. Esto se hace para
-               // que cuando el usuario quiera mover un item, saber que item es
-               // y pedirle su ícono para mostrar cono icono durante la operacion,
-               // Esto va a permitir, que si un widget tiene una imagen rotara o algo
-               // raro se vea el widget tal cual.
-               a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
-               a->show();      
-               listaItems.push_back(a);
-       } else {
-                       id--;
-                       delete a;
+void Constructor::on_btn_check_clicked()
+{
+       std::list<CItem *>::iterator i = listaItems.begin();
+       if ( !listaItems.empty() ) {
+               while ( i != listaItems.end() ) {
+                       CItem *temp = *i;
+                       if ( !temp->check_connection() ) {
+                               dlg_connect->set_title("Error");
+                               dlg_label->set_text("Los elementos no estan conectados\n\t\tcorrectamente");
+                               dlg_connect->show();
+                               return;
+                       }       
+               i++;
+               }
+               dlg_connect->set_title("Conexion");
+               dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
+               dlg_connect->show();    
        }
-  }
-  context->drag_finish(false, false, time);
 }