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