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");
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);
33 refGlade->get_widget_derived("workplace", workplace); //fixed
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") );
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);
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));
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));
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));
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));
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;
81 Constructor::~Constructor()
83 std::list<CItem *>::iterator i = listaItems.begin();
84 std::cout << "ok" << std::endl;
85 while ( i != listaItems.end() ){
88 std::cout << "Elimnando ... " << std::endl;
90 i = listaItems.begin();
94 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
96 gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"canio_n.png", 14);
99 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
101 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"y_n.png",10);
104 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
106 gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"codo_o.png",13);
109 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
111 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"tanque_e.png",15);
114 void Constructor::on_btn_bomba_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
116 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"bomba_e.png",15);
119 void Constructor::on_btn_exclusa_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
121 gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"exclusa_h.png",15);
124 void Constructor::on_main_menu_quit()
131 void Constructor::on_main_menu_save()
133 file_selection->show();
136 void Constructor::on_edit_menu_del()
138 // hay que meter algo aca.
141 void Constructor::on_chk_btn_clicked()
146 void Constructor::on_btn_file_ok_clicked()
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() ){
157 fprintf(archivo,"</planta>\n");
159 std::cout<<"NO SE ABRIO EL ARCHIVO"<<std::endl;
161 file_selection->hide();
164 void Constructor::on_btn_file_cancel_clicked()
166 file_selection->hide();
169 void Constructor::on_btn_dlg_connect_clicked()
174 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
176 context->set_icon(ico_canio, 5, 5);
179 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
181 context->set_icon(ico_y, 5, 5);
184 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
186 context->set_icon(ico_codo, 5, 5);
189 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
191 context->set_icon(ico_tanque, 5, 5);
194 void Constructor::on_bomba_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
196 context->set_icon(ico_bomba, 5, 5);
199 void Constructor::on_exclusa_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
201 context->set_icon(ico_exclusa, 5, 5);
204 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
206 context->set_icon(item->get_image(), 5, 5);
209 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
211 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
214 bool Constructor::can_drop(CItem *item, int _x, int _y)
216 std::list<CItem*>::iterator i = listaItems.begin();
217 while( i != listaItems.end() ) {
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) ) )
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)
235 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
237 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
238 // separacion de 2 cuadritos
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);
249 // El Drag es desde la barra de tareas
250 if ((selection_data->length >= 0) && (selection_data->format == 8)) {
252 if (strcmp((const char *)selection_data->data, "codo_o.png")==0)
254 else if (strcmp((const char *)selection_data->data, "canio_n.png")==0)
256 else if (strcmp((const char *)selection_data->data, "y_n.png")==0)
258 else if (strcmp((const char *)selection_data->data, "tanque_e.png")==0)
260 else if (strcmp((const char *)selection_data->data, "bomba_e.png")==0)
262 else if (strcmp((const char *)selection_data->data, "exclusa_h.png")==0)
267 sprintf(char_id," %d",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;
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));
292 listaItems.push_back(a);
298 context->drag_finish(false, false, time);
302 void Constructor::on_btn_check_clicked()
304 std::list<CItem *>::iterator i = listaItems.begin();
305 if ( !listaItems.empty() ) {
306 while ( i != listaItems.end() ) {
308 if ( !temp->check_connection() ) {
309 dlg_connect->set_title("Error");
310 dlg_label->set_text("Los elementos no estan conectados\n\t\tcorrectamente");
316 dlg_connect->set_title("Conexion");
317 dlg_label->set_text("Los elementos estan conectados\n\t\tcorrectamente");