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 "canvas/line_set.h"
|
|
|
|
#include "canvas/utils.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
|
|
|
class LineSorter {
|
|
|
|
public:
|
2014-06-09 15:39:19 -04:00
|
|
|
bool operator() (LineSet::Line const & a, LineSet::Line const & b) {
|
|
|
|
return a.pos < b.pos;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
LineSet::LineSet (Canvas* c, Orientation o)
|
|
|
|
: Item (c)
|
|
|
|
, _extent (0)
|
|
|
|
, _orientation (o)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
LineSet::LineSet (Item* parent, Orientation o)
|
|
|
|
: Item (parent)
|
2014-06-09 15:39:19 -04:00
|
|
|
, _extent (0)
|
|
|
|
, _orientation (o)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::compute_bounding_box () const
|
|
|
|
{
|
|
|
|
if (_lines.empty ()) {
|
|
|
|
_bounding_box = boost::optional<Rect> ();
|
2013-10-24 17:13:22 -04:00
|
|
|
} else {
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
if (_orientation == Horizontal) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
_bounding_box = Rect (0, /* x0 */
|
|
|
|
_lines.front().pos - (_lines.front().width/2.0), /* y0 */
|
|
|
|
_extent, /* x1 */
|
|
|
|
_lines.back().pos - (_lines.back().width/2.0) /* y1 */
|
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
_bounding_box = Rect (_lines.front().pos - _lines.front().width/2.0, /* x0 */
|
|
|
|
0, /* y0 */
|
|
|
|
_lines.back().pos + _lines.back().width/2.0, /* x1 */
|
|
|
|
_extent /* y1 */
|
|
|
|
);
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2014-06-09 15:39:19 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
_bounding_box_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-09 15:39:19 -04:00
|
|
|
LineSet::set_extent (Distance e)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
_extent = e;
|
2013-04-04 00:32:52 -04:00
|
|
|
_bounding_box_dirty = true;
|
2014-06-09 15:39:19 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
2013-10-22 21:12:47 -04:00
|
|
|
/* area is in window coordinates */
|
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
for (vector<Line>::const_iterator i = _lines.begin(); i != _lines.end(); ++i) {
|
|
|
|
|
|
|
|
Rect self;
|
2013-10-22 21:12:47 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
if (_orientation == Horizontal) {
|
|
|
|
self = item_to_window (Rect (0, i->pos - (i->width/2.0), _extent, i->pos + (i->width/2.0)));
|
|
|
|
} else {
|
|
|
|
self = item_to_window (Rect (i->pos - (i->width/2.0), 0, i->pos + (i->width/2.0), _extent));
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<Rect> isect = self.intersection (area);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
if (!isect) {
|
2015-10-05 10:17:49 -04:00
|
|
|
continue;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2013-10-24 17:13:22 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
Rect intersection (isect.get());
|
|
|
|
|
2013-10-24 17:13:22 -04:00
|
|
|
set_source_rgba (context, i->color);
|
|
|
|
context->set_line_width (i->width);
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
/* Not 100% sure that the computation of the invariant
|
|
|
|
* positions (y and x) below work correctly if the line width
|
|
|
|
* is not 1.0, but visual inspection suggests it is OK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (_orientation == Horizontal) {
|
|
|
|
double y = self.y0 + ((self.y1 - self.y0)/2.0);
|
|
|
|
context->move_to (intersection.x0, y);
|
|
|
|
context->line_to (intersection.x1, y);
|
|
|
|
} else {
|
|
|
|
double x = self.x0 + ((self.x1 - self.x0)/2.0);
|
|
|
|
context->move_to (x, intersection.y0);
|
|
|
|
context->line_to (x, intersection.y1);
|
|
|
|
}
|
|
|
|
|
2013-10-24 17:13:22 -04:00
|
|
|
context->stroke ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::add (Coord y, Distance width, Color color)
|
|
|
|
{
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
_lines.push_back (Line (y, width, color));
|
2014-06-09 15:39:19 -04:00
|
|
|
sort (_lines.begin(), _lines.end(), LineSorter());
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::clear ()
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
_lines.clear ();
|
|
|
|
_bounding_box_dirty = true;
|
|
|
|
end_change ();
|
|
|
|
}
|
2013-11-04 11:56:10 -05:00
|
|
|
|
|
|
|
bool
|
|
|
|
LineSet::covers (Duple const & /*point*/) const
|
|
|
|
{
|
2014-06-09 15:39:19 -04:00
|
|
|
/* lines are ordered by position along primary axis, so binary search
|
|
|
|
* to find the place to start looking.
|
|
|
|
*
|
|
|
|
* XXX but not yet.
|
|
|
|
*/
|
|
|
|
|
2013-11-04 11:56:10 -05:00
|
|
|
return false;
|
|
|
|
}
|