2 Copyright (C) 2013-2021 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 "content_panel.h"
22 #include "content_timeline.h"
23 #include "film_editor.h"
24 #include "film_viewer.h"
25 #include "content_timeline_atmos_view.h"
26 #include "content_timeline_audio_view.h"
27 #include "content_timeline_text_view.h"
28 #include "content_timeline_video_view.h"
29 #include "timeline_labels_view.h"
30 #include "timeline_reels_view.h"
31 #include "timeline_time_axis_view.h"
33 #include "lib/atmos_mxf_content.h"
34 #include "lib/audio_content.h"
36 #include "lib/image_content.h"
37 #include "lib/playlist.h"
38 #include "lib/text_content.h"
39 #include "lib/timer.h"
40 #include "lib/video_content.h"
41 #include <dcp/scope_guard.h>
42 #include <dcp/warnings.h>
43 LIBDCP_DISABLE_WARNINGS
44 #include <wx/graphics.h>
45 LIBDCP_ENABLE_WARNINGS
51 using std::dynamic_pointer_cast;
53 using std::make_shared;
56 using std::shared_ptr;
59 using boost::optional;
60 using namespace dcpomatic;
61 #if BOOST_VERSION >= 106100
62 using namespace boost::placeholders;
66 int const ContentTimeline::_minimum_pixels_per_track = 16;
69 ContentTimeline::ContentTimeline(wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, FilmViewer& viewer)
71 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
72 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
76 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
77 , _reels_view (new TimelineReelsView (*this, 32))
78 , _labels_view (new TimelineLabelsView (*this))
81 , _down_view_position (0)
83 , _menu (this, viewer)
88 , _pixels_per_track (48)
89 , _first_resize (true)
93 _labels_canvas->SetDoubleBuffered (true);
94 _main_canvas->SetDoubleBuffered (true);
97 auto sizer = new wxBoxSizer (wxHORIZONTAL);
98 sizer->Add (_labels_canvas, 0, wxEXPAND);
99 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
100 sizer->Add (_main_canvas, 1, wxEXPAND);
103 _labels_canvas->Bind(wxEVT_PAINT, boost::bind(&ContentTimeline::paint_labels, this));
104 _main_canvas->Bind (wxEVT_PAINT, boost::bind(&ContentTimeline::paint_main, this));
105 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind(&ContentTimeline::left_down, this, _1));
106 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind(&ContentTimeline::left_up, this, _1));
107 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind(&ContentTimeline::right_down, this, _1));
108 _main_canvas->Bind (wxEVT_MOTION, boost::bind(&ContentTimeline::mouse_moved, this, _1));
109 _main_canvas->Bind (wxEVT_SIZE, boost::bind(&ContentTimeline::resized, this));
110 _main_canvas->Bind (wxEVT_MOUSEWHEEL, boost::bind(&ContentTimeline::mouse_wheel_turned, this, _1));
111 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind(&ContentTimeline::scrolled, this, _1));
112 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind(&ContentTimeline::scrolled, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind(&ContentTimeline::scrolled, this, _1));
114 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind(&ContentTimeline::scrolled, this, _1));
115 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind(&ContentTimeline::scrolled, this, _1));
116 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind(&ContentTimeline::scrolled, this, _1));
117 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind(&ContentTimeline::scrolled, this, _1));
119 film_change(ChangeType::DONE, FilmProperty::CONTENT);
121 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
123 _film_changed_connection = film->Change.connect(bind(&ContentTimeline::film_change, this, _1, _2));
124 _film_content_change_connection = film->ContentChange.connect(bind(&ContentTimeline::film_content_change, this, _1, _3, _4));
126 Bind(wxEVT_TIMER, boost::bind(&ContentTimeline::update_playhead, this));
127 _timer.Start (200, wxTIMER_CONTINUOUS);
130 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
135 ContentTimeline::mouse_wheel_turned(wxMouseEvent& event)
137 auto const rotation = event.GetWheelRotation();
139 if (event.ControlDown()) {
140 /* On my mouse one click of the scroll wheel is 120, and it's -ve when
141 * scrolling the wheel towards me.
143 auto const scale = rotation > 0 ?
144 (1.0 / (rotation / 90.0)) :
149 _main_canvas->GetViewStart(&before_start_x, &before_start_y);
151 auto const before_pps = _pixels_per_second.get_value_or(1);
152 auto const before_pos = _last_mouse_wheel_x && *_last_mouse_wheel_x == event.GetX() ?
153 *_last_mouse_wheel_time :
154 (before_start_x * _x_scroll_rate + event.GetX()) / before_pps;
156 set_pixels_per_second(before_pps * scale);
159 auto after_left = std::max(0.0, before_pos * _pixels_per_second.get_value_or(1) - event.GetX());
160 _main_canvas->Scroll(after_left / _x_scroll_rate, before_start_y);
161 _labels_canvas->Scroll(0, before_start_y);
164 if (!_last_mouse_wheel_x || *_last_mouse_wheel_x != event.GetX()) {
165 _last_mouse_wheel_x = event.GetX();
166 _last_mouse_wheel_time = before_pos;
168 } else if (event.ShiftDown()) {
171 _main_canvas->GetViewStart(&before_start_x, &before_start_y);
172 auto const width = _main_canvas->GetSize().GetWidth();
173 _main_canvas->Scroll(std::max(0.0, before_start_x - rotation * 100.0 / width), before_start_y);
179 ContentTimeline::update_playhead()
186 ContentTimeline::paint_labels()
188 wxPaintDC dc (_labels_canvas);
190 auto film = _film.lock();
191 if (film->content().empty()) {
195 auto gc = wxGraphicsContext::Create (dc);
200 dcp::ScopeGuard sg = [gc]() { delete gc; };
203 _labels_canvas->GetViewStart (&vsx, &vsy);
204 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
206 _labels_view->paint (gc, {});
211 ContentTimeline::paint_main()
213 wxPaintDC dc (_main_canvas);
216 auto film = _film.lock();
217 if (film->content().empty()) {
221 _main_canvas->DoPrepareDC (dc);
223 auto gc = wxGraphicsContext::Create (dc);
228 dcp::ScopeGuard sg = [gc]() { delete gc; };
230 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
232 for (auto i: _views) {
234 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
236 /* Find areas of overlap with other content views, so that we can plot them */
237 list<dcpomatic::Rect<int>> overlaps;
238 for (auto j: _views) {
239 auto jc = dynamic_pointer_cast<TimelineContentView> (j);
240 /* No overlap with non-content views, views on different tracks, audio views or non-active views */
241 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
245 auto r = j->bbox().intersection(i->bbox());
247 overlaps.push_back (r.get ());
251 i->paint (gc, overlaps);
255 gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
256 gc->SetBrush (*wxTRANSPARENT_BRUSH);
258 min (_down_point.x, _zoom_point->x),
259 min (_down_point.y, _zoom_point->y),
260 abs (_down_point.x - _zoom_point->x),
261 abs (_down_point.y - _zoom_point->y)
267 gc->SetPen (*wxRED_PEN);
268 auto path = gc->CreatePath ();
269 double const ph = _viewer.position().seconds() * pixels_per_second().get_value_or(0);
270 path.MoveToPoint (ph, 0);
271 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
272 gc->StrokePath (path);
277 ContentTimeline::film_change(ChangeType type, FilmProperty p)
279 if (type != ChangeType::DONE) {
283 if (p == FilmProperty::CONTENT || p == FilmProperty::REEL_TYPE || p == FilmProperty::REEL_LENGTH) {
286 } else if (p == FilmProperty::CONTENT_ORDER) {
293 ContentTimeline::recreate_views()
295 auto film = _film.lock ();
301 _views.push_back (_time_axis_view);
302 _views.push_back (_reels_view);
304 for (auto i: film->content ()) {
306 _views.push_back(make_shared<ContentTimelineVideoView>(*this, i));
309 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
310 _views.push_back(make_shared<ContentTimelineAudioView>(*this, i));
313 for (auto j: i->text) {
314 _views.push_back(make_shared<ContentTimelineTextView>(*this, i, j));
318 _views.push_back(make_shared<ContentTimelineAtmosView>(*this, i));
329 ContentTimeline::film_content_change(ChangeType type, int property, bool frequent)
331 if (type != ChangeType::DONE) {
337 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
339 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
340 _reels_view->force_redraw ();
341 } else if (!frequent) {
350 place(shared_ptr<const Film> film, ContentTimelineViewList& views, int& tracks)
352 int const base = tracks;
354 for (auto i: views) {
355 if (!dynamic_pointer_cast<T>(i)) {
359 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
360 DCPOMATIC_ASSERT(cv);
364 auto content = cv->content();
365 DCPTimePeriod const content_period = content->period(film);
368 auto j = views.begin();
369 while (j != views.end()) {
370 auto test = dynamic_pointer_cast<T> (*j);
376 auto test_content = test->content();
378 test->track() && test->track().get() == t &&
379 content_period.overlap(test_content->period(film))
381 /* we have an overlap on track `t' */
389 if (j == views.end ()) {
390 /* no overlap on `t' */
396 tracks = max (tracks, t + 1);
399 return tracks - base;
403 /** Compare the mapped output channels of two TimelineViews, so we can into
404 * order of first mapped DCP channel.
406 struct AudioMappingComparator {
407 bool operator()(shared_ptr<ContentTimelineView> a, shared_ptr<ContentTimelineView> b) {
409 auto cva = dynamic_pointer_cast<ContentTimelineAudioView>(a);
411 auto oc = cva->content()->audio->mapping().mapped_output_channels();
412 la = *min_element(boost::begin(oc), boost::end(oc));
415 auto cvb = dynamic_pointer_cast<ContentTimelineAudioView>(b);
417 auto oc = cvb->content()->audio->mapping().mapped_output_channels();
418 lb = *min_element(boost::begin(oc), boost::end(oc));
426 ContentTimeline::assign_tracks()
441 auto film = _film.lock ();
442 DCPOMATIC_ASSERT (film);
446 for (auto i: _views) {
447 auto c = dynamic_pointer_cast<TimelineContentView>(i);
453 int const video_tracks = place<ContentTimelineVideoView>(film, _views, _tracks);
454 int const text_tracks = place<ContentTimelineTextView>(film, _views, _tracks);
458 bool have_atmos = false;
459 for (auto i: _views) {
460 auto cv = dynamic_pointer_cast<ContentTimelineAtmosView>(i);
462 cv->set_track (_tracks);
471 /* Audio. We're sorting the views so that we get the audio views in order of increasing
476 sort(views.begin(), views.end(), AudioMappingComparator());
477 int const audio_tracks = place<ContentTimelineAudioView>(film, views, _tracks);
479 _labels_view->set_video_tracks (video_tracks);
480 _labels_view->set_audio_tracks (audio_tracks);
481 _labels_view->set_text_tracks (text_tracks);
482 _labels_view->set_atmos (have_atmos);
484 _time_axis_view->set_y (tracks());
485 _reels_view->set_y (8);
490 ContentTimeline::tracks() const
497 ContentTimeline::setup_scrollbars()
499 auto film = _film.lock ();
500 if (!film || !_pixels_per_second) {
504 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
506 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
507 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
508 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
509 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
513 shared_ptr<ContentTimelineView>
514 ContentTimeline::event_to_view(wxMouseEvent& ev)
516 /* Search backwards through views so that we find the uppermost one first */
517 auto i = _views.rbegin();
520 _main_canvas->GetViewStart (&vsx, &vsy);
521 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
523 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
527 if (i == _views.rend ()) {
536 ContentTimeline::left_down(wxMouseEvent& ev)
539 _down_point = ev.GetPosition ();
543 left_down_select (ev);
556 ContentTimeline::left_down_select(wxMouseEvent& ev)
558 auto view = event_to_view (ev);
559 auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
564 _down_view = content_view;
565 _down_view_position = content_view->content()->position ();
568 if (dynamic_pointer_cast<TimelineTimeAxisView>(view)) {
570 _main_canvas->GetViewStart(&vsx, &vsy);
571 _viewer.seek(DCPTime::from_seconds((ev.GetPosition().x + vsx * _x_scroll_rate) / _pixels_per_second.get_value_or(1)), true);
574 for (auto i: _views) {
575 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
580 if (!ev.ShiftDown ()) {
581 cv->set_selected (view == i);
585 if (content_view && ev.ShiftDown ()) {
586 content_view->set_selected (!content_view->selected ());
592 /* Pre-compute the points that we might snap to */
593 for (auto i: _views) {
594 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
595 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
599 auto film = _film.lock ();
600 DCPOMATIC_ASSERT (film);
602 _start_snaps.push_back (cv->content()->position());
603 _end_snaps.push_back (cv->content()->position());
604 _start_snaps.push_back (cv->content()->end(film));
605 _end_snaps.push_back (cv->content()->end(film));
607 for (auto i: cv->content()->reel_split_points(film)) {
608 _start_snaps.push_back (i);
612 /* Tell everyone that things might change frequently during the drag */
613 _down_view->content()->set_change_signals_frequent (true);
619 ContentTimeline::left_up(wxMouseEvent& ev)
639 ContentTimeline::left_up_select(wxMouseEvent& ev)
642 _down_view->content()->set_change_signals_frequent (false);
645 _content_panel->set_selection (selected_content ());
646 /* Since we may have just set change signals back to `not-frequent', we have to
647 make sure this position change is signalled, even if the position value has
648 not changed since the last time it was set (with frequent=true). This is
651 set_position_from_event (ev, true);
653 /* Clear up up the stuff we don't do during drag */
658 _start_snaps.clear ();
664 ContentTimeline::left_up_zoom(wxMouseEvent& ev)
666 _zoom_point = ev.GetPosition ();
669 _main_canvas->GetViewStart (&vsx, &vsy);
671 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
672 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
674 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
675 /* Very small zoom rectangle: we assume it wasn't intentional */
676 _zoom_point = optional<wxPoint> ();
681 auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
682 auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
683 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
685 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
686 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
687 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
690 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
691 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
692 _labels_canvas->Scroll (0, y);
694 _zoom_point = optional<wxPoint> ();
700 ContentTimeline::set_pixels_per_track(int h)
702 _pixels_per_track = max(_minimum_pixels_per_track, h);
707 ContentTimeline::mouse_moved(wxMouseEvent& ev)
711 mouse_moved_select (ev);
714 mouse_moved_zoom (ev);
725 ContentTimeline::mouse_moved_select(wxMouseEvent& ev)
731 set_position_from_event (ev);
736 ContentTimeline::mouse_moved_zoom(wxMouseEvent& ev)
742 _zoom_point = ev.GetPosition ();
749 ContentTimeline::right_down(wxMouseEvent& ev)
753 right_down_select (ev);
757 set_pixels_per_second (*_pixels_per_second / 2);
758 set_pixels_per_track (_pixels_per_track / 2);
771 ContentTimeline::right_down_select(wxMouseEvent& ev)
773 auto view = event_to_view (ev);
774 auto cv = dynamic_pointer_cast<TimelineContentView> (view);
779 if (!cv->selected ()) {
781 cv->set_selected (true);
784 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
789 ContentTimeline::maybe_snap(DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
791 auto const d = a - b;
792 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
793 nearest_distance = d;
799 ContentTimeline::set_position_from_event(wxMouseEvent& ev, bool force_emit)
801 if (!_pixels_per_second) {
805 double const pps = _pixels_per_second.get ();
807 auto const p = ev.GetPosition();
810 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
811 before the drag is considered to have started.
813 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
824 auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
826 auto film = _film.lock ();
827 DCPOMATIC_ASSERT (film);
830 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
831 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
834 optional<DCPTime> nearest_distance;
836 /* Find the nearest snap point */
838 for (auto i: _start_snaps) {
839 maybe_snap (i, new_position, nearest_distance);
842 for (auto i: _end_snaps) {
843 maybe_snap (i, new_end, nearest_distance);
846 if (nearest_distance) {
847 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
848 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
849 new_position += nearest_distance.get ();
854 if (new_position < DCPTime ()) {
855 new_position = DCPTime ();
858 _down_view->content()->set_position (film, new_position, force_emit);
860 film->set_sequence (false);
865 ContentTimeline::force_redraw(dcpomatic::Rect<int> const & r)
867 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
871 shared_ptr<const Film>
872 ContentTimeline::film() const
874 return _film.lock ();
879 ContentTimeline::resized()
881 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
883 _first_resize = false;
890 ContentTimeline::clear_selection()
892 for (auto i: _views) {
893 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
895 cv->set_selected (false);
901 TimelineContentViewList
902 ContentTimeline::selected_views() const
904 TimelineContentViewList sel;
906 for (auto i: _views) {
907 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
908 if (cv && cv->selected()) {
918 ContentTimeline::selected_content() const
922 for (auto i: selected_views()) {
923 sel.push_back(i->content());
931 ContentTimeline::set_selection(ContentList selection)
933 for (auto i: _views) {
934 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
936 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
943 ContentTimeline::tracks_y_offset() const
945 return _reels_view->bbox().height + 4;
950 ContentTimeline::width() const
952 return _main_canvas->GetVirtualSize().GetWidth();
957 ContentTimeline::scrolled(wxScrollWinEvent& ev)
959 if (ev.GetOrientation() == wxVERTICAL) {
961 _main_canvas->GetViewStart (&x, &y);
962 _labels_canvas->Scroll (0, y);
969 ContentTimeline::tool_clicked(Tool t)
987 ContentTimeline::zoom_all()
989 auto film = _film.lock ();
990 DCPOMATIC_ASSERT (film);
991 set_pixels_per_second((_main_canvas->GetSize().GetWidth() - 32) / std::max(1.0, film->length().seconds()));
992 set_pixels_per_track((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / std::max(1, _tracks));
994 _main_canvas->Scroll (0, 0);
995 _labels_canvas->Scroll (0, 0);
1001 ContentTimeline::keypress(wxKeyEvent const& event)
1003 if (event.GetKeyCode() == WXK_DELETE) {
1004 auto film = _film.lock();
1005 DCPOMATIC_ASSERT(film);
1006 film->remove_content(selected_content());
1008 switch (event.GetRawKeyCode()) {
1010 set_pixels_per_second(_pixels_per_second.get_value_or(1) * 2);
1014 set_pixels_per_second(_pixels_per_second.get_value_or(1) / 2);