2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <wx/treectrl.h>
21 #include <wx/datectrl.h>
22 #include <wx/timectrl.h>
23 #include "lib/cinema.h"
24 #include "lib/config.h"
25 #include "kdm_dialog.h"
26 #include "cinema_dialog.h"
27 #include "screen_dialog.h"
29 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
30 #include "dir_picker_ctrl.h"
32 #include <wx/filepicker.h>
40 using boost::shared_ptr;
42 KDMDialog::KDMDialog (wxWindow* parent)
43 : wxDialog (parent, wxID_ANY, _("Make KDMs"))
45 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
46 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
48 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS);
49 targets->Add (_targets, 1, wxEXPAND | wxALL, 6);
51 _root = _targets->AddRoot ("Foo");
53 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
54 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
58 _targets->ExpandAll ();
60 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
62 _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
63 target_buttons->Add (_add_cinema, 1, wxEXPAND, 6);
64 _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
65 target_buttons->Add (_edit_cinema, 1, wxEXPAND, 6);
66 _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
67 target_buttons->Add (_remove_cinema, 1, wxEXPAND, 6);
69 _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
70 target_buttons->Add (_add_screen, 1, wxEXPAND, 6);
71 _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
72 target_buttons->Add (_edit_screen, 1, wxEXPAND, 6);
73 _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
74 target_buttons->Add (_remove_screen, 1, wxEXPAND, 6);
76 targets->Add (target_buttons, 0, 0, 6);
78 vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
80 wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
81 add_label_to_sizer (table, this, "From", true);
82 _from_date = new wxDatePickerCtrl (this, wxID_ANY);
83 table->Add (_from_date, 1, wxEXPAND);
84 _from_time = new wxTimePickerCtrl (this, wxID_ANY);
85 table->Add (_from_time, 1, wxEXPAND);
87 add_label_to_sizer (table, this, "Until", true);
88 _until_date = new wxDatePickerCtrl (this, wxID_ANY);
89 table->Add (_until_date, 1, wxEXPAND);
90 _until_time = new wxTimePickerCtrl (this, wxID_ANY);
91 table->Add (_until_time, 1, wxEXPAND);
93 add_label_to_sizer (table, this, "Write to", true);
95 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
96 _folder = new DirPickerCtrl (this);
98 _folder = new wxDirPickerCtrl (this, wxID_ANY);
101 table->Add (_folder, 1, wxEXPAND);
103 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
105 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
107 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
110 _targets->Connect (wxID_ANY, wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler (KDMDialog::targets_selection_changed), 0, this);
112 _add_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_cinema_clicked), 0, this);
113 _edit_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_cinema_clicked), 0, this);
114 _remove_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_cinema_clicked), 0, this);
116 _add_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_screen_clicked), 0, this);
117 _edit_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_screen_clicked), 0, this);
118 _remove_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_screen_clicked), 0, this);
120 setup_sensitivity ();
124 vertical->SetSizeHints (this);
127 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
128 KDMDialog::selected_cinemas () const
130 wxArrayTreeItemIds s;
131 _targets->GetSelections (s);
133 list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
134 for (size_t i = 0; i < s.GetCount(); ++i) {
135 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
136 if (j != _cinemas.end ()) {
137 c.push_back (make_pair (j->first, j->second));
144 list<pair<wxTreeItemId, shared_ptr<Screen> > >
145 KDMDialog::selected_screens () const
147 wxArrayTreeItemIds s;
148 _targets->GetSelections (s);
150 list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
151 for (size_t i = 0; i < s.GetCount(); ++i) {
152 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
153 if (j != _screens.end ()) {
154 c.push_back (make_pair (j->first, j->second));
162 KDMDialog::targets_selection_changed (wxCommandEvent &)
164 setup_sensitivity ();
168 KDMDialog::setup_sensitivity ()
170 bool const sc = selected_cinemas().size() == 1;
171 bool const ss = selected_screens().size() == 1;
173 _edit_cinema->Enable (sc);
174 _remove_cinema->Enable (sc);
176 _add_screen->Enable (sc);
177 _edit_screen->Enable (ss);
178 _remove_screen->Enable (ss);
180 wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
181 ok->Enable (sc || sc);
185 KDMDialog::add_cinema (shared_ptr<Cinema> c)
187 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
189 list<shared_ptr<Screen> > sc = c->screens ();
190 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
196 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
198 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
199 while (i != _cinemas.end() && i->second != c) {
203 if (i == _cinemas.end()) {
207 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
211 KDMDialog::add_cinema_clicked (wxCommandEvent &)
213 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
216 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
217 Config::instance()->add_cinema (c);
220 Config::instance()->write ();
226 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
228 if (selected_cinemas().size() != 1) {
232 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
234 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
237 c.second->name = d->name ();
238 c.second->email = d->email ();
239 _targets->SetItemText (c.first, std_to_wx (d->name()));
241 Config::instance()->write ();
247 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
249 if (selected_cinemas().size() != 1) {
253 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
255 Config::instance()->remove_cinema (c.second);
256 _targets->Delete (c.first);
258 Config::instance()->write ();
262 KDMDialog::add_screen_clicked (wxCommandEvent &)
264 if (selected_cinemas().size() != 1) {
268 shared_ptr<Cinema> c = selected_cinemas().front().second;
270 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
273 shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
277 Config::instance()->write ();
283 KDMDialog::edit_screen_clicked (wxCommandEvent &)
285 if (selected_screens().size() != 1) {
289 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
291 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
294 s.second->name = d->name ();
295 s.second->certificate = d->certificate ();
296 _targets->SetItemText (s.first, std_to_wx (d->name()));
298 Config::instance()->write ();
304 KDMDialog::remove_screen_clicked (wxCommandEvent &)
306 if (selected_screens().size() != 1) {
310 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
312 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
313 list<shared_ptr<Screen> > sc = i->second->screens ();
314 while (i != _cinemas.end() && find (sc.begin(), sc.end(), s.second) == sc.end()) {
318 if (i == _cinemas.end()) {
322 i->second->remove_screen (s.second);
323 _targets->Delete (s.first);
325 Config::instance()->write ();
328 list<shared_ptr<Screen> >
329 KDMDialog::screens () const
331 list<shared_ptr<Screen> > s;
333 list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
334 for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
335 list<shared_ptr<Screen> > sc = i->second->screens ();
336 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
341 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
342 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
343 s.push_back (i->second);
352 boost::posix_time::ptime
353 KDMDialog::from () const
355 return posix_time (_from_date, _from_time);
358 boost::posix_time::ptime
359 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
361 wxDateTime const date = date_picker->GetValue ();
362 wxDateTime const time = time_picker->GetValue ();
363 return boost::posix_time::ptime (
364 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
365 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
369 boost::posix_time::ptime
370 KDMDialog::until () const
372 return posix_time (_until_date, _until_time);
376 KDMDialog::directory () const
378 return wx_to_std (_folder->GetPath ());