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

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3