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"
28 #include "repeat_dialog.h"
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
38 class View : public boost::noncopyable
47 void paint (wxGraphicsContext* g)
49 _last_paint_bbox = bbox ();
55 _timeline.force_redraw (_last_paint_bbox);
56 _timeline.force_redraw (bbox ());
59 virtual dcpomatic::Rect<int> bbox () const = 0;
62 virtual void do_paint (wxGraphicsContext *) = 0;
64 int time_x (Time t) const
66 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
72 dcpomatic::Rect<int> _last_paint_bbox;
75 class ContentView : public View
78 ContentView (Timeline& tl, shared_ptr<Content> c, int t)
84 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2));
87 dcpomatic::Rect<int> bbox () const
89 shared_ptr<const Film> film = _timeline.film ();
91 return dcpomatic::Rect<int> ();
94 return dcpomatic::Rect<int> (
95 time_x (_content->start ()) - 8,
97 _content->length () * _timeline.pixels_per_time_unit() + 16,
98 _timeline.track_height() + 16
102 void set_selected (bool s) {
107 bool selected () const {
111 shared_ptr<Content> content () const {
115 void set_track (int t) {
123 virtual wxString type () const = 0;
124 virtual wxColour colour () const = 0;
128 void do_paint (wxGraphicsContext* gc)
130 shared_ptr<const Film> film = _timeline.film ();
135 Time const start = _content->start ();
136 Time const len = _content->length ();
138 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
140 gc->SetPen (*wxBLACK_PEN);
142 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
144 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
146 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
149 wxGraphicsPath path = gc->CreatePath ();
150 path.MoveToPoint (time_x (start), y_pos (_track) + 4);
151 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
152 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
153 path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4);
154 path.AddLineToPoint (time_x (start), y_pos (_track) + 4);
155 gc->StrokePath (path);
158 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (_content->file().filename().string()).data(), type().data());
160 wxDouble name_height;
161 wxDouble name_descent;
162 wxDouble name_leading;
163 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
165 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
166 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
170 int y_pos (int t) const
172 return _timeline.tracks_position().y + t * _timeline.track_height();
175 void content_changed (int p)
177 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
182 /* This must be a shared_ptr, not a weak_ptr, as in the looped case this
183 will be the only remaining pointer to the looped content that we get
186 boost::shared_ptr<Content> _content;
190 boost::signals2::scoped_connection _content_connection;
193 class AudioContentView : public ContentView
196 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
197 : ContentView (tl, c, t)
201 wxString type () const
206 wxColour colour () const
208 return wxColour (149, 121, 232, 255);
212 class VideoContentView : public ContentView
215 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
216 : ContentView (tl, c, t)
221 wxString type () const
223 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
230 wxColour colour () const
232 return wxColour (242, 92, 120, 255);
236 class TimeAxisView : public View
239 TimeAxisView (Timeline& tl, int y)
244 dcpomatic::Rect<int> bbox () const
246 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
257 void do_paint (wxGraphicsContext* gc)
259 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
261 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
262 if (mark_interval > 5) {
263 mark_interval -= mark_interval % 5;
265 if (mark_interval > 10) {
266 mark_interval -= mark_interval % 10;
268 if (mark_interval > 60) {
269 mark_interval -= mark_interval % 60;
271 if (mark_interval > 3600) {
272 mark_interval -= mark_interval % 3600;
275 if (mark_interval < 1) {
279 wxGraphicsPath path = gc->CreatePath ();
280 path.MoveToPoint (_timeline.x_offset(), _y);
281 path.AddLineToPoint (_timeline.width(), _y);
282 gc->StrokePath (path);
285 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
286 wxGraphicsPath path = gc->CreatePath ();
287 path.MoveToPoint (time_x (t), _y - 4);
288 path.AddLineToPoint (time_x (t), _y + 4);
289 gc->StrokePath (path);
291 int tc = t / TIME_HZ;
292 int const h = tc / 3600;
294 int const m = tc / 60;
298 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
301 wxDouble str_descent;
302 wxDouble str_leading;
303 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
305 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
306 if ((tx + str_width) < _timeline.width()) {
307 gc->DrawText (str, time_x (t), _y + 16);
310 t += mark_interval * TIME_HZ;
322 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
323 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
326 , _time_axis_view (new TimeAxisView (*this, 32))
328 , _pixels_per_time_unit (0)
330 , _down_view_start (0)
331 , _first_move (false)
335 SetDoubleBuffered (true);
338 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
339 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
340 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
341 Connect (wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler (Timeline::right_down), 0, this);
342 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
343 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
344 Connect (ID_repeat, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Timeline::repeat), 0, this);
348 SetMinSize (wxSize (640, tracks() * track_height() + 96));
350 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
354 Timeline::paint (wxPaintEvent &)
358 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
363 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
365 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
373 Timeline::playlist_changed ()
375 shared_ptr<const Film> fl = _film.lock ();
381 _views.push_back (_time_axis_view);
383 Playlist::ContentList content = fl->playlist()->content ();
385 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
386 if (dynamic_pointer_cast<VideoContent> (*i)) {
387 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
389 if (dynamic_pointer_cast<AudioContent> (*i)) {
390 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
395 setup_pixels_per_time_unit ();
400 Timeline::assign_tracks ()
402 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
403 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
410 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
411 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
416 shared_ptr<Content> acv_content = acv->content();
420 list<shared_ptr<View> >::iterator j = _views.begin();
421 while (j != _views.end()) {
422 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
428 shared_ptr<Content> test_content = test->content();
430 if (test && test->track() == t) {
431 if ((acv_content->start() < test_content->start() && test_content->start() < acv_content->end()) ||
432 (acv_content->start() < test_content->end() && test_content->end() < acv_content->end())) {
433 /* we have an overlap on track `t' */
442 if (j == _views.end ()) {
443 /* no overlap on `t' */
449 _tracks = max (_tracks, t + 1);
452 _time_axis_view->set_y (tracks() * track_height() + 32);
456 Timeline::tracks () const
462 Timeline::setup_pixels_per_time_unit ()
464 shared_ptr<const Film> film = _film.lock ();
469 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
473 Timeline::event_to_view (wxMouseEvent& ev)
475 list<shared_ptr<View> >::iterator i = _views.begin();
476 Position<int> const p (ev.GetX(), ev.GetY());
477 while (i != _views.end() && !(*i)->bbox().contains (p)) {
481 if (i == _views.end ()) {
482 return shared_ptr<View> ();
489 Timeline::left_down (wxMouseEvent& ev)
491 shared_ptr<View> view = event_to_view (ev);
496 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
499 _down_view_start = cv->content()->start ();
503 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
504 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
506 cv->set_selected (view == *i);
508 _film_editor->set_selection (cv->content ());
514 _down_point = ev.GetPosition ();
518 _down_view->content()->set_change_signals_frequent (true);
523 Timeline::left_up (wxMouseEvent& ev)
528 _down_view->content()->set_change_signals_frequent (false);
531 set_start_from_event (ev);
535 Timeline::mouse_moved (wxMouseEvent& ev)
541 set_start_from_event (ev);
545 Timeline::right_down (wxMouseEvent& ev)
547 shared_ptr<View> view = event_to_view (ev);
548 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
553 if (!cv->selected ()) {
555 cv->set_selected (true);
560 _menu->Append (ID_repeat, _("Repeat..."));
563 PopupMenu (_menu, ev.GetPosition ());
567 Timeline::set_start_from_event (wxMouseEvent& ev)
569 wxPoint const p = ev.GetPosition();
572 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
579 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
581 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
583 shared_ptr<Film> film = _film.lock ();
585 film->set_sequence_video (false);
590 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
592 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
595 shared_ptr<const Film>
596 Timeline::film () const
598 return _film.lock ();
602 Timeline::resized (wxSizeEvent &)
604 setup_pixels_per_time_unit ();
608 Timeline::clear_selection ()
610 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
611 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
613 cv->set_selected (false);
619 Timeline::repeat (wxCommandEvent &)
621 shared_ptr<ContentView> sel = selected ();
626 RepeatDialog d (this);
629 shared_ptr<const Film> film = _film.lock ();
634 film->playlist()->repeat (sel->content (), d.number ());
638 shared_ptr<ContentView>
639 Timeline::selected () const
641 for (list<shared_ptr<View> >::const_iterator i = _views.begin(); i != _views.end(); ++i) {
642 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
643 if (cv && cv->selected()) {
648 return shared_ptr<ContentView> ();