2 Copyright (C) 2013-2014 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.
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
24 #include "lib/playlist.h"
25 #include "lib/image_content.h"
26 #include "film_editor.h"
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
37 using boost::optional;
40 * @brief Parent class for components of the timeline (e.g. a piece of content or an axis).
42 class View : public boost::noncopyable
53 void paint (wxGraphicsContext* g)
55 _last_paint_bbox = bbox ();
61 _timeline.force_redraw (_last_paint_bbox);
62 _timeline.force_redraw (bbox ());
65 virtual dcpomatic::Rect<int> bbox () const = 0;
68 virtual void do_paint (wxGraphicsContext *) = 0;
70 int time_x (DCPTime t) const
72 return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second().get_value_or (0);
78 dcpomatic::Rect<int> _last_paint_bbox;
82 /** @class ContentView
83 * @brief Parent class for views of pieces of content.
85 class ContentView : public View
88 ContentView (Timeline& tl, shared_ptr<Content> c)
93 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
96 dcpomatic::Rect<int> bbox () const
100 shared_ptr<const Film> film = _timeline.film ();
101 shared_ptr<const Content> content = _content.lock ();
102 if (!film || !content) {
103 return dcpomatic::Rect<int> ();
106 return dcpomatic::Rect<int> (
107 time_x (content->position ()) - 8,
108 y_pos (_track.get()) - 8,
109 content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0) + 16,
110 _timeline.track_height() + 16
114 void set_selected (bool s) {
119 bool selected () const {
123 shared_ptr<Content> content () const {
124 return _content.lock ();
127 void set_track (int t) {
131 void unset_track () {
132 _track = boost::optional<int> ();
135 optional<int> track () const {
139 virtual wxString type () const = 0;
140 virtual wxColour colour () const = 0;
144 void do_paint (wxGraphicsContext* gc)
148 shared_ptr<const Film> film = _timeline.film ();
149 shared_ptr<const Content> cont = content ();
150 if (!film || !cont) {
154 DCPTime const position = cont->position ();
155 DCPTime const len = cont->length_after_trim ();
157 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
159 gc->SetPen (*wxBLACK_PEN);
161 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
163 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
165 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
168 wxGraphicsPath path = gc->CreatePath ();
169 path.MoveToPoint (time_x (position), y_pos (_track.get()) + 4);
170 path.AddLineToPoint (time_x (position + len), y_pos (_track.get()) + 4);
171 path.AddLineToPoint (time_x (position + len), y_pos (_track.get() + 1) - 4);
172 path.AddLineToPoint (time_x (position), y_pos (_track.get() + 1) - 4);
173 path.AddLineToPoint (time_x (position), y_pos (_track.get()) + 4);
174 gc->StrokePath (path);
177 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->summary()).data(), type().data());
179 wxDouble name_height;
180 wxDouble name_descent;
181 wxDouble name_leading;
182 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
184 gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height()));
185 gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4);
189 int y_pos (int t) const
191 return _timeline.tracks_position().y + t * _timeline.track_height();
194 void content_changed (int p, bool frequent)
198 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
203 _timeline.setup_pixels_per_second ();
204 _timeline.Refresh ();
208 boost::weak_ptr<Content> _content;
209 optional<int> _track;
212 boost::signals2::scoped_connection _content_connection;
215 class AudioContentView : public ContentView
218 AudioContentView (Timeline& tl, shared_ptr<Content> c)
219 : ContentView (tl, c)
223 wxString type () const
228 wxColour colour () const
230 return wxColour (149, 121, 232, 255);
234 class VideoContentView : public ContentView
237 VideoContentView (Timeline& tl, shared_ptr<Content> c)
238 : ContentView (tl, c)
243 wxString type () const
245 if (dynamic_pointer_cast<ImageContent> (content ()) && content()->number_of_paths() == 1) {
252 wxColour colour () const
254 return wxColour (242, 92, 120, 255);
258 class SubtitleContentView : public ContentView
261 SubtitleContentView (Timeline& tl, shared_ptr<Content> c)
262 : ContentView (tl, c)
266 wxString type () const
268 return _("subtitles");
271 wxColour colour () const
273 return wxColour (163, 255, 154, 255);
277 class TimeAxisView : public View
280 TimeAxisView (Timeline& tl, int y)
285 dcpomatic::Rect<int> bbox () const
287 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
298 void do_paint (wxGraphicsContext* gc)
300 if (!_timeline.pixels_per_second()) {
304 double const pps = _timeline.pixels_per_second().get ();
306 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
308 double mark_interval = rint (128 / pps);
309 if (mark_interval > 5) {
310 mark_interval -= int (rint (mark_interval)) % 5;
312 if (mark_interval > 10) {
313 mark_interval -= int (rint (mark_interval)) % 10;
315 if (mark_interval > 60) {
316 mark_interval -= int (rint (mark_interval)) % 60;
318 if (mark_interval > 3600) {
319 mark_interval -= int (rint (mark_interval)) % 3600;
322 if (mark_interval < 1) {
326 wxGraphicsPath path = gc->CreatePath ();
327 path.MoveToPoint (_timeline.x_offset(), _y);
328 path.AddLineToPoint (_timeline.width(), _y);
329 gc->StrokePath (path);
331 /* Time in seconds */
333 while ((t.seconds() * pps) < _timeline.width()) {
334 wxGraphicsPath path = gc->CreatePath ();
335 path.MoveToPoint (time_x (t), _y - 4);
336 path.AddLineToPoint (time_x (t), _y + 4);
337 gc->StrokePath (path);
339 double tc = t.seconds ();
340 int const h = tc / 3600;
342 int const m = tc / 60;
346 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
349 wxDouble str_descent;
350 wxDouble str_leading;
351 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
353 int const tx = _timeline.x_offset() + t.seconds() * pps;
354 if ((tx + str_width) < _timeline.width()) {
355 gc->DrawText (str, time_x (t), _y + 16);
358 t += DCPTime::from_seconds (mark_interval);
367 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
368 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
371 , _time_axis_view (new TimeAxisView (*this, 32))
374 , _down_view_position (0)
375 , _first_move (false)
380 SetDoubleBuffered (true);
383 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
384 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
385 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
386 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
387 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
388 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
392 SetMinSize (wxSize (640, tracks() * track_height() + 96));
394 _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
395 _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2));
403 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
408 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
410 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
418 Timeline::playlist_changed ()
422 shared_ptr<const Film> fl = _film.lock ();
428 _views.push_back (_time_axis_view);
430 ContentList content = fl->playlist()->content ();
432 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
433 if (dynamic_pointer_cast<VideoContent> (*i)) {
434 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
436 if (dynamic_pointer_cast<AudioContent> (*i)) {
437 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
440 shared_ptr<SubtitleContent> sc = dynamic_pointer_cast<SubtitleContent> (*i);
441 if (sc && sc->has_subtitles ()) {
442 _views.push_back (shared_ptr<View> (new SubtitleContentView (*this, *i)));
447 setup_pixels_per_second ();
452 Timeline::playlist_content_changed (int property)
456 if (property == ContentProperty::POSITION) {
458 setup_pixels_per_second ();
464 Timeline::assign_tracks ()
466 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
467 shared_ptr<ContentView> c = dynamic_pointer_cast<ContentView> (*i);
473 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
474 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
479 shared_ptr<Content> content = cv->content();
483 ViewList::iterator j = _views.begin();
484 while (j != _views.end()) {
485 shared_ptr<ContentView> test = dynamic_pointer_cast<ContentView> (*j);
491 shared_ptr<Content> test_content = test->content();
493 if (test && test->track() && test->track().get() == t) {
494 bool const no_overlap =
495 (content->position() < test_content->position() && content->end() < test_content->position()) ||
496 (content->position() > test_content->end() && content->end() > test_content->end());
499 /* we have an overlap on track `t' */
508 if (j == _views.end ()) {
509 /* no overlap on `t' */
515 _tracks = max (_tracks, t + 1);
518 _time_axis_view->set_y (tracks() * track_height() + 32);
522 Timeline::tracks () const
528 Timeline::setup_pixels_per_second ()
530 shared_ptr<const Film> film = _film.lock ();
531 if (!film || film->length() == DCPTime ()) {
535 _pixels_per_second = static_cast<double>(width() - x_offset() * 2) / film->length().seconds ();
539 Timeline::event_to_view (wxMouseEvent& ev)
541 ViewList::iterator i = _views.begin();
542 Position<int> const p (ev.GetX(), ev.GetY());
543 while (i != _views.end() && !(*i)->bbox().contains (p)) {
547 if (i == _views.end ()) {
548 return shared_ptr<View> ();
555 Timeline::left_down (wxMouseEvent& ev)
557 shared_ptr<View> view = event_to_view (ev);
558 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
563 _down_view = content_view;
564 _down_view_position = content_view->content()->position ();
567 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
568 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
573 if (!ev.ShiftDown ()) {
574 cv->set_selected (view == *i);
578 _film_editor->set_selection (cv->content ());
582 if (content_view && ev.ShiftDown ()) {
583 content_view->set_selected (!content_view->selected ());
587 _down_point = ev.GetPosition ();
591 _down_view->content()->set_change_signals_frequent (true);
596 Timeline::left_up (wxMouseEvent& ev)
601 _down_view->content()->set_change_signals_frequent (false);
604 set_position_from_event (ev);
608 Timeline::mouse_moved (wxMouseEvent& ev)
614 set_position_from_event (ev);
618 Timeline::right_down (wxMouseEvent& ev)
620 shared_ptr<View> view = event_to_view (ev);
621 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
626 if (!cv->selected ()) {
628 cv->set_selected (true);
631 _menu.popup (_film, selected_content (), ev.GetPosition ());
635 Timeline::set_position_from_event (wxMouseEvent& ev)
637 if (!_pixels_per_second) {
641 double const pps = _pixels_per_second.get ();
643 wxPoint const p = ev.GetPosition();
646 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
647 before the drag is considered to have started.
649 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
660 DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
665 DCPTime nearest_distance = DCPTime::max ();
666 DCPTime nearest_new_position = DCPTime::max ();
668 /* Find the nearest content edge; this is inefficient */
669 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
670 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
671 if (!cv || cv == _down_view) {
676 /* Snap starts to ends */
677 DCPTime const d = DCPTime (cv->content()->end() - new_position).abs ();
678 if (first || d < nearest_distance) {
679 nearest_distance = d;
680 nearest_new_position = cv->content()->end();
685 /* Snap ends to starts */
686 DCPTime const d = DCPTime (
687 cv->content()->position() - (new_position + _down_view->content()->length_after_trim())
690 if (d < nearest_distance) {
691 nearest_distance = d;
692 nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
700 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
701 if (nearest_distance < DCPTime::from_seconds ((width() / pps) / 32)) {
702 new_position = nearest_new_position;
707 if (new_position < DCPTime ()) {
708 new_position = DCPTime ();
711 _down_view->content()->set_position (new_position);
713 shared_ptr<Film> film = _film.lock ();
715 film->set_sequence_video (false);
719 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
721 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
724 shared_ptr<const Film>
725 Timeline::film () const
727 return _film.lock ();
733 setup_pixels_per_second ();
737 Timeline::clear_selection ()
739 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
740 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
742 cv->set_selected (false);
747 Timeline::ContentViewList
748 Timeline::selected_views () const
752 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
753 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
754 if (cv && cv->selected()) {
763 Timeline::selected_content () const
766 ContentViewList views = selected_views ();
768 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
769 sel.push_back ((*i)->content ());