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) );
132 double x1, x2, y1, y2;
133 tree->get_bounds (x1, y1, x2, y2);
134 real_canvas->scroll_to (0, 0);
141 real_canvas->set_scroll_region (0, 0, 5000, 5000);
144 if (d.run () == Gtk::RESPONSE_OK) {
145 uint altas = d.getAdds ();
146 uint bajas = d.getDels ();
148 double paltas = bajas / (double)altas;
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) {
158 std::list<int>::iterator it;
160 Random::Ints (lst, altas);
164 while (it != lst.end ()) {
167 double l = Random::Double (0.0f, 1.0f);
168 std::cout << l << " >= " << paltas << std::endl;
172 } catch (Exception *e) {
173 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
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 ();
182 while (j < aborrar) {
190 std::cout << "Clave Borrada " << sss << std::endl;
196 std::list<std::string> lst;
197 std::list<std::string>::iterator it;
199 Random::Strings (lst, altas);
202 while (it != lst.end ()) {
203 ClaveVariable c(*it);
207 } catch (Exception *e) {
208 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
214 double x1, x2, y1, y2;
215 tree->get_bounds (x1, y1, x2, y2);
216 real_canvas->scroll_to (0, 0);
220 void agregar_clave ()
224 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
225 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
229 KeyDialog d("Agregar");
230 if (d.run () == Gtk::RESPONSE_OK)
232 Glib::ustring str_key = d.key();
233 Glib::ustring str_val = d.val();
234 if (tree->type() == BTree::KEY_FIXED)
236 ClaveFija c(atoi(str_key.c_str()));
241 ClaveVariable c(str_key);
245 /* Muestro la clave agregada */
246 BTreeFindResult* result = 0;
248 if (tree->type() == BTree::KEY_FIXED) {
249 c = new ClaveFija (atoi(str_key.c_str()));
250 result = tree->FindKey(*c);
252 c = new ClaveVariable (str_key);
253 result = tree->FindKey(*c);
257 tree->AddNode(result->node);
258 tree->HighliteKey (*c);
261 delete tree->last_selected;
262 tree->last_selected = NULL;
266 real_canvas->scroll_to (0, 0);
274 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
275 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
279 KeyDialog d("Borrar");
280 if (d.run () == Gtk::RESPONSE_OK)
282 Glib::ustring str_key = d.key();
283 if (tree->type() == BTree::KEY_FIXED)
285 ClaveFija c(atoi(str_key.c_str()));
290 ClaveVariable c(str_key);
293 delete tree->last_selected;
302 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
303 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
314 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
315 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
319 KeyDialog d("Buscar");
320 while (true) // Repite hasta que se encuentre algo o se cancele
322 if (d.run () == Gtk::RESPONSE_OK)
324 BTreeFindResult* result = 0;
326 Glib::ustring str_key = d.key();
327 if (tree->type() == BTree::KEY_FIXED)
329 c = new ClaveFija (atoi(str_key.c_str()));
330 result = tree->FindKey(*c);
334 c = new ClaveVariable (str_key);
335 result = tree->FindKey(*c);
340 tree->AddNode(result->node);
341 tree->HighliteKey (*c);
344 real_canvas->scroll_to (0, 0);
345 return; // Encontramos, salimos
348 Gtk::MessageDialog msg("Clave no encontrada!", false,
349 Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
351 // Seguimos intentando
353 else return; // Cancelaron, salimos
359 double r = real_canvas->get_pixels_per_unit ();
363 real_canvas->set_pixels_per_unit (r);
368 double r = real_canvas->get_pixels_per_unit ();
372 real_canvas->set_pixels_per_unit (r);
377 real_canvas->set_pixels_per_unit (1.0f);