Wt examples
3.3.0
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.3.0
examples
treelist
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
45
expandIcon_
=
new
IconPair
(
imagePlus_
[
Last
],
imageMin_
[Last]);
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
);
75
expandIcon_
->
icon2Clicked
.
connect
(
this
, &
TreeNode::collapse
);
76
}
//
77
78
bool
TreeNode::isLastChildNode
()
const
79
{
80
if
(
parentNode_
) {
81
return
parentNode_
->
childNodes_
.back() ==
this
;
82
}
else
83
return
true
;
84
}
85
86
void
TreeNode::addChildNode
(
TreeNode
*node)
87
{
88
childNodes_
.push_back(node);
89
node->
parentNode_
=
this
;
90
91
expandedContent_
->addWidget(node);
92
93
childNodesChanged
();
94
}
95
96
void
TreeNode::removeChildNode
(
TreeNode
*node)
97
{
98
childNodes_
.erase(std::find(
childNodes_
.begin(),
childNodes_
.end(), node));
99
100
node->
parentNode_
= 0;
101
102
expandedContent_
->removeWidget(node);
103
104
childNodesChanged
();
105
}
//
106
107
void
TreeNode::childNodesChanged
()
108
{
109
for
(
unsigned
i = 0; i <
childNodes_
.size(); ++i)
110
childNodes_
[i]->
adjustExpandIcon
();
111
112
adjustExpandIcon
();
113
114
if
(
childNodes_
.size())
115
childCountLabel_
116
->setText(
"("
+ boost::lexical_cast<std::string>(
childNodes_
.size())
117
+
")"
);
118
else
119
childCountLabel_
->setText(
""
);
120
121
resetLearnedSlots();
122
}
//
123
124
void
TreeNode::collapse
()
125
{
126
wasCollapsed_
=
expandedContent_
->
isHidden
();
127
128
expandIcon_
->
setState
(0);
129
expandedContent_
->hide();
130
if
(
labelIcon_
)
131
labelIcon_
->
setState
(0);
132
}
//
133
134
void
TreeNode::expand
()
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
150
void
TreeNode::undoCollapse
()
151
{
152
if
(!
wasCollapsed_
) {
153
// re-expand
154
expandIcon_
->
setState
(1);
155
expandedContent_
->show();
156
if
(
labelIcon_
)
157
labelIcon_
->
setState
(1);
158
}
159
}
160
161
void
TreeNode::undoExpand
()
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
178
void
TreeNode::adjustExpandIcon
()
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
}
//
Generated on Fri May 31 2013 for
the C++ Web Toolkit (Wt)
by
1.8.3.1