File: TreeNode.h

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (149 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef TREENODE_H_
#define TREENODE_H_

#include <Wt/WCompositeWidget>
#include <Wt/WText>

class IconPair;

namespace Wt {
  class WTable;
  class WImage;
}

/**
 * @addtogroup treelist
 */
/*@{*/

/*! \brief Example implementation of a single tree list node.
 *
 * <i>This is an example of a basic treelist implementation. As of
 * version 1.1.8, a more flexible treenode implementation is included as
 * part of the library: WTreeNode.</i>
 *
 * A tree list is constructed by nesting TreeNode objects in a tree
 * hierarchy.
 *
 * A TreeNode has a label, and optionally a two-state label icon, which
 * defines a different image depending on the state of the node (expanded
 * or collapsed). When the node has any children, a child count is also
 * indicated.
 *
 * Next to the icons, two style classes determine the look of a TreeNode:
 * the label has style "treenodelabel", and the child count has as style
 * "treenodechildcount".
 *
 * Use CSS nested selectors to apply different styles to different treenodes.
 * For example, to style the treenode with style class "mynode":
 *
 * The behaviour of the tree node is to collapse all children when the
 * node is expanded (this is similar to how most tree node implementations
 * work).
 *
 * The widget uses a number of images which must be available in an
 * "icons/" folder (see the %Wt treelist examples).
 *
 * This widget is part of the %Wt treelist example.
 */
class TreeNode : public Wt::WCompositeWidget
{
public:
  /*! \brief Construct a tree node with the given label.
   *
   * The label is formatted in a WText with the given formatting.
   * The labelIcon (if not 0) will appear next to the label and its state
   * will reflect the expand/collapse state of the node.
   *
   * Optionally, a userContent widget may be associated with the node.
   * When expanded, this widget will be shown below the widget, but above
   * any of the children nodes.
   */
  TreeNode(const std::string labelText,
	   Wt::TextFormat labelFormat,
	   IconPair *labelIcon, Wt::WContainerWidget *parent = 0);

  /*! \brief Add a child node.
   */
  void addChildNode(TreeNode *node);

  /*! \brief Remove a child node.
   */
  void removeChildNode(TreeNode *node);

  /*! \brief Get the list of children.
   */
  const std::vector<TreeNode *>& childNodes() const { return childNodes_; }

public slots:
  /*! \brief Collapse this node.
   */
  void collapse();

  /*! \brief Expand this node.
   */
  void expand();

private:
  //! List of child nodes.
  std::vector<TreeNode *> childNodes_;

  //! The parent node.
  TreeNode		   *parentNode_;

  //! Layout (2x2 table).
  Wt::WTable               *layout_;

  //! The icon for expanding or collapsing.
  IconPair                 *expandIcon_;

  //! The single image shown instead of the expand/collapse icon when no children.
  Wt::WImage		   *noExpandIcon_;

  //! The icon next to the label.
  IconPair		   *labelIcon_;

  //! The label.
  Wt::WText		   *labelText_;

  //! The children count '(x)' for x children.
  Wt::WText		   *childCountLabel_;

  //! The container in which the children are managed.
  Wt::WContainerWidget     *expandedContent_;

  //! Adjust the expand icon
  void adjustExpandIcon();

  //! Returns if is the last child within its parent (is rendered differently)
  bool isLastChildNode() const;

  //! Rerender when children have changed.
  void childNodesChanged();

  //! Was collapsed (for undo of prelearned collapse() and expand() slots.
  bool wasCollapsed_;

  //! Undo function for prelearning collapse()
  void undoCollapse();

  //! Undo function for prelearning expand()
  void undoExpand();

  //! Two sets of images, for a normal node, and for the last node.
  enum ImageIndex { Middle = 0, Last = 1 };

  static std::string imageLine_[];
  static std::string imagePlus_[];
  static std::string imageMin_[];
}; //

/*@}*/

#endif // WTREENODE_H_