Wt examples  4.10.4
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 
16 using namespace Wt;
17 
18 std::string TreeNode::imageLine_[] = { "icons/line-middle.gif",
19  "icons/line-last.gif" };
20 std::string TreeNode::imagePlus_[] = { "icons/nav-plus-line-middle.gif",
21  "icons/nav-plus-line-last.gif" };
22 std::string TreeNode::imageMin_[] = { "icons/nav-minus-line-middle.gif",
23  "icons/nav-minus-line-last.gif" };
24 
25 TreeNode::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 ...
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 
43  expandIcon_->hide();
44 
46 
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 
81 void 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 
91 void TreeNode::removeChildNode(TreeNode *node, int index)
92 {
93  childNodes_.erase(childNodes_.begin() + index);
94 
95  node->parentNode_ = nullptr;
96 
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 {
113  }
114 
116 } //
117 
119 {
121 
122  expandIcon_->setState(0);
124  if (labelIcon_)
125  labelIcon_->setState(0);
126 } //
127 
129 {
131 
132  expandIcon_->setState(1);
134  if (labelIcon_)
135  labelIcon_->setState(1);
136 
137  /*
138  * collapse all children
139  */
140  for (auto node : childNodes_)
141  node->collapse();
142 } //
143 
145 {
146  if (!wasCollapsed_) {
147  // re-expand
148  expandIcon_->setState(1);
150  if (labelIcon_)
151  labelIcon_->setState(1);
152  }
153 }
154 
156 {
157  if (wasCollapsed_) {
158  // re-collapse
159  expandIcon_->setState(0);
161  if (labelIcon_)
162  labelIcon_->setState(0);
163  }
164 
165  /*
166  * undo collapse of children
167  */
168  for (auto node : childNodes_)
169  node->undoCollapse();
170 } //
171 
173 {
174  ImageIndex index = isLastChildNode() ? Last : Middle;
175 
176  if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
178  if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
180  if (noExpandIcon_->imageLink().url() != imageLine_[index])
182 
183  if (index == Last) {
184  layout_->elementAt(0, 0)
186  layout_->elementAt(1, 0)
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
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:50
WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:63
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
Wt::Signals::connection connect(F function)
virtual void setVerticalAlignment(AlignmentFlag alignment, const WLength &length=WLength::Auto) override
virtual bool isHidden() const override
virtual std::unique_ptr< WWidget > removeWidget(WWidget *widget) override
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
Widget * addNew(Args &&...args)
virtual void addWidget(std::unique_ptr< WWidget > widget)
void setBackgroundImage(const WLink &link, WFlags< Orientation > repeat=Orientation::Horizontal|Orientation::Vertical, WFlags< Side > sides=None)
const WLink & imageLink() const
void setImageLink(const WLink &link)
void resetLearnedSlots()
WStatelessSlot * implementStateless(void(T::*method)())
WTableCell * elementAt(int row, int column)
bool setText(const WString &text)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
virtual void setStyleClass(const WString &styleClass) override
virtual bool isHidden() const override
virtual WCssDecorationStyle & decorationStyle() override
TextFormat