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 "film_editor.h"
23 #include "film_viewer.h"
25 #include "timeline_atmos_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_labels_view.h"
28 #include "timeline_reels_view.h"
29 #include "timeline_text_content_view.h"
30 #include "timeline_time_axis_view.h"
31 #include "timeline_video_content_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 /* 3 hours in 640 pixels */
67 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
68 int const Timeline::_minimum_pixels_per_track = 16;
71 Timeline::Timeline(wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, FilmViewer& viewer)
72 : wxPanel (parent, wxID_ANY)
73 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
74 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
78 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
79 , _reels_view (new TimelineReelsView (*this, 32))
80 , _labels_view (new TimelineLabelsView (*this))
83 , _down_view_position (0)
85 , _menu (this, viewer)
90 , _pixels_per_track (48)
91 , _first_resize (true)
95 _labels_canvas->SetDoubleBuffered (true);
96 _main_canvas->SetDoubleBuffered (true);
99 auto sizer = new wxBoxSizer (wxHORIZONTAL);
100 sizer->Add (_labels_canvas, 0, wxEXPAND);
101 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
102 sizer->Add (_main_canvas, 1, wxEXPAND);
105 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
106 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
107 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
108 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
109 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
110 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
111 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
112 _main_canvas->Bind (wxEVT_MOUSEWHEEL, boost::bind(&Timeline::mouse_wheel_turned, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
114 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
115 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
116 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
117 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
118 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
119 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
121 film_change(ChangeType::DONE, FilmProperty::CONTENT);
123 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
125 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
126 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
128 Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
129 _timer.Start (200, wxTIMER_CONTINUOUS);
132 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
137 Timeline::mouse_wheel_turned(wxMouseEvent& event)
139 if (event.ControlDown()) {
140 auto const rotation = event.GetWheelRotation();
141 /* On my mouse one click of the scroll wheel is 120, and it's -ve when
142 * scrolling the wheel towards me.
144 auto const scale = rotation > 0 ?
145 (1.0 / (rotation / 90.0)) :
150 _main_canvas->GetViewStart(&before_start_x, &before_start_y);
152 auto const before_pps = _pixels_per_second.get_value_or(1);
153 auto const before_pos = _last_mouse_wheel_x && *_last_mouse_wheel_x == event.GetX() ?
154 *_last_mouse_wheel_time :
155 (before_start_x * _x_scroll_rate + event.GetX()) / before_pps;
157 set_pixels_per_second(before_pps * scale);
160 auto after_left = std::max(0.0, before_pos * _pixels_per_second.get_value_or(1) - event.GetX());
161 _main_canvas->Scroll(after_left / _x_scroll_rate, before_start_y);
162 _labels_canvas->Scroll(0, before_start_y);
165 if (!_last_mouse_wheel_x || *_last_mouse_wheel_x != event.GetX()) {
166 _last_mouse_wheel_x = event.GetX();
167 _last_mouse_wheel_time = before_pos;
174 Timeline::update_playhead ()
181 Timeline::set_pixels_per_second (double pps)
183 _pixels_per_second = max (_minimum_pixels_per_second, pps);
188 Timeline::paint_labels ()
190 wxPaintDC dc (_labels_canvas);
192 auto film = _film.lock();
193 if (film->content().empty()) {
197 auto gc = wxGraphicsContext::Create (dc);
202 dcp::ScopeGuard sg = [gc]() { delete gc; };
205 _labels_canvas->GetViewStart (&vsx, &vsy);
206 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
208 _labels_view->paint (gc, {});
213 Timeline::paint_main ()
215 wxPaintDC dc (_main_canvas);
218 auto film = _film.lock();
219 if (film->content().empty()) {
223 _main_canvas->DoPrepareDC (dc);
225 auto gc = wxGraphicsContext::Create (dc);
230 dcp::ScopeGuard sg = [gc]() { delete gc; };
232 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
234 for (auto i: _views) {
236 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
238 /* Find areas of overlap with other content views, so that we can plot them */
239 list<dcpomatic::Rect<int>> overlaps;
240 for (auto j: _views) {
241 auto jc = dynamic_pointer_cast<TimelineContentView> (j);
242 /* No overlap with non-content views, views on different tracks, audio views or non-active views */
243 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
247 auto r = j->bbox().intersection(i->bbox());
249 overlaps.push_back (r.get ());
253 i->paint (gc, overlaps);
257 gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
258 gc->SetBrush (*wxTRANSPARENT_BRUSH);
260 min (_down_point.x, _zoom_point->x),
261 min (_down_point.y, _zoom_point->y),
262 abs (_down_point.x - _zoom_point->x),
263 abs (_down_point.y - _zoom_point->y)
269 gc->SetPen (*wxRED_PEN);
270 auto path = gc->CreatePath ();
271 double const ph = _viewer.position().seconds() * pixels_per_second().get_value_or(0);
272 path.MoveToPoint (ph, 0);
273 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
274 gc->StrokePath (path);
279 Timeline::film_change(ChangeType type, FilmProperty p)
281 if (type != ChangeType::DONE) {
285 if (p == FilmProperty::CONTENT || p == FilmProperty::REEL_TYPE || p == FilmProperty::REEL_LENGTH) {
288 } else if (p == FilmProperty::CONTENT_ORDER) {
295 Timeline::recreate_views ()
297 auto film = _film.lock ();
303 _views.push_back (_time_axis_view);
304 _views.push_back (_reels_view);
306 for (auto i: film->content ()) {
308 _views.push_back (make_shared<TimelineVideoContentView>(*this, i));
311 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
312 _views.push_back (make_shared<TimelineAudioContentView>(*this, i));
315 for (auto j: i->text) {
316 _views.push_back (make_shared<TimelineTextContentView>(*this, i, j));
320 _views.push_back (make_shared<TimelineAtmosContentView>(*this, i));
331 Timeline::film_content_change (ChangeType type, int property, bool frequent)
333 if (type != ChangeType::DONE) {
339 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
341 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
342 _reels_view->force_redraw ();
343 } else if (!frequent) {
352 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
354 int const base = tracks;
356 for (auto i: views) {
357 if (!dynamic_pointer_cast<T>(i)) {
361 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
362 DCPOMATIC_ASSERT(cv);
366 auto content = cv->content();
367 DCPTimePeriod const content_period = content->period(film);
370 auto j = views.begin();
371 while (j != views.end()) {
372 auto test = dynamic_pointer_cast<T> (*j);
378 auto test_content = test->content();
380 test->track() && test->track().get() == t &&
381 content_period.overlap(test_content->period(film))
383 /* we have an overlap on track `t' */
391 if (j == views.end ()) {
392 /* no overlap on `t' */
398 tracks = max (tracks, t + 1);
401 return tracks - base;
405 /** Compare the mapped output channels of two TimelineViews, so we can into
406 * order of first mapped DCP channel.
408 struct AudioMappingComparator {
409 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
411 auto cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
413 auto oc = cva->content()->audio->mapping().mapped_output_channels();
414 la = *min_element(boost::begin(oc), boost::end(oc));
417 auto cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
419 auto oc = cvb->content()->audio->mapping().mapped_output_channels();
420 lb = *min_element(boost::begin(oc), boost::end(oc));
428 Timeline::assign_tracks ()
443 auto film = _film.lock ();
444 DCPOMATIC_ASSERT (film);
448 for (auto i: _views) {
449 auto c = dynamic_pointer_cast<TimelineContentView>(i);
455 int const video_tracks = place<TimelineVideoContentView> (film, _views, _tracks);
456 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
460 bool have_atmos = false;
461 for (auto i: _views) {
462 auto cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
464 cv->set_track (_tracks);
473 /* Audio. We're sorting the views so that we get the audio views in order of increasing
478 sort(views.begin(), views.end(), AudioMappingComparator());
479 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
481 _labels_view->set_video_tracks (video_tracks);
482 _labels_view->set_audio_tracks (audio_tracks);
483 _labels_view->set_text_tracks (text_tracks);
484 _labels_view->set_atmos (have_atmos);
486 _time_axis_view->set_y (tracks());
487 _reels_view->set_y (8);
492 Timeline::tracks () const
499 Timeline::setup_scrollbars ()
501 auto film = _film.lock ();
502 if (!film || !_pixels_per_second) {
506 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
508 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
509 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
510 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
511 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
515 shared_ptr<TimelineView>
516 Timeline::event_to_view (wxMouseEvent& ev)
518 /* Search backwards through views so that we find the uppermost one first */
519 auto i = _views.rbegin();
522 _main_canvas->GetViewStart (&vsx, &vsy);
523 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
525 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
529 if (i == _views.rend ()) {
538 Timeline::left_down (wxMouseEvent& ev)
541 _down_point = ev.GetPosition ();
545 left_down_select (ev);
558 Timeline::left_down_select (wxMouseEvent& ev)
560 auto view = event_to_view (ev);
561 auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
566 _down_view = content_view;
567 _down_view_position = content_view->content()->position ();
570 if (dynamic_pointer_cast<TimelineTimeAxisView>(view)) {
572 _main_canvas->GetViewStart(&vsx, &vsy);
573 _viewer.seek(DCPTime::from_seconds((ev.GetPosition().x + vsx * _x_scroll_rate) / _pixels_per_second.get_value_or(1)), true);
576 for (auto i: _views) {
577 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
582 if (!ev.ShiftDown ()) {
583 cv->set_selected (view == i);
587 if (content_view && ev.ShiftDown ()) {
588 content_view->set_selected (!content_view->selected ());
594 /* Pre-compute the points that we might snap to */
595 for (auto i: _views) {
596 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
597 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
601 auto film = _film.lock ();
602 DCPOMATIC_ASSERT (film);
604 _start_snaps.push_back (cv->content()->position());
605 _end_snaps.push_back (cv->content()->position());
606 _start_snaps.push_back (cv->content()->end(film));
607 _end_snaps.push_back (cv->content()->end(film));
609 for (auto i: cv->content()->reel_split_points(film)) {
610 _start_snaps.push_back (i);
614 /* Tell everyone that things might change frequently during the drag */
615 _down_view->content()->set_change_signals_frequent (true);
621 Timeline::left_up (wxMouseEvent& ev)
641 Timeline::left_up_select (wxMouseEvent& ev)
644 _down_view->content()->set_change_signals_frequent (false);
647 _content_panel->set_selection (selected_content ());
648 /* Since we may have just set change signals back to `not-frequent', we have to
649 make sure this position change is signalled, even if the position value has
650 not changed since the last time it was set (with frequent=true). This is
653 set_position_from_event (ev, true);
655 /* Clear up up the stuff we don't do during drag */
660 _start_snaps.clear ();
666 Timeline::left_up_zoom (wxMouseEvent& ev)
668 _zoom_point = ev.GetPosition ();
671 _main_canvas->GetViewStart (&vsx, &vsy);
673 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
674 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
676 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
677 /* Very small zoom rectangle: we assume it wasn't intentional */
678 _zoom_point = optional<wxPoint> ();
683 auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
684 auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
685 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
687 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
688 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
689 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
692 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
693 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
694 _labels_canvas->Scroll (0, y);
696 _zoom_point = optional<wxPoint> ();
702 Timeline::set_pixels_per_track (int h)
704 _pixels_per_track = max(_minimum_pixels_per_track, h);
709 Timeline::mouse_moved (wxMouseEvent& ev)
713 mouse_moved_select (ev);
716 mouse_moved_zoom (ev);
727 Timeline::mouse_moved_select (wxMouseEvent& ev)
733 set_position_from_event (ev);
738 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
744 _zoom_point = ev.GetPosition ();
751 Timeline::right_down (wxMouseEvent& ev)
755 right_down_select (ev);
759 set_pixels_per_second (*_pixels_per_second / 2);
760 set_pixels_per_track (_pixels_per_track / 2);
773 Timeline::right_down_select (wxMouseEvent& ev)
775 auto view = event_to_view (ev);
776 auto cv = dynamic_pointer_cast<TimelineContentView> (view);
781 if (!cv->selected ()) {
783 cv->set_selected (true);
786 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
791 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
793 auto const d = a - b;
794 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
795 nearest_distance = d;
801 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
803 if (!_pixels_per_second) {
807 double const pps = _pixels_per_second.get ();
809 auto const p = ev.GetPosition();
812 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
813 before the drag is considered to have started.
815 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
826 auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
828 auto film = _film.lock ();
829 DCPOMATIC_ASSERT (film);
832 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
833 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
836 optional<DCPTime> nearest_distance;
838 /* Find the nearest snap point */
840 for (auto i: _start_snaps) {
841 maybe_snap (i, new_position, nearest_distance);
844 for (auto i: _end_snaps) {
845 maybe_snap (i, new_end, nearest_distance);
848 if (nearest_distance) {
849 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
850 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
851 new_position += nearest_distance.get ();
856 if (new_position < DCPTime ()) {
857 new_position = DCPTime ();
860 _down_view->content()->set_position (film, new_position, force_emit);
862 film->set_sequence (false);
867 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
869 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
873 shared_ptr<const Film>
874 Timeline::film () const
876 return _film.lock ();
883 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
885 _first_resize = false;
892 Timeline::clear_selection ()
894 for (auto i: _views) {
895 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
897 cv->set_selected (false);
903 TimelineContentViewList
904 Timeline::selected_views () const
906 TimelineContentViewList sel;
908 for (auto i: _views) {
909 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
910 if (cv && cv->selected()) {
920 Timeline::selected_content () const
924 for (auto i: selected_views()) {
925 sel.push_back(i->content());
933 Timeline::set_selection (ContentList selection)
935 for (auto i: _views) {
936 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
938 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
945 Timeline::tracks_y_offset () const
947 return _reels_view->bbox().height + 4;
952 Timeline::width () const
954 return _main_canvas->GetVirtualSize().GetWidth();
959 Timeline::scrolled (wxScrollWinEvent& ev)
961 if (ev.GetOrientation() == wxVERTICAL) {
963 _main_canvas->GetViewStart (&x, &y);
964 _labels_canvas->Scroll (0, y);
971 Timeline::tool_clicked (Tool t)
989 Timeline::zoom_all ()
991 auto film = _film.lock ();
992 DCPOMATIC_ASSERT (film);
993 set_pixels_per_second((_main_canvas->GetSize().GetWidth() - 32) / std::max(1.0, film->length().seconds()));
994 set_pixels_per_track((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / std::max(1, _tracks));
996 _main_canvas->Scroll (0, 0);
997 _labels_canvas->Scroll (0, 0);
1003 Timeline::keypress(wxKeyEvent const& event)
1005 if (event.GetKeyCode() == WXK_DELETE) {
1006 auto film = _film.lock();
1007 DCPOMATIC_ASSERT(film);
1008 film->remove_content(selected_content());
1010 switch (event.GetRawKeyCode()) {
1012 set_pixels_per_second(_pixels_per_second.get_value_or(1) * 2);
1016 set_pixels_per_second(_pixels_per_second.get_value_or(1) / 2);