X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fwx%2Fscreens_panel.cc;h=d3b1db77d4e36da0a04323244ac368922ec8789b;hb=d5f5a9d9f5635f84a5e372181dea1c7cbb0ae732;hp=52d9fd88d2085682b1fb3fe70f3969e9a521463b;hpb=bac28a7b12f7a3df11fc7c9a06d67dd175d24809;p=dcpomatic.git diff --git a/src/wx/screens_panel.cc b/src/wx/screens_panel.cc index 52d9fd88d..d3b1db77d 100644 --- a/src/wx/screens_panel.cc +++ b/src/wx/screens_panel.cc @@ -19,6 +19,7 @@ */ +#include "check_box.h" #include "cinema_dialog.h" #include "dcpomatic_button.h" #include "screen_dialog.h" @@ -28,14 +29,11 @@ #include "lib/config.h" #include "lib/screen.h" #include "lib/timer.h" -#include -#include -#include -#include -#include +#include using std::cout; +using std::list; using std::make_pair; using std::make_shared; using std::map; @@ -44,20 +42,30 @@ using std::shared_ptr; using std::string; using std::vector; using boost::optional; +#if BOOST_VERSION >= 106100 +using namespace boost::placeholders; +#endif using namespace dcpomatic; ScreensPanel::ScreensPanel (wxWindow* parent) : wxPanel (parent, wxID_ANY) { - auto sizer = new wxBoxSizer (wxVERTICAL); + _overall_sizer = new wxBoxSizer(wxVERTICAL); + + auto search_sizer = new wxBoxSizer(wxHORIZONTAL); _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height())); #ifndef __WXGTK3__ /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */ _search->ShowCancelButton (true); #endif - sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP); + search_sizer->Add(_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP); + + _show_only_checked = new CheckBox(this, _("Show only checked")); + search_sizer->Add(_show_only_checked, 1, wxEXPAND | wxLEFT | wxBOTTOM, DCPOMATIC_SIZER_GAP); + + _overall_sizer->Add(search_sizer); auto targets = new wxBoxSizer (wxHORIZONTAL); _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER); @@ -97,11 +105,13 @@ ScreensPanel::ScreensPanel (wxWindow* parent) targets->Add (side_buttons, 0, 0); - sizer->Add (targets, 1, wxEXPAND); + _overall_sizer->Add (targets, 1, wxEXPAND); - _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this)); + _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::display_filter_changed, this)); + _show_only_checked->Bind(wxEVT_CHECKBOX, boost::bind(&ScreensPanel::display_filter_changed, this)); _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this); _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this); + _targets->Bind (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this); _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this)); _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this)); @@ -112,17 +122,11 @@ ScreensPanel::ScreensPanel (wxWindow* parent) _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this)); _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this)); - _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this)); + _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this)); - SetSizer (sizer); + SetSizer(_overall_sizer); - UErrorCode status = U_ZERO_ERROR; - _collator = ucol_open(nullptr, &status); - if (_collator) { - ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); - ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); - ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); - } + _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1)); } @@ -130,10 +134,6 @@ ScreensPanel::~ScreensPanel () { _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this); _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this); - - if (_collator) { - ucol_close (_collator); - } } @@ -175,19 +175,43 @@ ScreensPanel::setup_sensitivity () _add_screen->Enable (sc || ss); _edit_screen->Enable (ss); _remove_screen->Enable (_selected_screens.size() >= 1); + + _show_only_checked->Enable(!_checked_screens.empty()); +} + + +void +ScreensPanel::convert_to_lower(string& s) +{ + transform(s.begin(), s.end(), s.begin(), ::tolower); +} + + +bool +ScreensPanel::matches_search(shared_ptr cinema, string search) +{ + if (search.empty()) { + return true; + } + + return _collator.find(search, cinema->name); } optional ScreensPanel::add_cinema (shared_ptr cinema, wxTreeListItem previous) { - auto search = wx_to_std (_search->GetValue ()); - transform (search.begin(), search.end(), search.begin(), ::tolower); + auto const search = wx_to_std(_search->GetValue()); + if (!matches_search(cinema, search)) { + return {}; + } - if (!search.empty ()) { - auto name = cinema->name; - transform (name.begin(), name.end(), name.begin(), ::tolower); - if (name.find (search) == string::npos) { + if (_show_only_checked->get()) { + auto screens = cinema->screens(); + auto iter = std::find_if(screens.begin(), screens.end(), [this](shared_ptr screen) { + return _checked_screens.find(screen) != _checked_screens.end(); + }); + if (iter == screens.end()) { return {}; } } @@ -225,37 +249,48 @@ ScreensPanel::add_screen (shared_ptr cinema, shared_ptr screen) void ScreensPanel::add_cinema_clicked () { - auto d = new CinemaDialog (GetParent(), _("Add Cinema")); - if (d->ShowModal () == wxID_OK) { - auto cinema = make_shared(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()); + CinemaDialog dialog(GetParent(), _("Add Cinema")); - auto cinemas = Config::instance()->cinemas(); - cinemas.sort( - [this](shared_ptr a, shared_ptr b) { return compare(a->name, b->name) < 0; } - ); + if (dialog.ShowModal() == wxID_OK) { + auto cinema = make_shared(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute()); - optional item; + auto cinemas = sorted_cinemas(); + + try { + _ignore_cinemas_changed = true; + dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; }; + Config::instance()->add_cinema(cinema); + } catch (FileError& e) { + error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file. Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what())); + return; + } + + wxTreeListItem previous = wxTLI_FIRST; + bool found = false; + auto const search = wx_to_std(_search->GetValue()); for (auto existing_cinema: cinemas) { - if (!item && compare(d->name(), existing_cinema->name) < 0) { - if (auto existing_item = cinema_to_item(existing_cinema)) { - item = add_cinema (cinema, *existing_item); - } + if (!matches_search(existing_cinema, search)) { + continue; + } + if (_collator.compare(dialog.name(), existing_cinema->name) < 0) { + /* existing_cinema should be after the one we're inserting */ + found = true; + break; } + auto item = cinema_to_item(existing_cinema); + DCPOMATIC_ASSERT(item); + previous = *item; } - if (!item) { - item = add_cinema (cinema, wxTLI_LAST); - } + auto item = add_cinema(cinema, found ? previous : wxTLI_LAST); if (item) { _targets->UnselectAll (); _targets->Select (*item); } - - Config::instance()->add_cinema (cinema); } - d->Destroy (); + selection_changed (); } @@ -276,27 +311,30 @@ void ScreensPanel::edit_cinema_clicked () { auto cinema = cinema_for_operation (); - if (!cinema) { - return; + if (cinema) { + edit_cinema(cinema); } +} - auto d = new CinemaDialog ( + +void +ScreensPanel::edit_cinema(shared_ptr cinema) +{ + CinemaDialog dialog( GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute() ); - if (d->ShowModal() == wxID_OK) { - cinema->name = d->name(); - cinema->emails = d->emails(); - cinema->notes = d->notes(); - cinema->set_utc_offset_hour(d->utc_offset_hour()); - cinema->set_utc_offset_minute(d->utc_offset_minute()); + if (dialog.ShowModal() == wxID_OK) { + cinema->name = dialog.name(); + cinema->emails = dialog.emails(); + cinema->notes = dialog.notes(); + cinema->set_utc_offset_hour(dialog.utc_offset_hour()); + cinema->set_utc_offset_minute(dialog.utc_offset_minute()); + notify_cinemas_changed(); auto item = cinema_to_item(cinema); DCPOMATIC_ASSERT(item); - _targets->SetItemText (*item, std_to_wx(d->name())); - Config::instance()->changed (Config::CINEMAS); + _targets->SetItemText (*item, std_to_wx(dialog.name())); } - - d->Destroy (); } @@ -313,7 +351,14 @@ ScreensPanel::remove_cinema_clicked () } } - for (auto const& cinema: _selected_cinemas) { + auto cinemas_to_remove = _selected_cinemas; + + for (auto const& cinema: cinemas_to_remove) { + _ignore_cinemas_changed = true; + dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; }; + for (auto screen: cinema->screens()) { + _checked_screens.erase(screen); + } Config::instance()->remove_cinema(cinema); auto item = cinema_to_item(cinema); DCPOMATIC_ASSERT(item); @@ -321,6 +366,7 @@ ScreensPanel::remove_cinema_clicked () } selection_changed (); + setup_show_only_checked(); } @@ -332,48 +378,49 @@ ScreensPanel::add_screen_clicked () return; } - auto d = new ScreenDialog (GetParent(), _("Add Screen")); - if (d->ShowModal () != wxID_OK) { - d->Destroy (); + ScreenDialog dialog(GetParent(), _("Add Screen")); + + if (dialog.ShowModal () != wxID_OK) { return; } for (auto screen: cinema->screens()) { - if (screen->name == d->name()) { + if (screen->name == dialog.name()) { error_dialog ( GetParent(), wxString::Format ( _("You cannot add a screen called '%s' as the cinema already has a screen with this name."), - std_to_wx(d->name()).data() + std_to_wx(dialog.name()).data() ) ); return; } } - auto screen = std::make_shared(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices()); + auto screen = std::make_shared(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices()); cinema->add_screen (screen); + notify_cinemas_changed(); + auto id = add_screen (cinema, screen); if (id) { _targets->Expand (id.get ()); } - - Config::instance()->changed (Config::CINEMAS); - - d->Destroy (); } void ScreensPanel::edit_screen_clicked () { - if (_selected_screens.size() != 1) { - return; + if (_selected_screens.size() == 1) { + edit_screen(_selected_screens[0]); } +} - auto edit_screen = _selected_screens[0]; - auto d = new ScreenDialog ( +void +ScreensPanel::edit_screen(shared_ptr edit_screen) +{ + ScreenDialog dialog( GetParent(), _("Edit screen"), edit_screen->name, edit_screen->notes, @@ -382,36 +429,34 @@ ScreensPanel::edit_screen_clicked () edit_screen->trusted_devices ); - if (d->ShowModal() != wxID_OK) { - d->Destroy (); + if (dialog.ShowModal() != wxID_OK) { return; } auto cinema = edit_screen->cinema; for (auto screen: cinema->screens()) { - if (screen != edit_screen && screen->name == d->name()) { + if (screen != edit_screen && screen->name == dialog.name()) { error_dialog ( GetParent(), wxString::Format ( _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."), - std_to_wx(d->name()).data() + std_to_wx(dialog.name()).data() ) ); return; } } - edit_screen->name = d->name(); - edit_screen->notes = d->notes(); - edit_screen->recipient = d->recipient(); - edit_screen->recipient_file = d->recipient_file(); - edit_screen->trusted_devices = d->trusted_devices(); + edit_screen->name = dialog.name(); + edit_screen->notes = dialog.notes(); + edit_screen->recipient = dialog.recipient(); + edit_screen->recipient_file = dialog.recipient_file(); + edit_screen->trusted_devices = dialog.trusted_devices(); + notify_cinemas_changed(); + auto item = screen_to_item(edit_screen); DCPOMATIC_ASSERT (item); - _targets->SetItemText (*item, std_to_wx(d->name())); - Config::instance()->changed (Config::CINEMAS); - - d->Destroy (); + _targets->SetItemText(*item, std_to_wx(dialog.name())); } @@ -428,14 +473,20 @@ ScreensPanel::remove_screen_clicked () } } - for (auto const& screen: _selected_screens) { + for (auto screen: _selected_screens) { + _checked_screens.erase(screen); screen->cinema->remove_screen(screen); auto item = screen_to_item(screen); DCPOMATIC_ASSERT(item); _targets->DeleteItem(*item); } - Config::instance()->changed (Config::CINEMAS); + /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves + * as well. + */ + selection_changed(); + notify_cinemas_changed(); + setup_show_only_checked(); } @@ -481,22 +532,30 @@ ScreensPanel::selection_changed () } -void -ScreensPanel::add_cinemas () +list> +ScreensPanel::sorted_cinemas() const { auto cinemas = Config::instance()->cinemas(); + cinemas.sort( - [this](shared_ptr a, shared_ptr b) { return compare(a->name, b->name) < 0; } + [this](shared_ptr a, shared_ptr b) { return _collator.compare(a->name, b->name) < 0; } ); - for (auto cinema: cinemas) { + return cinemas; +} + + +void +ScreensPanel::add_cinemas () +{ + for (auto cinema: sorted_cinemas()) { add_cinema (cinema, wxTLI_LAST); } } void -ScreensPanel::search_changed () +ScreensPanel::clear_and_re_add() { _targets->DeleteAllItems (); @@ -506,6 +565,14 @@ ScreensPanel::search_changed () _screen_to_item.clear (); add_cinemas (); +} + + +/** Search and/or "show only checked" changed */ +void +ScreensPanel::display_filter_changed() +{ + clear_and_re_add(); _ignore_selection_change = true; @@ -546,6 +613,8 @@ ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked) } else { _checked_screens.erase(screen); } + + setup_show_only_checked(); } @@ -644,17 +713,57 @@ ScreensPanel::screen_to_item (shared_ptr screen) const } -int -ScreensPanel::compare (string const& utf8_a, string const& utf8_b) +bool +ScreensPanel::notify_cinemas_changed() +{ + _ignore_cinemas_changed = true; + dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; }; + + try { + Config::instance()->changed(Config::CINEMAS); + } catch (FileError& e) { + error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file. Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what())); + return false; + } + + return true; +} + + +void +ScreensPanel::config_changed(Config::Property property) +{ + if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) { + clear_and_re_add(); + } +} + + +void +ScreensPanel::item_activated(wxTreeListEvent& ev) { - if (_collator) { - UErrorCode error = U_ZERO_ERROR; - boost::scoped_array utf16_a(new uint16_t[utf8_a.size() + 1]); - u_strFromUTF8(reinterpret_cast(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error); - boost::scoped_array utf16_b(new uint16_t[utf8_b.size() + 1]); - u_strFromUTF8(reinterpret_cast(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error); - return ucol_strcoll(_collator, reinterpret_cast(utf16_a.get()), -1, reinterpret_cast(utf16_b.get()), -1); + auto iter = _item_to_cinema.find(ev.GetItem()); + if (iter != _item_to_cinema.end()) { + edit_cinema(iter->second); } else { - return strcoll(utf8_a.c_str(), utf8_b.c_str()); + auto iter = _item_to_screen.find(ev.GetItem()); + if (iter != _item_to_screen.end()) { + edit_screen(iter->second); + } } } + + +void +ScreensPanel::setup_show_only_checked() +{ + if (_checked_screens.empty()) { + _show_only_checked->set_text(_("Show only checked")); + } else { + _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast(_checked_screens.size()))); + } + + _overall_sizer->Layout(); + setup_sensitivity(); +} +