]> git.llucax.com Git - z.facultad/75.52/treemulator.git/commitdiff
Remarco la clave a pedido del usuario.
authorRicardo Markiewicz <rmarkie@fi.uba.ar>
Sat, 29 Oct 2005 00:14:08 +0000 (00:14 +0000)
committerRicardo Markiewicz <rmarkie@fi.uba.ar>
Sat, 29 Oct 2005 00:14:08 +0000 (00:14 +0000)
viewer/main.cpp
viewer/view_btree.cpp
viewer/view_btree.h
viewer/view_btree_data.cpp
viewer/view_btree_data.h
viewer/view_node.cpp
viewer/view_node.h

index fc8782b898d989e2f287e1a1b94e3f4aa9a48d88..f73897becd32175475bf3aef01fb4c89b4721b9d 100644 (file)
@@ -8,6 +8,7 @@
 #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;
@@ -20,7 +21,12 @@ using namespace Gnome::Canvas;
 "      <separator/>"
 "      <menuitem action='Salir'/>"
 "    </menu>"
-"   <menu action='MenuZoom'>"
+"    <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/>"
@@ -30,6 +36,9 @@ using namespace Gnome::Canvas;
 "</ui>";
 
 void nuevo_arbol ();
+void agregar_clave ();
+void borrar_clave ();
+void buscar_clave ();
 void zoom_out ();
 void zoom_in ();
 void zoom_normal ();
@@ -69,7 +78,11 @@ int main(int argc, char *argv[])
 
        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("Salir", Gtk::Stock::QUIT), Gtk::AccelKey ("<control>q"), &Gtk::Main::quit);
+       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("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);
@@ -168,6 +181,107 @@ void nuevo_arbol ()
        }
 }
 
+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();
+               if (tree->type() == BTree::KEY_FIXED)
+               {
+                       ClaveFija c(atoi(str_key.c_str()));
+                       tree->AddKey(c);
+               }
+               else
+               {
+                       ClaveVariable c(str_key);
+                       tree->AddKey(c);
+               }
+               delete tree->last_selected;
+               tree->AddNode (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 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;
+                               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
+       }
+}
+
 void zoom_out ()
 {
        double r = real_canvas->get_pixels_per_unit ();
index 3fba848252c01d506c56fcc8a30093cceb366847..fc1881e1ea4bffffa43423d1680a3e11418d35a4 100644 (file)
@@ -61,7 +61,6 @@ void ViewBTree::on_item_selected (BTreeData *data, uint num, ViewNode *vnode)
                if (vnode != last_selected)
                        last_selected->SetSelected (false);
        last_selected = vnode;
-       vnode->SetSelected (true);
 
        m_signal_selected (data, node_h);
 }
@@ -71,3 +70,14 @@ ViewBTree::type_signal_selected ViewBTree::signal_selected ()
        return m_signal_selected;
 }
 
+void ViewBTree::Clear ()
+{
+       if (last_selected)
+               delete last_selected;
+}
+
+void ViewBTree::HighliteKey (Clave &k)
+{
+       last_selected->HighliteKey (k);
+}
+
index 529dab26034cde1a993e425743b9cbe584eef6bc..bb955bd5dc88f567fdc1fc9987cd95c907d37198 100644 (file)
@@ -16,6 +16,9 @@ class ViewBTree : public Canvas::Group, public BTree {
        public:
                ViewBTree (Canvas::Group *parent, std::string filename, uint block_size, int type);
 
+               void Clear ();
+               void HighliteKey (Clave &k);
+
                static double byte_to_pixels;
                static double node_width;
                static double node_height;
index d2321c6e8d6ca976795e31f010dc704d07edb717..0c29461bab2619024db1d9da1143c9fc68b77f4a 100644 (file)
@@ -24,7 +24,7 @@ bool ViewBTreeData::on_event (GdkEvent *p1)
 {
        switch (p1->type) {
                case GDK_BUTTON_PRESS:
-                       m_signal_clicked(data, this);
+                       SetSelected (true);
                break;
                case GDK_2BUTTON_PRESS:
                        m_signal_double_clicked (data, this);
@@ -71,8 +71,16 @@ void ViewBTreeData::SetSelected (bool b)
 {
        if (b) {
                property_fill_color () = "yellow";
+               m_signal_clicked(data, this);
        } else {
                property_fill_color () = "red";
        }
 }
 
+bool ViewBTreeData::operator == (Clave &k) const
+{
+       if (!data->GetKey ()) return false;
+
+       return (*(data->GetKey ())) == k;
+}
+
index 3a08bf61138c98ae5921f0770b1a574207d960d5..e3000da96b5ca98a4996c3da993b089856a437f0 100644 (file)
@@ -24,6 +24,7 @@ class ViewBTreeData :public Canvas::Rect {
                type_signal_double_clicked signal_double_clicked ();
 
                void SetSelected (bool b);
+               bool operator == (Clave &k) const;
        protected:
                type_signal_clicked m_signal_clicked;
                type_signal_double_clicked m_signal_double_clicked;
index 6dc664941968a35693b53cb78db1688f6b7b8be1..dbcc8185a19202dad989423345ba2a75950d7d57 100644 (file)
@@ -39,6 +39,8 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader
                x += w;
                it++;
 
+               datas.push_back (v);
+
                if (!dynamic_cast<BTreeLeafData *>(data)) {
                        /* Si no es un dato de una hoja, tiene hijos */
                        hijos.push_back (data->GetChild ());
@@ -79,3 +81,19 @@ void ViewNode::SetSelected (bool b)
        }
 }
 
+void ViewNode::HighliteKey (Clave &k)
+{
+       std::list<ViewBTreeData *>::iterator it;
+
+       it = datas.begin ();
+       while (it != datas.end ()) {
+               if ((*(*it)) == k) {
+                       if (last_selected)
+                               last_selected->SetSelected (false);
+                       last_selected = *it;
+                       last_selected->SetSelected (true);
+               }
+               it++;
+       }
+}
+
index 549ccb0095869dde6e11e14fa4c9ba17401da2b3..e401f8fc7e93f8ee9ab40cd2bf0865a8a0469d1f 100644 (file)
@@ -12,6 +12,8 @@ class ViewNode : public Canvas::Group {
        public:
                ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list<BTreeData *> &keys);
 
+               void HighliteKey (Clave &k);
+
                std::list<uint>& getChilds () { return hijos; } 
 
                typedef SigC::Signal3<void, BTreeData *, uint, ViewNode *> type_signal_selected;
@@ -30,6 +32,7 @@ class ViewNode : public Canvas::Group {
                uint num, padre;
                Canvas::Rect *fondo;
                ViewBTreeData *last_selected;
+               std::list<ViewBTreeData *> datas;
 };
 
 #endif