2 Copyright (C) 2013 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 "film_editor.h"
32 using boost::shared_ptr;
33 using boost::weak_ptr;
34 using boost::dynamic_pointer_cast;
36 using boost::optional;
38 /** Parent class for components of the timeline (e.g. a piece of content or an axis) */
39 class View : public boost::noncopyable
50 void paint (wxGraphicsContext* g)
52 _last_paint_bbox = bbox ();
58 _timeline.force_redraw (_last_paint_bbox);
59 _timeline.force_redraw (bbox ());
62 virtual dcpomatic::Rect<int> bbox () const = 0;
65 virtual void do_paint (wxGraphicsContext *) = 0;
67 int time_x (Time t) const
69 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
75 dcpomatic::Rect<int> _last_paint_bbox;
78 class ContentView : public View
81 ContentView (Timeline& tl, shared_ptr<Content> c)
87 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
90 dcpomatic::Rect<int> bbox () const
92 shared_ptr<const Film> film = _timeline.film ();
93 shared_ptr<const Content> content = _content.lock ();
94 if (!film || !content) {
95 return dcpomatic::Rect<int> ();
98 return dcpomatic::Rect<int> (
99 time_x (content->position ()) - 8,
101 content->length_after_trim () * _timeline.pixels_per_time_unit() + 16,
102 _timeline.track_height() + 16
106 void set_selected (bool s) {
111 bool selected () const {
115 shared_ptr<Content> content () const {
116 return _content.lock ();
119 void set_track (int t) {
127 virtual wxString type () const = 0;
128 virtual wxColour colour () const = 0;
132 void do_paint (wxGraphicsContext* gc)
134 shared_ptr<const Film> film = _timeline.film ();
135 shared_ptr<const Content> cont = content ();
136 if (!film || !cont) {
140 Time const position = cont->position ();
141 Time const len = cont->length_after_trim ();
143 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
145 gc->SetPen (*wxBLACK_PEN);
147 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
149 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
151 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
154 wxGraphicsPath path = gc->CreatePath ();
155 path.MoveToPoint (time_x (position), y_pos (_track) + 4);
156 path.AddLineToPoint (time_x (position + len), y_pos (_track) + 4);
157 path.AddLineToPoint (time_x (position + len), y_pos (_track + 1) - 4);
158 path.AddLineToPoint (time_x (position), y_pos (_track + 1) - 4);
159 path.AddLineToPoint (time_x (position), y_pos (_track) + 4);
160 gc->StrokePath (path);
163 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path().filename().string()).data(), type().data());
165 wxDouble name_height;
166 wxDouble name_descent;
167 wxDouble name_leading;
168 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
170 gc->Clip (wxRegion (time_x (position), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
171 gc->DrawText (name, time_x (position) + 12, y_pos (_track + 1) - name_height - 4);
175 int y_pos (int t) const
177 return _timeline.tracks_position().y + t * _timeline.track_height();
180 void content_changed (int p, bool frequent)
184 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
189 _timeline.setup_pixels_per_time_unit ();
190 _timeline.Refresh ();
194 boost::weak_ptr<Content> _content;
198 boost::signals2::scoped_connection _content_connection;
201 class AudioContentView : public ContentView
204 AudioContentView (Timeline& tl, shared_ptr<Content> c)
205 : ContentView (tl, c)
209 wxString type () const
214 wxColour colour () const
216 return wxColour (149, 121, 232, 255);
220 class VideoContentView : public ContentView
223 VideoContentView (Timeline& tl, shared_ptr<Content> c)
224 : ContentView (tl, c)
229 wxString type () const
231 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
238 wxColour colour () const
240 return wxColour (242, 92, 120, 255);
244 class TimeAxisView : public View
247 TimeAxisView (Timeline& tl, int y)
252 dcpomatic::Rect<int> bbox () const
254 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
265 void do_paint (wxGraphicsContext* gc)
267 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
269 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
270 if (mark_interval > 5) {
271 mark_interval -= mark_interval % 5;
273 if (mark_interval > 10) {
274 mark_interval -= mark_interval % 10;
276 if (mark_interval > 60) {
277 mark_interval -= mark_interval % 60;
279 if (mark_interval > 3600) {
280 mark_interval -= mark_interval % 3600;
283 if (mark_interval < 1) {
287 wxGraphicsPath path = gc->CreatePath ();
288 path.MoveToPoint (_timeline.x_offset(), _y);
289 path.AddLineToPoint (_timeline.width(), _y);
290 gc->StrokePath (path);
293 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
294 wxGraphicsPath path = gc->CreatePath ();
295 path.MoveToPoint (time_x (t), _y - 4);
296 path.AddLineToPoint (time_x (t), _y + 4);
297 gc->StrokePath (path);
299 int tc = t / TIME_HZ;
300 int const h = tc / 3600;
302 int const m = tc / 60;
306 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
309 wxDouble str_descent;
310 wxDouble str_leading;
311 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
313 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
314 if ((tx + str_width) < _timeline.width()) {
315 gc->DrawText (str, time_x (t), _y + 16);
318 t += mark_interval * TIME_HZ;
326 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
327 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
330 , _time_axis_view (new TimeAxisView (*this, 32))
332 , _pixels_per_time_unit (0)
334 , _down_view_position (0)
335 , _first_move (false)
340 SetDoubleBuffered (true);
343 Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this));
344 Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1));
345 Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1));
346 Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1));
347 Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1));
348 Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this));
352 SetMinSize (wxSize (640, tracks() * track_height() + 96));
354 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
362 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
367 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
369 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
377 Timeline::playlist_changed ()
381 shared_ptr<const Film> fl = _film.lock ();
387 _views.push_back (_time_axis_view);
389 ContentList content = fl->playlist()->content ();
391 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
392 if (dynamic_pointer_cast<VideoContent> (*i)) {
393 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
395 if (dynamic_pointer_cast<AudioContent> (*i)) {
396 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
401 setup_pixels_per_time_unit ();
406 Timeline::assign_tracks ()
408 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
409 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
416 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
417 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
422 shared_ptr<Content> acv_content = acv->content();
426 ViewList::iterator j = _views.begin();
427 while (j != _views.end()) {
428 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
434 shared_ptr<Content> test_content = test->content();
436 if (test && test->track() == t) {
437 bool const no_overlap =
438 (acv_content->position() < test_content->position() && acv_content->end() < test_content->position()) ||
439 (acv_content->position() > test_content->end() && acv_content->end() > test_content->end());
442 /* we have an overlap on track `t' */
451 if (j == _views.end ()) {
452 /* no overlap on `t' */
458 _tracks = max (_tracks, t + 1);
461 _time_axis_view->set_y (tracks() * track_height() + 32);
465 Timeline::tracks () const
471 Timeline::setup_pixels_per_time_unit ()
473 shared_ptr<const Film> film = _film.lock ();
478 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
482 Timeline::event_to_view (wxMouseEvent& ev)
484 ViewList::iterator i = _views.begin();
485 Position<int> const p (ev.GetX(), ev.GetY());
486 while (i != _views.end() && !(*i)->bbox().contains (p)) {
490 if (i == _views.end ()) {
491 return shared_ptr<View> ();
498 Timeline::left_down (wxMouseEvent& ev)
500 shared_ptr<View> view = event_to_view (ev);
501 shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
506 _down_view = content_view;
507 _down_view_position = content_view->content()->position ();
510 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
511 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
516 if (!ev.ShiftDown ()) {
517 cv->set_selected (view == *i);
521 _film_editor->set_selection (cv->content ());
525 if (content_view && ev.ShiftDown ()) {
526 content_view->set_selected (!content_view->selected ());
530 _down_point = ev.GetPosition ();
534 _down_view->content()->set_change_signals_frequent (true);
539 Timeline::left_up (wxMouseEvent& ev)
544 _down_view->content()->set_change_signals_frequent (false);
547 set_position_from_event (ev);
551 Timeline::mouse_moved (wxMouseEvent& ev)
557 set_position_from_event (ev);
561 Timeline::right_down (wxMouseEvent& ev)
563 shared_ptr<View> view = event_to_view (ev);
564 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
569 if (!cv->selected ()) {
571 cv->set_selected (true);
574 _menu.popup (selected_content (), ev.GetPosition ());
578 Timeline::set_position_from_event (wxMouseEvent& ev)
580 wxPoint const p = ev.GetPosition();
583 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
584 before the drag is considered to have started.
586 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
594 Time new_position = _down_view_position + (p.x - _down_point.x) / _pixels_per_time_unit;
599 Time nearest_distance = TIME_MAX;
600 Time nearest_new_position = TIME_MAX;
602 /* Find the nearest content edge; this is inefficient */
603 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
604 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
605 if (!cv || cv == _down_view) {
610 /* Snap starts to ends */
611 Time const d = abs (cv->content()->end() - new_position);
612 if (first || d < nearest_distance) {
613 nearest_distance = d;
614 nearest_new_position = cv->content()->end();
619 /* Snap ends to starts */
620 Time const d = abs (cv->content()->position() - (new_position + _down_view->content()->length_after_trim()));
621 if (d < nearest_distance) {
622 nearest_distance = d;
623 nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
631 /* Snap if it's close; `close' means within a proportion of the time on the timeline */
632 if (nearest_distance < (width() / pixels_per_time_unit()) / 32) {
633 new_position = nearest_new_position;
638 if (new_position < 0) {
642 _down_view->content()->set_position (new_position);
644 shared_ptr<Film> film = _film.lock ();
646 film->set_sequence_video (false);
651 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
653 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
656 shared_ptr<const Film>
657 Timeline::film () const
659 return _film.lock ();
665 setup_pixels_per_time_unit ();
669 Timeline::clear_selection ()
671 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
672 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
674 cv->set_selected (false);
679 Timeline::ContentViewList
680 Timeline::selected_views () const
684 for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
685 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
686 if (cv && cv->selected()) {
695 Timeline::selected_content () const
698 ContentViewList views = selected_views ();
700 for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
701 sel.push_back ((*i)->content ());