2 Copyright (C) 2015-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 "check_box.h"
23 #include "cinema_dialog.h"
24 #include "dcpomatic_button.h"
25 #include "screen_dialog.h"
26 #include "screens_panel.h"
28 #include "wx_variant.h"
29 #include "lib/cinema.h"
30 #include "lib/config.h"
31 #include "lib/screen.h"
32 #include "lib/timer.h"
33 #include <dcp/scope_guard.h>
39 using std::make_shared;
42 using std::shared_ptr;
45 using boost::optional;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
49 using namespace dcpomatic;
52 ScreensPanel::ScreensPanel (wxWindow* parent)
53 : wxPanel (parent, wxID_ANY)
55 _overall_sizer = new wxBoxSizer(wxVERTICAL);
57 auto search_sizer = new wxBoxSizer(wxHORIZONTAL);
59 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
61 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
62 _search->ShowCancelButton (true);
64 search_sizer->Add(_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
66 _show_only_checked = new CheckBox(this, _("Show only checked"));
67 search_sizer->Add(_show_only_checked, 1, wxEXPAND | wxLEFT | wxBOTTOM, DCPOMATIC_SIZER_GAP);
69 _overall_sizer->Add(search_sizer);
71 auto targets = new wxBoxSizer (wxHORIZONTAL);
72 _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
73 _targets->AppendColumn (wxT("foo"));
75 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
79 auto side_buttons = new wxBoxSizer (wxVERTICAL);
81 auto target_buttons = new wxBoxSizer (wxVERTICAL);
83 _add_cinema = new Button (this, _("Add Cinema..."));
84 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
85 _edit_cinema = new Button (this, _("Edit Cinema..."));
86 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
87 _remove_cinema = new Button (this, _("Remove Cinema"));
88 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
89 _add_screen = new Button (this, _("Add Screen..."));
90 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
91 _edit_screen = new Button (this, _("Edit Screen..."));
92 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
93 _remove_screen = new Button (this, _("Remove Screen"));
94 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
96 side_buttons->Add (target_buttons, 0, 0);
98 auto check_buttons = new wxBoxSizer (wxVERTICAL);
100 _check_all = new Button (this, _("Check all"));
101 check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
102 _uncheck_all = new Button (this, _("Uncheck all"));
103 check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
105 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
107 targets->Add (side_buttons, 0, 0);
109 _overall_sizer->Add (targets, 1, wxEXPAND);
111 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::display_filter_changed, this));
112 _show_only_checked->Bind(wxEVT_CHECKBOX, boost::bind(&ScreensPanel::display_filter_changed, this));
113 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
114 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
115 _targets->Bind (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
117 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
118 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
119 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
121 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
122 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
123 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
125 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
126 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
128 SetSizer(_overall_sizer);
130 _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
134 ScreensPanel::~ScreensPanel ()
136 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
137 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
142 ScreensPanel::check_all ()
144 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
145 _targets->CheckItem(cinema, wxCHK_CHECKED);
146 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
147 _targets->CheckItem(screen, wxCHK_CHECKED);
148 set_screen_checked(screen, true);
155 ScreensPanel::uncheck_all ()
157 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
158 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
159 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
160 _targets->CheckItem(screen, wxCHK_UNCHECKED);
161 set_screen_checked(screen, false);
168 ScreensPanel::setup_sensitivity ()
170 bool const sc = _selected_cinemas.size() == 1;
171 bool const ss = _selected_screens.size() == 1;
173 _edit_cinema->Enable (sc || ss);
174 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
176 _add_screen->Enable (sc || ss);
177 _edit_screen->Enable (ss);
178 _remove_screen->Enable (_selected_screens.size() >= 1);
180 _show_only_checked->Enable(!_checked_screens.empty());
185 ScreensPanel::convert_to_lower(string& s)
187 transform(s.begin(), s.end(), s.begin(), ::tolower);
192 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
194 if (search.empty()) {
198 return _collator.find(search, cinema->name);
202 optional<wxTreeListItem>
203 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
205 auto const search = wx_to_std(_search->GetValue());
206 if (!matches_search(cinema, search)) {
210 if (_show_only_checked->get()) {
211 auto screens = cinema->screens();
212 auto iter = std::find_if(screens.begin(), screens.end(), [this](shared_ptr<dcpomatic::Screen> screen) {
213 return _checked_screens.find(screen) != _checked_screens.end();
215 if (iter == screens.end()) {
220 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
222 _item_to_cinema[id] = cinema;
223 _cinema_to_item[cinema] = id;
225 for (auto screen: cinema->screens()) {
226 add_screen (cinema, screen);
233 optional<wxTreeListItem>
234 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
236 auto item = cinema_to_item(cinema);
241 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
243 _item_to_screen[id] = screen;
244 _screen_to_item[screen] = id;
251 ScreensPanel::add_cinema_clicked ()
253 CinemaDialog dialog(GetParent(), _("Add Cinema"));
255 if (dialog.ShowModal() == wxID_OK) {
256 auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes());
258 auto cinemas = sorted_cinemas();
261 _ignore_cinemas_changed = true;
262 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
263 Config::instance()->add_cinema(cinema);
264 } catch (FileError& e) {
267 variant::wx::insert_dcpomatic(
268 _("Could not write cinema details to the cinemas.xml file. Check that the location of "
269 "cinemas.xml is valid in %s's preferences.")
276 wxTreeListItem previous = wxTLI_FIRST;
278 auto const search = wx_to_std(_search->GetValue());
279 for (auto existing_cinema: cinemas) {
280 if (!matches_search(existing_cinema, search)) {
283 if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
284 /* existing_cinema should be after the one we're inserting */
288 auto item = cinema_to_item(existing_cinema);
289 DCPOMATIC_ASSERT(item);
293 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
296 _targets->UnselectAll ();
297 _targets->Select (*item);
301 selection_changed ();
306 ScreensPanel::cinema_for_operation () const
308 if (_selected_cinemas.size() == 1) {
309 return _selected_cinemas[0];
310 } else if (_selected_screens.size() == 1) {
311 return _selected_screens[0]->cinema;
319 ScreensPanel::edit_cinema_clicked ()
321 auto cinema = cinema_for_operation ();
329 ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
331 CinemaDialog dialog(GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes);
333 if (dialog.ShowModal() == wxID_OK) {
334 cinema->name = dialog.name();
335 cinema->emails = dialog.emails();
336 cinema->notes = dialog.notes();
337 notify_cinemas_changed();
338 auto item = cinema_to_item(cinema);
339 DCPOMATIC_ASSERT(item);
340 _targets->SetItemText (*item, std_to_wx(dialog.name()));
346 ScreensPanel::remove_cinema_clicked ()
348 if (_selected_cinemas.size() == 1) {
349 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
353 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
358 auto cinemas_to_remove = _selected_cinemas;
360 for (auto const& cinema: cinemas_to_remove) {
361 _ignore_cinemas_changed = true;
362 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
363 for (auto screen: cinema->screens()) {
364 _checked_screens.erase(screen);
366 Config::instance()->remove_cinema(cinema);
367 auto item = cinema_to_item(cinema);
368 DCPOMATIC_ASSERT(item);
369 _targets->DeleteItem(*item);
372 selection_changed ();
373 setup_show_only_checked();
378 ScreensPanel::add_screen_clicked ()
380 auto cinema = cinema_for_operation ();
385 ScreenDialog dialog(GetParent(), _("Add Screen"));
387 if (dialog.ShowModal () != wxID_OK) {
391 for (auto screen: cinema->screens()) {
392 if (screen->name == dialog.name()) {
396 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
397 std_to_wx(dialog.name()).data()
404 auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
405 cinema->add_screen (screen);
406 notify_cinemas_changed();
408 auto id = add_screen (cinema, screen);
410 _targets->Expand (id.get ());
416 ScreensPanel::edit_screen_clicked ()
418 if (_selected_screens.size() == 1) {
419 edit_screen(_selected_screens[0]);
425 ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
428 GetParent(), _("Edit screen"),
431 edit_screen->recipient,
432 edit_screen->recipient_file,
433 edit_screen->trusted_devices
436 if (dialog.ShowModal() != wxID_OK) {
440 auto cinema = edit_screen->cinema;
441 for (auto screen: cinema->screens()) {
442 if (screen != edit_screen && screen->name == dialog.name()) {
446 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
447 std_to_wx(dialog.name()).data()
454 edit_screen->name = dialog.name();
455 edit_screen->notes = dialog.notes();
456 edit_screen->recipient = dialog.recipient();
457 edit_screen->recipient_file = dialog.recipient_file();
458 edit_screen->trusted_devices = dialog.trusted_devices();
459 notify_cinemas_changed();
461 auto item = screen_to_item(edit_screen);
462 DCPOMATIC_ASSERT (item);
463 _targets->SetItemText(*item, std_to_wx(dialog.name()));
468 ScreensPanel::remove_screen_clicked ()
470 if (_selected_screens.size() == 1) {
471 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
475 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
480 for (auto screen: _selected_screens) {
481 _checked_screens.erase(screen);
482 screen->cinema->remove_screen(screen);
483 auto item = screen_to_item(screen);
484 DCPOMATIC_ASSERT(item);
485 _targets->DeleteItem(*item);
488 /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
492 notify_cinemas_changed();
493 setup_show_only_checked();
497 vector<shared_ptr<Screen>>
498 ScreensPanel::screens () const
500 vector<shared_ptr<Screen>> output;
501 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
507 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
509 selection_changed ();
514 ScreensPanel::selection_changed ()
516 if (_ignore_selection_change) {
520 wxTreeListItems selection;
521 _targets->GetSelections (selection);
523 _selected_cinemas.clear ();
524 _selected_screens.clear ();
526 for (size_t i = 0; i < selection.size(); ++i) {
527 if (auto cinema = item_to_cinema(selection[i])) {
528 _selected_cinemas.push_back(cinema);
530 if (auto screen = item_to_screen(selection[i])) {
531 _selected_screens.push_back(screen);
535 setup_sensitivity ();
539 list<shared_ptr<Cinema>>
540 ScreensPanel::sorted_cinemas() const
542 auto cinemas = Config::instance()->cinemas();
545 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
553 ScreensPanel::add_cinemas ()
555 for (auto cinema: sorted_cinemas()) {
556 add_cinema (cinema, wxTLI_LAST);
562 ScreensPanel::clear_and_re_add()
564 _targets->DeleteAllItems ();
566 _item_to_cinema.clear ();
567 _cinema_to_item.clear ();
568 _item_to_screen.clear ();
569 _screen_to_item.clear ();
575 /** Search and/or "show only checked" changed */
577 ScreensPanel::display_filter_changed()
581 _ignore_selection_change = true;
583 for (auto const& selection: _selected_cinemas) {
584 if (auto item = cinema_to_item(selection)) {
585 _targets->Select (*item);
589 for (auto const& selection: _selected_screens) {
590 if (auto item = screen_to_item(selection)) {
591 _targets->Select (*item);
595 _ignore_selection_change = false;
597 _ignore_check_change = true;
599 for (auto const& checked: _checked_screens) {
600 if (auto item = screen_to_item(checked)) {
601 _targets->CheckItem(*item, wxCHK_CHECKED);
602 setup_cinema_checked_state(*item);
606 _ignore_check_change = false;
611 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
613 auto screen = item_to_screen(item);
614 DCPOMATIC_ASSERT(screen);
616 _checked_screens.insert(screen);
618 _checked_screens.erase(screen);
621 setup_show_only_checked();
626 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
628 auto cinema = _targets->GetItemParent(screen);
629 DCPOMATIC_ASSERT (cinema.IsOk());
632 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
633 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
640 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
641 } else if (unchecked == 0) {
642 _targets->CheckItem(cinema, wxCHK_CHECKED);
644 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
650 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
652 if (_ignore_check_change) {
656 if (item_to_cinema(ev.GetItem())) {
657 /* Cinema: check/uncheck all children */
658 auto const checked = _targets->GetCheckedState(ev.GetItem());
659 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
660 _targets->CheckItem(child, checked);
661 set_screen_checked(child, checked);
664 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
665 setup_cinema_checked_state(ev.GetItem());
673 ScreensPanel::item_to_cinema (wxTreeListItem item) const
675 auto iter = _item_to_cinema.find (item);
676 if (iter == _item_to_cinema.end()) {
685 ScreensPanel::item_to_screen (wxTreeListItem item) const
687 auto iter = _item_to_screen.find (item);
688 if (iter == _item_to_screen.end()) {
696 optional<wxTreeListItem>
697 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
699 auto iter = _cinema_to_item.find (cinema);
700 if (iter == _cinema_to_item.end()) {
708 optional<wxTreeListItem>
709 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
711 auto iter = _screen_to_item.find (screen);
712 if (iter == _screen_to_item.end()) {
721 ScreensPanel::notify_cinemas_changed()
723 _ignore_cinemas_changed = true;
724 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
727 Config::instance()->changed(Config::CINEMAS);
728 } catch (FileError& e) {
731 variant::wx::insert_dcpomatic(
732 _("Could not write cinema details to the cinemas.xml file. Check that the location of "
733 "cinemas.xml is valid in %s's preferences.")
745 ScreensPanel::config_changed(Config::Property property)
747 if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
754 ScreensPanel::item_activated(wxTreeListEvent& ev)
756 auto iter = _item_to_cinema.find(ev.GetItem());
757 if (iter != _item_to_cinema.end()) {
758 edit_cinema(iter->second);
760 auto iter = _item_to_screen.find(ev.GetItem());
761 if (iter != _item_to_screen.end()) {
762 edit_screen(iter->second);
769 ScreensPanel::setup_show_only_checked()
771 if (_checked_screens.empty()) {
772 _show_only_checked->set_text(_("Show only checked"));
774 _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast<int>(_checked_screens.size())));
777 _overall_sizer->Layout();