2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
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.
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
24 #include "lib/playlist.h"
25 #include "lib/image_content.h"
26 #include "film_editor.h"
28 #include "timeline_time_axis_view.h"
29 #include "timeline_video_content_view.h"
30 #include "timeline_audio_content_view.h"
31 #include "timeline_subtitle_content_view.h"
32 #include "content_panel.h"
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::dynamic_pointer_cast;
42 using boost::optional;
44 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
45 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
48 , _time_axis_view (new TimelineTimeAxisView (*this, 32))
51 , _down_view_position (0)
57 SetDoubleBuffered (true);
60 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
61 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
62 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
63 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
64 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
65 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
69 SetMinSize (wxSize (640, tracks() * track_height() + 96));
71 _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
72 _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2));
80 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
85 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
93 Timeline::playlist_changed ()
100 Timeline::recreate_views ()
102 shared_ptr<const Film> fl = _film.lock ();
108 _views.push_back (_time_axis_view);
110 ContentList content = fl->playlist()->content ();
112 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
113 if (dynamic_pointer_cast<VideoContent> (*i)) {
114 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, *i)));
117 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (*i);
118 if (ac && !ac->audio_mapping().mapped_dcp_channels().empty ()) {
119 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, *i)));
122 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
123 if (sc && sc->has_subtitles ()) {
124 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, sc)));
129 setup_pixels_per_second ();
134 Timeline::playlist_content_changed (int property)
138 if (property == ContentProperty::POSITION) {
140 setup_pixels_per_second ();
142 } else if (property == AudioContentProperty::AUDIO_MAPPING) {
148 Timeline::assign_tracks ()
150 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
151 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
157 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
158 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
163 shared_ptr<Content> content = cv->content();
167 TimelineViewList::iterator j = _views.begin();
168 while (j != _views.end()) {
169 shared_ptr<TimelineContentView> test = dynamic_pointer_cast<TimelineContentView> (*j);
175 shared_ptr<Content> test_content = test->content();
177 if (test && test->track() && test->track().get() == t) {
178 bool const no_overlap =
179 (content->position() < test_content->position() && content->end() < test_content->position()) ||
180 (content->position() > test_content->end() && content->end() > test_content->end());
183 /* we have an overlap on track `t' */
192 if (j == _views.end ()) {
193 /* no overlap on `t' */
199 _tracks = max (_tracks, t + 1);
202 _time_axis_view->set_y (tracks() * track_height() + 32);
206 Timeline::tracks () const
212 Timeline::setup_pixels_per_second ()
214 shared_ptr<const Film> film = _film.lock ();
215 if (!film || film->length() == DCPTime ()) {
219 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
222 shared_ptr<TimelineView>
223 Timeline::event_to_view (wxMouseEvent& ev)
225 TimelineViewList::iterator i = _views.begin();
226 Position<int> const p (ev.GetX(), ev.GetY());
227 while (i != _views.end() && !(*i)->bbox().contains (p)) {
231 if (i == _views.end ()) {
232 return shared_ptr<TimelineView> ();
239 Timeline::left_down (wxMouseEvent& ev)
241 shared_ptr<TimelineView> view = event_to_view (ev);
242 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
247 _down_view = content_view;
248 _down_view_position = content_view->content()->position ();
251 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
252 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
257 if (!ev.ShiftDown ()) {
258 cv->set_selected (view == *i);
262 _content_panel->set_selection (cv->content ());
266 if (content_view && ev.ShiftDown ()) {
267 content_view->set_selected (!content_view->selected ());
271 _down_point = ev.GetPosition ();
275 _down_view->content()->set_change_signals_frequent (true);
280 Timeline::left_up (wxMouseEvent& ev)
285 _down_view->content()->set_change_signals_frequent (false);
288 set_position_from_event (ev);
292 Timeline::mouse_moved (wxMouseEvent& ev)
298 set_position_from_event (ev);
302 Timeline::right_down (wxMouseEvent& ev)
304 shared_ptr<TimelineView> view = event_to_view (ev);
305 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
310 if (!cv->selected ()) {
312 cv->set_selected (true);
315 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
319 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
321 DCPTime const d = a - b;
322 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
323 nearest_distance = d;
328 Timeline::set_position_from_event (wxMouseEvent& ev)
330 if (!_pixels_per_second) {
334 double const pps = _pixels_per_second.get ();
336 wxPoint const p = ev.GetPosition();
339 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
340 before the drag is considered to have started.
342 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
353 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
357 DCPTime const new_end = new_position + _down_view->content()->length_after_trim ();
358 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
361 optional<DCPTime> nearest_distance;
363 /* Find the nearest content edge; this is inefficient */
364 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
365 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
366 if (!cv || cv == _down_view) {
370 maybe_snap (cv->content()->position(), new_position, nearest_distance);
371 maybe_snap (cv->content()->position(), new_end, nearest_distance);
372 maybe_snap (cv->content()->end(), new_position, nearest_distance);
373 maybe_snap (cv->content()->end(), new_end, nearest_distance);
376 if (nearest_distance) {
377 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
378 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
379 new_position += nearest_distance.get ();
384 if (new_position < DCPTime ()) {
385 new_position = DCPTime ();
388 _down_view->content()->set_position (new_position);
390 shared_ptr<Film> film = _film.lock ();
391 DCPOMATIC_ASSERT (film);
392 film->set_sequence_video (false);
396 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
398 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
401 shared_ptr<const Film>
402 Timeline::film () const
404 return _film.lock ();
410 setup_pixels_per_second ();
414 Timeline::clear_selection ()
416 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
417 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
419 cv->set_selected (false);
424 TimelineContentViewList
425 Timeline::selected_views () const
427 TimelineContentViewList sel;
429 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
430 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
431 if (cv && cv->selected()) {
440 Timeline::selected_content () const
443 TimelineContentViewList views = selected_views ();
445 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
446 sel.push_back ((*i)->content ());