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