]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/constructor.cpp
- Se agrega el método HTTPRequest::method_str() para obtener el método como un
[z.facultad/75.42/plaqui.git] / Constructor / constructor.cpp
1 #include "constructor.h"
2
3 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
4 {
5         id = 0;
6         set_title("Constructor");
7         
8         
9         ico_canio = Gdk::Pixbuf::create_from_file("canio_n.png");
10         ico_y     = Gdk::Pixbuf::create_from_file("y_n.png");
11         ico_codo  = Gdk::Pixbuf::create_from_file("codo_o.png");
12         ico_tanque  = Gdk::Pixbuf::create_from_file("tanque_e.png");
13         ico_exclusa = Gdk::Pixbuf::create_from_file("exclusa_h.png");
14         ico_bomba = Gdk::Pixbuf::create_from_file("bomba_e.png");
15         
16         refGlade->get_widget("btn_canio", btn_canio);
17         refGlade->get_widget("btn_codo", btn_codo);
18         refGlade->get_widget("btn_y", btn_y);
19         refGlade->get_widget("btn_tanque",btn_tanque);
20         refGlade->get_widget("btn_bomba", btn_bomba);
21         refGlade->get_widget("btn_exclusa", btn_exclusa);
22         refGlade->get_widget("main_menu_quit",main_menu_quit);
23         refGlade->get_widget("main_menu_save",main_menu_save);
24         refGlade->get_widget("edit_menu_del",edit_menu_del);
25         refGlade->get_widget("chk_btn_logica",chk_btn_logica);
26         refGlade->get_widget("file_selection",file_selection);
27         refGlade->get_widget("combo_entry",combo_entry);
28         refGlade->get_widget("btn_check",btn_check);
29         refGlade->get_widget("dlg_connect", dlg_connect);
30         refGlade->get_widget("btn_dlg_close", btn_dlg_close);
31         refGlade->get_widget("dlg_label", dlg_label);
32         
33         refGlade->get_widget_derived("workplace", workplace);  //fixed
34         
35         //Targets
36         listTargets.push_back( Gtk::TargetEntry("STRING") );
37         listTargets.push_back( Gtk::TargetEntry("text/plain") );
38         listTargets.push_back( Gtk::TargetEntry("POINTER") );
39         listTargets.push_back( Gtk::TargetEntry("application/pointer") );
40         
41         btn_canio->drag_source_set(listTargets);
42         btn_y->drag_source_set(listTargets);
43         btn_codo->drag_source_set(listTargets);
44         btn_tanque->drag_source_set(listTargets);
45         btn_bomba->drag_source_set(listTargets);
46         btn_exclusa->drag_source_set(listTargets);
47         
48         btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
49         btn_bomba->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_bomba_drag_get));
50         btn_exclusa->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_exclusa_drag_get));
51         btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
52         btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
53         btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
54         btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
55         btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
56         chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
57         
58         main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
59         main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
60         edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
61                         
62         btn_file_cancel = file_selection->get_cancel_button();
63         btn_file_ok = file_selection->get_ok_button();
64         btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
65         btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
66         
67         // Señales para cambiar el icono cuando empieza el drag.
68         btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
69         btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
70         btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
71         btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
72         btn_bomba->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_bomba_drag_begin));
73         btn_exclusa->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_exclusa_drag_begin));
74
75         workplace->drag_dest_set(listTargets);
76         workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
77         workplace->listaItems = &listaItems;    
78         logica = false;
79 }
80
81 Constructor::~Constructor()
82 {
83         std::list<CItem *>::iterator i = listaItems.begin();
84         std::cout << "ok" << std::endl;
85         while ( i != listaItems.end() ){
86                 CItem *temp = *i;
87                 listaItems.erase(i);
88                 std::cout << "Elimnando ... " << std::endl;
89                 delete temp;
90                 i = listaItems.begin();
91         }
92 }
93
94 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
95 {
96         gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 14);
97 }
98
99 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
100 {
101         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",10);
102 }
103
104 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
105 {
106         gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",13);
107 }
108
109 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
110 {
111         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",15);
112 }
113
114 void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
115 {
116         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",15);
117 }
118
119 void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
120 {
121         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",15);
122 }
123
124 void Constructor::on_main_menu_quit()
125 {
126         delete this;
127         Gtk::Main::quit();
128         //hide();
129 }
130
131 void Constructor::on_main_menu_save()
132 {
133         file_selection->show();
134 }
135
136 void Constructor::on_edit_menu_del()
137 {
138         // hay que meter algo aca.
139 }
140
141 void Constructor::on_chk_btn_clicked()
142 {
143         logica = !logica;
144 }
145
146 void Constructor::on_btn_file_ok_clicked()
147 {
148         std::list<CItem *>::iterator i = listaItems.begin();
149         std::string file_name = file_selection->get_filename();
150         if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
151                 fprintf (archivo,"<planta>\n");
152                 while ( i != listaItems.end() ){
153                         CItem *temp = *i;
154                         temp->save(archivo);
155                         i++;
156                 }
157                 fprintf(archivo,"</planta>\n");
158         } else 
159                 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
160         fclose(archivo);
161         file_selection->hide();
162 }
163
164 void Constructor::on_btn_file_cancel_clicked()
165 {
166         file_selection->hide();
167 }
168
169 void Constructor::on_btn_dlg_connect_clicked()
170 {
171         dlg_connect->hide();
172 }
173
174 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
175 {
176         context->set_icon(ico_canio, 5, 5); 
177 }
178
179 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
180 {
181         context->set_icon(ico_y, 5, 5); 
182 }
183
184 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
185 {
186         context->set_icon(ico_codo, 5, 5); 
187 }
188
189 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
190 {
191         context->set_icon(ico_tanque, 5, 5); 
192 }
193
194 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
195 {
196         context->set_icon(ico_bomba, 5, 5); 
197 }
198
199 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
200 {
201         context->set_icon(ico_exclusa, 5, 5); 
202 }
203
204 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
205 {
206         context->set_icon(item->get_image(), 5, 5);
207 }
208
209 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
210 {       
211                 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
212 }
213
214 bool Constructor::can_drop(CItem *item, int _x, int _y)
215 {
216         std::list<CItem*>::iterator  i = listaItems.begin();
217         while( i != listaItems.end() ) {
218                 CItem *temp = *i;
219                 if ( temp->get_id() != item->get_id()   ) {
220                         if (  (temp->is_occupied_area(_x, _y))  ||  
221                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
222                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
223                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
224                                 return false;
225                         else i++;
226                 }
227                 else i++;
228         }
229         return true;
230 }
231
232 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
233 {
234         if ( !logica ) {
235                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
236                 int i,j;
237                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
238                 // separacion de 2 cuadritos
239                 i = (x+1)/32;
240                 j = (y+1)/32;
241                 // El drag es de un item
242                 if (selection_data->format == 10) {
243                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
244                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
245                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
246                         }
247                 }
248
249                 // El Drag es desde la barra de tareas
250                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
251                         CItem *a;
252                         if (strcmp((const char *)selection_data->data, "codo_o.png")==0) 
253                                 a = new Splitter();
254                                         else    if (strcmp((const char *)selection_data->data, "canio_n.png")==0) 
255                                                 a = new Conduct();
256                                                         else if (strcmp((const char *)selection_data->data, "y_n.png")==0) 
257                                                                 a = new Union();
258                                                                         else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0) 
259                                                                                 a = new Cistern();
260                                                                                         else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0) 
261                                                                                                 a =new Pump();
262                                                                                                         else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0) 
263                                                                                                                 a = new Exclusa();
264                                                                                                                         
265                 
266                         char char_id[10];
267                         sprintf(char_id," %d",id);
268                         a->set_id(++id);                
269                         a->set_name( a->get_name()+char_id );
270                         if ( can_drop(a, i*32, j*32) ) {
271                                 workplace->put(*a, i*32, j*32);
272                                 //Apunto al workplace
273                                 a->workplace= workplace;
274                                 //Apunto a la lista.
275                                 a->combo_entry = combo_entry;
276                                 //Apunto a la listaItems.
277                                 a->listaItems = &listaItems;
278                                 //Seteo la posicion del item
279                                 a->set_position(i*32,j*32);
280                                 // Seteo la lista de tipos de drags 
281                                 a->drag_source_set(listTargets);
282                                 // Conecto las señales
283                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
284                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
285                                 // parametro extra, en este caso un CItem *. Esto se hace para
286                                 // que cuando el usuario quiera mover un item, saber que item es
287                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
288                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
289                                 // raro se vea el widget tal cual.
290                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
291                                 a->show();      
292                                 listaItems.push_back(a);
293                         } else {
294                                         id--;
295                                         delete a;
296                                 }
297                 }
298                 context->drag_finish(false, false, time);
299         }
300 }
301
302 void Constructor::on_btn_check_clicked()
303 {
304         std::list<CItem *>::iterator i = listaItems.begin();
305         if ( !listaItems.empty() ) {
306                 while ( i != listaItems.end() ) {
307                         CItem *temp = *i;
308                         if ( !temp->check_connection() ) {
309                                 dlg_connect->set_title("Error");
310                                 dlg_label->set_text("Los elementos no estan conectados\n\t\tcorrectamente");
311                                 dlg_connect->show();
312                                 return;
313                         }       
314                 i++;
315                 }
316                 dlg_connect->set_title("Conexion");
317                 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");
318                 dlg_connect->show();    
319         }
320 }