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