BOOST_FOREACH.
[dcpomatic.git] / src / wx / timeline_content_view.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "timeline_content_view.h"
22 #include "timeline.h"
23 #include "wx_util.h"
24 #include "lib/content.h"
25 #include <wx/graphics.h>
26
27 using std::list;
28 using std::shared_ptr;
29 using namespace dcpomatic;
30 #if BOOST_VERSION >= 106100
31 using namespace boost::placeholders;
32 #endif
33
34 TimelineContentView::TimelineContentView (Timeline& tl, shared_ptr<Content> c)
35         : TimelineView (tl)
36         , _content (c)
37         , _selected (false)
38 {
39         _content_connection = c->Change.connect (bind (&TimelineContentView::content_change, this, _1, _3));
40 }
41
42 dcpomatic::Rect<int>
43 TimelineContentView::bbox () const
44 {
45         DCPOMATIC_ASSERT (_track);
46
47         shared_ptr<const Film> film = _timeline.film ();
48         shared_ptr<const Content> content = _content.lock ();
49         if (!film || !content) {
50                 return dcpomatic::Rect<int> ();
51         }
52
53         return dcpomatic::Rect<int> (
54                 time_x (content->position ()),
55                 y_pos (_track.get()),
56                 content->length_after_trim(film).seconds() * _timeline.pixels_per_second().get_value_or(0),
57                 _timeline.pixels_per_track()
58                 );
59 }
60
61 void
62 TimelineContentView::set_selected (bool s)
63 {
64         _selected = s;
65         force_redraw ();
66 }
67
68 bool
69 TimelineContentView::selected () const
70 {
71         return _selected;
72 }
73
74 shared_ptr<Content>
75 TimelineContentView::content () const
76 {
77         return _content.lock ();
78 }
79
80 void
81 TimelineContentView::set_track (int t)
82 {
83         _track = t;
84 }
85
86 void
87 TimelineContentView::unset_track ()
88 {
89         _track = boost::optional<int> ();
90 }
91
92 boost::optional<int>
93 TimelineContentView::track () const
94 {
95         return _track;
96 }
97
98 void
99 TimelineContentView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int> > overlaps)
100 {
101         DCPOMATIC_ASSERT (_track);
102
103         shared_ptr<const Film> film = _timeline.film ();
104         shared_ptr<const Content> cont = content ();
105         if (!film || !cont) {
106                 return;
107         }
108
109         DCPTime const position = cont->position ();
110         DCPTime const len = cont->length_after_trim (film);
111
112         wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
113
114         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
115         if (_selected) {
116                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
117         } else {
118                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
119         }
120
121         /* Outline */
122         wxGraphicsPath path = gc->CreatePath ();
123         path.MoveToPoint    (time_x (position) + 2,           y_pos (_track.get()) + 4);
124         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get()) + 4);
125         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get() + 1) - 4);
126         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get() + 1) - 4);
127         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get()) + 4);
128         gc->StrokePath (path);
129         gc->FillPath (path);
130
131         /* Reel split points */
132         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 1, wxPENSTYLE_DOT));
133         for (auto i: cont->reel_split_points(film)) {
134                 path = gc->CreatePath ();
135                 path.MoveToPoint (time_x (i), y_pos (_track.get()) + 4);
136                 path.AddLineToPoint (time_x (i), y_pos (_track.get() + 1) - 4);
137                 gc->StrokePath (path);
138         }
139
140         /* Overlaps */
141         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (foreground_colour(), wxBRUSHSTYLE_CROSSDIAG_HATCH));
142         for (list<dcpomatic::Rect<int> >::const_iterator i = overlaps.begin(); i != overlaps.end(); ++i) {
143                 gc->DrawRectangle (i->x, i->y + 4, i->width, i->height - 8);
144         }
145
146         /* Label text */
147         wxString lab = label ();
148         wxDouble lab_width;
149         wxDouble lab_height;
150         wxDouble lab_descent;
151         wxDouble lab_leading;
152         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
153         gc->GetTextExtent (lab, &lab_width, &lab_height, &lab_descent, &lab_leading);
154         gc->PushState ();
155         gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.pixels_per_track()));
156         gc->DrawText (lab, time_x (position) + 12, y_pos (_track.get() + 1) - lab_height - 4);
157         gc->PopState ();
158 }
159
160 int
161 TimelineContentView::y_pos (int t) const
162 {
163         return t * _timeline.pixels_per_track() + _timeline.tracks_y_offset();
164 }
165
166 void
167 TimelineContentView::content_change (ChangeType type, int p)
168 {
169         if (type != CHANGE_TYPE_DONE) {
170                 return;
171         }
172
173         ensure_ui_thread ();
174
175         if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
176                 force_redraw ();
177         }
178 }
179
180 wxString
181 TimelineContentView::label () const
182 {
183         return std_to_wx(content()->summary());
184 }