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