From 1f2e83ab78a55bc0730bdd507c4fd64482fb0846 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Wed, 19 Oct 2005 16:08:06 +0000 Subject: [PATCH] Dejando armado el viewer. --- viewer/main.cpp | 43 ++++++++++++++++++++++----- viewer/view_btree.cpp | 59 ++++++++++++++++---------------------- viewer/view_btree.h | 16 +++++++---- viewer/view_btree_data.cpp | 9 ++++++ viewer/view_btree_data.h | 7 +++-- viewer/view_node.cpp | 40 +++++++++++++++++++++----- viewer/view_node.h | 18 ++++++++---- viewer/view_properties.cpp | 6 +++- 8 files changed, 136 insertions(+), 62 deletions(-) diff --git a/viewer/main.cpp b/viewer/main.cpp index 692bed8..7a10f11 100644 --- a/viewer/main.cpp +++ b/viewer/main.cpp @@ -4,6 +4,7 @@ #include #include +#include "random.h" #include "view_btree.h" #include "view_properties.h" #include "new_tree_dialog.h" @@ -57,7 +58,7 @@ int main(int argc, char *argv[]) real_frame = &frame; vdebug = &debug; - canvas.set_scroll_region (0, 0, 5000, 5000); + canvas.set_scroll_region (0, 0, 100, 100); area.add (canvas); hbox.pack_start (frame, false, false, 10); @@ -97,19 +98,47 @@ int main(int argc, char *argv[]) void nuevo_arbol () { + real_canvas->set_scroll_region (0, 0, 5000, 5000); + NewTreeDialog d; if (d.run () == Gtk::RESPONSE_OK) { uint tot = d.getAmount (); - tree = Glib::RefPtr(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize ())); + int type = d.getKeyType (); + tree = Glib::RefPtr(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), type)); tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) ); vdebug->SetTree (tree); - for (uint i=0; i <= tot; i++) { - ClaveFija c(i); - - tree->AddKey (c); - vdebug->AddKey (c); + if (type == BTree::KEY_FIXED) { + std::list lst; + std::list::iterator it; + Random::Init (); + Random::Ints (lst, tot); + + it = lst.begin (); + while (it != lst.end ()) { + ClaveFija c(*it); + + tree->AddKey (c); + vdebug->AddKey (c); + it++; + } + } else { + std::list lst; + std::list::iterator it; + Random::Init (); + Random::Strings (lst, tot); + + it = lst.begin (); + while (it != lst.end ()) { + ClaveVariable c(*it); + + tree->AddKey (c); + vdebug->AddKey (c); + it++; + } } tree->AddNode (0); + double x1, x2, y1, y2; + tree->get_bounds (x1, y1, x2, y2); } } diff --git a/viewer/view_btree.cpp b/viewer/view_btree.cpp index 1180c93..6bdcb67 100644 --- a/viewer/view_btree.cpp +++ b/viewer/view_btree.cpp @@ -5,57 +5,47 @@ double ViewBTree::byte_to_pixels = 0; double ViewBTree::node_width = 0; +double ViewBTree::node_height = 0; -ViewBTree::ViewBTree (Canvas::Group *parent, std::string filename, uint block_size):Canvas::Group (*parent, 0, 0), - BTree (filename, block_size) +ViewBTree::ViewBTree (Canvas::Group *parent, std::string filename, uint block_size, int type):Canvas::Group (*parent, 0, 0), + BTree (filename, block_size, type) { /* Cada bytes lo hago de 5 units de ancho */ - node_width = 3 * block_size; // 64 == blocksize - byte_to_pixels = node_width/block_size; // TODO : 64 == BlockSize + node_width = 5 * block_size; + node_height = node_width/10; + byte_to_pixels = node_width/block_size; last_selected = NULL; } -void ViewBTree::AddNode (uint num) +void ViewBTree::AddNode (uint num, uint padre) { - if (node_placed[num]) { - std::cout << "WARNING : Tratando de agregar de nuevo el nodo " << num << std::endl; - return; - } - - node_placed[num] = true; - /* Muestro la raiz */ - double y = 0; uchar *node = ReadBlock (num); BTreeNodeHeader node_h; ReadNodoHeader (node, &node_h); - if (num == 0) { - max_level = node_h.level; - } else { - y = (max_level - node_h.level)*(NODE_HEIGHT+10); - } - std::list keys = ReadKeys (node, node_h); - ViewNode *vnode = new ViewNode (this, num, node_h, keys); - vnode->property_y () = y; - vnode->property_x () = pos_x[node_h.level]; - pos_x[node_h.level] += ViewBTree::node_width+10; + ViewNode *vnode = new ViewNode (this, num, padre, node_h, keys); + vnode->property_y () = 10; + vnode->property_x () = 10; + vnode->signal_selected().connect ( sigc::mem_fun (*this, &ViewBTree::on_item_selected) ); + vnode->signal_activated().connect ( sigc::mem_fun (*this, &ViewBTree::on_item_activated) ); + + last_selected = vnode; +} - std::list childs = vnode->getChilds (); - std::list::iterator hit = childs.begin (); - while (hit != childs.end ()) { - if ((*hit) == 0) { - std::cout << "WARNING : Referencia a 0 encontrada!!" << std::endl; - hit = childs.end (); - continue; - } - AddNode (*hit); - hit++; - } +void ViewBTree::on_item_activated (BTreeData *data, uint num, ViewNode *vnode) +{ + last_selected = NULL; + + uint next = data->getChild (); + delete vnode; + + std::cout << next << std::endl; + AddNode (next, num); } void ViewBTree::on_item_selected (BTreeData *data, uint num, ViewNode *vnode) @@ -72,6 +62,7 @@ void ViewBTree::on_item_selected (BTreeData *data, uint num, ViewNode *vnode) last_selected->SetSelected (false); last_selected = vnode; vnode->SetSelected (true); + m_signal_selected (data, node_h); } diff --git a/viewer/view_btree.h b/viewer/view_btree.h index d8e585a..529dab2 100644 --- a/viewer/view_btree.h +++ b/viewer/view_btree.h @@ -8,16 +8,17 @@ #include "btree.h" #include "view_node.h" -#define NODE_WIDTH 500 -#define NODE_HEIGHT 50 - using namespace Gnome; +class ViewNode; + class ViewBTree : public Canvas::Group, public BTree { public: - ViewBTree (Canvas::Group *parent, std::string filename); + ViewBTree (Canvas::Group *parent, std::string filename, uint block_size, int type); static double byte_to_pixels; + static double node_width; + static double node_height; typedef SigC::Signal2 type_signal_selected; type_signal_selected signal_selected (); @@ -30,9 +31,12 @@ class ViewBTree : public Canvas::Group, public BTree { /* Un nodo que quiera ser puesto 2 veces es un bug :) */ std::map node_placed; - void AddNode (uint num); - void on_item_selected (BTreeData *, uint); + public: + void AddNode (uint num, uint padre = 0); + void on_item_selected (BTreeData *, uint, ViewNode *); + void on_item_activated (BTreeData *, uint, ViewNode *); + ViewNode *last_selected; }; #endif diff --git a/viewer/view_btree_data.cpp b/viewer/view_btree_data.cpp index 9f4142d..d2321c6 100644 --- a/viewer/view_btree_data.cpp +++ b/viewer/view_btree_data.cpp @@ -25,6 +25,9 @@ bool ViewBTreeData::on_event (GdkEvent *p1) switch (p1->type) { case GDK_BUTTON_PRESS: m_signal_clicked(data, this); + break; + case GDK_2BUTTON_PRESS: + m_signal_double_clicked (data, this); } return Canvas::Rect::on_event (p1); @@ -36,6 +39,12 @@ ViewBTreeData::type_signal_clicked ViewBTreeData::signal_clicked () return m_signal_clicked; } +ViewBTreeData::type_signal_double_clicked ViewBTreeData::signal_double_clicked () +{ + return m_signal_double_clicked; +} + + ViewBTreeData* ViewBTreeData::Create (BTreeData *data, Canvas::Group *parent, double x1, double y1, double x2, double y2) { diff --git a/viewer/view_btree_data.h b/viewer/view_btree_data.h index 1af3866..3a08bf6 100644 --- a/viewer/view_btree_data.h +++ b/viewer/view_btree_data.h @@ -18,11 +18,15 @@ class ViewBTreeData :public Canvas::Rect { static ViewBTreeData *Create (BTreeData *data, Canvas::Group *parent, double x1, double y1, double x2, double y2); - typedef SigC::Signal1 type_signal_clicked; + typedef SigC::Signal2 type_signal_clicked; type_signal_clicked signal_clicked (); + typedef SigC::Signal2 type_signal_double_clicked; + type_signal_double_clicked signal_double_clicked (); + void SetSelected (bool b); protected: type_signal_clicked m_signal_clicked; + type_signal_double_clicked m_signal_double_clicked; virtual void init (Canvas::Group *parent); BTreeData *data; @@ -37,7 +41,6 @@ class ViewBTreeChildData :public ViewBTreeData { protected: virtual void init (Canvas::Group *parent); - BTreeData *data; }; #endif diff --git a/viewer/view_node.cpp b/viewer/view_node.cpp index 0092dc0..122ebc6 100644 --- a/viewer/view_node.cpp +++ b/viewer/view_node.cpp @@ -1,19 +1,21 @@ #include "view_node.h" -ViewNode::ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, std::list &keys) +ViewNode::ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list &keys) : Canvas::Group (*parent) { + last_selected = NULL; double header_w = ViewBTree::byte_to_pixels * sizeof (BTreeNodeHeader); this->num = num; + this->padre = padre; /* Fondo */ - Canvas::Rect *fondo = new Canvas::Rect (*this, 0, 0, NODE_WIDTH, NODE_HEIGHT); + fondo = new Canvas::Rect (*this, 0, 0, ViewBTree::node_width, ViewBTree::node_height); fondo->property_fill_color() = "gray"; fondo->property_outline_color() = "black"; /* Header */ - Canvas::Rect *h = new Canvas::Rect (*this, 0, 0, header_w, NODE_HEIGHT); + Canvas::Rect *h = new Canvas::Rect (*this, 0, 0, header_w, ViewBTree::node_height); h->property_fill_color() = "blue"; h->property_outline_color() = "black"; @@ -22,7 +24,7 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, st std::stringstream ss; ss << num; ss >> node_num; - new Canvas::Text (*this, header_w/2, NODE_HEIGHT/2, node_num); + new Canvas::Text (*this, header_w/2, ViewBTree::node_height/2, node_num); double x = header_w; @@ -31,8 +33,9 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, st BTreeData *data = (*it); double w = ViewBTree::byte_to_pixels * data->Size (); - ViewBTreeData *v = ViewBTreeData::Create (data, this, x, 0, w+x, NODE_HEIGHT); + ViewBTreeData *v = ViewBTreeData::Create (data, this, x, 0, w+x, ViewBTree::node_height); v->signal_clicked ().connect ( sigc::mem_fun (this , &ViewNode::on_item_clicked )); + v->signal_double_clicked ().connect ( sigc::mem_fun (this , &ViewNode::on_item_activated )); x += w; it++; @@ -43,9 +46,20 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, st } } -void ViewNode::on_item_clicked (BTreeData *data) +void ViewNode::on_item_clicked (BTreeData *data, ViewBTreeData *s) { - m_signal_selected (data, num); + if (last_selected) + if (s != last_selected) + last_selected->SetSelected (false); + last_selected = s; + m_signal_selected (data, num, this); +} + +void ViewNode::on_item_activated (BTreeData *data, ViewBTreeData *s) +{ + /* Solo activo los que tienen hijos */ + if (dynamic_cast (data) == NULL) + m_signal_activated (data, num, this); } ViewNode::type_signal_selected ViewNode::signal_selected () @@ -53,3 +67,15 @@ ViewNode::type_signal_selected ViewNode::signal_selected () return m_signal_selected; } +ViewNode::type_signal_activated ViewNode::signal_activated () +{ + return m_signal_activated; +} + +void ViewNode::SetSelected (bool b) +{ + if (last_selected) { + last_selected->SetSelected (b); + } +} + diff --git a/viewer/view_node.h b/viewer/view_node.h index dcd786b..549ccb0 100644 --- a/viewer/view_node.h +++ b/viewer/view_node.h @@ -5,23 +5,31 @@ #include #include "view_btree.h" #include "view_btree_data.h" -#include "btree.h" using namespace Gnome; class ViewNode : public Canvas::Group { public: - ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, std::list &keys); + ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list &keys); std::list& getChilds () { return hijos; } - typedef SigC::Signal2 type_signal_selected; + typedef SigC::Signal3 type_signal_selected; type_signal_selected signal_selected (); + + typedef SigC::Signal3 type_signal_activated; + type_signal_activated signal_activated (); + + void SetSelected (bool); private: - void on_item_clicked (BTreeData *data); + void on_item_clicked (BTreeData *data, ViewBTreeData *); + void on_item_activated (BTreeData *data, ViewBTreeData *); std::list hijos; type_signal_selected m_signal_selected; - uint num; + type_signal_activated m_signal_activated; + uint num, padre; + Canvas::Rect *fondo; + ViewBTreeData *last_selected; }; #endif diff --git a/viewer/view_properties.cpp b/viewer/view_properties.cpp index 4576965..c10bbae 100644 --- a/viewer/view_properties.cpp +++ b/viewer/view_properties.cpp @@ -42,8 +42,12 @@ ViewProperties::ViewProperties ():Frame () table.attach (data_type, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK); table.attach (data_key, 1, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK); table.attach (data_child, 1, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK); + + table.property_column_spacing () = 8; + table.property_row_spacing () = 8; add (table); + property_label_xalign () = 0; set_label ("Propiedades"); set_shadow_type(Gtk::SHADOW_NONE); set_size_request (200, 0); @@ -82,7 +86,7 @@ void ViewProperties::ShowItem (BTreeData *data, BTreeNodeHeader &header) data_child.set_label ("N/C"); std::string s4 = *(data->getClave ()); - data_child.set_label (s4); + data_key.set_label (s4); } else { data_type.set_label ("BTreeData"); -- 2.43.0