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