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 
26 
27 #include <memory>
28 
29 // qt
30 #include <QPointF>
31 #include <QRectF>
32 #include <QSize>
33 #include <QGraphicsScene>
34 #include <QGraphicsEllipseItem>
35 
36 namespace 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
47  static const QSize DefaultNodeHorizontalSeparator;
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  /**
73  * Retrieve size of the node.
74  * @return node size
75  */
76  QSize getSize()
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 getBoundingBox()
88  {
89  return boundingBox;
90  }
91 
92  /**
93  * Retrieve size of the complete subtree where the current node is root. Only valid after
94  * update has been callse;
95  * @return size of the subtree represented by this node
96  */
98  {
99  return subTreeSize;
100  }
101 
102  protected:
103  /**
104  * Sets size of node. Layout has to be updated afterwards.
105  * @param nodeSize size of the node
106  */
107  void setSize(QSize nodeSize)
108  {
109  size = nodeSize;
110  }
111 
112  /**
113  * Draws an edge to this node. Overwrite this in order provide your own visualization.
114  * @param line line from parent to this node
115  */
116  virtual void drawEdge(QLineF line);
117 
118  /**
119  * Draws the node. Overwrite this in order provide your own visualization.
120  * @param boundingBox of the node to be drawn
121  */
122  virtual void drawNode(QRectF boundingBox);
123 
124  private:
125  // tree structure
126  void setParent(TreeNodeWeakPtr parent);
127 
128  // recursive layout update
129  void updateLayout(QPointF center);
130 
131  // recursive size calculation
132  QSize calculateSubTreeSize();
133 
134  // helpers methods
135  QRectF calculateChildsBoundingBox();
136  void drawEdges();
137 
138  // visualization
139  QGraphicsEllipseItem* nodeItem;
140  QGraphicsLineItem* edgeItem;
141 
142  // sizes
143  QSize size; // node size
144  QSize subTreeSize; // including all subnodes
145  QRectF boundingBox; // bounding box of subtree
146 
147  // tree structure
148  TreeNodeWeakPtr parent;
149  std::vector<TreeNodePtr> childs;
150  };
151 }
152 
armarx::TreeNode::DefaultNodeVerticalSeparator
static const QSize DefaultNodeVerticalSeparator
Definition: TreeNode.h:48
armarx::TreeNodePtr
std::shared_ptr< TreeNode > TreeNodePtr
Definition: TreeNode.h:40
armarx::TreeNode::DefaultNodeHorizontalSeparator
static const QSize DefaultNodeHorizontalSeparator
Definition: TreeNode.h:47
armarx::TreeNode::DefaultNodeSize
static const QSize DefaultNodeSize
Definition: TreeNode.h:49
armarx::TreeNode::getSubTreeSize
QSize getSubTreeSize()
Retrieve size of the complete subtree where the current node is root.
Definition: TreeNode.h:97
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
armarx::TreeNodeWeakPtr
std::weak_ptr< TreeNode > TreeNodeWeakPtr
Definition: TreeNode.h:41
armarx::TreeNode::getBoundingBox
QRectF getBoundingBox()
Retrieve boundingbox of the node.
Definition: TreeNode.h:87
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::TreeNode
class ARMARXCOMPONENT_IMPORT_EXPORT TreeNode
Definition: TreeNode.h:38
armarx::TreeNode::setSize
void setSize(QSize nodeSize)
Sets size of node.
Definition: TreeNode.h:107
armarx::TreeNode::getSize
QSize getSize()
Retrieve size of the node.
Definition: TreeNode.h:76
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::TreeNode
Definition: TreeNode.h:43
ImportExportComponent.h