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