]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/and.cpp
se arregla el bug de la NOT
[z.facultad/75.42/plaqui.git] / Constructor / src / and.cpp
1 #include "and.h"
2
3 And::And(int orientacion)
4 {
5         is_logic = true;
6         out_connected = false;
7         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_n.png");
8         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_s.png");
9         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_e.png");
10         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_o.png");
11         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
12         imgActual = orientacion;
13         switch (imgActual) {
14                 case 1:
15                         image = imageS;
16                         in_x = x +16;
17                         in_y = y;
18                         out_x = x+16;
19                         out_y = y+32;
20                         break;
21                 case 2:
22                         image = imageO;
23                         in_x = x+32;
24                         in_y = y+16;
25                         out_x = x;
26                         out_y = y+16;
27                         break;
28                 case 3:
29                         image = imageN;
30                         in_x = x+16;
31                         in_y = y+32;
32                         out_x = x+16;
33                         out_y = y;
34                         break;
35                 default: 
36                         imgActual = 0;
37                         image = imageE;                 
38                         in_x = x;
39                         in_y = y+16;
40                         out_x = x+32;
41                         out_y = y+16;
42         }
43         set_size_request(image->get_width(), image->get_height());
44         name = "and";
45         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
46 }
47
48 And::~And()
49 {
50 }
51
52 bool And::on_button_press_event(GdkEventButton *event)
53 {
54         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
55                 WorkPlace::pointed = ID;
56                 t_line tmp_line;
57                 std::cout << CItem::gate_id << std::endl;
58                 if (CItem::logic_connect) {
59                         if (CItem::gate_id != -1 ) {
60                                 if ( detect_click_position((int)event->x, (int)event->y ) == IN && CItem::gate_id != ID && !workplace->get_logic_item(CItem::gate_id)->get_out_logic_connect()) {
61                                         tmp_line.logic_id =  CItem::gate_id;
62                                         workplace->get_logic_item(CItem::gate_id)->set_out_connected(true);
63                                         tmp_line.store_id = ID;
64                                         workplace->lista_lineas_logic.push_back(tmp_line);
65                                         workplace->queue_draw();
66                                         CItem::gate_id = -1;
67                                 }
68                         } else {
69                                 CItem::gate_id = ID;
70                         }
71                 }
72                 if ( CItem::logic_connect && detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected) {
73                         CItem::gate_id = ID;
74                 }
75                 list_pointed->push_back(name);
76                 combo_entry->set_popdown_strings(*list_pointed);
77                 combo_entry->get_entry()->set_text (name);
78                 workplace->queue_draw();
79                 char f[20];
80                 Glib::ustring text;
81                 sprintf(f,"  Conector OUT = %d",out_connected);
82                 text = name+f;
83                 status_bar->push ( text, 0);
84         }
85         
86         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
87                 image = null;   
88                 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);
89                 imgActual++;
90                 switch (imgActual) {
91                         case 1:
92                                 image = imageS;
93                                 in_x = x +16;
94                                 in_y = y;
95                                 out_x = x+16;
96                                 out_y = y+32;
97                                 break;
98                         case 2:
99                                 image = imageO;
100                                 in_x = x+32;
101                                 in_y = y+16;
102                                 out_x = x;
103                                 out_y = y+16;
104                                 break;
105                         case 3:
106                                 image = imageN;
107                                 in_x = x+16;
108                                 in_y = y+32;
109                                 out_x = x+16;
110                                 out_y = y;
111                                 break;
112                         default: 
113                                 imgActual = 0;
114                                 image = imageE;                 
115                                 in_x = x;
116                                 in_y = y+16;
117                                 out_x = x+32;
118                                 out_y = y+16;
119                 }
120                 set_size_request(image->get_width(),image->get_height());
121                 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);
122         }
123         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
124                 menu_popup.popup(event->button, event->time);
125                 return true; //It has been handled.
126         }
127         workplace->queue_draw();
128         return true;
129 }
130 void And::on_menu_popup_rotar()
131 {
132         GdkEventButton event; 
133         event.type = GDK_BUTTON_PRESS;
134         event.button = 2;
135         And::on_button_press_event(&event);
136 }
137
138 void And::save(FILE *archivo)
139 {
140         vec_connector.clear();
141         check_connection();
142         Glib::ustring dato;
143         char c_img[50], c_x[50], c_y[50], c_id[50];
144         sprintf(c_id,"%d",ID);
145         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
146         sprintf(c_x,"\t\t<x>%d</x>\n",x);
147         sprintf(c_y,"\t\t<y>%d</y>\n",y);
148         dato = "\t<and nombre=\""+name+"\" id=\"";
149         dato += c_id;
150         dato += "\">\n";
151         dato += c_img;
152         dato += c_x;
153         dato += c_y;
154         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
155                 if ( vec_connector[i].type == IN ) {
156                         dato += "\t\t<entrada>";
157                         dato += vec_connector[i].name_dest + "</entrada>\n";
158                 } else {
159                         dato += "\t\t<salida>";
160                         dato += vec_connector[i].name_dest + "</salida>\n";
161                 }       
162         }       
163         dato += "\t</and>\n";
164         fprintf(archivo, dato.c_str() );
165 }
166
167 bool And::check_connection()
168 {
169         t_logic_connector temp;
170         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
171         while ( i != workplace->lista_lineas_in.end() ) {
172                 if ( (*i).logic_id== ID ) {
173                         temp.type = OUT;
174                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
175                         vec_connector.push_back(temp);
176                 }
177                 i++;
178         }
179
180         i = workplace->lista_lineas_out.begin();
181         while ( i != workplace->lista_lineas_out.end() ) {
182                 if ( (*i).logic_id== ID ) {
183                         temp.type = IN;
184                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
185                         vec_connector.push_back(temp);
186                 }
187                 i++;
188         }
189
190         i = workplace->lista_lineas_logic.begin();
191         while ( i != workplace->lista_lineas_logic.end() ) {
192                 if ( (*i).logic_id == ID ) {
193                         temp.type = OUT;
194                         temp.name_dest = workplace->get_logic_item((*i).store_id)->get_name();
195                         vec_connector.push_back(temp);
196                 }
197                 i++;
198         }
199
200         i = workplace->lista_lineas_logic.begin();
201         while ( i != workplace->lista_lineas_logic.end() ) {
202                 if ( (*i).store_id == ID ) {
203                         temp.type = IN;
204                         temp.name_dest = workplace->get_logic_item((*i).logic_id)->get_name();
205                         vec_connector.push_back(temp);
206                 }
207                 i++;
208         }
209
210         return true; //out_connected;
211 }
212
213 ConnectorType And::detect_click_position(int _a, int _b)
214 {
215         switch (imgActual) {
216                 case 0: 
217                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
218                                 return OUT;
219                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
220                                 return IN;
221                         break;  
222                 case 1: 
223                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
224                                 return OUT;
225                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
226                                 return IN;
227                         break;
228                 case 2: 
229                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
230                                 return OUT;
231                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
232                                 return IN;
233                         break;
234                 case 3: 
235                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
236                                 return OUT;
237                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
238                                 return IN;
239         }
240         return UNDEF;
241 }
242
243 void And::update_logic_position() 
244 {
245         switch (imgActual) {
246                 case 0: 
247                         in_x = x;
248                         in_y = y+16;
249                         out_x = x+32;
250                         out_y = y+16;
251                         break;
252                 case 1:
253                         in_x = x+16;
254                         in_y = y;
255                         out_x = x+16;
256                         out_y = y+32;
257                         break;
258                 case 2:
259                         in_x = x+32;
260                         in_y = y+16;
261                         out_x = x;
262                         out_y = y+16;
263                         break;
264                 case 3:
265                         in_x = x+16;
266                         in_y = y+32;
267                         out_x = x+16;
268                         out_y = y;
269         }
270 }
271
272 void And::get_in_logic_connect_position(int& _a, int& _b)
273 {
274         _a = in_x;
275         _b = in_y;
276 }
277
278 void And::get_out_logic_connect_position(int& _a, int& _b)
279 {
280         _a =out_x;
281         _b =out_y;
282 }
283
284 void And::set_out_connected(bool _o)
285 {
286         out_connected = _o;
287 }
288
289 bool And::get_out_logic_connect()
290 {
291         return out_connected;
292 }
293
294 void And::draw_connectors()
295 {
296         Glib::RefPtr<Gdk::Window> window = get_window();
297         int a,b,c,d;
298         switch (imgActual) {
299                 case 0: 
300                         a = in_x-x; 
301                         b = in_y-y-5;
302                         c = out_x-x-10;
303                         d = out_y-y-5;
304                 break;
305                 case 1: 
306                         a = in_x-x-5;
307                         b = in_y-y;
308                         c = out_x-x-5;
309                         d = out_y-y-10;
310                 break;
311                 case 2:
312                         a = in_x-x-10;
313                         b = in_y-y-5;
314                         c = out_x-x;
315                         d = out_y-y-5;
316                 break;
317                 case 3:
318                         a = in_x-x-5;
319                         b = in_y-y-10;
320                         c = out_x-x-5;
321                         d = out_y-y;
322         }
323         gc->set_foreground(red);
324         gc->set_background(red);
325         window->draw_rectangle(gc, 1, a, b, 10, 10);
326         gc->set_foreground(blue);
327         gc->set_background(blue);
328         window->draw_rectangle(gc, 1, c, d, 10, 10);
329         queue_draw();
330 }