]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/and.cpp
cuando se eliminann items las compuetas ponen sus estados de las salidas o entradas...
[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         int cant_in = 0, cant_out =0;
170         t_logic_connector temp;
171         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
172         while ( i != workplace->lista_lineas_in.end() ) {
173                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
174                         temp.type = OUT;
175                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
176                         vec_connector.push_back(temp);
177                         cant_out++;
178                 }
179                 i++;
180         }
181
182         i = workplace->lista_lineas_out.begin();
183         while ( i != workplace->lista_lineas_out.end() ) {
184                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
185                         temp.type = IN;
186                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
187                         vec_connector.push_back(temp);
188                         cant_in++;
189                 }
190                 i++;
191         }
192         return true; //out_connected;
193 }
194
195 ConnectorType And::detect_click_position(int _a, int _b)
196 {
197         switch (imgActual) {
198                 case 0: 
199                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
200                                 return OUT;
201                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
202                                 return IN;
203                         break;  
204                 case 1: 
205                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
206                                 return OUT;
207                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
208                                 return IN;
209                         break;
210                 case 2: 
211                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
212                                 return OUT;
213                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
214                                 return IN;
215                         break;
216                 case 3: 
217                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
218                                 return OUT;
219                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
220                                 return IN;
221         }
222         return UNDEF;
223 }
224
225 void And::update_logic_position() 
226 {
227         switch (imgActual) {
228                 case 0: 
229                         in_x = x;
230                         in_y = y+16;
231                         out_x = x+32;
232                         out_y = y+16;
233                         break;
234                 case 1:
235                         in_x = x+16;
236                         in_y = y;
237                         out_x = x+16;
238                         out_y = y+32;
239                         break;
240                 case 2:
241                         in_x = x+32;
242                         in_y = y+16;
243                         out_x = x;
244                         out_y = y+16;
245                         break;
246                 case 3:
247                         in_x = x+16;
248                         in_y = y+32;
249                         out_x = x+16;
250                         out_y = y;
251         }
252 }
253
254 void And::get_in_logic_connect_position(int& _a, int& _b)
255 {
256         _a = in_x;
257         _b = in_y;
258 }
259
260 void And::get_out_logic_connect_position(int& _a, int& _b)
261 {
262         _a =out_x;
263         _b =out_y;
264 }
265
266 void And::set_out_connected(bool _o)
267 {
268         out_connected = _o;
269 }
270
271 bool And::get_out_logic_connect()
272 {
273         return out_connected;
274 }
275
276 void And::draw_connectors()
277 {
278         Glib::RefPtr<Gdk::Window> window = get_window();
279         int a,b,c,d;
280         switch (imgActual) {
281                 case 0: 
282                         a = in_x-x; 
283                         b = in_y-y-5;
284                         c = out_x-x-10;
285                         d = out_y-y-5;
286                 break;
287                 case 1: 
288                         a = in_x-x-5;
289                         b = in_y-y;
290                         c = out_x-x-5;
291                         d = out_y-y-10;
292                 break;
293                 case 2:
294                         a = in_x-x-10;
295                         b = in_y-y-5;
296                         c = out_x-x;
297                         d = out_y-y-5;
298                 break;
299                 case 3:
300                         a = in_x-x-5;
301                         b = in_y-y-10;
302                         c = out_x-x-5;
303                         d = out_y-y;
304         }
305         gc->set_foreground(red);
306         gc->set_background(red);
307         window->draw_rectangle(gc, 1, a, b, 10, 10);
308         gc->set_foreground(blue);
309         gc->set_background(blue);
310         window->draw_rectangle(gc, 1, c, d, 10, 10);
311         queue_draw();
312 }