mo' better debugging of canvas "structure" via Item::dump and derivatives
[ardour.git] / libs / canvas / poly_item.cc
1 #include <algorithm>
2
3 #include "pbd/xml++.h"
4 #include "pbd/compose.h"
5
6 #include "canvas/poly_item.h"
7 #include "canvas/canvas.h"
8
9 using namespace std;
10 using namespace ArdourCanvas;
11
12 PolyItem::PolyItem (Group* parent)
13         : Item (parent)
14         , Outline (parent)
15 {
16
17 }
18
19 void
20 PolyItem::compute_bounding_box () const
21 {
22         bool have_one = false;
23
24         Rect bbox;
25
26         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
27                 if (have_one) {
28                         bbox.x0 = min (bbox.x0, i->x);
29                         bbox.y0 = min (bbox.y0, i->y);
30                         bbox.x1 = max (bbox.x1, i->x);
31                         bbox.y1 = max (bbox.y1, i->y);
32                 } else {
33                         bbox.x0 = bbox.x1 = i->x;
34                         bbox.y0 = bbox.y1 = i->y;
35                         have_one = true;
36                 }
37         }
38
39
40         if (!have_one) {
41                 _bounding_box = boost::optional<Rect> ();
42         } else {
43                 _bounding_box = bbox.expand (_outline_width / 2);
44         }
45         
46         _bounding_box_dirty = false;
47 }
48
49 void
50 PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
51 {
52         bool done_first = false;
53         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
54                 if (done_first) {
55                         context->line_to (i->x, i->y);
56                 } else {
57                         context->move_to (i->x, i->y);
58                         done_first = true;
59                 }
60         }
61 }
62
63 void
64 PolyItem::set (Points const & points)
65 {
66         begin_change ();
67         
68         _points = points;
69         
70         _bounding_box_dirty = true;
71         end_change ();
72 }
73
74 Points const &
75 PolyItem::get () const
76 {
77         return _points;
78 }
79
80 void
81 PolyItem::add_poly_item_state (XMLNode* node) const
82 {
83         add_item_state (node);
84         
85         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
86                 XMLNode* p = new XMLNode ("Point");
87                 p->add_property ("x", string_compose ("%1", i->x));
88                 p->add_property ("y", string_compose ("%1", i->y));
89                 node->add_child_nocopy (*p);
90         }
91 }
92
93 void
94 PolyItem::set_poly_item_state (XMLNode const * node)
95 {
96         XMLNodeList const & children = node->children ();
97         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
98                 Duple p;
99                 p.x = atof ((*i)->property("x")->value().c_str());
100                 p.y = atof ((*i)->property("y")->value().c_str());
101                 _points.push_back (p);
102         }
103
104         _bounding_box_dirty = true;
105 }
106
107 void
108 PolyItem::dump (ostream& o) const
109 {
110         Item::dump (o);
111
112         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
113         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
114                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
115         }
116 }