A bit of tidying up; don't build player ever for now; add -c option to makedcp to...
[dcpomatic.git] / src / wx / config_dialog.cc
1 /*
2     Copyright (C) 2012 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 /** @file src/config_dialog.cc
21  *  @brief A dialogue to edit DVD-o-matic configuration.
22  */
23
24 #include <iostream>
25 #include <boost/lexical_cast.hpp>
26 #include "lib/config.h"
27 #include "lib/server.h"
28 #include "lib/screen.h"
29 #include "lib/format.h"
30 #include "lib/scaler.h"
31 #include "lib/filter.h"
32 #include "config_dialog.h"
33 #include "wx_util.h"
34 #include "filter_dialog.h"
35 #include "server_dialog.h"
36
37 using namespace std;
38 using namespace boost;
39
40 ConfigDialog::ConfigDialog (wxWindow* parent)
41         : wxDialog (parent, wxID_ANY, _("DVD-o-matic Configuration"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
42 {
43         wxFlexGridSizer* table = new wxFlexGridSizer (3, 6, 6);
44         table->AddGrowableCol (1, 1);
45
46         add_label_to_sizer (table, this, "TMS IP address");
47         _tms_ip = new wxTextCtrl (this, wxID_ANY);
48         table->Add (_tms_ip, 1, wxEXPAND);
49         table->AddSpacer (0);
50
51         add_label_to_sizer (table, this, "TMS target path");
52         _tms_path = new wxTextCtrl (this, wxID_ANY);
53         table->Add (_tms_path, 1, wxEXPAND);
54         table->AddSpacer (0);
55
56         add_label_to_sizer (table, this, "TMS user name");
57         _tms_user = new wxTextCtrl (this, wxID_ANY);
58         table->Add (_tms_user, 1, wxEXPAND);
59         table->AddSpacer (0);
60
61         add_label_to_sizer (table, this, "TMS password");
62         _tms_password = new wxTextCtrl (this, wxID_ANY);
63         table->Add (_tms_password, 1, wxEXPAND);
64         table->AddSpacer (0);
65
66         add_label_to_sizer (table, this, "Threads to use for encoding on this host");
67         _num_local_encoding_threads = new wxSpinCtrl (this);
68         table->Add (_num_local_encoding_threads, 1, wxEXPAND);
69         table->AddSpacer (0);
70
71         add_label_to_sizer (table, this, "Colour look-up table");
72         _colour_lut = new wxComboBox (this, wxID_ANY);
73         for (int i = 0; i < 2; ++i) {
74                 _colour_lut->Append (std_to_wx (colour_lut_index_to_name (i)));
75         }
76         _colour_lut->SetSelection (0);
77         table->Add (_colour_lut, 1, wxEXPAND);
78         table->AddSpacer (0);
79
80         add_label_to_sizer (table, this, "JPEG2000 bandwidth");
81         _j2k_bandwidth = new wxSpinCtrl (this, wxID_ANY);
82         table->Add (_j2k_bandwidth, 1, wxEXPAND);
83         add_label_to_sizer (table, this, "MBps");
84
85         add_label_to_sizer (table, this, "Reference scaler for A/B");
86         _reference_scaler = new wxComboBox (this, wxID_ANY);
87         vector<Scaler const *> const sc = Scaler::all ();
88         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
89                 _reference_scaler->Append (std_to_wx ((*i)->name ()));
90         }
91
92         table->Add (_reference_scaler, 1, wxEXPAND);
93         table->AddSpacer (0);
94
95         {
96                 add_label_to_sizer (table, this, "Reference filters for A/B");
97                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
98                 _reference_filters = new wxStaticText (this, wxID_ANY, wxT (""));
99                 s->Add (_reference_filters, 1, wxEXPAND);
100                 _reference_filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
101                 s->Add (_reference_filters_button, 0);
102                 table->Add (s, 1, wxEXPAND);
103                 table->AddSpacer (0);
104         }
105
106         add_label_to_sizer (table, this, "Encoding Servers");
107         _servers = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (220, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
108         wxListItem ip;
109         ip.SetId (0);
110         ip.SetText (_("IP address"));
111         ip.SetWidth (120);
112         _servers->InsertColumn (0, ip);
113         ip.SetId (1);
114         ip.SetText (_("Threads"));
115         ip.SetWidth (80);
116         _servers->InsertColumn (1, ip);
117         table->Add (_servers, 1, wxEXPAND | wxALL);
118
119         {
120                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
121                 _add_server = new wxButton (this, wxID_ANY, _("Add"));
122                 s->Add (_add_server);
123                 _edit_server = new wxButton (this, wxID_ANY, _("Edit"));
124                 s->Add (_edit_server);
125                 _remove_server = new wxButton (this, wxID_ANY, _("Remove"));
126                 s->Add (_remove_server);
127                 table->Add (s, 0);
128         }
129                 
130         Config* config = Config::instance ();
131
132         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
133         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
134         _tms_path->SetValue (std_to_wx (config->tms_path ()));
135         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
136         _tms_user->SetValue (std_to_wx (config->tms_user ()));
137         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
138         _tms_password->SetValue (std_to_wx (config->tms_password ()));
139         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
140
141         _num_local_encoding_threads->SetRange (1, 128);
142         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
143         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
144
145         _colour_lut->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::colour_lut_changed), 0, this);
146         
147         _j2k_bandwidth->SetRange (50, 250);
148         _j2k_bandwidth->SetValue (config->j2k_bandwidth() / 1e6);
149         _j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::j2k_bandwidth_changed), 0, this);
150
151         _reference_scaler->SetSelection (Scaler::as_index (config->reference_scaler ()));
152         _reference_scaler->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::reference_scaler_changed), 0, this);
153
154         pair<string, string> p = Filter::ffmpeg_strings (config->reference_filters ());
155         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
156         _reference_filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_reference_filters_clicked), 0, this);
157
158         vector<Server*> servers = config->servers ();
159         for (vector<Server*>::iterator i = servers.begin(); i != servers.end(); ++i) {
160                 add_server_to_control (*i);
161         }
162         
163         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
164         _edit_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_server_clicked), 0, this);
165         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
166
167         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
168         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
169         wxListEvent ev;
170         server_selection_changed (ev);
171
172         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
173         overall_sizer->Add (table, 1, wxEXPAND);
174
175         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
176         if (buttons) {
177                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
178         }
179
180         SetSizer (overall_sizer);
181         overall_sizer->Layout ();
182         overall_sizer->SetSizeHints (this);
183 }
184
185 void
186 ConfigDialog::tms_ip_changed (wxCommandEvent &)
187 {
188         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
189 }
190
191 void
192 ConfigDialog::tms_path_changed (wxCommandEvent &)
193 {
194         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
195 }
196
197 void
198 ConfigDialog::tms_user_changed (wxCommandEvent &)
199 {
200         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
201 }
202
203 void
204 ConfigDialog::tms_password_changed (wxCommandEvent &)
205 {
206         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
207 }
208
209 void
210 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
211 {
212         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
213 }
214
215 void
216 ConfigDialog::colour_lut_changed (wxCommandEvent &)
217 {
218         Config::instance()->set_colour_lut_index (_colour_lut->GetSelection ());
219 }
220
221 void
222 ConfigDialog::j2k_bandwidth_changed (wxCommandEvent &)
223 {
224         Config::instance()->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
225 }
226
227 void
228 ConfigDialog::add_server_to_control (Server* s)
229 {
230         wxListItem item;
231         int const n = _servers->GetItemCount ();
232         item.SetId (n);
233         _servers->InsertItem (item);
234         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
235         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
236 }
237
238 void
239 ConfigDialog::add_server_clicked (wxCommandEvent &)
240 {
241         ServerDialog* d = new ServerDialog (this, 0);
242         d->ShowModal ();
243         Server* s = d->server ();
244         d->Destroy ();
245         
246         add_server_to_control (s);
247         vector<Server*> o = Config::instance()->servers ();
248         o.push_back (s);
249         Config::instance()->set_servers (o);
250 }
251
252 void
253 ConfigDialog::edit_server_clicked (wxCommandEvent &)
254 {
255         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
256         if (i == -1) {
257                 return;
258         }
259
260         wxListItem item;
261         item.SetId (i);
262         item.SetColumn (0);
263         _servers->GetItem (item);
264
265         vector<Server*> servers = Config::instance()->servers ();
266         vector<Server*>::iterator j = servers.begin();
267         while (j != servers.end() && (*j)->host_name() != wx_to_std (item.GetText ())) {
268                 ++j;
269         }
270
271         if (j == servers.end()) {
272                 return;
273         }
274
275         ServerDialog* d = new ServerDialog (this, *j);
276         d->ShowModal ();
277         d->Destroy ();
278
279         _servers->SetItem (i, 0, std_to_wx ((*j)->host_name ()));
280         _servers->SetItem (i, 1, std_to_wx (boost::lexical_cast<string> ((*j)->threads ())));
281 }
282
283 void
284 ConfigDialog::remove_server_clicked (wxCommandEvent &)
285 {
286         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
287         if (i >= 0) {
288                 _servers->DeleteItem (i);
289         }
290 }
291
292 void
293 ConfigDialog::server_selection_changed (wxListEvent& ev)
294 {
295         int const i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
296         _edit_server->Enable (i >= 0);
297         _remove_server->Enable (i >= 0);
298 }
299
300 void
301 ConfigDialog::reference_scaler_changed (wxCommandEvent &)
302 {
303         int const n = _reference_scaler->GetSelection ();
304         if (n >= 0) {
305                 Config::instance()->set_reference_scaler (Scaler::from_index (n));
306         }
307 }
308
309 void
310 ConfigDialog::edit_reference_filters_clicked (wxCommandEvent &)
311 {
312         FilterDialog* d = new FilterDialog (this, Config::instance()->reference_filters ());
313         d->ActiveChanged.connect (sigc::mem_fun (*this, &ConfigDialog::reference_filters_changed));
314         d->ShowModal ();
315         d->Destroy ();
316 }
317
318 void
319 ConfigDialog::reference_filters_changed (vector<Filter const *> f)
320 {
321         Config::instance()->set_reference_filters (f);
322         pair<string, string> p = Filter::ffmpeg_strings (Config::instance()->reference_filters ());
323         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
324 }