Advertisement
Guest User

Untitled

a guest
Sep 5th, 2014
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wt/WGlobal>
  2. #include <Wt/WCompositeWidget>
  3. #include <Wt/WContainerWidget>
  4. #include <Wt/WVBoxLayout>
  5. #include <Wt/WTreeView>
  6. #include <Wt/WPushButton>
  7. #include <Wt/WStandardItemModel>
  8. #include <Wt/WStandardItem>
  9.  
  10. #include <boost/filesystem.hpp>
  11.  
  12.  
  13. const char* pathToWTDev = "C:\\Wt";
  14.  
  15. class __Test_LargeTree : public Wt::WCompositeWidget {
  16.    Wt::WTreeView* tree;
  17.    bool toogle;
  18.  
  19.    void DoFolder (const boost::filesystem::path& path, Wt::WStandardItem* parent);
  20.    void Toogle ();
  21.  
  22. public:
  23.    __Test_LargeTree (Wt::WContainerWidget* parent = nullptr);
  24. };
  25.  
  26.  
  27. __Test_LargeTree::__Test_LargeTree (Wt::WContainerWidget* parent)
  28.    : WCompositeWidget(parent), tree(nullptr), toogle(false)
  29. {
  30.    auto impl = new Wt::WContainerWidget;
  31.    setImplementation(impl);
  32.  
  33.    auto layout = new Wt::WVBoxLayout(impl);
  34.  
  35.    tree = new Wt::WTreeView;
  36.    layout->addWidget(tree, 1);
  37.  
  38.    auto model = new Wt::WStandardItemModel(0, 1, tree);
  39.    model->setHeaderData(0, Wt::Horizontal, "name");
  40.  
  41.    tree->setAlternatingRowColors(true);
  42.    tree->setModel(model);
  43.  
  44.    auto btnToogle = new Wt::WPushButton("Toogle");;
  45.    layout->addWidget(btnToogle, 0, Wt::AlignCenter);
  46.  
  47.    btnToogle->clicked().connect(this, &__Test_LargeTree::Toogle);
  48.  
  49.    DoFolder(boost::filesystem::path(pathToWTDev), model->invisibleRootItem());
  50. }
  51.  
  52. void __Test_LargeTree::DoFolder (const boost::filesystem::path& path, Wt::WStandardItem* parent)
  53. {
  54.    boost::filesystem::directory_iterator end;
  55.  
  56.    for (auto it = boost::filesystem::directory_iterator(path); it != end; ++it) {
  57.  
  58.       if (boost::filesystem::is_directory(it->path())) {
  59.          auto row = new Wt::WStandardItem(it->path().string());
  60.          parent->appendRow(row);
  61.          DoFolder(*it, row);
  62.       }
  63.       else {
  64.          parent->appendRow(new Wt::WStandardItem(it->path().string()));
  65.       }
  66.  
  67.    }
  68. }
  69.  
  70. static void ExpandNodeRecursive (Wt::WTreeView* tree, const Wt::WModelIndex& index, bool expand)
  71. {
  72.    int rowCount = tree->model()->rowCount(index);
  73.  
  74.    // ignore invisible root item
  75.    if (index.isValid() && rowCount > 0) tree->setExpanded(index, expand);
  76.  
  77.    for (int i = 0; i < rowCount; ++i) {
  78.       ExpandNodeRecursive(tree, tree->model()->index(i, 0, index), expand);
  79.    }
  80. }
  81.  
  82. void __Test_LargeTree::Toogle ()
  83. {
  84.    toogle = !toogle;
  85.    ExpandNodeRecursive(tree, Wt::WModelIndex(), toogle);
  86. }
Advertisement
RAW Paste Data Copied
Advertisement