summaryrefslogtreecommitdiff
path: root/libs/canvas/poly_item.cc
blob: d6e67ede0c47b5094fbc929753f0dbec5d94b53e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <algorithm>

#include "pbd/xml++.h"
#include "pbd/compose.h"

#include "canvas/poly_item.h"
#include "canvas/canvas.h"

using namespace std;
using namespace ArdourCanvas;

PolyItem::PolyItem (Group* parent)
	: Item (parent)
	, Outline (parent)
{

}

void
PolyItem::compute_bounding_box () const
{
	bool have_one = false;

	Rect bbox;

	for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
		if (have_one) {
			bbox.x0 = min (bbox.x0, i->x);
			bbox.y0 = min (bbox.y0, i->y);
			bbox.x1 = max (bbox.x1, i->x);
			bbox.y1 = max (bbox.y1, i->y);
		} else {
			bbox.x0 = bbox.x1 = i->x;
			bbox.y0 = bbox.y1 = i->y;
			have_one = true;
		}
	}


	if (!have_one) {
		_bounding_box = boost::optional<Rect> ();
	} else {
		_bounding_box = bbox.expand (_outline_width / 2);
	}
	
	_bounding_box_dirty = false;
}

void
PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
{
	bool done_first = false;
	for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
		if (done_first) {
			context->line_to (i->x, i->y);
		} else {
			context->move_to (i->x, i->y);
			done_first = true;
		}
	}
}

void
PolyItem::set (Points const & points)
{
	begin_change ();
	
	_points = points;
	
	_bounding_box_dirty = true;
	end_change ();
}

Points const &
PolyItem::get () const
{
	return _points;
}

void
PolyItem::add_poly_item_state (XMLNode* node) const
{
	add_item_state (node);
	
	for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
		XMLNode* p = new XMLNode ("Point");
		p->add_property ("x", string_compose ("%1", i->x));
		p->add_property ("y", string_compose ("%1", i->y));
		node->add_child_nocopy (*p);
	}
}

void
PolyItem::set_poly_item_state (XMLNode const * node)
{
	XMLNodeList const & children = node->children ();
	for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
		Duple p;
		p.x = atof ((*i)->property("x")->value().c_str());
		p.y = atof ((*i)->property("y")->value().c_str());
		_points.push_back (p);
	}

	_bounding_box_dirty = true;
}

void
PolyItem::dump (ostream& o) const
{
	Item::dump (o);

	o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
	for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
		o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
	}
}