Remove some left-over JSON bits.
[dcpomatic.git] / src / wx / timing_panel.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libdcp/raw_convert.h>
21 #include "lib/content.h"
22 #include "lib/image_content.h"
23 #include "timing_panel.h"
24 #include "wx_util.h"
25 #include "timecode.h"
26 #include "film_editor.h"
27
28 using std::cout;
29 using std::string;
30 using std::set;
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
33 using libdcp::raw_convert;
34
35 TimingPanel::TimingPanel (FilmEditor* e)
36         /* horrid hack for apparent lack of context support with wxWidgets i18n code */
37         : FilmEditorPanel (e, S_("Timing|Timing"))
38 {
39         wxFlexGridSizer* grid = new wxFlexGridSizer (2, 4, 4);
40         _sizer->Add (grid, 0, wxALL, 8);
41
42         add_label_to_sizer (grid, this, _("Position"), true);
43         _position = new Timecode (this);
44         grid->Add (_position);
45         add_label_to_sizer (grid, this, _("Full length"), true);
46         _full_length = new Timecode (this);
47         grid->Add (_full_length);
48         add_label_to_sizer (grid, this, _("Trim from start"), true);
49         _trim_start = new Timecode (this);
50         grid->Add (_trim_start);
51         add_label_to_sizer (grid, this, _("Trim from end"), true);
52         _trim_end = new Timecode (this);
53         grid->Add (_trim_end);
54         add_label_to_sizer (grid, this, _("Play length"), true);
55         _play_length = new Timecode (this);
56         grid->Add (_play_length);
57
58         {
59                 add_label_to_sizer (grid, this, _("Video frame rate"), true);
60                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
61                 _video_frame_rate = new wxTextCtrl (this, wxID_ANY);
62                 s->Add (_video_frame_rate, 1, wxEXPAND);
63                 _set_video_frame_rate = new wxButton (this, wxID_ANY, _("Set"));
64                 _set_video_frame_rate->Enable (false);
65                 s->Add (_set_video_frame_rate, 0, wxLEFT | wxRIGHT, 8);
66                 grid->Add (s, 1, wxEXPAND);
67         }
68
69         _position->Changed.connect    (boost::bind (&TimingPanel::position_changed, this));
70         _full_length->Changed.connect (boost::bind (&TimingPanel::full_length_changed, this));
71         _trim_start->Changed.connect  (boost::bind (&TimingPanel::trim_start_changed, this));
72         _trim_end->Changed.connect    (boost::bind (&TimingPanel::trim_end_changed, this));
73         _play_length->Changed.connect (boost::bind (&TimingPanel::play_length_changed, this));
74         _video_frame_rate->Bind       (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&TimingPanel::video_frame_rate_changed, this));
75         _set_video_frame_rate->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&TimingPanel::set_video_frame_rate, this));
76 }
77
78 void
79 TimingPanel::film_content_changed (int property)
80 {
81         ContentList cl = _editor->selected_content ();
82         int const film_video_frame_rate = _editor->film()->video_frame_rate ();
83
84         /* Here we check to see if we have exactly one different value of various
85            properties, and fill the controls with that value if so.
86         */
87         
88         if (property == ContentProperty::POSITION) {
89
90                 set<Time> check;
91                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
92                         check.insert ((*i)->position ());
93                 }
94
95                 if (check.size() == 1) {
96                         _position->set (cl.front()->position(), film_video_frame_rate);
97                 } else {
98                         _position->clear ();
99                 }
100                 
101         } else if (
102                 property == ContentProperty::LENGTH ||
103                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
104                 property == VideoContentProperty::VIDEO_FRAME_TYPE
105                 ) {
106
107                 set<Time> check;
108                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
109                         check.insert ((*i)->full_length ());
110                 }
111                 
112                 if (check.size() == 1) {
113                         _full_length->set (cl.front()->full_length (), film_video_frame_rate);
114                 } else {
115                         _full_length->clear ();
116                 }
117
118         } else if (property == ContentProperty::TRIM_START) {
119
120                 set<Time> check;
121                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
122                         check.insert ((*i)->trim_start ());
123                 }
124                 
125                 if (check.size() == 1) {
126                         _trim_start->set (cl.front()->trim_start (), film_video_frame_rate);
127                 } else {
128                         _trim_start->clear ();
129                 }
130                 
131         } else if (property == ContentProperty::TRIM_END) {
132
133                 set<Time> check;
134                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
135                         check.insert ((*i)->trim_end ());
136                 }
137                 
138                 if (check.size() == 1) {
139                         _trim_end->set (cl.front()->trim_end (), film_video_frame_rate);
140                 } else {
141                         _trim_end->set (0, 24);
142                 }
143         }
144
145         if (
146                 property == ContentProperty::LENGTH ||
147                 property == ContentProperty::TRIM_START ||
148                 property == ContentProperty::TRIM_END ||
149                 property == VideoContentProperty::VIDEO_FRAME_RATE ||
150                 property == VideoContentProperty::VIDEO_FRAME_TYPE
151                 ) {
152
153                 set<Time> check;
154                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
155                         check.insert ((*i)->length_after_trim ());
156                 }
157                 
158                 if (check.size() == 1) {
159                         _play_length->set (cl.front()->length_after_trim (), film_video_frame_rate);
160                 } else {
161                         _play_length->clear ();
162                 }
163         }
164
165         if (property == VideoContentProperty::VIDEO_FRAME_RATE) {
166                 set<float> check;
167                 shared_ptr<VideoContent> vc;
168                 for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
169                         vc = dynamic_pointer_cast<VideoContent> (*i);
170                         if (vc) {
171                                 check.insert (vc->video_frame_rate ());
172                         }
173                 }
174                 if (check.size() == 1) {
175                         _video_frame_rate->SetValue (std_to_wx (raw_convert<string> (vc->video_frame_rate (), 5)));
176                         _video_frame_rate->Enable (true);
177                 } else {
178                         _video_frame_rate->SetValue ("");
179                         _video_frame_rate->Enable (false);
180                 }
181         }
182
183         bool have_still = false;
184         for (ContentList::const_iterator i = cl.begin (); i != cl.end(); ++i) {
185                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
186                 if (ic && ic->still ()) {
187                         have_still = true;
188                 }
189         }
190
191         _full_length->set_editable (have_still);
192         _play_length->set_editable (!have_still);
193         _set_video_frame_rate->Enable (false);
194 }
195
196 void
197 TimingPanel::position_changed ()
198 {
199         ContentList c = _editor->selected_content ();
200         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
201                 (*i)->set_position (_position->get (_editor->film()->video_frame_rate ()));
202         }
203 }
204
205 void
206 TimingPanel::full_length_changed ()
207 {
208         ContentList c = _editor->selected_content ();
209         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
210                 shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
211                 if (ic && ic->still ()) {
212                         ic->set_video_length (rint (_full_length->get (_editor->film()->video_frame_rate()) * ic->video_frame_rate() / TIME_HZ));
213                 }
214         }
215 }
216
217 void
218 TimingPanel::trim_start_changed ()
219 {
220         ContentList c = _editor->selected_content ();
221         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
222                 (*i)->set_trim_start (_trim_start->get (_editor->film()->video_frame_rate ()));
223         }
224 }
225
226
227 void
228 TimingPanel::trim_end_changed ()
229 {
230         ContentList c = _editor->selected_content ();
231         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
232                 (*i)->set_trim_end (_trim_end->get (_editor->film()->video_frame_rate ()));
233         }
234 }
235
236 void
237 TimingPanel::play_length_changed ()
238 {
239         ContentList c = _editor->selected_content ();
240         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
241                 (*i)->set_trim_end ((*i)->full_length() - _play_length->get (_editor->film()->video_frame_rate()) - (*i)->trim_start());
242         }
243 }
244
245 void
246 TimingPanel::video_frame_rate_changed ()
247 {
248         _set_video_frame_rate->Enable (true);
249 }
250
251 void
252 TimingPanel::set_video_frame_rate ()
253 {
254         ContentList c = _editor->selected_content ();
255         for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
256                 shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
257                 if (vc) {
258                         vc->set_video_frame_rate (raw_convert<float> (wx_to_std (_video_frame_rate->GetValue ())));
259                 }
260                 _set_video_frame_rate->Enable (false);
261         }
262 }
263
264 void
265 TimingPanel::content_selection_changed ()
266 {
267         bool const e = !_editor->selected_content().empty ();
268
269         _position->Enable (e);
270         _full_length->Enable (e);
271         _trim_start->Enable (e);
272         _trim_end->Enable (e);
273         _play_length->Enable (e);
274         _video_frame_rate->Enable (e);
275         
276         film_content_changed (ContentProperty::POSITION);
277         film_content_changed (ContentProperty::LENGTH);
278         film_content_changed (ContentProperty::TRIM_START);
279         film_content_changed (ContentProperty::TRIM_END);
280         film_content_changed (VideoContentProperty::VIDEO_FRAME_RATE);
281 }