2013-04-15 22:10:18 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011-2013 Paul Davis
|
|
|
|
Author: Carl Hetherington <cth@carlh.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include <algorithm>
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "pbd/compose.h"
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/poly_item.h"
|
2013-04-05 11:27:26 -04:00
|
|
|
#include "canvas/canvas.h"
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
PolyItem::PolyItem (Canvas* c)
|
|
|
|
: Item (c)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-06-12 14:53:44 -04:00
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
PolyItem::PolyItem (Item* parent)
|
|
|
|
: Item (parent)
|
2014-06-12 14:53:44 -04:00
|
|
|
{
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PolyItem::compute_bounding_box () const
|
|
|
|
{
|
2014-03-07 11:24:51 -05:00
|
|
|
|
|
|
|
if (!_points.empty()) {
|
|
|
|
|
|
|
|
Rect bbox;
|
|
|
|
Points::const_iterator i = _points.begin();
|
|
|
|
|
|
|
|
bbox.x0 = bbox.x1 = i->x;
|
|
|
|
bbox.y0 = bbox.y1 = i->y;
|
2014-03-10 10:41:41 -04:00
|
|
|
|
|
|
|
++i;
|
|
|
|
|
2014-03-07 11:24:51 -05:00
|
|
|
while (i != _points.end()) {
|
2013-04-04 00:32:52 -04:00
|
|
|
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);
|
2014-03-07 11:24:51 -05:00
|
|
|
++i;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-06-27 10:30:09 -04:00
|
|
|
_bounding_box = bbox.expand (_outline_width + 0.5);
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2014-03-07 11:24:51 -05:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
} else {
|
2014-03-07 11:24:51 -05:00
|
|
|
_bounding_box = boost::optional<Rect> ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_bounding_box_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-06 20:45:23 -05:00
|
|
|
PolyItem::render_path (Rect const & /* area */, Cairo::RefPtr<Cairo::Context> context) const
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-01-03 14:05:05 -05:00
|
|
|
if (_points.size() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-28 13:52:08 -05:00
|
|
|
Points::const_iterator i = _points.begin();
|
|
|
|
Duple c (item_to_window (Duple (i->x, i->y)));
|
2014-01-13 09:28:06 -05:00
|
|
|
const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
|
2013-12-28 13:52:08 -05:00
|
|
|
|
2014-01-13 09:28:06 -05:00
|
|
|
context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
2014-01-06 20:45:23 -05:00
|
|
|
++i;
|
2013-12-28 13:52:08 -05:00
|
|
|
|
|
|
|
while (i != _points.end()) {
|
|
|
|
c = item_to_window (Duple (i->x, i->y));
|
2014-01-13 09:28:06 -05:00
|
|
|
context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
2013-12-28 13:52:08 -05:00
|
|
|
++i;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 22:54:12 -04:00
|
|
|
void
|
|
|
|
PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
|
|
|
|
{
|
|
|
|
if (_points.size() <= 2) {
|
|
|
|
render_path (area, context);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Points::const_iterator cp1 = first_control_points.begin();
|
|
|
|
Points::const_iterator cp2 = second_control_points.begin();
|
2014-01-03 14:05:05 -05:00
|
|
|
Points::const_iterator p = _points.begin();
|
2014-01-13 09:28:06 -05:00
|
|
|
const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
|
2013-04-11 22:54:12 -04:00
|
|
|
|
2014-01-03 14:05:05 -05:00
|
|
|
Duple c = item_to_window (Duple (p->x, p->y));
|
2014-01-13 09:28:06 -05:00
|
|
|
context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
2014-01-06 20:45:23 -05:00
|
|
|
++p;
|
2013-04-11 22:54:12 -04:00
|
|
|
|
2014-01-03 14:05:05 -05:00
|
|
|
while (p != _points.end()) {
|
2013-06-18 08:23:06 -04:00
|
|
|
|
2014-01-03 14:05:05 -05:00
|
|
|
Duple c1 = item_to_window (Duple (cp1->x, cp1->y));
|
|
|
|
Duple c2 = item_to_window (Duple (cp2->x, cp2->y));
|
2013-04-11 22:54:12 -04:00
|
|
|
|
2014-01-03 14:05:05 -05:00
|
|
|
c = item_to_window (Duple (p->x, p->y));
|
|
|
|
|
2014-01-13 09:28:06 -05:00
|
|
|
context->curve_to (c1.x + pixel_adjust,
|
|
|
|
c1.y + pixel_adjust,
|
|
|
|
c2.x + pixel_adjust,
|
|
|
|
c2.y + pixel_adjust,
|
|
|
|
c.x + pixel_adjust,
|
|
|
|
c.y + pixel_adjust);
|
2014-01-03 14:05:05 -05:00
|
|
|
|
|
|
|
++cp1;
|
|
|
|
++cp2;
|
|
|
|
++p;
|
2013-04-11 22:54:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
void
|
|
|
|
PolyItem::set (Points const & points)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (_points != points) {
|
|
|
|
|
|
|
|
begin_change ();
|
|
|
|
|
|
|
|
_points = points;
|
|
|
|
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Points const &
|
|
|
|
PolyItem::get () const
|
|
|
|
{
|
|
|
|
return _points;
|
|
|
|
}
|
|
|
|
|
2013-04-05 11:27:26 -04:00
|
|
|
void
|
|
|
|
PolyItem::dump (ostream& o) const
|
|
|
|
{
|
|
|
|
Item::dump (o);
|
|
|
|
|
2013-04-08 19:48:09 -04:00
|
|
|
o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
|
2013-04-05 11:27:26 -04:00
|
|
|
for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
|
2013-04-08 19:48:09 -04:00
|
|
|
o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
|
2013-04-05 11:27:26 -04:00
|
|
|
}
|
|
|
|
}
|