]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blobdiff - viewer/main.cpp
tagged 1.0
[z.facultad/75.52/treemulator.git] / viewer / main.cpp
index d5f82abe45b16e891d5f79c188743419321c478d..59f34f77bc20179fcfbee67da22d2e890583f496 100644 (file)
@@ -4,9 +4,12 @@
 #include <gtkmm.h>
 #include <libgnomecanvasmm.h>
 
 #include <gtkmm.h>
 #include <libgnomecanvasmm.h>
 
+#include "random.h"
 #include "view_btree.h"
 #include "view_properties.h"
 #include "new_tree_dialog.h"
 #include "view_btree.h"
 #include "view_properties.h"
 #include "new_tree_dialog.h"
+#include "key_dialog.h"
+#include "view_debug.h"
 
 using namespace Gnome::Canvas;
 
 
 using namespace Gnome::Canvas;
 
@@ -15,10 +18,19 @@ using namespace Gnome::Canvas;
 "  <menubar name='MenuBar'>"
 "    <menu action='MenuFile'>"
 "      <menuitem action='Nuevo'/>"
 "  <menubar name='MenuBar'>"
 "    <menu action='MenuFile'>"
 "      <menuitem action='Nuevo'/>"
+"      <menuitem action='Abrir'/>"
 "      <separator/>"
 "      <menuitem action='Salir'/>"
 "    </menu>"
 "      <separator/>"
 "      <menuitem action='Salir'/>"
 "    </menu>"
-"   <menu action='MenuZoom'>"
+"    <menu action='MenuNode'>"
+"      <menuitem action='Ir al Padre'/>"
+"    </menu>"
+"    <menu action='MenuKey'>"
+"      <menuitem action='Agregar Clave'/>"
+"      <menuitem action='Borrar Clave'/>"
+"      <menuitem action='Buscar Clave'/>"
+"    </menu>"
+"    <menu action='MenuZoom'>"
 "      <menuitem action='Alejar'/>"
 "      <menuitem action='Acercar'/>"
 "      <separator/>"
 "      <menuitem action='Alejar'/>"
 "      <menuitem action='Acercar'/>"
 "      <separator/>"
@@ -28,12 +40,18 @@ using namespace Gnome::Canvas;
 "</ui>";
 
 void nuevo_arbol ();
 "</ui>";
 
 void nuevo_arbol ();
+void abrir_arbol ();
+void agregar_clave ();
+void borrar_clave ();
+void ir_al_padre ();
+void buscar_clave ();
 void zoom_out ();
 void zoom_in ();
 void zoom_normal ();
 
 void zoom_out ();
 void zoom_in ();
 void zoom_normal ();
 
-ViewBTree *tree;
+Glib::RefPtr<ViewBTree> tree;
 Gnome::Canvas::Canvas *real_canvas;
 Gnome::Canvas::Canvas *real_canvas;
+ViewProperties *real_frame;
 
 int main(int argc, char *argv[])
 {
 
 int main(int argc, char *argv[])
 {
@@ -42,32 +60,39 @@ int main(int argc, char *argv[])
        Gnome::Canvas::init ();
 
        Gtk::Window window;
        Gnome::Canvas::init ();
 
        Gtk::Window window;
-       Gtk::HBox hbox;
+       Gtk::VBox hbox;
        Gtk::VBox vbox;
 
        Gtk::VBox vbox;
 
-       ViewProperties frame;
        Gtk::ScrolledWindow area;
        Gnome::Canvas::Canvas canvas;
        Gtk::ScrolledWindow area;
        Gnome::Canvas::Canvas canvas;
+       ViewProperties frame;
 
 
-       ViewBTree canvas_grp (canvas.root (), "test.idx");
-       tree = &canvas_grp;
        real_canvas = &canvas;
        real_canvas = &canvas;
+       real_frame = &frame;
 
 
-       canvas.set_scroll_region (0, 0, 5000, 5000);
+       canvas.set_scroll_region (0, 0, 100, 100);
        area.add (canvas);
 
        area.add (canvas);
 
-       hbox.pack_start (frame, false, false, 10);
        hbox.pack_start (area);
        hbox.pack_start (area);
+       hbox.pack_start (frame, false, false, 10);
+       frame.set_size_request (200, 200);
 
        Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
 
        actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
        actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
 
        Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
 
        actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
        actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
-       actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), &Gtk::Main::quit);
+       actiongroup->add( Gtk::Action::create("Abrir", Gtk::Stock::OPEN), &abrir_arbol);
+       actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), Gtk::AccelKey ("<control>q"), &Gtk::Main::quit);
+       actiongroup->add( Gtk::Action::create("MenuNode", "_Nodo") );
+       actiongroup->add( Gtk::Action::create("Ir al Padre", Gtk::Stock::GO_BACK), Gtk::AccelKey ("<control>b"), &ir_al_padre);
+       actiongroup->add( Gtk::Action::create("MenuKey", "_Clave") );
+       actiongroup->add( Gtk::Action::create("Agregar Clave", Gtk::Stock::ADD), Gtk::AccelKey ("<control>a"), &agregar_clave);
+       actiongroup->add( Gtk::Action::create("Borrar Clave", Gtk::Stock::REMOVE), Gtk::AccelKey ("<control>d"), &borrar_clave);
+       actiongroup->add( Gtk::Action::create("Buscar Clave", Gtk::Stock::FIND), Gtk::AccelKey ("<control>f"), &buscar_clave);
        actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
        actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
-       actiongroup->add( Gtk::Action::create("Alejar", "_Alejar"), &zoom_out );
-       actiongroup->add( Gtk::Action::create("Acercar", "A_cercar"), &zoom_in);
-       actiongroup->add( Gtk::Action::create("100 %", "100 %"), &zoom_normal);
+       actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("<control>z"), &zoom_out );
+       actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("<control>x"), &zoom_in);
+       actiongroup->add( Gtk::Action::create("100 %", Gtk::Stock::ZOOM_100), Gtk::AccelKey ("<control>1"), &zoom_normal);
 
        Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
        m_refUIManager->insert_action_group (actiongroup);
 
        Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
        m_refUIManager->insert_action_group (actiongroup);
@@ -75,30 +100,255 @@ int main(int argc, char *argv[])
        m_refUIManager->add_ui_from_string(ui_info);
        Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
        menubar->show_all ();
        m_refUIManager->add_ui_from_string(ui_info);
        Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
        menubar->show_all ();
-       vbox.pack_start (*menubar, false, true, 0);
 
 
+       vbox.pack_start (*menubar, false, true, 0);
        vbox.pack_end (hbox, true, true, 5);
        vbox.pack_end (hbox, true, true, 5);
+
+       window.add_accel_group (m_refUIManager->get_accel_group ());
        window.add (vbox);
        window.set_size_request (640, 480);
        window.show_all ();
 
        /* Conecto el Canvas con el Frame */
        window.add (vbox);
        window.set_size_request (640, 480);
        window.show_all ();
 
        /* Conecto el Canvas con el Frame */
-       canvas_grp.signal_selected ().connect ( sigc::mem_fun (frame, &ViewProperties::ShowItem) );
        Gtk::Main::run(window);
                                                            
        return 0;
 }
 
        Gtk::Main::run(window);
                                                            
        return 0;
 }
 
+void abrir_arbol ()
+{
+       Gtk::FileChooserDialog dlg ("Abrir Arbol");
+
+       dlg.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+       dlg.add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
+       if (dlg.run () == Gtk::RESPONSE_ACCEPT) {
+               Glib::ustring s = dlg.get_filename ();
+               std::string filename = s; 
+               ViewBTree *ptree = ViewBTree::Open (real_canvas->root (), filename);
+               if (ptree != NULL) {
+                       tree = Glib::RefPtr<ViewBTree>(ptree);
+                       tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
+                       tree->AddNode (0);
+                       double x1, x2, y1, y2;
+                       tree->get_bounds (x1, y1, x2, y2);
+                       real_canvas->scroll_to (0, 0);
+               }
+       }
+}
+
 void nuevo_arbol ()
 {
 void nuevo_arbol ()
 {
+       real_canvas->set_scroll_region (0, 0, 5000, 5000);
+
        NewTreeDialog d;
        if (d.run () == Gtk::RESPONSE_OK) {
        NewTreeDialog d;
        if (d.run () == Gtk::RESPONSE_OK) {
-               for (uint i=0; i<=d.getAmount (); i++) {
-                       ClaveFija c(i);
+               uint altas = d.getAdds ();
+               uint bajas = d.getDels ();
+
+               double paltas = bajas / (double)altas;
+
+               int type = d.getKeyType ();
+               int atype = d.getTreeType ();
+               tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), atype, type));
+               tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
+               if (type == BTree::KEY_FIXED) {
+                       std::list<int> lst;
+                       std::list<int>::iterator it;
+                       Random::Init ();
+                       Random::Ints (lst, altas);
+
+                       it = lst.begin ();
+                       uint i = 0;
+                       while (it != lst.end ()) {
+                               ClaveFija c(*it);
+
+                               double l = Random::Double (0.0f, 1.0f);
+                               std::cout << l << " >= " << paltas << std::endl;
+                               if (l >= paltas) {
+                                       try {
+                                               tree->AddKey (c);
+                                       } catch (Exception *e) {
+                                               std::cout << "====== " << (std::string)c << e->Message () << std::endl;
+                                       }
+                                       i++;
+                               } else {
+                                       /* Tengo que borrar una clave entre 0 e "i" de la lista
+                                        * porque son las que ya agregue. */
+                                       int aborrar = (int)Random::Double (0, i);
+                                       std::list<int>::iterator otro = lst.begin ();
+                                       int j = 0;
+                                       while (j < aborrar) {
+                                               otro++;
+                                               j++;
+                                       }
+                                       ClaveFija c(*otro);
 
 
-                       tree->AddKey (c);
+                                       tree->DelKey (c);
+                                       std::string sss = c;
+                                       std::cout << "Clave Borrada " << sss << std::endl;
+                               }
+
+                               it++;
+                       }
+               } else {
+                       std::list<std::string> lst;
+                       std::list<std::string>::iterator it;
+                       Random::Init ();
+                       Random::Strings (lst, altas);
+
+                       it = lst.begin ();
+                       while (it != lst.end ()) {
+                               ClaveVariable c(*it);
+
+                               try {
+                                       tree->AddKey (c);
+                               } catch (Exception *e) {
+                                       std::cout << "====== " << (std::string)c << e->Message () << std::endl;
+                               }
+                               it++;
+                       }
                }
                tree->AddNode (0);
                }
                tree->AddNode (0);
+               double x1, x2, y1, y2;
+               tree->get_bounds (x1, y1, x2, y2);
+               real_canvas->scroll_to (0, 0);
+       }
+}
+
+void agregar_clave ()
+{
+       if (!tree)
+       {
+               Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
+                               false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+               d.run();
+               return;
+       }
+       KeyDialog d("Agregar");
+       if (d.run () == Gtk::RESPONSE_OK)
+       {
+               Glib::ustring str_key = d.key();
+               Glib::ustring str_val = d.val();
+               if (tree->type() == BTree::KEY_FIXED)
+               {
+                       ClaveFija c(atoi(str_key.c_str()));
+                       tree->AddKey(c);
+               }
+               else
+               {
+                       ClaveVariable c(str_key);
+                       tree->AddKey(c);
+               }
+
+               /* Muestro la clave agregada */
+               BTreeFindResult* result = 0;
+               Clave *c = NULL;
+               if (tree->type() == BTree::KEY_FIXED) {
+                       c = new ClaveFija (atoi(str_key.c_str()));
+                       result = tree->FindKey(*c);
+               } else {
+                       c = new ClaveVariable (str_key);
+                       result = tree->FindKey(*c);
+               }
+               if (result) {
+                       tree->Clear ();
+                       tree->AddNode(result->node);
+                       tree->HighliteKey (*c);
+                       delete result;
+               } else {
+                       delete tree->last_selected;
+                       tree->last_selected = NULL;
+                       tree->AddNode (0);
+               }
+               if (c) delete c;
+               real_canvas->scroll_to (0, 0);
+       }
+}
+
+void borrar_clave ()
+{
+       if (!tree)
+       {
+               Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
+                               false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+               d.run();
+               return;
+       }
+       KeyDialog d("Borrar");
+       if (d.run () == Gtk::RESPONSE_OK)
+       {
+               Glib::ustring str_key = d.key();
+               if (tree->type() == BTree::KEY_FIXED)
+               {
+                       ClaveFija c(atoi(str_key.c_str()));
+                       tree->DelKey(c);
+               }
+               else
+               {
+                       ClaveVariable c(str_key);
+                       tree->DelKey(c);
+               }
+               delete tree->last_selected;
+               tree->AddNode (0);
+       }
+}
+
+void ir_al_padre ()
+{
+       if (!tree)
+       {
+               Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
+                               false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+               d.run();
+               return;
+       }
+       tree->GoBack ();
+}
+
+void buscar_clave ()
+{
+       if (!tree)
+       {
+               Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
+                               false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+               d.run();
+               return;
+       }
+       KeyDialog d("Buscar");
+       while (true) // Repite hasta que se encuentre algo o se cancele
+       {
+               if (d.run () == Gtk::RESPONSE_OK)
+               {
+                       BTreeFindResult* result = 0;
+                       Clave *c = NULL;
+                       Glib::ustring str_key = d.key();
+                       if (tree->type() == BTree::KEY_FIXED)
+                       {
+                               c = new ClaveFija (atoi(str_key.c_str()));
+                               result = tree->FindKey(*c);
+                       }
+                       else
+                       {
+                               c = new ClaveVariable (str_key);
+                               result = tree->FindKey(*c);
+                       }
+                       if (result)
+                       {
+                               tree->Clear ();
+                               tree->AddNode(result->node);
+                               tree->HighliteKey (*c);
+                               delete result;
+                               delete c;
+                               real_canvas->scroll_to (0, 0);
+                               return; // Encontramos, salimos
+                       }
+                       if (c) delete c;
+                       Gtk::MessageDialog msg("Clave no encontrada!", false,
+                                       Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+                       msg.run();
+                       // Seguimos intentando
+               }
+               else return; // Cancelaron, salimos
        }
 }
 
        }
 }