]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/workplace.cpp
Se agrega el doc del cliente REVISAR Y CORREGIR A GUSTO!
[z.facultad/75.42/plaqui.git] / Constructor / src / workplace.cpp
1 #include "workplace.h"
2 #include "item.h"
3 #include "pump.h"
4 #include "exclusa.h"
5 #include "not.h"
6
7 int WorkPlace::pointed = -1;
8
9 WorkPlace::WorkPlace(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> &refGlade):Gtk::Fixed(cobject)
10 {
11         gc = Gdk::GC::create(get_window());
12         Glib::RefPtr<Gdk::Colormap> colormap = gc->get_colormap();
13         color_in = Gdk::Color("red");
14         color_out = Gdk::Color("blue");
15         color_dot = Gdk::Color("black");
16         colormap->alloc_color(color_in);
17         colormap->alloc_color(color_out);
18         colormap->alloc_color(color_dot);
19 }
20
21 WorkPlace::~WorkPlace()
22 {
23 }
24
25 void WorkPlace::on_realize()
26 {
27         Gtk::Fixed::on_realize();
28         gc = Gdk::GC::create(get_window());
29         Glib::RefPtr<Gdk::Colormap> colormap = gc->get_colormap();
30         color_in = Gdk::Color("red");
31         color_out = Gdk::Color("blue");
32         colormap->alloc_color(color_in);
33         colormap->alloc_color(color_out);
34 }
35
36 bool WorkPlace::on_expose_event(GdkEventExpose *event)
37 {
38         int x, y;
39         gc->set_foreground(color_dot);
40         for(x=0; x<get_width(); x+=32) 
41                 for (y=0; y<get_height();y+=32){
42                         get_window()->draw_point (gc, x,y);
43                         get_window()->draw_point (gc, x,y+1);
44                         get_window()->draw_point (gc, x,y-1);
45                         get_window()->draw_point (gc, x+1,y);
46                         get_window()->draw_point (gc, x-1,y);
47                 }
48                 
49         if ( *logica ) {        
50                 std::list<CItem *>::iterator j = listaItems->begin();
51                 while ( j != listaItems->end() ) {
52                         (*j)->update_logic_position();
53                         j++;
54                 }
55         }
56         
57         int a, b, w, z, x_offset, y_offset, img;
58         int item_x_offset, item_y_offset;
59         std::list<t_line>::iterator i = lista_lineas_in.begin();
60         while ( i != lista_lineas_in.end() ) {
61                 t_line temp = *i;
62                 if ( get_item(temp.store_id) != NULL && get_logic_item(temp.logic_id) != NULL ) {
63                         img = get_logic_item(temp.logic_id)->get_img_actual();
64                         item_y_offset = item_x_offset = 0;
65                         x_offset = y_offset = 0;
66                         if (dynamic_cast<Pump *>(get_item(temp.store_id))) {
67                                 item_y_offset = -15;
68                         } else if (dynamic_cast<Exclusa *>(get_item(temp.store_id))) {
69                                 if (get_item(temp.store_id)->get_img_actual()==0) {
70                                         item_y_offset = -15;
71                                 } else {
72                                         item_x_offset = 15;
73                                 }
74                         }
75                         switch (img) {
76                                 case 0:
77                                         x_offset = 15;
78                                 break;
79                                 case 1:
80                                         y_offset = 15;
81                                 break;
82                                 case 2:
83                                         x_offset = -15;
84                                 break;
85                                 case 3:
86                                         y_offset = -15;
87                         }
88                         get_logic_item(temp.logic_id)->get_out_logic_connect_position(a, b);
89                         get_item(temp.store_id)->get_in_logic_connect_position(w,z);
90                         draw_line(a+x_offset, b+y_offset, w+item_x_offset, z+item_y_offset, color_in);
91                         get_window()->draw_line(gc, a, b, a+x_offset, b+y_offset);
92                         get_window()->draw_line(gc, w, z, w+item_x_offset, z+item_y_offset);
93                 }
94                 i++;
95         }
96         i = lista_lineas_out.begin();
97         while ( i != lista_lineas_out.end() ) {
98                 t_line temp = *i;
99                 if ( get_item(temp.store_id) != NULL && get_logic_item(temp.logic_id) != NULL ) {
100                         img = get_logic_item(temp.logic_id)->get_img_actual();
101                         item_y_offset = item_x_offset = 0;
102                         x_offset = y_offset = 0;
103                         switch (img) {
104                                 case 0:
105                                         x_offset = -15;
106                                 break;
107                                 case 1:
108                                         y_offset = -15;
109                                 break;
110                                 case 2:
111                                         x_offset = 15;
112                                 break;
113                                 case 3:
114                                         y_offset = 15;
115                         }
116                         if ( temp.cistern_out1 ) 
117                                 get_item(temp.store_id)->get_in_logic_connect_position(w,z);
118                         else
119                                 get_item(temp.store_id)->get_out_logic_connect_position(w,z);
120                         get_logic_item(temp.logic_id)->get_in_logic_connect_position(a, b);
121                         draw_line(w+item_y_offset, z+item_y_offset, a+x_offset, b+y_offset,color_out);
122                         get_window()->draw_line(gc, a, b, a+x_offset, b+y_offset);
123                 }
124                 i++;
125         }
126         
127         i = lista_lineas_logic.begin();
128         while ( i != lista_lineas_logic.end() ) {
129                 t_line temp = *i;
130                 if ( get_logic_item(temp.store_id) != NULL && get_logic_item(temp.logic_id) != NULL ) {
131                         get_logic_item(temp.logic_id)->get_out_logic_connect_position(w,z);
132                         get_logic_item(temp.store_id)->get_in_logic_connect_position(a, b);
133                         get_window()->draw_line(gc, a, b, w,z);
134                 }
135                 i++;
136         }
137         return true;
138 }
139
140 void WorkPlace::delete_item(int _id)
141 {
142         delete_line(_id);
143         std::list<CItem*>::iterator i = listaItems->begin();
144         while ( i != listaItems->end() ){
145         CItem *temp = *i;
146                 if ( temp->get_id() == _id ){
147                         listaItems->erase(i);
148                         delete temp;
149                         break;
150                 }
151                 i++;
152         }
153         i = lista_logic_Items->begin();
154         while ( i != lista_logic_Items->end() ){
155         CItem *temp = *i;
156                 if ( temp->get_id() == _id ){
157                         lista_logic_Items->erase(i);
158                         delete temp;
159                         break;
160                 }
161                 i++;
162         }
163         CItem::gate_id = -1;    
164 }
165
166 CItem* WorkPlace::get_logic_item(int _id)
167 {
168         std::list<CItem *>::iterator i = lista_logic_Items->begin();
169         while ( i != lista_logic_Items->end() ){
170                 if ( (*i)->get_id() == _id ) 
171                         return *i;
172                 i++;
173         }
174         return NULL;
175 }
176
177 CItem *WorkPlace::get_item(int _id)
178 {
179         std::list<CItem *>::iterator i = listaItems->begin();
180         while ( i != listaItems->end() ){
181                 if ( (*i)->get_id() == _id ) 
182                         return *i;
183                 i++;
184         }
185         return NULL;
186 }
187
188 int WorkPlace::get_logic_id(const std::string &_s)
189 {
190         std::list<CItem *>::iterator i = lista_logic_Items->begin();
191         while ( i != lista_logic_Items->end() ){
192                 if ( (*i)->get_name() == _s ) 
193                         return (*i)->get_id();
194                 i++;
195         }
196         return -1;
197 }
198
199 int WorkPlace::get_item_id(const std::string &_s)
200 {
201         std::list<CItem *>::iterator i = listaItems->begin();
202         while ( i != listaItems->end() ){
203                 if ( (*i)->get_name() == _s ) 
204                         return (*i)->get_id();
205                 i++;
206         }
207         return -1;
208 }
209         
210 void WorkPlace::delete_line(int _id)
211 {
212         /* LINEAS A LAS ENTRADAS DE LOS ITEMS */
213         /*      Borro la linea desde el item eliminado hasta el que este conectado */
214         std::list<t_line>::iterator i = lista_lineas_in.begin();
215         while ( i != lista_lineas_in.end() ){
216                 if (get_item( (*i).store_id ) != NULL)
217                         if ( get_item( (*i).store_id )->get_id() == _id ) {
218                                 get_logic_item((*i).logic_id)->set_out_connected(false);        
219                                 lista_lineas_in.erase(i);
220                                 i = lista_lineas_in.begin();
221                         }
222                 i++;
223         }
224         
225         /* Borra la linea desde la compuerta eliminada hasta el item conectado a ella */
226         i = lista_lineas_in.begin();
227         while ( i != lista_lineas_in.end() ){
228                 if ( get_logic_item((*i).logic_id) != NULL)
229                         if ( get_logic_item((*i).logic_id)->get_id() == _id) {
230                                 lista_lineas_in.erase(i);
231                                 i = lista_lineas_in.begin();
232                         }
233                 i++;
234         }
235         
236         /* LINEAS A LAS SALIDAS DE LOS ITEMS */
237         /*      Borro la linea desde el item eliminado hasta el que este conectado */
238         std::list<t_line>::iterator j = lista_lineas_out.begin();
239         while ( j != lista_lineas_out.end() ){
240                 if ( get_item((*j).store_id) != NULL )
241                         if ( get_item((*j).store_id)->get_id() == _id ) {
242                                 if (dynamic_cast<Not *>(get_logic_item((*j).logic_id))) 
243                                         get_logic_item((*j).logic_id)->set_in_connected(false);
244                                 lista_lineas_out.erase(j);
245                                 j = lista_lineas_out.begin();
246                         }
247                 j++;
248         }       
249         
250         /* Borra la linea desde la compuerta eliminada hasta el item conectado a ella */
251         j = lista_lineas_out.begin();
252         while ( j != lista_lineas_out.end() ){
253                 if ( get_logic_item((*j).logic_id) != NULL )
254                         if ( get_logic_item((*j).logic_id)->get_id() == _id ) {
255                                 lista_lineas_out.erase(j);
256                                 j = lista_lineas_out.begin();
257                         }
258                 j++;
259         }       
260         
261         /* LINEAS ENTRE COMPUERTAS */
262         /* Borra la linea entre al compuerta eliminada y la que esta conectada a ella en su entrada */
263         std::list<t_line>::iterator k = lista_lineas_logic.begin();
264         while ( k != lista_lineas_logic.end() ){
265                 if ( get_logic_item((*k).store_id) != NULL )
266                         if ( get_logic_item((*k).store_id)->get_id() == _id ) {
267                                 get_logic_item((*k).logic_id)->set_out_connected(false);
268                                 std::cout<< get_logic_item((*k).logic_id)->get_name() <<std::endl;
269                                 lista_lineas_logic.erase(k);
270                                 k = lista_lineas_logic.begin();
271                         }
272                 k++;
273         }       
274         
275         /* Borra la linea entre al compuerta eliminada y la que esta conectada a ella en su salida*/
276         k = lista_lineas_logic.begin();
277         while ( k != lista_lineas_logic.end() ){
278                 if ( get_logic_item((*k).logic_id) != NULL )
279                         if ( get_logic_item((*k).logic_id)->get_id() == _id ) {
280                                 if (dynamic_cast<Not *>(get_logic_item((*k).store_id))) 
281                                         get_logic_item((*k).store_id)->set_in_connected(false);
282                                 lista_lineas_logic.erase(k);
283                                 k = lista_lineas_logic.begin();
284                         }
285                 k++;
286         }       
287 }
288
289 void WorkPlace::update_logic_position()
290 {
291         std::list<CItem*>::iterator i = listaItems->begin();
292         while ( i != listaItems->end() ){
293                 (*i)->update_logic_position();
294                 i++;
295         }
296         i = lista_logic_Items->begin();
297         while ( i != lista_logic_Items->end() ){
298                 (*i)->update_logic_position();
299                 i++;
300         }
301 }
302                 
303 void WorkPlace::draw_line(int x1, int y1, int x2, int y2, Gdk::Color &color)
304 {
305         gc->set_foreground(color);
306         gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
307         get_window()->draw_line (gc, x1, y1, x1, y2);
308         get_window()->draw_line (gc, x1, y2, x2, y2);
309 }
310
311
312 bool WorkPlace::chek_name(Glib::ustring _name, int _id)
313 {
314         std::list<CItem *>::iterator i;
315         i  = listaItems->begin();
316         while ( i != listaItems->end() ) {
317                 if ( (*i)->get_name() == _name && (*i)->get_id() != _id )
318                                 return true;
319                 i++;
320         }
321         return false;
322 }