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