Wt examples 4.6.1
TreeNode.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include <Wt/WTable.h>
8#include <Wt/WTableCell.h>
9#include <Wt/WImage.h>
10#include <Wt/WText.h>
11#include <Wt/WCssDecorationStyle.h>
12
13#include "TreeNode.h"
14#include "IconPair.h"
15
16using namespace Wt;
17
18std::string TreeNode::imageLine_[] = { "icons/line-middle.gif",
19 "icons/line-last.gif" };
20std::string TreeNode::imagePlus_[] = { "icons/nav-plus-line-middle.gif",
21 "icons/nav-plus-line-last.gif" };
22std::string TreeNode::imageMin_[] = { "icons/nav-minus-line-middle.gif",
23 "icons/nav-minus-line-last.gif" };
24
25TreeNode::TreeNode(const std::string labelText,
26 TextFormat labelFormat,
27 std::unique_ptr<IconPair> labelIcon)
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} //
72
74{
75 if (parentNode_) {
76 return parentNode_->childNodes_.back() == this;
77 } else
78 return true;
79}
80
81void TreeNode::addChildNode(std::unique_ptr<TreeNode> node)
82{
83 childNodes_.push_back(node.get());
84 node->parentNode_ = this;
85
86 expandedContent_->addWidget(std::move(node));
87
89}
90
91void TreeNode::removeChildNode(TreeNode *node, int index)
92{
93 childNodes_.erase(childNodes_.begin() + index);
94
95 node->parentNode_ = nullptr;
96
97 expandedContent_->removeWidget(node);
98
100} //
101
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} //
117
119{
120 wasCollapsed_ = expandedContent_->isHidden();
121
123 expandedContent_->hide();
124 if (labelIcon_)
126} //
127
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} //
143
145{
146 if (!wasCollapsed_) {
147 // re-expand
149 expandedContent_->show();
150 if (labelIcon_)
151 labelIcon_->setState(1);
152 }
153}
154
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} //
171
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} //
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
WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:63
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:50
WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:67
EventSignal< WMouseEvent > * icon1Clicked
Signal emitted when clicked while in state 0 (icon 1 is shown).
Definition: IconPair.h:90
Example implementation of a single tree list node.
Definition: TreeNode.h:58
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:144
void removeChildNode(TreeNode *node, int index)
Removes a child node.
Definition: TreeNode.C:91
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:96
void undoExpand()
Undo function for prelearning expand()
Definition: TreeNode.C:155
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:102
static std::string imageLine_[]
Definition: TreeNode.h:140
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:105
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:129
WTable * layout_
Layout (2x2 table).
Definition: TreeNode.h:102
static std::string imagePlus_[]
Definition: TreeNode.h:141
TreeNode(const std::string labelText, TextFormat labelFormat, std::unique_ptr< IconPair > labelIcon)
Construct a tree node with the given label.
Definition: TreeNode.C:25
void collapse()
Collapses this node.
Definition: TreeNode.C:118
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition: TreeNode.C:73
WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:117
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition: TreeNode.h:138
@ Middle
Definition: TreeNode.h:138
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
void addChildNode(std::unique_ptr< TreeNode > node)
Adds a child node.
Definition: TreeNode.C:81
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:99
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:172
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

Generated on Fri Jan 28 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3