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