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"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/text_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
51 using boost::shared_ptr;
52 using boost::weak_ptr;
53 using boost::dynamic_pointer_cast;
55 using boost::optional;
57 /* 3 hours in 640 pixels */
58 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
59 int const Timeline::_minimum_pixels_per_track = 16;
61 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
62 : wxPanel (parent, wxID_ANY)
63 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
64 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
67 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
68 , _reels_view (new TimelineReelsView (*this, 32))
69 , _labels_view (new TimelineLabelsView (*this))
72 , _down_view_position (0)
79 , _pixels_per_track (48)
80 , _first_resize (true)
83 _labels_canvas->SetDoubleBuffered (true);
84 _main_canvas->SetDoubleBuffered (true);
87 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
88 sizer->Add (_labels_canvas, 0, wxEXPAND);
89 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
90 sizer->Add (_main_canvas, 1, wxEXPAND);
93 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
94 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
95 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
96 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
97 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
98 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
99 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
100 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
101 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
102 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
103 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
104 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
105 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
106 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
108 film_change (CHANGE_TYPE_DONE, Film::CONTENT);
110 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
112 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
113 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
116 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
120 Timeline::set_pixels_per_second (double pps)
122 _pixels_per_second = max (_minimum_pixels_per_second, pps);
126 Timeline::paint_labels ()
128 wxPaintDC dc (_labels_canvas);
130 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
136 _labels_canvas->GetViewStart (&vsx, &vsy);
137 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
139 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
145 Timeline::paint_main ()
147 wxPaintDC dc (_main_canvas);
148 _main_canvas->DoPrepareDC (dc);
150 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
156 _main_canvas->GetViewStart (&vsx, &vsy);
157 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
159 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
161 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
163 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
165 /* Find areas of overlap with other content views, so that we can plot them */
166 list<dcpomatic::Rect<int> > overlaps;
167 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
168 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
169 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
170 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
174 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
176 overlaps.push_back (r.get ());
180 i->paint (gc, overlaps);
184 /* Translate back as _down_point and _zoom_point do not take scroll into account */
185 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
186 gc->SetPen (*wxBLACK_PEN);
187 gc->SetBrush (*wxTRANSPARENT_BRUSH);
189 min (_down_point.x, _zoom_point->x),
190 min (_down_point.y, _zoom_point->y),
191 fabs (_down_point.x - _zoom_point->x),
192 fabs (_down_point.y - _zoom_point->y)
200 Timeline::film_change (ChangeType type, Film::Property p)
202 if (type != CHANGE_TYPE_DONE) {
206 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
209 } else if (p == Film::CONTENT_ORDER) {
215 Timeline::recreate_views ()
217 shared_ptr<const Film> film = _film.lock ();
223 _views.push_back (_time_axis_view);
224 _views.push_back (_reels_view);
226 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
228 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
231 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
232 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
235 BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
236 _views.push_back (shared_ptr<TimelineView> (new TimelineTextContentView (*this, i, j)));
239 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
240 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
250 Timeline::film_content_change (ChangeType type, int property, bool frequent)
252 if (type != CHANGE_TYPE_DONE) {
258 if (property == AudioContentProperty::STREAMS) {
260 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
261 _reels_view->force_redraw ();
262 } else if (!frequent) {
270 place (TimelineViewList& views, int& tracks)
272 int const base = tracks;
274 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
275 if (!dynamic_pointer_cast<T>(i)) {
279 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
283 shared_ptr<Content> content = cv->content();
284 DCPTimePeriod const content_period (content->position(), content->end());
287 TimelineViewList::iterator j = views.begin();
288 while (j != views.end()) {
289 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
295 shared_ptr<Content> test_content = test->content();
297 test->track() && test->track().get() == t &&
298 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
299 /* we have an overlap on track `t' */
307 if (j == views.end ()) {
308 /* no overlap on `t' */
314 tracks = max (tracks, t + 1);
317 return tracks - base;
320 /** Compare the mapped output channels of two TimelineViews, so we can into
321 * order of first mapped DCP channel.
323 struct AudioMappingComparator {
324 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
326 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
328 list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
329 la = *min_element(boost::begin(oc), boost::end(oc));
332 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
334 list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
335 lb = *min_element(boost::begin(oc), boost::end(oc));
342 Timeline::assign_tracks ()
345 Video (mono or left-eye)
358 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
359 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
367 bool have_3d = false;
368 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
369 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
374 /* Video on tracks 0 and maybe 1 (left and right eye) */
375 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
377 _tracks = max (_tracks, 2);
384 _tracks = max (_tracks, 1);
388 int const text_tracks = place<TimelineTextContentView> (_views, _tracks);
392 bool have_atmos = false;
393 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
394 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
398 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
399 cv->set_track (_tracks - 1);
408 /* Audio. We're sorting the views so that we get the audio views in order of increasing
412 TimelineViewList views = _views;
413 sort(views.begin(), views.end(), AudioMappingComparator());
414 int const audio_tracks = place<TimelineAudioContentView> (views, _tracks);
416 _labels_view->set_3d (have_3d);
417 _labels_view->set_audio_tracks (audio_tracks);
418 _labels_view->set_text_tracks (text_tracks);
419 _labels_view->set_atmos (have_atmos);
421 _time_axis_view->set_y (tracks());
422 _reels_view->set_y (8);
426 Timeline::tracks () const
432 Timeline::setup_scrollbars ()
434 shared_ptr<const Film> film = _film.lock ();
435 if (!film || !_pixels_per_second) {
439 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
441 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
442 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
443 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
444 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
447 shared_ptr<TimelineView>
448 Timeline::event_to_view (wxMouseEvent& ev)
450 /* Search backwards through views so that we find the uppermost one first */
451 TimelineViewList::reverse_iterator i = _views.rbegin();
452 Position<int> const p (ev.GetX(), ev.GetY());
453 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
454 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
458 if (i == _views.rend ()) {
459 return shared_ptr<TimelineView> ();
466 Timeline::left_down (wxMouseEvent& ev)
469 _down_point = ev.GetPosition ();
473 left_down_select (ev);
485 Timeline::left_down_select (wxMouseEvent& ev)
487 shared_ptr<TimelineView> view = event_to_view (ev);
488 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
493 _down_view = content_view;
494 _down_view_position = content_view->content()->position ();
497 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
498 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
503 if (!ev.ShiftDown ()) {
504 cv->set_selected (view == *i);
508 if (content_view && ev.ShiftDown ()) {
509 content_view->set_selected (!content_view->selected ());
515 /* Pre-compute the points that we might snap to */
516 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
517 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
518 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
522 _start_snaps.push_back (cv->content()->position());
523 _end_snaps.push_back (cv->content()->position());
524 _start_snaps.push_back (cv->content()->end());
525 _end_snaps.push_back (cv->content()->end());
527 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
528 _start_snaps.push_back (i);
532 /* Tell everyone that things might change frequently during the drag */
533 _down_view->content()->set_change_signals_frequent (true);
538 Timeline::left_up (wxMouseEvent& ev)
557 Timeline::left_up_select (wxMouseEvent& ev)
560 _down_view->content()->set_change_signals_frequent (false);
563 _content_panel->set_selection (selected_content ());
564 set_position_from_event (ev);
566 /* Clear up up the stuff we don't do during drag */
571 _start_snaps.clear ();
576 Timeline::left_up_zoom (wxMouseEvent& ev)
578 _zoom_point = ev.GetPosition ();
581 _main_canvas->GetViewStart (&vsx, &vsy);
583 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
584 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
586 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
587 /* Very small zoom rectangle: we assume it wasn't intentional */
591 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
592 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
593 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
595 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
596 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
597 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
600 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
601 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
602 _labels_canvas->Scroll (0, y);
604 _zoom_point = optional<wxPoint> ();
609 Timeline::set_pixels_per_track (int h)
611 _pixels_per_track = max(_minimum_pixels_per_track, h);
615 Timeline::mouse_moved (wxMouseEvent& ev)
619 mouse_moved_select (ev);
622 mouse_moved_zoom (ev);
632 Timeline::mouse_moved_select (wxMouseEvent& ev)
638 set_position_from_event (ev);
642 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
648 _zoom_point = ev.GetPosition ();
653 Timeline::right_down (wxMouseEvent& ev)
657 right_down_select (ev);
661 set_pixels_per_second (*_pixels_per_second / 2);
662 set_pixels_per_track (_pixels_per_track / 2);
674 Timeline::right_down_select (wxMouseEvent& ev)
676 shared_ptr<TimelineView> view = event_to_view (ev);
677 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
682 if (!cv->selected ()) {
684 cv->set_selected (true);
687 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
691 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
693 DCPTime const d = a - b;
694 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
695 nearest_distance = d;
700 Timeline::set_position_from_event (wxMouseEvent& ev)
702 if (!_pixels_per_second) {
706 double const pps = _pixels_per_second.get ();
708 wxPoint const p = ev.GetPosition();
711 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
712 before the drag is considered to have started.
714 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
725 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
729 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
730 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
733 optional<DCPTime> nearest_distance;
735 /* Find the nearest snap point */
737 BOOST_FOREACH (DCPTime i, _start_snaps) {
738 maybe_snap (i, new_position, nearest_distance);
741 BOOST_FOREACH (DCPTime i, _end_snaps) {
742 maybe_snap (i, new_end, nearest_distance);
745 if (nearest_distance) {
746 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
747 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
748 new_position += nearest_distance.get ();
753 if (new_position < DCPTime ()) {
754 new_position = DCPTime ();
757 _down_view->content()->set_position (new_position);
759 shared_ptr<Film> film = _film.lock ();
760 DCPOMATIC_ASSERT (film);
761 film->set_sequence (false);
765 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
767 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
770 shared_ptr<const Film>
771 Timeline::film () const
773 return _film.lock ();
779 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
781 _first_resize = false;
787 Timeline::clear_selection ()
789 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
790 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
792 cv->set_selected (false);
797 TimelineContentViewList
798 Timeline::selected_views () const
800 TimelineContentViewList sel;
802 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
803 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
804 if (cv && cv->selected()) {
813 Timeline::selected_content () const
816 TimelineContentViewList views = selected_views ();
818 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
819 sel.push_back ((*i)->content ());
826 Timeline::set_selection (ContentList selection)
828 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
829 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
831 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
837 Timeline::tracks_y_offset () const
839 return _reels_view->bbox().height + 4;
843 Timeline::width () const
845 return _main_canvas->GetVirtualSize().GetWidth();
849 Timeline::scrolled (wxScrollWinEvent& ev)
851 if (ev.GetOrientation() == wxVERTICAL) {
853 _main_canvas->GetViewStart (&x, &y);
854 _labels_canvas->Scroll (0, y);
860 Timeline::tool_clicked (Tool t)
877 Timeline::zoom_all ()
879 shared_ptr<Film> film = _film.lock ();
880 DCPOMATIC_ASSERT (film);
881 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
882 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
884 _main_canvas->Scroll (0, 0);
885 _labels_canvas->Scroll (0, 0);