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 "lib/cinema.h"
29 #include "lib/config.h"
30 #include "lib/screen.h"
31 #include "lib/timer.h"
32 #include <dcp/scope_guard.h>
38 using std::make_shared;
41 using std::shared_ptr;
44 using boost::optional;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
48 using namespace dcpomatic;
51 ScreensPanel::ScreensPanel (wxWindow* parent)
52 : wxPanel (parent, wxID_ANY)
54 _overall_sizer = new wxBoxSizer(wxVERTICAL);
56 auto search_sizer = new wxBoxSizer(wxHORIZONTAL);
58 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
60 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
61 _search->ShowCancelButton (true);
63 search_sizer->Add(_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
65 _show_only_checked = new CheckBox(this, _("Show only checked"));
66 search_sizer->Add(_show_only_checked, 1, wxEXPAND | wxLEFT | wxBOTTOM, DCPOMATIC_SIZER_GAP);
68 _overall_sizer->Add(search_sizer);
70 auto targets = new wxBoxSizer (wxHORIZONTAL);
71 _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
72 _targets->AppendColumn (wxT("foo"));
74 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
78 auto side_buttons = new wxBoxSizer (wxVERTICAL);
80 auto target_buttons = new wxBoxSizer (wxVERTICAL);
82 _add_cinema = new Button (this, _("Add Cinema..."));
83 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84 _edit_cinema = new Button (this, _("Edit Cinema..."));
85 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
86 _remove_cinema = new Button (this, _("Remove Cinema"));
87 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
88 _add_screen = new Button (this, _("Add Screen..."));
89 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
90 _edit_screen = new Button (this, _("Edit Screen..."));
91 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
92 _remove_screen = new Button (this, _("Remove Screen"));
93 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
95 side_buttons->Add (target_buttons, 0, 0);
97 auto check_buttons = new wxBoxSizer (wxVERTICAL);
99 _check_all = new Button (this, _("Check all"));
100 check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
101 _uncheck_all = new Button (this, _("Uncheck all"));
102 check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
104 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
106 targets->Add (side_buttons, 0, 0);
108 _overall_sizer->Add (targets, 1, wxEXPAND);
110 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::display_filter_changed, this));
111 _show_only_checked->Bind(wxEVT_CHECKBOX, boost::bind(&ScreensPanel::display_filter_changed, this));
112 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
113 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
114 _targets->Bind (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
116 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
117 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
118 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
120 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
121 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
122 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
124 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
125 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
127 SetSizer(_overall_sizer);
129 _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
133 ScreensPanel::~ScreensPanel ()
135 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
136 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
141 ScreensPanel::check_all ()
143 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
144 _targets->CheckItem(cinema, wxCHK_CHECKED);
145 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
146 _targets->CheckItem(screen, wxCHK_CHECKED);
147 set_screen_checked(screen, true);
154 ScreensPanel::uncheck_all ()
156 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
157 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
158 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
159 _targets->CheckItem(screen, wxCHK_UNCHECKED);
160 set_screen_checked(screen, false);
167 ScreensPanel::setup_sensitivity ()
169 bool const sc = _selected_cinemas.size() == 1;
170 bool const ss = _selected_screens.size() == 1;
172 _edit_cinema->Enable (sc || ss);
173 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
175 _add_screen->Enable (sc || ss);
176 _edit_screen->Enable (ss);
177 _remove_screen->Enable (_selected_screens.size() >= 1);
179 _show_only_checked->Enable(!_checked_screens.empty());
184 ScreensPanel::convert_to_lower(string& s)
186 transform(s.begin(), s.end(), s.begin(), ::tolower);
191 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
193 if (search.empty()) {
197 return _collator.find(search, cinema->name);
201 optional<wxTreeListItem>
202 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
204 auto const search = wx_to_std(_search->GetValue());
205 if (!matches_search(cinema, search)) {
209 if (_show_only_checked->get()) {
210 auto screens = cinema->screens();
211 auto iter = std::find_if(screens.begin(), screens.end(), [this](shared_ptr<dcpomatic::Screen> screen) {
212 return _checked_screens.find(screen) != _checked_screens.end();
214 if (iter == screens.end()) {
219 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
221 _item_to_cinema[id] = cinema;
222 _cinema_to_item[cinema] = id;
224 for (auto screen: cinema->screens()) {
225 add_screen (cinema, screen);
232 optional<wxTreeListItem>
233 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
235 auto item = cinema_to_item(cinema);
240 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
242 _item_to_screen[id] = screen;
243 _screen_to_item[screen] = id;
250 ScreensPanel::add_cinema_clicked ()
252 CinemaDialog dialog(GetParent(), _("Add Cinema"));
254 if (dialog.ShowModal() == wxID_OK) {
255 auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
257 auto cinemas = sorted_cinemas();
260 _ignore_cinemas_changed = true;
261 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
262 Config::instance()->add_cinema(cinema);
263 } catch (FileError& e) {
264 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()));
268 wxTreeListItem previous = wxTLI_FIRST;
270 auto const search = wx_to_std(_search->GetValue());
271 for (auto existing_cinema: cinemas) {
272 if (!matches_search(existing_cinema, search)) {
275 if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
276 /* existing_cinema should be after the one we're inserting */
280 auto item = cinema_to_item(existing_cinema);
281 DCPOMATIC_ASSERT(item);
285 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
288 _targets->UnselectAll ();
289 _targets->Select (*item);
293 selection_changed ();
298 ScreensPanel::cinema_for_operation () const
300 if (_selected_cinemas.size() == 1) {
301 return _selected_cinemas[0];
302 } else if (_selected_screens.size() == 1) {
303 return _selected_screens[0]->cinema;
311 ScreensPanel::edit_cinema_clicked ()
313 auto cinema = cinema_for_operation ();
321 ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
324 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
327 if (dialog.ShowModal() == wxID_OK) {
328 cinema->name = dialog.name();
329 cinema->emails = dialog.emails();
330 cinema->notes = dialog.notes();
331 cinema->set_utc_offset_hour(dialog.utc_offset_hour());
332 cinema->set_utc_offset_minute(dialog.utc_offset_minute());
333 notify_cinemas_changed();
334 auto item = cinema_to_item(cinema);
335 DCPOMATIC_ASSERT(item);
336 _targets->SetItemText (*item, std_to_wx(dialog.name()));
342 ScreensPanel::remove_cinema_clicked ()
344 if (_selected_cinemas.size() == 1) {
345 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
349 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
354 auto cinemas_to_remove = _selected_cinemas;
356 for (auto const& cinema: cinemas_to_remove) {
357 _ignore_cinemas_changed = true;
358 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
359 for (auto screen: cinema->screens()) {
360 _checked_screens.erase(screen);
362 Config::instance()->remove_cinema(cinema);
363 auto item = cinema_to_item(cinema);
364 DCPOMATIC_ASSERT(item);
365 _targets->DeleteItem(*item);
368 selection_changed ();
369 setup_show_only_checked();
374 ScreensPanel::add_screen_clicked ()
376 auto cinema = cinema_for_operation ();
381 ScreenDialog dialog(GetParent(), _("Add Screen"));
383 if (dialog.ShowModal () != wxID_OK) {
387 for (auto screen: cinema->screens()) {
388 if (screen->name == dialog.name()) {
392 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
393 std_to_wx(dialog.name()).data()
400 auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
401 cinema->add_screen (screen);
402 notify_cinemas_changed();
404 auto id = add_screen (cinema, screen);
406 _targets->Expand (id.get ());
412 ScreensPanel::edit_screen_clicked ()
414 if (_selected_screens.size() == 1) {
415 edit_screen(_selected_screens[0]);
421 ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
424 GetParent(), _("Edit screen"),
427 edit_screen->recipient,
428 edit_screen->recipient_file,
429 edit_screen->trusted_devices
432 if (dialog.ShowModal() != wxID_OK) {
436 auto cinema = edit_screen->cinema;
437 for (auto screen: cinema->screens()) {
438 if (screen != edit_screen && screen->name == dialog.name()) {
442 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
443 std_to_wx(dialog.name()).data()
450 edit_screen->name = dialog.name();
451 edit_screen->notes = dialog.notes();
452 edit_screen->recipient = dialog.recipient();
453 edit_screen->recipient_file = dialog.recipient_file();
454 edit_screen->trusted_devices = dialog.trusted_devices();
455 notify_cinemas_changed();
457 auto item = screen_to_item(edit_screen);
458 DCPOMATIC_ASSERT (item);
459 _targets->SetItemText(*item, std_to_wx(dialog.name()));
464 ScreensPanel::remove_screen_clicked ()
466 if (_selected_screens.size() == 1) {
467 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
471 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
476 for (auto screen: _selected_screens) {
477 _checked_screens.erase(screen);
478 screen->cinema->remove_screen(screen);
479 auto item = screen_to_item(screen);
480 DCPOMATIC_ASSERT(item);
481 _targets->DeleteItem(*item);
484 /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
488 notify_cinemas_changed();
489 setup_show_only_checked();
493 vector<shared_ptr<Screen>>
494 ScreensPanel::screens () const
496 vector<shared_ptr<Screen>> output;
497 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
503 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
505 selection_changed ();
510 ScreensPanel::selection_changed ()
512 if (_ignore_selection_change) {
516 wxTreeListItems selection;
517 _targets->GetSelections (selection);
519 _selected_cinemas.clear ();
520 _selected_screens.clear ();
522 for (size_t i = 0; i < selection.size(); ++i) {
523 if (auto cinema = item_to_cinema(selection[i])) {
524 _selected_cinemas.push_back(cinema);
526 if (auto screen = item_to_screen(selection[i])) {
527 _selected_screens.push_back(screen);
531 setup_sensitivity ();
535 list<shared_ptr<Cinema>>
536 ScreensPanel::sorted_cinemas() const
538 auto cinemas = Config::instance()->cinemas();
541 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
549 ScreensPanel::add_cinemas ()
551 for (auto cinema: sorted_cinemas()) {
552 add_cinema (cinema, wxTLI_LAST);
558 ScreensPanel::clear_and_re_add()
560 _targets->DeleteAllItems ();
562 _item_to_cinema.clear ();
563 _cinema_to_item.clear ();
564 _item_to_screen.clear ();
565 _screen_to_item.clear ();
571 /** Search and/or "show only checked" changed */
573 ScreensPanel::display_filter_changed()
577 _ignore_selection_change = true;
579 for (auto const& selection: _selected_cinemas) {
580 if (auto item = cinema_to_item(selection)) {
581 _targets->Select (*item);
585 for (auto const& selection: _selected_screens) {
586 if (auto item = screen_to_item(selection)) {
587 _targets->Select (*item);
591 _ignore_selection_change = false;
593 _ignore_check_change = true;
595 for (auto const& checked: _checked_screens) {
596 if (auto item = screen_to_item(checked)) {
597 _targets->CheckItem(*item, wxCHK_CHECKED);
598 setup_cinema_checked_state(*item);
602 _ignore_check_change = false;
607 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
609 auto screen = item_to_screen(item);
610 DCPOMATIC_ASSERT(screen);
612 _checked_screens.insert(screen);
614 _checked_screens.erase(screen);
617 setup_show_only_checked();
622 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
624 auto cinema = _targets->GetItemParent(screen);
625 DCPOMATIC_ASSERT (cinema.IsOk());
628 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
629 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
636 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
637 } else if (unchecked == 0) {
638 _targets->CheckItem(cinema, wxCHK_CHECKED);
640 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
646 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
648 if (_ignore_check_change) {
652 if (item_to_cinema(ev.GetItem())) {
653 /* Cinema: check/uncheck all children */
654 auto const checked = _targets->GetCheckedState(ev.GetItem());
655 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
656 _targets->CheckItem(child, checked);
657 set_screen_checked(child, checked);
660 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
661 setup_cinema_checked_state(ev.GetItem());
669 ScreensPanel::item_to_cinema (wxTreeListItem item) const
671 auto iter = _item_to_cinema.find (item);
672 if (iter == _item_to_cinema.end()) {
681 ScreensPanel::item_to_screen (wxTreeListItem item) const
683 auto iter = _item_to_screen.find (item);
684 if (iter == _item_to_screen.end()) {
692 optional<wxTreeListItem>
693 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
695 auto iter = _cinema_to_item.find (cinema);
696 if (iter == _cinema_to_item.end()) {
704 optional<wxTreeListItem>
705 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
707 auto iter = _screen_to_item.find (screen);
708 if (iter == _screen_to_item.end()) {
717 ScreensPanel::notify_cinemas_changed()
719 _ignore_cinemas_changed = true;
720 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
723 Config::instance()->changed(Config::CINEMAS);
724 } catch (FileError& e) {
725 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()));
734 ScreensPanel::config_changed(Config::Property property)
736 if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
743 ScreensPanel::item_activated(wxTreeListEvent& ev)
745 auto iter = _item_to_cinema.find(ev.GetItem());
746 if (iter != _item_to_cinema.end()) {
747 edit_cinema(iter->second);
749 auto iter = _item_to_screen.find(ev.GetItem());
750 if (iter != _item_to_screen.end()) {
751 edit_screen(iter->second);
758 ScreensPanel::setup_show_only_checked()
760 if (_checked_screens.empty()) {
761 _show_only_checked->set_text(_("Show only checked"));
763 _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast<int>(_checked_screens.size())));
766 _overall_sizer->Layout();