]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blob - viewer/main.cpp
Arreglo Stock Icon.
[z.facultad/75.52/treemulator.git] / viewer / main.cpp
1
2 #include <iostream>
3
4 #include <gtkmm.h>
5 #include <libgnomecanvasmm.h>
6
7 #include "random.h"
8 #include "view_btree.h"
9 #include "view_properties.h"
10 #include "new_tree_dialog.h"
11 #include "key_dialog.h"
12 #include "view_debug.h"
13
14 using namespace Gnome::Canvas;
15
16  Glib::ustring ui_info =
17 "<ui>"
18 "  <menubar name='MenuBar'>"
19 "    <menu action='MenuFile'>"
20 "      <menuitem action='Nuevo'/>"
21 "      <separator/>"
22 "      <menuitem action='Salir'/>"
23 "    </menu>"
24 "    <menu action='MenuNode'>"
25 "      <menuitem action='Ir al Padre'/>"
26 "    </menu>"
27 "    <menu action='MenuKey'>"
28 "      <menuitem action='Agregar Clave'/>"
29 "      <menuitem action='Borrar Clave'/>"
30 "      <menuitem action='Buscar Clave'/>"
31 "    </menu>"
32 "    <menu action='MenuZoom'>"
33 "      <menuitem action='Alejar'/>"
34 "      <menuitem action='Acercar'/>"
35 "      <separator/>"
36 "      <menuitem action='100 %'/>"
37 "    </menu>"
38 "  </menubar>"
39 "</ui>";
40
41 void nuevo_arbol ();
42 void agregar_clave ();
43 void borrar_clave ();
44 void ir_al_padre ();
45 void buscar_clave ();
46 void zoom_out ();
47 void zoom_in ();
48 void zoom_normal ();
49
50 Glib::RefPtr<ViewBTree> tree;
51 Gnome::Canvas::Canvas *real_canvas;
52 ViewProperties *real_frame;
53
54 int main(int argc, char *argv[])
55 {
56         Gtk::Main kit(argc, argv);
57
58         Gnome::Canvas::init ();
59
60         Gtk::Window window;
61         Gtk::VBox hbox;
62         Gtk::VBox vbox;
63
64         Gtk::ScrolledWindow area;
65         Gnome::Canvas::Canvas canvas;
66         ViewProperties frame;
67
68         real_canvas = &canvas;
69         real_frame = &frame;
70
71         canvas.set_scroll_region (0, 0, 100, 100);
72         area.add (canvas);
73
74         hbox.pack_start (area);
75         hbox.pack_start (frame, false, false, 10);
76         frame.set_size_request (200, 200);
77
78         Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
79
80         actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
81         actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
82         actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), Gtk::AccelKey ("<control>q"), &Gtk::Main::quit);
83         actiongroup->add( Gtk::Action::create("MenuNode", "_Nodo") );
84         actiongroup->add( Gtk::Action::create("Ir al Padre", Gtk::Stock::GO_BACK), Gtk::AccelKey ("<control>b"), &ir_al_padre);
85         actiongroup->add( Gtk::Action::create("MenuKey", "_Clave") );
86         actiongroup->add( Gtk::Action::create("Agregar Clave", Gtk::Stock::ADD), Gtk::AccelKey ("<control>a"), &agregar_clave);
87         actiongroup->add( Gtk::Action::create("Borrar Clave", Gtk::Stock::REMOVE), Gtk::AccelKey ("<control>d"), &borrar_clave);
88         actiongroup->add( Gtk::Action::create("Buscar Clave", Gtk::Stock::FIND), Gtk::AccelKey ("<control>f"), &buscar_clave);
89         actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
90         actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("<control>z"), &zoom_out );
91         actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("<control>x"), &zoom_in);
92         actiongroup->add( Gtk::Action::create("100 %", Gtk::Stock::ZOOM_100), Gtk::AccelKey ("<control>1"), &zoom_normal);
93
94         Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
95         m_refUIManager->insert_action_group (actiongroup);
96
97         m_refUIManager->add_ui_from_string(ui_info);
98         Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
99         menubar->show_all ();
100
101         vbox.pack_start (*menubar, false, true, 0);
102         vbox.pack_end (hbox, true, true, 5);
103
104         window.add_accel_group (m_refUIManager->get_accel_group ());
105         window.add (vbox);
106         window.set_size_request (640, 480);
107         window.show_all ();
108
109         /* Conecto el Canvas con el Frame */
110         Gtk::Main::run(window);
111                                                             
112         return 0;
113 }
114
115 void nuevo_arbol ()
116 {
117         real_canvas->set_scroll_region (0, 0, 5000, 5000);
118
119         NewTreeDialog d;
120         if (d.run () == Gtk::RESPONSE_OK) {
121                 uint altas = d.getAdds ();
122                 uint bajas = d.getDels ();
123
124                 double paltas = bajas / (double)altas;
125
126                 int type = d.getKeyType ();
127                 int atype = d.getTreeType ();
128                 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), atype, type));
129                 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
130                 if (type == BTree::KEY_FIXED) {
131                         std::list<int> lst;
132                         std::list<int>::iterator it;
133                         Random::Init ();
134                         Random::Ints (lst, altas);
135
136                         it = lst.begin ();
137                         uint i = 0;
138                         while (it != lst.end ()) {
139                                 ClaveFija c(*it, i);
140
141                                 double l = Random::Double (0.0f, 1.0f);
142                                 std::cout << l << " >= " << paltas << std::endl;
143                                 if (l >= paltas) {
144                                         try {
145                                                 tree->AddKey (c);
146                                         } catch (Exception *e) {
147                                                 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
148                                         }
149                                         i++;
150                                 } else {
151                                         /* Tengo que borrar una clave entre 0 e "i" de la lista
152                                          * porque son las que ya agregue. */
153                                         int aborrar = (int)Random::Double (0, i);
154                                         std::list<int>::iterator otro = lst.begin ();
155                                         int j = 0;
156                                         while (j < aborrar) {
157                                                 otro++;
158                                                 j++;
159                                         }
160                                         ClaveFija c(*otro, 0);
161
162                                         tree->DelKey (c);
163                                         std::string sss = c;
164                                         std::cout << "Clave Borrada " << sss << std::endl;
165                                 }
166
167                                 it++;
168                         }
169                 } else {
170                         std::list<std::string> lst;
171                         std::list<std::string>::iterator it;
172                         Random::Init ();
173                         Random::Strings (lst, altas);
174
175                         it = lst.begin ();
176                         while (it != lst.end ()) {
177                                 ClaveVariable c(*it, 0);
178
179                                 try {
180                                         tree->AddKey (c);
181                                 } catch (Exception *e) {
182                                         std::cout << "====== " << (std::string)c << e->Message () << std::endl;
183                                 }
184                                 it++;
185                         }
186                 }
187                 tree->AddNode (0);
188                 double x1, x2, y1, y2;
189                 tree->get_bounds (x1, y1, x2, y2);
190                 real_canvas->scroll_to (0, 0);
191         }
192 }
193
194 void agregar_clave ()
195 {
196         if (!tree)
197         {
198                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
199                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
200                 d.run();
201                 return;
202         }
203         KeyDialog d("Agregar", true);
204         if (d.run () == Gtk::RESPONSE_OK)
205         {
206                 Glib::ustring str_key = d.key();
207                 Glib::ustring str_val = d.val();
208                 if (tree->type() == BTree::KEY_FIXED)
209                 {
210                         ClaveFija c(atoi(str_key.c_str()), atoi(str_val.c_str()));
211                         tree->AddKey(c);
212                 }
213                 else
214                 {
215                         ClaveVariable c(str_key, atoi(str_val.c_str()));
216                         tree->AddKey(c);
217                 }
218                 delete tree->last_selected;
219                 tree->AddNode (0);
220                 real_canvas->scroll_to (0, 0);
221         }
222 }
223
224 void borrar_clave ()
225 {
226         if (!tree)
227         {
228                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
229                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
230                 d.run();
231                 return;
232         }
233         KeyDialog d("Borrar");
234         if (d.run () == Gtk::RESPONSE_OK)
235         {
236                 Glib::ustring str_key = d.key();
237                 if (tree->type() == BTree::KEY_FIXED)
238                 {
239                         ClaveFija c(atoi(str_key.c_str()), 0);
240                         tree->DelKey(c);
241                 }
242                 else
243                 {
244                         ClaveVariable c(str_key, 0);
245                         tree->DelKey(c);
246                 }
247                 delete tree->last_selected;
248                 tree->AddNode (0);
249                 real_canvas->scroll_to (0, 0);
250         }
251 }
252
253 void ir_al_padre ()
254 {
255         if (!tree)
256         {
257                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
258                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
259                 d.run();
260                 return;
261         }
262         tree->GoBack ();
263 }
264
265 void buscar_clave ()
266 {
267         if (!tree)
268         {
269                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
270                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
271                 d.run();
272                 return;
273         }
274         KeyDialog d("Buscar");
275         while (true) // Repite hasta que se encuentre algo o se cancele
276         {
277                 if (d.run () == Gtk::RESPONSE_OK)
278                 {
279                         BTreeFindResult* result = 0;
280                         Clave *c = NULL;
281                         Glib::ustring str_key = d.key();
282                         if (tree->type() == BTree::KEY_FIXED)
283                         {
284                                 c = new ClaveFija (atoi(str_key.c_str()), 0);
285                                 result = tree->FindKey(*c);
286                         }
287                         else
288                         {
289                                 c = new ClaveVariable (str_key, 0);
290                                 result = tree->FindKey(*c);
291                         }
292                         if (result)
293                         {
294                                 tree->Clear ();
295                                 tree->AddNode(result->node);
296                                 tree->HighliteKey (*c);
297                                 delete result;
298                                 delete c;
299                                 real_canvas->scroll_to (0, 0);
300                                 return; // Encontramos, salimos
301                         }
302                         if (c) delete c;
303                         Gtk::MessageDialog msg("Clave no encontrada!", false,
304                                         Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
305                         msg.run();
306                         // Seguimos intentando
307                 }
308                 else return; // Cancelaron, salimos
309         }
310 }
311
312 void zoom_out ()
313 {
314         double r = real_canvas->get_pixels_per_unit ();
315         r *= 0.9f;
316         if (r < 0.0001)
317                 r = 0.0001;
318         real_canvas->set_pixels_per_unit (r);
319 }
320
321 void zoom_in ()
322 {
323         double r = real_canvas->get_pixels_per_unit ();
324         r *= 1.1f;
325         if (r > 10)
326                 r = 10;
327         real_canvas->set_pixels_per_unit (r);
328 }
329
330 void zoom_normal ()
331 {
332         real_canvas->set_pixels_per_unit (1.0f);
333 }
334