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.
20 #include "film_editor.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_video_content_view.h"
24 #include "timeline_audio_content_view.h"
25 #include "timeline_subtitle_content_view.h"
26 #include "content_panel.h"
29 #include "lib/playlist.h"
30 #include "lib/image_content.h"
31 #include "lib/audio_content.h"
32 #include "lib/subtitle_content.h"
33 #include <wx/graphics.h>
34 #include <boost/weak_ptr.hpp>
35 #include <boost/foreach.hpp>
42 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::dynamic_pointer_cast;
46 using boost::optional;
48 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
49 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
52 , _time_axis_view (new TimelineTimeAxisView (*this, 32))
55 , _down_view_position (0)
61 SetDoubleBuffered (true);
64 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
65 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
66 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
67 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
68 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
69 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
71 film_changed (Film::CONTENT);
73 SetMinSize (wxSize (640, tracks() * track_height() + 96));
75 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
76 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2));
84 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
89 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
97 Timeline::film_changed (Film::Property p)
99 if (p == Film::CONTENT) {
106 Timeline::recreate_views ()
108 shared_ptr<const Film> film = _film.lock ();
114 _views.push_back (_time_axis_view);
116 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
117 if (dynamic_pointer_cast<VideoContent> (i)) {
118 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
121 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
122 if (ac && !ac->audio_mapping().mapped_output_channels().empty ()) {
123 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
126 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
127 if (sc && sc->has_subtitles ()) {
128 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, sc)));
133 setup_pixels_per_second ();
138 Timeline::film_content_changed (int property)
142 if (property == ContentProperty::POSITION) {
145 /* Only do this if we are not dragging, as it's confusing otherwise */
146 setup_pixels_per_second ();
149 } else if (property == AudioContentProperty::AUDIO_STREAMS) {
155 Timeline::assign_tracks ()
157 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
158 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
164 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
165 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
170 shared_ptr<Content> content = cv->content();
171 DCPTimePeriod content_period (content->position(), content->end());
175 TimelineViewList::iterator j = _views.begin();
176 while (j != _views.end()) {
177 shared_ptr<TimelineContentView> test = dynamic_pointer_cast<TimelineContentView> (*j);
183 shared_ptr<Content> test_content = test->content();
185 if (test && test->track() && test->track().get() == t) {
186 if (content_period.overlaps (DCPTimePeriod(test_content->position(), test_content->end()))) {
187 /* we have an overlap on track `t' */
196 if (j == _views.end ()) {
197 /* no overlap on `t' */
203 _tracks = max (_tracks, t + 1);
206 _time_axis_view->set_y (tracks() * track_height() + 32);
210 Timeline::tracks () const
216 Timeline::setup_pixels_per_second ()
218 shared_ptr<const Film> film = _film.lock ();
219 if (!film || film->length() == DCPTime ()) {
223 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
226 shared_ptr<TimelineView>
227 Timeline::event_to_view (wxMouseEvent& ev)
229 TimelineViewList::iterator i = _views.begin();
230 Position<int> const p (ev.GetX(), ev.GetY());
231 while (i != _views.end() && !(*i)->bbox().contains (p)) {
235 if (i == _views.end ()) {
236 return shared_ptr<TimelineView> ();
243 Timeline::left_down (wxMouseEvent& ev)
245 shared_ptr<TimelineView> view = event_to_view (ev);
246 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
251 _down_view = content_view;
252 _down_view_position = content_view->content()->position ();
255 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
256 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
261 if (!ev.ShiftDown ()) {
262 cv->set_selected (view == *i);
266 _content_panel->set_selection (cv->content ());
270 if (content_view && ev.ShiftDown ()) {
271 content_view->set_selected (!content_view->selected ());
275 _down_point = ev.GetPosition ();
279 _down_view->content()->set_change_signals_frequent (true);
284 Timeline::left_up (wxMouseEvent& ev)
289 _down_view->content()->set_change_signals_frequent (false);
292 set_position_from_event (ev);
294 /* We don't do this during drag, and set_position_from_event above
295 might not have changed the position, so do it now.
297 setup_pixels_per_second ();
302 Timeline::mouse_moved (wxMouseEvent& ev)
308 set_position_from_event (ev);
312 Timeline::right_down (wxMouseEvent& ev)
314 shared_ptr<TimelineView> view = event_to_view (ev);
315 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
320 if (!cv->selected ()) {
322 cv->set_selected (true);
325 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
329 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
331 DCPTime const d = a - b;
332 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
333 nearest_distance = d;
338 Timeline::set_position_from_event (wxMouseEvent& ev)
340 if (!_pixels_per_second) {
344 double const pps = _pixels_per_second.get ();
346 wxPoint const p = ev.GetPosition();
349 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
350 before the drag is considered to have started.
352 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
363 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
367 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
368 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
371 optional<DCPTime> nearest_distance;
373 /* Find the nearest content edge; this is inefficient */
374 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
375 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
376 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
380 maybe_snap (cv->content()->position(), new_position, nearest_distance);
381 maybe_snap (cv->content()->position(), new_end, nearest_distance);
382 maybe_snap (cv->content()->end(), new_position, nearest_distance);
383 maybe_snap (cv->content()->end(), new_end, nearest_distance);
386 if (nearest_distance) {
387 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
388 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
389 new_position += nearest_distance.get ();
394 if (new_position < DCPTime ()) {
395 new_position = DCPTime ();
398 _down_view->content()->set_position (new_position);
400 shared_ptr<Film> film = _film.lock ();
401 DCPOMATIC_ASSERT (film);
402 film->set_sequence_video (false);
406 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
408 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
411 shared_ptr<const Film>
412 Timeline::film () const
414 return _film.lock ();
420 setup_pixels_per_second ();
424 Timeline::clear_selection ()
426 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
427 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
429 cv->set_selected (false);
434 TimelineContentViewList
435 Timeline::selected_views () const
437 TimelineContentViewList sel;
439 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
440 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
441 if (cv && cv->selected()) {
450 Timeline::selected_content () const
453 TimelineContentViewList views = selected_views ();
455 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
456 sel.push_back ((*i)->content ());