Stop invalid dates causing boost::posix_time to raise exceptions.
[dcpomatic.git] / src / wx / kdm_timing_panel.cc
1 /*
2     Copyright (C) 2015-2020 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 "static_text.h"
25 #include <wx/datectrl.h>
26 #include <wx/dateevt.h>
27
28 using std::cout;
29 using boost::bind;
30
31 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
32         : wxPanel (parent, wxID_ANY)
33 {
34         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
35
36         wxSizer* table = new wxBoxSizer (wxHORIZONTAL);
37         add_label_to_sizer (table, this, _("From"), true);
38         wxDateTime from;
39         from.SetToCurrent ();
40         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
41         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
42         _from_time = new TimePicker (this, from);
43 #ifdef DCPOMATIC_OSX
44         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
45         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
46 #else
47         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
48 #endif
49
50         add_label_to_sizer (table, this, _("until"), true);
51         wxDateTime to = from;
52         /* 1 week from now */
53         to.Add (wxDateSpan (0, 0, 1, 0));
54         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
55         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
56         _until_time = new TimePicker (this, to);
57 #ifdef DCPOMATIC_OSX
58         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL | wxTOP, 4);
59 #else
60         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
61 #endif
62
63         overall_sizer->Add (table);
64
65         _warning = new StaticText (this, wxT(""));
66         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
67         wxFont font = _warning->GetFont();
68         font.SetStyle(wxFONTSTYLE_ITALIC);
69         font.SetPointSize(font.GetPointSize() - 1);
70         _warning->SetForegroundColour (wxColour (255, 0, 0));
71         _warning->SetFont(font);
72
73         /* I said I've been to the year 3000.  Not much has changed but they live underwater.  And your In-in-in-interop DCP
74            is pretty fine.
75          */
76         _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
77         _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
78
79         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
80         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
81         _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
82         _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
83
84         SetSizer (overall_sizer);
85 }
86
87 boost::posix_time::ptime
88 KDMTimingPanel::from () const
89 {
90         return posix_time (_from_date, _from_time);
91 }
92
93 boost::posix_time::ptime
94 KDMTimingPanel::posix_time (wxDatePickerCtrl* date_picker, TimePicker* time_picker)
95 {
96         wxDateTime const date = date_picker->GetValue ();
97         return boost::posix_time::ptime (
98                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
99                 boost::posix_time::time_duration (time_picker->hours(), time_picker->minutes(), 0)
100                 );
101 }
102
103 boost::posix_time::ptime
104 KDMTimingPanel::until () const
105 {
106         return posix_time (_until_date, _until_time);
107 }
108
109 bool
110 KDMTimingPanel::valid () const
111 {
112         return until() > from();
113 }
114
115 void
116 KDMTimingPanel::changed () const
117 {
118         if (valid ()) {
119                 _warning->SetLabel (wxT (""));
120         } else {
121                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
122         }
123
124         TimingChanged ();
125 }