Wt examples  3.3.6
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TreeNode Class Reference

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

#include <TreeNode.h>

Inheritance diagram for TreeNode:
Inheritance graph
[legend]

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. More...
 
void addChildNode (TreeNode *node)
 Adds a child node. More...
 
void removeChildNode (TreeNode *node)
 Removes a child node. More...
 
const std::vector< TreeNode * > & childNodes () const
 Returns the list of children. More...
 
void collapse ()
 Collapses this node. More...
 
void expand ()
 Expands this node. More...
 

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. More...
 
bool isLastChildNode () const
 Returns if is the last child within its parent (is rendered differently) More...
 
void childNodesChanged ()
 Rerender when children have changed. More...
 
void undoCollapse ()
 Undo function for prelearning collapse() More...
 
void undoExpand ()
 Undo function for prelearning expand() More...
 

Private Attributes

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

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

§ ImageIndex

enum TreeNode::ImageIndex
private

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

Enumerator
Middle 
Last 

Definition at line 139 of file TreeNode.h.

139 { Middle = 0, Last = 1 };

Constructor & Destructor Documentation

§ TreeNode()

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 26 of file TreeNode.C.

30  : Wt::WCompositeWidget(parent),
31  parentNode_(0),
32  labelIcon_(labelIcon)
33 {
34  // pre-learned stateless implementations ...
35  implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
36  implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);
37 
38  // ... or auto-learned stateless implementations
39  // which do not need undo functions
40  //implementStateless(&TreeNode::expand);
41  //implementStateless(&TreeNode::collapse);
42 
43  setImplementation(layout_ = new Wt::WTable());
44 
46  expandIcon_->hide();
47  noExpandIcon_ = new Wt::WImage(imageLine_[Last]);
48 
49  expandedContent_ = new Wt::WContainerWidget();
50  expandedContent_->hide();
51 
52  labelText_ = new Wt::WText(labelText);
53  labelText_->setTextFormat(labelFormat);
54  labelText_->setStyleClass("treenodelabel");
55  childCountLabel_ = new Wt::WText();
56  childCountLabel_->setMargin(7, Wt::Left);
57  childCountLabel_->setStyleClass("treenodechildcount");
58 
59  layout_->elementAt(0, 0)->addWidget(expandIcon_);
60  layout_->elementAt(0, 0)->addWidget(noExpandIcon_);
61 
62  if (labelIcon_) {
63  layout_->elementAt(0, 1)->addWidget(labelIcon_);
64  labelIcon_->setVerticalAlignment(Wt::AlignMiddle);
65  }
66  layout_->elementAt(0, 1)->addWidget(labelText_);
67  layout_->elementAt(0, 1)->addWidget(childCountLabel_);
68 
69  layout_->elementAt(1, 1)->addWidget(expandedContent_);
70 
71  layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop);
72  layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle);
73 
74  expandIcon_->icon1Clicked.connect(this, &TreeNode::expand);
76 } //
An icon pair (identical to WIconPair)
Definition: IconPair.h:34
void undoExpand()
Undo function for prelearning expand()
Definition: TreeNode.C:161
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:150
void expand()
Expands this node.
Definition: TreeNode.C:134
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:97
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
Wt::WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:106
Wt::EventSignal< Wt::WMouseEvent > & icon1Clicked
Signal emitted when clicked while in state 0 (icon 1 is shown).
Definition: IconPair.h:88
Wt::WTable * layout_
Layout (2x2 table).
Definition: TreeNode.h:100
Wt::WText * childCountLabel_
The children count &#39;(x)&#39; for x children.
Definition: TreeNode.h:115
Wt::WText * labelText_
The label.
Definition: TreeNode.h:112
static std::string imageMin_[]
Definition: TreeNode.h:143
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103
Wt::EventSignal< Wt::WMouseEvent > & icon2Clicked
Signal emitted when clicked while in state 1 (icon 2 is shown).
Definition: IconPair.h:93
static std::string imageLine_[]
Definition: TreeNode.h:141
static std::string imagePlus_[]
Definition: TreeNode.h:142
void collapse()
Collapses this node.
Definition: TreeNode.C:124

Member Function Documentation

§ addChildNode()

void TreeNode::addChildNode ( TreeNode node)

Adds a child node.

Definition at line 86 of file TreeNode.C.

87 {
88  childNodes_.push_back(node);
89  node->parentNode_ = this;
90 
91  expandedContent_->addWidget(node);
92 
94 }
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:97
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:107

§ adjustExpandIcon()

void TreeNode::adjustExpandIcon ( )
private

Adjust the expand icon.

Definition at line 178 of file TreeNode.C.

179 {
180  ImageIndex index = isLastChildNode() ? Last : Middle;
181 
182  if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
183  expandIcon_->icon1()->setImageLink(imagePlus_[index]);
184  if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
185  expandIcon_->icon2()->setImageLink(imageMin_[index]);
186  if (noExpandIcon_->imageLink().url() != imageLine_[index])
187  noExpandIcon_->setImageLink(imageLine_[index]);
188 
189  if (index == Last) {
190  layout_->elementAt(0, 0)
191  ->decorationStyle().setBackgroundImage("");
192  layout_->elementAt(1, 0)
193  ->decorationStyle().setBackgroundImage("");
194  } else {
195  layout_->elementAt(0, 0)
196  ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
197  Wt::WCssDecorationStyle::RepeatY);
198  layout_->elementAt(1, 0)
199  ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
200  Wt::WCssDecorationStyle::RepeatY);
201  } //
202 
203  if (childNodes_.empty()) {
204  if (noExpandIcon_->isHidden()) {
205  noExpandIcon_->show();
206  expandIcon_->hide();
207  }
208  } else {
209  if (expandIcon_->isHidden()) {
210  noExpandIcon_->hide();
211  expandIcon_->show();
212  }
213  }
214 } //
Wt::WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:61
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition: TreeNode.h:139
Wt::WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:65
Wt::WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:106
Wt::WTable * layout_
Layout (2x2 table).
Definition: TreeNode.h:100
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
static std::string imageMin_[]
Definition: TreeNode.h:143
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition: TreeNode.C:78
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103
static std::string imageLine_[]
Definition: TreeNode.h:141
static std::string imagePlus_[]
Definition: TreeNode.h:142

§ childNodes()

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

Returns the list of children.

Definition at line 82 of file TreeNode.h.

82 { return childNodes_; }
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94

§ childNodesChanged()

void TreeNode::childNodesChanged ( )
private

Rerender when children have changed.

Definition at line 107 of file TreeNode.C.

108 {
109  for (unsigned i = 0; i < childNodes_.size(); ++i)
111 
113 
114  if (childNodes_.size())
116  ->setText("(" + boost::lexical_cast<std::string>(childNodes_.size())
117  + ")");
118  else
119  childCountLabel_->setText("");
120 
121  resetLearnedSlots();
122 } //
Wt::WText * childCountLabel_
The children count &#39;(x)&#39; for x children.
Definition: TreeNode.h:115
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:178

§ collapse()

void TreeNode::collapse ( )

Collapses this node.

Definition at line 124 of file TreeNode.C.

125 {
126  wasCollapsed_ = expandedContent_->isHidden();
127 
128  expandIcon_->setState(0);
129  expandedContent_->hide();
130  if (labelIcon_)
131  labelIcon_->setState(0);
132 } //
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:41
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:130
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103

§ expand()

void TreeNode::expand ( )

Expands this node.

Definition at line 134 of file TreeNode.C.

135 {
136  wasCollapsed_ = expandedContent_->isHidden();
137 
138  expandIcon_->setState(1);
139  expandedContent_->show();
140  if (labelIcon_)
141  labelIcon_->setState(1);
142 
143  /*
144  * collapse all children
145  */
146  for (unsigned i = 0; i < childNodes_.size(); ++i)
147  childNodes_[i]->collapse();
148 } //
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:41
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:130
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103
void collapse()
Collapses this node.
Definition: TreeNode.C:124

§ isLastChildNode()

bool TreeNode::isLastChildNode ( ) const
private

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

Definition at line 78 of file TreeNode.C.

79 {
80  if (parentNode_) {
81  return parentNode_->childNodes_.back() == this;
82  } else
83  return true;
84 }
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:97
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94

§ removeChildNode()

void TreeNode::removeChildNode ( TreeNode node)

Removes a child node.

Definition at line 96 of file TreeNode.C.

97 {
98  childNodes_.erase(std::find(childNodes_.begin(), childNodes_.end(), node));
99 
100  node->parentNode_ = 0;
101 
102  expandedContent_->removeWidget(node);
103 
105 } //
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:97
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:107

§ undoCollapse()

void TreeNode::undoCollapse ( )
private

Undo function for prelearning collapse()

Definition at line 150 of file TreeNode.C.

151 {
152  if (!wasCollapsed_) {
153  // re-expand
154  expandIcon_->setState(1);
155  expandedContent_->show();
156  if (labelIcon_)
157  labelIcon_->setState(1);
158  }
159 }
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:41
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:130
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103

§ undoExpand()

void TreeNode::undoExpand ( )
private

Undo function for prelearning expand()

Definition at line 161 of file TreeNode.C.

162 {
163  if (wasCollapsed_) {
164  // re-collapse
165  expandIcon_->setState(0);
166  expandedContent_->hide();
167  if (labelIcon_)
168  labelIcon_->setState(0);
169  }
170 
171  /*
172  * undo collapse of children
173  */
174  for (unsigned i = 0; i < childNodes_.size(); ++i)
175  childNodes_[i]->undoCollapse();
176 } //
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:150
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:41
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:118
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:130
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:103

Member Data Documentation

§ childCountLabel_

Wt::WText* TreeNode::childCountLabel_
private

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

Definition at line 115 of file TreeNode.h.

§ childNodes_

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

List of child nodes.

Definition at line 94 of file TreeNode.h.

§ expandedContent_

Wt::WContainerWidget* TreeNode::expandedContent_
private

The container in which the children are managed.

Definition at line 118 of file TreeNode.h.

§ expandIcon_

IconPair* TreeNode::expandIcon_
private

The icon for expanding or collapsing.

Definition at line 103 of file TreeNode.h.

§ imageLine_

std::string TreeNode::imageLine_
staticprivate
Initial value:
= { "icons/line-middle.gif",
"icons/line-last.gif" }

Definition at line 141 of file TreeNode.h.

§ imageMin_

std::string TreeNode::imageMin_
staticprivate
Initial value:
= { "icons/nav-minus-line-middle.gif",
"icons/nav-minus-line-last.gif" }

Definition at line 143 of file TreeNode.h.

§ imagePlus_

std::string TreeNode::imagePlus_
staticprivate
Initial value:
= { "icons/nav-plus-line-middle.gif",
"icons/nav-plus-line-last.gif" }

Definition at line 142 of file TreeNode.h.

§ labelIcon_

IconPair* TreeNode::labelIcon_
private

The icon next to the label.

Definition at line 109 of file TreeNode.h.

§ labelText_

Wt::WText* TreeNode::labelText_
private

The label.

Definition at line 112 of file TreeNode.h.

§ layout_

Wt::WTable* TreeNode::layout_
private

Layout (2x2 table).

Definition at line 100 of file TreeNode.h.

§ noExpandIcon_

Wt::WImage* TreeNode::noExpandIcon_
private

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

Definition at line 106 of file TreeNode.h.

§ parentNode_

TreeNode* TreeNode::parentNode_
private

The parent node.

Definition at line 97 of file TreeNode.h.

§ wasCollapsed_

bool TreeNode::wasCollapsed_
private

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

Definition at line 130 of file TreeNode.h.


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

Generated on Thu Jan 12 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.12