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 "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"
32 #include "film_viewer.h"
34 #include "lib/playlist.h"
35 #include "lib/image_content.h"
36 #include "lib/timer.h"
37 #include "lib/audio_content.h"
38 #include "lib/text_content.h"
39 #include "lib/video_content.h"
40 #include "lib/atmos_mxf_content.h"
41 #include <wx/graphics.h>
51 using std::shared_ptr;
53 using std::dynamic_pointer_cast;
54 using std::make_shared;
56 using boost::optional;
57 using namespace dcpomatic;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
62 /* 3 hours in 640 pixels */
63 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
64 int const Timeline::_minimum_pixels_per_track = 16;
66 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
67 : wxPanel (parent, wxID_ANY)
68 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
69 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
73 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
74 , _reels_view (new TimelineReelsView (*this, 32))
75 , _labels_view (new TimelineLabelsView (*this))
78 , _down_view_position (0)
85 , _pixels_per_track (48)
86 , _first_resize (true)
90 _labels_canvas->SetDoubleBuffered (true);
91 _main_canvas->SetDoubleBuffered (true);
94 auto sizer = new wxBoxSizer (wxHORIZONTAL);
95 sizer->Add (_labels_canvas, 0, wxEXPAND);
96 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
97 sizer->Add (_main_canvas, 1, wxEXPAND);
100 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
101 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
102 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
103 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
104 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
105 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
106 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
107 _main_canvas->Bind (wxEVT_SCROLLWIN_TOP, boost::bind (&Timeline::scrolled, this, _1));
108 _main_canvas->Bind (wxEVT_SCROLLWIN_BOTTOM, boost::bind (&Timeline::scrolled, this, _1));
109 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEUP, boost::bind (&Timeline::scrolled, this, _1));
110 _main_canvas->Bind (wxEVT_SCROLLWIN_LINEDOWN, boost::bind (&Timeline::scrolled, this, _1));
111 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEUP, boost::bind (&Timeline::scrolled, this, _1));
112 _main_canvas->Bind (wxEVT_SCROLLWIN_PAGEDOWN, boost::bind (&Timeline::scrolled, this, _1));
113 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
115 film_change (ChangeType::DONE, Film::Property::CONTENT);
117 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
119 _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
120 _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
122 Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
123 _timer.Start (200, wxTIMER_CONTINUOUS);
126 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
130 Timeline::update_playhead ()
136 Timeline::set_pixels_per_second (double pps)
138 _pixels_per_second = max (_minimum_pixels_per_second, pps);
142 Timeline::paint_labels ()
144 wxPaintDC dc (_labels_canvas);
146 auto gc = wxGraphicsContext::Create (dc);
152 _labels_canvas->GetViewStart (&vsx, &vsy);
153 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
155 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
161 Timeline::paint_main ()
163 wxPaintDC dc (_main_canvas);
164 _main_canvas->DoPrepareDC (dc);
166 auto gc = wxGraphicsContext::Create (dc);
172 _main_canvas->GetViewStart (&vsx, &vsy);
173 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
175 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
177 for (auto i: _views) {
179 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
181 /* Find areas of overlap with other content views, so that we can plot them */
182 list<dcpomatic::Rect<int>> overlaps;
183 for (auto j: _views) {
184 auto jc = dynamic_pointer_cast<TimelineContentView> (j);
185 /* No overlap with non-content views, views on different tracks, audio views or non-active views */
186 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
190 auto r = j->bbox().intersection(i->bbox());
192 overlaps.push_back (r.get ());
196 i->paint (gc, overlaps);
200 /* Translate back as _down_point and _zoom_point do not take scroll into account */
201 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
202 gc->SetPen (*wxBLACK_PEN);
203 gc->SetBrush (*wxTRANSPARENT_BRUSH);
205 min (_down_point.x, _zoom_point->x),
206 min (_down_point.y, _zoom_point->y),
207 abs (_down_point.x - _zoom_point->x),
208 abs (_down_point.y - _zoom_point->y)
214 auto vp = _viewer.lock ();
215 DCPOMATIC_ASSERT (vp);
217 gc->SetPen (*wxRED_PEN);
218 auto path = gc->CreatePath ();
219 double const ph = vp->position().seconds() * pixels_per_second().get_value_or(0);
220 path.MoveToPoint (ph, 0);
221 path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
222 gc->StrokePath (path);
228 Timeline::film_change (ChangeType type, Film::Property p)
230 if (type != ChangeType::DONE) {
234 if (p == Film::Property::CONTENT || p == Film::Property::REEL_TYPE || p == Film::Property::REEL_LENGTH) {
237 } else if (p == Film::Property::CONTENT_ORDER) {
243 Timeline::recreate_views ()
245 auto film = _film.lock ();
251 _views.push_back (_time_axis_view);
252 _views.push_back (_reels_view);
254 for (auto i: film->content ()) {
256 _views.push_back (make_shared<TimelineVideoContentView>(*this, i));
259 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
260 _views.push_back (make_shared<TimelineAudioContentView>(*this, i));
263 for (auto j: i->text) {
264 _views.push_back (make_shared<TimelineTextContentView>(*this, i, j));
268 _views.push_back (make_shared<TimelineAtmosContentView>(*this, i));
278 Timeline::film_content_change (ChangeType type, int property, bool frequent)
280 if (type != ChangeType::DONE) {
286 if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
288 } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
289 _reels_view->force_redraw ();
290 } else if (!frequent) {
298 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
300 int const base = tracks;
302 for (auto i: views) {
303 if (!dynamic_pointer_cast<T>(i)) {
307 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
311 auto content = cv->content();
312 DCPTimePeriod const content_period (content->position(), content->end(film));
315 auto j = views.begin();
316 while (j != views.end()) {
317 auto test = dynamic_pointer_cast<T> (*j);
323 auto test_content = test->content();
325 test->track() && test->track().get() == t &&
326 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
327 /* we have an overlap on track `t' */
335 if (j == views.end ()) {
336 /* no overlap on `t' */
342 tracks = max (tracks, t + 1);
345 return tracks - base;
348 /** Compare the mapped output channels of two TimelineViews, so we can into
349 * order of first mapped DCP channel.
351 struct AudioMappingComparator {
352 bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
354 auto cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
356 auto oc = cva->content()->audio->mapping().mapped_output_channels();
357 la = *min_element(boost::begin(oc), boost::end(oc));
360 auto cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
362 auto oc = cvb->content()->audio->mapping().mapped_output_channels();
363 lb = *min_element(boost::begin(oc), boost::end(oc));
370 Timeline::assign_tracks ()
385 auto film = _film.lock ();
386 DCPOMATIC_ASSERT (film);
390 for (auto i: _views) {
391 auto c = dynamic_pointer_cast<TimelineContentView>(i);
397 int const video_tracks = place<TimelineVideoContentView> (film, _views, _tracks);
398 int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
402 bool have_atmos = false;
403 for (auto i: _views) {
404 auto cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
406 cv->set_track (_tracks);
415 /* Audio. We're sorting the views so that we get the audio views in order of increasing
420 sort(views.begin(), views.end(), AudioMappingComparator());
421 int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
423 _labels_view->set_video_tracks (video_tracks);
424 _labels_view->set_audio_tracks (audio_tracks);
425 _labels_view->set_text_tracks (text_tracks);
426 _labels_view->set_atmos (have_atmos);
428 _time_axis_view->set_y (tracks());
429 _reels_view->set_y (8);
433 Timeline::tracks () const
439 Timeline::setup_scrollbars ()
441 auto film = _film.lock ();
442 if (!film || !_pixels_per_second) {
446 int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
448 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
449 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
450 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
451 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
454 shared_ptr<TimelineView>
455 Timeline::event_to_view (wxMouseEvent& ev)
457 /* Search backwards through views so that we find the uppermost one first */
458 auto i = _views.rbegin();
461 _main_canvas->GetViewStart (&vsx, &vsy);
462 Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
464 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
465 auto cv = dynamic_pointer_cast<TimelineContentView>(*i);
469 if (i == _views.rend ()) {
477 Timeline::left_down (wxMouseEvent& ev)
480 _down_point = ev.GetPosition ();
484 left_down_select (ev);
496 Timeline::left_down_select (wxMouseEvent& ev)
498 auto view = event_to_view (ev);
499 auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
504 _down_view = content_view;
505 _down_view_position = content_view->content()->position ();
508 for (auto i: _views) {
509 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
514 if (!ev.ShiftDown ()) {
515 cv->set_selected (view == i);
519 if (content_view && ev.ShiftDown ()) {
520 content_view->set_selected (!content_view->selected ());
526 /* Pre-compute the points that we might snap to */
527 for (auto i: _views) {
528 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
529 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
533 auto film = _film.lock ();
534 DCPOMATIC_ASSERT (film);
536 _start_snaps.push_back (cv->content()->position());
537 _end_snaps.push_back (cv->content()->position());
538 _start_snaps.push_back (cv->content()->end(film));
539 _end_snaps.push_back (cv->content()->end(film));
541 for (auto i: cv->content()->reel_split_points(film)) {
542 _start_snaps.push_back (i);
546 /* Tell everyone that things might change frequently during the drag */
547 _down_view->content()->set_change_signals_frequent (true);
552 Timeline::left_up (wxMouseEvent& ev)
571 Timeline::left_up_select (wxMouseEvent& ev)
574 _down_view->content()->set_change_signals_frequent (false);
577 _content_panel->set_selection (selected_content ());
578 /* Since we may have just set change signals back to `not-frequent', we have to
579 make sure this position change is signalled, even if the position value has
580 not changed since the last time it was set (with frequent=true). This is
583 set_position_from_event (ev, true);
585 /* Clear up up the stuff we don't do during drag */
590 _start_snaps.clear ();
595 Timeline::left_up_zoom (wxMouseEvent& ev)
597 _zoom_point = ev.GetPosition ();
600 _main_canvas->GetViewStart (&vsx, &vsy);
602 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
603 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
605 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
606 /* Very small zoom rectangle: we assume it wasn't intentional */
607 _zoom_point = optional<wxPoint> ();
612 auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
613 auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
614 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
616 double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
617 double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
618 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
621 int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
622 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
623 _labels_canvas->Scroll (0, y);
625 _zoom_point = optional<wxPoint> ();
630 Timeline::set_pixels_per_track (int h)
632 _pixels_per_track = max(_minimum_pixels_per_track, h);
636 Timeline::mouse_moved (wxMouseEvent& ev)
640 mouse_moved_select (ev);
643 mouse_moved_zoom (ev);
653 Timeline::mouse_moved_select (wxMouseEvent& ev)
659 set_position_from_event (ev);
663 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
669 _zoom_point = ev.GetPosition ();
674 Timeline::right_down (wxMouseEvent& ev)
678 right_down_select (ev);
682 set_pixels_per_second (*_pixels_per_second / 2);
683 set_pixels_per_track (_pixels_per_track / 2);
695 Timeline::right_down_select (wxMouseEvent& ev)
697 auto view = event_to_view (ev);
698 auto cv = dynamic_pointer_cast<TimelineContentView> (view);
703 if (!cv->selected ()) {
705 cv->set_selected (true);
708 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
712 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
714 auto const d = a - b;
715 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
716 nearest_distance = d;
721 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
723 if (!_pixels_per_second) {
727 double const pps = _pixels_per_second.get ();
729 auto const p = ev.GetPosition();
732 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
733 before the drag is considered to have started.
735 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
746 auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
748 auto film = _film.lock ();
749 DCPOMATIC_ASSERT (film);
752 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
753 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
756 optional<DCPTime> nearest_distance;
758 /* Find the nearest snap point */
760 for (auto i: _start_snaps) {
761 maybe_snap (i, new_position, nearest_distance);
764 for (auto i: _end_snaps) {
765 maybe_snap (i, new_end, nearest_distance);
768 if (nearest_distance) {
769 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
770 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
771 new_position += nearest_distance.get ();
776 if (new_position < DCPTime ()) {
777 new_position = DCPTime ();
780 _down_view->content()->set_position (film, new_position, force_emit);
782 film->set_sequence (false);
786 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
788 _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
791 shared_ptr<const Film>
792 Timeline::film () const
794 return _film.lock ();
800 if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
802 _first_resize = false;
808 Timeline::clear_selection ()
810 for (auto i: _views) {
811 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
813 cv->set_selected (false);
818 TimelineContentViewList
819 Timeline::selected_views () const
821 TimelineContentViewList sel;
823 for (auto i: _views) {
824 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
825 if (cv && cv->selected()) {
834 Timeline::selected_content () const
838 for (auto i: selected_views()) {
839 sel.push_back(i->content());
846 Timeline::set_selection (ContentList selection)
848 for (auto i: _views) {
849 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
851 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
857 Timeline::tracks_y_offset () const
859 return _reels_view->bbox().height + 4;
863 Timeline::width () const
865 return _main_canvas->GetVirtualSize().GetWidth();
869 Timeline::scrolled (wxScrollWinEvent& ev)
871 if (ev.GetOrientation() == wxVERTICAL) {
873 _main_canvas->GetViewStart (&x, &y);
874 _labels_canvas->Scroll (0, y);
880 Timeline::tool_clicked (Tool t)
897 Timeline::zoom_all ()
899 auto film = _film.lock ();
900 DCPOMATIC_ASSERT (film);
901 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
902 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
904 _main_canvas->Scroll (0, 0);
905 _labels_canvas->Scroll (0, 0);