X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Fwx%2Fscreens_panel.cc;h=6c5b28b8d0fdd048449ce2c0e3a0bdfc8994d3a3;hp=a8138a8930d420882da92e44f965aff62b7afa8a;hb=da13c2af9e8f19229061ca1939cbe8bd37c72eba;hpb=f6caaae7747ed481fbaddcaa6afcfdefbbfc545a diff --git a/src/wx/screens_panel.cc b/src/wx/screens_panel.cc index a8138a893..6c5b28b8d 100644 --- a/src/wx/screens_panel.cc +++ b/src/wx/screens_panel.cc @@ -1,314 +1,724 @@ /* - Copyright (C) 2015 Carl Hetherington + Copyright (C) 2015-2022 Carl Hetherington - This program is free software; you can redistribute it and/or modify + 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. - This program is distributed in the hope that it will be useful, + 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 this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ -#include "lib/config.h" -#include "lib/cinema.h" -#include "lib/screen.h" -#include "screens_panel.h" -#include "wx_util.h" + #include "cinema_dialog.h" +#include "dcpomatic_button.h" #include "screen_dialog.h" +#include "screens_panel.h" +#include "wx_util.h" +#include "lib/cinema.h" +#include "lib/config.h" +#include "lib/scope_guard.h" +#include "lib/screen.h" +#include "lib/timer.h" + -using std::list; -using std::pair; using std::cout; -using std::map; +using std::list; using std::make_pair; -using boost::shared_ptr; +using std::make_shared; +using std::map; +using std::pair; +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) { - wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL); + auto sizer = new wxBoxSizer (wxVERTICAL); - wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL); - _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT); - targets->Add (_targets, 1, wxEXPAND | wxTOP | wxRIGHT, DCPOMATIC_SIZER_GAP); + _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); - _root = _targets->AddRoot ("Foo"); + auto targets = new wxBoxSizer (wxHORIZONTAL); + _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER); + _targets->AppendColumn (wxT("foo")); - list > c = Config::instance()->cinemas (); - for (list >::iterator i = c.begin(); i != c.end(); ++i) { - add_cinema (*i); - } + targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP); - _targets->ExpandAll (); + add_cinemas (); - wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL); + auto side_buttons = new wxBoxSizer (wxVERTICAL); - _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema...")); - target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); - _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema...")); - target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); - _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema")); - target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); + auto target_buttons = new wxBoxSizer (wxVERTICAL); - _add_screen = new wxButton (this, wxID_ANY, _("Add Screen...")); - target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); - _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen...")); - target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); - _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen")); - target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP); + _add_cinema = new Button (this, _("Add Cinema...")); + target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _edit_cinema = new Button (this, _("Edit Cinema...")); + target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _remove_cinema = new Button (this, _("Remove Cinema")); + target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _add_screen = new Button (this, _("Add Screen...")); + target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _edit_screen = new Button (this, _("Edit Screen...")); + target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _remove_screen = new Button (this, _("Remove Screen")); + target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); - targets->Add (target_buttons, 0, 0); + side_buttons->Add (target_buttons, 0, 0); + + auto check_buttons = new wxBoxSizer (wxVERTICAL); + + _check_all = new Button (this, _("Check all")); + check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + _uncheck_all = new Button (this, _("Uncheck all")); + check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP); + + side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8); + + targets->Add (side_buttons, 0, 0); sizer->Add (targets, 1, wxEXPAND); - _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this); + _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_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)); + _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this)); - _add_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this)); - _edit_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this)); - _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this)); + _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this)); + _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this)); + _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this)); - _add_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this)); - _edit_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this)); - _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, 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)); SetSizer (sizer); + + _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1)); } + ScreensPanel::~ScreensPanel () { - _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this); + _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this); + _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this); } -list > > -ScreensPanel::selected_cinemas () const -{ - wxArrayTreeItemIds s; - _targets->GetSelections (s); - list > > c; - for (size_t i = 0; i < s.GetCount(); ++i) { - map >::const_iterator j = _cinemas.find (s[i]); - if (j != _cinemas.end ()) { - c.push_back (make_pair (j->first, j->second)); +void +ScreensPanel::check_all () +{ + for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) { + _targets->CheckItem(cinema, wxCHK_CHECKED); + for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) { + _targets->CheckItem(screen, wxCHK_CHECKED); + set_screen_checked(screen, true); } } - - return c; } -list > > -ScreensPanel::selected_screens () const -{ - wxArrayTreeItemIds s; - _targets->GetSelections (s); - list > > c; - for (size_t i = 0; i < s.GetCount(); ++i) { - map >::const_iterator j = _screens.find (s[i]); - if (j != _screens.end ()) { - c.push_back (make_pair (j->first, j->second)); +void +ScreensPanel::uncheck_all () +{ + for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) { + _targets->CheckItem(cinema, wxCHK_UNCHECKED); + for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) { + _targets->CheckItem(screen, wxCHK_UNCHECKED); + set_screen_checked(screen, false); } } - - return c; } + void ScreensPanel::setup_sensitivity () { - bool const sc = selected_cinemas().size() == 1; - bool const ss = selected_screens().size() == 1; + bool const sc = _selected_cinemas.size() == 1; + bool const ss = _selected_screens.size() == 1; - _edit_cinema->Enable (sc); - _remove_cinema->Enable (sc); + _edit_cinema->Enable (sc || ss); + _remove_cinema->Enable (_selected_cinemas.size() >= 1); - _add_screen->Enable (sc); + _add_screen->Enable (sc || ss); _edit_screen->Enable (ss); - _remove_screen->Enable (ss); + _remove_screen->Enable (_selected_screens.size() >= 1); } void -ScreensPanel::add_cinema (shared_ptr c) +ScreensPanel::convert_to_lower(string& s) { - _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c; + transform(s.begin(), s.end(), s.begin(), ::tolower); +} + - list > sc = c->screens (); - for (list >::iterator i = sc.begin(); i != sc.end(); ++i) { - add_screen (c, *i); +bool +ScreensPanel::matches_search(shared_ptr cinema, string search) +{ + if (search.empty()) { + return true; } - _targets->SortChildren (_root); + return _collator.find(search, cinema->name); } -void -ScreensPanel::add_screen (shared_ptr c, shared_ptr s) + +optional +ScreensPanel::add_cinema (shared_ptr cinema, wxTreeListItem previous) { - map >::const_iterator i = _cinemas.begin(); - while (i != _cinemas.end() && i->second != c) { - ++i; + auto const search = wx_to_std(_search->GetValue()); + if (!matches_search(cinema, search)) { + return {}; } - if (i == _cinemas.end()) { - return; + auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name)); + + _item_to_cinema[id] = cinema; + _cinema_to_item[cinema] = id; + + for (auto screen: cinema->screens()) { + add_screen (cinema, screen); } - _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s; - _targets->Expand (i->first); + return id; +} + + +optional +ScreensPanel::add_screen (shared_ptr cinema, shared_ptr screen) +{ + auto item = cinema_to_item(cinema); + if (!item) { + return {}; + } + + auto id = _targets->AppendItem(*item, std_to_wx(screen->name)); + + _item_to_screen[id] = screen; + _screen_to_item[screen] = id; + + return item; } + void ScreensPanel::add_cinema_clicked () { - CinemaDialog* d = new CinemaDialog (this, "Add Cinema"); - if (d->ShowModal () == wxID_OK) { - shared_ptr c (new Cinema (d->name(), d->emails())); - Config::instance()->add_cinema (c); - add_cinema (c); + CinemaDialog dialog(GetParent(), _("Add Cinema")); + + if (dialog.ShowModal() == wxID_OK) { + auto cinema = make_shared(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute()); + + auto cinemas = sorted_cinemas(); + + try { + _ignore_cinemas_changed = true; + 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 (!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; + } + + auto item = add_cinema(cinema, found ? previous : wxTLI_LAST); + + if (item) { + _targets->UnselectAll (); + _targets->Select (*item); + } } - d->Destroy (); + selection_changed (); } + +shared_ptr +ScreensPanel::cinema_for_operation () const +{ + if (_selected_cinemas.size() == 1) { + return _selected_cinemas[0]; + } else if (_selected_screens.size() == 1) { + return _selected_screens[0]->cinema; + } + + return {}; +} + + void ScreensPanel::edit_cinema_clicked () { - if (selected_cinemas().size() != 1) { - return; + auto cinema = cinema_for_operation (); + if (cinema) { + edit_cinema(cinema); } +} - pair > c = selected_cinemas().front(); - CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->emails); - if (d->ShowModal () == wxID_OK) { - c.second->name = d->name (); - c.second->emails = d->emails (); - _targets->SetItemText (c.first, std_to_wx (d->name())); - Config::instance()->changed (); +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 (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(dialog.name())); } - - d->Destroy (); } + void ScreensPanel::remove_cinema_clicked () { - if (selected_cinemas().size() != 1) { - return; + if (_selected_cinemas.size() == 1) { + if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) { + return; + } + } else { + if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) { + return; + } } - pair > c = selected_cinemas().front(); + for (auto const& cinema: _selected_cinemas) { + _ignore_cinemas_changed = true; + ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; }; + Config::instance()->remove_cinema(cinema); + auto item = cinema_to_item(cinema); + DCPOMATIC_ASSERT(item); + _targets->DeleteItem(*item); + } - Config::instance()->remove_cinema (c.second); - _targets->Delete (c.first); + selection_changed (); } + void ScreensPanel::add_screen_clicked () { - if (selected_cinemas().size() != 1) { + auto cinema = cinema_for_operation (); + if (!cinema) { return; } - shared_ptr c = selected_cinemas().front().second; + ScreenDialog dialog(GetParent(), _("Add Screen")); - ScreenDialog* d = new ScreenDialog (this, "Add Screen"); - if (d->ShowModal () != wxID_OK) { + if (dialog.ShowModal () != wxID_OK) { return; } - shared_ptr s (new Screen (d->name(), d->recipient())); - c->add_screen (s); - add_screen (c, s); + for (auto screen: cinema->screens()) { + 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(dialog.name()).data() + ) + ); + return; + } + } - Config::instance()->changed (); + auto screen = std::make_shared(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices()); + cinema->add_screen (screen); + notify_cinemas_changed(); - d->Destroy (); + auto id = add_screen (cinema, screen); + if (id) { + _targets->Expand (id.get ()); + } } + void ScreensPanel::edit_screen_clicked () { - if (selected_screens().size() != 1) { - return; + if (_selected_screens.size() == 1) { + edit_screen(_selected_screens[0]); } +} - pair > s = selected_screens().front(); - ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient); - if (d->ShowModal () == wxID_OK) { - s.second->name = d->name (); - s.second->recipient = d->recipient (); - _targets->SetItemText (s.first, std_to_wx (d->name())); - Config::instance()->changed (); +void +ScreensPanel::edit_screen(shared_ptr edit_screen) +{ + ScreenDialog dialog( + GetParent(), _("Edit screen"), + edit_screen->name, + edit_screen->notes, + edit_screen->recipient, + edit_screen->recipient_file, + edit_screen->trusted_devices + ); + + if (dialog.ShowModal() != wxID_OK) { + return; + } + + auto cinema = edit_screen->cinema; + for (auto screen: cinema->screens()) { + 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(dialog.name()).data() + ) + ); + return; + } } - d->Destroy (); + 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(dialog.name())); } + void ScreensPanel::remove_screen_clicked () { - if (selected_screens().size() != 1) { + if (_selected_screens.size() == 1) { + if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) { + return; + } + } else { + if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) { + return; + } + } + + for (auto const& screen: _selected_screens) { + screen->cinema->remove_screen(screen); + auto item = screen_to_item(screen); + DCPOMATIC_ASSERT(item); + _targets->DeleteItem(*item); + } + + /* 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(); +} + + +vector> +ScreensPanel::screens () const +{ + vector> output; + std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output)); + return output; +} + + +void +ScreensPanel::selection_changed_shim (wxTreeListEvent &) +{ + selection_changed (); +} + + +void +ScreensPanel::selection_changed () +{ + if (_ignore_selection_change) { return; } - pair > s = selected_screens().front(); + wxTreeListItems selection; + _targets->GetSelections (selection); + + _selected_cinemas.clear (); + _selected_screens.clear (); - map >::iterator i = _cinemas.begin (); - while (i != _cinemas.end ()) { - list > sc = i->second->screens (); - if (find (sc.begin(), sc.end(), s.second) != sc.end ()) { - break; + for (size_t i = 0; i < selection.size(); ++i) { + if (auto cinema = item_to_cinema(selection[i])) { + _selected_cinemas.push_back(cinema); + } + if (auto screen = item_to_screen(selection[i])) { + _selected_screens.push_back(screen); } } - if (i == _cinemas.end()) { - return; + setup_sensitivity (); +} + + +list> +ScreensPanel::sorted_cinemas() const +{ + auto cinemas = Config::instance()->cinemas(); + + cinemas.sort( + [this](shared_ptr a, shared_ptr b) { return _collator.compare(a->name, b->name) < 0; } + ); + + return cinemas; +} + + +void +ScreensPanel::add_cinemas () +{ + for (auto cinema: sorted_cinemas()) { + add_cinema (cinema, wxTLI_LAST); } +} - i->second->remove_screen (s.second); - _targets->Delete (s.first); - Config::instance()->changed (); +void +ScreensPanel::clear_and_re_add() +{ + _targets->DeleteAllItems (); + + _item_to_cinema.clear (); + _cinema_to_item.clear (); + _item_to_screen.clear (); + _screen_to_item.clear (); + + add_cinemas (); } -list > -ScreensPanel::screens () const + +void +ScreensPanel::search_changed () { - list > s; + clear_and_re_add(); + + _ignore_selection_change = true; - list > > cinemas = selected_cinemas (); - for (list > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) { - list > sc = i->second->screens (); - for (list >::const_iterator j = sc.begin(); j != sc.end(); ++j) { - s.push_back (*j); + for (auto const& selection: _selected_cinemas) { + if (auto item = cinema_to_item(selection)) { + _targets->Select (*item); } } - list > > screens = selected_screens (); - for (list > >::iterator i = screens.begin(); i != screens.end(); ++i) { - s.push_back (i->second); + for (auto const& selection: _selected_screens) { + if (auto item = screen_to_item(selection)) { + _targets->Select (*item); + } + } + + _ignore_selection_change = false; + + _ignore_check_change = true; + + for (auto const& checked: _checked_screens) { + if (auto item = screen_to_item(checked)) { + _targets->CheckItem(*item, wxCHK_CHECKED); + setup_cinema_checked_state(*item); + } } - s.sort (); - s.unique (); + _ignore_check_change = false; +} + - return s; +void +ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked) +{ + auto screen = item_to_screen(item); + DCPOMATIC_ASSERT(screen); + if (checked) { + _checked_screens.insert(screen); + } else { + _checked_screens.erase(screen); + } } + void -ScreensPanel::selection_changed (wxTreeEvent &) +ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen) { - setup_sensitivity (); + auto cinema = _targets->GetItemParent(screen); + DCPOMATIC_ASSERT (cinema.IsOk()); + int checked = 0; + int unchecked = 0; + for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) { + if (_targets->GetCheckedState(child) == wxCHK_CHECKED) { + ++checked; + } else { + ++unchecked; + } + } + if (checked == 0) { + _targets->CheckItem(cinema, wxCHK_UNCHECKED); + } else if (unchecked == 0) { + _targets->CheckItem(cinema, wxCHK_CHECKED); + } else { + _targets->CheckItem(cinema, wxCHK_UNDETERMINED); + } +} + + +void +ScreensPanel::checkbox_changed (wxTreeListEvent& ev) +{ + if (_ignore_check_change) { + return; + } + + if (item_to_cinema(ev.GetItem())) { + /* Cinema: check/uncheck all children */ + auto const checked = _targets->GetCheckedState(ev.GetItem()); + for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) { + _targets->CheckItem(child, checked); + set_screen_checked(child, checked); + } + } else { + set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem())); + setup_cinema_checked_state(ev.GetItem()); + } + ScreensChanged (); } + + +shared_ptr +ScreensPanel::item_to_cinema (wxTreeListItem item) const +{ + auto iter = _item_to_cinema.find (item); + if (iter == _item_to_cinema.end()) { + return {}; + } + + return iter->second; +} + + +shared_ptr +ScreensPanel::item_to_screen (wxTreeListItem item) const +{ + auto iter = _item_to_screen.find (item); + if (iter == _item_to_screen.end()) { + return {}; + } + + return iter->second; +} + + +optional +ScreensPanel::cinema_to_item (shared_ptr cinema) const +{ + auto iter = _cinema_to_item.find (cinema); + if (iter == _cinema_to_item.end()) { + return {}; + } + + return iter->second; +} + + +optional +ScreensPanel::screen_to_item (shared_ptr screen) const +{ + auto iter = _screen_to_item.find (screen); + if (iter == _screen_to_item.end()) { + return {}; + } + + return iter->second; +} + + +bool +ScreensPanel::notify_cinemas_changed() +{ + _ignore_cinemas_changed = true; + 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) +{ + auto iter = _item_to_cinema.find(ev.GetItem()); + if (iter != _item_to_cinema.end()) { + edit_cinema(iter->second); + } else { + auto iter = _item_to_screen.find(ev.GetItem()); + if (iter != _item_to_screen.end()) { + edit_screen(iter->second); + } + } +} + +