Clean up grok's presence in the config file and make sure it's optional.
[dcpomatic.git] / src / wx / grok / gpu_config_panel.h
index a0f2a1f7fdfd8ae99f6978c37d51ca478c260cb3..cbf03759227cd8896ea12371b31ee89cb45b2595 100644 (file)
@@ -41,170 +41,187 @@ static std::vector<std::string> get_gpu_names(boost::filesystem::path binary, bo
     return gpu_names;
 }
 
+
 class GpuList : public wxPanel
 {
 public:
-    GpuList(wxPanel* parent) : wxPanel(parent, wxID_ANY), selection(0) {
-        comboBox = new wxComboBox(this, wxID_ANY, "", wxDefaultPosition, wxSize(400, -1));
-        comboBox->Bind(wxEVT_COMBOBOX, &GpuList::OnComboBox, this);
-        update();
-
-        wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
+       GpuList(wxPanel* parent)
+               : wxPanel(parent, wxID_ANY)
+       {
+               _combo_box = new wxComboBox(this, wxID_ANY, "", wxDefaultPosition, wxSize(400, -1));
+               _combo_box->Bind(wxEVT_COMBOBOX, &GpuList::OnComboBox, this);
+               update();
 
-        sizer->Add(comboBox, 0, wxALIGN_CENTER_VERTICAL); // Vertically center the comboBox
+               auto sizer = new wxBoxSizer(wxHORIZONTAL);
+               sizer->Add(_combo_box, 0, wxALIGN_CENTER_VERTICAL);
+               SetSizerAndFit(sizer);
+       }
 
-        this->SetSizerAndFit(sizer);
-    }
-    void update(void) {
-       auto cfg = Config::instance();
-       auto lister_binary = cfg->gpu_binary_location() / "gpu_lister";
-       auto lister_file = cfg->gpu_binary_location () / "gpus.txt";
-       if (boost::filesystem::exists(lister_binary)) {
+       void update()
+       {
+               auto grok = Config::instance()->grok().get_value_or({});
+               auto lister_binary = grok.binary_location / "gpu_lister";
+               auto lister_file = grok.binary_location / "gpus.txt";
+               if (boost::filesystem::exists(lister_binary)) {
                        auto gpu_names = get_gpu_names(lister_binary, lister_file);
 
-                       comboBox->Clear();
-                       for (const auto& name : gpu_names)
-                                comboBox->Append(name);
-       }
-    }
+                       _combo_box->Clear();
+                       for (auto const& name: gpu_names) {
+                               _combo_box->Append(name);
+                       }
+               }
+       }
 
-    int getSelection(void) {
-       return selection;
-    }
-    void setSelection(int sel) {
-        if ((int)comboBox->GetCount() > sel)
-            comboBox->SetSelection(sel);
-    }
+       void set_selection(int sel)
+       {
+               if (sel < static_cast<int>(_combo_box->GetCount())) {
+                       _combo_box->SetSelection(sel);
+               }
+       }
 
 private:
-    void OnComboBox(wxCommandEvent&)
-    {
-        selection = comboBox->GetSelection();
-        if (selection != wxNOT_FOUND)
-               Config::instance ()->set_selected_gpu(selection);
-    }
+       void OnComboBox(wxCommandEvent&)
+       {
+               auto selection = _combo_box->GetSelection();
+               if (selection != wxNOT_FOUND) {
+                       auto grok = Config::instance()->grok().get_value_or({});
+                       grok.selected = selection;
+                       Config::instance()->set_grok(grok);
+               }
+       }
 
-    wxComboBox* comboBox;
-    int selection;
+       wxComboBox* _combo_box;
+       int _selection = 0;
 };
 
+
 class GPUPage : public Page
 {
 public:
-       GPUPage (wxSize panel_size, int border)
-               : Page (panel_size, border),
-                 _enable_gpu(nullptr), _binary_location(nullptr), _gpu_list_control(nullptr)
+       GPUPage(wxSize panel_size, int border)
+               : Page(panel_size, border)
        {}
 
-       wxString GetName () const override
+       wxString GetName() const override
        {
                return _("GPU");
        }
 
 #ifdef DCPOMATIC_OSX
-       wxBitmap GetLargeIcon () const override
+       /* XXX: this icon does not exist */
+       wxBitmap GetLargeIcon() const override
        {
-               return wxBitmap(icon_path("tms"), wxBITMAP_TYPE_PNG);
+               return wxBitmap(icon_path("gpu"), wxBITMAP_TYPE_PNG);
        }
 #endif
 
 private:
-       void setup () override
+       void setup() override
        {
-               auto config = Config::instance ();
-
                _enable_gpu = new CheckBox(_panel, _("Enable GPU acceleration"));
-               _panel->GetSizer()->Add (_enable_gpu, 0, wxALL | wxEXPAND, _border);
+               _panel->GetSizer()->Add(_enable_gpu, 0, wxALL | wxEXPAND, _border);
 
-               wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
-               table->AddGrowableCol (1, 1);
-               _panel->GetSizer()->Add (table, 1, wxALL | wxEXPAND, _border);
+               wxFlexGridSizer* table = new wxFlexGridSizer(2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
+               table->AddGrowableCol(1, 1);
+               _panel->GetSizer()->Add(table, 1, wxALL | wxEXPAND, _border);
 
                add_label_to_sizer(table, _panel, _("Acceleration binary folder"), true, 0, wxLEFT | wxLEFT | wxALIGN_CENTRE_VERTICAL);
-               _binary_location = new wxDirPickerCtrl (_panel, wxDD_DIR_MUST_EXIST);
-               table->Add (_binary_location, 1, wxEXPAND);
+               _binary_location = new wxDirPickerCtrl(_panel, wxDD_DIR_MUST_EXIST);
+               table->Add(_binary_location, 1, wxEXPAND);
 
                add_label_to_sizer(table, _panel, _("GPU selection"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
                _gpu_list_control = new GpuList(_panel);
-               table->Add (_gpu_list_control, 1, wxEXPAND);
+               table->Add(_gpu_list_control, 1, wxEXPAND);
 
                add_label_to_sizer(table, _panel, _("License server"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
-               _server = new wxTextCtrl (_panel, wxID_ANY);
-               table->Add (_server, 1, wxEXPAND | wxALL);
+               _server = new wxTextCtrl(_panel, wxID_ANY);
+               table->Add(_server, 1, wxEXPAND | wxALL);
 
-               add_label_to_sizer (table, _panel, _("Port"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
-               _port = new wxSpinCtrl (_panel, wxID_ANY);
-               _port->SetRange (0, 65535);
-               table->Add (_port);
+               add_label_to_sizer(table, _panel, _("Port"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
+               _port = new wxSpinCtrl(_panel, wxID_ANY);
+               _port->SetRange(0, 65535);
+               table->Add(_port);
 
-               add_label_to_sizer (table, _panel, _("License"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
-               _license = new PasswordEntry (_panel);
-               table->Add (_license->get_panel(), 1, wxEXPAND | wxALL);
+               add_label_to_sizer(table, _panel, _("License"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
+               _licence = new PasswordEntry(_panel);
+               table->Add(_licence->get_panel(), 1, wxEXPAND | wxALL);
 
                _enable_gpu->bind(&GPUPage::enable_gpu_changed, this);
-               _binary_location->Bind (wxEVT_DIRPICKER_CHANGED, boost::bind (&GPUPage::binary_location_changed, this));
-               _server->Bind (wxEVT_TEXT, boost::bind(&GPUPage::server_changed, this));
-               _port->Bind (wxEVT_SPINCTRL, boost::bind(&GPUPage::port_changed, this));
-               _license->Changed.connect (boost::bind(&GPUPage::license_changed, this));
-
-               _binary_location->Enable(config->enable_gpu());
-               _gpu_list_control->Enable(config->enable_gpu());
-               _server->Enable(config->enable_gpu());
-               _port->Enable(config->enable_gpu());
-               _license->get_panel()->Enable(config->enable_gpu());
+               _binary_location->Bind(wxEVT_DIRPICKER_CHANGED, boost::bind (&GPUPage::binary_location_changed, this));
+               _server->Bind(wxEVT_TEXT, boost::bind(&GPUPage::server_changed, this));
+               _port->Bind(wxEVT_SPINCTRL, boost::bind(&GPUPage::port_changed, this));
+               _licence->Changed.connect(boost::bind(&GPUPage::licence_changed, this));
+
+               setup_sensitivity();
        }
 
+       void setup_sensitivity()
+       {
+               auto grok = Config::instance()->grok().get_value_or({});
+
+               _binary_location->Enable(grok.enable);
+               _gpu_list_control->Enable(grok.enable);
+               _server->Enable(grok.enable);
+               _port->Enable(grok.enable);
+               _licence->get_panel()->Enable(grok.enable);
+       }
 
-       void config_changed () override
+       void config_changed() override
        {
-               auto config = Config::instance ();
+               auto grok = Config::instance()->grok().get_value_or({});
 
-               checked_set (_enable_gpu, config->enable_gpu());
-               _binary_location->SetPath(std_to_wx(config->gpu_binary_location().string()));
+               checked_set(_enable_gpu, grok.enable);
+               _binary_location->SetPath(std_to_wx(grok.binary_location.string()));
                _gpu_list_control->update();
-               _gpu_list_control->setSelection(config->selected_gpu());
-               checked_set (_server, config->gpu_license_server());
-               checked_set (_port, config->gpu_license_port());
-               checked_set (_license, config->gpu_license());
+               _gpu_list_control->set_selection(grok.selected);
+               checked_set(_server, grok.licence_server);
+               checked_set(_port, grok.licence_port);
+               checked_set(_licence, grok.licence);
        }
 
-       void enable_gpu_changed ()
+       void enable_gpu_changed()
        {
-               auto config = Config::instance ();
-
-               config->set_enable_gpu (_enable_gpu->GetValue());
-               _binary_location->Enable(config->enable_gpu());
-               _gpu_list_control->Enable(config->enable_gpu());
-               _server->Enable(config->enable_gpu());
-               _port->Enable(config->enable_gpu());
-               _license->get_panel()->Enable(config->enable_gpu());
+               auto grok = Config::instance()->grok().get_value_or({});
+               grok.enable = _enable_gpu->GetValue();
+               Config::instance()->set_grok(grok);
+
+               setup_sensitivity();
        }
 
-       void binary_location_changed ()
+       void binary_location_changed()
        {
-               Config::instance()->set_gpu_binary_location (wx_to_std (_binary_location->GetPath ()));
+               auto grok = Config::instance()->grok().get_value_or({});
+               grok.binary_location = wx_to_std(_binary_location->GetPath());
+               Config::instance()->set_grok(grok);
+
                _gpu_list_control->update();
        }
 
-       void server_changed ()
+       void server_changed()
        {
-               Config::instance()->set_gpu_license_server(wx_to_std(_server->GetValue()));
+               auto grok = Config::instance()->grok().get_value_or({});
+               grok.licence_server = wx_to_std(_server->GetValue());
+               Config::instance()->set_grok(grok);
        }
 
-       void port_changed ()
+       void port_changed()
        {
-               Config::instance()->set_gpu_license_port(_port->GetValue());
+               auto grok = Config::instance()->grok().get_value_or({});
+               grok.licence_port = _port->GetValue();
+               Config::instance()->set_grok(grok);
        }
 
-       void license_changed ()
+       void licence_changed()
        {
-               Config::instance()->set_gpu_license(_license->get());
+               auto grok = Config::instance()->grok().get_value_or({});
+               grok.licence = wx_to_std(_licence->get());
+               Config::instance()->set_grok(grok);
        }
 
-       CheckBox* _enable_gpu;
-       wxDirPickerCtrl* _binary_location;
-       GpuList *_gpu_list_control;
-       wxTextCtrl* _server;
-       wxSpinCtrl* _port;
-       PasswordEntry* _license;
+       CheckBox* _enable_gpu = nullptr;
+       wxDirPickerCtrl* _binary_location = nullptr;
+       GpuList* _gpu_list_control = nullptr;
+       wxTextCtrl* _server = nullptr;
+       wxSpinCtrl* _port = nullptr;
+       PasswordEntry* _licence = nullptr;
 };