TreeNode Class Reference
[Treelist example]

Example implementation of a single tree list node. More...

#include <TreeNode.h>

Inheritance diagram for TreeNode:

Inheritance graph
[legend]

List of all members.

Public Slots

void collapse ()
 Collapse this node.
void expand ()
 Expand this node.

Public Member Functions

 TreeNode (const std::string labelText, Wt::TextFormat labelFormat, IconPair *labelIcon, Wt::WContainerWidget *parent=0)
 Construct a tree node with the given label.
void addChildNode (TreeNode *node)
 Add a child node.
void removeChildNode (TreeNode *node)
 Remove a child node.
const std::vector< TreeNode * > & childNodes () const
 Get the list of children.

Private Types

enum  ImageIndex { Middle = 0, Last = 1 }
 Two sets of images, for a normal node, and for the last node. More...

Private Member Functions

void adjustExpandIcon ()
 Adjust the expand icon.
bool isLastChildNode () const
 Returns if is the last child within its parent (is rendered differently).
void childNodesChanged ()
 Rerender when children have changed.
void undoCollapse ()
 Undo function for prelearning collapse().
void undoExpand ()
 Undo function for prelearning expand().

Private Attributes

std::vector< TreeNode * > childNodes_
 List of child nodes.
TreeNodeparentNode_
 The parent node.
Wt::WTablelayout_
 Layout (2x2 table).
IconPairexpandIcon_
 The icon for expanding or collapsing.
Wt::WImagenoExpandIcon_
 The single image shown instead of the expand/collapse icon when no children.
IconPairlabelIcon_
 The icon next to the label.
Wt::WTextlabelText_
 The label.
Wt::WTextchildCountLabel_
 The children count '(x)' for x children.
Wt::WContainerWidgetexpandedContent_
 The container in which the children are managed.
bool wasCollapsed_
 Was collapsed (for undo of prelearned collapse() and expand() slots.

Static Private Attributes

static std::string imageLine_ []
static std::string imagePlus_ []
static std::string imageMin_ []


Detailed Description

Example implementation of a single tree list node.

This is an example of a basic treelist implementation. As of version 1.1.8, a more flexible treenode implementation is included as part of the library: WTreeNode.

A tree list is constructed by nesting TreeNode objects in a tree hierarchy.

A TreeNode has a label, and optionally a two-state label icon, which defines a different image depending on the state of the node (expanded or collapsed). When the node has any children, a child count is also indicated.

Next to the icons, two style classes determine the look of a TreeNode: the label has style "treenodelabel", and the child count has as style "treenodechildcount".

Use CSS nested selectors to apply different styles to different treenodes. For example, to style the treenode with style class "mynode":

The behaviour of the tree node is to collapse all children when the node is expanded (this is similar to how most tree node implementations work).

The widget uses a number of images which must be available in an "icons/" folder (see the Wt treelist examples).

This widget is part of the Wt treelist example.

Definition at line 55 of file TreeNode.h.


Member Enumeration Documentation

enum TreeNode::ImageIndex [private]

Two sets of images, for a normal node, and for the last node.

Enumerator:
Middle 
Last 

Definition at line 140 of file TreeNode.h.

00140 { Middle = 0, Last = 1 };


Constructor & Destructor Documentation

TreeNode::TreeNode ( const std::string  labelText,
Wt::TextFormat  labelFormat,
IconPair labelIcon,
Wt::WContainerWidget parent = 0 
)

Construct a tree node with the given label.

The label is formatted in a WText with the given formatting. The labelIcon (if not 0) will appear next to the label and its state will reflect the expand/collapse state of the node.

Optionally, a userContent widget may be associated with the node. When expanded, this widget will be shown below the widget, but above any of the children nodes.

Definition at line 25 of file TreeNode.C.

00029   : Wt::WCompositeWidget(parent),
00030     parentNode_(0),
00031     labelIcon_(labelIcon)
00032 {
00033   // pre-learned stateless implementations ...
00034   implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
00035   implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);
00036 
00037   // ... or auto-learned stateless implementations
00038   // which do not need undo functions
00039   //implementStateless(&TreeNode::expand);
00040   //implementStateless(&TreeNode::collapse);
00041 
00042   setImplementation(layout_ = new Wt::WTable());
00043 
00044   expandIcon_ = new IconPair(imagePlus_[Last], imageMin_[Last]);
00045   expandIcon_->hide();
00046   noExpandIcon_ = new Wt::WImage(imageLine_[Last]);
00047 
00048   expandedContent_ = new Wt::WContainerWidget();
00049   expandedContent_->hide();
00050 
00051   labelText_ = new Wt::WText(labelText);
00052   labelText_->setTextFormat(labelFormat);
00053   labelText_->setStyleClass("treenodelabel");
00054   childCountLabel_ = new Wt::WText();
00055   childCountLabel_->setMargin(7, Wt::Left);
00056   childCountLabel_->setStyleClass("treenodechildcount");
00057 
00058   layout_->elementAt(0, 0)->addWidget(expandIcon_);
00059   layout_->elementAt(0, 0)->addWidget(noExpandIcon_);
00060 
00061   if (labelIcon_) {
00062     layout_->elementAt(0, 1)->addWidget(labelIcon_);
00063     labelIcon_->setVerticalAlignment(Wt::AlignMiddle);
00064   }
00065   layout_->elementAt(0, 1)->addWidget(labelText_);
00066   layout_->elementAt(0, 1)->addWidget(childCountLabel_);
00067 
00068   layout_->elementAt(1, 1)->addWidget(expandedContent_);
00069 
00070   layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop);
00071   layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle);
00072 
00073   expandIcon_->icon1Clicked.connect(SLOT(this, TreeNode::expand));
00074   expandIcon_->icon2Clicked.connect(SLOT(this, TreeNode::collapse));
00075 } //


Member Function Documentation

void TreeNode::addChildNode ( TreeNode node  ) 

Add a child node.

Definition at line 85 of file TreeNode.C.

00086 {
00087   childNodes_.push_back(node);
00088   node->parentNode_ = this;
00089 
00090   expandedContent_->addWidget(node);
00091 
00092   childNodesChanged();
00093 }

void TreeNode::removeChildNode ( TreeNode node  ) 

Remove a child node.

Definition at line 95 of file TreeNode.C.

00096 {
00097   childNodes_.erase(find(childNodes_.begin(), childNodes_.end(), node));
00098 
00099   node->parentNode_ = 0;
00100 
00101   expandedContent_->removeWidget(node);
00102 
00103   childNodesChanged();
00104 } //

const std::vector<TreeNode *>& TreeNode::childNodes (  )  const [inline]

Get the list of children.

Definition at line 82 of file TreeNode.h.

00082 { return childNodes_; }

void TreeNode::collapse (  )  [slot]

Collapse this node.

Definition at line 123 of file TreeNode.C.

00124 {
00125   wasCollapsed_ = expandedContent_->isHidden();
00126 
00127   expandIcon_->setState(0);
00128   expandedContent_->hide();
00129   if (labelIcon_)
00130     labelIcon_->setState(0);
00131 } //

void TreeNode::expand (  )  [slot]

Expand this node.

Definition at line 133 of file TreeNode.C.

00134 {
00135   wasCollapsed_ = expandedContent_->isHidden();
00136 
00137   expandIcon_->setState(1);
00138   expandedContent_->show();
00139   if (labelIcon_)
00140     labelIcon_->setState(1);
00141 
00142   /*
00143    * collapse all children
00144    */
00145   for (unsigned i = 0; i < childNodes_.size(); ++i)
00146     childNodes_[i]->collapse();
00147 } //

void TreeNode::adjustExpandIcon (  )  [private]

Adjust the expand icon.

Definition at line 177 of file TreeNode.C.

00178 {
00179   ImageIndex index = isLastChildNode() ? Last : Middle;
00180 
00181   if (expandIcon_->icon1()->imageRef() != imagePlus_[index])
00182     expandIcon_->icon1()->setImageRef(imagePlus_[index]);
00183   if (expandIcon_->icon2()->imageRef() != imageMin_[index])
00184     expandIcon_->icon2()->setImageRef(imageMin_[index]);
00185   if (noExpandIcon_->imageRef() != imageLine_[index])
00186     noExpandIcon_->setImageRef(imageLine_[index]);
00187 
00188   if (index == Last) {
00189     layout_->elementAt(0, 0)
00190       ->decorationStyle().setBackgroundImage("");
00191     layout_->elementAt(1, 0)
00192       ->decorationStyle().setBackgroundImage("");
00193   } else {
00194     layout_->elementAt(0, 0)
00195       ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
00196                                              Wt::WCssDecorationStyle::RepeatY);
00197     layout_->elementAt(1, 0)
00198       ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
00199                                              Wt::WCssDecorationStyle::RepeatY);
00200   } //
00201 
00202   if (childNodes_.empty()) {
00203     if (noExpandIcon_->isHidden()) {
00204       noExpandIcon_->show();
00205       expandIcon_->hide();
00206     }
00207   } else {
00208     if (expandIcon_->isHidden()) {
00209       noExpandIcon_->hide();
00210       expandIcon_->show();
00211     }
00212   }
00213 } //

bool TreeNode::isLastChildNode (  )  const [private]

Returns if is the last child within its parent (is rendered differently).

Definition at line 77 of file TreeNode.C.

00078 {
00079   if (parentNode_) {
00080     return parentNode_->childNodes_.back() == this;
00081   } else
00082     return true;
00083 }

void TreeNode::childNodesChanged (  )  [private]

Rerender when children have changed.

Definition at line 106 of file TreeNode.C.

00107 {
00108   for (unsigned i = 0; i < childNodes_.size(); ++i)
00109     childNodes_[i]->adjustExpandIcon();
00110 
00111   adjustExpandIcon();
00112 
00113   if (childNodes_.size())
00114     childCountLabel_
00115       ->setText("(" + boost::lexical_cast<std::string>(childNodes_.size())
00116                 + ")");
00117   else
00118     childCountLabel_->setText("");
00119 
00120   resetLearnedSlots();
00121 } //

void TreeNode::undoCollapse (  )  [private]

Undo function for prelearning collapse().

Definition at line 149 of file TreeNode.C.

00150 {
00151   if (!wasCollapsed_) {
00152     // re-expand
00153     expandIcon_->setState(1);
00154     expandedContent_->show();
00155     if (labelIcon_)
00156       labelIcon_->setState(1);    
00157   }
00158 }

void TreeNode::undoExpand (  )  [private]

Undo function for prelearning expand().

Definition at line 160 of file TreeNode.C.

00161 {
00162   if (wasCollapsed_) {
00163     // re-collapse
00164     expandIcon_->setState(0);
00165     expandedContent_->hide();
00166     if (labelIcon_)
00167       labelIcon_->setState(0);
00168   }
00169 
00170   /*
00171    * undo collapse of children
00172    */
00173   for (unsigned i = 0; i < childNodes_.size(); ++i)
00174     childNodes_[i]->undoCollapse();  
00175 } //


Member Data Documentation

std::vector<TreeNode *> TreeNode::childNodes_ [private]

List of child nodes.

Definition at line 95 of file TreeNode.h.

The parent node.

Definition at line 98 of file TreeNode.h.

Layout (2x2 table).

Definition at line 101 of file TreeNode.h.

The icon for expanding or collapsing.

Definition at line 104 of file TreeNode.h.

The single image shown instead of the expand/collapse icon when no children.

Definition at line 107 of file TreeNode.h.

The icon next to the label.

Definition at line 110 of file TreeNode.h.

The label.

Definition at line 113 of file TreeNode.h.

The children count '(x)' for x children.

Definition at line 116 of file TreeNode.h.

The container in which the children are managed.

Definition at line 119 of file TreeNode.h.

bool TreeNode::wasCollapsed_ [private]

Was collapsed (for undo of prelearned collapse() and expand() slots.

Definition at line 131 of file TreeNode.h.

std::string TreeNode::imageLine_ [static, private]

Initial value:

 { "icons/line-middle.gif",
                                        "icons/line-last.gif" }

Definition at line 142 of file TreeNode.h.

std::string TreeNode::imagePlus_ [static, private]

Initial value:

 { "icons/nav-plus-line-middle.gif",
                                        "icons/nav-plus-line-last.gif" }

Definition at line 143 of file TreeNode.h.

std::string TreeNode::imageMin_ [static, private]

Initial value:

 { "icons/nav-minus-line-middle.gif",
                                       "icons/nav-minus-line-last.gif" }

Definition at line 144 of file TreeNode.h.


The documentation for this class was generated from the following files:

Generated on Tue Nov 3 15:34:40 2009 for Wt by doxygen 1.5.6