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