5 #include <libgnomecanvasmm.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"
14 using namespace Gnome::Canvas;
16 Glib::ustring ui_info =
18 " <menubar name='MenuBar'>"
19 " <menu action='MenuFile'>"
20 " <menuitem action='Nuevo'/>"
22 " <menuitem action='Salir'/>"
24 " <menu action='MenuKey'>"
25 " <menuitem action='Agregar Clave'/>"
26 " <menuitem action='Borrar Clave'/>"
27 " <menuitem action='Buscar Clave'/>"
29 " <menu action='MenuZoom'>"
30 " <menuitem action='Alejar'/>"
31 " <menuitem action='Acercar'/>"
33 " <menuitem action='100 %'/>"
39 void agregar_clave ();
46 Glib::RefPtr<ViewBTree> tree;
47 Gnome::Canvas::Canvas *real_canvas;
48 ViewProperties *real_frame;
50 int main(int argc, char *argv[])
52 Gtk::Main kit(argc, argv);
54 Gnome::Canvas::init ();
60 Gtk::ScrolledWindow area;
61 Gnome::Canvas::Canvas canvas;
64 real_canvas = &canvas;
67 canvas.set_scroll_region (0, 0, 100, 100);
70 hbox.pack_start (area);
71 hbox.pack_start (frame, false, false, 10);
72 frame.set_size_request (200, 200);
74 Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
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);
88 Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
89 m_refUIManager->insert_action_group (actiongroup);
91 m_refUIManager->add_ui_from_string(ui_info);
92 Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
95 vbox.pack_start (*menubar, false, true, 0);
96 vbox.pack_end (hbox, true, true, 5);
98 window.add_accel_group (m_refUIManager->get_accel_group ());
100 window.set_size_request (640, 480);
103 /* Conecto el Canvas con el Frame */
104 Gtk::Main::run(window);
111 real_canvas->set_scroll_region (0, 0, 5000, 5000);
114 if (d.run () == Gtk::RESPONSE_OK) {
115 uint altas = d.getAdds ();
116 uint bajas = d.getDels ();
118 double paltas = bajas / (double)altas;
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) {
126 std::list<int>::iterator it;
128 Random::Ints (lst, altas);
132 while (it != lst.end ()) {
135 double l = Random::Double (0.0f, 1.0f);
136 std::cout << l << " >= " << paltas << std::endl;
140 } catch (Exception *e) {
141 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
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 ();
150 while (j < aborrar) {
154 ClaveFija c(*otro, 0);
158 std::cout << "Clave Borrada " << sss << std::endl;
164 std::list<std::string> lst;
165 std::list<std::string>::iterator it;
167 Random::Strings (lst, altas);
170 while (it != lst.end ()) {
171 ClaveVariable c(*it, 0);
175 } catch (Exception *e) {
176 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
182 double x1, x2, y1, y2;
183 tree->get_bounds (x1, y1, x2, y2);
184 real_canvas->scroll_to (0, 0);
188 void agregar_clave ()
192 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
193 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
197 KeyDialog d("Agregar");
198 if (d.run () == Gtk::RESPONSE_OK)
200 Glib::ustring str_key = d.key();
201 if (tree->type() == BTree::KEY_FIXED)
203 ClaveFija c(atoi(str_key.c_str()), 0);
208 ClaveVariable c(str_key, 0);
211 delete tree->last_selected;
213 real_canvas->scroll_to (0, 0);
221 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
222 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
226 KeyDialog d("Borrar");
227 if (d.run () == Gtk::RESPONSE_OK)
229 Glib::ustring str_key = d.key();
230 if (tree->type() == BTree::KEY_FIXED)
232 ClaveFija c(atoi(str_key.c_str()), 0);
237 ClaveVariable c(str_key, 0);
240 delete tree->last_selected;
242 real_canvas->scroll_to (0, 0);
250 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
251 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
255 KeyDialog d("Buscar");
256 while (true) // Repite hasta que se encuentre algo o se cancele
258 if (d.run () == Gtk::RESPONSE_OK)
260 BTreeFindResult* result = 0;
262 Glib::ustring str_key = d.key();
263 if (tree->type() == BTree::KEY_FIXED)
265 c = new ClaveFija (atoi(str_key.c_str()), 0);
266 result = tree->FindKey(*c);
270 c = new ClaveVariable (str_key, 0);
271 result = tree->FindKey(*c);
276 tree->AddNode(result->node);
277 tree->HighliteKey (*c);
280 real_canvas->scroll_to (0, 0);
281 return; // Encontramos, salimos
284 Gtk::MessageDialog msg("Clave no encontrada!", false,
285 Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
287 // Seguimos intentando
289 else return; // Cancelaron, salimos
295 double r = real_canvas->get_pixels_per_unit ();
299 real_canvas->set_pixels_per_unit (r);
304 double r = real_canvas->get_pixels_per_unit ();
308 real_canvas->set_pixels_per_unit (r);
313 real_canvas->set_pixels_per_unit (1.0f);