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