]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/or.cpp
* Ahora el modelo carga las compuertas logicas y crea las lineas necesarias
[z.facultad/75.42/plaqui.git] / Constructor / src / or.cpp
1 #include "or.h"
2
3 Or::Or(int orientacion)
4 {
5         is_logic = true;
6         out_connected = false;
7         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_n.png");
8         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_s.png");
9         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
10         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_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 = "or";
45         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
46 }
47
48 Or::~Or()
49 {
50 }
51
52 bool Or::on_button_press_event(GdkEventButton *event)
53 {
54         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
55                 if ( CItem::logic_connect ) {
56                         if ( detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected) {
57                                 CItem::gate_id = ID;
58                                 WorkPlace::pointed = ID;
59                         } 
60                         if ( detect_click_position((int)event->x, (int)event->y ) == IN ) {
61                                 CItem::gate_id = ID;
62                                 WorkPlace::pointed = ID;
63                         }
64                 }
65                 list_pointed->push_back(name);
66                 combo_entry->set_popdown_strings(*list_pointed);
67                 combo_entry->get_entry()->set_text (name);
68         }
69         
70         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
71                 image = null;   
72                 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);
73                 imgActual++;
74                 switch (imgActual) {
75                         case 1:
76                                 image = imageS;
77                                 in_x = x +16;
78                                 in_y = y;
79                                 out_x = x+16;
80                                 out_y = y+32;
81                                 break;
82                         case 2:
83                                 image = imageO;
84                                 in_x = x+32;
85                                 in_y = y+16;
86                                 out_x = x;
87                                 out_y = y+16;
88                                 break;
89                         case 3:
90                                 image = imageN;
91                                 in_x = x+16;
92                                 in_y = y+32;
93                                 out_x = x+16;
94                                 out_y = y;
95                                 break;
96                         default: 
97                                 imgActual = 0;
98                                 image = imageE;                 
99                                 in_x = x;
100                                 in_y = y+16;
101                                 out_x = x+32;
102                                 out_y = y+16;
103                 }
104                 set_size_request(image->get_width(),image->get_height());
105                 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);
106         }
107         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
108                 menu_popup.popup(event->button, event->time);
109                  return true; //It has been handled.
110         }
111         workplace->queue_draw();
112         return true;
113 }
114
115 void Or::on_menu_popup_rotar()
116 {
117         GdkEventButton event; 
118         event.type = GDK_BUTTON_PRESS;
119         event.button = 2;
120         Or::on_button_press_event(&event);
121 }
122
123 void Or::save(FILE *archivo)
124 {
125         vec_connector.clear();
126         check_connection();
127         Glib::ustring dato;
128         char c_img[50], c_x[50], c_y[50], c_id[50];
129         sprintf(c_id,"%d",ID);
130         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
131         sprintf(c_x,"\t\t<x>%d</x>\n",x);
132         sprintf(c_y,"\t\t<y>%d</y>\n",y);
133         dato = "\t<or nombre=\""+name+"\" id=\"";
134         dato += c_id;
135         dato += "\">\n";
136         dato += c_img;
137         dato += c_x;
138         dato += c_y;
139         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
140                 if ( vec_connector[i].type == IN ) {
141                         dato += "\t\t<entrada>";
142                         dato += vec_connector[i].name_dest + "</entrada>\n";
143                 } else {
144                         dato += "\t\t<salida>";
145                         dato += vec_connector[i].name_dest + "</salida>\n";
146                 }       
147         }       
148         dato += "\t</or>\n";
149         fprintf(archivo, dato.c_str() );
150 }
151
152 bool Or::check_connection()
153 {
154         int cant_in = 0, cant_out =0;
155         t_logic_connector temp;
156         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
157         while ( i != workplace->lista_lineas_in.end() ) {
158                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
159                         temp.type = OUT;
160                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
161                         vec_connector.push_back(temp);
162                         cant_out++;
163                 }
164                 i++;
165         }
166
167         i = workplace->lista_lineas_out.begin();
168         while ( i != workplace->lista_lineas_out.end() ) {
169                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
170                         temp.type = IN;
171                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
172                         vec_connector.push_back(temp);
173                         cant_in++;
174                 }
175                 i++;
176         }
177
178         
179         return true;// out_connected;
180 }
181
182 ConnectorType Or::detect_click_position(int _a, int _b)
183 {
184         switch (imgActual) {
185                 case 0: 
186                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
187                                 return OUT;
188                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
189                                 return IN;
190                         break;  
191                 case 1: 
192                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
193                                 return OUT;
194                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
195                                 return IN;
196                         break;
197                 case 2: 
198                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
199                                 return OUT;
200                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
201                                 return IN;
202                         break;
203                 case 3: 
204                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
205                                 return OUT;
206                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
207                                 return IN;
208         }
209         return UNDEF;
210 }
211         
212 void Or::update_logic_position() 
213 {
214         switch (imgActual) {
215                 case 0: 
216                         in_x = x;
217                         in_y = y+16;
218                         out_x = x+32;
219                         out_y = y+16;
220                         break;
221                 case 1:
222                         in_x = x+16;
223                         in_y = y;
224                         out_x = x+16;
225                         out_y = y+32;
226                         break;
227                 case 2:
228                         in_x = x+32;
229                         in_y = y+16;
230                         out_x = x;
231                         out_y = y+16;
232                         break;
233                 case 3:
234                         in_x = x+16;
235                         in_y = y+32;
236                         out_x = x+16;
237                         out_y = y;
238         }
239 }
240
241 void Or::get_in_logic_connect_position(int& _a, int& _b)
242 {
243         _a = in_x;
244         _b = in_y;
245 }
246
247 void Or::get_out_logic_connect_position(int& _a, int& _b)
248 {
249         _a =out_x;
250         _b =out_y;
251 }
252
253 void Or::set_out_connected(bool _o)
254 {
255         out_connected = _o;
256 }