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