Fix some vertical alignment.
[dcpomatic.git] / src / wx / smpte_metadata_dialog.cc
1 /*
2     Copyright (C) 2019-2020 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 #include "content_version_dialog.h"
22 #include "editable_list.h"
23 #include "language_tag_dialog.h"
24 #include "smpte_metadata_dialog.h"
25 #include "rating_dialog.h"
26 #include "lib/film.h"
27 #include <dcp/types.h>
28 #include <wx/gbsizer.h>
29 #include <wx/spinctrl.h>
30
31 using std::string;
32 using std::vector;
33 using boost::optional;
34 using boost::shared_ptr;
35 using boost::weak_ptr;
36
37
38 static string
39 ratings_column (dcp::Rating r, int c)
40 {
41         if (c == 0) {
42                 return r.agency;
43         }
44
45         return r.label;
46 }
47
48
49 static string
50 content_versions_column (string v, int)
51 {
52         return v;
53 }
54
55
56 SMPTEMetadataDialog::SMPTEMetadataDialog (wxWindow* parent, weak_ptr<Film> weak_film)
57         : wxDialog (parent, wxID_ANY, _("Metadata"))
58         , _film (weak_film)
59 {
60         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
61         SetSizer (overall_sizer);
62
63         wxFlexGridSizer* sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
64         sizer->AddGrowableCol (1, 1);
65
66         wxButton* edit_name_language = 0;
67         Button* edit_release_territory = 0;
68
69         add_label_to_sizer(sizer, this, _("Title language"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
70         {
71                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
72                 _name_language = new wxStaticText (this, wxID_ANY, wxT(""));
73                 _name_language->SetToolTip (wxString::Format(_("The language that the film's title (\"%s\") is in"), std_to_wx(film()->name())));
74                 s->Add (_name_language, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
75                 edit_name_language = new Button (this, _("Edit..."));
76                 s->Add (edit_name_language, 0, wxLEFT, DCPOMATIC_SIZER_GAP);
77                 sizer->Add (s, 0, wxEXPAND);
78         }
79
80         add_label_to_sizer (sizer, this, _("Release territory"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
81         {
82                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
83                 _release_territory = new wxStaticText (this, wxID_ANY, wxT(""));
84                 s->Add (_release_territory, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
85                 edit_release_territory = new Button (this, _("Edit..."));
86                 s->Add (edit_release_territory, 0, wxLEFT, DCPOMATIC_SIZER_GAP);
87                 sizer->Add (s, 0, wxEXPAND);
88         }
89
90         add_label_to_sizer (sizer, this, _("Version number"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
91         _version_number = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 1000);
92         sizer->Add (_version_number, 0);
93
94         add_label_to_sizer (sizer, this, _("Status"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
95         _status = new wxChoice (this, wxID_ANY);
96         sizer->Add (_status, 0);
97
98         add_label_to_sizer (sizer, this, _("Chain"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
99         _chain = new wxTextCtrl (this, wxID_ANY);
100         sizer->Add (_chain, 1, wxEXPAND);
101
102         add_label_to_sizer (sizer, this, _("Distributor"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
103         _distributor = new wxTextCtrl (this, wxID_ANY);
104         sizer->Add (_distributor, 1, wxEXPAND);
105
106         add_label_to_sizer (sizer, this, _("Facility"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
107         _facility = new wxTextCtrl (this, wxID_ANY);
108         sizer->Add (_facility, 1, wxEXPAND);
109
110         add_label_to_sizer (sizer, this, _("Luminance"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL);
111         {
112                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
113                 _luminance_value = new wxSpinCtrlDouble (this, wxID_ANY);
114                 _luminance_value->SetDigits (1);
115                 _luminance_value->SetIncrement (0.1);
116                 s->Add (_luminance_value, 0);
117                 _luminance_unit = new wxChoice (this, wxID_ANY);
118                 s->Add (_luminance_unit, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
119                 sizer->Add (s, 1, wxEXPAND);
120         }
121
122         {
123                 int flags = wxALIGN_TOP | wxLEFT | wxRIGHT | wxTOP;
124 #ifdef __WXOSX__
125                 flags |= wxALIGN_RIGHT;
126 #endif
127                 wxStaticText* m = create_label (this, _("Ratings"), true);
128                 sizer->Add (m, 0, flags, DCPOMATIC_SIZER_GAP);
129         }
130
131         vector<EditableListColumn> columns;
132         columns.push_back (EditableListColumn("Agency", 200, true));
133         columns.push_back (EditableListColumn("Label", 50, true));
134         _ratings = new EditableList<dcp::Rating, RatingDialog> (
135                 this,
136                 columns,
137                 boost::bind(&SMPTEMetadataDialog::ratings, this),
138                 boost::bind(&SMPTEMetadataDialog::set_ratings, this, _1),
139                 boost::bind(&ratings_column, _1, _2),
140                 true,
141                 false
142                 );
143         sizer->Add (_ratings, 1, wxEXPAND);
144
145         {
146                 int flags = wxALIGN_TOP | wxLEFT | wxRIGHT | wxTOP;
147 #ifdef __WXOSX__
148                 flags |= wxALIGN_RIGHT;
149 #endif
150                 wxStaticText* m = create_label (this, _("Content versions"), true);
151                 sizer->Add (m, 0, flags, DCPOMATIC_SIZER_GAP);
152         }
153
154         columns.clear ();
155         columns.push_back (EditableListColumn("Version", 350, true));
156         _content_versions = new EditableList<string, ContentVersionDialog> (
157                 this,
158                 columns,
159                 boost::bind(&SMPTEMetadataDialog::content_versions, this),
160                 boost::bind(&SMPTEMetadataDialog::set_content_versions, this, _1),
161                 boost::bind(&content_versions_column, _1, _2),
162                 true,
163                 false
164                 );
165         sizer->Add (_content_versions, 1, wxEXPAND);
166
167         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
168
169         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
170         if (buttons) {
171                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
172         }
173
174         overall_sizer->Layout ();
175         overall_sizer->SetSizeHints (this);
176
177         _status->Append (_("Temporary"));
178         _status->Append (_("Pre-release"));
179         _status->Append (_("Final"));
180
181         _luminance_unit->Append (_("candela per m²"));
182         _luminance_unit->Append (_("foot lambert"));
183
184         edit_name_language->Bind (wxEVT_BUTTON, boost::bind(&SMPTEMetadataDialog::edit_name_language, this));
185         edit_release_territory->Bind (wxEVT_BUTTON, boost::bind(&SMPTEMetadataDialog::edit_release_territory, this));
186         _version_number->Bind (wxEVT_SPINCTRL, boost::bind(&SMPTEMetadataDialog::version_number_changed, this));
187         _status->Bind (wxEVT_CHOICE, boost::bind(&SMPTEMetadataDialog::status_changed, this));
188         _chain->Bind (wxEVT_TEXT, boost::bind(&SMPTEMetadataDialog::chain_changed, this));
189         _distributor->Bind (wxEVT_TEXT, boost::bind(&SMPTEMetadataDialog::distributor_changed, this));
190         _facility->Bind (wxEVT_TEXT, boost::bind(&SMPTEMetadataDialog::facility_changed, this));
191         _luminance_value->Bind (wxEVT_SPINCTRLDOUBLE, boost::bind(&SMPTEMetadataDialog::luminance_changed, this));
192         _luminance_unit->Bind (wxEVT_CHOICE, boost::bind(&SMPTEMetadataDialog::luminance_changed, this));
193
194         _version_number->SetFocus ();
195
196         _film_changed_connection = film()->Change.connect(boost::bind(&SMPTEMetadataDialog::film_changed, this, _1, _2));
197
198         film_changed (CHANGE_TYPE_DONE, Film::NAME_LANGUAGE);
199         film_changed (CHANGE_TYPE_DONE, Film::RELEASE_TERRITORY);
200         film_changed (CHANGE_TYPE_DONE, Film::VERSION_NUMBER);
201         film_changed (CHANGE_TYPE_DONE, Film::STATUS);
202         film_changed (CHANGE_TYPE_DONE, Film::CHAIN);
203         film_changed (CHANGE_TYPE_DONE, Film::DISTRIBUTOR);
204         film_changed (CHANGE_TYPE_DONE, Film::FACILITY);
205         film_changed (CHANGE_TYPE_DONE, Film::CONTENT_VERSIONS);
206         film_changed (CHANGE_TYPE_DONE, Film::LUMINANCE);
207 }
208
209
210 void
211 SMPTEMetadataDialog::film_changed (ChangeType type, Film::Property property)
212 {
213         if (type != CHANGE_TYPE_DONE || film()->interop()) {
214                 return;
215         }
216
217         if (property == Film::NAME_LANGUAGE) {
218                 checked_set (_name_language, std_to_wx(film()->name_language().to_string()));
219         } else if (property == Film::RELEASE_TERRITORY) {
220                 checked_set (_release_territory, std_to_wx(*dcp::LanguageTag::get_subtag_description(dcp::LanguageTag::REGION, film()->release_territory().subtag())));
221         } else if (property == Film::VERSION_NUMBER) {
222                 checked_set (_version_number, film()->version_number());
223         } else if (property == Film::STATUS) {
224                 switch (film()->status()) {
225                 case dcp::TEMP:
226                         checked_set (_status, 0);
227                         break;
228                 case dcp::PRE:
229                         checked_set (_status, 1);
230                         break;
231                 case dcp::FINAL:
232                         checked_set (_status, 2);
233                         break;
234                 }
235         } else if (property == Film::CHAIN) {
236                 checked_set (_chain, film()->chain());
237         } else if (property == Film::DISTRIBUTOR) {
238                 checked_set (_distributor, film()->distributor());
239         } else if (property == Film::FACILITY) {
240                 checked_set (_facility, film()->facility());
241         } else if (property == Film::LUMINANCE) {
242                 checked_set (_luminance_value, film()->luminance().value());
243                 switch (film()->luminance().unit()) {
244                 case dcp::Luminance::CANDELA_PER_SQUARE_METRE:
245                         checked_set (_luminance_unit, 0);
246                         break;
247                 case dcp::Luminance::FOOT_LAMBERT:
248                         checked_set (_luminance_unit, 1);
249                         break;
250                 }
251         }
252 }
253
254
255 vector<dcp::Rating>
256 SMPTEMetadataDialog::ratings () const
257 {
258         return film()->ratings ();
259 }
260
261
262 void
263 SMPTEMetadataDialog::set_ratings (vector<dcp::Rating> r)
264 {
265         film()->set_ratings (r);
266 }
267
268
269 vector<string>
270 SMPTEMetadataDialog::content_versions () const
271 {
272         return film()->content_versions ();
273 }
274
275
276 void
277 SMPTEMetadataDialog::set_content_versions (vector<string> cv)
278 {
279         film()->set_content_versions (cv);
280 }
281
282
283 void
284 SMPTEMetadataDialog::edit_name_language ()
285 {
286         LanguageTagDialog* d = new LanguageTagDialog(this, film()->name_language());
287         d->ShowModal ();
288         film()->set_name_language (d->get());
289         d->Destroy ();
290 }
291
292
293 void
294 SMPTEMetadataDialog::edit_release_territory ()
295 {
296         RegionSubtagDialog* d = new RegionSubtagDialog(this, film()->release_territory());
297         d->ShowModal ();
298         optional<dcp::LanguageTag::RegionSubtag> tag = d->get();
299         if (tag) {
300                 film()->set_release_territory (*tag);
301         }
302         d->Destroy ();
303 }
304
305
306 shared_ptr<Film>
307 SMPTEMetadataDialog::film () const
308 {
309         shared_ptr<Film> film = _film.lock ();
310         DCPOMATIC_ASSERT (film);
311         return film;
312 }
313
314
315 void
316 SMPTEMetadataDialog::version_number_changed ()
317 {
318         film()->set_version_number (_version_number->GetValue());
319 }
320
321
322 void
323 SMPTEMetadataDialog::status_changed ()
324 {
325         switch (_status->GetSelection()) {
326         case 0:
327                 film()->set_status (dcp::TEMP);
328                 break;
329         case 1:
330                 film()->set_status (dcp::PRE);
331                 break;
332         case 2:
333                 film()->set_status (dcp::FINAL);
334                 break;
335         }
336 }
337
338
339 void
340 SMPTEMetadataDialog::chain_changed ()
341 {
342         film()->set_chain (wx_to_std(_chain->GetValue()));
343 }
344
345
346 void
347 SMPTEMetadataDialog::distributor_changed ()
348 {
349         film()->set_distributor (wx_to_std(_distributor->GetValue()));
350 }
351
352
353 void
354 SMPTEMetadataDialog::facility_changed ()
355 {
356         film()->set_facility (wx_to_std(_facility->GetValue()));
357 }
358
359
360 void
361 SMPTEMetadataDialog::luminance_changed ()
362 {
363         dcp::Luminance::Unit unit;
364         switch (_luminance_unit->GetSelection()) {
365         case 0:
366                 unit = dcp::Luminance::CANDELA_PER_SQUARE_METRE;
367                 break;
368         case 1:
369                 unit = dcp::Luminance::FOOT_LAMBERT;
370                 break;
371         }
372
373         film()->set_luminance (dcp::Luminance(_luminance_value->GetValue(), unit));
374 }