2 Copyright (C) 2019-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "dcpomatic_spin_ctrl.h"
23 #include "rating_dialog.h"
25 #include <unicode/unistr.h>
26 #include <wx/listctrl.h>
27 #include <wx/notebook.h>
28 #include <wx/srchctrl.h>
33 using boost::optional;
34 #if BOOST_VERSION >= 106100
35 using namespace boost::placeholders;
39 RatingDialog::RatingDialog (wxWindow* parent)
40 : wxDialog (parent, wxID_ANY, _("Rating"))
42 _notebook = new wxNotebook (this, wxID_ANY);
44 _standard_page = new StandardRatingDialogPage (_notebook);
45 _custom_page = new CustomRatingDialogPage (_notebook);
47 _notebook->AddPage (_standard_page, _("Standard"));
48 _notebook->AddPage (_custom_page, _("Custom"));
50 _active_page = _standard_page;
52 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
53 overall_sizer->Add (_notebook, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
55 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
57 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
60 SetSizerAndFit (overall_sizer);
62 _notebook->Bind(wxEVT_NOTEBOOK_PAGE_CHANGED, boost::bind(&RatingDialog::page_changed, this));
64 _standard_page->Changed.connect(boost::bind(&RatingDialog::setup_sensitivity, this, _1));
65 _custom_page->Changed.connect(boost::bind(&RatingDialog::setup_sensitivity, this, _1));
70 RatingDialog::page_changed ()
72 if (_notebook->GetSelection() == 0) {
73 _active_page = _standard_page;
75 _active_page = _custom_page;
81 RatingDialog::set (dcp::Rating rating)
83 if (_standard_page->set(rating)) {
84 _notebook->SetSelection(0);
86 _custom_page->set(rating);
87 _notebook->SetSelection(1);
93 RatingDialog::get () const
95 return _active_page->get();
100 RatingDialog::setup_sensitivity (bool ok_valid)
102 auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this));
104 ok->Enable (ok_valid);
109 RatingDialogPage::RatingDialogPage (wxNotebook* notebook)
110 : wxPanel (notebook, wxID_ANY)
116 StandardRatingDialogPage::StandardRatingDialogPage (wxNotebook* notebook)
117 : RatingDialogPage (notebook)
119 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
121 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
122 _search->ShowCancelButton (true);
125 _found_systems_view = new wxListView (this, wxID_ANY, wxDefaultPosition, wxSize(600, 400), wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
126 _found_systems_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 150);
127 _found_systems_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 50);
128 _found_systems_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 400);
129 _rating = new wxChoice (this, wxID_ANY);
131 auto sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
133 add_label_to_sizer (sizer, this, _("Agency"), true, 0, wxALIGN_CENTER_VERTICAL);
134 sizer->Add (_search, 0, wxEXPAND, DCPOMATIC_SIZER_Y_GAP);
136 sizer->AddSpacer (0);
137 sizer->Add (_found_systems_view, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
139 add_label_to_sizer (sizer, this, _("Rating"), true, 0, wxALIGN_CENTER_VERTICAL);
140 sizer->Add (_rating, 1, wxEXPAND);
142 auto pad_sizer = new wxBoxSizer (wxVERTICAL);
143 pad_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
145 SetSizerAndFit (pad_sizer);
147 _search->Bind (wxEVT_TEXT, boost::bind(&StandardRatingDialogPage::search_changed, this));
148 _found_systems_view->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&StandardRatingDialogPage::found_systems_view_selection_changed, this));
149 _found_systems_view->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&StandardRatingDialogPage::found_systems_view_selection_changed, this));
155 /** The user clicked something different in the list of systems found by the search */
157 StandardRatingDialogPage::found_systems_view_selection_changed ()
159 auto selected_index = _found_systems_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
160 if (selected_index < 0 || selected_index >= static_cast<int>(_found_systems.size())) {
161 _selected_system = boost::none;
163 _selected_system = _found_systems[selected_index];
166 /* Update the ratings dropdown */
168 if (_selected_system) {
169 for (auto rating: _selected_system->ratings) {
170 items.Add(std_to_wx(rating.label));
176 if (!items.empty()) {
177 _rating->SetSelection(0);
180 Changed (static_cast<bool>(_selected_system));
185 StandardRatingDialogPage::search_changed ()
187 _found_systems_view->DeleteAllItems();
188 _found_systems.clear();
190 icu::UnicodeString term(wx_to_std(_search->GetValue()).c_str(), "UTF-8");
191 term = term.toLower();
194 for (auto const& system: dcp::rating_systems()) {
195 icu::UnicodeString name(system.name.c_str(), "UTF-8");
196 name = name.toLower();
197 icu::UnicodeString country_and_region_names(system.country_and_region_names.c_str(), "UTF-8");
198 country_and_region_names = country_and_region_names.toLower();
199 icu::UnicodeString country_code(system.country_code.c_str(), "UTF-8");
200 country_code = country_code.toLower();
201 if (term.isEmpty() || name.indexOf(term) != -1 || country_and_region_names.indexOf(term) != -1 || country_code.indexOf(term) != -1) {
204 _found_systems_view->InsertItem(item);
205 _found_systems_view->SetItem(N, 0, std_to_wx(system.name));
206 _found_systems_view->SetItem(N, 1, std_to_wx(system.country_code));
207 _found_systems_view->SetItem(N, 2, std_to_wx(system.country_and_region_names));
208 _found_systems.push_back(system);
213 update_found_system_selection ();
217 /** Reflect _selected_system in the current _found_systems_view */
219 StandardRatingDialogPage::update_found_system_selection ()
221 if (!_selected_system) {
222 for (auto i = 0; i < _found_systems_view->GetItemCount(); ++i) {
223 _found_systems_view->Select(i, false);
229 for (auto const& system: _found_systems) {
230 bool const selected = system.agency == _selected_system->agency;
231 _found_systems_view->Select(index, selected);
233 _found_systems_view->EnsureVisible(index);
241 StandardRatingDialogPage::set (dcp::Rating rating)
243 _selected_system = boost::none;
244 for (auto const& system: dcp::rating_systems()) {
245 if (system.agency == rating.agency) {
246 _selected_system = system;
251 if (!_selected_system) {
255 update_found_system_selection ();
257 int rating_index = 0;
258 for (auto const& possible_rating: _selected_system->ratings) {
259 if (possible_rating.label == rating.label) {
260 _rating->SetSelection (rating_index);
271 StandardRatingDialogPage::get () const
273 DCPOMATIC_ASSERT (_selected_system);
274 auto selected_rating = _rating->GetSelection();
275 DCPOMATIC_ASSERT (selected_rating >= 0);
276 DCPOMATIC_ASSERT (selected_rating < static_cast<int>(_selected_system->ratings.size()));
277 return dcp::Rating(_selected_system->agency, _selected_system->ratings[selected_rating].label);
281 CustomRatingDialogPage::CustomRatingDialogPage (wxNotebook* notebook)
282 : RatingDialogPage (notebook)
284 auto sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
286 _agency = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1));
287 _rating = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1));
289 add_label_to_sizer (sizer, this, _("Agency"), true, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
290 sizer->Add (_agency, 1, wxEXPAND);
291 add_label_to_sizer (sizer, this, _("Rating"), true, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL);
292 sizer->Add (_rating, 1, wxEXPAND);
294 auto pad_sizer = new wxBoxSizer (wxVERTICAL);
295 pad_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
297 SetSizerAndFit (pad_sizer);
299 _agency->Bind(wxEVT_TEXT, boost::bind(&CustomRatingDialogPage::changed, this));
300 _rating->Bind(wxEVT_TEXT, boost::bind(&CustomRatingDialogPage::changed, this));
305 CustomRatingDialogPage::changed ()
307 Changed (!_agency->IsEmpty() && !_rating->IsEmpty());
312 CustomRatingDialogPage::get () const
314 return dcp::Rating(wx_to_std(_agency->GetValue()), wx_to_std(_rating->GetValue()));
319 CustomRatingDialogPage::set (dcp::Rating rating)
321 _agency->SetValue(std_to_wx(rating.agency));
322 _rating->SetValue(std_to_wx(rating.label));