/home/koen/project/wt/cvs/wt/examples/wt-homepage/TreeListExample.C

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 #include <boost/lexical_cast.hpp>
00007 
00008 #include <Wt/WBreak>
00009 #include <Wt/WImage>
00010 #include <Wt/WLabel>
00011 #include <Wt/WGroupBox>
00012 #include <Wt/WLineEdit>
00013 #include <Wt/WPushButton>
00014 #include <Wt/WText>
00015 
00016 #include "TreeListExample.h"
00017 #include "TreeNode.h"
00018 #include "IconPair.h"
00019 
00020 TreeListExample::TreeListExample(WContainerWidget *parent)
00021   : WContainerWidget(parent),
00022     testCount_(0)
00023 {
00024   tree_ = makeTreeMap("TreeListExample", 0);
00025   addWidget(tree_);
00026   tree_->expand();
00027 
00028   TreeNode *treelist = makeTreeMap("Tree List", tree_);
00029   TreeNode *wstateicon = makeTreeMap("class IconPair", treelist);
00030   makeTreeFile("IconPair.h", wstateicon);
00031   makeTreeFile("IconPair.C", wstateicon);
00032   TreeNode *wtreenode = makeTreeMap("class TreeNode", treelist);
00033   makeTreeFile("TreeNode.h", wtreenode);
00034   makeTreeFile("TreeNode.C", wtreenode);
00035   TreeNode *wtreeexample = makeTreeMap("class TreeListExample", treelist);
00036   makeTreeFile("TreeListExample.h", wtreeexample);
00037   makeTreeFile("TreeListExample.C", wtreeexample);
00038 
00039   testMap_ = makeTreeMap("Test map", tree_);
00040 
00041   /*
00042    * Buttons to demonstrate dynamically changing the tree contents
00043    * implies no magic at all.
00044    */
00045   addWidget
00046     (new WText("<p>Use the following buttons to change the "
00047                "contents of the Test map:</p>"));
00048 
00049   WGroupBox *addBox = new WGroupBox("Add map", this);
00050 
00051   WLabel *mapNameLabel = new WLabel("Map name:", addBox);
00052   mapNameLabel->setMargin(WLength(1, WLength::FontEx), Right);
00053   mapNameEdit_ = new WLineEdit("Map", addBox);
00054   mapNameLabel->setBuddy(mapNameEdit_);
00055 
00056   /*
00057    * Example of validation: make the map name mandatory, and give
00058    * feedback when invalid.
00059    */
00060   mapNameEdit_->setValidator(new WValidator(true));
00061 
00062   addMapButton_ = new WPushButton("Add map", addBox);
00063   addMapButton_->clicked.connect(SLOT(this, TreeListExample::addMap));
00064 
00065   new WBreak(this);
00066 
00067   WGroupBox *removeBox = new WGroupBox("Remove map", this);
00068 
00069   removeMapButton_
00070     = new WPushButton("Remove map", removeBox);
00071   removeMapButton_->clicked.connect(SLOT(this, TreeListExample::removeMap));
00072   removeMapButton_->disable();
00073 }
00074 
00075 void TreeListExample::addMap()
00076 {
00077   if (mapNameEdit_->validate() == WValidator::Valid) {
00078     TreeNode *node
00079       = makeTreeMap(mapNameEdit_->text() + " "
00080                     + boost::lexical_cast<std::string>(++testCount_),
00081                     testMap_);
00082     makeTreeFile("File " + boost::lexical_cast<std::string>(testCount_),
00083                  node);
00084 
00085     removeMapButton_->enable();
00086   } else
00087     mapNameEdit_->setStyleClass("Wt-invalid");
00088 }
00089 
00090 void TreeListExample::removeMap()
00091 {
00092   int numMaps = testMap_->childNodes().size();
00093 
00094   if (numMaps > 0) {
00095     int c = rand() % numMaps;
00096 
00097     TreeNode *child = testMap_->childNodes()[c];
00098     testMap_->removeChildNode(child);
00099     delete child;
00100 
00101     if (numMaps == 1)
00102       removeMapButton_->disable();
00103   }
00104 }
00105 
00106 TreeNode *TreeListExample::makeTreeMap(const WString& name,
00107                                        TreeNode *parent)
00108 {
00109   IconPair *labelIcon
00110     = new IconPair("icons/yellow-folder-closed.png",
00111                    "icons/yellow-folder-open.png",
00112                    false);
00113 
00114   TreeNode *node = new TreeNode(name, WText::PlainFormatting, labelIcon, 0);
00115   if (parent)
00116     parent->addChildNode(node);
00117 
00118   return node;
00119 }
00120 
00121 TreeNode *TreeListExample::makeTreeFile(const WString& name,
00122                                         TreeNode *parent)
00123 {
00124   IconPair *labelIcon
00125     = new IconPair("icons/document.png", "icons/yellow-folder-open.png",
00126                    false);
00127 
00128   TreeNode *node = new TreeNode("<a href=\"" + name + "\" target=\"_blank\">"
00129                                 + name + "</a>", WText::XHTMLFormatting,
00130                                 labelIcon, 0);
00131   if (parent)
00132     parent->addChildNode(node);
00133 
00134   return node;
00135 }

Generated on Fri Jul 25 17:05:59 2008 for Wt by doxygen 1.5.3