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 <libgnomecanvasmm/canvas.h>
21 #include <libgnomecanvasmm/group.h>
22 #include "tempo_lines.h"
23 #include "ardour_ui.h"
27 #define MAX_CACHED_LINES 128
29 TempoLines::TempoLines(ArdourCanvas::Canvas& canvas, ArdourCanvas::Group* group, double screen_height)
32 , _clean_left(DBL_MAX)
34 , _height(screen_height)
39 TempoLines::tempo_map_changed()
41 _clean_left = DBL_MAX;
45 // TODO: Dirty/slow, but 'needed' for zoom :(
46 for (Lines::iterator i = _lines.begin(); i != _lines.end(); d += 1.0) {
47 Lines::iterator next = i;
49 i->second->property_x1() = - d;
50 i->second->property_x2() = - d;
51 ArdourCanvas::SimpleLine* f = i->second;
53 _lines.insert(make_pair(- d, f));
61 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
69 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
75 TempoLines::draw (const ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
76 const ARDOUR::TempoMap::BBTPointList::const_iterator& end,
77 double frames_per_unit)
79 ARDOUR::TempoMap::BBTPointList::const_iterator i;
80 ArdourCanvas::SimpleLine *line = NULL;
83 double x1, x2, y1, beat_density;
89 const size_t needed = distance (begin, end);
91 _canvas.get_scroll_region (x1, y1, x2, who_cares);
93 /* get the first bar spacing */
97 bars = (*i).bar - (*begin).bar;
98 beats = distance (begin, end) - bars;
100 beat_density = (beats * 10.0f) / _canvas.get_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_unit);
109 const double needed_right = xpos;
113 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_unit);
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 inserted_last_time = true;
133 bool invalidated = false;
135 for (i = begin; i != end; ++i) {
138 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
140 if (beat_density > 2.0) {
141 continue; /* only draw beat lines if the gaps between beats are large. */
143 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
146 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_unit);
148 if (inserted_last_time && !_lines.empty()) {
149 li = _lines.lower_bound(xpos); // first line >= xpos
152 line = (li != _lines.end()) ? li->second : NULL;
153 assert(!line || line->property_x1() == li->first);
155 Lines::iterator next = li;
156 if (next != _lines.end())
159 exhausted = (next == _lines.end());
161 // Hooray, line is perfect
162 if (line && line->property_x1() == xpos) {
163 if (li != _lines.end())
166 line->property_color_rgba() = color;
167 inserted_last_time = false; // don't search next time
169 // Use existing line, moving if necessary
170 } else if (!exhausted) {
171 Lines::iterator steal = _lines.end();
174 // Steal from the right
175 if (left->first > needed_left && li != steal && steal->first > needed_right) {
176 //cout << "*** STEALING FROM RIGHT" << endl;
177 double const x = steal->first;
178 line = steal->second;
180 line->property_x1() = xpos;
181 line->property_x2() = xpos;
182 line->property_color_rgba() = color;
183 _lines.insert(make_pair(xpos, line));
184 inserted_last_time = true; // search next time
187 // Shift clean range left
188 _clean_left = min(_clean_left, xpos);
189 _clean_right = min(_clean_right, x);
191 // Move this line to where we need it
193 Lines::iterator existing = _lines.find(xpos);
194 if (existing != _lines.end()) {
195 //cout << "*** EXISTING LINE" << endl;
197 li->second->property_color_rgba() = color;
198 inserted_last_time = false; // don't search next time
200 //cout << "*** MOVING LINE" << endl;
201 const double x1 = line->property_x1();
202 const bool was_clean = x1 >= _clean_left && x1 <= _clean_right;
203 invalidated = invalidated || was_clean;
204 // Invalidate clean portion (XXX: too harsh?)
205 _clean_left = needed_left;
206 _clean_right = needed_right;
208 line->property_color_rgba() = color;
209 line->property_x1() = xpos;
210 line->property_x2() = xpos;
211 _lines.insert(make_pair(xpos, line));
212 inserted_last_time = true; // search next time
217 } else if (_lines.size() < needed || _lines.size() < MAX_CACHED_LINES) {
218 //cout << "*** CREATING LINE" << endl;
219 assert(_lines.find(xpos) == _lines.end());
220 line = new ArdourCanvas::SimpleLine (*_group);
221 line->property_x1() = xpos;
222 line->property_x2() = xpos;
223 line->property_y1() = 0.0;
224 line->property_y2() = _height;
225 line->property_color_rgba() = color;
226 _lines.insert(make_pair(xpos, line));
227 inserted_last_time = true;
229 // Steal from the left
231 //cout << "*** STEALING FROM LEFT" << endl;
232 assert(_lines.find(xpos) == _lines.end());
233 Lines::iterator steal = _lines.begin();
234 double const x = steal->first;
235 line = steal->second;
237 line->property_color_rgba() = color;
238 line->property_x1() = xpos;
239 line->property_x2() = xpos;
240 _lines.insert(make_pair(xpos, line));
241 inserted_last_time = true; // search next time
244 // Shift clean range right
245 _clean_left = max(_clean_left, x);
246 _clean_right = max(_clean_right, xpos);
250 // Extend range to what we've 'fixed'
252 _clean_left = min(_clean_left, needed_left);
253 _clean_right = max(_clean_right, needed_right);