2 Copyright (C) 2013-2018 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_subtitle_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/subtitle_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
54 using boost::optional;
56 /* 3 hours in 640 pixels */
57 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
58 int const Timeline::_minimum_pixels_per_track = 16;
60 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
61 : wxPanel (parent, wxID_ANY)
62 , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
63 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
66 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
67 , _reels_view (new TimelineReelsView (*this, 32))
68 , _labels_view (new TimelineLabelsView (*this))
71 , _down_view_position (0)
78 , _pixels_per_track (48)
81 _labels_canvas->SetDoubleBuffered (true);
82 _main_canvas->SetDoubleBuffered (true);
85 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
86 sizer->Add (_labels_canvas, 0, wxEXPAND);
87 _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
88 sizer->Add (_main_canvas, 1, wxEXPAND);
91 _labels_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
92 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
93 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
94 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
95 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
96 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
97 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
98 _main_canvas->Bind (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled, this, _1));
100 film_changed (Film::CONTENT);
102 SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
104 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
105 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
107 set_pixels_per_second (static_cast<double>(640) / film->length().seconds ());
110 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
114 Timeline::set_pixels_per_second (double pps)
116 _pixels_per_second = max (_minimum_pixels_per_second, pps);
120 Timeline::paint_labels ()
122 wxPaintDC dc (_labels_canvas);
124 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
130 _labels_canvas->GetViewStart (&vsx, &vsy);
131 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
133 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
139 Timeline::paint_main ()
141 wxPaintDC dc (_main_canvas);
142 _main_canvas->DoPrepareDC (dc);
144 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
150 _main_canvas->GetViewStart (&vsx, &vsy);
151 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
153 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
155 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
157 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
159 /* Find areas of overlap with other content views, so that we can plot them */
160 list<dcpomatic::Rect<int> > overlaps;
161 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
162 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
163 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
164 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
168 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
170 overlaps.push_back (r.get ());
174 i->paint (gc, overlaps);
178 /* Translate back as _down_point and _zoom_point do not take scroll into account */
179 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
180 gc->SetPen (*wxBLACK_PEN);
181 gc->SetBrush (*wxTRANSPARENT_BRUSH);
183 min (_down_point.x, _zoom_point->x),
184 min (_down_point.y, _zoom_point->y),
185 fabs (_down_point.x - _zoom_point->x),
186 fabs (_down_point.y - _zoom_point->y)
194 Timeline::film_changed (Film::Property p)
196 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
199 } else if (p == Film::CONTENT_ORDER) {
205 Timeline::recreate_views ()
207 shared_ptr<const Film> film = _film.lock ();
213 _views.push_back (_time_axis_view);
214 _views.push_back (_reels_view);
216 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
218 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
221 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
222 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
226 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
229 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
230 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
240 Timeline::film_content_changed (int property, bool frequent)
244 if (property == AudioContentProperty::STREAMS) {
246 } else if (!frequent) {
254 place (TimelineViewList& views, int& tracks)
256 int const base = tracks;
258 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
259 if (!dynamic_pointer_cast<T>(i)) {
263 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
267 shared_ptr<Content> content = cv->content();
268 DCPTimePeriod const content_period (content->position(), content->end());
271 TimelineViewList::iterator j = views.begin();
272 while (j != views.end()) {
273 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
279 shared_ptr<Content> test_content = test->content();
281 test->track() && test->track().get() == t &&
282 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
283 /* we have an overlap on track `t' */
291 if (j == views.end ()) {
292 /* no overlap on `t' */
298 tracks = max (tracks, t + 1);
301 return tracks - base;
305 Timeline::assign_tracks ()
308 Video (mono or left-eye)
321 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
322 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
330 bool have_3d = false;
331 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
332 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
337 /* Video on tracks 0 and maybe 1 (left and right eye) */
338 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
340 _tracks = max (_tracks, 2);
347 _tracks = max (_tracks, 1);
351 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
355 bool have_atmos = false;
356 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
357 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
361 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
362 cv->set_track (_tracks - 1);
373 place<TimelineAudioContentView> (_views, _tracks);
375 _labels_view->set_3d (have_3d);
376 _labels_view->set_subtitle_tracks (subtitle_tracks);
377 _labels_view->set_atmos (have_atmos);
379 _time_axis_view->set_y (tracks());
380 _reels_view->set_y (8);
384 Timeline::tracks () const
390 Timeline::setup_scrollbars ()
392 shared_ptr<const Film> film = _film.lock ();
393 if (!film || !_pixels_per_second) {
396 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * pixels_per_track() + 96);
397 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
398 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * pixels_per_track() + 96);
399 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
402 shared_ptr<TimelineView>
403 Timeline::event_to_view (wxMouseEvent& ev)
405 /* Search backwards through views so that we find the uppermost one first */
406 TimelineViewList::reverse_iterator i = _views.rbegin();
407 Position<int> const p (ev.GetX(), ev.GetY());
408 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
409 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
413 if (i == _views.rend ()) {
414 return shared_ptr<TimelineView> ();
421 Timeline::left_down (wxMouseEvent& ev)
424 _down_point = ev.GetPosition ();
428 left_down_select (ev);
438 Timeline::left_down_select (wxMouseEvent& ev)
440 shared_ptr<TimelineView> view = event_to_view (ev);
441 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
446 _down_view = content_view;
447 _down_view_position = content_view->content()->position ();
450 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
451 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
456 if (!ev.ShiftDown ()) {
457 cv->set_selected (view == *i);
461 if (content_view && ev.ShiftDown ()) {
462 content_view->set_selected (!content_view->selected ());
468 /* Pre-compute the points that we might snap to */
469 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
470 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
471 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
475 _start_snaps.push_back (cv->content()->position());
476 _end_snaps.push_back (cv->content()->position());
477 _start_snaps.push_back (cv->content()->end());
478 _end_snaps.push_back (cv->content()->end());
480 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
481 _start_snaps.push_back (i);
485 /* Tell everyone that things might change frequently during the drag */
486 _down_view->content()->set_change_signals_frequent (true);
491 Timeline::left_up (wxMouseEvent& ev)
508 Timeline::left_up_select (wxMouseEvent& ev)
511 _down_view->content()->set_change_signals_frequent (false);
514 _content_panel->set_selection (selected_content ());
515 set_position_from_event (ev);
517 /* Clear up up the stuff we don't do during drag */
522 _start_snaps.clear ();
527 Timeline::left_up_zoom (wxMouseEvent& ev)
529 _zoom_point = ev.GetPosition ();
532 _main_canvas->GetViewStart (&vsx, &vsy);
534 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
535 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
537 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
538 /* Very small zoom rectangle: we assume it wasn't intentional */
542 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
543 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
544 set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
546 double const tracks_top = double(top_left.y) / _pixels_per_track;
547 double const tracks_bottom = double(bottom_right.y) / _pixels_per_track;
548 set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
551 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _pixels_per_track / _y_scroll_rate);
552 _labels_canvas->Scroll (0, tracks_top * _pixels_per_track / _y_scroll_rate);
554 _zoom_point = optional<wxPoint> ();
559 Timeline::set_pixels_per_track (int h)
561 _pixels_per_track = max(_minimum_pixels_per_track, h);
565 Timeline::mouse_moved (wxMouseEvent& ev)
569 mouse_moved_select (ev);
572 mouse_moved_zoom (ev);
580 Timeline::mouse_moved_select (wxMouseEvent& ev)
586 set_position_from_event (ev);
590 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
596 _zoom_point = ev.GetPosition ();
601 Timeline::right_down (wxMouseEvent& ev)
605 right_down_select (ev);
609 set_pixels_per_second (*_pixels_per_second / 2);
610 set_pixels_per_track (_pixels_per_track / 2);
620 Timeline::right_down_select (wxMouseEvent& ev)
622 shared_ptr<TimelineView> view = event_to_view (ev);
623 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
628 if (!cv->selected ()) {
630 cv->set_selected (true);
633 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
637 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
639 DCPTime const d = a - b;
640 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
641 nearest_distance = d;
646 Timeline::set_position_from_event (wxMouseEvent& ev)
648 if (!_pixels_per_second) {
652 double const pps = _pixels_per_second.get ();
654 wxPoint const p = ev.GetPosition();
657 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
658 before the drag is considered to have started.
660 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
671 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
675 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
676 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
679 optional<DCPTime> nearest_distance;
681 /* Find the nearest snap point */
683 BOOST_FOREACH (DCPTime i, _start_snaps) {
684 maybe_snap (i, new_position, nearest_distance);
687 BOOST_FOREACH (DCPTime i, _end_snaps) {
688 maybe_snap (i, new_end, nearest_distance);
691 if (nearest_distance) {
692 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
693 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
694 new_position += nearest_distance.get ();
699 if (new_position < DCPTime ()) {
700 new_position = DCPTime ();
703 _down_view->content()->set_position (new_position);
705 shared_ptr<Film> film = _film.lock ();
706 DCPOMATIC_ASSERT (film);
707 film->set_sequence (false);
711 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
713 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
716 shared_ptr<const Film>
717 Timeline::film () const
719 return _film.lock ();
729 Timeline::clear_selection ()
731 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
732 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
734 cv->set_selected (false);
739 TimelineContentViewList
740 Timeline::selected_views () const
742 TimelineContentViewList sel;
744 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
745 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
746 if (cv && cv->selected()) {
755 Timeline::selected_content () const
758 TimelineContentViewList views = selected_views ();
760 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
761 sel.push_back ((*i)->content ());
768 Timeline::set_selection (ContentList selection)
770 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
771 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
773 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
779 Timeline::tracks_y_offset () const
781 return _reels_view->bbox().height + 4;
785 Timeline::width () const
787 return _main_canvas->GetVirtualSize().GetWidth();
791 Timeline::scrolled (wxScrollWinEvent& ev)
793 if (ev.GetOrientation() == wxVERTICAL) {
794 _labels_canvas->Scroll (0, ev.GetPosition ());
800 Timeline::tool_clicked (Tool t)
808 shared_ptr<Film> film = _film.lock ();
809 DCPOMATIC_ASSERT (film);
810 set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
811 set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);