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);
106 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
107 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
108 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
110 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
111 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
112 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
114 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
115 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
119 _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
123 ScreensPanel::~ScreensPanel ()
125 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
126 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
131 ScreensPanel::check_all ()
133 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
134 _targets->CheckItem(cinema, wxCHK_CHECKED);
135 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
136 _targets->CheckItem(screen, wxCHK_CHECKED);
137 set_screen_checked(screen, true);
144 ScreensPanel::uncheck_all ()
146 for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk(); cinema = _targets->GetNextSibling(cinema)) {
147 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
148 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
149 _targets->CheckItem(screen, wxCHK_UNCHECKED);
150 set_screen_checked(screen, false);
157 ScreensPanel::setup_sensitivity ()
159 bool const sc = _selected_cinemas.size() == 1;
160 bool const ss = _selected_screens.size() == 1;
162 _edit_cinema->Enable (sc || ss);
163 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
165 _add_screen->Enable (sc || ss);
166 _edit_screen->Enable (ss);
167 _remove_screen->Enable (_selected_screens.size() >= 1);
172 ScreensPanel::convert_to_lower(string& s)
174 transform(s.begin(), s.end(), s.begin(), ::tolower);
179 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string lower_case_search)
181 if (lower_case_search.empty()) {
185 auto name = cinema->name;
186 convert_to_lower(name);
187 return name.find(lower_case_search) != string::npos;
191 optional<wxTreeListItem>
192 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
194 auto search = wx_to_std (_search->GetValue ());
195 convert_to_lower(search);
196 if (!matches_search(cinema, search)) {
200 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
202 _item_to_cinema[id] = cinema;
203 _cinema_to_item[cinema] = id;
205 for (auto screen: cinema->screens()) {
206 add_screen (cinema, screen);
213 optional<wxTreeListItem>
214 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
216 auto item = cinema_to_item(cinema);
221 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
223 _item_to_screen[id] = screen;
224 _screen_to_item[screen] = id;
231 ScreensPanel::add_cinema_clicked ()
233 auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
234 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
236 if (dialog->ShowModal() == wxID_OK) {
237 auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
239 auto cinemas = sorted_cinemas();
242 _ignore_cinemas_changed = true;
243 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
244 Config::instance()->add_cinema(cinema);
245 } catch (FileError& e) {
246 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()));
250 wxTreeListItem previous = wxTLI_FIRST;
252 for (auto existing_cinema: cinemas) {
253 if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
254 /* existing_cinema should be after the one we're inserting */
258 auto item = cinema_to_item(existing_cinema);
259 DCPOMATIC_ASSERT(item);
263 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
266 _targets->UnselectAll ();
267 _targets->Select (*item);
271 selection_changed ();
276 ScreensPanel::cinema_for_operation () const
278 if (_selected_cinemas.size() == 1) {
279 return _selected_cinemas[0];
280 } else if (_selected_screens.size() == 1) {
281 return _selected_screens[0]->cinema;
289 ScreensPanel::edit_cinema_clicked ()
291 auto cinema = cinema_for_operation ();
296 auto dialog = new CinemaDialog(
297 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
299 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
301 if (dialog->ShowModal() == wxID_OK) {
302 cinema->name = dialog->name();
303 cinema->emails = dialog->emails();
304 cinema->notes = dialog->notes();
305 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
306 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
307 notify_cinemas_changed();
308 auto item = cinema_to_item(cinema);
309 DCPOMATIC_ASSERT(item);
310 _targets->SetItemText (*item, std_to_wx(dialog->name()));
316 ScreensPanel::remove_cinema_clicked ()
318 if (_selected_cinemas.size() == 1) {
319 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
323 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
328 for (auto const& cinema: _selected_cinemas) {
329 _ignore_cinemas_changed = true;
330 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
331 Config::instance()->remove_cinema(cinema);
332 auto item = cinema_to_item(cinema);
333 DCPOMATIC_ASSERT(item);
334 _targets->DeleteItem(*item);
337 selection_changed ();
342 ScreensPanel::add_screen_clicked ()
344 auto cinema = cinema_for_operation ();
349 auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
350 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
352 if (dialog->ShowModal () != wxID_OK) {
356 for (auto screen: cinema->screens()) {
357 if (screen->name == dialog->name()) {
361 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
362 std_to_wx(dialog->name()).data()
369 auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
370 cinema->add_screen (screen);
371 notify_cinemas_changed();
373 auto id = add_screen (cinema, screen);
375 _targets->Expand (id.get ());
381 ScreensPanel::edit_screen_clicked ()
383 if (_selected_screens.size() != 1) {
387 auto edit_screen = _selected_screens[0];
389 auto dialog = new ScreenDialog(
390 GetParent(), _("Edit screen"),
393 edit_screen->recipient,
394 edit_screen->recipient_file,
395 edit_screen->trusted_devices
397 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
399 if (dialog->ShowModal() != wxID_OK) {
403 auto cinema = edit_screen->cinema;
404 for (auto screen: cinema->screens()) {
405 if (screen != edit_screen && screen->name == dialog->name()) {
409 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
410 std_to_wx(dialog->name()).data()
417 edit_screen->name = dialog->name();
418 edit_screen->notes = dialog->notes();
419 edit_screen->recipient = dialog->recipient();
420 edit_screen->recipient_file = dialog->recipient_file();
421 edit_screen->trusted_devices = dialog->trusted_devices();
422 notify_cinemas_changed();
424 auto item = screen_to_item(edit_screen);
425 DCPOMATIC_ASSERT (item);
426 _targets->SetItemText (*item, std_to_wx(dialog->name()));
431 ScreensPanel::remove_screen_clicked ()
433 if (_selected_screens.size() == 1) {
434 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
438 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
443 for (auto const& screen: _selected_screens) {
444 screen->cinema->remove_screen(screen);
445 auto item = screen_to_item(screen);
446 DCPOMATIC_ASSERT(item);
447 _targets->DeleteItem(*item);
450 notify_cinemas_changed();
454 vector<shared_ptr<Screen>>
455 ScreensPanel::screens () const
457 vector<shared_ptr<Screen>> output;
458 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
464 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
466 selection_changed ();
471 ScreensPanel::selection_changed ()
473 if (_ignore_selection_change) {
477 wxTreeListItems selection;
478 _targets->GetSelections (selection);
480 _selected_cinemas.clear ();
481 _selected_screens.clear ();
483 for (size_t i = 0; i < selection.size(); ++i) {
484 if (auto cinema = item_to_cinema(selection[i])) {
485 _selected_cinemas.push_back(cinema);
487 if (auto screen = item_to_screen(selection[i])) {
488 _selected_screens.push_back(screen);
492 setup_sensitivity ();
496 list<shared_ptr<Cinema>>
497 ScreensPanel::sorted_cinemas() const
499 auto cinemas = Config::instance()->cinemas();
502 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
510 ScreensPanel::add_cinemas ()
512 for (auto cinema: sorted_cinemas()) {
513 add_cinema (cinema, wxTLI_LAST);
519 ScreensPanel::clear_and_re_add()
521 _targets->DeleteAllItems ();
523 _item_to_cinema.clear ();
524 _cinema_to_item.clear ();
525 _item_to_screen.clear ();
526 _screen_to_item.clear ();
533 ScreensPanel::search_changed ()
537 _ignore_selection_change = true;
539 for (auto const& selection: _selected_cinemas) {
540 if (auto item = cinema_to_item(selection)) {
541 _targets->Select (*item);
545 for (auto const& selection: _selected_screens) {
546 if (auto item = screen_to_item(selection)) {
547 _targets->Select (*item);
551 _ignore_selection_change = false;
553 _ignore_check_change = true;
555 for (auto const& checked: _checked_screens) {
556 if (auto item = screen_to_item(checked)) {
557 _targets->CheckItem(*item, wxCHK_CHECKED);
558 setup_cinema_checked_state(*item);
562 _ignore_check_change = false;
567 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
569 auto screen = item_to_screen(item);
570 DCPOMATIC_ASSERT(screen);
572 _checked_screens.insert(screen);
574 _checked_screens.erase(screen);
580 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
582 auto cinema = _targets->GetItemParent(screen);
583 DCPOMATIC_ASSERT (cinema.IsOk());
586 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
587 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
594 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
595 } else if (unchecked == 0) {
596 _targets->CheckItem(cinema, wxCHK_CHECKED);
598 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
604 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
606 if (_ignore_check_change) {
610 if (item_to_cinema(ev.GetItem())) {
611 /* Cinema: check/uncheck all children */
612 auto const checked = _targets->GetCheckedState(ev.GetItem());
613 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
614 _targets->CheckItem(child, checked);
615 set_screen_checked(child, checked);
618 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
619 setup_cinema_checked_state(ev.GetItem());
627 ScreensPanel::item_to_cinema (wxTreeListItem item) const
629 auto iter = _item_to_cinema.find (item);
630 if (iter == _item_to_cinema.end()) {
639 ScreensPanel::item_to_screen (wxTreeListItem item) const
641 auto iter = _item_to_screen.find (item);
642 if (iter == _item_to_screen.end()) {
650 optional<wxTreeListItem>
651 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
653 auto iter = _cinema_to_item.find (cinema);
654 if (iter == _cinema_to_item.end()) {
662 optional<wxTreeListItem>
663 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
665 auto iter = _screen_to_item.find (screen);
666 if (iter == _screen_to_item.end()) {
675 ScreensPanel::notify_cinemas_changed()
677 _ignore_cinemas_changed = true;
678 ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
681 Config::instance()->changed(Config::CINEMAS);
682 } catch (FileError& e) {
683 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()));
692 ScreensPanel::config_changed(Config::Property property)
694 if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {