2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "film_editor.h"
23 #include "timeline_time_axis_view.h"
24 #include "timeline_reels_view.h"
25 #include "timeline_labels_view.h"
26 #include "timeline_video_content_view.h"
27 #include "timeline_audio_content_view.h"
28 #include "timeline_text_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
32 #include "film_viewer.h"
34 #include "lib/playlist.h"
35 #include "lib/image_content.h"
36 #include "lib/timer.h"
37 #include "lib/audio_content.h"
38 #include "lib/text_content.h"
39 #include "lib/video_content.h"
40 #include "lib/atmos_mxf_content.h"
41 #include <wx/graphics.h>
42 #include <boost/weak_ptr.hpp>
43 #include <boost/foreach.hpp>
53 using boost::shared_ptr;
54 using boost::weak_ptr;
55 using boost::dynamic_pointer_cast;
57 using boost::optional;
58 using namespace dcpomatic;
60 /* 3 hours in 640 pixels */
61 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
62 int const Timeline::_minimum_pixels_per_track = 16;
64 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
65 : wxPanel (parent, wxID_ANY)
66 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
67 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
71 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
72 , _reels_view (new TimelineReelsView (*this, 32))
73 , _labels_view (new TimelineLabelsView (*this))
76 , _down_view_position (0)
83 , _pixels_per_track (48)
84 , _first_resize (true)
87 _labels_canvas->SetDoubleBuffered (true);
88 _main_canvas->SetDoubleBuffered (true);
91 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
92 sizer->Add (_labels_canvas, 0, wxEXPAND);
93 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
94 sizer->Add (_main_canvas, 1, wxEXPAND);
97 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
98 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
99 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
100 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
101 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
102 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
103 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
104 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
105 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
106 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
107 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
108 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
109 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
110 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
112 film_change (CHANGE_TYPE_DONE, Film::CONTENT);
114 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
116 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
117 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
119 shared_ptr<FilmViewer> vp = viewer.lock ();
120 DCPOMATIC_ASSERT (vp);
121 _viewer_position_change_connection = vp->PositionChanged.connect (bind(&Timeline::position_change, this));
124 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
128 Timeline::position_change ()
134 Timeline::set_pixels_per_second (double pps)
136 _pixels_per_second = max (_minimum_pixels_per_second, pps);
140 Timeline::paint_labels ()
142 wxPaintDC dc (_labels_canvas);
144 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
150 _labels_canvas->GetViewStart (&vsx, &vsy);
151 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
153 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
159 Timeline::paint_main ()
161 wxPaintDC dc (_main_canvas);
162 _main_canvas->DoPrepareDC (dc);
164 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
170 _main_canvas->GetViewStart (&vsx, &vsy);
171 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
173 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
175 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
177 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
179 /* Find areas of overlap with other content views, so that we can plot them */
180 list<dcpomatic::Rect<int> > overlaps;
181 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
182 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
183 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
184 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
188 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
190 overlaps.push_back (r.get ());
194 i->paint (gc, overlaps);
198 /* Translate back as _down_point and _zoom_point do not take scroll into account */
199 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
200 gc->SetPen (*wxBLACK_PEN);
201 gc->SetBrush (*wxTRANSPARENT_BRUSH);
203 min (_down_point.x, _zoom_point->x),
204 min (_down_point.y, _zoom_point->y),
205 abs (_down_point.x - _zoom_point->x),
206 abs (_down_point.y - _zoom_point->y)
212 shared_ptr<FilmViewer> vp = _viewer.lock ();
213 DCPOMATIC_ASSERT (vp);
215 gc->SetPen (*wxRED_PEN);
216 wxGraphicsPath path = gc->CreatePath ();
217 double const ph = vp->position().seconds() * pixels_per_second().get_value_or(0);
218 path.MoveToPoint (ph, 0);
219 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
220 gc->StrokePath (path);
226 Timeline::film_change (ChangeType type, Film::Property p)
228 if (type != CHANGE_TYPE_DONE) {
232 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
235 } else if (p == Film::CONTENT_ORDER) {
241 Timeline::recreate_views ()
243 shared_ptr<const Film> film = _film.lock ();
249 _views.push_back (_time_axis_view);
250 _views.push_back (_reels_view);
252 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
254 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
257 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
258 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
261 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
262 _views.push_back (shared_ptr<TimelineView> (new TimelineTextContentView (*this, i, j)));
265 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
266 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
276 Timeline::film_content_change (ChangeType type, int property, bool frequent)
278 if (type != CHANGE_TYPE_DONE) {
284 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
286 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
287 _reels_view->force_redraw ();
288 } else if (!frequent) {
296 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
298 int const base = tracks;
300 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
301 if (!dynamic_pointer_cast<T>(i)) {
305 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
309 shared_ptr<Content> content = cv->content();
310 DCPTimePeriod const content_period (content->position(), content->end(film));
313 TimelineViewList::iterator j = views.begin();
314 while (j != views.end()) {
315 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
321 shared_ptr<Content> test_content = test->content();
323 test->track() && test->track().get() == t &&
324 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
325 /* we have an overlap on track `t' */
333 if (j == views.end ()) {
334 /* no overlap on `t' */
340 tracks = max (tracks, t + 1);
343 return tracks - base;
346 /** Compare the mapped output channels of two TimelineViews, so we can into
347 * order of first mapped DCP channel.
349 struct AudioMappingComparator {
350 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
352 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
354 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
355 la = *min_element(boost::begin(oc), boost::end(oc));
358 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
360 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
361 lb = *min_element(boost::begin(oc), boost::end(oc));
368 Timeline::assign_tracks ()
371 Video (mono or left-eye)
382 shared_ptr<const Film> film = _film.lock ();
383 DCPOMATIC_ASSERT (film);
387 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
388 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
396 bool have_3d = false;
397 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
398 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
403 /* Video on tracks 0 and maybe 1 (left and right eye) */
404 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
406 _tracks = max (_tracks, 2);
413 _tracks = max (_tracks, 1);
417 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
421 bool have_atmos = false;
422 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
423 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
427 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
428 cv->set_track (_tracks - 1);
437 /* Audio. We're sorting the views so that we get the audio views in order of increasing
441 TimelineViewList views = _views;
442 sort(views.begin(), views.end(), AudioMappingComparator());
443 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
445 _labels_view->set_3d (have_3d);
446 _labels_view->set_audio_tracks (audio_tracks);
447 _labels_view->set_text_tracks (text_tracks);
448 _labels_view->set_atmos (have_atmos);
450 _time_axis_view->set_y (tracks());
451 _reels_view->set_y (8);
455 Timeline::tracks () const
461 Timeline::setup_scrollbars ()
463 shared_ptr<const Film> film = _film.lock ();
464 if (!film || !_pixels_per_second) {
468 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
470 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
471 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
472 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
473 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
476 shared_ptr<TimelineView>
477 Timeline::event_to_view (wxMouseEvent& ev)
479 /* Search backwards through views so that we find the uppermost one first */
480 TimelineViewList::reverse_iterator i = _views.rbegin();
481 Position<int> const p (ev.GetX(), ev.GetY());
482 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
483 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
487 if (i == _views.rend ()) {
488 return shared_ptr<TimelineView> ();
495 Timeline::left_down (wxMouseEvent& ev)
498 _down_point = ev.GetPosition ();
502 left_down_select (ev);
514 Timeline::left_down_select (wxMouseEvent& ev)
516 shared_ptr<TimelineView> view = event_to_view (ev);
517 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
522 _down_view = content_view;
523 _down_view_position = content_view->content()->position ();
526 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
527 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
532 if (!ev.ShiftDown ()) {
533 cv->set_selected (view == *i);
537 if (content_view && ev.ShiftDown ()) {
538 content_view->set_selected (!content_view->selected ());
544 /* Pre-compute the points that we might snap to */
545 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
546 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
547 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
551 shared_ptr<Film> film = _film.lock ();
552 DCPOMATIC_ASSERT (film);
554 _start_snaps.push_back (cv->content()->position());
555 _end_snaps.push_back (cv->content()->position());
556 _start_snaps.push_back (cv->content()->end(film));
557 _end_snaps.push_back (cv->content()->end(film));
559 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points(film)) {
560 _start_snaps.push_back (i);
564 /* Tell everyone that things might change frequently during the drag */
565 _down_view->content()->set_change_signals_frequent (true);
570 Timeline::left_up (wxMouseEvent& ev)
589 Timeline::left_up_select (wxMouseEvent& ev)
592 _down_view->content()->set_change_signals_frequent (false);
595 _content_panel->set_selection (selected_content ());
596 /* Since we may have just set change signals back to `not-frequent', we have to
597 make sure this position change is signalled, even if the position value has
598 not changed since the last time it was set (with frequent=true). This is
601 set_position_from_event (ev, true);
603 /* Clear up up the stuff we don't do during drag */
608 _start_snaps.clear ();
613 Timeline::left_up_zoom (wxMouseEvent& ev)
615 _zoom_point = ev.GetPosition ();
618 _main_canvas->GetViewStart (&vsx, &vsy);
620 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
621 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
623 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
624 /* Very small zoom rectangle: we assume it wasn't intentional */
628 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
629 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
630 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
632 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
633 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
634 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
637 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
638 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
639 _labels_canvas->Scroll (0, y);
641 _zoom_point = optional<wxPoint> ();
646 Timeline::set_pixels_per_track (int h)
648 _pixels_per_track = max(_minimum_pixels_per_track, h);
652 Timeline::mouse_moved (wxMouseEvent& ev)
656 mouse_moved_select (ev);
659 mouse_moved_zoom (ev);
669 Timeline::mouse_moved_select (wxMouseEvent& ev)
675 set_position_from_event (ev);
679 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
685 _zoom_point = ev.GetPosition ();
690 Timeline::right_down (wxMouseEvent& ev)
694 right_down_select (ev);
698 set_pixels_per_second (*_pixels_per_second / 2);
699 set_pixels_per_track (_pixels_per_track / 2);
711 Timeline::right_down_select (wxMouseEvent& ev)
713 shared_ptr<TimelineView> view = event_to_view (ev);
714 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
719 if (!cv->selected ()) {
721 cv->set_selected (true);
724 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
728 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
730 DCPTime const d = a - b;
731 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
732 nearest_distance = d;
737 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
739 if (!_pixels_per_second) {
743 double const pps = _pixels_per_second.get ();
745 wxPoint const p = ev.GetPosition();
748 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
749 before the drag is considered to have started.
751 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
762 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
764 shared_ptr<Film> film = _film.lock ();
765 DCPOMATIC_ASSERT (film);
768 DCPTime const new_end = new_position + _down_view->content()->length_after_trim(film);
769 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
772 optional<DCPTime> nearest_distance;
774 /* Find the nearest snap point */
776 BOOST_FOREACH (DCPTime i, _start_snaps) {
777 maybe_snap (i, new_position, nearest_distance);
780 BOOST_FOREACH (DCPTime i, _end_snaps) {
781 maybe_snap (i, new_end, nearest_distance);
784 if (nearest_distance) {
785 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
786 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
787 new_position += nearest_distance.get ();
792 if (new_position < DCPTime ()) {
793 new_position = DCPTime ();
796 _down_view->content()->set_position (film, new_position, force_emit);
798 film->set_sequence (false);
802 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
804 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
807 shared_ptr<const Film>
808 Timeline::film () const
810 return _film.lock ();
816 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
818 _first_resize = false;
824 Timeline::clear_selection ()
826 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
827 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
829 cv->set_selected (false);
834 TimelineContentViewList
835 Timeline::selected_views () const
837 TimelineContentViewList sel;
839 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
840 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
841 if (cv && cv->selected()) {
850 Timeline::selected_content () const
853 TimelineContentViewList views = selected_views ();
855 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
856 sel.push_back ((*i)->content ());
863 Timeline::set_selection (ContentList selection)
865 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
866 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
868 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
874 Timeline::tracks_y_offset () const
876 return _reels_view->bbox().height + 4;
880 Timeline::width () const
882 return _main_canvas->GetVirtualSize().GetWidth();
886 Timeline::scrolled (wxScrollWinEvent& ev)
888 if (ev.GetOrientation() == wxVERTICAL) {
890 _main_canvas->GetViewStart (&x, &y);
891 _labels_canvas->Scroll (0, y);
897 Timeline::tool_clicked (Tool t)
914 Timeline::zoom_all ()
916 shared_ptr<Film> film = _film.lock ();
917 DCPOMATIC_ASSERT (film);
918 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
919 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
921 _main_canvas->Scroll (0, 0);
922 _labels_canvas->Scroll (0, 0);