Hand-apply 33b76b675d747fd828aba91d9d857227cb8a8244 from master; make sure signals...
[dcpomatic.git] / src / wx / content_colour_conversion_dialog.cc
1 /*
2     Copyright (C) 2013-2015 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 #include <wx/statline.h>
21 #include "lib/colour_conversion.h"
22 #include "lib/config.h"
23 #include "lib/util.h"
24 #include "wx_util.h"
25 #include "content_colour_conversion_dialog.h"
26 #include "colour_conversion_editor.h"
27
28 using std::string;
29 using std::vector;
30 using std::cout;
31 using boost::optional;
32
33 ContentColourConversionDialog::ContentColourConversionDialog (wxWindow* parent)
34         : wxDialog (parent, wxID_ANY, _("Colour conversion"))
35         , _editor (new ColourConversionEditor (this))
36         , _setting (false)
37 {
38         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
39         SetSizer (overall_sizer);
40
41         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
42         _preset_check = new wxCheckBox (this, wxID_ANY, _("Use preset"));
43         table->Add (_preset_check, 0, wxALIGN_CENTER_VERTICAL);
44         _preset_choice = new wxChoice (this, wxID_ANY);
45         table->Add (_preset_choice);
46
47         overall_sizer->Add (table, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
48         overall_sizer->Add (new wxStaticLine (this, wxID_ANY), 0, wxEXPAND);
49         overall_sizer->Add (_editor);
50
51         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
52         if (buttons) {
53                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
54         }
55
56         overall_sizer->Layout ();
57         overall_sizer->SetSizeHints (this);
58
59         _preset_check->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&ContentColourConversionDialog::preset_check_clicked, this));
60         _preset_choice->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentColourConversionDialog::preset_choice_changed, this));
61
62         _editor_connection = _editor->Changed.connect (boost::bind (&ContentColourConversionDialog::check_for_preset, this));
63
64         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
65         for (vector<PresetColourConversion>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
66                 _preset_choice->Append (std_to_wx (i->name));
67         }
68 }
69
70 ColourConversion
71 ContentColourConversionDialog::get () const
72 {
73         return _editor->get ();
74 }
75
76 void
77 ContentColourConversionDialog::set (ColourConversion c)
78 {
79         _setting = true;
80         _editor->set (c);
81         _setting = false;
82
83         check_for_preset ();
84 }
85
86 void
87 ContentColourConversionDialog::check_for_preset ()
88 {
89         if (_setting) {
90                 return;
91         }
92         
93         optional<size_t> preset = _editor->get().preset ();
94
95         _preset_check->SetValue (preset);
96         _preset_choice->Enable (preset);
97         if (preset) {
98                 _preset_choice->SetSelection (preset.get ());
99         } else {
100                 _preset_choice->SetSelection (-1);
101         }
102 }
103
104 void
105 ContentColourConversionDialog::preset_check_clicked ()
106 {
107         if (_preset_check->GetValue ()) {
108                 _preset_choice->SetSelection (0);
109                 preset_choice_changed ();
110         } else {
111                 _preset_choice->SetSelection (-1);
112                 _preset_choice->Enable (false);
113         }
114 }
115
116 void
117 ContentColourConversionDialog::preset_choice_changed ()
118 {
119         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
120         int const s = _preset_choice->GetCurrentSelection();
121         if (s != -1) {
122                 set (presets[s].conversion);
123         }
124 }
125
126