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 "cinema_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.h"
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/scope_guard.h"
30 #include "lib/screen.h"
31 #include "lib/timer.h"
37 using std::make_shared;
40 using std::shared_ptr;
43 using boost::optional;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
47 using namespace dcpomatic;
50 ScreensPanel::ScreensPanel (wxWindow* parent)
51 : wxPanel (parent, wxID_ANY)
53 auto sizer = new wxBoxSizer (wxVERTICAL);
55 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
57 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
58 _search->ShowCancelButton (true);
60 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
62 auto targets = new wxBoxSizer (wxHORIZONTAL);
63 _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
64 _targets->AppendColumn (wxT("foo"));
66 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
70 auto side_buttons = new wxBoxSizer (wxVERTICAL);
72 auto target_buttons = new wxBoxSizer (wxVERTICAL);
74 _add_cinema = new Button (this, _("Add Cinema..."));
75 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76 _edit_cinema = new Button (this, _("Edit Cinema..."));
77 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78 _remove_cinema = new Button (this, _("Remove Cinema"));
79 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
80 _add_screen = new Button (this, _("Add Screen..."));
81 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
82 _edit_screen = new Button (this, _("Edit Screen..."));
83 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84 _remove_screen = new Button (this, _("Remove Screen"));
85 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
87 side_buttons->Add (target_buttons, 0, 0);
89 auto check_buttons = new wxBoxSizer (wxVERTICAL);
91 _check_all = new Button (this, _("Check all"));
92 check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
93 _uncheck_all = new Button (this, _("Uncheck all"));
94 check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
96 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
98 targets->Add (side_buttons, 0, 0);
100 sizer->Add (targets, 1, wxEXPAND);
102 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
103 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
104 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
105 _targets->Bind (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
107 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
108 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
109 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
111 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
112 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
113 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
115 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
116 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
120 _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
124 ScreensPanel::~ScreensPanel ()
126 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
127 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
132 ScreensPanel::check_all ()
134 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
135 _targets->CheckItem(cinema, wxCHK_CHECKED);
136 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
137 _targets->CheckItem(screen, wxCHK_CHECKED);
138 set_screen_checked(screen, true);
145 ScreensPanel::uncheck_all ()
147 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
148 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
149 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
150 _targets->CheckItem(screen, wxCHK_UNCHECKED);
151 set_screen_checked(screen, false);
158 ScreensPanel::setup_sensitivity ()
160 bool const sc = _selected_cinemas.size() == 1;
161 bool const ss = _selected_screens.size() == 1;
163 _edit_cinema->Enable (sc || ss);
164 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
166 _add_screen->Enable (sc || ss);
167 _edit_screen->Enable (ss);
168 _remove_screen->Enable (_selected_screens.size() >= 1);
173 ScreensPanel::convert_to_lower(string& s)
175 transform(s.begin(), s.end(), s.begin(), ::tolower);
180 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
182 if (search.empty()) {
186 return _collator.find(search, cinema->name);
190 optional<wxTreeListItem>
191 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
193 auto const search = wx_to_std(_search->GetValue());
194 if (!matches_search(cinema, search)) {
198 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
200 _item_to_cinema[id] = cinema;
201 _cinema_to_item[cinema] = id;
203 for (auto screen: cinema->screens()) {
204 add_screen (cinema, screen);
211 optional<wxTreeListItem>
212 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
214 auto item = cinema_to_item(cinema);
219 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
221 _item_to_screen[id] = screen;
222 _screen_to_item[screen] = id;
229 ScreensPanel::add_cinema_clicked ()
231 CinemaDialog dialog(GetParent(), _("Add Cinema"));
233 if (dialog.ShowModal() == wxID_OK) {
234 auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
236 auto cinemas = sorted_cinemas();
239 _ignore_cinemas_changed = true;
240 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
241 Config::instance()->add_cinema(cinema);
242 } catch (FileError& e) {
243 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()));
247 wxTreeListItem previous = wxTLI_FIRST;
249 auto const search = wx_to_std(_search->GetValue());
250 for (auto existing_cinema: cinemas) {
251 if (!matches_search(existing_cinema, search)) {
254 if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
255 /* existing_cinema should be after the one we're inserting */
259 auto item = cinema_to_item(existing_cinema);
260 DCPOMATIC_ASSERT(item);
264 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
267 _targets->UnselectAll ();
268 _targets->Select (*item);
272 selection_changed ();
277 ScreensPanel::cinema_for_operation () const
279 if (_selected_cinemas.size() == 1) {
280 return _selected_cinemas[0];
281 } else if (_selected_screens.size() == 1) {
282 return _selected_screens[0]->cinema;
290 ScreensPanel::edit_cinema_clicked ()
292 auto cinema = cinema_for_operation ();
300 ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
303 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
306 if (dialog.ShowModal() == wxID_OK) {
307 cinema->name = dialog.name();
308 cinema->emails = dialog.emails();
309 cinema->notes = dialog.notes();
310 cinema->set_utc_offset_hour(dialog.utc_offset_hour());
311 cinema->set_utc_offset_minute(dialog.utc_offset_minute());
312 notify_cinemas_changed();
313 auto item = cinema_to_item(cinema);
314 DCPOMATIC_ASSERT(item);
315 _targets->SetItemText (*item, std_to_wx(dialog.name()));
321 ScreensPanel::remove_cinema_clicked ()
323 if (_selected_cinemas.size() == 1) {
324 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
328 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
333 auto cinemas_to_remove = _selected_cinemas;
335 for (auto const& cinema: cinemas_to_remove) {
336 _ignore_cinemas_changed = true;
337 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
338 for (auto screen: cinema->screens()) {
339 _checked_screens.erase(screen);
341 Config::instance()->remove_cinema(cinema);
342 auto item = cinema_to_item(cinema);
343 DCPOMATIC_ASSERT(item);
344 _targets->DeleteItem(*item);
347 selection_changed ();
352 ScreensPanel::add_screen_clicked ()
354 auto cinema = cinema_for_operation ();
359 ScreenDialog dialog(GetParent(), _("Add Screen"));
361 if (dialog.ShowModal () != wxID_OK) {
365 for (auto screen: cinema->screens()) {
366 if (screen->name == dialog.name()) {
370 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
371 std_to_wx(dialog.name()).data()
378 auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
379 cinema->add_screen (screen);
380 notify_cinemas_changed();
382 auto id = add_screen (cinema, screen);
384 _targets->Expand (id.get ());
390 ScreensPanel::edit_screen_clicked ()
392 if (_selected_screens.size() == 1) {
393 edit_screen(_selected_screens[0]);
399 ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
402 GetParent(), _("Edit screen"),
405 edit_screen->recipient,
406 edit_screen->recipient_file,
407 edit_screen->trusted_devices
410 if (dialog.ShowModal() != wxID_OK) {
414 auto cinema = edit_screen->cinema;
415 for (auto screen: cinema->screens()) {
416 if (screen != edit_screen && screen->name == dialog.name()) {
420 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
421 std_to_wx(dialog.name()).data()
428 edit_screen->name = dialog.name();
429 edit_screen->notes = dialog.notes();
430 edit_screen->recipient = dialog.recipient();
431 edit_screen->recipient_file = dialog.recipient_file();
432 edit_screen->trusted_devices = dialog.trusted_devices();
433 notify_cinemas_changed();
435 auto item = screen_to_item(edit_screen);
436 DCPOMATIC_ASSERT (item);
437 _targets->SetItemText(*item, std_to_wx(dialog.name()));
442 ScreensPanel::remove_screen_clicked ()
444 if (_selected_screens.size() == 1) {
445 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
449 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
454 for (auto screen: _selected_screens) {
455 _checked_screens.erase(screen);
456 screen->cinema->remove_screen(screen);
457 auto item = screen_to_item(screen);
458 DCPOMATIC_ASSERT(item);
459 _targets->DeleteItem(*item);
462 /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
466 notify_cinemas_changed();
470 vector<shared_ptr<Screen>>
471 ScreensPanel::screens () const
473 vector<shared_ptr<Screen>> output;
474 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
480 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
482 selection_changed ();
487 ScreensPanel::selection_changed ()
489 if (_ignore_selection_change) {
493 wxTreeListItems selection;
494 _targets->GetSelections (selection);
496 _selected_cinemas.clear ();
497 _selected_screens.clear ();
499 for (size_t i = 0; i < selection.size(); ++i) {
500 if (auto cinema = item_to_cinema(selection[i])) {
501 _selected_cinemas.push_back(cinema);
503 if (auto screen = item_to_screen(selection[i])) {
504 _selected_screens.push_back(screen);
508 setup_sensitivity ();
512 list<shared_ptr<Cinema>>
513 ScreensPanel::sorted_cinemas() const
515 auto cinemas = Config::instance()->cinemas();
518 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
526 ScreensPanel::add_cinemas ()
528 for (auto cinema: sorted_cinemas()) {
529 add_cinema (cinema, wxTLI_LAST);
535 ScreensPanel::clear_and_re_add()
537 _targets->DeleteAllItems ();
539 _item_to_cinema.clear ();
540 _cinema_to_item.clear ();
541 _item_to_screen.clear ();
542 _screen_to_item.clear ();
549 ScreensPanel::search_changed ()
553 _ignore_selection_change = true;
555 for (auto const& selection: _selected_cinemas) {
556 if (auto item = cinema_to_item(selection)) {
557 _targets->Select (*item);
561 for (auto const& selection: _selected_screens) {
562 if (auto item = screen_to_item(selection)) {
563 _targets->Select (*item);
567 _ignore_selection_change = false;
569 _ignore_check_change = true;
571 for (auto const& checked: _checked_screens) {
572 if (auto item = screen_to_item(checked)) {
573 _targets->CheckItem(*item, wxCHK_CHECKED);
574 setup_cinema_checked_state(*item);
578 _ignore_check_change = false;
583 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
585 auto screen = item_to_screen(item);
586 DCPOMATIC_ASSERT(screen);
588 _checked_screens.insert(screen);
590 _checked_screens.erase(screen);
596 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
598 auto cinema = _targets->GetItemParent(screen);
599 DCPOMATIC_ASSERT (cinema.IsOk());
602 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
603 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
610 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
611 } else if (unchecked == 0) {
612 _targets->CheckItem(cinema, wxCHK_CHECKED);
614 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
620 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
622 if (_ignore_check_change) {
626 if (item_to_cinema(ev.GetItem())) {
627 /* Cinema: check/uncheck all children */
628 auto const checked = _targets->GetCheckedState(ev.GetItem());
629 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
630 _targets->CheckItem(child, checked);
631 set_screen_checked(child, checked);
634 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
635 setup_cinema_checked_state(ev.GetItem());
643 ScreensPanel::item_to_cinema (wxTreeListItem item) const
645 auto iter = _item_to_cinema.find (item);
646 if (iter == _item_to_cinema.end()) {
655 ScreensPanel::item_to_screen (wxTreeListItem item) const
657 auto iter = _item_to_screen.find (item);
658 if (iter == _item_to_screen.end()) {
666 optional<wxTreeListItem>
667 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
669 auto iter = _cinema_to_item.find (cinema);
670 if (iter == _cinema_to_item.end()) {
678 optional<wxTreeListItem>
679 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
681 auto iter = _screen_to_item.find (screen);
682 if (iter == _screen_to_item.end()) {
691 ScreensPanel::notify_cinemas_changed()
693 _ignore_cinemas_changed = true;
694 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
697 Config::instance()->changed(Config::CINEMAS);
698 } catch (FileError& e) {
699 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()));
708 ScreensPanel::config_changed(Config::Property property)
710 if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
717 ScreensPanel::item_activated(wxTreeListEvent& ev)
719 auto iter = _item_to_cinema.find(ev.GetItem());
720 if (iter != _item_to_cinema.end()) {
721 edit_cinema(iter->second);
723 auto iter = _item_to_screen.find(ev.GetItem());
724 if (iter != _item_to_screen.end()) {
725 edit_screen(iter->second);