]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/splitter.cpp
* Se mejora la deteccion de conexiones en el constructor
[z.facultad/75.42/plaqui.git] / Constructor / src / splitter.cpp
1 #include "splitter.h"
2
3 Splitter::Splitter(int orientacion)
4 {
5         in_x = -1;
6         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_n.png");
7         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_s.png");
8         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_e.png");
9         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/codo_o.png");
10         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
11         imgActual = orientacion;
12         switch (imgActual) {
13                 case 1:
14                         image = imageN;
15                         break;
16                 case 2:
17                         image = imageE;
18                         break;
19                 case 3:
20                         image = imageS;
21                         break;
22                 default: 
23                         imgActual = 0;
24                         image = imageO;                 
25         }
26         set_size_request(image->get_width(), image->get_height());
27         property_wnd->set_title("Propiedades del Codo");        
28         name = "codo";
29         Connector temp;
30         temp.id_dest = -1;
31         temp.type = UNDEF;
32         connect_vec.push_back(temp);
33         connect_vec.push_back(temp);
34 }
35
36 Splitter::~Splitter()
37 {
38 }
39
40 bool Splitter::on_button_press_event(GdkEventButton *event)
41 {
42         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
43                 list_pointed->push_back(name);
44                 combo_entry->set_popdown_strings(*list_pointed);
45                 combo_entry->get_entry()->set_text (name);
46                 workplace->queue_draw();
47                 WorkPlace::pointed = ID;
48         }
49         
50         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
51                 image = null;   
52                 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);
53                 imgActual++;
54                 switch (imgActual) {
55                         case 1:
56                                 image = imageN;
57                                 break;
58                         case 2:
59                                 image = imageE;
60                                 break;
61                         case 3:
62                                 image = imageS;
63                                 break;
64                         default: 
65                                 imgActual = 0;
66                                 image = imageO;                 
67                 }
68                 set_size_request(image->get_width(),image->get_height());
69                 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);
70         }
71         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
72                 menu_popup.popup(event->button, event->time);
73                  return true; //It has been handled.
74         }
75         
76         if ((event->type == GDK_2BUTTON_PRESS) && ( event->button == 1)){
77                 property_wnd->spin_caudal->set_value( caudal_max );
78                 property_wnd->txt_item_name->set_text( name );
79                 property_wnd->show();
80         }
81         return true;
82 }
83 void Splitter::on_menu_popup_rotar()
84 {
85         GdkEventButton event; 
86         event.type = GDK_BUTTON_PRESS;
87         event.button = 2;
88         Splitter::on_button_press_event(&event);
89 }
90
91 void Splitter::on_menu_popup_propiedades()
92 {
93         GdkEventButton event;
94         event.type = GDK_2BUTTON_PRESS;
95         event.button = 1;
96         Splitter::on_button_press_event(&event);
97 }
98
99 void Splitter::save(FILE *archivo)
100 {
101         char c_id[50], c_caudal[50], c_x[50], c_y[50], c_img[50];
102         Glib::ustring  con0, con1;
103         if (connect_vec[0].type == IN)
104                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
105         else
106                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
107         if (connect_vec[1].type == IN) 
108                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
109         else
110                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
111         sprintf(c_x,"\t\t<x>%d</x>\n",x);
112         sprintf(c_y,"\t\t<y>%d</y>\n",y);
113         sprintf(c_id,"%d",ID);
114         sprintf(c_caudal,"\t\t<caudal>%.2f</caudal>\n", caudal_max);
115         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
116         Glib::ustring dato;
117         dato = "\t<codo nombre=\""+name+"\" id=\"";
118         dato += c_id;
119         dato += "\">\n";
120         dato += c_caudal;
121         dato += "\t\t<conector>\n"+con0 + con1+"\t\t</conector>\n";
122         dato += c_img;
123         dato += c_x; 
124         dato += c_y;
125         dato += "\t</codo>\n";
126         fprintf(archivo,dato.c_str());  
127 }
128
129 bool Splitter::check_connection()
130 {
131         ConnectorType aux, aux1, aux2;
132         CItem *_item0, *_item1;
133         switch (get_img_actual()) {
134                         case 0:
135                                 aux1 = is_other_connection_area( get_position_x()-5, get_position_y()+16, &_item0); //arriba-izquierda h
136                                 aux2 = is_other_connection_area( get_position_x()+get_image()->get_width()-16, +get_position_y()+get_image()->get_height() + 5, &_item1);//abajo-derecha v
137                                 break;  
138                         case 1:
139                                 aux1 = is_other_connection_area( get_position_x()+get_image()->get_width() - 16, get_position_y() -5,& _item0);//arriba-derecha v
140                                 aux2 = is_other_connection_area( get_position_x()-5, get_position_y()+get_image()->get_height()-16, &_item1);//abajo-izquierda h
141                                 break;
142                         case 2:
143                                 aux2 = is_other_connection_area( get_position_x() + 16, get_position_y() - 5, &_item1);//arriba-izquierda v
144                                 aux1 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+get_image()->get_height() - 16, &_item0); //abajo-derecha h
145                                 break;
146                         case 3:
147                                 aux2 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y() +16,& _item1);//arriba-derecha h
148                                 aux1 = is_other_connection_area( get_position_x() + 16, get_position_y()+get_image()->get_height() + 5, &_item0); //abajo-izquierda v
149         }
150         if ((aux2 != UNDEF) && (aux1 != UNDEF)) {
151                 if (!is_connected) {
152                         connect_vec[0].type = aux2;
153                         connect_vec[1].type = aux1;
154                 }
155                 connect_vec[0].id_dest = _item0->get_id();
156                 connect_vec[1].id_dest = _item1->get_id();
157                 return ( is_connected = (connect_vec[0].type != connect_vec[1].type) );
158         }
159         return is_connected = false;
160 }
161
162 ConnectorType Splitter::get_connector_type(int _a, int _b)
163 {
164         CItem *_item;
165         switch (imgActual) {
166                 case 0: 
167                         if ( (_a <= x+10)&&(_a > x) && (_b <=y+22)&&(_b>=y+10) ) {// arriba - izquierda h
168                                 if ( is_connected ) return connect_vec[0].type;
169                                 connect_vec[1].type = is_other_connection_area (get_position_x()+get_image()->get_width()-16, +get_position_y()+get_image()->get_height() + 5, &_item);
170                                 switch ( connect_vec[1].type ) {
171                                         case IN: 
172                                                 connect_vec[0].type = IN;
173                                                 connect_vec[1].type = OUT;
174                                         break;
175                                         case OUT:
176                                                 connect_vec[0].type = OUT;
177                                                 connect_vec[1].type = IN;
178                                         break;
179                                         case UNDEF:
180                                                 return UNDEF;
181                                 }
182                                 is_connected = true;
183                                 return connect_vec[0].type;
184                         }
185                         if ( (_a <= x+image->get_width()-10)&&(_a>=x+image->get_width()-22)&&(_b<=y+image->get_height()-1)&&(_b>=y+image->get_height()-10) ) {//abajo-derecha v
186                                 if ( is_connected ) return connect_vec[1].type;
187                                 connect_vec[0].type = is_other_connection_area( get_position_x()-5, get_position_y()+16,& _item);
188                                 switch ( connect_vec[0].type ) {
189                                         case IN: 
190                                                 connect_vec[0].type = OUT;
191                                                 connect_vec[1].type = IN;
192                                                 break;
193                                         case OUT:
194                                                 connect_vec[0].type = IN;
195                                                 connect_vec[1].type = OUT;
196                                                 break;
197                                         case UNDEF:
198                                                 return UNDEF;
199                                 }
200                                 is_connected = true;
201                                 return connect_vec[1].type;
202                         }
203                 break;
204                 case 1:
205                         if ( (_a <= x+image->get_width()-10)&&(_a >= x+image->get_width()-22) && (_b <=y+10)&&(_b > y) ) {//arriba-derecha v
206                                 if (is_connected) return connect_vec[0].type;
207                                 connect_vec[1].type = is_other_connection_area( get_position_x()-5, get_position_y()+get_image()->get_height()-16, &_item);
208                                 switch ( connect_vec[1].type ) {
209                                         case IN: 
210                                                 connect_vec[0].type = IN;
211                                                 connect_vec[1].type = OUT;
212                                         break;
213                                         case OUT:
214                                                 connect_vec[0].type = OUT;
215                                                 connect_vec[1].type = IN;
216                                         break;
217                                         case UNDEF:
218                                                 return UNDEF;
219                                 }
220                                 is_connected = true;
221                                 return connect_vec[0].type;
222                         }
223                                         
224                         if ( (_a <= x+10)&&(_a > x)&&(_b<=y+image->get_height()-10)&&(_b>=y+image->get_height()-22) ) {// abajo-izquierda h
225                                 if ( is_connected ) return connect_vec[1].type;
226                                 connect_vec[0].type = is_other_connection_area( get_position_x()+get_image()->get_width() - 16, get_position_y() -5, &_item);
227                                 switch ( connect_vec[0].type ) {
228                                         case IN: 
229                                                 connect_vec[0].type = OUT;
230                                                 connect_vec[1].type = IN;
231                                                 break;
232                                         case OUT:
233                                                 connect_vec[0].type = IN;
234                                                 connect_vec[1].type = OUT;
235                                                 break;
236                                         case UNDEF:
237                                                 return UNDEF;
238                                 }
239                                 is_connected = true;
240                                 return connect_vec[1].type;
241                         }
242                 break;
243                 case 2:
244                         if ( (_a <= x+22)&&(_a >= x+10) && (_b <=y+10)&&(_b > y) ) {//arriba-izquierda v
245                                 if ( is_connected ) return connect_vec[1].type;
246                                 connect_vec[0].type = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+get_image()->get_height() - 16, &_item); 
247                                 switch ( connect_vec[0].type ) {
248                                         case IN: 
249                                                 connect_vec[0].type = OUT;
250                                                 connect_vec[1].type = IN;
251                                                 break;
252                                         case OUT:
253                                                 connect_vec[0].type = IN;
254                                                 connect_vec[1].type = OUT;
255                                                 break;
256                                         case UNDEF:
257                                                 return UNDEF;
258                                 }
259                                 is_connected = true;
260                                 return connect_vec[1].type;
261                         }
262
263                         if  ( (_a <= x+image->get_width()-1)&&(_a>=x+image->get_width()-10)&&(_b<=y+image->get_height()-10)&&(_b>=y+image->get_height()-22) ) {// abajo-derecha h
264                                 if (is_connected ) return connect_vec[0].type;
265                                 connect_vec[1].type = is_other_connection_area( get_position_x() + 16, get_position_y() - 5,& _item);
266                                 switch ( connect_vec[1].type ) {
267                                         case IN: 
268                                                 connect_vec[0].type = IN;
269                                                 connect_vec[1].type = OUT;
270                                         break;
271                                         case OUT:
272                                                 connect_vec[0].type = OUT;
273                                                 connect_vec[1].type = IN;
274                                         break;
275                                         case UNDEF:
276                                                 return UNDEF;
277                                 }
278                                 is_connected = true;
279                                 return connect_vec[0].type;
280                         }
281                         break;
282                 case 3:
283                         if ( (_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <=y+22)&&(_b>=y+10) ) {//arriba-derecha h
284                                 if ( is_connected ) return connect_vec[1].type;
285                                 connect_vec[0].type = is_other_connection_area( get_position_x() + 16, get_position_y()+get_image()->get_height() + 5, &_item);
286                                 switch ( connect_vec[0].type ) {
287                                         case IN: 
288                                                 connect_vec[0].type = OUT;
289                                                 connect_vec[1].type = IN;
290                                                 break;
291                                         case OUT:
292                                                 connect_vec[0].type = IN;
293                                                 connect_vec[1].type = OUT;
294                                                 break;
295                                         case UNDEF:
296                                                 return UNDEF;
297                                 }
298                                 is_connected = true;
299                                 return connect_vec[1].type;
300                         }
301                                 
302                         if ( (_a <= x+22)&&(_a>=x+10)&&(_b<=y+image->get_height()-1)&&(_b>=y+image->get_height()-10) ) {// abajo-izquierda v
303                                 if ( is_connected ) return connect_vec[0].type;
304                                 connect_vec[1].type = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y() +16,& _item);
305                                 switch ( connect_vec[1].type ) {
306                                         case IN: 
307                                                 connect_vec[0].type = IN;
308                                                 connect_vec[1].type = OUT;
309                                         break;
310                                         case OUT:
311                                                 connect_vec[0].type = OUT;
312                                                 connect_vec[1].type = IN;
313                                         break;
314                                         case UNDEF:
315                                                 return UNDEF;
316                                 }
317                                 is_connected = true;
318                                 return connect_vec[0].type;
319                         }
320         }
321         return UNDEF;
322 }       
323
324 void Splitter::set_default_connector()
325 {
326         connect_vec[0].type = UNDEF;
327         connect_vec[1].type = UNDEF;
328 }