Wt examples 3.3.12
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
17using std::find;
18
19std::string TreeNode::imageLine_[] = { "icons/line-middle.gif",
20 "icons/line-last.gif" };
21std::string TreeNode::imagePlus_[] = { "icons/nav-plus-line-middle.gif",
22 "icons/nav-plus-line-last.gif" };
23std::string TreeNode::imageMin_[] = { "icons/nav-minus-line-middle.gif",
24 "icons/nav-minus-line-last.gif" };
25
26TreeNode::TreeNode(const std::string labelText,
27 Wt::TextFormat labelFormat,
28 IconPair *labelIcon,
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
48
51
52 labelText_ = new Wt::WText(labelText);
53 labelText_->setTextFormat(labelFormat);
54 labelText_->setStyleClass("treenodelabel");
57 childCountLabel_->setStyleClass("treenodechildcount");
58
61
62 if (labelIcon_) {
65 }
68
70
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
133
135{
137
140 if (labelIcon_)
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
156 if (labelIcon_)
157 labelIcon_->setState(1);
158 }
159}
160
162{
163 if (wasCollapsed_) {
164 // re-collapse
167 if (labelIcon_)
169 }
170
171 /*
172 * undo collapse of children
173 */
174 for (unsigned i = 0; i < childNodes_.size(); ++i)
176} //
177
179{
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",
198 layout_->elementAt(1, 0)
199 ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
201 } //
202
203 if (childNodes_.empty()) {
204 if (noExpandIcon_->isHidden()) {
206 expandIcon_->hide();
207 }
208 } else {
209 if (expandIcon_->isHidden()) {
211 expandIcon_->show();
212 }
213 }
214} //
An icon pair (identical to WIconPair)
Definition IconPair.h:35
void setState(int num)
Set which icon should be visible.
Definition IconPair.C:41
Wt::WImage * icon1() const
Get the first icon image.
Definition IconPair.h:61
Wt::WImage * icon2() const
Get the second icon image.
Definition IconPair.h:65
Wt::EventSignal< Wt::WMouseEvent > & icon2Clicked
Signal emitted when clicked while in state 1 (icon 2 is shown).
Definition IconPair.h:93
Wt::EventSignal< Wt::WMouseEvent > & icon1Clicked
Signal emitted when clicked while in state 0 (icon 1 is shown).
Definition IconPair.h:88
Example implementation of a single tree list node.
Definition TreeNode.h:56
void undoCollapse()
Undo function for prelearning collapse()
Definition TreeNode.C:150
Wt::WContainerWidget * expandedContent_
The container in which the children are managed.
Definition TreeNode.h:118
std::vector< TreeNode * > childNodes_
List of child nodes.
Definition TreeNode.h:94
void undoExpand()
Undo function for prelearning expand()
Definition TreeNode.C:161
Wt::WTable * layout_
Layout (2x2 table).
Definition TreeNode.h:100
void childNodesChanged()
Rerender when children have changed.
Definition TreeNode.C:107
static std::string imageLine_[]
Definition TreeNode.h:19
IconPair * expandIcon_
The icon for expanding or collapsing.
Definition TreeNode.h:103
bool wasCollapsed_
Was collapsed (for undo of prelearned collapse() and expand() slots.
Definition TreeNode.h:130
Wt::WText * childCountLabel_
The children count '(x)' for x children.
Definition TreeNode.h:115
static std::string imagePlus_[]
Definition TreeNode.h:21
void collapse()
Collapses this node.
Definition TreeNode.C:124
bool isLastChildNode() const
Returns if is the last child within its parent (is rendered differently)
Definition TreeNode.C:78
ImageIndex
Two sets of images, for a normal node, and for the last node.
Definition TreeNode.h:139
Wt::WText * labelText_
The label.
Definition TreeNode.h:112
Wt::WImage * noExpandIcon_
The single image shown instead of the expand/collapse icon when no children.
Definition TreeNode.h:106
void addChildNode(TreeNode *node)
Adds a child node.
Definition TreeNode.C:86
void removeChildNode(TreeNode *node)
Removes a child node.
Definition TreeNode.C:96
static std::string imageMin_[]
Definition TreeNode.h:23
void expand()
Expands this node.
Definition TreeNode.C:134
TreeNode * parentNode_
The parent node.
Definition TreeNode.h:97
void adjustExpandIcon()
Adjust the expand icon.
Definition TreeNode.C:178
IconPair * labelIcon_
The icon next to the label.
Definition TreeNode.h:109
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
Wt::Signals::connection connect(const F &function)
virtual bool isHidden() const
void setImplementation(WWidget *widget)
virtual void setVerticalAlignment(AlignmentFlag alignment, const WLength &length=WLength::Auto)
virtual void addWidget(WWidget *widget)
virtual void removeWidget(WWidget *widget)
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
void setBackgroundImage(const WLink &link, Repeat repeat=RepeatXY, WFlags< Side > sides=0)
void setImageLink(const WLink &link)
const WLink & imageLink() const
void resetLearnedSlots()
WStatelessSlot * implementStateless(void(T::*method)())
WTableCell * elementAt(int row, int column)
bool setText(const WString &text)
bool setTextFormat(TextFormat format)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=All)
virtual WCssDecorationStyle & decorationStyle()
virtual bool isHidden() const
virtual void setStyleClass(const WString &styleClass)
TextFormat
AlignMiddle
AlignTop

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8