Wt examples  4.2.2
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 std::find;
17 using namespace Wt;
18 
19 std::string TreeNode::imageLine_[] = { "icons/line-middle.gif",
20  "icons/line-last.gif" };
21 std::string TreeNode::imagePlus_[] = { "icons/nav-plus-line-middle.gif",
22  "icons/nav-plus-line-last.gif" };
23 std::string TreeNode::imageMin_[] = { "icons/nav-minus-line-middle.gif",
24  "icons/nav-minus-line-last.gif" };
25 
26 TreeNode::TreeNode(const std::string labelText,
27  TextFormat labelFormat,
28  std::unique_ptr<IconPair> labelIcon)
29  : WCompositeWidget(),
30  parentNode_(nullptr),
31  labelIcon_(labelIcon.get())
32 {
33  // pre-learned stateless implementations ...
34  implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
35  implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);
36 
37  // ... or auto-learned stateless implementations
38  // which do not need undo functions
39  //implementStateless(&TreeNode::expand);
40  //implementStateless(&TreeNode::collapse);
41 
42  auto layout = cpp14::make_unique<WTable>();
43  layout_ = layout.get();
44  setImplementation(std::move(layout));
45 
46  auto expandIcon =
47  cpp14::make_unique<IconPair>(imagePlus_[Last], imageMin_[Last]);
48  expandIcon_ = expandIcon.get();
49  expandIcon_->hide();
50 
51  auto noExpandIcon =
52  cpp14::make_unique<WImage>(imageLine_[Last]);
53 
54  noExpandIcon_ = noExpandIcon.get();
55 
56  auto expandedContent = cpp14::make_unique<WContainerWidget>();
57  expandedContent_ = expandedContent.get();
58  expandedContent_->hide();
59 
60  labelText_ = cpp14::make_unique<WText>(labelText);
61  labelText_->setTextFormat(labelFormat);
62  labelText_->setStyleClass("treenodelabel");
63 
64  auto childCountLabel = cpp14::make_unique<WText>();
65  childCountLabel_ = childCountLabel.get();
66  childCountLabel_->setMargin(7, Side::Left);
67  childCountLabel_->setStyleClass("treenodechildcount");
68 
69  layout_->elementAt(0, 0)->addWidget(std::move(expandIcon));
70  layout_->elementAt(0, 0)->addWidget(std::move(noExpandIcon));
71 
72  if (labelIcon) {
73  layout_->elementAt(0, 1)->addWidget(std::move(labelIcon));
74  labelIcon_->setVerticalAlignment(AlignmentFlag::Middle);
75  }
76  layout_->elementAt(0, 1)->addWidget(std::move(labelText_));
77  layout_->elementAt(0, 1)->addWidget(std::move(childCountLabel));
78 
79  layout_->elementAt(1, 1)->addWidget(std::move(expandedContent));
80 
81  layout_->elementAt(0, 0)->setContentAlignment(AlignmentFlag::Top);
82  layout_->elementAt(0, 1)->setContentAlignment(AlignmentFlag::Middle);
83 
84  expandIcon_->icon1Clicked->connect(this, &TreeNode::expand);
86 } //
87 
89 {
90  if (parentNode_) {
91  return parentNode_->childNodes_.back() == this;
92  } else
93  return true;
94 }
95 
96 void TreeNode::addChildNode(std::unique_ptr<TreeNode> node)
97 {
98  childNodes_.push_back(node.get());
99  node->parentNode_ = this;
100 
101  expandedContent_->addWidget(std::move(node));
102 
104 }
105 
106 void TreeNode::removeChildNode(TreeNode *node, int index)
107 {
108  childNodes_.erase(childNodes_.begin() + index);
109 
110  node->parentNode_ = nullptr;
111 
112  expandedContent_->removeWidget(node);
113 
115 } //
116 
118 {
119  for (auto node : childNodes_)
120  node->adjustExpandIcon();
121 
123 
124  if (childNodes_.size())
126  ->setText("(" + std::to_string(childNodes_.size())
127  + ")");
128  else
129  childCountLabel_->setText("");
130 
131  resetLearnedSlots();
132 } //
133 
135 {
136  wasCollapsed_ = expandedContent_->isHidden();
137 
138  expandIcon_->setState(0);
139  expandedContent_->hide();
140  if (labelIcon_)
141  labelIcon_->setState(0);
142 } //
143 
145 {
146  wasCollapsed_ = expandedContent_->isHidden();
147 
148  expandIcon_->setState(1);
149  expandedContent_->show();
150  if (labelIcon_)
151  labelIcon_->setState(1);
152 
153  /*
154  * collapse all children
155  */
156  for (auto node : childNodes_)
157  node->collapse();
158 } //
159 
161 {
162  if (!wasCollapsed_) {
163  // re-expand
164  expandIcon_->setState(1);
165  expandedContent_->show();
166  if (labelIcon_)
167  labelIcon_->setState(1);
168  }
169 }
170 
172 {
173  if (wasCollapsed_) {
174  // re-collapse
175  expandIcon_->setState(0);
176  expandedContent_->hide();
177  if (labelIcon_)
178  labelIcon_->setState(0);
179  }
180 
181  /*
182  * undo collapse of children
183  */
184  for (auto node : childNodes_)
185  node->undoCollapse();
186 } //
187 
189 {
190  ImageIndex index = isLastChildNode() ? Last : Middle;
191 
192  if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
193  expandIcon_->icon1()->setImageLink(imagePlus_[index]);
194  if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
195  expandIcon_->icon2()->setImageLink(imageMin_[index]);
196  if (noExpandIcon_->imageLink().url() != imageLine_[index])
197  noExpandIcon_->setImageLink(imageLine_[index]);
198 
199  if (index == Last) {
200  layout_->elementAt(0, 0)
201  ->decorationStyle().setBackgroundImage("");
202  layout_->elementAt(1, 0)
203  ->decorationStyle().setBackgroundImage("");
204  } else {
205  layout_->elementAt(0, 0)
206  ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
207  Orientation::Vertical);
208  layout_->elementAt(1, 0)
209  ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
210  Orientation::Vertical);
211  } //
212 
213  if (childNodes_.empty()) {
214  if (noExpandIcon_->isHidden()) {
215  noExpandIcon_->show();
216  expandIcon_->hide();
217  }
218  } else {
219  if (expandIcon_->isHidden()) {
220  noExpandIcon_->hide();
221  expandIcon_->show();
222  }
223  }
224 } //
WContainerWidget * expandedContent_
The container in which the children are managed.
Definition: TreeNode.h:120
WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:67
void undoExpand()
Undo function for prelearning expand()
Definition: TreeNode.C:171
TreeNode(const std::string labelText, TextFormat labelFormat, std::unique_ptr< IconPair > labelIcon)
Construct a tree node with the given label.
Definition: TreeNode.C:26
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:160
void expand()
Expands this node.
Definition: TreeNode.C:144
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition: TreeNode.h:141
EventSignal< WMouseEvent > * icon2Clicked
Signal emitted when clicked while in state 1 (icon 2 is shown).
Definition: IconPair.h:95
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:99
void setState(int num)
Set which icon should be visible.
Definition: IconPair.C:50
WText * childCountLabel_
The children count '(x)' for x children.
Definition: TreeNode.h:117
EventSignal< WMouseEvent > * icon1Clicked
Signal emitted when clicked while in state 0 (icon 1 is shown).
Definition: IconPair.h:90
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:96
static std::string imageMin_[]
Definition: TreeNode.h:145
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:111
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:132
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition: TreeNode.C:88
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition: TreeNode.h:105
static std::string imageLine_[]
Definition: TreeNode.h:143
void removeChildNode(TreeNode *node, int index)
Removes a child node.
Definition: TreeNode.C:106
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:117
static std::string imagePlus_[]
Definition: TreeNode.h:144
void addChildNode(std::unique_ptr< TreeNode > node)
Adds a child node.
Definition: TreeNode.C:96
WTable * layout_
Layout (2x2 table).
Definition: TreeNode.h:102
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:188
WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:108
WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:63
std::unique_ptr< WText > labelText_
The label.
Definition: TreeNode.h:114
Example implementation of a single tree list node.
Definition: TreeNode.h:57
void collapse()
Collapses this node.
Definition: TreeNode.C:134

Generated on Fri Mar 20 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.15