Example implementation of a single tree list node. More...
#include <TreeNode.h>
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. | |
TreeNode * | parentNode_ |
The parent node. | |
Wt::WTable * | layout_ |
Layout (2x2 table). | |
IconPair * | expandIcon_ |
The icon for expanding or collapsing. | |
Wt::WImage * | noExpandIcon_ |
The single image shown instead of the expand/collapse icon when no children. | |
IconPair * | labelIcon_ |
The icon next to the label. | |
Wt::WText * | labelText_ |
The label. | |
Wt::WText * | childCountLabel_ |
The children count '(x)' for x children. | |
Wt::WContainerWidget * | expandedContent_ |
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_ [] |
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.
enum TreeNode::ImageIndex [private] |
Two sets of images, for a normal node, and for the last node.
Definition at line 140 of file TreeNode.h.
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.
: Wt::WCompositeWidget(parent), parentNode_(0), labelIcon_(labelIcon) { // pre-learned stateless implementations ... implementStateless(&TreeNode::expand, &TreeNode::undoExpand); implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse); // ... or auto-learned stateless implementations // which do not need undo functions //implementStateless(&TreeNode::expand); //implementStateless(&TreeNode::collapse); setImplementation(layout_ = new Wt::WTable()); expandIcon_ = new IconPair(imagePlus_[Last], imageMin_[Last]); expandIcon_->hide(); noExpandIcon_ = new Wt::WImage(imageLine_[Last]); expandedContent_ = new Wt::WContainerWidget(); expandedContent_->hide(); labelText_ = new Wt::WText(labelText); labelText_->setTextFormat(labelFormat); labelText_->setStyleClass("treenodelabel"); childCountLabel_ = new Wt::WText(); childCountLabel_->setMargin(7, Wt::Left); childCountLabel_->setStyleClass("treenodechildcount"); layout_->elementAt(0, 0)->addWidget(expandIcon_); layout_->elementAt(0, 0)->addWidget(noExpandIcon_); if (labelIcon_) { layout_->elementAt(0, 1)->addWidget(labelIcon_); labelIcon_->setVerticalAlignment(Wt::AlignMiddle); } layout_->elementAt(0, 1)->addWidget(labelText_); layout_->elementAt(0, 1)->addWidget(childCountLabel_); layout_->elementAt(1, 1)->addWidget(expandedContent_); layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop); layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle); expandIcon_->icon1Clicked.connect(SLOT(this, TreeNode::expand)); expandIcon_->icon2Clicked.connect(SLOT(this, TreeNode::collapse)); } //
void TreeNode::addChildNode | ( | TreeNode * | node ) |
Add a child node.
Definition at line 85 of file TreeNode.C.
{ childNodes_.push_back(node); node->parentNode_ = this; expandedContent_->addWidget(node); childNodesChanged(); }
void TreeNode::adjustExpandIcon | ( | ) | [private] |
Adjust the expand icon.
Definition at line 177 of file TreeNode.C.
{ ImageIndex index = isLastChildNode() ? Last : Middle; if (expandIcon_->icon1()->imageRef() != imagePlus_[index]) expandIcon_->icon1()->setImageRef(imagePlus_[index]); if (expandIcon_->icon2()->imageRef() != imageMin_[index]) expandIcon_->icon2()->setImageRef(imageMin_[index]); if (noExpandIcon_->imageRef() != imageLine_[index]) noExpandIcon_->setImageRef(imageLine_[index]); if (index == Last) { layout_->elementAt(0, 0) ->decorationStyle().setBackgroundImage(""); layout_->elementAt(1, 0) ->decorationStyle().setBackgroundImage(""); } else { layout_->elementAt(0, 0) ->decorationStyle().setBackgroundImage("icons/line-trunk.gif", Wt::WCssDecorationStyle::RepeatY); layout_->elementAt(1, 0) ->decorationStyle().setBackgroundImage("icons/line-trunk.gif", Wt::WCssDecorationStyle::RepeatY); } // if (childNodes_.empty()) { if (noExpandIcon_->isHidden()) { noExpandIcon_->show(); expandIcon_->hide(); } } else { if (expandIcon_->isHidden()) { noExpandIcon_->hide(); expandIcon_->show(); } } } //
const std::vector<TreeNode *>& TreeNode::childNodes | ( | ) | const [inline] |
void TreeNode::childNodesChanged | ( | ) | [private] |
Rerender when children have changed.
Definition at line 106 of file TreeNode.C.
{ for (unsigned i = 0; i < childNodes_.size(); ++i) childNodes_[i]->adjustExpandIcon(); adjustExpandIcon(); if (childNodes_.size()) childCountLabel_ ->setText("(" + boost::lexical_cast<std::string>(childNodes_.size()) + ")"); else childCountLabel_->setText(""); resetLearnedSlots(); } //
void TreeNode::collapse | ( | ) | [slot] |
Collapse this node.
Definition at line 123 of file TreeNode.C.
{ wasCollapsed_ = expandedContent_->isHidden(); expandIcon_->setState(0); expandedContent_->hide(); if (labelIcon_) labelIcon_->setState(0); } //
void TreeNode::expand | ( | ) | [slot] |
Expand this node.
Definition at line 133 of file TreeNode.C.
{ wasCollapsed_ = expandedContent_->isHidden(); expandIcon_->setState(1); expandedContent_->show(); if (labelIcon_) labelIcon_->setState(1); /* * collapse all children */ for (unsigned i = 0; i < childNodes_.size(); ++i) childNodes_[i]->collapse(); } //
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.
{ if (parentNode_) { return parentNode_->childNodes_.back() == this; } else return true; }
void TreeNode::removeChildNode | ( | TreeNode * | node ) |
Remove a child node.
Definition at line 95 of file TreeNode.C.
{ childNodes_.erase(std::find(childNodes_.begin(), childNodes_.end(), node)); node->parentNode_ = 0; expandedContent_->removeWidget(node); childNodesChanged(); } //
void TreeNode::undoCollapse | ( | ) | [private] |
Undo function for prelearning collapse()
Definition at line 149 of file TreeNode.C.
{ if (!wasCollapsed_) { // re-expand expandIcon_->setState(1); expandedContent_->show(); if (labelIcon_) labelIcon_->setState(1); } }
void TreeNode::undoExpand | ( | ) | [private] |
Undo function for prelearning expand()
Definition at line 160 of file TreeNode.C.
{ if (wasCollapsed_) { // re-collapse expandIcon_->setState(0); expandedContent_->hide(); if (labelIcon_) labelIcon_->setState(0); } /* * undo collapse of children */ for (unsigned i = 0; i < childNodes_.size(); ++i) childNodes_[i]->undoCollapse(); } //
Wt::WText* TreeNode::childCountLabel_ [private] |
The children count '(x)' for x children.
Definition at line 116 of file TreeNode.h.
std::vector<TreeNode *> TreeNode::childNodes_ [private] |
List of child nodes.
Definition at line 95 of file TreeNode.h.
Wt::WContainerWidget* TreeNode::expandedContent_ [private] |
The container in which the children are managed.
Definition at line 119 of file TreeNode.h.
IconPair* TreeNode::expandIcon_ [private] |
The icon for expanding or collapsing.
Definition at line 104 of file TreeNode.h.
std::string TreeNode::imageLine_ [static, private] |
{ "icons/line-middle.gif", "icons/line-last.gif" }
Definition at line 142 of file TreeNode.h.
std::string TreeNode::imageMin_ [static, private] |
{ "icons/nav-minus-line-middle.gif", "icons/nav-minus-line-last.gif" }
Definition at line 144 of file TreeNode.h.
std::string TreeNode::imagePlus_ [static, private] |
{ "icons/nav-plus-line-middle.gif", "icons/nav-plus-line-last.gif" }
Definition at line 143 of file TreeNode.h.
IconPair* TreeNode::labelIcon_ [private] |
The icon next to the label.
Definition at line 110 of file TreeNode.h.
Wt::WText* TreeNode::labelText_ [private] |
The label.
Definition at line 113 of file TreeNode.h.
Wt::WTable* TreeNode::layout_ [private] |
Layout (2x2 table).
Definition at line 101 of file TreeNode.h.
Wt::WImage* TreeNode::noExpandIcon_ [private] |
The single image shown instead of the expand/collapse icon when no children.
Definition at line 107 of file TreeNode.h.
TreeNode* TreeNode::parentNode_ [private] |
The parent node.
Definition at line 98 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.