Wt examples  3.7.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 #include <boost/lexical_cast.hpp>
7 
8 #include <Wt/WTable>
9 #include <Wt/WTableCell>
10 #include <Wt/WImage>
11 #include <Wt/WText>
12 #include <Wt/WCssDecorationStyle>
13 
14 #include "TreeNode.h"
15 #include "IconPair.h"
16 
17 using std::find;
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  Wt::TextFormat labelFormat,
28  IconPair *labelIcon,
29  Wt::WContainerWidget *parent)
30  : Wt::WCompositeWidget(parent),
31  parentNode_(0),
32  labelIcon_(labelIcon)
33 {
34  // pre-learned stateless implementations ...
37 
38  // ... or auto-learned stateless implementations
39  // which do not need undo functions
40  //implementStateless(&TreeNode::expand);
41  //implementStateless(&TreeNode::collapse);
42 
44 
46  expandIcon_->hide();
47  noExpandIcon_ = new Wt::WImage(imageLine_[Last]);
48 
51 
52  labelText_ = new Wt::WText(labelText);
53  labelText_->setTextFormat(labelFormat);
54  labelText_->setStyleClass("treenodelabel");
56  childCountLabel_->setMargin(7, Wt::Left);
57  childCountLabel_->setStyleClass("treenodechildcount");
58 
61 
62  if (labelIcon_) {
64  labelIcon_->setVerticalAlignment(Wt::AlignMiddle);
65  }
68 
70 
71  layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop);
72  layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle);
73 
76 } //
77 
79 {
80  if (parentNode_) {
81  return parentNode_->childNodes_.back() == this;
82  } else
83  return true;
84 }
85 
87 {
88  childNodes_.push_back(node);
89  node->parentNode_ = this;
90 
92 
94 }
95 
97 {
98  childNodes_.erase(std::find(childNodes_.begin(), childNodes_.end(), node));
99 
100  node->parentNode_ = 0;
101 
103 
105 } //
106 
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
120 
122 } //
123 
125 {
127 
128  expandIcon_->setState(0);
130  if (labelIcon_)
131  labelIcon_->setState(0);
132 } //
133 
135 {
137 
138  expandIcon_->setState(1);
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 } //
149 
151 {
152  if (!wasCollapsed_) {
153  // re-expand
154  expandIcon_->setState(1);
156  if (labelIcon_)
157  labelIcon_->setState(1);
158  }
159 }
160 
162 {
163  if (wasCollapsed_) {
164  // re-collapse
165  expandIcon_->setState(0);
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 } //
177 
179 {
180  ImageIndex index = isLastChildNode() ? Last : Middle;
181 
182  if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
184  if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
186  if (noExpandIcon_->imageLink().url() != imageLine_[index])
188 
189  if (index == Last) {
190  layout_->elementAt(0, 0)
192  layout_->elementAt(1, 0)
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 } //
An icon pair (identical to WIconPair)
Definition: IconPair.h:34
void undoExpand()
Undo function for prelearning expand()
Definition: TreeNode.C:161
virtual void setStyleClass(const WString &styleClass) override
Wt::WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:61
const WLink & imageLink() const
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:150
virtual void setVerticalAlignment(AlignmentFlag alignment, const WLength &length=WLength::Auto) override
void expand()
Expands this node.
Definition: TreeNode.C:134
virtual WCssDecorationStyle & decorationStyle() override
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition: TreeNode.h:139
TreeNode * parentNode_
The parent node.
Definition: TreeNode.h:97
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
virtual bool isHidden() const override
Wt::WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:65
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
WStatelessSlot * implementStateless(void(T::*method)())
Wt::WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:106
void setBackgroundImage(const WLink &link, WFlags< Orientation > repeat=Orientation::Horizontal|Orientation::Vertical, WFlags< Side > sides=None)
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
bool setText(const WString &text)
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
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
static std::string imageMin_[]
Definition: TreeNode.h:143
bool setTextFormat(TextFormat format)
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
WTableCell * elementAt(int row, int column)
void setImplementation(std::unique_ptr< WWidget > widget)
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition: TreeNode.h:130
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition: TreeNode.C:78
void addChildNode(TreeNode *node)
Adds a child node.
Definition: TreeNode.C:86
void resetLearnedSlots()
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
TextFormat
void setImageLink(const WLink &link)
virtual std::unique_ptr< WWidget > removeWidget(WWidget *widget) override
void childNodesChanged()
Rerender when children have changed.
Definition: TreeNode.C:107
static std::string imagePlus_[]
Definition: TreeNode.h:142
TreeNode(const std::string labelText, Wt::TextFormat labelFormat, IconPair *labelIcon, Wt::WContainerWidget *parent=0)
Construct a tree node with the given label.
Definition: TreeNode.C:26
void removeChildNode(TreeNode *node)
Removes a child node.
Definition: TreeNode.C:96
Wt::Signals::connection connect(F function)
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:178
virtual bool isHidden() const override
virtual void addWidget(std::unique_ptr< WWidget > widget)
Example implementation of a single tree list node.
Definition: TreeNode.h:55
void collapse()
Collapses this node.
Definition: TreeNode.C:124

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13