]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/exclusa.cpp
corregi las imagenes del manual del usuario y agregue una mas
[z.facultad/75.42/plaqui.git] / Constructor / src / exclusa.cpp
1 #include "exclusa.h"
2 #include "exclusaptywnd.h"
3 #include "not.h"
4
5 Exclusa::Exclusa(int orientacion)
6 {
7         in_x = x + 48;
8         in_y = y;
9         out_x = in_x;
10         out_y = y + 32;
11         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
12         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_s.png");
13         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
14         imgActual = orientacion;
15         switch (imgActual) {
16                 case 1:
17                         image = imageS;
18                         break;
19                 default: 
20                         imgActual = 0;
21                         image = imageN;                 
22         }
23         set_size_request(image->get_width(), image->get_height());
24         
25         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(PACKAGE_DATA_DIR"/plaqui-constructor/dialogs/constructor.glade", "exclusa_pty_wnd");
26         ref->get_widget_derived("exclusa_pty_wnd",exclusa_pty_wnd);
27         exclusa_pty_wnd->exclusa = this;
28         name = "exclusa";
29         
30         Connector temp;
31         temp.id_dest = -1;
32         temp.type = UNDEF;
33         connect_vec.push_back(temp);
34         connect_vec.push_back(temp);
35 }
36
37 Exclusa::~Exclusa()
38 {
39 }
40
41 bool Exclusa::on_button_press_event(GdkEventButton *event)
42 {
43         t_line tmp_line;
44         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
45                 WorkPlace::pointed = ID;
46                 list_pointed->push_back(name);
47                 combo_entry->set_popdown_strings(*list_pointed);
48                 combo_entry->get_entry()->set_text (name);
49                 if (CItem::logic_connect && CItem::gate_id != -1) {
50                         if ( detect_click_position((int)event->x, (int)event->y) == IN && !workplace->get_logic_item(CItem::gate_id)->get_out_logic_connect()) {
51                                 tmp_line.logic_id =  CItem::gate_id;
52                                 workplace->get_logic_item(CItem::gate_id)->set_out_connected(true);
53                                 tmp_line.store_id = ID;
54                                 workplace->lista_lineas_in.push_back(tmp_line);
55                                 workplace->queue_draw();        
56                         } else if (detect_click_position((int)event->x, (int)event->y) == OUT )  { 
57                                 if (dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))) {
58                                         if (!dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))->get_in_logic_connect()) {
59                                                 workplace->get_logic_item(CItem::gate_id)->set_in_connected(true);
60                                                 tmp_line.logic_id =  CItem::gate_id;
61                                                 tmp_line.store_id = ID;
62                                                 workplace->lista_lineas_out.push_back(tmp_line);
63                                                 workplace->queue_draw();
64                                         }
65                                 } else {
66                                         tmp_line.logic_id =  CItem::gate_id;
67                                         tmp_line.store_id = ID;
68                                         workplace->lista_lineas_out.push_back(tmp_line);
69                                         workplace->queue_draw();
70                                 }                                       
71                         }
72                         CItem::gate_id = -1;
73                 }
74                 Glib::ustring text;
75                 if(estado) text = name+"  Estado: ABIERTO";
76                 else text = name+"  Estado: CERRADO";
77                 status_bar->push ( text, 0);            
78         }
79         
80         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
81                 image = null; 
82                 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);
83                 imgActual++;
84                 switch (imgActual) {
85                         case 1:
86                                 image = imageS;
87                                 in_x = x + image->get_width()-5;
88                                 in_y = y + image->get_height()/2-5;
89                                 out_x = x+5;
90                                 out_y = in_y+10;
91                                 break;
92                         default: 
93                                 imgActual = 0;
94                                 image = imageN;                 
95                                 in_x = x + image->get_width()/2;
96                                 in_y = y;
97                                 out_x = in_x;
98                                 out_y = y +image->get_height();
99                 }
100                 set_size_request(image->get_width(),image->get_height());               
101                 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);
102         }
103         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
104                 menu_popup.popup(event->button, event->time);
105                  return true; //It has been handled.
106         }
107         
108         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
109                 exclusa_pty_wnd->txt_exclusa_name->set_text( name);
110                 if (estado) exclusa_pty_wnd->rd_btn_open->set_active(true);
111                 exclusa_pty_wnd->show();
112         }
113         workplace->queue_draw();
114         return true;
115 }
116
117 void Exclusa::set_estado(bool _state)
118 {
119         estado = _state;
120 }
121
122 bool Exclusa::get_estado()
123 {
124         return estado;
125 }
126
127 void Exclusa::on_menu_popup_rotar()
128 {
129         GdkEventButton event; 
130                 event.type = GDK_BUTTON_PRESS;
131                 event.button = 2;
132         Exclusa::on_button_press_event(&event);
133 }
134
135 void Exclusa::on_menu_popup_propiedades()
136 {
137         GdkEventButton event;
138         event.type = GDK_2BUTTON_PRESS;
139         event.button = 1;
140         Exclusa::on_button_press_event(&event);
141 }
142
143 void Exclusa::save(FILE *archivo)
144 {
145         char c_id[50], c_est[50], c_x[50], c_y[50], c_img[50];
146         int est;
147         Glib::ustring  con0, con1;
148         if (connect_vec[0].type == IN)
149                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
150         else
151                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
152         if (connect_vec[1].type == IN) 
153                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
154         else
155                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
156         sprintf(c_x,"\t\t<x>%d</x>\n",x);
157         sprintf(c_y,"\t\t<y>%d</y>\n",y);
158         sprintf(c_id,"%d",ID);
159         if (estado) est = 1;
160         else est = 0;
161         sprintf(c_est,"\t\t<estado>%d</estado>\n", est);
162         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
163         Glib::ustring dato;
164         dato = "\t<exclusa nombre=\""+name+"\" id=\"";
165         dato += c_id;
166         dato += "\">\n";
167         dato += "\t\t<conector>\n"+con0 + con1+"\t\t</conector>\n";
168         dato += c_img;
169         dato += c_est;
170         dato += c_x; 
171         dato += c_y;
172         dato += "\t</exclusa>\n";
173         fprintf(archivo,dato.c_str());  
174 }
175
176 bool Exclusa::check_connection()
177 {
178         ConnectorType aux, aux1, aux2;
179         CItem *_item0, *_item1;
180         switch (get_img_actual()) {
181                 case 0:
182                         aux1 = is_other_connection_area( get_position_x()-5, get_position_y()+16, &_item0); //izquierda
183                         aux2 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+16,& _item1); //derecha
184                 break;          
185                 case 1:
186                         aux1 =  is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item0); // arriba
187                         aux2 = is_other_connection_area( get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item1); //abajo
188         }
189         if ( aux1 != UNDEF && aux2 != UNDEF ) {
190                 if (!is_connected) {
191                         connect_vec[0].type = aux2; 
192                         connect_vec[1].type = aux1;
193                 }
194                 connect_vec[0].id_dest = _item0->get_id();
195                 connect_vec[1].id_dest = _item1->get_id();
196                 return ( is_connected = (connect_vec[0].type != connect_vec[1].type) );
197         }
198         return is_connected = false;
199 }
200
201 ConnectorType Exclusa::get_connector_type(int _a, int _b)
202 {
203         CItem *_item;
204         switch (imgActual) {
205                 case 1:
206                         if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > y ) ){  //arriba
207                                 if ( is_connected ) return connect_vec[0].type;                 
208                                 connect_vec[1].type = is_other_connection_area(get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item); //pregunto que hay abajo
209                                 switch ( connect_vec[1].type ) {
210                                         case IN: 
211                                         connect_vec[1].type = OUT;
212                                         connect_vec[0].type = IN;               
213                                         break;
214                                         case OUT:
215                                         connect_vec[1].type = IN;
216                                         connect_vec[0].type = OUT;              
217                                         break;
218                                         case UNDEF:
219                                         return UNDEF;
220                                 }
221                                 is_connected = true;
222                                 return connect_vec[0].type;
223                         }
224                         
225                         if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 )) {//abajo
226                                 if ( is_connected ) return connect_vec[1].type;                 
227                                 connect_vec[0].type = is_other_connection_area( get_position_x()+16, get_position_y()-5,& _item);//pregunto que hay arriba.
228                                 switch ( connect_vec[0].type ) {
229                                         case IN: 
230                                         connect_vec[0].type = OUT;
231                                         connect_vec[1].type = IN;               
232                                         break;
233                                         case OUT:
234                                         connect_vec[0].type = IN;
235                                         connect_vec[1].type = OUT;              
236                                         break;
237                                         case UNDEF:
238                                         return UNDEF;
239                                 }
240                                 is_connected = true;
241                                 return connect_vec[1].type;
242                         }
243                 break;
244                 case 0: 
245                         if ((_a <= x+10 )&&(_a > x) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )){ //izquierda
246                                 if ( is_connected ) return connect_vec[0].type;                 
247                                 connect_vec[1].type = is_other_connection_area(get_position_x()+get_image()->get_width()+5, get_position_y()+16, &_item);//pregunto por la derecha
248                                 switch ( connect_vec[1].type ) {
249                                         case IN: 
250                                         connect_vec[1].type = OUT;
251                                         connect_vec[0].type = IN;               
252                                         break;
253                                         case OUT:
254                                         connect_vec[1].type = IN;
255                                         connect_vec[0].type = OUT;              
256                                         break;
257                                         case UNDEF:
258                                         return UNDEF;
259                                 }
260                                 is_connected = true;
261                                 return connect_vec[0].type;
262                         }
263                   if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) {//derecha
264                                 if ( is_connected ) return connect_vec[1].type;                 
265                                 connect_vec[0].type =   is_other_connection_area(get_position_x()-5, get_position_y()+16, &_item); //pregunto por la izquierda. 
266                                 switch ( connect_vec[0].type ) {
267                                         case IN: 
268                                         connect_vec[0].type = OUT;
269                                         connect_vec[1].type = IN;               
270                                         break;
271                                         case OUT:
272                                         connect_vec[0].type = IN;
273                                         connect_vec[1].type = OUT;              
274                                         break;
275                                         case UNDEF:
276                                         return UNDEF;
277                                 }
278                                 is_connected = true;
279                                 return connect_vec[1].type;
280                         }
281                 }
282         return UNDEF;
283 }
284         
285 void Exclusa::set_default_connector()
286 {
287         connect_vec[0].type = UNDEF;
288         connect_vec[1].type = UNDEF;
289 }
290
291 void Exclusa::get_in_logic_connect_position(int& _a, int& _b)
292 {
293         _a = in_x;
294         _b = in_y;
295 }
296
297 void Exclusa::get_out_logic_connect_position(int& _a, int& _b)
298 {
299         _a = out_x;
300         _b = out_y;
301 }
302         
303 ConnectorType Exclusa::detect_click_position(int _a, int _b)
304 {
305         switch (imgActual) {
306                 case 0:
307                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=16)&&(_b >= 0) )
308                                 return IN;
309                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=32)&&(_b > 16) )
310                                 return OUT;
311                         break;
312                 case 1:
313                         if ( (_a <=32)&&(_a> 16)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
314                                 return IN;
315                         if ( (_a <= 16)&&(_a>=0)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
316                                 return OUT;
317                 }
318                 return UNDEF;
319 }
320
321 void Exclusa::update_logic_position()
322 {
323         switch (imgActual) {
324                 case 1:
325                         in_x = x + image->get_width()-5;
326                         in_y = y + image->get_height()/2-5;
327                         out_x = x+5;
328                         out_y = in_y+10;
329                         break;
330                 case 0: 
331                         in_x = x + image->get_width()/2;
332                         in_y = y;
333                         out_x = in_x;
334                         out_y = y +image->get_height();
335         }
336 }