From: Ricardo Markiewicz Date: Sat, 29 Oct 2005 00:14:08 +0000 (+0000) Subject: Remarco la clave a pedido del usuario. X-Git-Tag: 1_0-pre1~4 X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/commitdiff_plain/0fd0b8da8e1e9c889282ad4cb41f4b4cf2e63838 Remarco la clave a pedido del usuario. --- diff --git a/viewer/main.cpp b/viewer/main.cpp index fc8782b..f73897b 100644 --- a/viewer/main.cpp +++ b/viewer/main.cpp @@ -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; " " " " " " -" " +" " +" " +" " +" " +" " +" " " " " " " " @@ -30,6 +36,9 @@ using namespace Gnome::Canvas; ""; 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 ("q"), &Gtk::Main::quit); + actiongroup->add( Gtk::Action::create("MenuKey", "_Clave") ); + actiongroup->add( Gtk::Action::create("Agregar Clave", Gtk::Stock::ADD), Gtk::AccelKey ("a"), &agregar_clave); + actiongroup->add( Gtk::Action::create("Borrar Clave", Gtk::Stock::REMOVE), Gtk::AccelKey ("d"), &borrar_clave); + actiongroup->add( Gtk::Action::create("Buscar Clave", Gtk::Stock::FIND), Gtk::AccelKey ("f"), &buscar_clave); actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom")); actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("z"), &zoom_out ); actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("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 (); diff --git a/viewer/view_btree.cpp b/viewer/view_btree.cpp index 3fba848..fc1881e 100644 --- a/viewer/view_btree.cpp +++ b/viewer/view_btree.cpp @@ -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); +} + diff --git a/viewer/view_btree.h b/viewer/view_btree.h index 529dab2..bb955bd 100644 --- a/viewer/view_btree.h +++ b/viewer/view_btree.h @@ -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; diff --git a/viewer/view_btree_data.cpp b/viewer/view_btree_data.cpp index d2321c6..0c29461 100644 --- a/viewer/view_btree_data.cpp +++ b/viewer/view_btree_data.cpp @@ -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; +} + diff --git a/viewer/view_btree_data.h b/viewer/view_btree_data.h index 3a08bf6..e3000da 100644 --- a/viewer/view_btree_data.h +++ b/viewer/view_btree_data.h @@ -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; diff --git a/viewer/view_node.cpp b/viewer/view_node.cpp index 6dc6649..dbcc818 100644 --- a/viewer/view_node.cpp +++ b/viewer/view_node.cpp @@ -39,6 +39,8 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader x += w; it++; + datas.push_back (v); + if (!dynamic_cast(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::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++; + } +} + diff --git a/viewer/view_node.h b/viewer/view_node.h index 549ccb0..e401f8f 100644 --- a/viewer/view_node.h +++ b/viewer/view_node.h @@ -12,6 +12,8 @@ class ViewNode : public Canvas::Group { public: ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list &keys); + void HighliteKey (Clave &k); + std::list& getChilds () { return hijos; } typedef SigC::Signal3 type_signal_selected; @@ -30,6 +32,7 @@ class ViewNode : public Canvas::Group { uint num, padre; Canvas::Rect *fondo; ViewBTreeData *last_selected; + std::list datas; }; #endif