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/scope_guard.h"
39 #include "lib/text_content.h"
40 #include "lib/timer.h"
41 #include "lib/video_content.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_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
114 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
115 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
116 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
117 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
118 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
120 film_change(ChangeType::DONE, FilmProperty::CONTENT);
122 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
124 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
125 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
127 Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
128 _timer.Start (200, wxTIMER_CONTINUOUS);
131 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
136 Timeline::update_playhead ()
143 Timeline::set_pixels_per_second (double pps)
145 _pixels_per_second = max (_minimum_pixels_per_second, pps);
150 Timeline::paint_labels ()
152 wxPaintDC dc (_labels_canvas);
154 auto film = _film.lock();
155 if (film->content().empty()) {
159 auto gc = wxGraphicsContext::Create (dc);
164 ScopeGuard sg = [gc]() { delete gc; };
167 _labels_canvas->GetViewStart (&vsx, &vsy);
168 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
170 _labels_view->paint (gc, {});
175 Timeline::paint_main ()
177 wxPaintDC dc (_main_canvas);
180 auto film = _film.lock();
181 if (film->content().empty()) {
185 _main_canvas->DoPrepareDC (dc);
187 auto gc = wxGraphicsContext::Create (dc);
192 ScopeGuard sg = [gc]() { delete gc; };
194 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
196 for (auto i: _views) {
198 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
200 /* Find areas of overlap with other content views, so that we can plot them */
201 list<dcpomatic::Rect<int>> overlaps;
202 for (auto j: _views) {
203 auto jc = dynamic_pointer_cast<TimelineContentView> (j);
204 /* No overlap with non-content views, views on different tracks, audio views or non-active views */
205 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
209 auto r = j->bbox().intersection(i->bbox());
211 overlaps.push_back (r.get ());
215 i->paint (gc, overlaps);
219 gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
220 gc->SetBrush (*wxTRANSPARENT_BRUSH);
222 min (_down_point.x, _zoom_point->x),
223 min (_down_point.y, _zoom_point->y),
224 abs (_down_point.x - _zoom_point->x),
225 abs (_down_point.y - _zoom_point->y)
231 gc->SetPen (*wxRED_PEN);
232 auto path = gc->CreatePath ();
233 double const ph = _viewer.position().seconds() * pixels_per_second().get_value_or(0);
234 path.MoveToPoint (ph, 0);
235 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
236 gc->StrokePath (path);
241 Timeline::film_change(ChangeType type, FilmProperty p)
243 if (type != ChangeType::DONE) {
247 if (p == FilmProperty::CONTENT || p == FilmProperty::REEL_TYPE || p == FilmProperty::REEL_LENGTH) {
250 } else if (p == FilmProperty::CONTENT_ORDER) {
257 Timeline::recreate_views ()
259 auto film = _film.lock ();
265 _views.push_back (_time_axis_view);
266 _views.push_back (_reels_view);
268 for (auto i: film->content ()) {
270 _views.push_back (make_shared<TimelineVideoContentView>(*this, i));
273 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
274 _views.push_back (make_shared<TimelineAudioContentView>(*this, i));
277 for (auto j: i->text) {
278 _views.push_back (make_shared<TimelineTextContentView>(*this, i, j));
282 _views.push_back (make_shared<TimelineAtmosContentView>(*this, i));
293 Timeline::film_content_change (ChangeType type, int property, bool frequent)
295 if (type != ChangeType::DONE) {
301 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
303 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
304 _reels_view->force_redraw ();
305 } else if (!frequent) {
314 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
316 int const base = tracks;
318 for (auto i: views) {
319 if (!dynamic_pointer_cast<T>(i)) {
323 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
327 auto content = cv->content();
328 DCPTimePeriod const content_period = content->period(film);
331 auto j = views.begin();
332 while (j != views.end()) {
333 auto test = dynamic_pointer_cast<T> (*j);
339 auto test_content = test->content();
341 test->track() && test->track().get() == t &&
342 content_period.overlap(test_content->period(film))
344 /* we have an overlap on track `t' */
352 if (j == views.end ()) {
353 /* no overlap on `t' */
359 tracks = max (tracks, t + 1);
362 return tracks - base;
366 /** Compare the mapped output channels of two TimelineViews, so we can into
367 * order of first mapped DCP channel.
369 struct AudioMappingComparator {
370 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
372 auto cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
374 auto oc = cva->content()->audio->mapping().mapped_output_channels();
375 la = *min_element(boost::begin(oc), boost::end(oc));
378 auto cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
380 auto oc = cvb->content()->audio->mapping().mapped_output_channels();
381 lb = *min_element(boost::begin(oc), boost::end(oc));
389 Timeline::assign_tracks ()
404 auto film = _film.lock ();
405 DCPOMATIC_ASSERT (film);
409 for (auto i: _views) {
410 auto c = dynamic_pointer_cast<TimelineContentView>(i);
416 int const video_tracks = place<TimelineVideoContentView> (film, _views, _tracks);
417 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
421 bool have_atmos = false;
422 for (auto i: _views) {
423 auto cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
425 cv->set_track (_tracks);
434 /* Audio. We're sorting the views so that we get the audio views in order of increasing
439 sort(views.begin(), views.end(), AudioMappingComparator());
440 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
442 _labels_view->set_video_tracks (video_tracks);
443 _labels_view->set_audio_tracks (audio_tracks);
444 _labels_view->set_text_tracks (text_tracks);
445 _labels_view->set_atmos (have_atmos);
447 _time_axis_view->set_y (tracks());
448 _reels_view->set_y (8);
453 Timeline::tracks () const
460 Timeline::setup_scrollbars ()
462 auto film = _film.lock ();
463 if (!film || !_pixels_per_second) {
467 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
469 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
470 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
471 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
472 _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 auto i = _views.rbegin();
483 _main_canvas->GetViewStart (&vsx, &vsy);
484 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
486 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
490 if (i == _views.rend ()) {
499 Timeline::left_down (wxMouseEvent& ev)
502 _down_point = ev.GetPosition ();
506 left_down_select (ev);
519 Timeline::left_down_select (wxMouseEvent& ev)
521 auto view = event_to_view (ev);
522 auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
527 _down_view = content_view;
528 _down_view_position = content_view->content()->position ();
531 if (dynamic_pointer_cast<TimelineTimeAxisView>(view)) {
533 _main_canvas->GetViewStart(&vsx, &vsy);
534 _viewer.seek(DCPTime::from_seconds((ev.GetPosition().x + vsx * _x_scroll_rate) / _pixels_per_second.get_value_or(1)), true);
537 for (auto i: _views) {
538 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
543 if (!ev.ShiftDown ()) {
544 cv->set_selected (view == i);
548 if (content_view && ev.ShiftDown ()) {
549 content_view->set_selected (!content_view->selected ());
555 /* Pre-compute the points that we might snap to */
556 for (auto i: _views) {
557 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
558 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
562 auto film = _film.lock ();
563 DCPOMATIC_ASSERT (film);
565 _start_snaps.push_back (cv->content()->position());
566 _end_snaps.push_back (cv->content()->position());
567 _start_snaps.push_back (cv->content()->end(film));
568 _end_snaps.push_back (cv->content()->end(film));
570 for (auto i: cv->content()->reel_split_points(film)) {
571 _start_snaps.push_back (i);
575 /* Tell everyone that things might change frequently during the drag */
576 _down_view->content()->set_change_signals_frequent (true);
582 Timeline::left_up (wxMouseEvent& ev)
602 Timeline::left_up_select (wxMouseEvent& ev)
605 _down_view->content()->set_change_signals_frequent (false);
608 _content_panel->set_selection (selected_content ());
609 /* Since we may have just set change signals back to `not-frequent', we have to
610 make sure this position change is signalled, even if the position value has
611 not changed since the last time it was set (with frequent=true). This is
614 set_position_from_event (ev, true);
616 /* Clear up up the stuff we don't do during drag */
621 _start_snaps.clear ();
627 Timeline::left_up_zoom (wxMouseEvent& ev)
629 _zoom_point = ev.GetPosition ();
632 _main_canvas->GetViewStart (&vsx, &vsy);
634 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
635 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
637 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
638 /* Very small zoom rectangle: we assume it wasn't intentional */
639 _zoom_point = optional<wxPoint> ();
644 auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
645 auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
646 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
648 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
649 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
650 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
653 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
654 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
655 _labels_canvas->Scroll (0, y);
657 _zoom_point = optional<wxPoint> ();
663 Timeline::set_pixels_per_track (int h)
665 _pixels_per_track = max(_minimum_pixels_per_track, h);
670 Timeline::mouse_moved (wxMouseEvent& ev)
674 mouse_moved_select (ev);
677 mouse_moved_zoom (ev);
688 Timeline::mouse_moved_select (wxMouseEvent& ev)
694 set_position_from_event (ev);
699 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
705 _zoom_point = ev.GetPosition ();
712 Timeline::right_down (wxMouseEvent& ev)
716 right_down_select (ev);
720 set_pixels_per_second (*_pixels_per_second / 2);
721 set_pixels_per_track (_pixels_per_track / 2);
734 Timeline::right_down_select (wxMouseEvent& ev)
736 auto view = event_to_view (ev);
737 auto cv = dynamic_pointer_cast<TimelineContentView> (view);
742 if (!cv->selected ()) {
744 cv->set_selected (true);
747 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
752 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
754 auto const d = a - b;
755 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
756 nearest_distance = d;
762 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
764 if (!_pixels_per_second) {
768 double const pps = _pixels_per_second.get ();
770 auto const p = ev.GetPosition();
773 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
774 before the drag is considered to have started.
776 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
787 auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
789 auto film = _film.lock ();
790 DCPOMATIC_ASSERT (film);
793 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
794 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
797 optional<DCPTime> nearest_distance;
799 /* Find the nearest snap point */
801 for (auto i: _start_snaps) {
802 maybe_snap (i, new_position, nearest_distance);
805 for (auto i: _end_snaps) {
806 maybe_snap (i, new_end, nearest_distance);
809 if (nearest_distance) {
810 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
811 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
812 new_position += nearest_distance.get ();
817 if (new_position < DCPTime ()) {
818 new_position = DCPTime ();
821 _down_view->content()->set_position (film, new_position, force_emit);
823 film->set_sequence (false);
828 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
830 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
834 shared_ptr<const Film>
835 Timeline::film () const
837 return _film.lock ();
844 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
846 _first_resize = false;
853 Timeline::clear_selection ()
855 for (auto i: _views) {
856 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
858 cv->set_selected (false);
864 TimelineContentViewList
865 Timeline::selected_views () const
867 TimelineContentViewList sel;
869 for (auto i: _views) {
870 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
871 if (cv && cv->selected()) {
881 Timeline::selected_content () const
885 for (auto i: selected_views()) {
886 sel.push_back(i->content());
894 Timeline::set_selection (ContentList selection)
896 for (auto i: _views) {
897 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
899 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
906 Timeline::tracks_y_offset () const
908 return _reels_view->bbox().height + 4;
913 Timeline::width () const
915 return _main_canvas->GetVirtualSize().GetWidth();
920 Timeline::scrolled (wxScrollWinEvent& ev)
922 if (ev.GetOrientation() == wxVERTICAL) {
924 _main_canvas->GetViewStart (&x, &y);
925 _labels_canvas->Scroll (0, y);
932 Timeline::tool_clicked (Tool t)
950 Timeline::zoom_all ()
952 auto film = _film.lock ();
953 DCPOMATIC_ASSERT (film);
954 set_pixels_per_second((_main_canvas->GetSize().GetWidth() - 32) / std::max(1.0, film->length().seconds()));
955 set_pixels_per_track((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / std::max(1, _tracks));
957 _main_canvas->Scroll (0, 0);
958 _labels_canvas->Scroll (0, 0);
964 Timeline::keypress(wxKeyEvent const& event)
966 if (event.GetKeyCode() == WXK_DELETE) {
967 auto film = _film.lock();
968 DCPOMATIC_ASSERT(film);
969 film->remove_content(selected_content());
971 switch (event.GetRawKeyCode()) {
973 set_pixels_per_second(_pixels_per_second.get_value_or(1) * 2);
977 set_pixels_per_second(_pixels_per_second.get_value_or(1) / 2);