1 #include "constructor.h"
3 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
6 set_title("Constructor");
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 ico_and = Gdk::Pixbuf::create_from_file("and_e.png");
16 ico_or = Gdk::Pixbuf::create_from_file("or_e.png");
17 ico_not = Gdk::Pixbuf::create_from_file("not_e.png");
19 refGlade->get_widget("btn_canio", btn_canio);
20 refGlade->get_widget("btn_codo", btn_codo);
21 refGlade->get_widget("btn_y", btn_y);
22 refGlade->get_widget("btn_tanque",btn_tanque);
23 refGlade->get_widget("btn_bomba", btn_bomba);
24 refGlade->get_widget("btn_exclusa", btn_exclusa);
25 refGlade->get_widget("btn_and", btn_and);
26 refGlade->get_widget("btn_or", btn_or);
27 refGlade->get_widget("btn_not", btn_not);
28 refGlade->get_widget("main_menu_quit",main_menu_quit);
29 refGlade->get_widget("main_menu_save",main_menu_save);
30 refGlade->get_widget("edit_menu_del",edit_menu_del);
31 refGlade->get_widget("chk_btn_logica",chk_btn_logica);
32 refGlade->get_widget("file_selection",file_selection);
33 refGlade->get_widget("combo_entry",combo_entry);
34 refGlade->get_widget("btn_check",btn_check);
35 refGlade->get_widget("dlg_connect", dlg_connect);
36 refGlade->get_widget("btn_dlg_close", btn_dlg_close);
37 refGlade->get_widget("dlg_label", dlg_label);
39 refGlade->get_widget_derived("workplace", workplace); //fixed
42 listTargets.push_back( Gtk::TargetEntry("STRING") );
43 listTargets.push_back( Gtk::TargetEntry("text/plain") );
44 listTargets.push_back( Gtk::TargetEntry("POINTER") );
45 listTargets.push_back( Gtk::TargetEntry("application/pointer") );
47 btn_canio->drag_source_set(listTargets);
48 btn_y->drag_source_set(listTargets);
49 btn_codo->drag_source_set(listTargets);
50 btn_tanque->drag_source_set(listTargets);
51 btn_bomba->drag_source_set(listTargets);
52 btn_exclusa->drag_source_set(listTargets);
53 btn_and->drag_source_set(listTargets);
54 btn_or->drag_source_set(listTargets);
55 btn_not->drag_source_set(listTargets);
57 btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
58 btn_bomba->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_bomba_drag_get));
59 btn_exclusa->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_exclusa_drag_get));
60 btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
61 btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
62 btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
63 btn_and->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_and_drag_get));
64 btn_or->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_or_drag_get));
65 btn_not->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_not_drag_get));
66 btn_check->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_check_clicked));
67 btn_dlg_close->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_dlg_connect_clicked));
68 chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
70 main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
71 main_menu_save->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_save));
72 edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
74 btn_file_cancel = file_selection->get_cancel_button();
75 btn_file_ok = file_selection->get_ok_button();
76 btn_file_cancel->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_cancel_clicked));
77 btn_file_ok->signal_clicked().connect(SigC::slot(*this,&Constructor::on_btn_file_ok_clicked));
79 // Señales para cambiar el icono cuando empieza el drag.
80 btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
81 btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
82 btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
83 btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
84 btn_bomba->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_bomba_drag_begin));
85 btn_exclusa->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_exclusa_drag_begin));
86 btn_and->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_and_drag_begin));
87 btn_or->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_or_drag_begin));
88 btn_not->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_not_drag_begin));
89 workplace->drag_dest_set(listTargets);
90 workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );
91 workplace->listaItems = &listaItems;
95 Constructor::~Constructor()
97 std::list<CItem *>::iterator i = listaItems.begin();
98 while ( i != listaItems.end() ){
102 i = listaItems.begin();
106 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
108 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 14);
111 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
113 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",10);
116 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
118 gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",13);
121 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
123 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",15);
126 void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
128 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",15);
131 void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
133 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",15);
136 void Constructor::on_btn_and_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
138 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"and_e.png",9);
141 void Constructor::on_btn_or_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
143 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"or_e.png",8);
146 void Constructor::on_btn_not_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
148 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"not_e.png",9);
152 void Constructor::on_main_menu_quit()
159 void Constructor::on_main_menu_save()
161 file_selection->show();
164 void Constructor::on_edit_menu_del()
166 // hay que meter algo aca.
169 void Constructor::on_chk_btn_clicked()
174 void Constructor::on_btn_file_ok_clicked()
176 std::list<CItem *>::iterator i = listaItems.begin();
177 std::string file_name = file_selection->get_filename();
178 if ( (archivo = fopen( file_name.c_str(), "w+")) != NULL ){
179 fprintf (archivo,"<planta>\n");
180 while ( i != listaItems.end() ){
185 fprintf(archivo,"</planta>\n");
187 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
189 file_selection->hide();
192 void Constructor::on_btn_file_cancel_clicked()
194 file_selection->hide();
197 void Constructor::on_btn_dlg_connect_clicked()
202 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
204 context->set_icon(ico_canio, 5, 5);
207 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
209 context->set_icon(ico_y, 5, 5);
212 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
214 context->set_icon(ico_codo, 5, 5);
217 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
219 context->set_icon(ico_tanque, 5, 5);
222 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
224 context->set_icon(ico_bomba, 5, 5);
227 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
229 context->set_icon(ico_exclusa, 5, 5);
232 void Constructor::on_and_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
234 context->set_icon(ico_and, 5, 5);
237 void Constructor::on_or_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
239 context->set_icon(ico_or, 5, 5);
242 void Constructor::on_not_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
244 context->set_icon(ico_not, 5, 5);
247 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
249 context->set_icon(item->get_image(), 5, 5);
252 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
254 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
257 bool Constructor::can_drop(CItem *item, int _x, int _y)
259 std::list<CItem*>::iterator i = listaItems.begin();
260 while( i != listaItems.end() ) {
262 if ( temp->get_id() != item->get_id() ) {
263 if ( (temp->is_occupied_area(_x, _y)) ||
264 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1)) ||
265 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
266 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) )
275 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
278 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
280 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
281 // separacion de 2 cuadritos
284 // El drag es de un item
285 if (selection_data->format == 10) {
286 if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
287 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
288 workplace->move(*drag_get_source_widget(context), i*32, j*32);
292 // El Drag es desde la barra de tareas
293 if ((selection_data->length >= 0) && (selection_data->format == 8)) {
295 if (strcmp((const char *)selection_data->data, "codo_o.png")==0)
297 else if (strcmp((const char *)selection_data->data, "canio_n.png")==0)
299 else if (strcmp((const char *)selection_data->data, "y_n.png")==0)
301 else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0)
303 else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0)
305 else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0)
307 else if (strcmp((const char *)selection_data->data, "and_e.png")==0)
309 else if (strcmp((const char *)selection_data->data, "or_e.png")==0)
311 else if (strcmp((const char *)selection_data->data, "not_e.png")==0)
315 sprintf(char_id," %d",id);
317 a->set_name( a->get_name()+char_id );
318 if ( can_drop(a, i*32, j*32) ) {
319 workplace->put(*a, i*32, j*32);
320 //Apunto al workplace
321 a->workplace= workplace;
323 a->combo_entry = combo_entry;
324 //Apunto a la listaItems.
325 a->listaItems = &listaItems;
326 //Seteo la posicion del item
327 a->set_position(i*32,j*32);
328 // Seteo la lista de tipos de drags
329 a->drag_source_set(listTargets);
330 // Conecto las señales
331 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
332 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
333 // parametro extra, en este caso un CItem *. Esto se hace para
334 // que cuando el usuario quiera mover un item, saber que item es
335 // y pedirle su ícono para mostrar cono icono durante la operacion,
336 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
337 // raro se vea el widget tal cual.
338 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
340 listaItems.push_back(a);
346 context->drag_finish(false, false, time);
350 void Constructor::on_btn_check_clicked()
352 std::list<CItem *>::iterator i = listaItems.begin();
353 if ( !listaItems.empty() ) {
354 while ( i != listaItems.end() ) {
356 if ( !temp->check_connection() ) {
357 dlg_connect->set_title("Error");
358 dlg_label->set_text("Los elementos no estan conectados\n\t\tcorrectamente");
364 dlg_connect->set_title("Conexion");
365 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");