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