bpp-phyl  2.2.0
TreeDrawingListener.cpp
Go to the documentation of this file.
1 //
2 // File: TreeDrawingListener.cpp
3 // Created by: Julien Dutheil
4 // Created on: Tue May 18 10:33 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (2010)
9 
10 This software is a computer program whose purpose is to provide
11 graphic components to develop bioinformatics applications.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "TreeDrawingListener.h"
41 #include "AbstractTreeDrawing.h"
42 
43 //From the STL:
44 #include <string>
45 #include <exception>
46 #include <typeinfo>
47 
48 using namespace std;
49 
50 using namespace bpp;
51 
52 void NodesIdTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
53 {
54  GraphicDevice* gd = event.getGraphicDevice();
55  Cursor cursor = event.getCursor();
56  Font fontBck = gd->getCurrentFont();
57  if (settings_)
58  gd->setCurrentFont(settings_->fontNodesId);
59  string name = "#" + TextTools::toString(event.getNodeId());
60  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
61  gd->setCurrentFont(fontBck);
62 }
63 
64 void LeafNamesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
65 {
66  try
67  {
68  //Pointer-based event (efficient):
69  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
70  if (eventC.getINode()->isLeaf())
71  {
72  GraphicDevice* gd = event.getGraphicDevice();
73  Cursor cursor = event.getCursor();
74  Font fontBck = gd->getCurrentFont();
75  if (settings_)
76  gd->setCurrentFont(settings_->fontLeafNames);
77  string name = eventC.getINode()->getName();
78  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
79  gd->setCurrentFont(fontBck);
80  }
81  }
82  catch (bad_cast& e)
83  {
84  //Id-based event (less-efficient):
85  const TreeDrawing* td = event.getTreeDrawing();
86  if (td->getTree()->isLeaf(event.getNodeId()))
87  {
88  GraphicDevice* gd = event.getGraphicDevice();
89  Cursor cursor = event.getCursor();
90  Font fontBck = gd->getCurrentFont();
91  if (settings_)
92  gd->setCurrentFont(settings_->fontLeafNames);
93  string name = td->getTree()->getNodeName(event.getNodeId());
94  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
95  gd->setCurrentFont(fontBck);
96  }
97  }
98 }
99 
100 void BranchLengthsTreeDrawingListener::afterDrawBranch(const DrawBranchEvent& event)
101 {
102  try
103  {
104  //Pointer-based event (efficient):
105  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
106  if (eventC.getINode()->hasDistanceToFather())
107  {
108  GraphicDevice* gd = event.getGraphicDevice();
109  Cursor cursor = event.getBranchCursor(0.5);
110  Font fontBck = gd->getCurrentFont();
111  if (settings_)
112  gd->setCurrentFont(settings_->fontBranchLengths);
113  gd->drawText(cursor.getX(), cursor.getY(),
114  TextTools::toString(eventC.getINode()->getDistanceToFather()),
115  GraphicDevice::TEXT_HORIZONTAL_CENTER, GraphicDevice::TEXT_VERTICAL_BOTTOM, cursor.getAngle());
116  gd->setCurrentFont(fontBck);
117  }
118  }
119  catch (std::bad_cast& e)
120  {
121  //Id-based event (less-efficient):
122  const TreeDrawing* td = event.getTreeDrawing();
123  if (td->getTree()->hasDistanceToFather(event.getNodeId()))
124  {
125  GraphicDevice* gd = event.getGraphicDevice();
126  Cursor cursor = event.getBranchCursor(0.5);
127  Font fontBck = gd->getCurrentFont();
128  if (settings_)
129  gd->setCurrentFont(settings_->fontLeafNames);
130  gd->drawText(cursor.getX(), cursor.getY(),
131  TextTools::toString(td->getTree()->getDistanceToFather(event.getNodeId())),
132  GraphicDevice::TEXT_HORIZONTAL_CENTER, GraphicDevice::TEXT_VERTICAL_BOTTOM, cursor.getAngle());
133  gd->setCurrentFont(fontBck);
134  }
135  }
136 }
137 
138 void BootstrapValuesTreeDrawingListener::afterDrawBranch(const DrawBranchEvent& event)
139 {
140  try
141  {
142  //Pointer-based event (efficient):
143  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
144  if (eventC.getINode()->hasBranchProperty(TreeTools::BOOTSTRAP))
145  {
146  const Clonable* b = eventC.getINode()->getBranchProperty(TreeTools::BOOTSTRAP);
147  GraphicDevice* gd = event.getGraphicDevice();
148  Cursor cursor = event.getCursor();
149  Font fontBck = gd->getCurrentFont();
150  if (settings_)
151  gd->setCurrentFont(settings_->fontBranchLengths);
152  gd->drawText(cursor.getX(), cursor.getY(),
153  TextTools::toString(dynamic_cast<const Number<double> *>(b)->getValue()),
154  cursor.getHPos(), GraphicDevice::TEXT_VERTICAL_CENTER, cursor.getAngle());
155  gd->setCurrentFont(fontBck);
156  }
157  }
158  catch (std::bad_cast& e)
159  {
160  //Id-based event (less-efficient):
161  const TreeDrawing* td = event.getTreeDrawing();
162  if (td->getTree()->hasBranchProperty(event.getNodeId(), TreeTools::BOOTSTRAP))
163  {
164  const Clonable* b = td->getTree()->getBranchProperty(event.getNodeId(), TreeTools::BOOTSTRAP);
165  GraphicDevice* gd = event.getGraphicDevice();
166  Cursor cursor = event.getCursor();
167  Font fontBck = gd->getCurrentFont();
168  if (settings_)
169  gd->setCurrentFont(settings_->fontLeafNames);
170  gd->drawText(cursor.getX(), cursor.getY(),
171  TextTools::toString(dynamic_cast<const Number<double> *>(b)->getValue()),
172  cursor.getHPos(), GraphicDevice::TEXT_VERTICAL_CENTER, cursor.getAngle());
173  gd->setCurrentFont(fontBck);
174  }
175  }
176 }
177 
178 void LabelInnerNodesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
179 {
180  try
181  {
182  //Pointer-based event (efficient):
183  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
184  if (!eventC.getINode()->getInfos().isCollapsed())
185  {
186  GraphicDevice* gd = event.getGraphicDevice();
187  Cursor cursor = event.getCursor();
188  if (eventC.getINode()->hasName())
189  {
190  string name = eventC.getINode()->getName();
191  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
192  }
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  if (td->getTree()->hasNodeName(event.getNodeId()))
204  {
205  string name = td->getTree()->getNodeName(event.getNodeId());
206  gd->drawText(cursor.getX(), cursor.getY(), name, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
207  }
208  }
209  }
210 }
211 
212 void LabelCollapsedNodesTreeDrawingListener::afterDrawNode(const DrawNodeEvent& event)
213 {
214  try
215  {
216  //Pointer-based event (efficient):
217  const DrawINodeEvent& eventC = dynamic_cast<const DrawINodeEvent&>(event);
218  if (eventC.getINode()->getInfos().isCollapsed())
219  {
220  GraphicDevice* gd = event.getGraphicDevice();
221  Cursor cursor = event.getCursor();
222  size_t size = TreeTemplateTools::getNumberOfLeaves(*eventC.getINode());
223  string text = "";
224  if (eventC.getINode()->hasName())
225  text += eventC.getINode()->getName() + " ";
226  text += "(" + TextTools::toString(size) + " leaves)";
227  gd->drawText(cursor.getX(), cursor.getY(), text, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
228  }
229  }
230  catch(std::bad_cast& e)
231  {
232  //Id-based event (less-efficient):
233  const TreeDrawing* td = event.getTreeDrawing();
234  if (td->isNodeCollapsed(event.getNodeId()))
235  {
236  GraphicDevice* gd = event.getGraphicDevice();
237  Cursor cursor = event.getCursor();
238  size_t size = TreeTools::getNumberOfLeaves(*td->getTree(), event.getNodeId());
239  string text = "";
240  if (td->getTree()->hasNodeName(event.getNodeId()))
241  text += td->getTree()->getNodeName(event.getNodeId()) + " ";
242  text += "(" + TextTools::toString(size) + " leaves)";
243  gd->drawText(cursor.getX(), cursor.getY(), text, cursor.getHPos(), cursor.getVPos(), cursor.getAngle());
244  }
245  }
246 }
247 
Basal interface for tree drawing classes.
Definition: TreeDrawing.h:264
double getY() const
Definition: TreeDrawing.h:102
virtual int getNodeId() const
Definition: TreeDrawing.h:196
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
Definition: Node.h:236
virtual bool hasName() const
Tell is this node has a name.
Definition: Node.h:267
virtual Clonable * getBranchProperty(const std::string &name)
Definition: Node.h:617
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
STL namespace.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
Definition: Node.h:321
double getAngle() const
Definition: TreeDrawing.h:103
virtual bool isLeaf() const
Definition: Node.h:692
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
Definition: Node.h:283
virtual bool isLeaf(int nodeId) const =0
virtual bool isNodeCollapsed(int nodeId) const =0
virtual bool hasBranchProperty(int nodeId, const std::string &name) const =0
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:124
short getHPos() const
Definition: TreeDrawing.h:104
virtual double getDistanceToFather(int nodeId) const =0
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:165
virtual bool hasNodeName(int nodeId) const =0
virtual GraphicDevice * getGraphicDevice() const
Definition: TreeDrawing.h:154
virtual bool hasDistanceToFather(int nodeId) const =0
double getX() const
Definition: TreeDrawing.h:101
virtual const Tree * getTree() const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual int getNodeId() const
Definition: TreeDrawing.h:155
short getVPos() const
Definition: TreeDrawing.h:105
virtual const NodeInfos & getInfos() const
Definition: NodeTemplate.h:179
virtual bool hasBranchProperty(const std::string &name) const
Definition: Node.h:678
const INode * getINode() const
Data structure describing a plotting direction.
Definition: TreeDrawing.h:87
virtual std::string getNodeName(int nodeId) const =0