2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "film_editor.h"
22 #include "timeline_time_axis_view.h"
23 #include "timeline_video_content_view.h"
24 #include "timeline_audio_content_view.h"
25 #include "timeline_subtitle_content_view.h"
26 #include "content_panel.h"
29 #include "lib/playlist.h"
30 #include "lib/image_content.h"
31 #include "lib/audio_content.h"
32 #include "lib/subtitle_content.h"
33 #include <wx/graphics.h>
34 #include <boost/weak_ptr.hpp>
35 #include <boost/foreach.hpp>
41 using boost::shared_ptr;
42 using boost::weak_ptr;
43 using boost::dynamic_pointer_cast;
45 using boost::optional;
47 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
48 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
51 , _time_axis_view (new TimelineTimeAxisView (*this, 32))
54 , _down_view_position (0)
60 SetDoubleBuffered (true);
63 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
64 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
65 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
66 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
67 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
68 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
70 film_changed (Film::CONTENT);
72 SetMinSize (wxSize (640, tracks() * track_height() + 96));
74 _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
75 _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2));
83 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
88 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
96 Timeline::film_changed (Film::Property p)
98 if (p == Film::CONTENT) {
105 Timeline::recreate_views ()
107 shared_ptr<const Film> film = _film.lock ();
113 _views.push_back (_time_axis_view);
115 BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
116 if (dynamic_pointer_cast<VideoContent> (i)) {
117 _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
120 shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (i);
121 if (ac && !ac->audio_mapping().mapped_output_channels().empty ()) {
122 _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
125 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (i);
126 if (sc && sc->has_subtitles ()) {
127 _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, sc)));
132 setup_pixels_per_second ();
137 Timeline::film_content_changed (int property)
141 if (property == ContentProperty::POSITION) {
144 /* Only do this if we are not dragging, as it's confusing otherwise */
145 setup_pixels_per_second ();
148 } else if (property == AudioContentProperty::AUDIO_STREAMS) {
154 Timeline::assign_tracks ()
156 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
157 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
163 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
164 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
169 shared_ptr<Content> content = cv->content();
173 TimelineViewList::iterator j = _views.begin();
174 while (j != _views.end()) {
175 shared_ptr<TimelineContentView> test = dynamic_pointer_cast<TimelineContentView> (*j);
181 shared_ptr<Content> test_content = test->content();
183 if (test && test->track() && test->track().get() == t) {
184 bool const no_overlap =
185 (content->position() < test_content->position() && content->end() < test_content->position()) ||
186 (content->position() > test_content->end() && content->end() > test_content->end());
189 /* we have an overlap on track `t' */
198 if (j == _views.end ()) {
199 /* no overlap on `t' */
205 _tracks = max (_tracks, t + 1);
208 _time_axis_view->set_y (tracks() * track_height() + 32);
212 Timeline::tracks () const
218 Timeline::setup_pixels_per_second ()
220 shared_ptr<const Film> film = _film.lock ();
221 if (!film || film->length() == DCPTime ()) {
225 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
228 shared_ptr<TimelineView>
229 Timeline::event_to_view (wxMouseEvent& ev)
231 TimelineViewList::iterator i = _views.begin();
232 Position<int> const p (ev.GetX(), ev.GetY());
233 while (i != _views.end() && !(*i)->bbox().contains (p)) {
237 if (i == _views.end ()) {
238 return shared_ptr<TimelineView> ();
245 Timeline::left_down (wxMouseEvent& ev)
247 shared_ptr<TimelineView> view = event_to_view (ev);
248 shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
253 _down_view = content_view;
254 _down_view_position = content_view->content()->position ();
257 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
258 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
263 if (!ev.ShiftDown ()) {
264 cv->set_selected (view == *i);
268 _content_panel->set_selection (cv->content ());
272 if (content_view && ev.ShiftDown ()) {
273 content_view->set_selected (!content_view->selected ());
277 _down_point = ev.GetPosition ();
281 _down_view->content()->set_change_signals_frequent (true);
286 Timeline::left_up (wxMouseEvent& ev)
291 _down_view->content()->set_change_signals_frequent (false);
294 set_position_from_event (ev);
296 /* We don't do this during drag, and set_position_from_event above
297 might not have changed the position, so do it now.
299 setup_pixels_per_second ();
304 Timeline::mouse_moved (wxMouseEvent& ev)
310 set_position_from_event (ev);
314 Timeline::right_down (wxMouseEvent& ev)
316 shared_ptr<TimelineView> view = event_to_view (ev);
317 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
322 if (!cv->selected ()) {
324 cv->set_selected (true);
327 _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
331 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
333 DCPTime const d = a - b;
334 if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
335 nearest_distance = d;
340 Timeline::set_position_from_event (wxMouseEvent& ev)
342 if (!_pixels_per_second) {
346 double const pps = _pixels_per_second.get ();
348 wxPoint const p = ev.GetPosition();
351 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
352 before the drag is considered to have started.
354 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
365 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
369 DCPTime const new_end = new_position + _down_view->content()->length_after_trim () - DCPTime (1);
370 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
373 optional<DCPTime> nearest_distance;
375 /* Find the nearest content edge; this is inefficient */
376 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
377 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
378 if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
382 maybe_snap (cv->content()->position(), new_position, nearest_distance);
383 maybe_snap (cv->content()->position(), new_end + DCPTime (1), nearest_distance);
384 maybe_snap (cv->content()->end() + DCPTime (1), new_position, nearest_distance);
385 maybe_snap (cv->content()->end() + DCPTime (1), new_end, nearest_distance);
388 if (nearest_distance) {
389 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
390 if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
391 new_position += nearest_distance.get ();
396 if (new_position < DCPTime ()) {
397 new_position = DCPTime ();
400 _down_view->content()->set_position (new_position);
402 shared_ptr<Film> film = _film.lock ();
403 DCPOMATIC_ASSERT (film);
404 film->set_sequence_video (false);
408 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
410 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
413 shared_ptr<const Film>
414 Timeline::film () const
416 return _film.lock ();
422 setup_pixels_per_second ();
426 Timeline::clear_selection ()
428 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
429 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
431 cv->set_selected (false);
436 TimelineContentViewList
437 Timeline::selected_views () const
439 TimelineContentViewList sel;
441 for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
442 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
443 if (cv && cv->selected()) {
452 Timeline::selected_content () const
455 TimelineContentViewList views = selected_views ();
457 for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
458 sel.push_back ((*i)->content ());