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='MenuNode'>"
25 " <menuitem action='Ir al Padre'/>"
27 " <menu action='MenuKey'>"
28 " <menuitem action='Agregar Clave'/>"
29 " <menuitem action='Borrar Clave'/>"
30 " <menuitem action='Buscar Clave'/>"
32 " <menu action='MenuZoom'>"
33 " <menuitem action='Alejar'/>"
34 " <menuitem action='Acercar'/>"
36 " <menuitem action='100 %'/>"
42 void agregar_clave ();
50 Glib::RefPtr<ViewBTree> tree;
51 Gnome::Canvas::Canvas *real_canvas;
52 ViewProperties *real_frame;
54 int main(int argc, char *argv[])
56 Gtk::Main kit(argc, argv);
58 Gnome::Canvas::init ();
64 Gtk::ScrolledWindow area;
65 Gnome::Canvas::Canvas canvas;
68 real_canvas = &canvas;
71 canvas.set_scroll_region (0, 0, 100, 100);
74 hbox.pack_start (area);
75 hbox.pack_start (frame, false, false, 10);
76 frame.set_size_request (200, 200);
78 Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
80 actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
81 actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
82 actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), Gtk::AccelKey ("<control>q"), &Gtk::Main::quit);
83 actiongroup->add( Gtk::Action::create("MenuNode", "_Nodo") );
84 actiongroup->add( Gtk::Action::create("Ir al Padre", Gtk::Stock::GO_BACK), Gtk::AccelKey ("<control>b"), &ir_al_padre);
85 actiongroup->add( Gtk::Action::create("MenuKey", "_Clave") );
86 actiongroup->add( Gtk::Action::create("Agregar Clave", Gtk::Stock::ADD), Gtk::AccelKey ("<control>a"), &agregar_clave);
87 actiongroup->add( Gtk::Action::create("Borrar Clave", Gtk::Stock::REMOVE), Gtk::AccelKey ("<control>d"), &borrar_clave);
88 actiongroup->add( Gtk::Action::create("Buscar Clave", Gtk::Stock::FIND), Gtk::AccelKey ("<control>f"), &buscar_clave);
89 actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
90 actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("<control>z"), &zoom_out );
91 actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("<control>x"), &zoom_in);
92 actiongroup->add( Gtk::Action::create("100 %", Gtk::Stock::ZOOM_100), Gtk::AccelKey ("<control>1"), &zoom_normal);
94 Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
95 m_refUIManager->insert_action_group (actiongroup);
97 m_refUIManager->add_ui_from_string(ui_info);
98 Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
101 vbox.pack_start (*menubar, false, true, 0);
102 vbox.pack_end (hbox, true, true, 5);
104 window.add_accel_group (m_refUIManager->get_accel_group ());
106 window.set_size_request (640, 480);
109 /* Conecto el Canvas con el Frame */
110 Gtk::Main::run(window);
117 real_canvas->set_scroll_region (0, 0, 5000, 5000);
120 if (d.run () == Gtk::RESPONSE_OK) {
121 uint altas = d.getAdds ();
122 uint bajas = d.getDels ();
124 double paltas = bajas / (double)altas;
126 int type = d.getKeyType ();
127 int atype = d.getTreeType ();
128 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), atype, type));
129 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
130 if (type == BTree::KEY_FIXED) {
132 std::list<int>::iterator it;
134 Random::Ints (lst, altas);
138 while (it != lst.end ()) {
141 double l = Random::Double (0.0f, 1.0f);
142 std::cout << l << " >= " << paltas << std::endl;
146 } catch (Exception *e) {
147 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
151 /* Tengo que borrar una clave entre 0 e "i" de la lista
152 * porque son las que ya agregue. */
153 int aborrar = (int)Random::Double (0, i);
154 std::list<int>::iterator otro = lst.begin ();
156 while (j < aborrar) {
160 ClaveFija c(*otro, 0);
164 std::cout << "Clave Borrada " << sss << std::endl;
170 std::list<std::string> lst;
171 std::list<std::string>::iterator it;
173 Random::Strings (lst, altas);
176 while (it != lst.end ()) {
177 ClaveVariable c(*it, 0);
181 } catch (Exception *e) {
182 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
188 double x1, x2, y1, y2;
189 tree->get_bounds (x1, y1, x2, y2);
190 real_canvas->scroll_to (0, 0);
194 void agregar_clave ()
198 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
199 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
203 KeyDialog d("Agregar", true);
204 if (d.run () == Gtk::RESPONSE_OK)
206 Glib::ustring str_key = d.key();
207 Glib::ustring str_val = d.val();
208 if (tree->type() == BTree::KEY_FIXED)
210 ClaveFija c(atoi(str_key.c_str()), atoi(str_val.c_str()));
215 ClaveVariable c(str_key, atoi(str_val.c_str()));
219 /* Muestro la clave agregada */
220 BTreeFindResult* result = 0;
222 if (tree->type() == BTree::KEY_FIXED) {
223 c = new ClaveFija (atoi(str_key.c_str()), 0);
224 result = tree->FindKey(*c);
226 c = new ClaveVariable (str_key, 0);
227 result = tree->FindKey(*c);
231 tree->AddNode(result->node);
232 tree->HighliteKey (*c);
235 delete tree->last_selected;
236 tree->last_selected = NULL;
240 real_canvas->scroll_to (0, 0);
248 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
249 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
253 KeyDialog d("Borrar");
254 if (d.run () == Gtk::RESPONSE_OK)
256 Glib::ustring str_key = d.key();
257 if (tree->type() == BTree::KEY_FIXED)
259 ClaveFija c(atoi(str_key.c_str()), 0);
264 ClaveVariable c(str_key, 0);
267 delete tree->last_selected;
276 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
277 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
288 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
289 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
293 KeyDialog d("Buscar");
294 while (true) // Repite hasta que se encuentre algo o se cancele
296 if (d.run () == Gtk::RESPONSE_OK)
298 BTreeFindResult* result = 0;
300 Glib::ustring str_key = d.key();
301 if (tree->type() == BTree::KEY_FIXED)
303 c = new ClaveFija (atoi(str_key.c_str()), 0);
304 result = tree->FindKey(*c);
308 c = new ClaveVariable (str_key, 0);
309 result = tree->FindKey(*c);
314 tree->AddNode(result->node);
315 tree->HighliteKey (*c);
318 real_canvas->scroll_to (0, 0);
319 return; // Encontramos, salimos
322 Gtk::MessageDialog msg("Clave no encontrada!", false,
323 Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
325 // Seguimos intentando
327 else return; // Cancelaron, salimos
333 double r = real_canvas->get_pixels_per_unit ();
337 real_canvas->set_pixels_per_unit (r);
342 double r = real_canvas->get_pixels_per_unit ();
346 real_canvas->set_pixels_per_unit (r);
351 real_canvas->set_pixels_per_unit (1.0f);