bpp-phyl  2.2.0
AbstractTreeDrawing.h
Go to the documentation of this file.
1 //
2 // File: AbstractTreeDrawing.h
3 // Created by: Julien Dutheil
4 // Created on: Sun Oct 8 11:57 2006
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (November 16, 2004)
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 #ifndef _ABSTRACTTREEDRAWING_H_
41 #define _ABSTRACTTREEDRAWING_H_
42 
43 #include "../TreeTemplate.h"
44 #include "../NodeTemplate.h"
45 #include "TreeDrawing.h"
46 #include "TreeDrawingListener.h"
47 
48 // From ths STL:
49 #include <vector>
50 #include <string>
51 #include <memory>
52 
53 namespace bpp
54 {
55 
57 {
58  private:
59  Point2D<double> pos_;
60  bool collapsed_;
61 
62  public:
64  virtual ~TreeDrawingNodeInfo() {}
65 
66  public:
67  const Point2D<double>& getPosition() const { return pos_; }
68  Point2D<double>& getPosition() { return pos_; }
69  void setPosition(const Point2D<double>& position) { pos_ = position; }
70  double getX() const { return pos_.getX(); }
71  double getY() const { return pos_.getY(); }
72  void setX(double x) { pos_.setX(x); }
73  void setY(double y) { pos_.setY(y); }
74  void collapse(bool yn) { collapsed_ = yn; }
75  bool isCollapsed() const { return collapsed_; }
76 };
77 
79 
80 
81 
86  public DrawNodeEvent
87 {
88  private:
89  const INode* node_;
90 
91  public:
92  DrawINodeEvent(const TreeDrawing* source, GraphicDevice* gd, const INode* node, const Cursor& cursor) :
93  DrawNodeEvent(source, gd, node->getId(), cursor),
94  node_(node)
95  {}
96 
98  DrawNodeEvent(dne), node_(dne.node_)
99  {}
100 
102  {
104  node_ = dne.node_;
105  return *this;
106  }
107 
108  public:
109  const INode* getINode() const { return node_; }
110 
111 };
112 
113 
114 
119  public DrawBranchEvent
120 {
121  private:
122  const INode* node_;
123 
124  public:
125  DrawIBranchEvent(const TreeDrawing* source, GraphicDevice* gd, const INode* node, const Cursor& cursor) :
126  DrawBranchEvent(source, gd, node->getId(), cursor),
127  node_(node)
128  {}
129 
131  DrawBranchEvent(dne), node_(dne.node_)
132  {}
133 
135  {
137  node_ = dne.node_;
138  return *this;
139  }
140 
141  public:
142  const INode* getINode() const { return node_; }
143 
144 };
145 
146 
147 
156  public virtual TreeDrawing
157 {
158  private:
159  std::auto_ptr<TreeTemplate<INode> > tree_;
160  double xUnit_;
161  double yUnit_;
163  std::vector<TreeDrawingListener*> listeners_;
164 
165  public:
167 
169  tree_(atd.tree_.get() ? dynamic_cast<TreeTemplate<INode> *>(atd.tree_->clone()) : 0),
170  xUnit_(atd.xUnit_),
171  yUnit_(atd.yUnit_),
172  settings_(atd.settings_),
173  listeners_(atd.listeners_.size())
174  {
175  for (unsigned int i = 0; i < listeners_.size(); ++i)
176  {
177  if (atd.listeners_[i]->isAutonomous())
178  listeners_[i] = atd.listeners_[i];
179  else
180  listeners_[i] = dynamic_cast<TreeDrawingListener*>(atd.listeners_[i]->clone());
181  }
182  }
183 
185  {
186  if (atd.tree_.get())
187  tree_.reset(dynamic_cast<TreeTemplate<INode> *>(atd.tree_->clone()));
188  else tree_.reset();
189  xUnit_ = atd.xUnit_;
190  yUnit_ = atd.yUnit_;
191  settings_ = atd.settings_;
192  listeners_.resize(atd.listeners_.size());
193  for (unsigned int i = 0; i < listeners_.size(); ++i)
194  {
195  if (atd.listeners_[i]->isAutonomous())
196  listeners_[i] = atd.listeners_[i];
197  else
198  listeners_[i] = dynamic_cast<TreeDrawingListener*>(atd.listeners_[i]->clone());
199  }
200  return *this;
201  }
202 
204  {
205  for (unsigned int i = 0; i < listeners_.size(); i++)
206  if (!listeners_[i]->isAutonomous())
207  delete listeners_[i];
208  }
209 
210  public:
211 
212  bool hasTree() const { return tree_.get() != 0; }
213 
214 #ifdef NO_VIRTUAL_COV
215  Tree*
216 #else
217  const TreeTemplate<INode>*
218 #endif
219  getTree() const { return tree_.get(); }
220 
221  void setTree(const Tree* tree)
222  {
223  if (tree_.get())
224  tree_.reset();
225  if (!tree) tree_.reset();
226  else
227  {
228  tree_.reset(new TreeTemplate<INode>(*tree)); //We copy the tree
229  }
230  treeHasChanged();
231  }
232 
233  Point2D<double> getNodePosition(int nodeId) const throw (NodeNotFoundException);
234 
235  int getNodeAt(const Point2D<double>& position) const throw (NodeNotFoundException);
236 
246  bool belongsTo(const Point2D<double>& p1, const Point2D<double>& p2) const;
247 
260  virtual void drawAtNode(GraphicDevice& gDevice, const INode& node, const std::string& text,
261  double xOffset = 0, double yOffset = 0,
262  short hpos = GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos = GraphicDevice::TEXT_VERTICAL_CENTER, double angle = 0) const;
263 
276  virtual void drawAtBranch(GraphicDevice& gDevice, const INode& node, const std::string& text, double xOffset = 0, double yOffset = 0, short hpos = GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos = GraphicDevice::TEXT_VERTICAL_CENTER, double angle = 0) const;
277 
278  void setDisplaySettings(const TreeDrawingSettings* tds) throw (NullPointerException) {
279  if (!tds)
280  throw NullPointerException("AbstractTreeDrawing::setDisplaySettings. Null pointer provided.");
281  settings_ = tds;
282  }
284 
285  double getXUnit() const { return xUnit_; }
286 
287  double getYUnit() const { return yUnit_; }
288 
289  void setXUnit(double xu) { xUnit_ = xu; }
290 
291  void setYUnit(double yu) { yUnit_ = yu; }
292 
293  void collapseNode(int nodeId, bool yn) throw (NodeNotFoundException, Exception)
294  {
295  if(! tree_.get()) throw Exception("AbstractTreeDrawing::collapseNode. No tree is associated to the drawing.");
296  tree_->getNode(nodeId)->getInfos().collapse(yn);
297  }
298 
299  bool isNodeCollapsed(int nodeId) const throw (NodeNotFoundException, Exception)
300  {
301  if(! tree_.get()) throw Exception("AbstractTreeDrawing::isNodeCollapsed. No tree is associated to the drawing.");
302  return tree_->getNode(nodeId)->getInfos().isCollapsed();
303  }
304 
305  void addTreeDrawingListener(TreeDrawingListener* listener) throw (Exception)
306  {
307  if (find(listeners_.begin(), listeners_.end(), listener) != listeners_.end())
308  throw Exception("AbstractTreeDrawing::addTreeDrawingListener. Listener is already associated to this drawing.");
309  listeners_.push_back(listener);
310  }
311 
312  void removeTreeDrawingListener(TreeDrawingListener* listener) throw (Exception)
313  {
314  std::vector<TreeDrawingListener*>::iterator it = std::find(listeners_.begin(), listeners_.end(), listener);
315  if (it == listeners_.end())
316  throw Exception("AbstractTreeDrawing::addTreeDrawingListener. Listener is not associated to this drawing, and therefore can't be removed.");
317  listeners_.erase(it);
318  if (!listener->isAutonomous())
319  delete listener;
320  }
321 
325  virtual void treeHasChanged() = 0;
326 
327  protected:
328  TreeTemplate<INode>* getTree_() { return tree_.get(); }
329  const TreeTemplate<INode>* getTree_() const { return tree_.get(); }
330 
331  void fireBeforeTreeEvent_(const DrawTreeEvent& event) const
332  {
333  for (unsigned int i = 0; i < listeners_.size(); i++)
334  if (listeners_[i]->isEnabled())
335  listeners_[i]->beforeDrawTree(event);
336  }
337 
338  void fireAfterTreeEvent_(const DrawTreeEvent& event) const
339  {
340  for (unsigned int i = 0; i < listeners_.size(); i++)
341  if (listeners_[i]->isEnabled())
342  listeners_[i]->afterDrawTree(event);
343  }
344 
345  void fireBeforeNodeEvent_(const DrawINodeEvent& event) const
346  {
347  for (unsigned int i = 0; i < listeners_.size(); i++)
348  if (listeners_[i]->isEnabled())
349  listeners_[i]->beforeDrawNode(event);
350  }
351 
352  void fireAfterNodeEvent_(const DrawINodeEvent& event) const
353  {
354  for (unsigned int i = 0; i < listeners_.size(); i++)
355  if (listeners_[i]->isEnabled())
356  listeners_[i]->afterDrawNode(event);
357  }
358 
359  void fireBeforeBranchEvent_(const DrawIBranchEvent& event) const
360  {
361  for (unsigned int i = 0; i < listeners_.size(); i++)
362  if (listeners_[i]->isEnabled())
363  listeners_[i]->beforeDrawBranch(event);
364  }
365 
366  void fireAfterBranchEvent_(const DrawIBranchEvent& event) const
367  {
368  for (unsigned int i = 0; i < listeners_.size(); i++)
369  if (listeners_[i]->isEnabled())
370  listeners_[i]->afterDrawBranch(event);
371  }
372  public:
374 };
375 
376 } //end of namespace bpp.
377 
378 #endif //_ABSTRACTTREEDRAWING_H_
379 
const Point2D< double > & getPosition() const
Basal interface for tree drawing classes.
Definition: TreeDrawing.h:264
const TreeTemplate< INode > * getTree() const
DrawINodeEvent(const DrawINodeEvent &dne)
const TreeDrawingSettings & getDisplaySettings() const
void setTree(const Tree *tree)
void fireBeforeTreeEvent_(const DrawTreeEvent &event) const
void setDisplaySettings(const TreeDrawingSettings *tds)
Global drawing settings.
DrawBranchEvent & operator=(const DrawBranchEvent &dne)
Definition: TreeDrawing.h:182
void removeTreeDrawingListener(TreeDrawingListener *listener)
Remove a drawing listener from this instance.
DrawIBranchEvent(const DrawIBranchEvent &dne)
AbstractTreeDrawing & operator=(const AbstractTreeDrawing &atd)
Partial implementation of the TreeDrawing interface.
DrawINodeEvent & operator=(const DrawINodeEvent &dne)
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
void setXUnit(double xu)
Set the &#39;horizontal&#39; expansion unit.
TreeDrawing * clone() const =0
void setPosition(const Point2D< double > &position)
void fireAfterNodeEvent_(const DrawINodeEvent &event) const
Point2D< double > getNodePosition(int nodeId) const
Get the position of a node.
void fireAfterTreeEvent_(const DrawTreeEvent &event) const
The phylogenetic tree class.
DrawIBranchEvent(const TreeDrawing *source, GraphicDevice *gd, const INode *node, const Cursor &cursor)
Interface for phylogenetic tree objects.
Definition: Tree.h:148
virtual void treeHasChanged()=0
Method to implement to deal with redrawing when the underlying tree has been modified.
NodeTemplate< TreeDrawingNodeInfo > INode
virtual void drawAtNode(GraphicDevice &gDevice, const INode &node, const std::string &text, double xOffset=0, double yOffset=0, short hpos=GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos=GraphicDevice::TEXT_VERTICAL_CENTER, double angle=0) const
Draw some text at a particular node position.
std::auto_ptr< TreeTemplate< INode > > tree_
AbstractTreeDrawing(const AbstractTreeDrawing &atd)
const TreeTemplate< INode > * getTree_() const
void fireBeforeBranchEvent_(const DrawIBranchEvent &event) const
void fireAfterBranchEvent_(const DrawIBranchEvent &event) const
const TreeDrawingSettings * settings_
void fireBeforeNodeEvent_(const DrawINodeEvent &event) const
A set of options to tune the display of a TreeDrawing object.
Definition: TreeDrawing.h:61
void setYUnit(double yu)
Set the &#39;vertical&#39; expansion unit.
Exception thrown when something is wrong with a particular node.
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:124
DrawIBranchEvent & operator=(const DrawIBranchEvent &dne)
std::vector< TreeDrawingListener * > listeners_
Event class that uses INode object (more efficient than relying on nodes id, but less generic)...
Interface allowing to capture drawing events.
DrawNodeEvent & operator=(const DrawNodeEvent &dne)
Definition: TreeDrawing.h:141
virtual void drawAtBranch(GraphicDevice &gDevice, const INode &node, const std::string &text, double xOffset=0, double yOffset=0, short hpos=GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos=GraphicDevice::TEXT_VERTICAL_CENTER, double angle=0) const
Draw some text at a particular branch position.
Point2D< double > & getPosition()
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:165
bool belongsTo(const Point2D< double > &p1, const Point2D< double > &p2) const
Utilitary function, telling if a point belongs to a specified area.
DrawINodeEvent(const TreeDrawing *source, GraphicDevice *gd, const INode *node, const Cursor &cursor)
const INode * getINode() const
bool isNodeCollapsed(int nodeId) const
int getNodeAt(const Point2D< double > &position) const
Get the node corresponding to a position on the device.
TreeTemplate< INode > * getTree_()
void addTreeDrawingListener(TreeDrawingListener *listener)
Add a drawing listener to this instance.
static const TreeDrawingSettings DEFAULT_SETTINGS
The NodeTemplate class.
Definition: NodeTemplate.h:73
const INode * getINode() const
void collapseNode(int nodeId, bool yn)
Properties to draw.
Data structure describing a plotting direction.
Definition: TreeDrawing.h:87
Event class used by TreeDrawing classes.
Definition: TreeDrawing.h:211