]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/union.cpp
61e583816eececb3a070d9127e6b32dc5466afd4
[z.facultad/75.42/plaqui.git] / Constructor / src / union.cpp
1 #include "union.h"
2
3 Union::Union(int orientacion)
4 {
5         in_x = -1;
6         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_n.png");
7         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_s.png");
8         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_e.png");
9         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/y_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 = imageE;
15                         break;
16                 case 2:
17                         image = imageS;
18                         break;
19                 case 3:
20                         image = imageO;
21                         break;
22                 default: 
23                         imgActual = 0;
24                         image = imageN;                 
25         }
26         set_size_request(image->get_width(), image->get_height());
27         property_wnd->set_title("Propiedades del Empalme");
28         property_wnd->rd_btn_division->property_visible() = true;
29         property_wnd->rd_btn_union->property_visible() = true;
30         property_wnd->lb_type->property_visible() = true;
31         name = "union";
32         Connector temp;
33         temp.id_dest = -1;
34         temp.type = IN;
35         connect_vec.push_back(temp); //izquierda
36         connect_vec.push_back(temp); //derecha
37         temp.type = OUT;
38         connect_vec.push_back(temp); //medio
39 }
40
41 Union::~Union()
42 {
43 }
44
45 bool Union::on_button_press_event(GdkEventButton *event)
46 {
47         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
48                 combo_entry->set_text(name);
49                 WorkPlace::pointed = ID;
50         }
51         
52         if ((event->type == GDK_BUTTON_PRESS) && (event->button == 2 )){
53                 image = null;   
54                 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);
55                 imgActual++;
56                 switch (imgActual) {
57                         case 1:
58                                 image = imageE;
59                                 break;
60                         case 2:
61                                 image = imageS;
62                                 break;
63                         case 3:
64                                 image = imageO;
65                                 break;
66                         default: 
67                                 imgActual = 0;
68                                 image = imageN;                 
69                 }
70                 set_size_request(image->get_width(), image->get_height());              
71                 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);
72         }
73         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
74                 menu_popup.popup(event->button, event->time);
75                  return true; //It has been handled.
76         }
77         
78         if ((event->type == GDK_2BUTTON_PRESS) && ( event->button == 1)){
79                 property_wnd->spin_caudal->set_value( caudal_max );
80                 property_wnd->txt_item_name->set_text( name );
81                 if (is_union) property_wnd->rd_btn_union->set_active(true);
82                 property_wnd->show();
83         }
84         return true;
85 }
86
87 void Union::on_menu_popup_rotar()
88 {
89         GdkEventButton event; 
90         event.type = GDK_BUTTON_PRESS;
91         event.button = 2;
92         Union::on_button_press_event(&event);
93 }
94
95 void Union::on_menu_popup_propiedades()
96 {
97         GdkEventButton event; 
98         event.type = GDK_2BUTTON_PRESS;
99         event.button = 1;
100         Union::on_button_press_event(&event);
101 }
102
103 void Union::save(FILE *archivo)
104 {
105         char c_id[50], c_caudal[50], c_x[50], c_y[50], c_img[50];
106         Glib::ustring  con0, con1, con2, c_type;
107         if (is_union){
108                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
109                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
110                 con2 = "\t\t\t<salida>"+get_other_name(connect_vec[2].id_dest)+"</salida>\n";
111                 c_type = "\t\t<tipo>union</tipo>\n";
112         } else {
113                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
114                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
115                 con2 = "\t\t\t<entrada>"+get_other_name(connect_vec[2].id_dest)+"</entrada>\n";         
116                 c_type = "\t\t<tipo>division</tipo>\n";
117         }
118         sprintf(c_x,"\t\t<x>%d</x>\n",x);
119         sprintf(c_y,"\t\t<y>%d</y>\n",y);
120         sprintf(c_id,"%d",ID);
121         sprintf(c_caudal,"\t\t<caudal>%.2f</caudal>\n", caudal_max);
122         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
123         Glib::ustring dato;
124         dato = "\t<empalme nombre=\""+name+"\" id=\"";
125         dato += c_id;
126         dato += "\">\n";
127         dato += c_type;
128         dato += "\t\t<conector>\n"+con0+con1+con2+"\t\t</conector>\n";
129         dato += c_caudal;
130         dato += c_img;
131         dato += c_x; 
132         dato += c_y;
133         dato += "\t</empalme>\n";
134         fprintf(archivo,dato.c_str());  
135 }
136
137 bool Union::check_connection()
138 {
139         CItem * _item0, *_item1, *_item2;
140         ConnectorType temp0, temp1, temp2;
141         switch (get_img_actual()) {
142                         case 0:
143                                 temp0 = is_other_connection_area( get_position_x() - 5, get_position_y() +16, &_item0);
144                                 temp1 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+16, &_item1);
145                                 temp2 = is_other_connection_area( get_position_x()+get_image()->get_width()/2, get_position_y()+get_image()->get_height()+5, &_item2);
146                                 break;
147                         case 1:
148                                 temp2 = is_other_connection_area( get_position_x()-5, get_position_y()+get_image()->get_height()/2, &_item2);
149                                 temp0 = is_other_connection_area( get_position_x()+get_image()->get_width()-16, get_position_y()-5, &_item0);
150                                 temp1 = is_other_connection_area( get_position_x()+get_image()->get_width()-16, get_position_y()+get_image()->get_height()+5,& _item1);
151                                 break;
152                         case 2:
153                                 temp1 = is_other_connection_area( get_position_x()-5, get_position_y()+get_image()->get_height()-16,& _item1);
154                                 temp2 = is_other_connection_area( get_position_x()+get_image()->get_width()/2, get_position_y()-5, &_item2);
155                                 temp0 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+get_image()->get_height()-16, &_item0);
156                                 break;
157                         case 3:
158                                 temp1 = is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item1);
159                                 temp2 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+get_image()->get_height()/2,& _item0);
160                                 temp0 = is_other_connection_area( get_position_x()+16, get_position_y()+get_image()->get_height()+5, &_item1);
161         }
162         if ( is_union ) {
163                 if ( temp0 == OUT && temp1 == OUT && temp2 == IN ) {
164                         connect_vec[0].id_dest = _item0->get_id();
165                         connect_vec[1].id_dest = _item1->get_id();
166                         connect_vec[2].id_dest = _item2->get_id();
167                         return (is_connected = true);
168                 }
169         } else {
170                 if ( temp0 == IN && temp1 == IN && temp2 == OUT ) {
171                         connect_vec[0].id_dest = _item0->get_id();
172                         connect_vec[1].id_dest = _item1->get_id();
173                         connect_vec[2].id_dest = _item2->get_id();
174                         return (is_connected = true);
175                 }
176         }
177         return is_connected;
178 }
179
180 ConnectorType Union::get_connector_type(int _a, int _b)
181 {
182         switch ( imgActual) {
183                 case 0: if ((_a <= x+10) && (_a > x+1) && (_b<= y + 22) && (_b >= y+10))
184                                                 return connect_vec[0].type;
185                                         if ((_a <= x+image->get_width()-1) && (_a >= x+image->get_width()-10)&&(_b <= y+22) && (_b >= y+10))
186                                                 return connect_vec[1].type;
187                                         if ((_a <= x+image->get_width()/2 + 5)&&( _a>= x+image->get_width()/2 - 5) && (_b<=y+image->get_height()-1)&&(_b >= y+image->get_height()-10)) 
188                                                 return connect_vec[2].type;
189                                         break;
190                 case 1: if ((_a <= x+image->get_width()-10) && (_a >= x+image->get_width() -22) && (_b<= y + 10) && (_b > y ))
191                                                 return connect_vec[0].type;
192                                         if ((_a <= x +10) && (_a > x )&&(_b <= y+image->get_height()/2+5 ) && (_b >= y+image->get_height()/2-5))
193                                                 return connect_vec[2].type;
194                                         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))
195                                                 return connect_vec[1].type;
196                                         break;
197                 case 2: if ((_a <= x+10) && (_a > x) && (_b<= y+image->get_height()-10) && (_b >= y+image->get_height()-22))
198                                                 return connect_vec[1].type;
199                                         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))
200                                                 return connect_vec[0].type;
201                                         if ((_a <= x+image->get_width()/2 + 5)&&( _a>= x+image->get_width()/2 - 5) && (_b<=y+10)&&(_b > y))
202                                                 return connect_vec[2].type;
203                                         break;
204                 case 3: if ((_a <= x+22) && (_a >= x+10) && (_b<= y + 10) && (_b > y ))
205                                                 return connect_vec[1].type;
206                                         if ((_a <= x+22) && (_a >= x+10 )&&(_b <= y+image->get_height()-1 ) && (_b >= y+image->get_height()-10))
207                                                 return connect_vec[0].type;
208                                         if ((_a <= x+image->get_width() -1)&&( _a>= x+image->get_width()-10) && (_b<=y+image->get_height()/2+5)&&(_b >= y+image->get_height()/2-5))
209                                                 return connect_vec[2].type;
210         }
211         return UNDEF;
212 }
213
214 void Union::set_default_connector()
215 {
216         if (is_union) {
217                 connect_vec[0].type = IN;
218                 connect_vec[1].type = IN;
219                 connect_vec[2].type = OUT;
220         } else {
221                 connect_vec[0].type = OUT;
222                 connect_vec[1].type = OUT;
223                 connect_vec[2].type = IN;
224         }
225 }