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 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
57 : wxPanel (parent, wxID_ANY)
58 , _labels_panel (new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
59 , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
62 , _time_axis_view (new TimelineTimeAxisView (*this, 64))
63 , _reels_view (new TimelineReelsView (*this, 32))
64 , _labels_view (new TimelineLabelsView (*this))
67 , _down_view_position (0)
77 _labels_panel->SetDoubleBuffered (true);
78 _main_canvas->SetDoubleBuffered (true);
81 wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
82 sizer->Add (_labels_panel, 0, wxEXPAND);
83 _labels_panel->SetMinSize (wxSize (_labels_view->bbox().width, -1));
84 sizer->Add (_main_canvas, 1, wxEXPAND);
87 _labels_panel->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_labels, this));
88 _main_canvas->Bind (wxEVT_PAINT, boost::bind (&Timeline::paint_main, this));
89 _main_canvas->Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
90 _main_canvas->Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
91 _main_canvas->Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
92 _main_canvas->Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
93 _main_canvas->Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
95 film_changed (Film::CONTENT);
97 SetMinSize (wxSize (640, 4 * track_height() + 96));
99 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
100 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
102 _pixels_per_second = max (0.01, static_cast<double>(640) / film->length().seconds ());
105 _main_canvas->EnableScrolling (true, true);
109 Timeline::paint_labels ()
111 wxPaintDC dc (_labels_panel);
113 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
118 _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
124 Timeline::paint_main ()
126 wxPaintDC dc (_main_canvas);
127 _main_canvas->DoPrepareDC (dc);
129 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
135 _main_canvas->GetViewStart (&vsx, &vsy);
136 gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
138 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
140 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
142 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
144 /* Find areas of overlap with other content views, so that we can plot them */
145 list<dcpomatic::Rect<int> > overlaps;
146 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
147 shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
148 /* No overlap with non-content views, views no different tracks, audio views or non-active views */
149 if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
153 optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
155 overlaps.push_back (r.get ());
159 i->paint (gc, overlaps);
163 /* Translate back as _down_point and _zoom_point do not take scroll into account */
164 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
165 gc->SetPen (*wxBLACK_PEN);
166 gc->SetBrush (*wxTRANSPARENT_BRUSH);
168 min (_down_point.x, _zoom_point->x),
169 min (_down_point.y, _zoom_point->y),
170 fabs (_down_point.x - _zoom_point->x),
171 fabs (_down_point.y - _zoom_point->y)
179 Timeline::film_changed (Film::Property p)
181 if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
184 } else if (p == Film::CONTENT_ORDER) {
190 Timeline::recreate_views ()
192 shared_ptr<const Film> film = _film.lock ();
198 _views.push_back (_time_axis_view);
199 _views.push_back (_reels_view);
201 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
203 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
206 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
207 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
211 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
214 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
215 _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
225 Timeline::film_content_changed (int property, bool frequent)
229 if (property == AudioContentProperty::STREAMS) {
231 } else if (!frequent) {
239 place (TimelineViewList& views, int& tracks)
241 int const base = tracks;
243 BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
244 if (!dynamic_pointer_cast<T>(i)) {
248 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
252 shared_ptr<Content> content = cv->content();
253 DCPTimePeriod const content_period (content->position(), content->end());
256 TimelineViewList::iterator j = views.begin();
257 while (j != views.end()) {
258 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
264 shared_ptr<Content> test_content = test->content();
266 test->track() && test->track().get() == t &&
267 content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
268 /* we have an overlap on track `t' */
276 if (j == views.end ()) {
277 /* no overlap on `t' */
283 tracks = max (tracks, t + 1);
286 return tracks - base;
290 Timeline::assign_tracks ()
293 Video (mono or left-eye)
306 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
307 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
315 bool have_3d = false;
316 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
317 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
322 /* Video on tracks 0 and maybe 1 (left and right eye) */
323 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
325 _tracks = max (_tracks, 2);
332 _tracks = max (_tracks, 1);
336 int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
340 bool have_atmos = false;
341 BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
342 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
346 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
347 cv->set_track (_tracks - 1);
358 place<TimelineAudioContentView> (_views, _tracks);
360 _labels_view->set_3d (have_3d);
361 _labels_view->set_subtitle_tracks (subtitle_tracks);
362 _labels_view->set_atmos (have_atmos);
364 _time_axis_view->set_y (tracks() * track_height() + 64);
365 _reels_view->set_y (8);
369 Timeline::tracks () const
375 Timeline::setup_scrollbars ()
377 shared_ptr<const Film> film = _film.lock ();
378 if (!film || !_pixels_per_second) {
381 _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
382 _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
385 shared_ptr<TimelineView>
386 Timeline::event_to_view (wxMouseEvent& ev)
388 /* Search backwards through views so that we find the uppermost one first */
389 TimelineViewList::reverse_iterator i = _views.rbegin();
390 Position<int> const p (ev.GetX(), ev.GetY());
391 while (i != _views.rend() && !(*i)->bbox().contains (p)) {
392 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
396 if (i == _views.rend ()) {
397 return shared_ptr<TimelineView> ();
404 Timeline::left_down (wxMouseEvent& ev)
407 _down_point = ev.GetPosition ();
411 left_down_select (ev);
420 Timeline::left_down_select (wxMouseEvent& ev)
422 shared_ptr<TimelineView> view = event_to_view (ev);
423 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
428 _down_view = content_view;
429 _down_view_position = content_view->content()->position ();
432 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
433 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
438 if (!ev.ShiftDown ()) {
439 cv->set_selected (view == *i);
443 if (content_view && ev.ShiftDown ()) {
444 content_view->set_selected (!content_view->selected ());
450 /* Pre-compute the points that we might snap to */
451 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
452 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
453 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
457 _start_snaps.push_back (cv->content()->position());
458 _end_snaps.push_back (cv->content()->position());
459 _start_snaps.push_back (cv->content()->end());
460 _end_snaps.push_back (cv->content()->end());
462 BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
463 _start_snaps.push_back (i);
467 /* Tell everyone that things might change frequently during the drag */
468 _down_view->content()->set_change_signals_frequent (true);
473 Timeline::left_up (wxMouseEvent& ev)
488 Timeline::left_up_select (wxMouseEvent& ev)
491 _down_view->content()->set_change_signals_frequent (false);
494 _content_panel->set_selection (selected_content ());
495 set_position_from_event (ev);
497 /* Clear up up the stuff we don't do during drag */
502 _start_snaps.clear ();
507 Timeline::left_up_zoom (wxMouseEvent& ev)
509 _zoom_point = ev.GetPosition ();
512 _main_canvas->GetViewStart (&vsx, &vsy);
514 wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
515 wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
517 DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
518 DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
519 _pixels_per_second = GetSize().GetWidth() / (time_right.seconds() - time_left.seconds());
521 double const tracks_top = double(top_left.y) / _track_height;
522 double const tracks_bottom = double(bottom_right.y) / _track_height;
523 _track_height = GetSize().GetHeight() / (tracks_bottom - tracks_top);
524 cout << tracks_top << " " << tracks_bottom << "\n";
527 _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _track_height / _y_scroll_rate);
529 _zoom_point = optional<wxPoint> ();
534 Timeline::mouse_moved (wxMouseEvent& ev)
538 mouse_moved_select (ev);
541 mouse_moved_zoom (ev);
547 Timeline::mouse_moved_select (wxMouseEvent& ev)
553 set_position_from_event (ev);
557 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
563 _zoom_point = ev.GetPosition ();
568 Timeline::right_down (wxMouseEvent& ev)
572 right_down_select (ev);
576 _pixels_per_second = *_pixels_per_second / 2;
577 _track_height = max (8, _track_height / 2);
584 Timeline::right_down_select (wxMouseEvent& ev)
586 shared_ptr<TimelineView> view = event_to_view (ev);
587 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
592 if (!cv->selected ()) {
594 cv->set_selected (true);
597 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
601 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
603 DCPTime const d = a - b;
604 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
605 nearest_distance = d;
610 Timeline::set_position_from_event (wxMouseEvent& ev)
612 if (!_pixels_per_second) {
616 double const pps = _pixels_per_second.get ();
618 wxPoint const p = ev.GetPosition();
621 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
622 before the drag is considered to have started.
624 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
635 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
639 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
640 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
643 optional<DCPTime> nearest_distance;
645 /* Find the nearest snap point */
647 BOOST_FOREACH (DCPTime i, _start_snaps) {
648 maybe_snap (i, new_position, nearest_distance);
651 BOOST_FOREACH (DCPTime i, _end_snaps) {
652 maybe_snap (i, new_end, nearest_distance);
655 if (nearest_distance) {
656 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
657 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
658 new_position += nearest_distance.get ();
663 if (new_position < DCPTime ()) {
664 new_position = DCPTime ();
667 _down_view->content()->set_position (new_position);
669 shared_ptr<Film> film = _film.lock ();
670 DCPOMATIC_ASSERT (film);
671 film->set_sequence (false);
675 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
677 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
680 shared_ptr<const Film>
681 Timeline::film () const
683 return _film.lock ();
693 Timeline::clear_selection ()
695 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
696 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
698 cv->set_selected (false);
703 TimelineContentViewList
704 Timeline::selected_views () const
706 TimelineContentViewList sel;
708 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
709 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
710 if (cv && cv->selected()) {
719 Timeline::selected_content () const
722 TimelineContentViewList views = selected_views ();
724 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
725 sel.push_back ((*i)->content ());
732 Timeline::set_selection (ContentList selection)
734 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
735 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
737 cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());