2 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
3 * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <sigc++/bind.h>
21 #include "ardour/tempo.h"
23 #include "canvas/rectangle.h"
24 #include "canvas/container.h"
25 #include "canvas/curve.h"
26 #include "canvas/canvas.h"
27 #include "canvas/debug.h"
29 #include "ui_config.h"
31 #include "tempo_curve.h"
32 #include "public_editor.h"
34 #include "rgb_macros.h"
36 #include <gtkmm2ext/utils.h>
40 PBD::Signal1<void,TempoCurve*> TempoCurve::CatchDeletion;
42 static double curve_height = 13.0;
44 void TempoCurve::setup_sizes(const double timebar_height)
46 curve_height = floor (timebar_height) - 2.5;
48 /* ignores Tempo note type - only note_types_per_minute is potentially curved */
49 TempoCurve::TempoCurve (PublicEditor& ed, ArdourCanvas::Container& parent, guint32 rgba, ARDOUR::TempoSection& temp, samplepos_t sample, bool handle_events)
56 , _min_tempo (temp.note_types_per_minute())
57 , _max_tempo (temp.note_types_per_minute())
62 sample_position = sample;
63 unit_position = editor.sample_to_pixel (sample);
65 group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple (unit_position, 1));
67 group->name = string_compose ("TempoCurve::group for %1", _tempo.note_types_per_minute());
70 _curve = new ArdourCanvas::FramedCurve (group);
72 _curve->name = string_compose ("TempoCurve::curve for %1", _tempo.note_types_per_minute());
74 _curve->set_points_per_segment (3);
75 points = new ArdourCanvas::Points ();
76 _curve->set (*points);
78 _start_text = new ArdourCanvas::Text (group);
79 _end_text = new ArdourCanvas::Text (group);
80 _start_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
81 _end_text->set_font_description (ARDOUR_UI_UTILS::get_font_for_style (N_("MarkerText")));
82 _start_text->set_color (RGBA_TO_UINT (255,255,255,255));
83 _end_text->set_color (RGBA_TO_UINT (255,255,255,255));
85 snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
86 _start_text->set (buf);
87 snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
90 set_color_rgba (rgba);
92 editor.ZoomChanged.connect (sigc::mem_fun (*this, &TempoCurve::reposition));
94 /* events will be handled by both the group and the mark itself, so
95 * make sure they can both be used to lookup this object.
98 _curve->set_data ("tempo curve", this);
101 //group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_marker_event), group, this));
104 group->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_tempo_curve_event), _curve, this));
108 TempoCurve::~TempoCurve ()
110 CatchDeletion (this); /* EMIT SIGNAL */
112 /* destroying the parent group destroys its contents, namely any polygons etc. that we added */
116 void TempoCurve::reparent(ArdourCanvas::Container & parent)
118 group->reparent (&parent);
123 TempoCurve::canvas_height_set (double h)
129 TempoCurve::the_item() const
135 TempoCurve::set_position (samplepos_t sample, samplepos_t end_sample)
137 unit_position = editor.sample_to_pixel (sample);
138 group->set_x_position (unit_position);
139 sample_position = sample;
140 _end_sample = end_sample;
143 points = new ArdourCanvas::Points ();
145 points->push_back (ArdourCanvas::Duple (0.0, curve_height));
147 if (sample >= end_sample) {
148 /* shouldn't happen but ..*/
149 const double tempo_at = _tempo.note_types_per_minute();
150 const double y_pos = (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
152 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
153 points->push_back (ArdourCanvas::Duple (1.0, y_pos));
155 } else if (_tempo.type() == ARDOUR::TempoSection::Constant || _tempo.c() == 0.0) {
156 const double tempo_at = _tempo.note_types_per_minute();
157 const double y_pos = (curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height);
159 points->push_back (ArdourCanvas::Duple (0.0, y_pos));
160 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), y_pos));
163 const samplepos_t sample_step = max ((end_sample - sample) / 5, (samplepos_t) 1);
164 samplepos_t current_sample = sample;
166 while (current_sample < end_sample) {
167 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (current_sample)).note_types_per_minute();
168 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
170 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (current_sample - sample), min (y_pos, curve_height)));
172 current_sample += sample_step;
175 const double tempo_at = _tempo.tempo_at_minute (_tempo.minute_at_sample (end_sample)).note_types_per_minute();
176 const double y_pos = max ((curve_height) - (((tempo_at - _min_tempo) / (_max_tempo - _min_tempo)) * curve_height), 0.0);
178 points->push_back (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample), min (y_pos, curve_height)));
181 _curve->set (*points);
184 snprintf (buf, sizeof (buf), "%.3f/%.0f", _tempo.note_types_per_minute(), _tempo.note_type());
185 _start_text->set (buf);
186 snprintf (buf, sizeof (buf), "%.3f", _tempo.end_note_types_per_minute());
187 _end_text->set (buf);
189 _start_text->set_position (ArdourCanvas::Duple (10, .5 ));
190 _end_text->set_position (ArdourCanvas::Duple (editor.sample_to_pixel (end_sample - sample) - _end_text->text_width() - 10, .5 ));
192 if (_end_text->text_width() + _start_text->text_width() + 20 > editor.sample_to_pixel (end_sample - sample)) {
202 TempoCurve::reposition ()
204 set_position (sample_position, _end_sample);
224 TempoCurve::set_color_rgba (uint32_t c)
227 _curve->set_fill_color (UIConfiguration::instance().color_mod (_color, "selection rect"));
228 _curve->set_outline_color (_color);