TreeNode.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::Gui::TreeNode
17* @author Kai Welke ( welke at kit dot edu)
18* @date
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#pragma once
24
25#include <memory>
26
28
29// qt
30#include <QGraphicsEllipseItem>
31#include <QGraphicsScene>
32#include <QPointF>
33#include <QRectF>
34#include <QSize>
35
36namespace armarx
37{
39
40 using TreeNodePtr = std::shared_ptr<TreeNode>;
41 using TreeNodeWeakPtr = std::weak_ptr<TreeNode>;
42
43 class ARMARXCOMPONENT_IMPORT_EXPORT TreeNode : public std::enable_shared_from_this<TreeNode>
44 {
45 public:
46 // constants
48 static const QSize DefaultNodeVerticalSeparator;
49 static const QSize DefaultNodeSize;
50
51 /**
52 * Constructs a tree node as part of a Qt visualizable tree.
53 * @param scene the graphics scene used for rendering
54 * @param nodeSite size of the nodes boundingbox
55 */
56 TreeNode(QGraphicsScene* scene, QSize nodeSize = TreeNode::DefaultNodeSize);
57
58 /**
59 * Adds a child to the node in the tree structure
60 * @param child the child to add
61 */
62 void addChild(TreeNodePtr child);
63
64 /**
65 * Updates the layout of the tree. Only applicable for the root node. Is ignored
66 * for all other nodes.
67 * @param positionLeftTop left top position in the graphicsscene, where the tree is drawn.
68 */
69 void update(QPointF positionLeftTop = QPointF(0, 0));
70
71 /**
72 * Retrieve size of the node.
73 * @return node size
74 */
75 QSize
77 {
78 return size;
79 }
80
81 std::vector<TreeNodePtr> getChildren() const;
82
83 /**
84 * Retrieve boundingbox of the node. Only valid after update has been called.
85 * @return boundingbox of the node in the graphicsscene
86 */
87 QRectF
89 {
90 return boundingBox;
91 }
92
93 /**
94 * Retrieve size of the complete subtree where the current node is root. Only valid after
95 * update has been callse;
96 * @return size of the subtree represented by this node
97 */
98 QSize
100 {
101 return subTreeSize;
102 }
103
104 protected:
105 /**
106 * Sets size of node. Layout has to be updated afterwards.
107 * @param nodeSize size of the node
108 */
109 void
110 setSize(QSize nodeSize)
111 {
112 size = nodeSize;
113 }
114
115 /**
116 * Draws an edge to this node. Overwrite this in order provide your own visualization.
117 * @param line line from parent to this node
118 */
119 virtual void drawEdge(QLineF line);
120
121 /**
122 * Draws the node. Overwrite this in order provide your own visualization.
123 * @param boundingBox of the node to be drawn
124 */
125 virtual void drawNode(QRectF boundingBox);
126
127 private:
128 // tree structure
129 void setParent(TreeNodeWeakPtr parent);
130
131 // recursive layout update
132 void updateLayout(QPointF center);
133
134 // recursive size calculation
135 QSize calculateSubTreeSize();
136
137 // helpers methods
138 QRectF calculateChildsBoundingBox();
139 void drawEdges();
140
141 // visualization
142 QGraphicsEllipseItem* nodeItem;
143 QGraphicsLineItem* edgeItem;
144
145 // sizes
146 QSize size; // node size
147 QSize subTreeSize; // including all subnodes
148 QRectF boundingBox; // bounding box of subtree
149
150 // tree structure
151 TreeNodeWeakPtr parent;
152 std::vector<TreeNodePtr> childs;
153 };
154} // namespace armarx
#define ARMARXCOMPONENT_IMPORT_EXPORT
void setSize(QSize nodeSize)
Sets size of node.
Definition TreeNode.h:110
static const QSize DefaultNodeSize
Definition TreeNode.h:49
static const QSize DefaultNodeVerticalSeparator
Definition TreeNode.h:48
static const QSize DefaultNodeHorizontalSeparator
Definition TreeNode.h:47
void addChild(TreeNodePtr child)
Adds a child to the node in the tree structure.
Definition TreeNode.cpp:59
QSize getSize()
Retrieve size of the node.
Definition TreeNode.h:76
QRectF getBoundingBox()
Retrieve boundingbox of the node.
Definition TreeNode.h:88
TreeNode(QGraphicsScene *scene, QSize nodeSize=TreeNode::DefaultNodeSize)
Constructs a tree node as part of a Qt visualizable tree.
Definition TreeNode.cpp:36
void update(QPointF positionLeftTop=QPointF(0, 0))
Updates the layout of the tree.
Definition TreeNode.cpp:69
QSize getSubTreeSize()
Retrieve size of the complete subtree where the current node is root.
Definition TreeNode.h:99
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< TreeNode > TreeNodePtr
Definition TreeNode.h:40
std::weak_ptr< TreeNode > TreeNodeWeakPtr
Definition TreeNode.h:41