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