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