C++11 tidying.
[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 {
38         _content_connection = c->Change.connect (bind (&TimelineContentView::content_change, this, _1, _3));
39 }
40
41 dcpomatic::Rect<int>
42 TimelineContentView::bbox () const
43 {
44         DCPOMATIC_ASSERT (_track);
45
46         auto film = _timeline.film ();
47         auto content = _content.lock ();
48         if (!film || !content) {
49                 return {};
50         }
51
52         return dcpomatic::Rect<int> (
53                 time_x (content->position ()),
54                 y_pos (_track.get()),
55                 content->length_after_trim(film).seconds() * _timeline.pixels_per_second().get_value_or(0),
56                 _timeline.pixels_per_track()
57                 );
58 }
59
60 void
61 TimelineContentView::set_selected (bool s)
62 {
63         _selected = s;
64         force_redraw ();
65 }
66
67 bool
68 TimelineContentView::selected () const
69 {
70         return _selected;
71 }
72
73 shared_ptr<Content>
74 TimelineContentView::content () const
75 {
76         return _content.lock ();
77 }
78
79 void
80 TimelineContentView::set_track (int t)
81 {
82         _track = t;
83 }
84
85 void
86 TimelineContentView::unset_track ()
87 {
88         _track = boost::optional<int>();
89 }
90
91 boost::optional<int>
92 TimelineContentView::track () const
93 {
94         return _track;
95 }
96
97 void
98 TimelineContentView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int> > overlaps)
99 {
100         DCPOMATIC_ASSERT (_track);
101
102         auto film = _timeline.film ();
103         auto cont = content ();
104         if (!film || !cont) {
105                 return;
106         }
107
108         DCPTime const position = cont->position ();
109         DCPTime const len = cont->length_after_trim (film);
110
111         wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
112
113         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
114         if (_selected) {
115                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
116         } else {
117                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
118         }
119
120         /* Outline */
121         auto path = gc->CreatePath ();
122         path.MoveToPoint    (time_x (position) + 2,           y_pos (_track.get()) + 4);
123         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get()) + 4);
124         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get() + 1) - 4);
125         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get() + 1) - 4);
126         path.AddLineToPoint (time_x (position) + 2,           y_pos (_track.get()) + 4);
127         gc->StrokePath (path);
128         gc->FillPath (path);
129
130         /* Reel split points */
131         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 1, wxPENSTYLE_DOT));
132         for (auto i: cont->reel_split_points(film)) {
133                 path = gc->CreatePath ();
134                 path.MoveToPoint (time_x (i), y_pos (_track.get()) + 4);
135                 path.AddLineToPoint (time_x (i), y_pos (_track.get() + 1) - 4);
136                 gc->StrokePath (path);
137         }
138
139         /* Overlaps */
140         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (foreground_colour(), wxBRUSHSTYLE_CROSSDIAG_HATCH));
141         for (auto const& i: overlaps) {
142                 gc->DrawRectangle (i.x, i.y + 4, i.width, i.height - 8);
143         }
144
145         /* Label text */
146         auto lab = label ();
147         wxDouble lab_width;
148         wxDouble lab_height;
149         wxDouble lab_descent;
150         wxDouble lab_leading;
151         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
152         gc->GetTextExtent (lab, &lab_width, &lab_height, &lab_descent, &lab_leading);
153         gc->PushState ();
154         gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.pixels_per_track()));
155         gc->DrawText (lab, time_x (position) + 12, y_pos (_track.get() + 1) - lab_height - 4);
156         gc->PopState ();
157 }
158
159 int
160 TimelineContentView::y_pos (int t) const
161 {
162         return t * _timeline.pixels_per_track() + _timeline.tracks_y_offset();
163 }
164
165 void
166 TimelineContentView::content_change (ChangeType type, int p)
167 {
168         if (type != ChangeType::DONE) {
169                 return;
170         }
171
172         ensure_ui_thread ();
173
174         if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
175                 force_redraw ();
176         }
177 }
178
179 wxString
180 TimelineContentView::label () const
181 {
182         return std_to_wx(content()->summary());
183 }