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