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