]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blob - viewer/main.cpp
Fix.
[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 "view_debug.h"
12
13 using namespace Gnome::Canvas;
14
15  Glib::ustring ui_info =
16 "<ui>"
17 "  <menubar name='MenuBar'>"
18 "    <menu action='MenuFile'>"
19 "      <menuitem action='Nuevo'/>"
20 "      <separator/>"
21 "      <menuitem action='Salir'/>"
22 "    </menu>"
23 "   <menu action='MenuZoom'>"
24 "      <menuitem action='Alejar'/>"
25 "      <menuitem action='Acercar'/>"
26 "      <separator/>"
27 "      <menuitem action='100 %'/>"
28 "    </menu>"
29 "  </menubar>"
30 "</ui>";
31
32 void nuevo_arbol ();
33 void zoom_out ();
34 void zoom_in ();
35 void zoom_normal ();
36
37 Glib::RefPtr<ViewBTree> tree;
38 ViewDebug *vdebug;
39 Gnome::Canvas::Canvas *real_canvas;
40 ViewProperties *real_frame;
41
42 int main(int argc, char *argv[])
43 {
44         Gtk::Main kit(argc, argv);
45
46         Gnome::Canvas::init ();
47
48         Gtk::Window window;
49         Gtk::HBox hbox;
50         Gtk::VBox vbox;
51
52         Gtk::ScrolledWindow area;
53         Gnome::Canvas::Canvas canvas;
54         ViewProperties frame;
55         ViewDebug debug;
56
57         real_canvas = &canvas;
58         real_frame = &frame;
59         vdebug = &debug;
60
61         canvas.set_scroll_region (0, 0, 100, 100);
62         area.add (canvas);
63
64         hbox.pack_start (frame, false, false, 10);
65         hbox.pack_start (area);
66         hbox.pack_end (debug, false, true, 10);
67
68         Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
69
70         actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
71         actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
72         actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), &Gtk::Main::quit);
73         actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
74         actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("<control>z"), &zoom_out );
75         actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("<control>x"), &zoom_in);
76         actiongroup->add( Gtk::Action::create("100 %", Gtk::Stock::ZOOM_100), Gtk::AccelKey ("<control>1"), &zoom_normal);
77
78         Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
79         m_refUIManager->insert_action_group (actiongroup);
80
81         m_refUIManager->add_ui_from_string(ui_info);
82         Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
83         menubar->show_all ();
84
85         vbox.pack_start (*menubar, false, true, 0);
86         vbox.pack_end (hbox, true, true, 5);
87
88         window.add_accel_group (m_refUIManager->get_accel_group ());
89         window.add (vbox);
90         window.set_size_request (640, 480);
91         window.show_all ();
92
93         /* Conecto el Canvas con el Frame */
94         Gtk::Main::run(window);
95                                                             
96         return 0;
97 }
98
99 void nuevo_arbol ()
100 {
101         real_canvas->set_scroll_region (0, 0, 5000, 5000);
102
103         NewTreeDialog d;
104         if (d.run () == Gtk::RESPONSE_OK) {
105                 uint altas = d.getAdds ();
106                 uint bajas = d.getDels ();
107
108                 double paltas = bajas / (double)altas;
109
110                 int type = d.getKeyType ();
111                 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), type));
112                 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
113                 vdebug->SetTree (tree);
114                 if (type == BTree::KEY_FIXED) {
115                         std::list<int> lst;
116                         std::list<int>::iterator it;
117                         Random::Init ();
118                         Random::Ints (lst, altas);
119
120                         it = lst.begin ();
121                         uint i = 0;
122                         while (it != lst.end ()) {
123                                 ClaveFija c(*it);
124
125                                 double l = Random::Double (0.0f, 1.0f);
126                                 std::cout << l << " >= " << paltas << std::endl;
127                                 if (l >= paltas) {
128                                         tree->AddKey (c);
129                                         i++;
130                                         vdebug->AddKey (c);
131                                 } else {
132                                         /* Tengo que borrar una clave entre 0 e "i" de la lista
133                                          * porque son las que ya agregue. */
134                                         int aborrar = (int)Random::Double (0, i);
135                                         std::list<int>::iterator otro = lst.begin ();
136                                         int j = 0;
137                                         while (j < aborrar) {
138                                                 otro++;
139                                                 j++;
140                                         }
141                                         ClaveFija c(*otro);
142
143                                         tree->DelKey (c);
144                                         std::string sss = c;
145                                         std::cout << "Clave Borrada " << sss << std::endl;
146                                 }
147
148                                 it++;
149                         }
150                 } else {
151                         std::list<std::string> lst;
152                         std::list<std::string>::iterator it;
153                         Random::Init ();
154                         Random::Strings (lst, altas);
155
156                         it = lst.begin ();
157                         while (it != lst.end ()) {
158                                 ClaveVariable c(*it);
159
160                                 tree->AddKey (c);
161                                 vdebug->AddKey (c);
162                                 it++;
163                         }
164                 }
165                 tree->AddNode (0);
166                 double x1, x2, y1, y2;
167                 tree->get_bounds (x1, y1, x2, y2);
168         }
169 }
170
171 void zoom_out ()
172 {
173         double r = real_canvas->get_pixels_per_unit ();
174         r *= 0.9f;
175         if (r < 0.0001)
176                 r = 0.0001;
177         real_canvas->set_pixels_per_unit (r);
178 }
179
180 void zoom_in ()
181 {
182         double r = real_canvas->get_pixels_per_unit ();
183         r *= 1.1f;
184         if (r > 10)
185                 r = 10;
186         real_canvas->set_pixels_per_unit (r);
187 }
188
189 void zoom_normal ()
190 {
191         real_canvas->set_pixels_per_unit (1.0f);
192 }
193