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