Split screens panel from KDMDialog.
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "kdm_dialog.h"
21 #include "cinema_dialog.h"
22 #include "screen_dialog.h"
23 #include "wx_util.h"
24 #include "screens_panel.h"
25 #include "lib/cinema.h"
26 #include "lib/config.h"
27 #include "lib/film.h"
28 #include "lib/screen.h"
29 #include <libcxml/cxml.h>
30 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
31 #include "dir_picker_ctrl.h"
32 #else
33 #include <wx/filepicker.h>
34 #endif
35 #include <wx/treectrl.h>
36 #include <wx/datectrl.h>
37 #include <wx/timectrl.h>
38 #include <wx/stdpaths.h>
39 #include <wx/listctrl.h>
40 #include <iostream>
41
42 using std::string;
43 using std::map;
44 using std::list;
45 using std::pair;
46 using std::cout;
47 using std::vector;
48 using std::make_pair;
49 using boost::shared_ptr;
50
51 KDMDialog::KDMDialog (wxWindow* parent, boost::shared_ptr<const Film> film)
52         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
53 {
54         /* Main sizer */
55         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
56
57         /* Font for sub-headings */
58         wxFont subheading_font (*wxNORMAL_FONT);
59         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
60
61         /* Sub-heading: Screens */
62         wxStaticText* h = new wxStaticText (this, wxID_ANY, _("Screens"));
63         h->SetFont (subheading_font);
64         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL);
65         _screens = new ScreensPanel (this);
66         vertical->Add (_screens, 1, wxEXPAND);
67
68         /* Sub-heading: Timing */
69         h = new wxStaticText (this, wxID_ANY, S_("KDM|Timing"));
70         h->SetFont (subheading_font);
71         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
72
73         wxFlexGridSizer* table = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
74         add_label_to_sizer (table, this, _("From"), true);
75         wxDateTime from;
76         from.SetToCurrent ();
77         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
78         table->Add (_from_date, 1, wxEXPAND);
79         _from_time = new wxTimePickerCtrl (this, wxID_ANY, from);
80         table->Add (_from_time, 1, wxEXPAND);
81
82         add_label_to_sizer (table, this, _("Until"), true);
83         wxDateTime to = from;
84         /* 1 week from now */
85         to.Add (wxDateSpan (0, 0, 1, 0));
86         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
87         table->Add (_until_date, 1, wxEXPAND);
88         _until_time = new wxTimePickerCtrl (this, wxID_ANY, to);
89         table->Add (_until_time, 1, wxEXPAND);
90
91         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
92
93
94         /* Sub-heading: CPL */
95         h = new wxStaticText (this, wxID_ANY, _("CPL"));
96         h->SetFont (subheading_font);
97         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
98
99         /* CPL choice */
100         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
101         add_label_to_sizer (s, this, _("CPL"), true);
102         _cpl = new wxChoice (this, wxID_ANY);
103         s->Add (_cpl, 1, wxEXPAND);
104         _cpl_browse = new wxButton (this, wxID_ANY, _("Browse..."));
105         s->Add (_cpl_browse, 0);
106         vertical->Add (s, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
107
108         /* CPL details */
109         table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
110         add_label_to_sizer (table, this, _("DCP directory"), true);
111         _dcp_directory = new wxStaticText (this, wxID_ANY, "");
112         table->Add (_dcp_directory);
113         add_label_to_sizer (table, this, _("CPL ID"), true);
114         _cpl_id = new wxStaticText (this, wxID_ANY, "");
115         table->Add (_cpl_id);
116         add_label_to_sizer (table, this, _("CPL annotation text"), true);
117         _cpl_annotation_text = new wxStaticText (this, wxID_ANY, "");
118         table->Add (_cpl_annotation_text);
119         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
120
121         _cpls = film->cpls ();
122         update_cpl_choice ();
123
124
125         /* Sub-heading: Output */
126         h = new wxStaticText (this, wxID_ANY, _("Output"));
127         h->SetFont (subheading_font);
128         vertical->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
129
130         table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, 0);
131
132         add_label_to_sizer (table, this, _("KDM type"), true);
133         _type = new wxChoice (this, wxID_ANY);
134         _type->Append ("Modified Transitional 1", ((void *) dcp::MODIFIED_TRANSITIONAL_1));
135         if (!film->interop ()) {
136                 _type->Append ("DCI Any", ((void *) dcp::DCI_ANY));
137                 _type->Append ("DCI Specific", ((void *) dcp::DCI_SPECIFIC));
138         }
139         table->Add (_type, 1, wxEXPAND);
140         _type->SetSelection (0);
141
142         _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
143         table->Add (_write_to, 1, wxEXPAND);
144
145 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
146         _folder = new DirPickerCtrl (this);
147 #else
148         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
149 #endif
150
151         _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
152
153         table->Add (_folder, 1, wxEXPAND);
154
155         _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
156         table->Add (_email, 1, wxEXPAND);
157         table->AddSpacer (0);
158
159         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
160
161         /* Make an overall sizer to get a nice border, and put some buttons in */
162
163         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
164         overall_sizer->Add (vertical, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
165
166         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
167         if (buttons) {
168                 overall_sizer->Add (buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
169         }
170
171         _write_to->SetValue (true);
172
173         /* Bind */
174
175         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
176
177         _cpl->Bind           (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&KDMDialog::update_cpl_summary, this));
178         _cpl_browse->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&KDMDialog::cpl_browse_clicked, this));
179
180         _write_to->Bind      (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
181         _email->Bind         (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
182
183         setup_sensitivity ();
184
185         SetSizer (overall_sizer);
186         overall_sizer->Layout ();
187         overall_sizer->SetSizeHints (this);
188 }
189
190 void
191 KDMDialog::setup_sensitivity ()
192 {
193         _screens->setup_sensitivity ();
194
195         bool const sd = _cpl->GetSelection() != -1;
196
197         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
198         if (ok) {
199                 ok->Enable (!_screens->screens().empty() && sd);
200         }
201
202         _folder->Enable (_write_to->GetValue ());
203 }
204
205 boost::posix_time::ptime
206 KDMDialog::from () const
207 {
208         return posix_time (_from_date, _from_time);
209 }
210
211 boost::posix_time::ptime
212 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
213 {
214         wxDateTime const date = date_picker->GetValue ();
215         wxDateTime const time = time_picker->GetValue ();
216         return boost::posix_time::ptime (
217                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
218                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
219                 );
220 }
221
222 boost::posix_time::ptime
223 KDMDialog::until () const
224 {
225         return posix_time (_until_date, _until_time);
226 }
227
228 boost::filesystem::path
229 KDMDialog::cpl () const
230 {
231         int const item = _cpl->GetSelection ();
232         DCPOMATIC_ASSERT (item >= 0);
233         return _cpls[item].cpl_file;
234 }
235
236 boost::filesystem::path
237 KDMDialog::directory () const
238 {
239         return wx_to_std (_folder->GetPath ());
240 }
241
242 bool
243 KDMDialog::write_to () const
244 {
245         return _write_to->GetValue ();
246 }
247
248 dcp::Formulation
249 KDMDialog::formulation () const
250 {
251         return (dcp::Formulation) reinterpret_cast<intptr_t> (_type->GetClientData (_type->GetSelection()));
252 }
253
254 void
255 KDMDialog::update_cpl_choice ()
256 {
257         _cpl->Clear ();
258
259         for (vector<CPLSummary>::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
260                 _cpl->Append (std_to_wx (i->cpl_id));
261
262                 if (_cpls.size() > 0) {
263                         _cpl->SetSelection (0);
264                 }
265         }
266
267         update_cpl_summary ();
268 }
269
270 void
271 KDMDialog::update_cpl_summary ()
272 {
273         int const n = _cpl->GetSelection();
274         if (n == wxNOT_FOUND) {
275                 return;
276         }
277
278         _dcp_directory->SetLabel (std_to_wx (_cpls[n].dcp_directory));
279         _cpl_id->SetLabel (std_to_wx (_cpls[n].cpl_id));
280         _cpl_annotation_text->SetLabel (std_to_wx (_cpls[n].cpl_annotation_text));
281 }
282
283 void
284 KDMDialog::cpl_browse_clicked ()
285 {
286         wxFileDialog* d = new wxFileDialog (this, _("Select CPL XML file"), wxEmptyString, wxEmptyString, "*.xml");
287         if (d->ShowModal() == wxID_CANCEL) {
288                 d->Destroy ();
289                 return;
290         }
291
292         boost::filesystem::path cpl_file (wx_to_std (d->GetPath ()));
293         boost::filesystem::path dcp_dir = cpl_file.parent_path ();
294
295         d->Destroy ();
296
297         /* XXX: hack alert */
298         cxml::Document cpl_document ("CompositionPlaylist");
299         cpl_document.read_file (cpl_file);
300
301         try {
302                 _cpls.push_back (
303                         CPLSummary (
304                                 dcp_dir.filename().string(),
305                                 cpl_document.string_child("Id").substr (9),
306                                 cpl_document.string_child ("ContentTitleText"),
307                                 cpl_file
308                                 )
309                         );
310         } catch (cxml::Error) {
311                 error_dialog (this, _("This is not a valid CPL file"));
312                 return;
313         }
314
315         update_cpl_choice ();
316         _cpl->SetSelection (_cpls.size() - 1);
317         update_cpl_summary ();
318 }
319
320 list<shared_ptr<Screen> >
321 KDMDialog::screens () const
322 {
323         return _screens->screens ();
324 }