]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blob - viewer/main.cpp
Agrego nombre de archivo.
[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                         double x1, x2, y1, y2;
133                         tree->get_bounds (x1, y1, x2, y2);
134                         real_canvas->scroll_to (0, 0);
135                 }
136         }
137 }
138
139 void nuevo_arbol ()
140 {
141         real_canvas->set_scroll_region (0, 0, 5000, 5000);
142
143         NewTreeDialog d;
144         if (d.run () == Gtk::RESPONSE_OK) {
145                 uint altas = d.getAdds ();
146                 uint bajas = d.getDels ();
147
148                 double paltas = bajas / (double)altas;
149
150                 int type = d.getKeyType ();
151                 int atype = d.getTreeType ();
152                 std::string name = d.getName ();
153                 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), name+".idx", d.getBlockSize (), atype, type));
154                 real_frame->SetTree (tree);
155                 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
156                 if (type == BTree::KEY_FIXED) {
157                         std::list<int> lst;
158                         std::list<int>::iterator it;
159                         Random::Init ();
160                         Random::Ints (lst, altas);
161
162                         it = lst.begin ();
163                         uint i = 0;
164                         while (it != lst.end ()) {
165                                 ClaveFija c(*it);
166
167                                 double l = Random::Double (0.0f, 1.0f);
168                                 std::cout << l << " >= " << paltas << std::endl;
169                                 if (l >= paltas) {
170                                         try {
171                                                 tree->AddKey (c);
172                                         } catch (Exception *e) {
173                                                 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
174                                         }
175                                         i++;
176                                 } else {
177                                         /* Tengo que borrar una clave entre 0 e "i" de la lista
178                                          * porque son las que ya agregue. */
179                                         int aborrar = (int)Random::Double (0, i);
180                                         std::list<int>::iterator otro = lst.begin ();
181                                         int j = 0;
182                                         while (j < aborrar) {
183                                                 otro++;
184                                                 j++;
185                                         }
186                                         ClaveFija c(*otro);
187
188                                         tree->DelKey (c);
189                                         std::string sss = c;
190                                         std::cout << "Clave Borrada " << sss << std::endl;
191                                 }
192
193                                 it++;
194                         }
195                 } else {
196                         std::list<std::string> lst;
197                         std::list<std::string>::iterator it;
198                         Random::Init ();
199                         Random::Strings (lst, altas);
200
201                         it = lst.begin ();
202                         while (it != lst.end ()) {
203                                 ClaveVariable c(*it);
204
205                                 try {
206                                         tree->AddKey (c);
207                                 } catch (Exception *e) {
208                                         std::cout << "====== " << (std::string)c << e->Message () << std::endl;
209                                 }
210                                 it++;
211                         }
212                 }
213                 tree->AddNode (0);
214                 double x1, x2, y1, y2;
215                 tree->get_bounds (x1, y1, x2, y2);
216                 real_canvas->scroll_to (0, 0);
217         }
218 }
219
220 void agregar_clave ()
221 {
222         if (!tree)
223         {
224                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
225                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
226                 d.run();
227                 return;
228         }
229         KeyDialog d("Agregar");
230         if (d.run () == Gtk::RESPONSE_OK)
231         {
232                 Glib::ustring str_key = d.key();
233                 Glib::ustring str_val = d.val();
234                 if (tree->type() == BTree::KEY_FIXED)
235                 {
236                         ClaveFija c(atoi(str_key.c_str()));
237                         tree->AddKey(c);
238                 }
239                 else
240                 {
241                         ClaveVariable c(str_key);
242                         tree->AddKey(c);
243                 }
244
245                 /* Muestro la clave agregada */
246                 BTreeFindResult* result = 0;
247                 Clave *c = NULL;
248                 if (tree->type() == BTree::KEY_FIXED) {
249                         c = new ClaveFija (atoi(str_key.c_str()));
250                         result = tree->FindKey(*c);
251                 } else {
252                         c = new ClaveVariable (str_key);
253                         result = tree->FindKey(*c);
254                 }
255                 if (result) {
256                         tree->Clear ();
257                         tree->AddNode(result->node);
258                         tree->HighliteKey (*c);
259                         delete result;
260                 } else {
261                         delete tree->last_selected;
262                         tree->last_selected = NULL;
263                         tree->AddNode (0);
264                 }
265                 if (c) delete c;
266                 real_canvas->scroll_to (0, 0);
267         }
268 }
269
270 void borrar_clave ()
271 {
272         if (!tree)
273         {
274                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
275                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
276                 d.run();
277                 return;
278         }
279         KeyDialog d("Borrar");
280         if (d.run () == Gtk::RESPONSE_OK)
281         {
282                 Glib::ustring str_key = d.key();
283                 if (tree->type() == BTree::KEY_FIXED)
284                 {
285                         ClaveFija c(atoi(str_key.c_str()));
286                         tree->DelKey(c);
287                 }
288                 else
289                 {
290                         ClaveVariable c(str_key);
291                         tree->DelKey(c);
292                 }
293                 delete tree->last_selected;
294                 tree->AddNode (0);
295         }
296 }
297
298 void ir_al_padre ()
299 {
300         if (!tree)
301         {
302                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
303                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
304                 d.run();
305                 return;
306         }
307         tree->GoBack ();
308 }
309
310 void buscar_clave ()
311 {
312         if (!tree)
313         {
314                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
315                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
316                 d.run();
317                 return;
318         }
319         KeyDialog d("Buscar");
320         while (true) // Repite hasta que se encuentre algo o se cancele
321         {
322                 if (d.run () == Gtk::RESPONSE_OK)
323                 {
324                         BTreeFindResult* result = 0;
325                         Clave *c = NULL;
326                         Glib::ustring str_key = d.key();
327                         if (tree->type() == BTree::KEY_FIXED)
328                         {
329                                 c = new ClaveFija (atoi(str_key.c_str()));
330                                 result = tree->FindKey(*c);
331                         }
332                         else
333                         {
334                                 c = new ClaveVariable (str_key);
335                                 result = tree->FindKey(*c);
336                         }
337                         if (result)
338                         {
339                                 tree->Clear ();
340                                 tree->AddNode(result->node);
341                                 tree->HighliteKey (*c);
342                                 delete result;
343                                 delete c;
344                                 real_canvas->scroll_to (0, 0);
345                                 return; // Encontramos, salimos
346                         }
347                         if (c) delete c;
348                         Gtk::MessageDialog msg("Clave no encontrada!", false,
349                                         Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
350                         msg.run();
351                         // Seguimos intentando
352                 }
353                 else return; // Cancelaron, salimos
354         }
355 }
356
357 void zoom_out ()
358 {
359         double r = real_canvas->get_pixels_per_unit ();
360         r *= 0.9f;
361         if (r < 0.0001)
362                 r = 0.0001;
363         real_canvas->set_pixels_per_unit (r);
364 }
365
366 void zoom_in ()
367 {
368         double r = real_canvas->get_pixels_per_unit ();
369         r *= 1.1f;
370         if (r > 10)
371                 r = 10;
372         real_canvas->set_pixels_per_unit (r);
373 }
374
375 void zoom_normal ()
376 {
377         real_canvas->set_pixels_per_unit (1.0f);
378 }
379