]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/workplace.cpp
* Se arregla un bug en el constructor al verificar conexiones
[z.facultad/75.42/plaqui.git] / Constructor / src / workplace.cpp
1 #include "workplace.h"
2 #include "item.h"
3
4 int WorkPlace::pointed = -1;
5
6 WorkPlace::WorkPlace(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> &refGlade):Gtk::Fixed(cobject)
7 {
8         gc = Gdk::GC::create(get_window());
9         std::cout << "Aca" << std::endl;
10         Glib::RefPtr<Gdk::Colormap> colormap = gc->get_colormap();
11         color_in = Gdk::Color("red");
12         color_out = Gdk::Color("blue");
13         color_dot = Gdk::Color("black");
14         colormap->alloc_color(color_in);
15         colormap->alloc_color(color_out);
16         colormap->alloc_color(color_dot);
17 }
18
19 WorkPlace::~WorkPlace()
20 {
21 }
22
23 void WorkPlace::on_realize()
24 {
25         Gtk::Fixed::on_realize();
26         gc = Gdk::GC::create(get_window());
27         std::cout << "Aca" << std::endl;
28         Glib::RefPtr<Gdk::Colormap> colormap = gc->get_colormap();
29         color_in = Gdk::Color("red");
30         color_out = Gdk::Color("blue");
31         colormap->alloc_color(color_in);
32         colormap->alloc_color(color_out);
33 }
34
35 bool WorkPlace::on_expose_event(GdkEventExpose *event)
36 {
37         int x, y;
38         gc->set_foreground(color_dot);
39         for(x=0; x<get_width(); x+=32) 
40                 for (y=0; y<get_height();y+=32){
41                         get_window()->draw_point (gc, x,y);
42                         get_window()->draw_point (gc, x,y+1);
43                         get_window()->draw_point (gc, x,y-1);
44                         get_window()->draw_point (gc, x+1,y);
45                         get_window()->draw_point (gc, x-1,y);
46                 }
47                 
48         if ( *logica ) {        
49                 std::list<CItem *>::iterator j = listaItems->begin();
50                 while ( j != listaItems->end() ) {
51                         (*j)->update_logic_position();
52                         j++;
53                 }
54         }
55         
56         int a, b, w, z, x_offset, y_offset, img;
57         std::list<t_line>::iterator i = lista_lineas_in.begin();
58         while ( i != lista_lineas_in.end() ) {
59                 t_line temp = *i;
60                 if ( get_item(temp.store_id) != NULL && get_logic_item(temp.logic_id) != NULL ) {
61                         img = get_logic_item(temp.logic_id)->get_img_actual();
62                         x_offset = y_offset = 0;
63                         switch (img) {
64                                 case 0:
65                                         x_offset = 15;
66                                 break;
67                                 case 1:
68                                         y_offset = 15;
69                                 break;
70                                 case 2:
71                                         x_offset = -15;
72                                 break;
73                                 case 3:
74                                         y_offset = -15;
75                         }
76                         get_logic_item(temp.logic_id)->get_out_logic_connect_position(a, b);
77                         get_item(temp.store_id)->get_in_logic_connect_position(w,z);
78                         draw_line(a+x_offset, b+y_offset, w,z, color_in);
79                         get_window()->draw_line(gc, a, b, a+x_offset, b+y_offset);
80                 }
81                 i++;
82         }
83         i = lista_lineas_out.begin();
84         while ( i != lista_lineas_out.end() ) {
85                 t_line temp = *i;
86                 if ( get_item(temp.store_id) != NULL && get_logic_item(temp.logic_id) != NULL ) {
87                         img = get_logic_item(temp.logic_id)->get_img_actual();
88                         x_offset = y_offset = 0;
89                         switch (img) {
90                                 case 0:
91                                         x_offset = 15;
92                                 break;
93                                 case 1:
94                                         y_offset = 15;
95                                 break;
96                                 case 2:
97                                         x_offset = -15;
98                                 break;
99                                 case 3:
100                                         y_offset = -15;
101                         }
102                         get_item(temp.store_id)->get_out_logic_connect_position(w,z);
103                         get_logic_item(temp.logic_id)->get_in_logic_connect_position(a, b);
104 //                      draw_line(w, z, a, b, color_out);
105                         draw_line(w, z, a+x_offset, b+y_offset,color_out);
106                         get_window()->draw_line(gc, a, b, a+x_offset, b+y_offset);
107                 }
108                 i++;
109         }
110         return true;
111 }
112
113 void WorkPlace::delete_item(int _id)
114 {
115         delete_line(_id);
116         CItem::logic_connect = false;
117         std::list<CItem*>::iterator i = listaItems->begin();
118         while ( i != listaItems->end() ){
119         CItem *temp = *i;
120                 if ( temp->get_id() == _id ){
121                         listaItems->erase(i);
122                         delete temp;
123                         break;
124                 }
125                 i++;
126         }
127         i = lista_logic_Items->begin();
128         while ( i != lista_logic_Items->end() ){
129         CItem *temp = *i;
130                 if ( temp->get_id() == _id ){
131                         lista_logic_Items->erase(i);
132                         delete temp;
133                         break;
134                 }
135                 i++;
136         }
137         CItem::gate_id = -1;    
138 }
139
140 CItem* WorkPlace::get_logic_item(int _id)
141 {
142         std::list<CItem *>::iterator i = lista_logic_Items->begin();
143         while ( i != lista_logic_Items->end() ){
144                 if ( (*i)->get_id() == _id ) 
145                         return *i;
146                 i++;
147         }
148         return NULL;
149 }
150
151 CItem *WorkPlace::get_item(int _id)
152 {
153         std::list<CItem *>::iterator i = listaItems->begin();
154         while ( i != listaItems->end() ){
155                 if ( (*i)->get_id() == _id ) 
156                         return *i;
157                 i++;
158         }
159         return NULL;
160 }
161
162         
163 void WorkPlace::delete_line(int _id)
164 {
165         std::list<t_line>::iterator i = lista_lineas_in.begin();
166         while ( i != lista_lineas_in.end() ){
167                 if (get_item( (*i).store_id ) != NULL)
168                         if ( get_item( (*i).store_id )->get_id() == _id ) {
169                                 lista_lineas_in.erase(i);
170                                 i = lista_lineas_in.begin();
171                         }
172                 i++;
173         }
174
175         i = lista_lineas_in.begin();
176         while ( i != lista_lineas_in.end() ){
177                 if ( get_logic_item((*i).logic_id) != NULL)
178                         if ( get_logic_item((*i).logic_id)->get_id() == _id) {
179                                 lista_lineas_in.erase(i);
180                                 i = lista_lineas_in.begin();
181                         }
182                 i++;
183         }
184
185         std::list<t_line>::iterator j = lista_lineas_out.begin();
186         while ( j != lista_lineas_out.end() ){
187                 if ( get_item((*j).store_id) != NULL )
188                         if ( get_item((*j).store_id)->get_id() == _id ) {
189                                 lista_lineas_out.erase(j);
190                                 j = lista_lineas_out.begin();
191                         }
192                 j++;
193         }       
194
195         j = lista_lineas_out.begin();
196         while ( j != lista_lineas_out.end() ){
197                 if ( get_logic_item((*j).logic_id) != NULL )
198                         if ( get_logic_item((*j).logic_id)->get_id() == _id ) {
199                                 lista_lineas_out.erase(j);
200                                 j = lista_lineas_out.begin();
201                         }
202                 j++;
203         }       
204 }
205
206 void WorkPlace::update_logic_position()
207 {
208         std::list<CItem*>::iterator i = listaItems->begin();
209         while ( i != listaItems->end() ){
210                 (*i)->update_logic_position();
211                 i++;
212         }
213         i = lista_logic_Items->begin();
214         while ( i != lista_logic_Items->end() ){
215                 (*i)->update_logic_position();
216                 i++;
217         }
218 }
219                 
220 void WorkPlace::draw_line(int x1, int y1, int x2, int y2, Gdk::Color &color)
221 {
222         gc->set_foreground(color);
223         gc->set_line_attributes(3, Gdk::LINE_SOLID, Gdk::CAP_NOT_LAST, Gdk::JOIN_MITER);
224         get_window()->draw_line (gc, x1, y1, x1, y2);
225         get_window()->draw_line (gc, x1, y2, x2, y2);
226 }
227