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_track_height = 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)
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 * track_height() + 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 _pixels_per_second = max (_minimum_pixels_per_second, static_cast<double>(640) / film->length().seconds ());
110 _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
114 Timeline::paint_labels ()
116 wxPaintDC dc (_labels_canvas);
118 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
124 _labels_canvas->GetViewStart (&vsx, &vsy);
125 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
127 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
133 Timeline::paint_main ()
135 wxPaintDC dc (_main_canvas);
136 _main_canvas->DoPrepareDC (dc);
138 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
144 _main_canvas->GetViewStart (&vsx, &vsy);
145 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
147 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
149 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
151 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
153 /* Find areas of overlap with other content views, so that we can plot them */
154 list<dcpomatic::Rect<int> > overlaps;
155 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
156 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
157 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
158 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
162 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
164 overlaps.push_back (r.get ());
168 i->paint (gc, overlaps);
172 /* Translate back as _down_point and _zoom_point do not take scroll into account */
173 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
174 gc->SetPen (*wxBLACK_PEN);
175 gc->SetBrush (*wxTRANSPARENT_BRUSH);
177 min (_down_point.x, _zoom_point->x),
178 min (_down_point.y, _zoom_point->y),
179 fabs (_down_point.x - _zoom_point->x),
180 fabs (_down_point.y - _zoom_point->y)
188 Timeline::film_changed (Film::Property p)
190 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
193 } else if (p == Film::CONTENT_ORDER) {
199 Timeline::recreate_views ()
201 shared_ptr<const Film> film = _film.lock ();
207 _views.push_back (_time_axis_view);
208 _views.push_back (_reels_view);
210 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
212 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
215 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
216 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
220 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
223 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
224 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
234 Timeline::film_content_changed (int property, bool frequent)
238 if (property == AudioContentProperty::STREAMS) {
240 } else if (!frequent) {
248 place (TimelineViewList& views, int& tracks)
250 int const base = tracks;
252 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
253 if (!dynamic_pointer_cast<T>(i)) {
257 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
261 shared_ptr<Content> content = cv->content();
262 DCPTimePeriod const content_period (content->position(), content->end());
265 TimelineViewList::iterator j = views.begin();
266 while (j != views.end()) {
267 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
273 shared_ptr<Content> test_content = test->content();
275 test->track() && test->track().get() == t &&
276 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
277 /* we have an overlap on track `t' */
285 if (j == views.end ()) {
286 /* no overlap on `t' */
292 tracks = max (tracks, t + 1);
295 return tracks - base;
299 Timeline::assign_tracks ()
302 Video (mono or left-eye)
315 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
316 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
324 bool have_3d = false;
325 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
326 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
331 /* Video on tracks 0 and maybe 1 (left and right eye) */
332 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
334 _tracks = max (_tracks, 2);
341 _tracks = max (_tracks, 1);
345 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
349 bool have_atmos = false;
350 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
351 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
355 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
356 cv->set_track (_tracks - 1);
367 place<TimelineAudioContentView> (_views, _tracks);
369 _labels_view->set_3d (have_3d);
370 _labels_view->set_subtitle_tracks (subtitle_tracks);
371 _labels_view->set_atmos (have_atmos);
373 _time_axis_view->set_y (tracks());
374 _reels_view->set_y (8);
378 Timeline::tracks () const
384 Timeline::setup_scrollbars ()
386 shared_ptr<const Film> film = _film.lock ();
387 if (!film || !_pixels_per_second) {
390 _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * track_height() + 96);
391 _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
392 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
393 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
396 shared_ptr<TimelineView>
397 Timeline::event_to_view (wxMouseEvent& ev)
399 /* Search backwards through views so that we find the uppermost one first */
400 TimelineViewList::reverse_iterator i = _views.rbegin();
401 Position<int> const p (ev.GetX(), ev.GetY());
402 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
403 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
407 if (i == _views.rend ()) {
408 return shared_ptr<TimelineView> ();
415 Timeline::left_down (wxMouseEvent& ev)
418 _down_point = ev.GetPosition ();
422 left_down_select (ev);
431 Timeline::left_down_select (wxMouseEvent& ev)
433 shared_ptr<TimelineView> view = event_to_view (ev);
434 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
439 _down_view = content_view;
440 _down_view_position = content_view->content()->position ();
443 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
444 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
449 if (!ev.ShiftDown ()) {
450 cv->set_selected (view == *i);
454 if (content_view && ev.ShiftDown ()) {
455 content_view->set_selected (!content_view->selected ());
461 /* Pre-compute the points that we might snap to */
462 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
463 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
464 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
468 _start_snaps.push_back (cv->content()->position());
469 _end_snaps.push_back (cv->content()->position());
470 _start_snaps.push_back (cv->content()->end());
471 _end_snaps.push_back (cv->content()->end());
473 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
474 _start_snaps.push_back (i);
478 /* Tell everyone that things might change frequently during the drag */
479 _down_view->content()->set_change_signals_frequent (true);
484 Timeline::left_up (wxMouseEvent& ev)
499 Timeline::left_up_select (wxMouseEvent& ev)
502 _down_view->content()->set_change_signals_frequent (false);
505 _content_panel->set_selection (selected_content ());
506 set_position_from_event (ev);
508 /* Clear up up the stuff we don't do during drag */
513 _start_snaps.clear ();
518 Timeline::left_up_zoom (wxMouseEvent& ev)
520 _zoom_point = ev.GetPosition ();
523 _main_canvas->GetViewStart (&vsx, &vsy);
525 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
526 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
528 if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
529 /* Very small zoom rectangle: we assume it wasn't intentional */
533 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
534 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
535 _pixels_per_second = max (_minimum_pixels_per_second, double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
537 double const tracks_top = double(top_left.y) / _track_height;
538 double const tracks_bottom = double(bottom_right.y) / _track_height;
539 _track_height = max(_minimum_track_height, int(lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top))));
542 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _track_height / _y_scroll_rate);
543 _labels_canvas->Scroll (0, tracks_top * _track_height / _y_scroll_rate);
545 _zoom_point = optional<wxPoint> ();
550 Timeline::mouse_moved (wxMouseEvent& ev)
554 mouse_moved_select (ev);
557 mouse_moved_zoom (ev);
563 Timeline::mouse_moved_select (wxMouseEvent& ev)
569 set_position_from_event (ev);
573 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
579 _zoom_point = ev.GetPosition ();
584 Timeline::right_down (wxMouseEvent& ev)
588 right_down_select (ev);
592 _pixels_per_second = max (_minimum_pixels_per_second, *_pixels_per_second / 2);
593 _track_height = max (_minimum_track_height, _track_height / 2);
601 Timeline::right_down_select (wxMouseEvent& ev)
603 shared_ptr<TimelineView> view = event_to_view (ev);
604 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
609 if (!cv->selected ()) {
611 cv->set_selected (true);
614 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
618 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
620 DCPTime const d = a - b;
621 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
622 nearest_distance = d;
627 Timeline::set_position_from_event (wxMouseEvent& ev)
629 if (!_pixels_per_second) {
633 double const pps = _pixels_per_second.get ();
635 wxPoint const p = ev.GetPosition();
638 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
639 before the drag is considered to have started.
641 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
652 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
656 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
657 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
660 optional<DCPTime> nearest_distance;
662 /* Find the nearest snap point */
664 BOOST_FOREACH (DCPTime i, _start_snaps) {
665 maybe_snap (i, new_position, nearest_distance);
668 BOOST_FOREACH (DCPTime i, _end_snaps) {
669 maybe_snap (i, new_end, nearest_distance);
672 if (nearest_distance) {
673 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
674 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
675 new_position += nearest_distance.get ();
680 if (new_position < DCPTime ()) {
681 new_position = DCPTime ();
684 _down_view->content()->set_position (new_position);
686 shared_ptr<Film> film = _film.lock ();
687 DCPOMATIC_ASSERT (film);
688 film->set_sequence (false);
692 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
694 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
697 shared_ptr<const Film>
698 Timeline::film () const
700 return _film.lock ();
710 Timeline::clear_selection ()
712 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
713 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
715 cv->set_selected (false);
720 TimelineContentViewList
721 Timeline::selected_views () const
723 TimelineContentViewList sel;
725 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
726 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
727 if (cv && cv->selected()) {
736 Timeline::selected_content () const
739 TimelineContentViewList views = selected_views ();
741 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
742 sel.push_back ((*i)->content ());
749 Timeline::set_selection (ContentList selection)
751 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
752 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
754 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
760 Timeline::tracks_y_offset () const
762 return _reels_view->bbox().height + 4;
766 Timeline::width () const
768 return _main_canvas->GetVirtualSize().GetWidth();
772 Timeline::scrolled (wxScrollWinEvent& ev)
774 if (ev.GetOrientation() == wxVERTICAL) {
775 _labels_canvas->Scroll (0, ev.GetPosition ());