Partial server dialog fixes.
[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         _colour_lut->Append (wxT ("sRGB"));
74         _colour_lut->Append (wxT ("Rec 709"));
75         _colour_lut->SetSelection (0);
76         table->Add (_colour_lut, 1, wxEXPAND);
77         table->AddSpacer (0);
78
79         add_label_to_sizer (table, this, "JPEG2000 bandwidth");
80         _j2k_bandwidth = new wxSpinCtrl (this, wxID_ANY);
81         table->Add (_j2k_bandwidth, 1, wxEXPAND);
82         add_label_to_sizer (table, this, "MBps");
83
84         add_label_to_sizer (table, this, "Reference scaler for A/B");
85         _reference_scaler = new wxComboBox (this, wxID_ANY);
86         vector<Scaler const *> const sc = Scaler::all ();
87         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
88                 _reference_scaler->Append (std_to_wx ((*i)->name ()));
89         }
90
91         table->Add (_reference_scaler, 1, wxEXPAND);
92         table->AddSpacer (0);
93
94         {
95                 add_label_to_sizer (table, this, "Reference filters for A/B");
96                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
97                 _reference_filters = new wxStaticText (this, wxID_ANY, wxT (""));
98                 s->Add (_reference_filters, 1, wxEXPAND);
99                 _reference_filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
100                 s->Add (_reference_filters_button, 0);
101                 table->Add (s, 1, wxEXPAND);
102                 table->AddSpacer (0);
103         }
104
105         add_label_to_sizer (table, this, "Encoding Servers");
106         _servers = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
107         wxListItem ip;
108         ip.SetId (0);
109         ip.SetText (_("IP address"));
110         ip.SetWidth (100);
111         _servers->InsertColumn (0, ip);
112         ip.SetId (1);
113         ip.SetText (_("Threads"));
114         ip.SetWidth (90);
115         _servers->InsertColumn (1, ip);
116         table->Add (_servers, 1, wxEXPAND);
117
118         {
119                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
120                 _add_server = new wxButton (this, wxID_ANY, _("Add"));
121                 s->Add (_add_server);
122                 _edit_server = new wxButton (this, wxID_ANY, _("Edit"));
123                 s->Add (_edit_server);
124                 _remove_server = new wxButton (this, wxID_ANY, _("Remove"));
125                 s->Add (_remove_server);
126                 table->Add (s, 0);
127         }
128                 
129         Config* config = Config::instance ();
130
131         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
132         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
133         _tms_path->SetValue (std_to_wx (config->tms_path ()));
134         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
135         _tms_user->SetValue (std_to_wx (config->tms_user ()));
136         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
137         _tms_password->SetValue (std_to_wx (config->tms_password ()));
138         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
139
140         _num_local_encoding_threads->SetRange (1, 128);
141         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
142         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
143
144         _colour_lut->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::colour_lut_changed), 0, this);
145         
146         _j2k_bandwidth->SetRange (50, 250);
147         _j2k_bandwidth->SetValue (config->j2k_bandwidth() / 1e6);
148         _j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::j2k_bandwidth_changed), 0, this);
149
150         _reference_scaler->SetSelection (Scaler::as_index (config->reference_scaler ()));
151         _reference_scaler->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::reference_scaler_changed), 0, this);
152
153         pair<string, string> p = Filter::ffmpeg_strings (config->reference_filters ());
154         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
155         _reference_filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_reference_filters_clicked), 0, this);
156
157         vector<Server*> servers = config->servers ();
158         for (vector<Server*>::iterator i = servers.begin(); i != servers.end(); ++i) {
159                 add_server_to_control (*i);
160         }
161         
162         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
163         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
164
165         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed));
166         wxListEvent ev;
167         server_selection_changed (ev);
168
169         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
170         overall_sizer->Add (table, 1, wxEXPAND);
171
172         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
173         if (buttons) {
174                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
175         }
176
177         SetSizer (overall_sizer);
178         overall_sizer->Layout ();
179         overall_sizer->SetSizeHints (this);
180 }
181
182 void
183 ConfigDialog::tms_ip_changed (wxCommandEvent &)
184 {
185         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
186 }
187
188 void
189 ConfigDialog::tms_path_changed (wxCommandEvent &)
190 {
191         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
192 }
193
194 void
195 ConfigDialog::tms_user_changed (wxCommandEvent &)
196 {
197         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
198 }
199
200 void
201 ConfigDialog::tms_password_changed (wxCommandEvent &)
202 {
203         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
204 }
205
206 void
207 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
208 {
209         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
210 }
211
212 void
213 ConfigDialog::colour_lut_changed (wxCommandEvent &)
214 {
215         Config::instance()->set_colour_lut_index (_colour_lut->GetSelection ());
216 }
217
218 void
219 ConfigDialog::j2k_bandwidth_changed (wxCommandEvent &)
220 {
221         Config::instance()->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
222 }
223
224 void
225 ConfigDialog::add_server_to_control (Server* s)
226 {
227         wxListItem item;
228         int const n = _servers->GetItemCount ();
229         item.SetId (n);
230         _servers->InsertItem (item);
231         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
232         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
233 }
234
235 void
236 ConfigDialog::add_server_clicked (wxCommandEvent &)
237 {
238         ServerDialog* d = new ServerDialog (this, 0);
239         d->ShowModal ();
240         Server* s = d->server ();
241         d->Destroy ();
242         
243         add_server_to_control (s);
244 }
245
246 void
247 ConfigDialog::edit_server_clicked (wxCommandEvent &)
248 {
249         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
250         if (i == -1) {
251                 return;
252         }
253
254         wxListItem item;
255         item.SetId (i);
256         item.SetColumn (0);
257         _servers->GetItem (item);
258
259         /* XXX: partial */
260 }
261
262 void
263 ConfigDialog::remove_server_clicked (wxCommandEvent &)
264 {
265         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
266         if (i >= 0) {
267                 _servers->DeleteItem (i);
268         }
269 }
270
271 void
272 ConfigDialog::server_selection_changed (wxListEvent& ev)
273 {
274 //      _remove_server->Enable (ev.GetIndex() >= 0);
275 }
276
277 void
278 ConfigDialog::reference_scaler_changed (wxCommandEvent &)
279 {
280         int const n = _reference_scaler->GetSelection ();
281         if (n >= 0) {
282                 Config::instance()->set_reference_scaler (Scaler::from_index (n));
283         }
284 }
285
286 void
287 ConfigDialog::edit_reference_filters_clicked (wxCommandEvent &)
288 {
289         FilterDialog* d = new FilterDialog (this, Config::instance()->reference_filters ());
290         d->ActiveChanged.connect (sigc::mem_fun (*this, &ConfigDialog::reference_filters_changed));
291         d->ShowModal ();
292         d->Destroy ();
293 }
294
295 void
296 ConfigDialog::reference_filters_changed (vector<Filter const *> f)
297 {
298         Config::instance()->set_reference_filters (f);
299         pair<string, string> p = Filter::ffmpeg_strings (Config::instance()->reference_filters ());
300         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
301 }