#include <gtkmm.h>
#include <libgnomecanvasmm.h>
+#include "random.h"
#include "view_btree.h"
#include "view_properties.h"
#include "new_tree_dialog.h"
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);
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<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize ()));
+ int type = d.getKeyType ();
+ tree = Glib::RefPtr<ViewBTree>(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<int> lst;
+ std::list<int>::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<std::string> lst;
+ std::list<std::string>::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);
}
}
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<BTreeData *> 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<uint> childs = vnode->getChilds ();
- std::list<uint>::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)
last_selected->SetSelected (false);
last_selected = vnode;
vnode->SetSelected (true);
+
m_signal_selected (data, node_h);
}
#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<void, BTreeData *, BTreeNodeHeader&> type_signal_selected;
type_signal_selected signal_selected ();
/* Un nodo que quiera ser puesto 2 veces es un bug :) */
std::map<uint,bool> 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
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);
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)
{
static ViewBTreeData *Create (BTreeData *data, Canvas::Group *parent, double x1, double y1, double x2, double y2);
- typedef SigC::Signal1<void, BTreeData *> type_signal_clicked;
+ typedef SigC::Signal2<void, BTreeData *, ViewBTreeData *> type_signal_clicked;
type_signal_clicked signal_clicked ();
+ typedef SigC::Signal2<void, BTreeData *, ViewBTreeData *> 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;
protected:
virtual void init (Canvas::Group *parent);
- BTreeData *data;
};
#endif
#include "view_node.h"
-ViewNode::ViewNode (Canvas::Group *parent, uint num, BTreeNodeHeader &header, std::list<BTreeData *> &keys)
+ViewNode::ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list<BTreeData *> &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";
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;
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++;
}
}
-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<BTreeLeafData *> (data) == NULL)
+ m_signal_activated (data, num, this);
}
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);
+ }
+}
+
#include <libgnomecanvasmm.h>
#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<BTreeData *> &keys);
+ ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader &header, std::list<BTreeData *> &keys);
std::list<uint>& getChilds () { return hijos; }
- typedef SigC::Signal2<void, BTreeData *, uint> type_signal_selected;
+ typedef SigC::Signal3<void, BTreeData *, uint, ViewNode *> type_signal_selected;
type_signal_selected signal_selected ();
+
+ typedef SigC::Signal3<void, BTreeData *, uint, ViewNode *> 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<uint> 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
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);
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");