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'/>"
21 " <menuitem action='Abrir'/>"
23 " <menuitem action='Salir'/>"
25 " <menu action='MenuNode'>"
26 " <menuitem action='Ir al Padre'/>"
28 " <menu action='MenuKey'>"
29 " <menuitem action='Agregar Clave'/>"
30 " <menuitem action='Borrar Clave'/>"
31 " <menuitem action='Buscar Clave'/>"
33 " <menu action='MenuZoom'>"
34 " <menuitem action='Alejar'/>"
35 " <menuitem action='Acercar'/>"
37 " <menuitem action='100 %'/>"
44 void agregar_clave ();
52 Glib::RefPtr<ViewBTree> tree;
53 Gnome::Canvas::Canvas *real_canvas;
54 ViewProperties *real_frame;
56 int main(int argc, char *argv[])
58 Gtk::Main kit(argc, argv);
60 Gnome::Canvas::init ();
66 Gtk::ScrolledWindow area;
67 Gnome::Canvas::Canvas canvas;
70 real_canvas = &canvas;
73 canvas.set_scroll_region (0, 0, 100, 100);
76 hbox.pack_start (area);
77 hbox.pack_start (frame, false, false, 10);
78 frame.set_size_request (200, 200);
80 Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
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);
97 Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
98 m_refUIManager->insert_action_group (actiongroup);
100 m_refUIManager->add_ui_from_string(ui_info);
101 Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
102 menubar->show_all ();
104 vbox.pack_start (*menubar, false, true, 0);
105 vbox.pack_end (hbox, true, true, 5);
107 window.add_accel_group (m_refUIManager->get_accel_group ());
109 window.set_size_request (640, 480);
112 /* Conecto el Canvas con el Frame */
113 Gtk::Main::run(window);
120 Gtk::FileChooserDialog dlg ("Abrir Arbol");
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);
129 tree = Glib::RefPtr<ViewBTree>(ptree);
130 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
138 real_canvas->set_scroll_region (0, 0, 5000, 5000);
141 if (d.run () == Gtk::RESPONSE_OK) {
142 uint altas = d.getAdds ();
143 uint bajas = d.getDels ();
145 double paltas = bajas / (double)altas;
147 int type = d.getKeyType ();
148 int atype = d.getTreeType ();
149 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), atype, type));
150 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
151 if (type == BTree::KEY_FIXED) {
153 std::list<int>::iterator it;
155 Random::Ints (lst, altas);
159 while (it != lst.end ()) {
162 double l = Random::Double (0.0f, 1.0f);
163 std::cout << l << " >= " << paltas << std::endl;
167 } catch (Exception *e) {
168 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
172 /* Tengo que borrar una clave entre 0 e "i" de la lista
173 * porque son las que ya agregue. */
174 int aborrar = (int)Random::Double (0, i);
175 std::list<int>::iterator otro = lst.begin ();
177 while (j < aborrar) {
185 std::cout << "Clave Borrada " << sss << std::endl;
191 std::list<std::string> lst;
192 std::list<std::string>::iterator it;
194 Random::Strings (lst, altas);
197 while (it != lst.end ()) {
198 ClaveVariable c(*it);
202 } catch (Exception *e) {
203 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
209 double x1, x2, y1, y2;
210 tree->get_bounds (x1, y1, x2, y2);
211 real_canvas->scroll_to (0, 0);
215 void agregar_clave ()
219 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
220 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
224 KeyDialog d("Agregar");
225 if (d.run () == Gtk::RESPONSE_OK)
227 Glib::ustring str_key = d.key();
228 Glib::ustring str_val = d.val();
229 if (tree->type() == BTree::KEY_FIXED)
231 ClaveFija c(atoi(str_key.c_str()));
236 ClaveVariable c(str_key);
240 /* Muestro la clave agregada */
241 BTreeFindResult* result = 0;
243 if (tree->type() == BTree::KEY_FIXED) {
244 c = new ClaveFija (atoi(str_key.c_str()));
245 result = tree->FindKey(*c);
247 c = new ClaveVariable (str_key);
248 result = tree->FindKey(*c);
252 tree->AddNode(result->node);
253 tree->HighliteKey (*c);
256 delete tree->last_selected;
257 tree->last_selected = NULL;
261 real_canvas->scroll_to (0, 0);
269 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
270 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
274 KeyDialog d("Borrar");
275 if (d.run () == Gtk::RESPONSE_OK)
277 Glib::ustring str_key = d.key();
278 if (tree->type() == BTree::KEY_FIXED)
280 ClaveFija c(atoi(str_key.c_str()));
285 ClaveVariable c(str_key);
288 delete tree->last_selected;
297 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
298 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
309 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
310 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
314 KeyDialog d("Buscar");
315 while (true) // Repite hasta que se encuentre algo o se cancele
317 if (d.run () == Gtk::RESPONSE_OK)
319 BTreeFindResult* result = 0;
321 Glib::ustring str_key = d.key();
322 if (tree->type() == BTree::KEY_FIXED)
324 c = new ClaveFija (atoi(str_key.c_str()));
325 result = tree->FindKey(*c);
329 c = new ClaveVariable (str_key);
330 result = tree->FindKey(*c);
335 tree->AddNode(result->node);
336 tree->HighliteKey (*c);
339 real_canvas->scroll_to (0, 0);
340 return; // Encontramos, salimos
343 Gtk::MessageDialog msg("Clave no encontrada!", false,
344 Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
346 // Seguimos intentando
348 else return; // Cancelaron, salimos
354 double r = real_canvas->get_pixels_per_unit ();
358 real_canvas->set_pixels_per_unit (r);
363 double r = real_canvas->get_pixels_per_unit ();
367 real_canvas->set_pixels_per_unit (r);
372 real_canvas->set_pixels_per_unit (1.0f);