]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blob - viewer/main.cpp
Paso el block_data a BTree en lugar de Clave.
[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
219                 /* Muestro la clave agregada */
220                 BTreeFindResult* result = 0;
221                 Clave *c = NULL;
222                 if (tree->type() == BTree::KEY_FIXED) {
223                         c = new ClaveFija (atoi(str_key.c_str()), 0);
224                         result = tree->FindKey(*c);
225                 } else {
226                         c = new ClaveVariable (str_key, 0);
227                         result = tree->FindKey(*c);
228                 }
229                 if (result) {
230                         tree->Clear ();
231                         tree->AddNode(result->node);
232                         tree->HighliteKey (*c);
233                         delete result;
234                 } else {
235                         delete tree->last_selected;
236                         tree->last_selected = NULL;
237                         tree->AddNode (0);
238                 }
239                 if (c) delete c;
240                 real_canvas->scroll_to (0, 0);
241         }
242 }
243
244 void borrar_clave ()
245 {
246         if (!tree)
247         {
248                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
249                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
250                 d.run();
251                 return;
252         }
253         KeyDialog d("Borrar");
254         if (d.run () == Gtk::RESPONSE_OK)
255         {
256                 Glib::ustring str_key = d.key();
257                 if (tree->type() == BTree::KEY_FIXED)
258                 {
259                         ClaveFija c(atoi(str_key.c_str()), 0);
260                         tree->DelKey(c);
261                 }
262                 else
263                 {
264                         ClaveVariable c(str_key, 0);
265                         tree->DelKey(c);
266                 }
267                 delete tree->last_selected;
268                 tree->AddNode (0);
269         }
270 }
271
272 void ir_al_padre ()
273 {
274         if (!tree)
275         {
276                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
277                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
278                 d.run();
279                 return;
280         }
281         tree->GoBack ();
282 }
283
284 void buscar_clave ()
285 {
286         if (!tree)
287         {
288                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
289                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
290                 d.run();
291                 return;
292         }
293         KeyDialog d("Buscar");
294         while (true) // Repite hasta que se encuentre algo o se cancele
295         {
296                 if (d.run () == Gtk::RESPONSE_OK)
297                 {
298                         BTreeFindResult* result = 0;
299                         Clave *c = NULL;
300                         Glib::ustring str_key = d.key();
301                         if (tree->type() == BTree::KEY_FIXED)
302                         {
303                                 c = new ClaveFija (atoi(str_key.c_str()), 0);
304                                 result = tree->FindKey(*c);
305                         }
306                         else
307                         {
308                                 c = new ClaveVariable (str_key, 0);
309                                 result = tree->FindKey(*c);
310                         }
311                         if (result)
312                         {
313                                 tree->Clear ();
314                                 tree->AddNode(result->node);
315                                 tree->HighliteKey (*c);
316                                 delete result;
317                                 delete c;
318                                 real_canvas->scroll_to (0, 0);
319                                 return; // Encontramos, salimos
320                         }
321                         if (c) delete c;
322                         Gtk::MessageDialog msg("Clave no encontrada!", false,
323                                         Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
324                         msg.run();
325                         // Seguimos intentando
326                 }
327                 else return; // Cancelaron, salimos
328         }
329 }
330
331 void zoom_out ()
332 {
333         double r = real_canvas->get_pixels_per_unit ();
334         r *= 0.9f;
335         if (r < 0.0001)
336                 r = 0.0001;
337         real_canvas->set_pixels_per_unit (r);
338 }
339
340 void zoom_in ()
341 {
342         double r = real_canvas->get_pixels_per_unit ();
343         r *= 1.1f;
344         if (r > 10)
345                 r = 10;
346         real_canvas->set_pixels_per_unit (r);
347 }
348
349 void zoom_normal ()
350 {
351         real_canvas->set_pixels_per_unit (1.0f);
352 }
353