Wt examples: TreeNode Class Reference

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
TreeNode Class Reference
Treelist example

Example implementation of a single tree list node. More...

`#include <TreeNode.h>`

Inheritance diagram for TreeNode:
[legend]

List of all members.

## Public Member Functions
| TreeNode (const std::string labelText, Wt::TextFormat labelFormat, IconPair *labelIcon, Wt::WContainerWidget *parent=0)
| Construct a tree node with the given label.
void | addChildNode (TreeNode *node)
| Adds a child node.
void | removeChildNode (TreeNode *node)
| Removes a child node.
const std::vector< TreeNode * > & | childNodes () const
| Returns the list of children.
void | collapse ()
| Collapses this node.
void | expand ()
| Expands this node.
## Private Types
enum | ImageIndex { Middle = 0, Last = 1 }
| Two sets of images, for a normal node, and for the last node. More...
## Private Member Functions
void | adjustExpandIcon ()
| Adjust the expand icon.
bool | isLastChildNode () const
| Returns if is the last child within its parent (is rendered differently)
void | childNodesChanged ()
| Rerender when children have changed.
void | undoCollapse ()
| Undo function for prelearning collapse()
void | undoExpand ()
| Undo function for prelearning expand()
## Private Attributes
std::vector< TreeNode * > | childNodes_
| List of child nodes.
TreeNode * | parentNode_
| The parent node.
Wt::WTable * | layout_
| Layout (2x2 table).
IconPair * | expandIcon_
| The icon for expanding or collapsing.
Wt::WImage * | noExpandIcon_
| The single image shown instead of the expand/collapse icon when no children.
IconPair * | labelIcon_
| The icon next to the label.
Wt::WText * | labelText_
| The label.
Wt::WText * | childCountLabel_
| The children count '(x)' for x children.
Wt::WContainerWidget * | expandedContent_
| The container in which the children are managed.
bool | wasCollapsed_
| Was collapsed (for undo of prelearned collapse() and expand() slots.
## Static Private Attributes
static std::string | imageLine_ []
static std::string | imagePlus_ []
static std::string | imageMin_ []

## Detailed Description

Example implementation of a single tree list node.

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.

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.

Definition at line 55 of file TreeNode.h.

## Member Enumeration Documentation

enum TreeNode::ImageIndex`[private]`

Two sets of images, for a normal node, and for the last node.

Enumerator:

  Middle |
  Last |

Definition at line 139 of file TreeNode.h.

    { Middle = 0, Last = 1 };

## Constructor & Destructor Documentation

TreeNode::TreeNode | ( | const std::string | labelText,
|  | Wt::TextFormat | labelFormat,
|  | IconPair * | labelIcon,
|  | Wt::WContainerWidget * | parent = `0`
| ) |  |

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.

Definition at line 26 of file TreeNode.C.

      : Wt::WCompositeWidget(parent),
        parentNode_(0),
        labelIcon_(labelIcon)
    {
      // pre-learned stateless implementations ...
      implementStateless(&TreeNode::expand, &TreeNode::undoExpand);
      implementStateless(&TreeNode::collapse, &TreeNode::undoCollapse);

      // ... or auto-learned stateless implementations
      // which do not need undo functions
      //implementStateless(&TreeNode::expand);
      //implementStateless(&TreeNode::collapse);

      setImplementation(layout_ = new Wt::WTable());

      expandIcon_ = new IconPair(imagePlus_[Last], imageMin_[Last]);
      expandIcon_->hide();
      noExpandIcon_ = new Wt::WImage(imageLine_[Last]);

      expandedContent_ = new Wt::WContainerWidget();
      expandedContent_->hide();

      labelText_ = new Wt::WText(labelText);
      labelText_->setTextFormat(labelFormat);
      labelText_->setStyleClass("treenodelabel");
      childCountLabel_ = new Wt::WText();
      childCountLabel_->setMargin(7, Wt::Left);
      childCountLabel_->setStyleClass("treenodechildcount");

      layout_->elementAt(0, 0)->addWidget(expandIcon_);
      layout_->elementAt(0, 0)->addWidget(noExpandIcon_);

      if (labelIcon_) {
        layout_->elementAt(0, 1)->addWidget(labelIcon_);
        labelIcon_->setVerticalAlignment(Wt::AlignMiddle);
      }
      layout_->elementAt(0, 1)->addWidget(labelText_);
      layout_->elementAt(0, 1)->addWidget(childCountLabel_);

      layout_->elementAt(1, 1)->addWidget(expandedContent_);

      layout_->elementAt(0, 0)->setContentAlignment(Wt::AlignTop);
      layout_->elementAt(0, 1)->setContentAlignment(Wt::AlignMiddle);

      expandIcon_->icon1Clicked.connect(this, &TreeNode::expand);
      expandIcon_->icon2Clicked.connect(this, &TreeNode::collapse);
    } //

## Member Function Documentation

void TreeNode::addChildNode | ( | TreeNode * | node | ) |

Adds a child node.

Definition at line 86 of file TreeNode.C.

    {
      childNodes_.push_back(node);
      node->parentNode_ = this;

      expandedContent_->addWidget(node);

      childNodesChanged();
    }

void TreeNode::adjustExpandIcon | ( |  | ) | `[private]`

Adjust the expand icon.

Definition at line 178 of file TreeNode.C.

    {
      ImageIndex index = isLastChildNode() ? Last : Middle;

      if (expandIcon_->icon1()->imageLink().url() != imagePlus_[index])
        expandIcon_->icon1()->setImageLink(imagePlus_[index]);
      if (expandIcon_->icon2()->imageLink().url() != imageMin_[index])
        expandIcon_->icon2()->setImageLink(imageMin_[index]);
      if (noExpandIcon_->imageLink().url() != imageLine_[index])
        noExpandIcon_->setImageLink(imageLine_[index]);

      if (index == Last) {
        layout_->elementAt(0, 0)
          ->decorationStyle().setBackgroundImage("");
        layout_->elementAt(1, 0)
          ->decorationStyle().setBackgroundImage("");
      } else {
        layout_->elementAt(0, 0)
          ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
                                                 Wt::WCssDecorationStyle::RepeatY);
        layout_->elementAt(1, 0)
          ->decorationStyle().setBackgroundImage("icons/line-trunk.gif",
                                                 Wt::WCssDecorationStyle::RepeatY);
      } //

      if (childNodes_.empty()) {
        if (noExpandIcon_->isHidden()) {
          noExpandIcon_->show();
          expandIcon_->hide();
        }
      } else {
        if (expandIcon_->isHidden()) {
          noExpandIcon_->hide();
          expandIcon_->show();
        }
      }
    } //

const std::vector<TreeNode *>& TreeNode::childNodes | ( |  | ) | const`[inline]`

Returns the list of children.

Definition at line 82 of file TreeNode.h.

    { return childNodes_; }

void TreeNode::childNodesChanged | ( |  | ) | `[private]`

Rerender when children have changed.

Definition at line 107 of file TreeNode.C.

    {
      for (unsigned i = 0; i < childNodes_.size(); ++i)
        childNodes_[i]->adjustExpandIcon();

      adjustExpandIcon();

      if (childNodes_.size())
        childCountLabel_
          ->setText("(" + boost::lexical_cast<std::string>(childNodes_.size())
                    + ")");
      else
        childCountLabel_->setText("");

      resetLearnedSlots();
    } //

void TreeNode::collapse | ( |  | ) |

Collapses this node.

Definition at line 124 of file TreeNode.C.

    {
      wasCollapsed_ = expandedContent_->isHidden();

      expandIcon_->setState(0);
      expandedContent_->hide();
      if (labelIcon_)
        labelIcon_->setState(0);
    } //

void TreeNode::expand | ( |  | ) |

Expands this node.

Definition at line 134 of file TreeNode.C.

    {
      wasCollapsed_ = expandedContent_->isHidden();

      expandIcon_->setState(1);
      expandedContent_->show();
      if (labelIcon_)
        labelIcon_->setState(1);

      /*
       * collapse all children
       */
      for (unsigned i = 0; i < childNodes_.size(); ++i)
        childNodes_[i]->collapse();
    } //

bool TreeNode::isLastChildNode | ( |  | ) | const`[private]`

Returns if is the last child within its parent (is rendered differently)

Definition at line 78 of file TreeNode.C.

    {
      if (parentNode_) {
        return parentNode_->childNodes_.back() == this;
      } else
        return true;
    }

void TreeNode::removeChildNode | ( | TreeNode * | node | ) |

Removes a child node.

Definition at line 96 of file TreeNode.C.

    {
      childNodes_.erase(std::find(childNodes_.begin(), childNodes_.end(), node));

      node->parentNode_ = 0;

      expandedContent_->removeWidget(node);

      childNodesChanged();
    } //

void TreeNode::undoCollapse | ( |  | ) | `[private]`

Undo function for prelearning collapse()

Definition at line 150 of file TreeNode.C.

    {
      if (!wasCollapsed_) {
        // re-expand
        expandIcon_->setState(1);
        expandedContent_->show();
        if (labelIcon_)
          labelIcon_->setState(1);
      }
    }

void TreeNode::undoExpand | ( |  | ) | `[private]`

Undo function for prelearning expand()

Definition at line 161 of file TreeNode.C.

    {
      if (wasCollapsed_) {
        // re-collapse
        expandIcon_->setState(0);
        expandedContent_->hide();
        if (labelIcon_)
          labelIcon_->setState(0);
      }

      /*
       * undo collapse of children
       */
      for (unsigned i = 0; i < childNodes_.size(); ++i)
        childNodes_[i]->undoCollapse();
    } //

## Member Data Documentation

Wt::WText* TreeNode::childCountLabel_`[private]`

The children count '(x)' for x children.

Definition at line 115 of file TreeNode.h.

std::vector<TreeNode *> TreeNode::childNodes_`[private]`

List of child nodes.

Definition at line 94 of file TreeNode.h.

Wt::WContainerWidget* TreeNode::expandedContent_`[private]`

The container in which the children are managed.

Definition at line 118 of file TreeNode.h.

IconPair* TreeNode::expandIcon_`[private]`

The icon for expanding or collapsing.

Definition at line 103 of file TreeNode.h.

std::string TreeNode::imageLine_`[static, private]`

Initial value:

     { "icons/line-middle.gif",
                                           "icons/line-last.gif" }

Definition at line 141 of file TreeNode.h.

std::string TreeNode::imageMin_`[static, private]`

Initial value:

     { "icons/nav-minus-line-middle.gif",
                                          "icons/nav-minus-line-last.gif" }

Definition at line 143 of file TreeNode.h.

std::string TreeNode::imagePlus_`[static, private]`

Initial value:

     { "icons/nav-plus-line-middle.gif",
                                           "icons/nav-plus-line-last.gif" }

Definition at line 142 of file TreeNode.h.

IconPair* TreeNode::labelIcon_`[private]`

The icon next to the label.

Definition at line 109 of file TreeNode.h.

Wt::WText* TreeNode::labelText_`[private]`

The label.

Definition at line 112 of file TreeNode.h.

Wt::WTable* TreeNode::layout_`[private]`

Layout (2x2 table).

Definition at line 100 of file TreeNode.h.

Wt::WImage* TreeNode::noExpandIcon_`[private]`

The single image shown instead of the expand/collapse icon when no children.

Definition at line 106 of file TreeNode.h.

TreeNode* TreeNode::parentNode_`[private]`

The parent node.

Definition at line 97 of file TreeNode.h.

bool TreeNode::wasCollapsed_`[private]`

Was collapsed (for undo of prelearned collapse() and expand() slots.

Definition at line 130 of file TreeNode.h.

The documentation for this class was generated from the following files:

- /home/koen/project/wt/public-git/wt/examples/treelist/TreeNode.h
- /home/koen/project/wt/public-git/wt/examples/treelist/TreeNode.C