~ubuntu-branches/ubuntu/saucy/libbpp-phyl/saucy-proposed

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Graphics/TreeDrawing.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: TreeDrawing.h
 
3
// Created by: Julien Dutheil
 
4
// Created on: Sun Oct 8 11:57 2006
 
5
//
 
6
 
 
7
/*
 
8
Copyright or Ā© or Copr. Bio++ Development Team, (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 _TREEDRAWING_H_
 
41
#define _TREEDRAWING_H_
 
42
 
 
43
#include <Bpp/Clonable.h>
 
44
#include <Bpp/Graphics/GraphicDevice.h>
 
45
#include <Bpp/Graphics/Point2D.h>
 
46
#include <Bpp/Graphics/Font/Font.h>
 
47
 
 
48
// From PhylLib:
 
49
#include "../Tree.h"
 
50
 
 
51
namespace bpp
 
52
{
 
53
 
 
54
//Forward declarations:
 
55
class TreeDrawing;
 
56
class TreeDrawingListener;
 
57
 
 
58
/**
 
59
 * @brief A set of options to tune the display of a TreeDrawing object.
 
60
 */
 
61
class TreeDrawingSettings
 
62
{
 
63
  public:
 
64
    Font fontLeafNames;
 
65
    Font fontBranchLengths;
 
66
    Font fontBootstrapValues;
 
67
    Font fontNodesId;
 
68
    unsigned int pointSize;
 
69
    double pointArea; //this specifies the radius of the point area
 
70
    //More options will be added in the future...
 
71
    
 
72
  public:
 
73
    TreeDrawingSettings() :
 
74
      fontLeafNames("Courier", Font::STYLE_NORMAL, Font::WEIGHT_NORMAL, 12),
 
75
      fontBranchLengths("Courier", Font::STYLE_ITALIC, Font::WEIGHT_NORMAL, 10),
 
76
      fontBootstrapValues("Courier", Font::STYLE_NORMAL, Font::WEIGHT_NORMAL, 10),
 
77
      fontNodesId("Courier", Font::STYLE_NORMAL, Font::WEIGHT_BOLD, 12),
 
78
      pointSize(1),
 
79
      pointArea(5)
 
80
  {}
 
81
};
 
82
 
 
83
 
 
84
/**
 
85
 * @brief Data structure describing a plotting direction.
 
86
 */
 
87
class Cursor
 
88
{
 
89
private:
 
90
  double x_;
 
91
  double y_;
 
92
  double angle_;
 
93
  short hpos_;
 
94
  short vpos_;
 
95
 
 
96
public:
 
97
  Cursor(double x, double y, double angle = 0, short hpos = GraphicDevice::TEXT_HORIZONTAL_CENTER, short vpos = GraphicDevice::TEXT_VERTICAL_CENTER) :
 
98
    x_(x), y_(y), angle_(angle), hpos_(hpos), vpos_(vpos) {}
 
99
 
 
100
public:
 
101
  double getX() const { return x_; }
 
102
  double getY() const { return y_; }
 
103
  double getAngle() const { return angle_; }
 
104
  short getHPos() const { return hpos_; }
 
105
  short getVPos() const { return vpos_; }
 
106
  double addX(double increment) { return x_ += increment; }
 
107
  double addY(double increment) { return y_ += increment; }
 
108
 
 
109
  Cursor getTranslation(double x, double y) const
 
110
  {
 
111
    Cursor c = *this;
 
112
    c.addX(x);
 
113
    c.addY(y);
 
114
    return c;
 
115
  }
 
116
 
 
117
};
 
118
 
 
119
 
 
120
 
 
121
/**
 
122
 * @brief Event class used by TreeDrawing classes.
 
123
 */
 
124
class DrawNodeEvent
 
125
{
 
126
  private:
 
127
    const TreeDrawing* td_;
 
128
    GraphicDevice* gd_;
 
129
    int id_;
 
130
    Cursor cursor_;
 
131
 
 
132
  public:
 
133
    DrawNodeEvent(const TreeDrawing* source, GraphicDevice* gd, int nodeId, const Cursor& cursor) :
 
134
      td_(source), gd_(gd), id_(nodeId), cursor_(cursor)
 
135
    {}
 
136
 
 
137
    DrawNodeEvent(const DrawNodeEvent& dne) :
 
138
      td_(dne.td_), gd_(dne.gd_), id_(dne.id_), cursor_(dne.cursor_)
 
139
    {}
 
140
    
 
141
    DrawNodeEvent& operator=(const DrawNodeEvent& dne)
 
142
    {
 
143
      td_     = dne.td_;
 
144
      gd_     = dne.gd_;
 
145
      id_     = dne.id_;
 
146
      cursor_ = dne.cursor_;
 
147
      return *this;
 
148
    }
 
149
 
 
150
    virtual ~DrawNodeEvent() {}
 
151
 
 
152
  public:
 
153
    virtual const TreeDrawing* getTreeDrawing() const { return td_; }
 
154
    virtual GraphicDevice* getGraphicDevice() const { return gd_; }
 
155
    virtual int getNodeId() const { return id_; }
 
156
    virtual const Cursor& getCursor() const { return cursor_; }
 
157
 
 
158
};
 
159
 
 
160
 
 
161
 
 
162
/**
 
163
 * @brief Event class used by TreeDrawing classes.
 
164
 */
 
165
class DrawBranchEvent
 
166
{
 
167
  private:
 
168
    const TreeDrawing* td_;
 
169
    GraphicDevice* gd_;
 
170
    int id_;
 
171
    Cursor cursor_;
 
172
 
 
173
  public:
 
174
    DrawBranchEvent(const TreeDrawing* source, GraphicDevice* gd, int nodeId, const Cursor& cursor) :
 
175
      td_(source), gd_(gd), id_(nodeId), cursor_(cursor)
 
176
    {}
 
177
 
 
178
    DrawBranchEvent(const DrawBranchEvent& dne) :
 
179
      td_(dne.td_), gd_(dne.gd_), id_(dne.id_), cursor_(dne.cursor_)
 
180
    {}
 
181
    
 
182
    DrawBranchEvent& operator=(const DrawBranchEvent& dne)
 
183
    {
 
184
      td_     = dne.td_;
 
185
      gd_     = dne.gd_;
 
186
      id_     = dne.id_;
 
187
      cursor_ = dne.cursor_;
 
188
      return *this;
 
189
    }
 
190
 
 
191
    virtual ~DrawBranchEvent() {}
 
192
 
 
193
  public:
 
194
    virtual const TreeDrawing* getTreeDrawing() const { return td_; }
 
195
    virtual GraphicDevice* getGraphicDevice() const { return gd_; }
 
196
    virtual int getNodeId() const { return id_; }
 
197
    virtual const Cursor& getCursor() const { return cursor_; }
 
198
    /**
 
199
     * @return The coordinate of a point on the branch.
 
200
     * @param position The position of the point on the branch, as a proportion of the total branch length.
 
201
     */
 
202
    virtual Cursor getBranchCursor(double position) const = 0;
 
203
 
 
204
};
 
205
 
 
206
 
 
207
 
 
208
/**
 
209
 * @brief Event class used by TreeDrawing classes.
 
210
 */
 
211
class DrawTreeEvent
 
212
{
 
213
  private:
 
214
    const TreeDrawing* td_;
 
215
    GraphicDevice* gd_;
 
216
 
 
217
  public:
 
218
    DrawTreeEvent(const TreeDrawing* source, GraphicDevice* gd) :
 
219
      td_(source), gd_(gd)
 
220
    {}
 
221
 
 
222
    DrawTreeEvent(const DrawTreeEvent& dne) :
 
223
      td_(dne.td_), gd_(dne.gd_)
 
224
    {}
 
225
    
 
226
    DrawTreeEvent& operator=(const DrawTreeEvent& dte)
 
227
    {
 
228
      td_     = dte.td_;
 
229
      gd_     = dte.gd_;
 
230
      return *this;
 
231
    }
 
232
 
 
233
    virtual ~DrawTreeEvent() {}
 
234
 
 
235
  public:
 
236
    virtual const TreeDrawing* getTreeDrawing() const { return td_; }
 
237
    virtual GraphicDevice* getGraphicDevice() const { return gd_; }
 
238
 
 
239
};
 
240
 
 
241
 
 
242
 
 
243
/**
 
244
 * @brief Basal interface for tree drawing classes.
 
245
 *
 
246
 * Basicly, a TreeDrawing object draw a graph of the tree and compute the coordinates
 
247
 * of each node on the graph. These coordinates may be retrieved by dedicated functions.
 
248
 * The drawing is performed on a GraphicDevice object.
 
249
 *
 
250
 * The TreeDrwing class is in charge of the tree reprensentation, and offer tools to retireve
 
251
 * the coordinates of nodes. Using these functions to plot annotation may turn to be unefficient
 
252
 * however, particularly for large trees, as they involve a search on the whole tree. For easier
 
253
 * tuning of the drawing extensions, the interface defines the drawProperty,
 
254
 * getSupportedDrawableProperties and isDrawable methods. These methods can be used to add features
 
255
 * to the plot. Adding new features can then be performed by subclassing an existing algorithm
 
256
 * and adding support for more properties.
 
257
 *
 
258
 * The TreeDrawing interface do not implies that the implementation works on a copy of the tree.
 
259
 * It takes a constant pointer toward the tree to plot. Depending on the implementation however,
 
260
 * the inheriting class may chose to store a copy of the tree for convenience. Refer to the
 
261
 * documentation of the specific implementation you are using for details.
 
262
 *
 
263
 */
 
264
class TreeDrawing:
 
265
  public virtual Clonable
 
266
{
 
267
  public:
 
268
    TreeDrawing() {}
 
269
    virtual ~TreeDrawing() {}
 
270
 
 
271
#ifndef NO_VIRTUAL_COV
 
272
    TreeDrawing* clone() const = 0;
 
273
#endif
 
274
 
 
275
  public:
 
276
    /**
 
277
     * @return A string describing this drawing algorithm.
 
278
     */
 
279
    virtual std::string getName() const = 0;
 
280
 
 
281
    /**
 
282
     * @return 'True' if a tree is attached to this instance.
 
283
     */
 
284
    virtual bool hasTree() const = 0;
 
285
 
 
286
    /**
 
287
     * @return A pointer toward the tree associated to this drawing.
 
288
     */
 
289
    virtual const Tree* getTree() const = 0;
 
290
    
 
291
    /**
 
292
     * @param tree A pointer toward the tree to associate with this drawing.
 
293
     */
 
294
    virtual void setTree(const Tree* tree) = 0;
 
295
 
 
296
    /**
 
297
     * @brief Set the 'horizontal' expansion unit.
 
298
     *
 
299
     * The effect of this expansion factor depends on the implementation of the interface.
 
300
     * @param xu The horizontal unit length.
 
301
     */
 
302
    virtual void setXUnit(double xu) = 0; 
 
303
 
 
304
    /**
 
305
     * @brief Set the 'vertical' expansion unit.
 
306
     *
 
307
     * The effect of this expansion factor depends on the implementation of the interface.
 
308
     * @param yu The vertical unit length.
 
309
     */
 
310
    virtual void setYUnit(double yu) = 0;
 
311
 
 
312
    /**
 
313
     * @return The horizontal unit length.
 
314
     */
 
315
    virtual double getXUnit() const = 0;
 
316
    
 
317
    /**
 
318
     * @return The vertical unit length.
 
319
     */
 
320
    virtual double getYUnit() const = 0;
 
321
 
 
322
    /**
 
323
     * @return The total width of the drawing, in X units.
 
324
     */
 
325
    virtual double getWidth() const = 0; 
 
326
 
 
327
    /**
 
328
     * @return The total height of the drawing, in Y units.
 
329
     */
 
330
    virtual double getHeight() const = 0; 
 
331
 
 
332
    /**
 
333
     * @brief Plot the tree onto the specified device.
 
334
     *
 
335
     * @param gDevice An object implementing the GraphicDevice interface.
 
336
     */
 
337
    virtual void plot(GraphicDevice& gDevice) const throw (Exception) = 0;
 
338
 
 
339
    /**
 
340
     * @brief Get the position of a node.
 
341
     *
 
342
     * @param nodeId The identifier of the node.
 
343
     * @return The localization of the node using the coordinate system of the last GraphicDevice used.
 
344
     * @throw NodeNotFoundException If the node does not correspond to a node in the tree.
 
345
     */
 
346
    virtual Point2D<double> getNodePosition(int nodeId) const throw (NodeNotFoundException) = 0;
 
347
 
 
348
    /**
 
349
     * @brief Get the node corresponding to a position on the device.
 
350
     *
 
351
     * @param position A position in the coordinates system of the last GraphicDevice used.
 
352
     * @return The corresponding node identifier if available.
 
353
     * @throw NodeNotFoundException If the node does not correspond to a node in the tree.
 
354
     */
 
355
    virtual int getNodeAt(const Point2D<double>& position) const throw (NodeNotFoundException) = 0;
 
356
 
 
357
    /**
 
358
     * @brief Properties to draw.
 
359
     *
 
360
     * @{
 
361
     */
 
362
 
 
363
    /**
 
364
     * @brief Collapsing nodes
 
365
     *
 
366
     * @{
 
367
     */
 
368
    virtual void collapseNode(int nodeId, bool yn) throw (NodeNotFoundException, Exception) = 0;
 
369
    virtual bool isNodeCollapsed(int nodeId) const throw (NodeNotFoundException, Exception) = 0;
 
370
    /** @} */
 
371
 
 
372
    /**
 
373
     * @brief Global drawing settings.
 
374
     *
 
375
     * @{
 
376
     */
 
377
    virtual void setDisplaySettings(const TreeDrawingSettings* tds) = 0;
 
378
    virtual const TreeDrawingSettings& getDisplaySettings() const = 0;
 
379
    /** @} */
 
380
 
 
381
    /**
 
382
     * @brief Add a drawing listener to this instance.
 
383
     *
 
384
     * @param listener a pointer toward an object implementing the TreeDrawingListener interface.
 
385
     * This object will then be owned by the class and copied and deleted if/when needed, unless
 
386
     * it is autonomous.
 
387
     * @see TreeDrawingListener
 
388
     */
 
389
    virtual void addTreeDrawingListener(TreeDrawingListener* listener) = 0;
 
390
     
 
391
    /**
 
392
     * @brief Remove a drawing listener from this instance.
 
393
     *
 
394
     * @param listener a pointer toward an object implementing the TreeDrawingListener interface.
 
395
     * If the listener is autonomous, it will be deleted.
 
396
     */
 
397
    virtual void removeTreeDrawingListener(TreeDrawingListener* listener) = 0;
 
398
    };
 
399
 
 
400
} //end of namespace bpp.
 
401
 
 
402
#endif //_TREEDRAWING_H_
 
403