Wt examples  4.9.1
Loading...
Searching...
No Matches
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:
[legend]

Public Member Functions

 TreeNode (const std::string labelText, TextFormat labelFormat, std::unique_ptr< IconPair > labelIcon)
 Construct a tree node with the given label.
 
void addChildNode (std::unique_ptr< TreeNode > node)
 Adds a child node.
 
void removeChildNode (TreeNode *node, int index)
 Removes a child node.
 
const std::vector< TreeNode * > & childNodes () const
 Returns the list of children.
 
void collapse ()
 Collapses this node.
 
void expand ()
 Expands this node.
 

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.
 
WTable * layout_
 Layout (2x2 table).
 
IconPairexpandIcon_
 The icon for expanding or collapsing.
 
WImage * noExpandIcon_
 The single image shown instead of the expand/collapse icon when no children.
 
IconPairlabelIcon_
 The icon next to the label.
 
WText * childCountLabel_
 The children count '(x)' for x children.
 
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_ []
 

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 57 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 138 of file TreeNode.h.

138{ Middle = 0, Last = 1 };
@ Middle
Definition: TreeNode.h:138

Constructor & Destructor Documentation

◆ TreeNode()

TreeNode::TreeNode ( const std::string  labelText,
TextFormat  labelFormat,
std::unique_ptr< IconPair labelIcon 
)

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.

28 : parentNode_(nullptr),
29 labelIcon_(labelIcon.get())
30{
31 // pre-learned stateless implementations ...
32 implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
33 implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);
34
35 // ... or auto-learned stateless implementations
36 // which do not need undo functions
37 //implementStateless(&TreeNode::expand);
38 //implementStateless(&TreeNode::collapse);
39
40 layout_ = setNewImplementation<WTable>();
41
42 expandIcon_ = layout_->elementAt(0, 0)->addNew<IconPair>(imagePlus_[Last], imageMin_[Last]);
43 expandIcon_->hide();
44
45 noExpandIcon_ = layout_->elementAt(0, 0)->addNew<WImage>(imageLine_[Last]);
46
47 expandedContent_ = layout_->elementAt(1, 1)->addNew<WContainerWidget>();
48 expandedContent_->hide();
49
50 auto labelTextWidget = std::make_unique<WText>(labelText);
51 labelTextWidget->setTextFormat(labelFormat);
52 labelTextWidget->setStyleClass("treenodelabel");
53
54 auto childCountLabel = std::make_unique<WText>();
55 childCountLabel_ = childCountLabel.get();
56 childCountLabel_->setMargin(7, Side::Left);
57 childCountLabel_->setStyleClass("treenodechildcount");
58
59 if (labelIcon) {
60 layout_->elementAt(0, 1)->addWidget(std::move(labelIcon));
61 labelIcon_->setVerticalAlignment(AlignmentFlag::Middle);
62 }
63 layout_->elementAt(0, 1)->addWidget(std::move(labelTextWidget));
64 layout_->elementAt(0, 1)->addWidget(std::move(childCountLabel));
65
66 layout_->elementAt(0, 0)->setContentAlignment(AlignmentFlag::Top);
67 layout_->elementAt(0, 1)->setContentAlignment(AlignmentFlag::Middle);
68
71} //
An icon pair (identical to WIconPair)
Definition: IconPair.h:37
EventSignal< WMouseEvent > * icon2Clicked
Signal emitted when clicked while in state 1 (icon 2 is shown).
Definition: IconPair.h:95
EventSignal< WMouseEvent > * icon1Clicked
Signal emitted when clicked while in state 0 (icon 1 is shown).
Definition: IconPair.h:90
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:144
void undoExpand()
Undo function for prelearning expand()
Definition: TreeNode.C:155
static std::string imageLine_[]
Definition: TreeNode.h:140
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:105
WTable * layout_
Layout (2x2 table).
Definition: TreeNode.h:102
static std::string imagePlus_[]
Definition: TreeNode.h:141
void collapse()
Collapses this node.
Definition: TreeNode.C:118
WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:117
WText * childCountLabel_
The children count '(x)' for x children.
Definition: TreeNode.h:114
static std::string imageMin_[]
Definition: TreeNode.h:142
void expand()
Expands this node.
Definition: TreeNode.C:128
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:99
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:111
WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:108

Member Function Documentation

◆ addChildNode()

void TreeNode::addChildNode ( std::unique_ptr< TreeNode node)

Adds a child node.

Definition at line 81 of file TreeNode.C.

82{
83 childNodes_.push_back(node.get());
84 node->parentNode_ = this;
85
86 expandedContent_->addWidget(std::move(node));
87
89}
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:96
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:102

◆ adjustExpandIcon()

void TreeNode::adjustExpandIcon ( )
private

Adjust the expand icon.

Definition at line 172 of file TreeNode.C.

173{
175
176 if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
177 expandIcon_->icon1()->setImageLink(imagePlus_[index]);
178 if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
179 expandIcon_->icon2()->setImageLink(imageMin_[index]);
180 if (noExpandIcon_->imageLink().url() != imageLine_[index])
181 noExpandIcon_->setImageLink(imageLine_[index]);
182
183 if (index == Last) {
184 layout_->elementAt(0, 0)
185 ->decorationStyle().setBackgroundImage("");
186 layout_->elementAt(1, 0)
187 ->decorationStyle().setBackgroundImage("");
188 } else {
189 layout_->elementAt(0, 0)
190 ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
191 Orientation::Vertical);
192 layout_->elementAt(1, 0)
193 ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
194 Orientation::Vertical);
195 } //
196
197 if (childNodes_.empty()) {
198 if (noExpandIcon_->isHidden()) {
199 noExpandIcon_->show();
200 expandIcon_->hide();
201 }
202 } else {
203 if (expandIcon_->isHidden()) {
204 noExpandIcon_->hide();
205 expandIcon_->show();
206 }
207 }
208} //
WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:63
WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:67
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition: TreeNode.C:73
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition: TreeNode.h:138

◆ childNodes()

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

Returns the list of children.

Definition at line 84 of file TreeNode.h.

84{ return childNodes_; }

◆ childNodesChanged()

void TreeNode::childNodesChanged ( )
private

Rerender when children have changed.

Definition at line 102 of file TreeNode.C.

103{
104 for (auto node : childNodes_)
105 node->adjustExpandIcon();
106
108
109 if (!childNodes_.empty()) {
110 childCountLabel_ ->setText("(" + std::to_string(childNodes_.size()) + ")");
111 } else {
112 childCountLabel_->setText("");
113 }
114
115 resetLearnedSlots();
116} //
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:172

◆ collapse()

void TreeNode::collapse ( )

Collapses this node.

Definition at line 118 of file TreeNode.C.

119{
120 wasCollapsed_ = expandedContent_->isHidden();
121
123 expandedContent_->hide();
124 if (labelIcon_)
126} //
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:50
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:129

◆ expand()

void TreeNode::expand ( )

Expands this node.

Definition at line 128 of file TreeNode.C.

129{
130 wasCollapsed_ = expandedContent_->isHidden();
131
133 expandedContent_->show();
134 if (labelIcon_)
136
137 /*
138 * collapse all children
139 */
140 for (auto node : childNodes_)
141 node->collapse();
142} //

◆ isLastChildNode()

bool TreeNode::isLastChildNode ( ) const
private

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

Definition at line 73 of file TreeNode.C.

74{
75 if (parentNode_) {
76 return parentNode_->childNodes_.back() == this;
77 } else
78 return true;
79}

◆ removeChildNode()

void TreeNode::removeChildNode ( TreeNode node,
int  index 
)

Removes a child node.

Definition at line 91 of file TreeNode.C.

92{
93 childNodes_.erase(childNodes_.begin() + index);
94
95 node->parentNode_ = nullptr;
96
97 expandedContent_->removeWidget(node);
98
100} //

◆ undoCollapse()

void TreeNode::undoCollapse ( )
private

Undo function for prelearning collapse()

Definition at line 144 of file TreeNode.C.

145{
146 if (!wasCollapsed_) {
147 // re-expand
149 expandedContent_->show();
150 if (labelIcon_)
152 }
153}

◆ undoExpand()

void TreeNode::undoExpand ( )
private

Undo function for prelearning expand()

Definition at line 155 of file TreeNode.C.

156{
157 if (wasCollapsed_) {
158 // re-collapse
160 expandedContent_->hide();
161 if (labelIcon_)
163 }
164
165 /*
166 * undo collapse of children
167 */
168 for (auto node : childNodes_)
169 node->undoCollapse();
170} //

Member Data Documentation

◆ childCountLabel_

WText* TreeNode::childCountLabel_
private

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

Definition at line 114 of file TreeNode.h.

◆ childNodes_

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

List of child nodes.

Definition at line 96 of file TreeNode.h.

◆ expandedContent_

WContainerWidget* TreeNode::expandedContent_
private

The container in which the children are managed.

Definition at line 117 of file TreeNode.h.

◆ expandIcon_

IconPair* TreeNode::expandIcon_
private

The icon for expanding or collapsing.

Definition at line 105 of file TreeNode.h.

◆ imageLine_

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

Definition at line 140 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 142 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 141 of file TreeNode.h.

◆ labelIcon_

IconPair* TreeNode::labelIcon_
private

The icon next to the label.

Definition at line 111 of file TreeNode.h.

◆ layout_

WTable* TreeNode::layout_
private

Layout (2x2 table).

Definition at line 102 of file TreeNode.h.

◆ noExpandIcon_

WImage* TreeNode::noExpandIcon_
private

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

Definition at line 108 of file TreeNode.h.

◆ parentNode_

TreeNode* TreeNode::parentNode_
private

The parent node.

Definition at line 99 of file TreeNode.h.

◆ wasCollapsed_

bool TreeNode::wasCollapsed_
private

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

Definition at line 129 of file TreeNode.h.


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