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