#include "view_btree.h"
#include "view_btree_data.h"
+#include <sigc++/slot.h>
-ViewBTree::ViewBTree (Canvas::Group *parent, std::string filename):Canvas::Group (*parent, 0, 0),
- BTree (filename, 64)
+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, int tree_type, int type):Canvas::Group (*parent, 0, 0),
+ BTree (filename, block_size, tree_type, type)
+{
+ /* Cada bytes lo hago de 5 units de ancho */
+ node_width = 4 * block_size;
+ node_height = 50;
+ byte_to_pixels = node_width/block_size;
+
+ last_selected = NULL;
+ last_visited = 0;
+}
+
+ViewBTree::ViewBTree (Canvas::Group *parent, const std::string filename)
+ :Canvas::Group (*parent, 0, 0), BTree (filename)
+{
+ /* Cada bytes lo hago de 5 units de ancho */
+ node_width = 4 * header.block_size;
+ node_height = 50;
+ byte_to_pixels = node_width/header.block_size;
+
+ last_selected = NULL;
+ last_visited = 0;
+}
+
+ViewBTree* ViewBTree::Open (Canvas::Group *parent, const std::string &filename)
{
- /* TODO : hace que el arbol se abra de un archivo ya creado o que se
- * cree afuera
- */
- for (int i=0; i<=64; i++) {
- ClaveFija c(i);
+ ViewBTree *tree = new ViewBTree (parent, filename);
- AddKey (c);
+ if (tree->fp == NULL) {
+ delete tree;
+ return NULL;
}
- Canvas::Rect *rect = new Canvas::Rect (*this, 0, 0, NODE_WIDTH, NODE_HEIGHT);
- rect->property_fill_color() = "gray";
+ /* El magic no coincide!! */
+ if (strcmp (tree->header.magic, "DILUMA")) {
+ delete tree;
+ return NULL;
+ }
+ return tree;
+}
- byte_to_pixels = NODE_WIDTH/64; // TODO : 64 == BlockSize
+void ViewBTree::GoBack ()
+{
+ uint l = back.top ();
+ back.pop ();
- AddNode (0);
+ Clear ();
+ AddNode (l, 1);
}
-void ViewBTree::AddNode (uint num)
+void ViewBTree::AddNode (uint num, uint padre)
{
- /* Muestro la raiz */
- double y = 0;
+ /* Hack de ultima hora :P */
+ if (padre == 0)
+ /* evito quedar en circulo :D */
+ back.push (last_visited);
+
+ last_visited = num;
+
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<BTreeData *> keys = ReadKeys (node, node_h);
- double header_w = byte_to_pixels * sizeof (BTreeNodeHeader);
- double x = 0;
+ ViewNode *vnode = new ViewNode (this, num, padre, node_h, keys);
+ vnode->property_y () = 10;
+ vnode->property_x () = 10;
- Canvas::Rect *view_header = new Canvas::Rect (*this, x, y, x+header_w, y+NODE_HEIGHT);
- view_header->property_fill_color() = "blue";
+ vnode->signal_selected().connect ( sigc::mem_fun (*this, &ViewBTree::on_item_selected) );
+ vnode->signal_activated().connect ( sigc::mem_fun (*this, &ViewBTree::on_item_activated) );
- /* Numero de nodo */
- std::string node_num;
- std::stringstream ss;
- ss << num;
- ss >> node_num;
- new Canvas::Text (*this, header_w/2, y+NODE_HEIGHT/2, node_num);
+ last_selected = vnode;
+}
+
+void ViewBTree::on_item_activated (BTreeData *data, uint num, ViewNode *vnode)
+{
+ last_selected = NULL;
+
+ uint next = data->GetChild ();
+ delete vnode;
- x += header_w;
+ std::cout << next << std::endl;
+ AddNode (next, 0);
+}
- /* Cuento los hijos para saber como centrarlos más adelante */
- std::list<uint> hijos;
+void ViewBTree::on_item_selected (BTreeData *data, uint num, ViewNode *vnode)
+{
+ BTreeNodeHeader node_h;
+ uchar *node;
- std::list<BTreeData *>::iterator it = keys.begin ();
- while (it != keys.end ()) {
- BTreeData *data = (*it);
- double w = byte_to_pixels * data->Size ();
+ node = ReadBlock (num);
+ ReadNodoHeader (node, &node_h);
+ delete [] node;
- ViewBTreeData *v = ViewBTreeData::Create (data, this, x, y, w+x, y+NODE_HEIGHT);
+ if (last_selected)
+ if (vnode != last_selected)
+ last_selected->SetSelected (false);
+ last_selected = vnode;
- x += w;
- it++;
+ m_signal_selected (data, node_h);
+}
- if (dynamic_cast<BTreeChildData *>(data)) {
- BTreeChildData *child = dynamic_cast<BTreeChildData *>(data);
- hijos.push_back (child->getChild ());
- }
- }
+ViewBTree::type_signal_selected ViewBTree::signal_selected ()
+{
+ return m_signal_selected;
+}
+void ViewBTree::Clear ()
+{
+ if (last_selected)
+ delete last_selected;
+}
- std::list<uint>::iterator hit = hijos.begin ();
- while (hit != hijos.end ()) {
- AddNode (*hit);
- hit++;
- }
+void ViewBTree::HighliteKey (Clave &k)
+{
+ last_selected->HighliteKey (k);
}