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