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/warnings.h>
42 LIBDCP_DISABLE_WARNINGS
43 #include <wx/graphics.h>
44 LIBDCP_ENABLE_WARNINGS
50 using std::dynamic_pointer_cast;
52 using std::make_shared;
55 using std::shared_ptr;
58 using boost::optional;
59 using namespace dcpomatic;
60 #if BOOST_VERSION >= 106100
61 using namespace boost::placeholders;
65 /* 3 hours in 640 pixels */
66 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
67 int const Timeline::_minimum_pixels_per_track = 16;
70 Timeline::Timeline(wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, FilmViewer& viewer)
71 : wxPanel (parent, wxID_ANY)
72 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
73 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
77 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
78 , _reels_view (new TimelineReelsView (*this, 32))
79 , _labels_view (new TimelineLabelsView (*this))
82 , _down_view_position (0)
84 , _menu (this, viewer)
89 , _pixels_per_track (48)
90 , _first_resize (true)
94 _labels_canvas->SetDoubleBuffered (true);
95 _main_canvas->SetDoubleBuffered (true);
98 auto sizer = new wxBoxSizer (wxHORIZONTAL);
99 sizer->Add (_labels_canvas, 0, wxEXPAND);
100 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
101 sizer->Add (_main_canvas, 1, wxEXPAND);
104 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
105 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
106 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
107 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
108 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
109 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
110 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
111 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
112 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
114 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
115 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
116 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
117 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
119 film_change (ChangeType::DONE, Film::Property::CONTENT);
121 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
123 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
124 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
126 Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
127 _timer.Start (200, wxTIMER_CONTINUOUS);
130 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
135 Timeline::update_playhead ()
142 Timeline::set_pixels_per_second (double pps)
144 _pixels_per_second = max (_minimum_pixels_per_second, pps);
149 Timeline::paint_labels ()
151 wxPaintDC dc (_labels_canvas);
153 auto gc = wxGraphicsContext::Create (dc);
159 _labels_canvas->GetViewStart (&vsx, &vsy);
160 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
162 _labels_view->paint (gc, {});
169 Timeline::paint_main ()
171 wxPaintDC dc (_main_canvas);
172 _main_canvas->DoPrepareDC (dc);
174 auto gc = wxGraphicsContext::Create (dc);
180 _main_canvas->GetViewStart (&vsx, &vsy);
181 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
183 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
185 for (auto i: _views) {
187 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
189 /* Find areas of overlap with other content views, so that we can plot them */
190 list<dcpomatic::Rect<int>> overlaps;
191 for (auto j: _views) {
192 auto jc = dynamic_pointer_cast<TimelineContentView> (j);
193 /* No overlap with non-content views, views on different tracks, audio views or non-active views */
194 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
198 auto r = j->bbox().intersection(i->bbox());
200 overlaps.push_back (r.get ());
204 i->paint (gc, overlaps);
208 /* Translate back as _down_point and _zoom_point do not take scroll into account */
209 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
210 gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
211 gc->SetBrush (*wxTRANSPARENT_BRUSH);
213 min (_down_point.x, _zoom_point->x),
214 min (_down_point.y, _zoom_point->y),
215 abs (_down_point.x - _zoom_point->x),
216 abs (_down_point.y - _zoom_point->y)
222 gc->SetPen (*wxRED_PEN);
223 auto path = gc->CreatePath ();
224 double const ph = _viewer.position().seconds() * pixels_per_second().get_value_or(0);
225 path.MoveToPoint (ph, 0);
226 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
227 gc->StrokePath (path);
234 Timeline::film_change (ChangeType type, Film::Property p)
236 if (type != ChangeType::DONE) {
240 if (p == Film::Property::CONTENT || p == Film::Property::REEL_TYPE || p == Film::Property::REEL_LENGTH) {
243 } else if (p == Film::Property::CONTENT_ORDER) {
250 Timeline::recreate_views ()
252 auto film = _film.lock ();
258 _views.push_back (_time_axis_view);
259 _views.push_back (_reels_view);
261 for (auto i: film->content ()) {
263 _views.push_back (make_shared<TimelineVideoContentView>(*this, i));
266 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
267 _views.push_back (make_shared<TimelineAudioContentView>(*this, i));
270 for (auto j: i->text) {
271 _views.push_back (make_shared<TimelineTextContentView>(*this, i, j));
275 _views.push_back (make_shared<TimelineAtmosContentView>(*this, i));
286 Timeline::film_content_change (ChangeType type, int property, bool frequent)
288 if (type != ChangeType::DONE) {
294 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
296 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
297 _reels_view->force_redraw ();
298 } else if (!frequent) {
307 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
309 int const base = tracks;
311 for (auto i: views) {
312 if (!dynamic_pointer_cast<T>(i)) {
316 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
320 auto content = cv->content();
321 DCPTimePeriod const content_period (content->position(), content->end(film));
324 auto j = views.begin();
325 while (j != views.end()) {
326 auto test = dynamic_pointer_cast<T> (*j);
332 auto test_content = test->content();
334 test->track() && test->track().get() == t &&
335 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
336 /* we have an overlap on track `t' */
344 if (j == views.end ()) {
345 /* no overlap on `t' */
351 tracks = max (tracks, t + 1);
354 return tracks - base;
358 /** Compare the mapped output channels of two TimelineViews, so we can into
359 * order of first mapped DCP channel.
361 struct AudioMappingComparator {
362 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
364 auto cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
366 auto oc = cva->content()->audio->mapping().mapped_output_channels();
367 la = *min_element(boost::begin(oc), boost::end(oc));
370 auto cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
372 auto oc = cvb->content()->audio->mapping().mapped_output_channels();
373 lb = *min_element(boost::begin(oc), boost::end(oc));
381 Timeline::assign_tracks ()
396 auto film = _film.lock ();
397 DCPOMATIC_ASSERT (film);
401 for (auto i: _views) {
402 auto c = dynamic_pointer_cast<TimelineContentView>(i);
408 int const video_tracks = place<TimelineVideoContentView> (film, _views, _tracks);
409 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
413 bool have_atmos = false;
414 for (auto i: _views) {
415 auto cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
417 cv->set_track (_tracks);
426 /* Audio. We're sorting the views so that we get the audio views in order of increasing
431 sort(views.begin(), views.end(), AudioMappingComparator());
432 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
434 _labels_view->set_video_tracks (video_tracks);
435 _labels_view->set_audio_tracks (audio_tracks);
436 _labels_view->set_text_tracks (text_tracks);
437 _labels_view->set_atmos (have_atmos);
439 _time_axis_view->set_y (tracks());
440 _reels_view->set_y (8);
445 Timeline::tracks () const
452 Timeline::setup_scrollbars ()
454 auto film = _film.lock ();
455 if (!film || !_pixels_per_second) {
459 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
461 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
462 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
463 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
464 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
468 shared_ptr<TimelineView>
469 Timeline::event_to_view (wxMouseEvent& ev)
471 /* Search backwards through views so that we find the uppermost one first */
472 auto i = _views.rbegin();
475 _main_canvas->GetViewStart (&vsx, &vsy);
476 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
478 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
479 auto cv = dynamic_pointer_cast<TimelineContentView>(*i);
483 if (i == _views.rend ()) {
492 Timeline::left_down (wxMouseEvent& ev)
495 _down_point = ev.GetPosition ();
499 left_down_select (ev);
512 Timeline::left_down_select (wxMouseEvent& ev)
514 auto view = event_to_view (ev);
515 auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
520 _down_view = content_view;
521 _down_view_position = content_view->content()->position ();
524 for (auto i: _views) {
525 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
530 if (!ev.ShiftDown ()) {
531 cv->set_selected (view == i);
535 if (content_view && ev.ShiftDown ()) {
536 content_view->set_selected (!content_view->selected ());
542 /* Pre-compute the points that we might snap to */
543 for (auto i: _views) {
544 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
545 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
549 auto film = _film.lock ();
550 DCPOMATIC_ASSERT (film);
552 _start_snaps.push_back (cv->content()->position());
553 _end_snaps.push_back (cv->content()->position());
554 _start_snaps.push_back (cv->content()->end(film));
555 _end_snaps.push_back (cv->content()->end(film));
557 for (auto i: cv->content()->reel_split_points(film)) {
558 _start_snaps.push_back (i);
562 /* Tell everyone that things might change frequently during the drag */
563 _down_view->content()->set_change_signals_frequent (true);
569 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 ();
614 Timeline::left_up_zoom (wxMouseEvent& ev)
616 _zoom_point = ev.GetPosition ();
619 _main_canvas->GetViewStart (&vsx, &vsy);
621 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
622 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
624 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
625 /* Very small zoom rectangle: we assume it wasn't intentional */
626 _zoom_point = optional<wxPoint> ();
631 auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
632 auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
633 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
635 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
636 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
637 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
640 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
641 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
642 _labels_canvas->Scroll (0, y);
644 _zoom_point = optional<wxPoint> ();
650 Timeline::set_pixels_per_track (int h)
652 _pixels_per_track = max(_minimum_pixels_per_track, h);
657 Timeline::mouse_moved (wxMouseEvent& ev)
661 mouse_moved_select (ev);
664 mouse_moved_zoom (ev);
675 Timeline::mouse_moved_select (wxMouseEvent& ev)
681 set_position_from_event (ev);
686 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
692 _zoom_point = ev.GetPosition ();
698 Timeline::right_down (wxMouseEvent& ev)
702 right_down_select (ev);
706 set_pixels_per_second (*_pixels_per_second / 2);
707 set_pixels_per_track (_pixels_per_track / 2);
720 Timeline::right_down_select (wxMouseEvent& ev)
722 auto view = event_to_view (ev);
723 auto cv = dynamic_pointer_cast<TimelineContentView> (view);
728 if (!cv->selected ()) {
730 cv->set_selected (true);
733 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
738 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
740 auto const d = a - b;
741 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
742 nearest_distance = d;
748 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
750 if (!_pixels_per_second) {
754 double const pps = _pixels_per_second.get ();
756 auto const p = ev.GetPosition();
759 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
760 before the drag is considered to have started.
762 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
773 auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
775 auto film = _film.lock ();
776 DCPOMATIC_ASSERT (film);
779 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
780 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
783 optional<DCPTime> nearest_distance;
785 /* Find the nearest snap point */
787 for (auto i: _start_snaps) {
788 maybe_snap (i, new_position, nearest_distance);
791 for (auto i: _end_snaps) {
792 maybe_snap (i, new_end, nearest_distance);
795 if (nearest_distance) {
796 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
797 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
798 new_position += nearest_distance.get ();
803 if (new_position < DCPTime ()) {
804 new_position = DCPTime ();
807 _down_view->content()->set_position (film, new_position, force_emit);
809 film->set_sequence (false);
814 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
816 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
820 shared_ptr<const Film>
821 Timeline::film () const
823 return _film.lock ();
830 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
832 _first_resize = false;
839 Timeline::clear_selection ()
841 for (auto i: _views) {
842 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
844 cv->set_selected (false);
850 TimelineContentViewList
851 Timeline::selected_views () const
853 TimelineContentViewList sel;
855 for (auto i: _views) {
856 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
857 if (cv && cv->selected()) {
867 Timeline::selected_content () const
871 for (auto i: selected_views()) {
872 sel.push_back(i->content());
880 Timeline::set_selection (ContentList selection)
882 for (auto i: _views) {
883 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
885 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
892 Timeline::tracks_y_offset () const
894 return _reels_view->bbox().height + 4;
899 Timeline::width () const
901 return _main_canvas->GetVirtualSize().GetWidth();
906 Timeline::scrolled (wxScrollWinEvent& ev)
908 if (ev.GetOrientation() == wxVERTICAL) {
910 _main_canvas->GetViewStart (&x, &y);
911 _labels_canvas->Scroll (0, y);
918 Timeline::tool_clicked (Tool t)
936 Timeline::zoom_all ()
938 auto film = _film.lock ();
939 DCPOMATIC_ASSERT (film);
940 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
941 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
943 _main_canvas->Scroll (0, 0);
944 _labels_canvas->Scroll (0, 0);