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