]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/exclusa.cpp
- Se agrega el metodo Model::Simulator::set_open() para abrir y cerrar bombas y
[z.facultad/75.42/plaqui.git] / Constructor / src / exclusa.cpp
1 #include "exclusa.h"
2 #include "exclusaptywnd.h"
3
4 Exclusa::Exclusa(int orientacion)
5 {
6         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
7         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_s.png");
8         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
9         imgActual = orientacion;
10         switch (imgActual) {
11                 case 1:
12                         image = imageS;
13                         break;
14                 default: 
15                         imgActual = 0;
16                         image = imageN;                 
17         }
18         set_size_request(image->get_width(), image->get_height());
19         
20         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(PACKAGE_DATA_DIR"/plaqui-constructor/dialogs/constructor.glade", "exclusa_pty_wnd");
21         ref->get_widget_derived("exclusa_pty_wnd",exclusa_pty_wnd);
22         exclusa_pty_wnd->exclusa = this;
23         name = "exclusa";
24         
25         Connector temp;
26         temp.id_dest = -1;
27         temp.type = UNDEF;
28         connect_vec.push_back(temp);
29         connect_vec.push_back(temp);
30 }
31
32 Exclusa::~Exclusa()
33 {
34 }
35
36 bool Exclusa::on_button_press_event(GdkEventButton *event)
37 {
38         GdkEventExpose e;
39         t_line tmp_line;
40         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
41                 combo_entry->set_text(name);
42                 WorkPlace::pointed = ID;
43                 if (CItem::logic_connect) {
44                         tmp_line.logic =  workplace->get_logic_item(CItem::quien);
45                         tmp_line.store = this;
46                         workplace->lista_lineas.push_back(tmp_line);
47                         workplace->on_expose_event(&e);
48                 }       
49         }
50         
51         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
52                 image = null; 
53                 image->render_to_drawable(get_window(),get_style()->get_black_gc(),0,0,0,0,image->get_width(),image->get_height(),Gdk::RGB_DITHER_NONE,0,0);
54                 imgActual++;
55                 switch (imgActual) {
56                         case 1:
57                                 image = imageS;
58                                 break;
59                         default: 
60                                 imgActual = 0;
61                                 image = imageN;                 
62                 }
63                 set_size_request(image->get_width(),image->get_height());               
64                 image->render_to_drawable(get_window(),get_style()->get_black_gc(),0,0,0,0,image->get_width(),image->get_height(),Gdk::RGB_DITHER_NONE,0,0);
65         }
66         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
67                 menu_popup.popup(event->button, event->time);
68                  return true; //It has been handled.
69         }
70         
71         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
72                 exclusa_pty_wnd->txt_exclusa_name->set_text( name);
73                 if (estado) exclusa_pty_wnd->rd_btn_open->set_active(true);
74                 exclusa_pty_wnd->show();
75         }
76         workplace->on_expose_event(&e);
77         return true;
78 }
79
80 void Exclusa::set_estado(bool _state)
81 {
82         estado = _state;
83 }
84
85 bool Exclusa::get_estado()
86 {
87         return estado;
88 }
89
90 void Exclusa::on_menu_popup_rotar()
91 {
92         GdkEventButton event; 
93                 event.type = GDK_BUTTON_PRESS;
94                 event.button = 2;
95         Exclusa::on_button_press_event(&event);
96 }
97
98 void Exclusa::on_menu_popup_propiedades()
99 {
100         GdkEventButton event;
101         event.type = GDK_2BUTTON_PRESS;
102         event.button = 1;
103         Exclusa::on_button_press_event(&event);
104 }
105
106 void Exclusa::save(FILE *archivo)
107 {
108         char c_id[50], c_est[50], c_x[50], c_y[50], c_img[50];
109         int est;
110         Glib::ustring  con0, con1;
111         if (connect_vec[0].type == IN)
112                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
113         else
114                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
115         if (connect_vec[1].type == IN) 
116                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
117         else
118                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
119         sprintf(c_x,"\t\t<x>%d</x>\n",x);
120         sprintf(c_y,"\t\t<y>%d</y>\n",y);
121         sprintf(c_id,"%d",ID);
122         if (estado) est = 1;
123         else est = 0;
124         sprintf(c_est,"\t\t<estado>%d</estado>\n", est);
125         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
126         Glib::ustring dato;
127         dato = "\t<exclusa nombre=\""+name+"\" id=\"";
128         dato += c_id;
129         dato += "\">\n";
130         dato += "\t\t<conector>\n"+con0 + con1+"\t\t</conector>\n";
131         dato += c_img;
132         dato += c_est;
133         dato += c_x; 
134         dato += c_y;
135         dato += "\t</exclusa>\n";
136         fprintf(archivo,dato.c_str());  
137 }
138
139 bool Exclusa::check_connection()
140 {
141         ConnectorType aux;
142         CItem *_item0, *_item1;
143         switch (get_img_actual()) {
144                         case 0:
145                                 connect_vec[0].type = is_other_connection_area( get_position_x()-5, get_position_y()+16, &_item0); //izquierda
146                                 connect_vec[1].type = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+16,& _item1); //derecha
147                         break;          
148                         case 1:
149                                 connect_vec[0].type =  is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item0); // arriba
150                                 connect_vec[1].type = is_other_connection_area( get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item1); //abajo
151         }
152         if ( connect_vec[0].type != UNDEF && connect_vec[1].type != UNDEF ) {
153                 aux = connect_vec[0].type; 
154                 connect_vec[0].type = connect_vec[1].type;
155                 connect_vec[1].type = aux;
156                 connect_vec[0].id_dest = _item0->get_id();
157                 connect_vec[1].id_dest = _item1->get_id();
158                 return ( is_connected = (connect_vec[0].type != connect_vec[1].type) );
159         }
160         else return is_connected;
161 }
162
163 ConnectorType Exclusa::get_connector_type(int _a, int _b)
164 {
165         CItem * _item;
166         if ( ! is_connected ) {
167                 switch (imgActual) {
168                         case 0:
169                                 if ((_a <= x+10 )&&(_a > 0) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )) //izquierda
170                                         return is_other_connection_area(get_position_x()+get_image()->get_width()+5, get_position_y()+16, &_item);//pregunto por la derecha
171                                 if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) //derecha
172                                         return is_other_connection_area(get_position_x()-5, get_position_y()+16, &_item); //pregunto por la izquierda.
173                                 break;
174                         case 1: 
175                                 if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > 0 ) )  //arriba
176                                         return is_other_connection_area(get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item); //pregunto que hay abajo
177                                 if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 ))//abajo
178                                         return is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item);//pregunto que hay arriba.
179                 }
180         } else {
181                 switch (imgActual) {
182                         case 0: 
183                                 if ((_a <= x+10 )&&(_a > 0) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )) //izquierda
184                                         return connect_vec[0].type;
185                                 if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) //derecha
186                                         return connect_vec[1].type;
187                                 break;
188                         case 1:
189                                 if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > 0 ) ) //arriba
190                                         return connect_vec[0].type;
191                                 if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 )) //abajo
192                                         return connect_vec[1].type;
193                 }
194         }
195         return UNDEF;
196 }
197
198 void Exclusa::get_logic_connect_position(int& _a, int& _b)
199 {
200         switch (imgActual) {
201                 case 0: 
202                         _a = x + image->get_width()/2;
203                         _b = y;
204                         break;
205                 case 1: 
206                         _a = x + image->get_width();
207                         _b = y + image->get_height()/2;
208         }
209 }
210         
211 void Exclusa::set_default_connector()
212 {
213         connect_vec[0].type = UNDEF;
214         connect_vec[1].type = UNDEF;
215 }