Wt examples  3.3.6
TreeNode.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, 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 ...
35  implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
36  implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);
37 
38  // ... or auto-learned stateless implementations
39  // which do not need undo functions
40  //implementStateless(&TreeNode::expand);
41  //implementStateless(&TreeNode::collapse);
42 
43  setImplementation(layout_ = new Wt::WTable());
44 
46  expandIcon_->hide();
47  noExpandIcon_ = new Wt::WImage(imageLine_[Last]);
48 
49  expandedContent_ = new Wt::WContainerWidget();
50  expandedContent_->hide();
51 
52  labelText_ = new Wt::WText(labelText);
53  labelText_->setTextFormat(labelFormat);
54  labelText_->setStyleClass("treenodelabel");
55  childCountLabel_ = new Wt::WText();
56  childCountLabel_->setMargin(7, Wt::Left);
57  childCountLabel_->setStyleClass("treenodechildcount");
58 
59  layout_->elementAt(0, 0)->addWidget(expandIcon_);
60  layout_->elementAt(0, 0)->addWidget(noExpandIcon_);
61 
62  if (labelIcon_) {
63  layout_->elementAt(0, 1)->addWidget(labelIcon_);
64  labelIcon_->setVerticalAlignment(Wt::AlignMiddle);
65  }
66  layout_->elementAt(0, 1)->addWidget(labelText_);
67  layout_->elementAt(0, 1)->addWidget(childCountLabel_);
68 
69  layout_->elementAt(1, 1)->addWidget(expandedContent_);
70 
71  layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop);
72  layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle);
73 
74  expandIcon_->icon1Clicked.connect(this, &TreeNode::expand);
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 
91  expandedContent_->addWidget(node);
92 
94 }
95 
97 {
98  childNodes_.erase(std::find(childNodes_.begin(), childNodes_.end(), node));
99 
100  node->parentNode_ = 0;
101 
102  expandedContent_->removeWidget(node);
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
119  childCountLabel_->setText("");
120 
121  resetLearnedSlots();
122 } //
123 
125 {
126  wasCollapsed_ = expandedContent_->isHidden();
127 
128  expandIcon_->setState(0);
129  expandedContent_->hide();
130  if (labelIcon_)
131  labelIcon_->setState(0);
132 } //
133 
135 {
136  wasCollapsed_ = expandedContent_->isHidden();
137 
138  expandIcon_->setState(1);
139  expandedContent_->show();
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);
155  expandedContent_->show();
156  if (labelIcon_)
157  labelIcon_->setState(1);
158  }
159 }
160 
162 {
163  if (wasCollapsed_) {
164  // re-collapse
165  expandIcon_->setState(0);
166  expandedContent_->hide();
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])
183  expandIcon_->icon1()->setImageLink(imagePlus_[index]);
184  if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
185  expandIcon_->icon2()->setImageLink(imageMin_[index]);
186  if (noExpandIcon_->imageLink().url() != imageLine_[index])
187  noExpandIcon_->setImageLink(imageLine_[index]);
188 
189  if (index == Last) {
190  layout_->elementAt(0, 0)
191  ->decorationStyle().setBackgroundImage("");
192  layout_->elementAt(1, 0)
193  ->decorationStyle().setBackgroundImage("");
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
Wt::WImage * icon1() const
Get the first icon image.
Definition: IconPair.h:61
void undoCollapse()
Undo function for prelearning collapse()
Definition: TreeNode.C:150
void expand()
Expands this node.
Definition: TreeNode.C:134
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
Wt::WImage * icon2() const
Get the second icon image.
Definition: IconPair.h:65
Wt::WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition: TreeNode.h:106
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
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
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition: TreeNode.h:94
static std::string imageMin_[]
Definition: TreeNode.h:143
IconPair * labelIcon_
The icon next to the label.
Definition: TreeNode.h:109
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
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
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
void adjustExpandIcon()
Adjust the expand icon.
Definition: TreeNode.C:178
Example implementation of a single tree list node.
Definition: TreeNode.h:55
void collapse()
Collapses this node.
Definition: TreeNode.C:124

Generated on Thu Jan 12 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.12