34282895d86888185821d15c1da4719b86c5fc5e
[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
36 using namespace std;
37 using namespace boost;
38
39 ConfigDialog::ConfigDialog (wxWindow* parent)
40         : wxDialog (parent, wxID_ANY, _("DVD-o-matic Configuration"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
41 {
42         wxFlexGridSizer* table = new wxFlexGridSizer (3, 6, 6);
43         table->AddGrowableCol (1, 1);
44
45         add_label_to_sizer (table, this, "TMS IP address");
46         _tms_ip = new wxTextCtrl (this, wxID_ANY);
47         table->Add (_tms_ip, 1, wxEXPAND);
48         table->AddSpacer (0);
49
50         add_label_to_sizer (table, this, "TMS target path");
51         _tms_path = new wxTextCtrl (this, wxID_ANY);
52         table->Add (_tms_path, 1, wxEXPAND);
53         table->AddSpacer (0);
54
55         add_label_to_sizer (table, this, "TMS user name");
56         _tms_user = new wxTextCtrl (this, wxID_ANY);
57         table->Add (_tms_user, 1, wxEXPAND);
58         table->AddSpacer (0);
59
60         add_label_to_sizer (table, this, "TMS password");
61         _tms_password = new wxTextCtrl (this, wxID_ANY);
62         table->Add (_tms_password, 1, wxEXPAND);
63         table->AddSpacer (0);
64
65         add_label_to_sizer (table, this, "Threads to use for encoding on this host");
66         _num_local_encoding_threads = new wxSpinCtrl (this);
67         table->Add (_num_local_encoding_threads, 1, wxEXPAND);
68         table->AddSpacer (0);
69
70         add_label_to_sizer (table, this, "Colour look-up table");
71         _colour_lut = new wxComboBox (this, wxID_ANY);
72         _colour_lut->Append (wxT ("sRGB"));
73         _colour_lut->Append (wxT ("Rec 709"));
74         _colour_lut->SetSelection (0);
75         table->Add (_colour_lut, 1, wxEXPAND);
76         table->AddSpacer (0);
77
78         add_label_to_sizer (table, this, "JPEG2000 bandwidth");
79         _j2k_bandwidth = new wxSpinCtrl (this, wxID_ANY);
80         table->Add (_j2k_bandwidth, 1, wxEXPAND);
81         add_label_to_sizer (table, this, "MBps");
82
83         add_label_to_sizer (table, this, "Reference scaler for A/B");
84         _reference_scaler = new wxComboBox (this, wxID_ANY);
85         vector<Scaler const *> const sc = Scaler::all ();
86         for (vector<Scaler const *>::const_iterator i = sc.begin(); i != sc.end(); ++i) {
87                 _reference_scaler->Append (std_to_wx ((*i)->name ()));
88         }
89
90         table->Add (_reference_scaler, 1, wxEXPAND);
91         table->AddSpacer (0);
92
93         {
94                 add_label_to_sizer (table, this, "Reference filters for A/B");
95                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
96                 _reference_filters = new wxStaticText (this, wxID_ANY, wxT (""));
97                 s->Add (_reference_filters, 1, wxEXPAND);
98                 _reference_filters_button = new wxButton (this, wxID_ANY, _("Edit..."));
99                 s->Add (_reference_filters_button, 0);
100                 table->Add (s, 1, wxEXPAND);
101                 table->AddSpacer (0);
102         }
103
104         add_label_to_sizer (table, this, "Encoding Servers");
105         _servers = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
106         wxListItem ip;
107         ip.SetId (0);
108         ip.SetText (_("IP address"));
109         ip.SetWidth (100);
110         _servers->InsertColumn (0, ip);
111         ip.SetId (1);
112         ip.SetText (_("Threads"));
113         ip.SetWidth (90);
114         _servers->InsertColumn (1, ip);
115         table->Add (_servers, 1, wxEXPAND);
116
117         {
118                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
119                 _add_server = new wxButton (this, wxID_ANY, _("Add"));
120                 s->Add (_add_server);
121                 _remove_server = new wxButton (this, wxID_ANY, _("Remove"));
122                 s->Add (_remove_server);
123                 table->Add (s, 0);
124         }
125                 
126         Config* config = Config::instance ();
127
128         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
129         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
130         _tms_path->SetValue (std_to_wx (config->tms_path ()));
131         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
132         _tms_user->SetValue (std_to_wx (config->tms_user ()));
133         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
134         _tms_password->SetValue (std_to_wx (config->tms_password ()));
135         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
136
137         _num_local_encoding_threads->SetRange (1, 128);
138         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
139         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
140
141         _colour_lut->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::colour_lut_changed), 0, this);
142         
143         _j2k_bandwidth->SetRange (50, 250);
144         _j2k_bandwidth->SetValue (config->j2k_bandwidth() / 1e6);
145         _j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::j2k_bandwidth_changed), 0, this);
146
147         _reference_scaler->SetSelection (Scaler::as_index (config->reference_scaler ()));
148         _reference_scaler->Connect (wxID_ANY, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler (ConfigDialog::reference_scaler_changed), 0, this);
149
150         pair<string, string> p = Filter::ffmpeg_strings (config->reference_filters ());
151         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
152         _reference_filters_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_reference_filters_clicked), 0, this);
153
154         vector<Server*> servers = config->servers ();
155         for (vector<Server*>::iterator i = servers.begin(); i != servers.end(); ++i) {
156                 add_server_to_control (*i);
157         }
158         
159         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
160         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
161
162         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed));
163         wxListEvent ev;
164         server_selection_changed (ev);
165
166         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
167         overall_sizer->Add (table, 1, wxEXPAND);
168
169         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
170         if (buttons) {
171                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
172         }
173
174         SetSizer (overall_sizer);
175         overall_sizer->Layout ();
176         overall_sizer->SetSizeHints (this);
177 }
178
179 void
180 ConfigDialog::tms_ip_changed (wxCommandEvent &)
181 {
182         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
183 }
184
185 void
186 ConfigDialog::tms_path_changed (wxCommandEvent &)
187 {
188         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
189 }
190
191 void
192 ConfigDialog::tms_user_changed (wxCommandEvent &)
193 {
194         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
195 }
196
197 void
198 ConfigDialog::tms_password_changed (wxCommandEvent &)
199 {
200         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
201 }
202
203
204 void
205 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
206 {
207         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
208 }
209
210 void
211 ConfigDialog::colour_lut_changed (wxCommandEvent &)
212 {
213         Config::instance()->set_colour_lut_index (_colour_lut->GetSelection ());
214 }
215
216 void
217 ConfigDialog::j2k_bandwidth_changed (wxCommandEvent &)
218 {
219         Config::instance()->set_j2k_bandwidth (_j2k_bandwidth->GetValue() * 1e6);
220 }
221
222 void
223 ConfigDialog::add_server_to_control (Server* s)
224 {
225         wxListItem item;
226         int const n = _servers->GetItemCount ();
227         item.SetId (n);
228         _servers->InsertItem (item);
229         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
230         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
231 }
232
233 void
234 ConfigDialog::add_server_clicked (wxCommandEvent &)
235 {
236         Server s ("localhost", 1);
237         add_server_to_control (&s);
238 }
239
240 void
241 ConfigDialog::remove_server_clicked (wxCommandEvent &)
242 {
243         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
244         if (i >= 0) {
245                 _servers->DeleteItem (i);
246         }
247 }
248
249 void
250 ConfigDialog::server_selection_changed (wxListEvent& ev)
251 {
252 //      _remove_server->Enable (ev.GetIndex() >= 0);
253 }
254
255 void
256 ConfigDialog::reference_scaler_changed (wxCommandEvent &)
257 {
258         int const n = _reference_scaler->GetSelection ();
259         if (n >= 0) {
260                 Config::instance()->set_reference_scaler (Scaler::from_index (n));
261         }
262 }
263
264 void
265 ConfigDialog::edit_reference_filters_clicked (wxCommandEvent &)
266 {
267 //      FilterDialog d (Config::instance()->reference_filters ());
268 //      d.ActiveChanged.connect (sigc::mem_fun (*this, &ConfigDialog::reference_filters_changed));
269 //      d.run ();
270 }
271
272 void
273 ConfigDialog::reference_filters_changed (vector<Filter const *> f)
274 {
275         Config::instance()->set_reference_filters (f);
276         pair<string, string> p = Filter::ffmpeg_strings (Config::instance()->reference_filters ());
277         _reference_filters->SetLabel (std_to_wx (p.first + " " + p.second));
278 }