53f674e2ecbe01f6edc83ec80461cd68c2259467
[dcpomatic.git] / src / wx / kdm_timing_panel.cc
1 /*
2     Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "kdm_timing_panel.h"
22 #include "wx_util.h"
23 #include "time_picker.h"
24 #include <wx/datectrl.h>
25 #include <wx/dateevt.h>
26
27 using std::cout;
28 using boost::bind;
29
30 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
31         : wxPanel (parent, wxID_ANY)
32 {
33         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
34
35         wxSizer* table = new wxBoxSizer (wxHORIZONTAL);
36         add_label_to_sizer (table, this, _("From"), true);
37         wxDateTime from;
38         from.SetToCurrent ();
39         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
40         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
41         _from_time = new TimePicker (this, from);
42 #ifdef DCPOMATIC_OSX
43         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
44         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
45 #else
46         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
47 #endif
48
49         add_label_to_sizer (table, this, _("until"), true);
50         wxDateTime to = from;
51         /* 1 week from now */
52         to.Add (wxDateSpan (0, 0, 1, 0));
53         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
54         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
55         _until_time = new TimePicker (this, to);
56 #ifdef DCPOMATIC_OSX
57         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
58 #else
59         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
60 #endif
61
62         overall_sizer->Add (table);
63
64         _warning = new wxStaticText (this, wxID_ANY, wxT (""));
65         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
66         wxFont font = _warning->GetFont();
67         font.SetStyle(wxFONTSTYLE_ITALIC);
68         font.SetPointSize(font.GetPointSize() - 1);
69         _warning->SetForegroundColour (wxColour (255, 0, 0));
70         _warning->SetFont(font);
71
72         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
73         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
74         _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
75         _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
76
77         SetSizer (overall_sizer);
78 }
79
80 boost::posix_time::ptime
81 KDMTimingPanel::from () const
82 {
83         return posix_time (_from_date, _from_time);
84 }
85
86 boost::posix_time::ptime
87 KDMTimingPanel::posix_time (wxDatePickerCtrl* date_picker, TimePicker* time_picker)
88 {
89         wxDateTime const date = date_picker->GetValue ();
90         return boost::posix_time::ptime (
91                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
92                 boost::posix_time::time_duration (time_picker->hours(), time_picker->minutes(), 0)
93                 );
94 }
95
96 boost::posix_time::ptime
97 KDMTimingPanel::until () const
98 {
99         return posix_time (_until_date, _until_time);
100 }
101
102 bool
103 KDMTimingPanel::valid () const
104 {
105         return until() > from();
106 }
107
108 void
109 KDMTimingPanel::changed () const
110 {
111         if (valid ()) {
112                 _warning->SetLabel (wxT (""));
113         } else {
114                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
115         }
116
117         TimingChanged ();
118 }