]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/cistern.cpp
- Se actualiza la funcion save() de todos los item
[z.facultad/75.42/plaqui.git] / Constructor / cistern.cpp
1 #include "cistern.h"
2 #include "cisternptywnd.h"
3
4 Cistern::Cistern()
5 {
6         imageE = Gdk::Pixbuf::create_from_file("tanque_e.png");
7         imageO = Gdk::Pixbuf::create_from_file("tanque_o.png");
8         null = Gdk::Pixbuf::create_from_file("tanque_null.png");
9         imgActual = 0;
10         image = imageE;
11         set_size_request(image->get_width(), image->get_height());
12
13         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create("constructor.glade", "cistern_pty_wnd");
14         ref->get_widget_derived("cistern_pty_wnd",cistern_pty_wnd);
15         cistern_pty_wnd->cistern = this;
16         cistern_pty_wnd->set_title("Propiedades del Tanque");   
17         name = "tanque";
18         
19         Connector temp;
20         temp.id_dest = -1;
21         temp.type = IN;
22         connect_vec.push_back(temp); // entrada arriba
23         temp.type = OUT;
24         connect_vec.push_back(temp); // salida abajo
25 }
26
27 Cistern::~Cistern()
28 {
29 }
30
31 bool Cistern::on_button_press_event(GdkEventButton *event)
32 {
33         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1))
34                 combo_entry->set_text(name);
35         
36         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
37                 image = null; 
38                 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);
39                 imgActual++;
40                 switch (imgActual) {
41                         case 1:
42                                 image = imageO;
43                                 break;
44                         default: 
45                                 imgActual = 0;
46                                 image = imageE;                 
47                 }
48                 set_size_request(image->get_width(),image->get_height());
49                 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);
50         }
51         
52         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
53                 menu_popup.popup(event->button, event->time);
54                  return true; //It has been handled.
55         }
56         
57         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
58                 cistern_pty_wnd->spin_capacidad->set_value( capacidad );
59                 cistern_pty_wnd->spin_inicial->set_value( contenido_inicial );
60                 cistern_pty_wnd->txt_cistern_name->set_text( name );
61                 cistern_pty_wnd->show();
62         }               
63         return true;
64 }
65
66 void Cistern::on_menu_popup_rotar()
67 {
68         GdkEventButton event; 
69                 event.type = GDK_BUTTON_PRESS;
70                 event.button = 2;
71         Cistern::on_button_press_event(&event);
72 }
73
74 void Cistern::on_menu_popup_propiedades()
75 {
76         GdkEventButton event; 
77                 event.type = GDK_2BUTTON_PRESS;
78                 event.button = 1;
79         Cistern::on_button_press_event(&event);
80 }
81
82 void Cistern::set_capacidad(double _cap)
83 {
84         capacidad = _cap;
85 }
86
87 void Cistern::set_liquid_color(Gdk::Color _color)
88 {
89         liquid_color = _color;
90 }
91
92 void Cistern::set_contenido_inicial(double _ini)
93 {
94         contenido_inicial = _ini;
95 }
96
97 double Cistern::get_capacidad()
98 {
99         return capacidad;
100 }
101
102 double Cistern::get_contenido_inicial()
103 {
104         return contenido_inicial;
105 }
106
107 Gdk::Color Cistern::get_liquid_color()
108 {
109         return liquid_color;
110 }
111
112 void Cistern::save(FILE *archivo)
113 {
114         char c_id[50], c_cap[50], c_x[50], c_y[50], c_img[50], c_ini[50], c_red[50], c_green[50], c_blue[50];
115         Glib::ustring  con0, con1;
116         if (connect_vec[0].type == IN)
117                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
118         else
119                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
120         if (connect_vec[1].type == IN) 
121                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
122         else 
123                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
124         
125         sprintf(c_red,"\t\t\t<rojo>%d</rojo>\n",liquid_color.get_red());
126         sprintf(c_green,"\t\t\t<verde>%d</verde>\n",liquid_color.get_green());
127         sprintf(c_blue,"\t\t\t<azul>%d</azul>\n",liquid_color.get_blue());
128         sprintf(c_x,"\t\t<x>%d</x>\n",x);
129         sprintf(c_y,"\t\t<y>%d</y>\n",y);
130         sprintf(c_id,"%d",ID);
131         sprintf(c_cap,"\t\t<capacidad>%.2f</capacidad>\n", capacidad);
132         sprintf(c_ini,"\t\t<inicial>%.2f</inicial>\n", contenido_inicial);
133         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
134         Glib::ustring dato;
135         dato = "\t<tanque nombre=\""+name+"\" id=\"";
136         dato += c_id;
137         dato += "\">\n";
138         dato += c_cap;
139         dato += c_ini;
140         dato += "\t\t<color>\n";
141         dato += c_red;
142         dato += c_green;
143         dato += c_blue;
144         dato += "\t\t</color>\n";
145         dato += "\t\t<conector>\n"+con0+con1+"\t\t</conector>\n";
146         dato += c_img;
147         dato += c_x; 
148         dato += c_y;
149         dato += "\t</tanque>\n";
150         fprintf(archivo,dato.c_str());  
151 }
152
153 bool Cistern::check_connection()
154 {
155         CItem * _item, *_item1;
156         ConnectorType temp0, temp1;
157         switch (get_img_actual()) {
158                 case 0:
159                         temp0 = is_other_connection_area( get_position_x()+get_image()->get_width() + 5 , get_position_y()+get_image()->get_height() -16, &_item);
160                         temp1 = is_other_connection_area( get_position_x() + 16, get_position_y() - 5, &_item1);
161                         break;
162                 case 1:
163                         temp0 = is_other_connection_area( get_position_x() -5, get_position_y()+get_image()->get_height()-16, &_item);
164                         temp1 = is_other_connection_area( get_position_x()+get_image()->get_width() - 16, get_position_y() - 5, &_item1);
165         }
166         if (temp0 == OUT && temp1 == IN) { 
167                 connect_vec[0].id_dest = _item->get_id();
168                 connect_vec[1].id_dest = _item1->get_id();
169                 return (is_connected = true);
170         }
171         return is_connected;
172 }
173
174 ConnectorType Cistern::get_connector_type(int _a, int _b)
175 {
176         switch (imgActual) {
177                 case 0: if ( (_a <= x+22)&&(_a>=x+10)&&(_b<=y+10)&&(_b > y) )
178                                                 return connect_vec[0].type;
179                                         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) )
180                                                 return connect_vec[1].type;
181                                         break;
182                 case 1: if ( (_a <= x+image->get_width()-10)&&(_a>=x+image->get_width()-22)&&(_b<=y+10)&&(_b > y) )
183                                                 return connect_vec[0].type;
184                                         if ( (_a <= x +10)&&(_a > x)&&(_b<=y+image->get_height()-10)&&(_b >=y+image->get_height()-22) )
185                                                 return connect_vec[1].type;
186         }
187         return UNDEF;
188 }
189
190 void Cistern::set_default_connector()
191 {
192         connect_vec[0].type = IN;
193         connect_vec[1].type = OUT;
194 }