96b9aa69745344c11a8bb55c1312e4e440e0e7c5
[dcpomatic.git] / src / wx / kdm_cpl_panel.cc
1 /*
2     Copyright (C) 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_cpl_panel.h"
21 #include "wx_util.h"
22 #include <libcxml/cxml.h>
23
24 using std::vector;
25
26 KDMCPLPanel::KDMCPLPanel (wxWindow* parent, vector<CPLSummary> cpls)
27         : wxPanel (parent, wxID_ANY)
28         , _cpls (cpls)
29 {
30         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
31
32         /* CPL choice */
33         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
34         add_label_to_sizer (s, this, _("CPL"), true);
35         _cpl = new wxChoice (this, wxID_ANY);
36         s->Add (_cpl, 1, wxEXPAND);
37         _cpl_browse = new wxButton (this, wxID_ANY, _("Browse..."));
38         s->Add (_cpl_browse, 0);
39         vertical->Add (s, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
40
41         /* CPL details */
42         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
43         add_label_to_sizer (table, this, _("DCP directory"), true);
44         _dcp_directory = new wxStaticText (this, wxID_ANY, "");
45         table->Add (_dcp_directory);
46         add_label_to_sizer (table, this, _("CPL ID"), true);
47         _cpl_id = new wxStaticText (this, wxID_ANY, "");
48         table->Add (_cpl_id);
49         add_label_to_sizer (table, this, _("CPL annotation text"), true);
50         _cpl_annotation_text = new wxStaticText (this, wxID_ANY, "");
51         table->Add (_cpl_annotation_text);
52         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
53
54         update_cpl_choice ();
55
56         _cpl->Bind        (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&KDMCPLPanel::update_cpl_summary, this));
57         _cpl_browse->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&KDMCPLPanel::cpl_browse_clicked, this));
58
59         SetSizerAndFit (vertical);
60 }
61
62 void
63 KDMCPLPanel::update_cpl_choice ()
64 {
65         _cpl->Clear ();
66
67         for (vector<CPLSummary>::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
68                 _cpl->Append (std_to_wx (i->cpl_id));
69
70                 if (_cpls.size() > 0) {
71                         _cpl->SetSelection (0);
72                 }
73         }
74
75         update_cpl_summary ();
76 }
77
78 void
79 KDMCPLPanel::update_cpl_summary ()
80 {
81         int const n = _cpl->GetSelection();
82         if (n == wxNOT_FOUND) {
83                 return;
84         }
85
86         _dcp_directory->SetLabel (std_to_wx (_cpls[n].dcp_directory));
87         _cpl_id->SetLabel (std_to_wx (_cpls[n].cpl_id));
88         _cpl_annotation_text->SetLabel (std_to_wx (_cpls[n].cpl_annotation_text));
89 }
90
91 void
92 KDMCPLPanel::cpl_browse_clicked ()
93 {
94         wxFileDialog* d = new wxFileDialog (this, _("Select CPL XML file"), wxEmptyString, wxEmptyString, "*.xml");
95         if (d->ShowModal() == wxID_CANCEL) {
96                 d->Destroy ();
97                 return;
98         }
99
100         boost::filesystem::path cpl_file (wx_to_std (d->GetPath ()));
101         boost::filesystem::path dcp_dir = cpl_file.parent_path ();
102
103         d->Destroy ();
104
105         /* XXX: hack alert */
106         cxml::Document cpl_document ("CompositionPlaylist");
107         cpl_document.read_file (cpl_file);
108
109         try {
110                 _cpls.push_back (
111                         CPLSummary (
112                                 dcp_dir.filename().string(),
113                                 cpl_document.string_child("Id").substr (9),
114                                 cpl_document.string_child ("ContentTitleText"),
115                                 cpl_file
116                                 )
117                         );
118         } catch (cxml::Error) {
119                 error_dialog (this, _("This is not a valid CPL file"));
120                 return;
121         }
122
123         update_cpl_choice ();
124         _cpl->SetSelection (_cpls.size() - 1);
125         update_cpl_summary ();
126 }
127
128 boost::filesystem::path
129 KDMCPLPanel::cpl () const
130 {
131         int const item = _cpl->GetSelection ();
132         DCPOMATIC_ASSERT (item >= 0);
133         return _cpls[item].cpl_file;
134 }
135
136 bool
137 KDMCPLPanel::has_selected () const
138 {
139         return _cpl->GetSelection() != -1;
140 }