diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-06-21 10:16:00 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-07-10 23:13:14 +0200 |
| commit | 09b4dcf460d6fbe69139eca40f1c890ea5f33a5d (patch) | |
| tree | b9f5ecb37e62baa21ae24bf671de3b939589d0e7 /src/wx/ratio_picker.cc | |
| parent | 4e244c8a4b34268445123b7d6df54ff303561fa5 (diff) | |
Extract ratio setup UI to a separate class.
Diffstat (limited to 'src/wx/ratio_picker.cc')
| -rw-r--r-- | src/wx/ratio_picker.cc | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/wx/ratio_picker.cc b/src/wx/ratio_picker.cc new file mode 100644 index 000000000..39d2c4f2a --- /dev/null +++ b/src/wx/ratio_picker.cc @@ -0,0 +1,158 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "dcpomatic_choice.h" +#include "check_box.h" +#include "ratio_picker.h" +#include "wx_util.h" +#include "lib/ratio.h" +#include <dcp/locale_convert.h> +#include <dcp/scope_guard.h> + + +using boost::optional; + + +RatioPicker::RatioPicker(wxWindow* parent, optional<float> ratio) + : wxPanel(parent, wxID_ANY) +{ + _enable = new CheckBox(parent, _("Crop output to")); + _preset = new Choice(this); + _custom = new wxTextCtrl(this, wxID_ANY); + + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(_preset, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP); + sizer->Add(_custom, 0, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP); + add_label_to_sizer(sizer, this, _(":1"), false, 0, wxALIGN_CENTER_VERTICAL); + + for (auto ratio: Ratio::all()) { + _preset->add_entry(ratio.image_nickname(), ratio.id()); + } + _preset->add_entry(_("Custom"), "custom"); + + SetSizer(sizer); + Layout(); + + set(ratio); + + _enable->bind(&RatioPicker::enable_changed, this); + _preset->bind(&RatioPicker::preset_changed, this); + _custom->Bind(wxEVT_TEXT, boost::bind(&RatioPicker::custom_changed, this)); + + setup_sensitivity(); +} + + +void +RatioPicker::set(optional<float> ratio) +{ + _enable->set(ratio.has_value()); + set_preset(ratio); + set_custom(ratio); +} + + +void +RatioPicker::enable_changed() +{ + setup_sensitivity(); + if (_enable->get()) { + Changed(1.85); + } else { + Changed({}); + } +} + + +void +RatioPicker::setup_sensitivity() +{ + _preset->Enable(_enable->get()); + _custom->Enable(_enable->get()); +} + + +void +RatioPicker::preset_changed() +{ + if (!_enable->get() || _ignore_changes) { + return; + } + + optional<Ratio> ratio; + + if (auto data = _preset->get_data()) { + ratio = Ratio::from_id_if_exists(*data); + } + + optional<float> new_value; + if (ratio) { + new_value = ratio->ratio(); + } else { + new_value = dcp::locale_convert<float>(wx_to_std(_custom->GetValue())); + } + + set_custom(new_value); + Changed(new_value); +} + + +void +RatioPicker::custom_changed() +{ + if (!_enable->get() || _ignore_changes) { + return; + } + + auto const new_value = dcp::locale_convert<float>(wx_to_std(_custom->GetValue())); + set_preset(new_value); + Changed(new_value); +} + + +void +RatioPicker::set_preset(optional<float> ratio) +{ + _ignore_changes = true; + dcp::ScopeGuard sg = [this]() { _ignore_changes = false; }; + + if (ratio) { + auto preset = Ratio::from_ratio(*ratio); + _preset->set_by_data(preset ? preset->id() : "custom"); + } else { + _preset->set_by_data("185"); + } +} + + +void +RatioPicker::set_custom(optional<float> ratio) +{ + _ignore_changes = true; + dcp::ScopeGuard sg = [this]() { _ignore_changes = false; }; + + if (ratio) { + auto preset = Ratio::from_ratio(*ratio); + _custom->SetValue(wxString::Format(char_to_wx("%.2f"), preset ? preset->ratio() : *ratio)); + } else { + _custom->SetValue(_("1.85")); + } +} |
