X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fwx%2Ftimeline.cc;h=d728bd47c11349b5dea1ed079e0b6cefb3c397d3;hb=94201bd2a5a4cb391b7f2bdeba56b928fed7cfe1;hp=f9205fc5d74264dc7315a7abd1b27b95b21ea2e2;hpb=3d9fdcf7e6a5d775a2688a071b69264b1a6971c7;p=dcpomatic.git diff --git a/src/wx/timeline.cc b/src/wx/timeline.cc index f9205fc5d..d728bd47c 100644 --- a/src/wx/timeline.cc +++ b/src/wx/timeline.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,10 +22,10 @@ #include #include "lib/film.h" #include "lib/playlist.h" +#include "lib/image_content.h" #include "film_editor.h" #include "timeline.h" #include "wx_util.h" -#include "repeat_dialog.h" using std::list; using std::cout; @@ -34,7 +34,11 @@ using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; using boost::bind; +using boost::optional; +/** @class View + * @brief Parent class for components of the timeline (e.g. a piece of content or an axis). + */ class View : public boost::noncopyable { public: @@ -43,6 +47,8 @@ public: { } + + virtual ~View () {} void paint (wxGraphicsContext* g) { @@ -61,9 +67,9 @@ public: protected: virtual void do_paint (wxGraphicsContext *) = 0; - int time_x (Time t) const + int time_x (DCPTime t) const { - return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit(); + return _timeline.tracks_position().x + t.seconds() * _timeline.pixels_per_second().get_value_or (0); } Timeline& _timeline; @@ -72,29 +78,35 @@ private: dcpomatic::Rect _last_paint_bbox; }; + +/** @class ContentView + * @brief Parent class for views of pieces of content. + */ class ContentView : public View { public: - ContentView (Timeline& tl, shared_ptr c, int t) + ContentView (Timeline& tl, shared_ptr c) : View (tl) , _content (c) - , _track (t) , _selected (false) { - _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2)); + _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3)); } dcpomatic::Rect bbox () const { + assert (_track); + shared_ptr film = _timeline.film (); - if (!film) { + shared_ptr content = _content.lock (); + if (!film || !content) { return dcpomatic::Rect (); } return dcpomatic::Rect ( - time_x (_content->start ()) - 8, - y_pos (_track) - 8, - _content->length () * _timeline.pixels_per_time_unit() + 16, + time_x (content->position ()) - 8, + y_pos (_track.get()) - 8, + content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0) + 16, _timeline.track_height() + 16 ); } @@ -109,61 +121,68 @@ public: } shared_ptr content () const { - return _content; + return _content.lock (); } void set_track (int t) { _track = t; } - int track () const { + void unset_track () { + _track = boost::optional (); + } + + optional track () const { return _track; } virtual wxString type () const = 0; - virtual wxColour colour () const = 0; + virtual wxColour background_colour () const = 0; + virtual wxColour foreground_colour () const = 0; private: void do_paint (wxGraphicsContext* gc) { + assert (_track); + shared_ptr film = _timeline.film (); - if (!film) { + shared_ptr cont = content (); + if (!film || !cont) { return; } - Time const start = _content->start (); - Time const len = _content->length (); + DCPTime const position = cont->position (); + DCPTime const len = cont->length_after_trim (); - wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2); + wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2); - gc->SetPen (*wxBLACK_PEN); - - gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID)); + gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID)); if (_selected) { gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID)); } else { - gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID)); + gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID)); } wxGraphicsPath path = gc->CreatePath (); - path.MoveToPoint (time_x (start), y_pos (_track) + 4); - path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4); - path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4); - path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4); - path.AddLineToPoint (time_x (start), y_pos (_track) + 4); + path.MoveToPoint (time_x (position), y_pos (_track.get()) + 4); + path.AddLineToPoint (time_x (position + len), y_pos (_track.get()) + 4); + path.AddLineToPoint (time_x (position + len), y_pos (_track.get() + 1) - 4); + path.AddLineToPoint (time_x (position), y_pos (_track.get() + 1) - 4); + path.AddLineToPoint (time_x (position), y_pos (_track.get()) + 4); gc->StrokePath (path); gc->FillPath (path); - wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (_content->file().filename().string()).data(), type().data()); + wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->summary()).data(), type().data()); wxDouble name_width; wxDouble name_height; wxDouble name_descent; wxDouble name_leading; gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading); - gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height())); - gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4); + gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height())); + gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ())); + gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4); gc->ResetClip (); } @@ -172,19 +191,22 @@ private: return _timeline.tracks_position().y + t * _timeline.track_height(); } - void content_changed (int p) + void content_changed (int p, bool frequent) { - if (p == ContentProperty::START || p == ContentProperty::LENGTH) { + ensure_ui_thread (); + + if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) { force_redraw (); } + + if (!frequent) { + _timeline.setup_pixels_per_second (); + _timeline.Refresh (); + } } - /* This must be a shared_ptr, not a weak_ptr, as in the looped case this - will be the only remaining pointer to the looped content that we get - from the playlist. - */ - boost::shared_ptr _content; - int _track; + boost::weak_ptr _content; + optional _track; bool _selected; boost::signals2::scoped_connection _content_connection; @@ -193,8 +215,8 @@ private: class AudioContentView : public ContentView { public: - AudioContentView (Timeline& tl, shared_ptr c, int t) - : ContentView (tl, c, t) + AudioContentView (Timeline& tl, shared_ptr c) + : ContentView (tl, c) {} private: @@ -203,34 +225,81 @@ private: return _("audio"); } - wxColour colour () const + wxColour background_colour () const { return wxColour (149, 121, 232, 255); } + + wxColour foreground_colour () const + { + return wxColour (0, 0, 0, 255); + } }; class VideoContentView : public ContentView { public: - VideoContentView (Timeline& tl, shared_ptr c, int t) - : ContentView (tl, c, t) + VideoContentView (Timeline& tl, shared_ptr c) + : ContentView (tl, c) {} private: wxString type () const { - if (dynamic_pointer_cast (content ())) { - return _("video"); - } else { + if (dynamic_pointer_cast (content ()) && content()->number_of_paths() == 1) { return _("still"); + } else { + return _("video"); } } - wxColour colour () const + wxColour background_colour () const { return wxColour (242, 92, 120, 255); } + + wxColour foreground_colour () const + { + return wxColour (0, 0, 0, 255); + } +}; + +class SubtitleContentView : public ContentView +{ +public: + SubtitleContentView (Timeline& tl, shared_ptr c) + : ContentView (tl, c) + , _subtitle_content (c) + {} + +private: + wxString type () const + { + return _("subtitles"); + } + + wxColour background_colour () const + { + shared_ptr sc = _subtitle_content.lock (); + if (!sc || !sc->use_subtitles ()) { + return wxColour (210, 210, 210, 128); + } + + return wxColour (163, 255, 154, 255); + } + + wxColour foreground_colour () const + { + shared_ptr sc = _subtitle_content.lock (); + if (!sc || !sc->use_subtitles ()) { + return wxColour (180, 180, 180, 128); + } + + return wxColour (0, 0, 0, 255); + } + + boost::weak_ptr _subtitle_content; }; class TimeAxisView : public View @@ -256,20 +325,26 @@ private: void do_paint (wxGraphicsContext* gc) { + if (!_timeline.pixels_per_second()) { + return; + } + + double const pps = _timeline.pixels_per_second().get (); + gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID)); - int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ())); + double mark_interval = rint (128 / pps); if (mark_interval > 5) { - mark_interval -= mark_interval % 5; + mark_interval -= int (rint (mark_interval)) % 5; } if (mark_interval > 10) { - mark_interval -= mark_interval % 10; + mark_interval -= int (rint (mark_interval)) % 10; } if (mark_interval > 60) { - mark_interval -= mark_interval % 60; + mark_interval -= int (rint (mark_interval)) % 60; } if (mark_interval > 3600) { - mark_interval -= mark_interval % 3600; + mark_interval -= int (rint (mark_interval)) % 3600; } if (mark_interval < 1) { @@ -281,14 +356,17 @@ private: path.AddLineToPoint (_timeline.width(), _y); gc->StrokePath (path); - Time t = 0; - while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) { + gc->SetFont (gc->CreateFont (*wxNORMAL_FONT)); + + /* Time in seconds */ + DCPTime t; + while ((t.seconds() * pps) < _timeline.width()) { wxGraphicsPath path = gc->CreatePath (); path.MoveToPoint (time_x (t), _y - 4); path.AddLineToPoint (time_x (t), _y + 4); gc->StrokePath (path); - int tc = t / TIME_HZ; + double tc = t.seconds (); int const h = tc / 3600; tc -= h * 3600; int const m = tc / 60; @@ -302,12 +380,12 @@ private: wxDouble str_leading; gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading); - int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit(); + int const tx = _timeline.x_offset() + t.seconds() * pps; if ((tx + str_width) < _timeline.width()) { gc->DrawText (str, time_x (t), _y + 16); } - t += mark_interval * TIME_HZ; + t += DCPTime::from_seconds (mark_interval); } } @@ -315,43 +393,40 @@ private: int _y; }; -enum { - ID_repeat -}; -Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr film) +Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr film) : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE) - , _film_editor (ed) + , _content_panel (cp) , _film (film) , _time_axis_view (new TimeAxisView (*this, 32)) , _tracks (0) - , _pixels_per_time_unit (0) , _left_down (false) - , _down_view_start (0) + , _down_view_position (0) , _first_move (false) - , _menu (0) + , _menu (this) + , _snap (true) { #ifndef __WXOSX__ SetDoubleBuffered (true); #endif - Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this); - Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this); - Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this); - Connect (wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler (Timeline::right_down), 0, this); - Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this); - Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this); - Connect (ID_repeat, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Timeline::repeat), 0, this); + Bind (wxEVT_PAINT, boost::bind (&Timeline::paint, this)); + Bind (wxEVT_LEFT_DOWN, boost::bind (&Timeline::left_down, this, _1)); + Bind (wxEVT_LEFT_UP, boost::bind (&Timeline::left_up, this, _1)); + Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down, this, _1)); + Bind (wxEVT_MOTION, boost::bind (&Timeline::mouse_moved, this, _1)); + Bind (wxEVT_SIZE, boost::bind (&Timeline::resized, this)); playlist_changed (); SetMinSize (wxSize (640, tracks() * track_height() + 96)); - _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this)); + _playlist_changed_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this)); + _playlist_content_changed_connection = film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_content_changed, this, _2)); } void -Timeline::paint (wxPaintEvent &) +Timeline::paint () { wxPaintDC dc (this); @@ -360,9 +435,7 @@ Timeline::paint (wxPaintEvent &) return; } - gc->SetFont (gc->CreateFont (*wxNORMAL_FONT)); - - for (list >::iterator i = _views.begin(); i != _views.end(); ++i) { + for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) { (*i)->paint (gc); } @@ -372,6 +445,8 @@ Timeline::paint (wxPaintEvent &) void Timeline::playlist_changed () { + ensure_ui_thread (); + shared_ptr fl = _film.lock (); if (!fl) { return; @@ -380,46 +455,62 @@ Timeline::playlist_changed () _views.clear (); _views.push_back (_time_axis_view); - Playlist::ContentList content = fl->playlist()->content (); + ContentList content = fl->playlist()->content (); - for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) { + for (ContentList::iterator i = content.begin(); i != content.end(); ++i) { if (dynamic_pointer_cast (*i)) { - _views.push_back (shared_ptr (new VideoContentView (*this, *i, 0))); + _views.push_back (shared_ptr (new VideoContentView (*this, *i))); } if (dynamic_pointer_cast (*i)) { - _views.push_back (shared_ptr (new AudioContentView (*this, *i, 0))); + _views.push_back (shared_ptr (new AudioContentView (*this, *i))); + } + + shared_ptr sc = dynamic_pointer_cast (*i); + if (sc && sc->has_subtitles ()) { + _views.push_back (shared_ptr (new SubtitleContentView (*this, sc))); } } assign_tracks (); - setup_pixels_per_time_unit (); + setup_pixels_per_second (); Refresh (); } +void +Timeline::playlist_content_changed (int property) +{ + ensure_ui_thread (); + + if (property == ContentProperty::POSITION) { + assign_tracks (); + setup_pixels_per_second (); + Refresh (); + } +} + void Timeline::assign_tracks () { - for (list >::iterator i = _views.begin(); i != _views.end(); ++i) { - shared_ptr cv = dynamic_pointer_cast (*i); - if (cv) { - cv->set_track (0); - _tracks = 1; + for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) { + shared_ptr c = dynamic_pointer_cast (*i); + if (c) { + c->unset_track (); } } - for (list >::iterator i = _views.begin(); i != _views.end(); ++i) { - shared_ptr acv = dynamic_pointer_cast (*i); - if (!acv) { + for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) { + shared_ptr cv = dynamic_pointer_cast (*i); + if (!cv) { continue; } - - shared_ptr acv_content = acv->content(); - - int t = 1; - while (1) { - list >::iterator j = _views.begin(); + + shared_ptr content = cv->content(); + + int t = 0; + while (true) { + ViewList::iterator j = _views.begin(); while (j != _views.end()) { - shared_ptr test = dynamic_pointer_cast (*j); + shared_ptr test = dynamic_pointer_cast (*j); if (!test) { ++j; continue; @@ -427,9 +518,12 @@ Timeline::assign_tracks () shared_ptr test_content = test->content(); - if (test && test->track() == t) { - if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) || - (acv_content->start() < test_content->end() && test_content->end() < acv_content->end())) { + if (test && test->track() && test->track().get() == t) { + bool const no_overlap = + (content->position() < test_content->position() && content->end() < test_content->position()) || + (content->position() > test_content->end() && content->end() > test_content->end()); + + if (!no_overlap) { /* we have an overlap on track `t' */ ++t; break; @@ -445,7 +539,7 @@ Timeline::assign_tracks () } } - acv->set_track (t); + cv->set_track (t); _tracks = max (_tracks, t + 1); } @@ -459,20 +553,20 @@ Timeline::tracks () const } void -Timeline::setup_pixels_per_time_unit () +Timeline::setup_pixels_per_second () { shared_ptr film = _film.lock (); - if (!film) { + if (!film || film->length() == DCPTime ()) { return; } - _pixels_per_time_unit = static_cast(width() - x_offset() * 2) / film->length (); + _pixels_per_second = static_cast(width() - x_offset() * 2) / film->length().seconds (); } shared_ptr Timeline::event_to_view (wxMouseEvent& ev) { - list >::iterator i = _views.begin(); + ViewList::iterator i = _views.begin(); Position const p (ev.GetX(), ev.GetY()); while (i != _views.end() && !(*i)->bbox().contains (p)) { ++i; @@ -495,10 +589,10 @@ Timeline::left_down (wxMouseEvent& ev) if (content_view) { _down_view = content_view; - _down_view_start = content_view->content()->start (); + _down_view_position = content_view->content()->position (); } - for (list >::iterator i = _views.begin(); i != _views.end(); ++i) { + for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) { shared_ptr cv = dynamic_pointer_cast (*i); if (!cv) { continue; @@ -509,7 +603,7 @@ Timeline::left_down (wxMouseEvent& ev) } if (view == *i) { - _film_editor->set_selection (cv->content ()); + _content_panel->set_selection (cv->content ()); } } @@ -535,7 +629,7 @@ Timeline::left_up (wxMouseEvent& ev) _down_view->content()->set_change_signals_frequent (false); } - set_start_from_event (ev); + set_position_from_event (ev); } void @@ -545,7 +639,7 @@ Timeline::mouse_moved (wxMouseEvent& ev) return; } - set_start_from_event (ev); + set_position_from_event (ev); } void @@ -562,20 +656,24 @@ Timeline::right_down (wxMouseEvent& ev) cv->set_selected (true); } - if (!_menu) { - _menu = new wxMenu; - _menu->Append (ID_repeat, _("Repeat...")); - } - - PopupMenu (_menu, ev.GetPosition ()); + _menu.popup (_film, selected_content (), ev.GetPosition ()); } void -Timeline::set_start_from_event (wxMouseEvent& ev) +Timeline::set_position_from_event (wxMouseEvent& ev) { + if (!_pixels_per_second) { + return; + } + + double const pps = _pixels_per_second.get (); + wxPoint const p = ev.GetPosition(); if (!_first_move) { + /* We haven't moved yet; in that case, we must move the mouse some reasonable distance + before the drag is considered to have started. + */ int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2)); if (dist < 8) { return; @@ -583,14 +681,66 @@ Timeline::set_start_from_event (wxMouseEvent& ev) _first_move = true; } - Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit; - if (_down_view) { - _down_view->content()->set_start (max (static_cast