]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/or.cpp
* Se agrega carga de colores desde el XML
[z.facultad/75.42/plaqui.git] / Constructor / src / or.cpp
1 #include "or.h"
2 #include "cistern.h"
3
4 Or::Or(int orientacion)
5 {
6         is_logic = true;
7         out_connected = false;
8         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_n.png");
9         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_s.png");
10         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
11         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_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 = "or";
46         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
47 }
48
49 Or::~Or()
50 {
51 }
52
53 bool Or::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 && !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                                 }
67                         } else 
68                                 CItem::gate_id = ID;
69                 }
70                 if ( CItem::logic_connect ) {
71                         if ( detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected) {
72                                 CItem::gate_id = ID;
73                         } 
74                 }
75                 list_pointed->push_back(name);
76                 combo_entry->set_popdown_strings(*list_pointed);
77                 combo_entry->get_entry()->set_text (name);
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
130 void Or::on_menu_popup_rotar()
131 {
132         GdkEventButton event; 
133         event.type = GDK_BUTTON_PRESS;
134         event.button = 2;
135         Or::on_button_press_event(&event);
136 }
137
138 void Or::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<or 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                         if ( vec_connector[i].cistern_connector == 1 ) {
157                                 dato += "\t\t<entrada id = \"superior\">";
158                                 dato += vec_connector[i].name_dest + "</entrada>\n";
159                         }
160                         if ( vec_connector[i].cistern_connector == 0 ) {
161                                 dato += "\t\t<entrada id = \"inferior\">";
162                                 dato += vec_connector[i].name_dest + "</entrada>\n";
163                         }
164                         if ( vec_connector[i].cistern_connector == -1 ) {
165                                 dato += "\t\t<entrada>";
166                                 dato += vec_connector[i].name_dest + "</entrada>\n";
167                         }
168                 } else {
169                         dato += "\t\t<salida>";
170                         dato += vec_connector[i].name_dest + "</salida>\n";
171                 }       
172         }       
173         dato += "\t</or>\n";
174         fprintf(archivo, dato.c_str() );
175 }
176
177 //<entrada><tanque id="superior">tanque1</tanque></entrada>
178
179 bool Or::check_connection()
180 {
181         int cant_in = 0, cant_out =0;
182         t_logic_connector temp;
183         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
184         while ( i != workplace->lista_lineas_in.end() ) {
185                 if ( (*i).logic_id== ID ) {
186                         temp.type = OUT;
187                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
188                         temp.cistern_connector = -1;
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                         std::cout<<"cistern_connector = "<<temp.cistern_connector<<std::endl;
208                         vec_connector.push_back(temp);
209                         cant_in++;
210                 }
211                 i++;
212         }
213
214         i = workplace->lista_lineas_logic.begin();
215         while ( i != workplace->lista_lineas_logic.end() ) {
216                 if ( (*i).logic_id == ID ) {
217                         temp.type = OUT;
218                         temp.name_dest = workplace->get_logic_item((*i).store_id)->get_name();
219                         vec_connector.push_back(temp);
220                         cant_in++;
221                 }
222                 i++;
223         }
224
225         i = workplace->lista_lineas_logic.begin();
226         while ( i != workplace->lista_lineas_logic.end() ) {
227                 if ( (*i).store_id == ID ) {
228                         temp.type = IN;
229                         temp.name_dest = workplace->get_logic_item((*i).logic_id)->get_name();
230                         temp.cistern_connector = -1;
231                         vec_connector.push_back(temp);
232                         cant_out++;
233                 }
234                 i++;
235         }
236         return true;
237 }
238
239 ConnectorType Or::detect_click_position(int _a, int _b)
240 {
241         switch (imgActual) {
242                 case 0: 
243                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
244                                 return OUT;
245                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
246                                 return IN;
247                         break;  
248                 case 1: 
249                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
250                                 return OUT;
251                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
252                                 return IN;
253                         break;
254                 case 2: 
255                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
256                                 return OUT;
257                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
258                                 return IN;
259                         break;
260                 case 3: 
261                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
262                                 return OUT;
263                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
264                                 return IN;
265         }
266         return UNDEF;
267 }
268         
269 void Or::update_logic_position() 
270 {
271         switch (imgActual) {
272                 case 0: 
273                         in_x = x;
274                         in_y = y+16;
275                         out_x = x+32;
276                         out_y = y+16;
277                         break;
278                 case 1:
279                         in_x = x+16;
280                         in_y = y;
281                         out_x = x+16;
282                         out_y = y+32;
283                         break;
284                 case 2:
285                         in_x = x+32;
286                         in_y = y+16;
287                         out_x = x;
288                         out_y = y+16;
289                         break;
290                 case 3:
291                         in_x = x+16;
292                         in_y = y+32;
293                         out_x = x+16;
294                         out_y = y;
295         }
296 }
297
298 void Or::get_in_logic_connect_position(int& _a, int& _b)
299 {
300         _a = in_x;
301         _b = in_y;
302 }
303
304 void Or::get_out_logic_connect_position(int& _a, int& _b)
305 {
306         _a =out_x;
307         _b =out_y;
308 }
309
310 void Or::set_out_connected(bool _o)
311 {
312         out_connected = _o;
313 }
314
315 bool Or::get_out_logic_connect()
316 {
317         return out_connected;
318 }
319
320 void Or::draw_connectors()
321 {
322         Glib::RefPtr<Gdk::Window> window = get_window();
323         int a,b,c,d;
324         switch (imgActual) {
325                 case 0: 
326                         a = in_x-x; 
327                         b = in_y-y-5;
328                         c = out_x-x-10;
329                         d = out_y-y-5;
330                 break;
331                 case 1: 
332                         a = in_x-x-5;
333                         b = in_y-y;
334                         c = out_x-x-5;
335                         d = out_y-y-10;
336                 break;
337                 case 2:
338                         a = in_x-x-10;
339                         b = in_y-y-5;
340                         c = out_x-x;
341                         d = out_y-y-5;
342                 break;
343                 case 3:
344                         a = in_x-x-5;
345                         b = in_y-y-10;
346                         c = out_x-x-5;
347                         d = out_y-y;
348         }
349         gc->set_foreground(red);
350         gc->set_background(red);
351         window->draw_rectangle(gc, 1, a, b, 10, 10);
352         gc->set_foreground(blue);
353         gc->set_background(blue);
354         window->draw_rectangle(gc, 1, c, d, 10, 10);
355         queue_draw();
356 }