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