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>
31 using boost::shared_ptr;
32 using boost::weak_ptr;
33 using boost::dynamic_pointer_cast;
45 void paint (wxGraphicsContext* g)
47 _last_paint_bbox = bbox ();
53 _timeline.force_redraw (_last_paint_bbox);
54 _timeline.force_redraw (bbox ());
57 virtual Rect bbox () const = 0;
60 virtual void do_paint (wxGraphicsContext *) = 0;
62 int time_x (Time t) const
64 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
70 Rect _last_paint_bbox;
73 class ContentView : public View
76 ContentView (Timeline& tl, shared_ptr<Content> c, int t)
82 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2));
87 shared_ptr<const Film> film = _timeline.film ();
88 shared_ptr<const Content> content = _content.lock ();
89 if (!film || !content) {
94 time_x (content->start ()) - 8,
96 content->length () * _timeline.pixels_per_time_unit() + 16,
97 _timeline.track_height() + 16
101 void set_selected (bool s) {
106 bool selected () const {
110 weak_ptr<Content> content () const {
114 virtual wxString type () const = 0;
115 virtual wxColour colour () const = 0;
119 void do_paint (wxGraphicsContext* gc)
121 shared_ptr<const Film> film = _timeline.film ();
122 shared_ptr<const Content> content = _content.lock ();
123 if (!film || !content) {
127 Time const start = content->start ();
128 Time const len = content->length ();
130 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
132 gc->SetPen (*wxBLACK_PEN);
134 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
135 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
137 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
139 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
142 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
144 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
146 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
150 wxGraphicsPath path = gc->CreatePath ();
151 path.MoveToPoint (time_x (start), y_pos (_track) + 4);
152 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
153 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
154 path.AddLineToPoint (time_x (start), y_pos (_track + 1) - 4);
155 path.AddLineToPoint (time_x (start), y_pos (_track) + 4);
156 gc->StrokePath (path);
159 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
161 wxDouble name_height;
162 wxDouble name_descent;
163 wxDouble name_leading;
164 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
166 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
167 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
171 int y_pos (int t) const
173 return _timeline.tracks_position().y + t * _timeline.track_height();
176 void content_changed (int p)
178 if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
183 boost::weak_ptr<Content> _content;
187 boost::signals2::scoped_connection _content_connection;
190 class AudioContentView : public ContentView
193 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
194 : ContentView (tl, c, t)
198 wxString type () const
203 wxColour colour () const
205 return wxColour (149, 121, 232, 255);
209 class VideoContentView : public ContentView
212 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
213 : ContentView (tl, c, t)
218 wxString type () const
223 wxColour colour () const
225 return wxColour (242, 92, 120, 255);
229 class TimeAxisView : public View
232 TimeAxisView (Timeline& tl, int y)
237 void do_paint (wxGraphicsContext* gc)
239 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
240 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
242 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
245 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
246 if (mark_interval > 5) {
247 mark_interval -= mark_interval % 5;
249 if (mark_interval > 10) {
250 mark_interval -= mark_interval % 10;
252 if (mark_interval > 60) {
253 mark_interval -= mark_interval % 60;
255 if (mark_interval > 3600) {
256 mark_interval -= mark_interval % 3600;
259 if (mark_interval < 1) {
263 wxGraphicsPath path = gc->CreatePath ();
264 path.MoveToPoint (_timeline.x_offset(), _y);
265 path.AddLineToPoint (_timeline.width(), _y);
266 gc->StrokePath (path);
269 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
270 wxGraphicsPath path = gc->CreatePath ();
271 path.MoveToPoint (time_x (t), _y - 4);
272 path.AddLineToPoint (time_x (t), _y + 4);
273 gc->StrokePath (path);
275 int tc = t / TIME_HZ;
276 int const h = tc / 3600;
278 int const m = tc / 60;
282 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
285 wxDouble str_descent;
286 wxDouble str_leading;
287 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
289 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
290 if ((tx + str_width) < _timeline.width()) {
291 gc->DrawText (str, time_x (t), _y + 16);
294 t += mark_interval * TIME_HZ;
300 return Rect (0, _y - 4, _timeline.width(), 24);
307 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
308 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
310 , _pixels_per_time_unit (0)
312 , _down_view_start (0)
313 , _first_move (false)
315 SetDoubleBuffered (true);
317 setup_pixels_per_time_unit ();
319 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
320 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
321 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
322 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
323 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
325 SetMinSize (wxSize (640, tracks() * track_height() + 96));
329 _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
333 Timeline::paint (wxPaintEvent &)
337 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
342 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
344 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
352 Timeline::playlist_changed ()
354 shared_ptr<const Film> fl = _film.lock ();
361 Playlist::ContentList content = fl->playlist()->content ();
363 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
364 if (dynamic_pointer_cast<VideoContent> (*i)) {
365 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
367 if (dynamic_pointer_cast<AudioContent> (*i)) {
368 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
372 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
378 Timeline::tracks () const
385 Timeline::setup_pixels_per_time_unit ()
387 shared_ptr<const Film> film = _film.lock ();
392 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
396 Timeline::left_down (wxMouseEvent& ev)
398 list<shared_ptr<View> >::iterator i = _views.begin();
399 Position const p (ev.GetX(), ev.GetY());
400 while (i != _views.end() && !(*i)->bbox().contains (p)) {
406 if (i != _views.end ()) {
407 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
410 shared_ptr<Content> c = cv->content().lock();
412 _down_view_start = c->start ();
416 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
417 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
419 cv->set_selected (i == j);
424 _down_point = ev.GetPosition ();
429 Timeline::left_up (wxMouseEvent &)
435 Timeline::mouse_moved (wxMouseEvent& ev)
441 wxPoint const p = ev.GetPosition();
444 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
451 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
453 shared_ptr<Content> c = _down_view->content().lock();
455 c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
461 Timeline::force_redraw (Rect const & r)
463 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
466 shared_ptr<const Film>
467 Timeline::film () const
469 return _film.lock ();
473 Timeline::resized (wxSizeEvent &)
475 setup_pixels_per_time_unit ();