Don't allow luminance metadata below 0.1 in the GUI (part of #2130).
[dcpomatic.git] / src / wx / metadata_dialog.cc
1 /*
2     Copyright (C) 2021 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
22 #include "dcpomatic_button.h"
23 #include "full_language_tag_dialog.h"
24 #include "language_tag_widget.h"
25 #include "metadata_dialog.h"
26 #include "wx_util.h"
27 #include "lib/film.h"
28 #include <boost/bind.hpp>
29 #include <boost/weak_ptr.hpp>
30 #include <wx/notebook.h>
31 #include <wx/spinctrl.h>
32 #include <wx/wx.h>
33
34
35 using std::weak_ptr;
36
37
38 MetadataDialog::MetadataDialog (wxWindow* parent, weak_ptr<Film> weak_film)
39         : wxDialog (parent, wxID_ANY, _("Metadata"))
40         , WeakFilm (weak_film)
41 {
42
43 }
44
45
46 void
47 MetadataDialog::setup ()
48 {
49         auto notebook = new wxNotebook (this, wxID_ANY);
50
51         auto prepare = [notebook](std::function<void (wxPanel*, wxSizer*)> setup, wxString name) {
52                 auto panel = new wxPanel (notebook, wxID_ANY);
53                 auto sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
54                 sizer->AddGrowableCol (1, 1);
55                 setup (panel, sizer);
56                 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
57                 overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
58                 panel->SetSizer (overall_sizer);
59                 notebook->AddPage (panel, name);
60         };
61
62         prepare (boost::bind(&MetadataDialog::setup_standard, this, _1, _2), _("Standard"));
63         prepare (boost::bind(&MetadataDialog::setup_advanced, this, _1, _2), _("Advanced"));
64
65         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
66         overall_sizer->Add (notebook, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
67
68         auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
69         if (buttons) {
70                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
71         }
72
73         SetSizer (overall_sizer);
74         overall_sizer->Layout ();
75         overall_sizer->SetSizeHints (this);
76
77         _sign_language_video_language->Changed.connect (boost::bind(&MetadataDialog::sign_language_video_language_changed, this));
78         _edit_release_territory->Bind (wxEVT_BUTTON, boost::bind(&MetadataDialog::edit_release_territory, this));
79         _enable_release_territory->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::enable_release_territory_changed, this));
80         _enable_facility->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::enable_facility_changed, this));
81         _facility->Bind (wxEVT_TEXT, boost::bind(&MetadataDialog::facility_changed, this));
82         _enable_studio->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::enable_studio_changed, this));
83         _studio->Bind (wxEVT_TEXT, boost::bind(&MetadataDialog::studio_changed, this));
84         _enable_chain->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::enable_chain_changed, this));
85         _chain->Bind (wxEVT_TEXT, boost::bind(&MetadataDialog::chain_changed, this));
86         _temp_version->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::temp_version_changed, this));
87         _pre_release->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::pre_release_changed, this));
88         _red_band->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::red_band_changed, this));
89         _two_d_version_of_three_d->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::two_d_version_of_three_d_changed, this));
90         _enable_luminance->Bind (wxEVT_CHECKBOX, boost::bind(&MetadataDialog::enable_luminance_changed, this));
91         _luminance_value->Bind (wxEVT_SPINCTRLDOUBLE, boost::bind(&MetadataDialog::luminance_changed, this));
92         _luminance_unit->Bind (wxEVT_CHOICE, boost::bind(&MetadataDialog::luminance_changed, this));
93
94         _film_changed_connection = film()->Change.connect(boost::bind(&MetadataDialog::film_changed, this, _1, _2));
95
96         film_changed (ChangeType::DONE, Film::Property::RELEASE_TERRITORY);
97         film_changed (ChangeType::DONE, Film::Property::SIGN_LANGUAGE_VIDEO_LANGUAGE);
98         film_changed (ChangeType::DONE, Film::Property::FACILITY);
99         film_changed (ChangeType::DONE, Film::Property::STUDIO);
100         film_changed (ChangeType::DONE, Film::Property::TEMP_VERSION);
101         film_changed (ChangeType::DONE, Film::Property::PRE_RELEASE);
102         film_changed (ChangeType::DONE, Film::Property::RED_BAND);
103         film_changed (ChangeType::DONE, Film::Property::TWO_D_VERSION_OF_THREE_D);
104         film_changed (ChangeType::DONE, Film::Property::CHAIN);
105         film_changed (ChangeType::DONE, Film::Property::LUMINANCE);
106
107         setup_sensitivity ();
108 }
109
110
111 void
112 MetadataDialog::film_changed (ChangeType type, Film::Property property)
113 {
114         if (type != ChangeType::DONE) {
115                 return;
116         }
117
118         if (property == Film::Property::SIGN_LANGUAGE_VIDEO_LANGUAGE) {
119                 _sign_language_video_language->set (film()->sign_language_video_language());
120         } else if (property == Film::Property::RELEASE_TERRITORY) {
121                 auto rt = film()->release_territory();
122                 checked_set (_enable_release_territory, static_cast<bool>(rt));
123                 if (rt) {
124                         _release_territory = *rt;
125                         checked_set (_release_territory_text, std_to_wx(*dcp::LanguageTag::get_subtag_description(*_release_territory)));
126                 }
127         } else if (property == Film::Property::FACILITY) {
128                 checked_set (_enable_facility, static_cast<bool>(film()->facility()));
129                 if (film()->facility()) {
130                         checked_set (_facility, *film()->facility());
131                 }
132         } else if (property == Film::Property::STUDIO) {
133                 checked_set (_enable_studio, static_cast<bool>(film()->studio()));
134                 if (film()->studio()) {
135                         checked_set (_studio, *film()->studio());
136                 }
137         } else if (property == Film::Property::CHAIN) {
138                 checked_set (_enable_chain, static_cast<bool>(film()->chain()));
139                 if (film()->chain()) {
140                         checked_set (_chain, *film()->chain());
141                 }
142         } else if (property == Film::Property::TEMP_VERSION) {
143                 checked_set (_temp_version, film()->temp_version());
144         } else if (property == Film::Property::PRE_RELEASE) {
145                 checked_set (_pre_release, film()->pre_release());
146         } else if (property == Film::Property::RED_BAND) {
147                 checked_set (_red_band, film()->red_band());
148         } else if (property == Film::Property::TWO_D_VERSION_OF_THREE_D) {
149                 checked_set (_two_d_version_of_three_d, film()->two_d_version_of_three_d());
150         } else if (property == Film::Property::LUMINANCE) {
151                 auto lum = film()->luminance();
152                 checked_set (_enable_luminance, static_cast<bool>(lum));
153                 if (lum) {
154                         checked_set (_luminance_value, lum->value());
155                         switch (lum->unit()) {
156                         case dcp::Luminance::Unit::CANDELA_PER_SQUARE_METRE:
157                                 checked_set (_luminance_unit, 0);
158                                 break;
159                         case dcp::Luminance::Unit::FOOT_LAMBERT:
160                                 checked_set (_luminance_unit, 1);
161                                 break;
162                         }
163                 } else {
164                         checked_set (_luminance_unit, 1);
165                 }
166         }
167 }
168
169
170 void
171 MetadataDialog::setup_standard (wxPanel* panel, wxSizer* sizer)
172 {
173         _enable_release_territory = new wxCheckBox (panel, wxID_ANY, _("Release territory"));
174         sizer->Add (_enable_release_territory, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
175         {
176                 auto s = new wxBoxSizer (wxHORIZONTAL);
177                 _release_territory_text = new wxStaticText (panel, wxID_ANY, wxT(""));
178                 s->Add (_release_territory_text, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
179                 _edit_release_territory = new Button (panel, _("Edit..."));
180                 s->Add (_edit_release_territory, 0, wxLEFT, DCPOMATIC_SIZER_GAP);
181                 sizer->Add (s, 0, wxEXPAND);
182         }
183 }
184
185
186 void
187 MetadataDialog::edit_release_territory ()
188 {
189         DCPOMATIC_ASSERT (film()->release_territory());
190         auto d = new RegionSubtagDialog(this, *film()->release_territory());
191         d->ShowModal ();
192         auto tag = d->get();
193         if (tag) {
194                 _release_territory = *tag;
195                 film()->set_release_territory(*tag);
196         }
197         d->Destroy ();
198 }
199
200
201 void
202 MetadataDialog::setup_sensitivity ()
203 {
204         _sign_language_video_language->enable (film()->has_sign_language_video_channel());
205         auto const enabled = _enable_release_territory->GetValue();
206         _release_territory_text->Enable (enabled);
207         _edit_release_territory->Enable (enabled);
208         _facility->Enable (_enable_facility->GetValue());
209         _chain->Enable (_enable_chain->GetValue());
210         _studio->Enable (_enable_studio->GetValue());
211         _luminance_value->Enable (_enable_luminance->GetValue());
212         _luminance_unit->Enable (_enable_luminance->GetValue());
213 }
214
215
216 void
217 MetadataDialog::enable_release_territory_changed ()
218 {
219         setup_sensitivity ();
220         if (_enable_release_territory->GetValue()) {
221                 film()->set_release_territory (_release_territory.get_value_or(dcp::LanguageTag::RegionSubtag("US")));
222         } else {
223                 film()->set_release_territory ();
224         }
225 }
226
227
228 void
229 MetadataDialog::setup_advanced (wxPanel* panel, wxSizer* sizer)
230 {
231         add_label_to_sizer (sizer, panel, _("Sign language video language"), true, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT);
232         _sign_language_video_language = new LanguageTagWidget (panel, _("Language used for any sign language video track"), {}, {});
233         sizer->Add (_sign_language_video_language->sizer(), 1, wxEXPAND);
234
235         _enable_facility = new wxCheckBox (panel, wxID_ANY, _("Facility"));
236         sizer->Add (_enable_facility, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
237         _facility = new wxTextCtrl (panel, wxID_ANY);
238         sizer->Add (_facility, 1, wxEXPAND);
239
240         _enable_studio = new wxCheckBox (panel, wxID_ANY, _("Studio"));
241         sizer->Add (_enable_studio, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
242         _studio = new wxTextCtrl (panel, wxID_ANY);
243         sizer->Add (_studio, 1, wxEXPAND);
244
245         _enable_chain = new wxCheckBox (panel, wxID_ANY, _("Chain"));
246         sizer->Add (_enable_chain, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
247         _chain = new wxTextCtrl (panel, wxID_ANY);
248         sizer->Add (_chain, 1, wxEXPAND);
249
250         _temp_version = new wxCheckBox (panel, wxID_ANY, _("Temporary version"));
251         sizer->Add (_temp_version, 0, wxALIGN_CENTER_VERTICAL);
252         sizer->AddSpacer (0);
253
254         _pre_release = new wxCheckBox (panel, wxID_ANY, _("Pre-release"));
255         sizer->Add (_pre_release, 0, wxALIGN_CENTER_VERTICAL);
256         sizer->AddSpacer (0);
257
258         _red_band = new wxCheckBox (panel, wxID_ANY, _("Red band"));
259         sizer->Add (_red_band, 0, wxALIGN_CENTER_VERTICAL);
260         sizer->AddSpacer (0);
261
262         _two_d_version_of_three_d = new wxCheckBox (panel, wxID_ANY, _("2D version of 3D DCP"));
263         sizer->Add (_two_d_version_of_three_d, 0, wxALIGN_CENTER_VERTICAL);
264         sizer->AddSpacer (0);
265
266         _enable_luminance = new wxCheckBox (panel, wxID_ANY, _("Luminance"));
267         sizer->Add (_enable_luminance, 0, wxALIGN_CENTER_VERTICAL);
268         {
269                 auto s = new wxBoxSizer (wxHORIZONTAL);
270                 _luminance_value = new wxSpinCtrlDouble (panel, wxID_ANY);
271                 _luminance_value->SetRange (0.1, 32.0);
272                 _luminance_value->SetDigits (1);
273                 _luminance_value->SetIncrement (0.1);
274                 s->Add (_luminance_value, 0);
275                 _luminance_unit = new wxChoice (panel, wxID_ANY);
276                 s->Add (_luminance_unit, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
277                 sizer->Add (s, 1, wxEXPAND);
278         }
279
280         _luminance_unit->Append (wxString::FromUTF8(_("candela per m²")));
281         _luminance_unit->Append (_("foot lambert"));
282
283 }
284
285
286 void
287 MetadataDialog::facility_changed ()
288 {
289         film()->set_facility (wx_to_std(_facility->GetValue()));
290 }
291
292
293 void
294 MetadataDialog::enable_facility_changed ()
295 {
296         setup_sensitivity ();
297         if (_enable_facility->GetValue()) {
298                 film()->set_facility (wx_to_std(_facility->GetValue()));
299         } else {
300                 film()->set_facility ();
301         }
302 }
303
304
305 void
306 MetadataDialog::studio_changed ()
307 {
308         film()->set_studio (wx_to_std(_studio->GetValue()));
309 }
310
311
312 void
313 MetadataDialog::enable_studio_changed ()
314 {
315         setup_sensitivity ();
316         if (_enable_studio->GetValue()) {
317                 film()->set_studio (wx_to_std(_studio->GetValue()));
318         } else {
319                 film()->set_studio ();
320         }
321 }
322
323
324 void
325 MetadataDialog::temp_version_changed ()
326 {
327         film()->set_temp_version(_temp_version->GetValue());
328 }
329
330
331 void
332 MetadataDialog::pre_release_changed ()
333 {
334         film()->set_pre_release(_pre_release->GetValue());
335 }
336
337
338 void
339 MetadataDialog::red_band_changed ()
340 {
341         film()->set_red_band(_red_band->GetValue());
342 }
343
344
345 void
346 MetadataDialog::two_d_version_of_three_d_changed ()
347 {
348         film()->set_two_d_version_of_three_d(_two_d_version_of_three_d->GetValue());
349 }
350
351
352 void
353 MetadataDialog::chain_changed ()
354 {
355         film()->set_chain (wx_to_std(_chain->GetValue()));
356 }
357
358
359 void
360 MetadataDialog::enable_chain_changed ()
361 {
362         setup_sensitivity ();
363         if (_enable_chain->GetValue()) {
364                 chain_changed ();
365         } else {
366                 film()->set_chain ();
367         }
368 }
369
370
371 void
372 MetadataDialog::enable_luminance_changed ()
373 {
374         setup_sensitivity ();
375         if (_enable_luminance->GetValue()) {
376                 luminance_changed ();
377         } else {
378                 film()->set_luminance ();
379         }
380 }
381
382
383 void
384 MetadataDialog::luminance_changed ()
385 {
386         dcp::Luminance::Unit unit;
387         switch (_luminance_unit->GetSelection()) {
388         case 0:
389                 unit = dcp::Luminance::Unit::CANDELA_PER_SQUARE_METRE;
390                 break;
391         case 1:
392                 unit = dcp::Luminance::Unit::FOOT_LAMBERT;
393                 break;
394         default:
395                 DCPOMATIC_ASSERT (false);
396         }
397
398         film()->set_luminance (dcp::Luminance(_luminance_value->GetValue(), unit));
399 }
400
401
402 void
403 MetadataDialog::sign_language_video_language_changed ()
404 {
405         film()->set_sign_language_video_language(_sign_language_video_language->get());
406 }
407