2 Copyright (C) 2002-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "canvas/line.h"
21 #include "canvas/canvas.h"
22 #include "canvas/debug.h"
23 #include "tempo_lines.h"
24 #include "ardour_ui.h"
28 #define MAX_CACHED_LINES 128
30 TempoLines::TempoLines (ArdourCanvas::GtkCanvasViewport& canvas_viewport, ArdourCanvas::Group* group, double screen_height)
31 : _canvas_viewport (canvas_viewport)
33 , _clean_left(DBL_MAX)
35 , _height(screen_height)
40 TempoLines::tempo_map_changed()
42 _clean_left = DBL_MAX;
46 // TODO: Dirty/slow, but 'needed' for zoom :(
47 for (Lines::iterator i = _lines.begin(); i != _lines.end(); d += 1.0) {
48 Lines::iterator next = i;
50 i->second->set_x0 (-d);
51 i->second->set_x1 (-d);
52 ArdourCanvas::Line* f = i->second;
54 _lines.insert(make_pair(- d, f));
62 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
70 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
76 TempoLines::draw (const ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
77 const ARDOUR::TempoMap::BBTPointList::const_iterator& end,
78 double frames_per_pixel)
80 ARDOUR::TempoMap::BBTPointList::const_iterator i;
81 ArdourCanvas::Line *line = 0;
89 const size_t needed = distance (begin, end);
91 ArdourCanvas::Rect const visible = _canvas_viewport.visible_area ();
93 /* get the first bar spacing */
97 bars = (*i).bar - (*begin).bar;
98 beats = distance (begin, end) - bars;
100 beat_density = (beats * 10.0f) / visible.width ();
102 if (beat_density > 4.0f) {
103 /* if the lines are too close together, they become useless */
108 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_pixel);
109 const double needed_right = xpos;
113 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_pixel);
114 const double needed_left = xpos;
116 Lines::iterator left = _lines.lower_bound(xpos); // first line >= xpos
118 bool exhausted = (left == _lines.end());
119 Lines::iterator li = left;
120 if (li != _lines.end())
123 // Tempo map hasn't changed and we're entirely within a clean
124 // range, don't need to do anything. Yay.
125 if (needed_left >= _clean_left && needed_right <= _clean_right) {
126 // cout << endl << "*** LINE CACHE PERFECT HIT" << endl;
130 //cout << endl << "*** LINE CACHE MISS" << endl;
132 bool invalidated = false;
134 for (i = begin; i != end; ++i) {
137 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
139 if (beat_density > 2.0) {
140 continue; /* only draw beat lines if the gaps between beats are large. */
142 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
145 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_pixel);
147 li = _lines.lower_bound(xpos); // first line >= xpos
149 line = (li != _lines.end()) ? li->second : 0;
150 assert(!line || line->x0() == li->first);
152 Lines::iterator next = li;
153 if (next != _lines.end())
156 exhausted = (next == _lines.end());
158 // Hooray, line is perfect
159 if (line && line->x0() == xpos) {
160 if (li != _lines.end())
163 line->set_outline_color (color);
165 // Use existing line, moving if necessary
166 } else if (!exhausted) {
167 Lines::iterator steal = _lines.end();
170 // Steal from the right
171 if (left->first > needed_left && li != steal && steal->first > needed_right) {
172 //cout << "*** STEALING FROM RIGHT" << endl;
173 double const x = steal->first;
174 line = steal->second;
178 line->set_outline_color (color);
179 _lines.insert(make_pair(xpos, line));
182 // Shift clean range left
183 _clean_left = min(_clean_left, xpos);
184 _clean_right = min(_clean_right, x);
186 // Move this line to where we need it
188 Lines::iterator existing = _lines.find(xpos);
189 if (existing != _lines.end()) {
190 //cout << "*** EXISTING LINE" << endl;
192 li->second->set_outline_color (color);
194 //cout << "*** MOVING LINE" << endl;
195 const double x1 = line->x0();
196 const bool was_clean = x1 >= _clean_left && x1 <= _clean_right;
197 invalidated = invalidated || was_clean;
198 // Invalidate clean portion (XXX: too harsh?)
199 _clean_left = needed_left;
200 _clean_right = needed_right;
202 line->set_outline_color (color);
205 _lines.insert(make_pair(xpos, line));
210 } else if (_lines.size() < needed || _lines.size() < MAX_CACHED_LINES) {
211 //cout << "*** CREATING LINE" << endl;
212 /* if we already have a line there ... don't sweat it */
213 if (_lines.find (xpos) == _lines.end()) {
214 line = new ArdourCanvas::Line (_group);
218 line->set_y1 (_height);
219 line->set_outline_color (color);
220 _lines.insert(make_pair(xpos, line));
223 // Steal from the left
225 //cout << "*** STEALING FROM LEFT" << endl;
226 if (_lines.find (xpos) == _lines.end()) {
227 Lines::iterator steal = _lines.begin();
228 double const x = steal->first;
229 line = steal->second;
231 line->set_outline_color (color);
234 _lines.insert(make_pair(xpos, line));
237 // Shift clean range right
238 _clean_left = max(_clean_left, x);
239 _clean_right = max(_clean_right, xpos);
244 // Extend range to what we've 'fixed'
246 _clean_left = min(_clean_left, needed_left);
247 _clean_right = max(_clean_right, needed_right);