Project

General

Profile

Dynamic WTreeView inside a WGroupBox ยป main.cpp

Rob Van Dyck, 01/24/2012 04:31 PM

 
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WInPlaceEdit>
#include <Wt/WLength>
#include <Wt/WSuggestionPopup>
#include <Wt/WStandardItemModel>
#include <Wt/WLineEdit>
#include <Wt/WText>
#include <Wt/WString>
#include <Wt/WTableView>
#include <Wt/WTreeView>
#include <Wt/WStandardItem>
#include <Wt/WItemDelegate>
#include <Wt/WDialog>
#include <Wt/WPushButton>
#include <Wt/WAbstractProxyModel>
#include <Wt/WEvent>
#include <Wt/WSignal>
#include <Wt/WModelIndex>
#include <Wt/WHBoxLayout>
#include <Wt/WVBoxLayout>
#include <Wt/WGroupBox>

#include <boost/bind.hpp>
#include <boost/foreach.hpp>

using namespace Wt;
using namespace boost;


class MyApplication: public WApplication {
public:
MyApplication(const Wt::WEnvironment& env);

/**
* Layout class that lets us call the update method
*/
class layout_class : public WHBoxLayout {
public:
layout_class() : WHBoxLayout() {};
void update_layout() {update();}
};

/**
* Treeview test class
*/
class treetest : public WTreeView {
public:
treetest(layout_class *layout) : WTreeView(0), layout_(layout) {
// make this size aware so we can adjust our column size horizontally
setLayoutSizeAware(true);

// add some data
WStandardItemModel *result = new WStandardItemModel(0, 2, this);
result->setHeaderData(0, Horizontal, std::string("File"));
result->setHeaderData(1, Horizontal, std::string("Size"));
WStandardItem *root = new WStandardItem("test_root");
root->setFlags(0);
result->appendRow(root);
WStandardItem *test;
root->appendRow(test = new WStandardItem("test"));
test->appendRow(new WStandardItem("test_child"));
root->appendRow(new WStandardItem("test1"));
root->appendRow(new WStandardItem("test2"));
this->setModel(result);

// react on collapsing and expansion of nodes
collapsed().connect(boost::bind(&treetest::item_collapsed, this));
expanded().connect(boost::bind(&treetest::item_expanded, this));

// set an initial height
evaluate_height();
};

/**
* Set a width for columns based on the available space
*/
void layoutSizeChanged(int width, int height2) {
std::cout << "width: " << width << " height:" << height2 << " currentheight: " << height().value() <<"\n";
setColumnWidth(0, WLength(width - 100 - 50));
setColumnWidth(1, WLength(100));
}

/**
* Calculate how many visible rows there are in the tree
*/
int calculate_nr_of_visible_rows(const WModelIndex& index) {
WStandardItemModel *model__ = dynamic_cast<WStandardItemModel *>(model());
WStandardItem *item__ = model__->itemFromIndex(index);
int count__ = 1;
if(isExpanded(index)) {
// count it's children
for(int i=0;i<item__->rowCount();i++) {
WStandardItem *child__ = item__->child(i, 0);
WModelIndex child_index__ = model__->indexFromItem(child__);
count__ += calculate_nr_of_visible_rows(child_index__);
}
}
return count__;
}

/**
* React on collapsing
*/
void item_collapsed() {
evaluate_height();
}

/**
* React on expansions
*/
void item_expanded() {
evaluate_height();
}

/**
* Set our height based on the nr of visible rows
*/
void evaluate_height() {
int row_count__ = calculate_nr_of_visible_rows(model()->index(0,0));
std::cout << "count: " << row_count__ << "\n";
resize(Wt::WLength::Auto, rowHeight().value()*(row_count__ + 1));
layout_->update_layout();
}
private:
MyApplication::layout_class *layout_;
};


};

MyApplication::MyApplication(const Wt::WEnvironment& env)
: Wt::WApplication(env)
{
setTitle("Test");

// our groupbox
WGroupBox *box = new WGroupBox(root());
// should grow it's height based on the size of the tree
layout_class *layout = new layout_class();
box->setLayout(layout, AlignTop | AlignJustify);
// create the tree
MyApplication::treetest *treeview = new MyApplication::treetest(layout);
// add the tree and let it be sized
layout->addWidget(treeview);
}

WApplication* createApplication(const Wt::WEnvironment& env)
{
WApplication* app = new MyApplication(env);
return app;
}

int main(int argc, char *argv[])
{
return Wt::WRun(argc, argv, &createApplication);
}
    (1-1/1)