bpp-phyl3  3.0.0
TreeDrawingListener.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "AbstractTreeDrawing.h"
6 #include "TreeDrawingListener.h"
7 
8 // From the STL:
9 #include <string>
10 #include <exception>
11 #include <typeinfo>
12 
13 using namespace std;
14 
15 using namespace bpp;
16 
17 void NodesIdTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
18 {
19  GraphicDevice* gd = event.getGraphicDevice();
20  Cursor cursor = event.getCursor();
21  Font fontBck = gd->getCurrentFont();
22  if (settings_)
23  gd->setCurrentFont(settings_->fontNodesId);
24  string name = "#" + TextTools::toString(event.getNodeId());
25  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
26  gd->setCurrentFont(fontBck);
27 }
28 
29 void LeafNamesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
30 {
31  try
32  {
33  // Pointer-based event (efficient):
34  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
35  if (eventC.getINode()->isLeaf())
36  {
37  GraphicDevice* gd = event.getGraphicDevice();
38  Cursor cursor = event.getCursor();
39  Font fontBck = gd->getCurrentFont();
40  if (settings_)
41  gd->setCurrentFont(settings_->fontLeafNames);
42  string name = eventC.getINode()->getName();
43  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
44  gd->setCurrentFont(fontBck);
45  }
46  }
47  catch (bad_cast& e)
48  {
49  // Id-based event (less-efficient):
50  const TreeDrawing* td = event.getTreeDrawing();
51  if (td->getTree()->isLeaf(event.getNodeId()))
52  {
53  GraphicDevice* gd = event.getGraphicDevice();
54  Cursor cursor = event.getCursor();
55  Font fontBck = gd->getCurrentFont();
56  if (settings_)
57  gd->setCurrentFont(settings_->fontLeafNames);
58  string name = td->getTree()->getNodeName(event.getNodeId());
59  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
60  gd->setCurrentFont(fontBck);
61  }
62  }
63 }
64 
65 void BranchLengthsTreeDrawingListener::afterDrawBranch(const DrawBranchEvent& event)
66 {
67  try
68  {
69  // Pointer-based event (efficient):
70  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
71  if (eventC.getINode()->hasDistanceToFather())
72  {
73  GraphicDevice* gd = event.getGraphicDevice();
74  Cursor cursor = event.getBranchCursor(0.5);
75  Font fontBck = gd->getCurrentFont();
76  if (settings_)
77  gd->setCurrentFont(settings_->fontBranchLengths);
78  gd->drawText(cursor.getX(), cursor.getY(),
79  TextTools::toString(eventC.getINode()->getDistanceToFather()),
80  GraphicDevice::TEXT_HORIZONTAL_CENTER, GraphicDevice::TEXT_VERTICAL_BOTTOM, cursor.getAngle());
81  gd->setCurrentFont(fontBck);
82  }
83  }
84  catch (std::bad_cast& e)
85  {
86  // Id-based event (less-efficient):
87  const TreeDrawing* td = event.getTreeDrawing();
88  if (td->getTree()->hasDistanceToFather(event.getNodeId()))
89  {
90  GraphicDevice* gd = event.getGraphicDevice();
91  Cursor cursor = event.getBranchCursor(0.5);
92  Font fontBck = gd->getCurrentFont();
93  if (settings_)
94  gd->setCurrentFont(settings_->fontLeafNames);
95  gd->drawText(cursor.getX(), cursor.getY(),
96  TextTools::toString(td->getTree()->getDistanceToFather(event.getNodeId())),
97  GraphicDevice::TEXT_HORIZONTAL_CENTER, GraphicDevice::TEXT_VERTICAL_BOTTOM, cursor.getAngle());
98  gd->setCurrentFont(fontBck);
99  }
100  }
101 }
102 
103 void BootstrapValuesTreeDrawingListener::afterDrawBranch(const DrawBranchEvent& event)
104 {
105  try
106  {
107  // Pointer-based event (efficient):
108  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
109  if (eventC.getINode()->hasBranchProperty(TreeTools::BOOTSTRAP))
110  {
111  const Clonable* b = eventC.getINode()->getBranchProperty(TreeTools::BOOTSTRAP);
112  GraphicDevice* gd = event.getGraphicDevice();
113  Cursor cursor = event.getCursor();
114  Font fontBck = gd->getCurrentFont();
115  if (settings_)
116  gd->setCurrentFont(settings_->fontBranchLengths);
117  gd->drawText(cursor.getX(), cursor.getY(),
118  TextTools::toString(dynamic_cast<const Number<double>*>(b)->getValue()),
119  cursor.getHPos(), GraphicDevice::TEXT_VERTICAL_CENTER, cursor.getAngle());
120  gd->setCurrentFont(fontBck);
121  }
122  }
123  catch (std::bad_cast& e)
124  {
125  // Id-based event (less-efficient):
126  const TreeDrawing* td = event.getTreeDrawing();
127  if (td->getTree()->hasBranchProperty(event.getNodeId(), TreeTools::BOOTSTRAP))
128  {
129  const Clonable* b = td->getTree()->getBranchProperty(event.getNodeId(), TreeTools::BOOTSTRAP);
130  GraphicDevice* gd = event.getGraphicDevice();
131  Cursor cursor = event.getCursor();
132  Font fontBck = gd->getCurrentFont();
133  if (settings_)
134  gd->setCurrentFont(settings_->fontLeafNames);
135  gd->drawText(cursor.getX(), cursor.getY(),
136  TextTools::toString(dynamic_cast<const Number<double>*>(b)->getValue()),
137  cursor.getHPos(), GraphicDevice::TEXT_VERTICAL_CENTER, cursor.getAngle());
138  gd->setCurrentFont(fontBck);
139  }
140  }
141 }
142 
143 void LabelInnerNodesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
144 {
145  try
146  {
147  // Pointer-based event (efficient):
148  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
149  if (!eventC.getINode()->getInfos().isCollapsed())
150  {
151  GraphicDevice* gd = event.getGraphicDevice();
152  Cursor cursor = event.getCursor();
153  if (eventC.getINode()->hasName())
154  {
155  string name = eventC.getINode()->getName();
156  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
157  }
158  }
159  }
160  catch (std::bad_cast& e)
161  {
162  // Id-based event (less-efficient):
163  const TreeDrawing* td = event.getTreeDrawing();
164  if (!td->isNodeCollapsed(event.getNodeId()))
165  {
166  GraphicDevice* gd = event.getGraphicDevice();
167  Cursor cursor = event.getCursor();
168  if (td->getTree()->hasNodeName(event.getNodeId()))
169  {
170  string name = td->getTree()->getNodeName(event.getNodeId());
171  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
172  }
173  }
174  }
175 }
176 
177 void LabelCollapsedNodesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
178 {
179  try
180  {
181  // Pointer-based event (efficient):
182  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
183  if (eventC.getINode()->getInfos().isCollapsed())
184  {
185  GraphicDevice* gd = event.getGraphicDevice();
186  Cursor cursor = event.getCursor();
187  size_t size = TreeTemplateTools::getNumberOfLeaves(*eventC.getINode());
188  string text = "";
189  if (eventC.getINode()->hasName())
190  text += eventC.getINode()->getName() + " ";
191  text += "(" + TextTools::toString(size) + " leaves)";
192  gd->drawText(cursor.getX(), cursor.getY(), text, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
193  }
194  }
195  catch (std::bad_cast& e)
196  {
197  // Id-based event (less-efficient):
198  const TreeDrawing* td = event.getTreeDrawing();
199  if (td->isNodeCollapsed(event.getNodeId()))
200  {
201  GraphicDevice* gd = event.getGraphicDevice();
202  Cursor cursor = event.getCursor();
203  size_t size = TreeTools::getNumberOfLeaves(*td->getTree(), event.getNodeId());
204  string text = "";
205  if (td->getTree()->hasNodeName(event.getNodeId()))
206  text += td->getTree()->getNodeName(event.getNodeId()) + " ";
207  text += "(" + TextTools::toString(size) + " leaves)";
208  gd->drawText(cursor.getX(), cursor.getY(), text, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
209  }
210  }
211 }
Data structure describing a plotting direction.
Definition: TreeDrawing.h:53
double getX() const
Definition: TreeDrawing.h:66
short getVPos() const
Definition: TreeDrawing.h:70
short getHPos() const
Definition: TreeDrawing.h:69
double getAngle() const
Definition: TreeDrawing.h:68
double getY() const
Definition: TreeDrawing.h:67
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:127
virtual int getNodeId() const
Definition: TreeDrawing.h:157
Event class that uses INode object (more efficient than relying on nodes id, but less generic).
const INode * getINode() const
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:88
virtual int getNodeId() const
Definition: TreeDrawing.h:118
virtual void setCurrentFont(const Font &font)=0
virtual void drawText(double x, double y, const std::string &text, short hpos=TEXT_HORIZONTAL_LEFT, short vpos=TEXT_VERTICAL_BOTTOM, double angle=0)=0
virtual const Font & getCurrentFont() const=0
virtual const NodeInfos & getInfos() const
Definition: NodeTemplate.h:144
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: Node.h:203
virtual bool hasBranchProperty(const std::string &name) const
Definition: Node.h:653
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:288
virtual bool isLeaf() const
Definition: Node.h:667
virtual Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:592
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:234
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:250
Basal interface for tree drawing classes.
Definition: TreeDrawing.h:223
virtual bool isNodeCollapsed(int nodeId) const =0
virtual const Tree * getTree() const =0
virtual std::string getNodeName(int nodeId) const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual bool isLeaf(int nodeId) const =0
virtual bool hasDistanceToFather(int nodeId) const =0
virtual bool hasNodeName(int nodeId) const =0
virtual double getDistanceToFather(int nodeId) const =0
virtual bool hasBranchProperty(int nodeId, const std::string &name) const =0
Defines the basic types of data flow nodes.