Move UTC offset for KDMs from the cinema to the point of KDM creation (#2300).
[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
22 #include "dcpomatic_choice.h"
23 #include "kdm_timing_panel.h"
24 #include "static_text.h"
25 #include "time_picker.h"
26 #include "wx_util.h"
27 #include "lib/config.h"
28 #include <dcp/utc_offset.h>
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/datectrl.h>
32 #include <wx/dateevt.h>
33 LIBDCP_ENABLE_WARNINGS
34
35
36 using std::cout;
37 using boost::bind;
38
39
40 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
41         : wxPanel (parent, wxID_ANY)
42 {
43         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
44
45 #ifdef __WXGTK3__
46         /* wxDatePickerCtrl is too small with the GTK3 backend so we need to make it bigger with some fudge factors */
47         wxClientDC dc (parent);
48         auto size = dc.GetTextExtent(wxT("99/99/9999"));
49         size.SetWidth (size.GetWidth() * 1.75);
50         size.SetHeight (-1);
51 #else
52         auto size = wxDefaultSize;
53 #endif
54
55         auto table = new wxBoxSizer (wxHORIZONTAL);
56         add_label_to_sizer (table, this, _("From"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
57         wxDateTime from;
58         from.SetToCurrent ();
59         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from, wxDefaultPosition, size);
60 #ifdef DCPOMATIC_OSX
61         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
62         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
63 #else
64         table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
65 #endif
66
67 #ifdef __WXGTK3__
68         _from_time = new TimePickerText (this, from);
69 #else
70         _from_time = new TimePickerSpin (this, from);
71 #endif
72
73         table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
74
75         add_label_to_sizer (table, this, _("until"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
76         auto to = from;
77
78         auto const duration = Config::instance()->default_kdm_duration();
79         switch (duration.unit) {
80         case RoughDuration::Unit::DAYS:
81                 to.Add(wxDateSpan(0, 0, 0, duration.duration));
82                 break;
83         case RoughDuration::Unit::WEEKS:
84                 to.Add(wxDateSpan(0, 0, duration.duration, 0));
85                 break;
86         case RoughDuration::Unit::MONTHS:
87                 to.Add(wxDateSpan(0, duration.duration, 0, 0));
88                 break;
89         case RoughDuration::Unit::YEARS:
90                 to.Add(wxDateSpan(duration.duration, 0, 0, 0));
91                 break;
92         }
93
94         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to, wxDefaultPosition, size);
95 #ifdef DCPOMATIC_OSX
96         /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
97         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
98 #else
99         table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
100 #endif
101
102 #ifdef __WXGTK3__
103         _until_time = new TimePickerText (this, to);
104 #else
105         _until_time = new TimePickerSpin (this, to);
106 #endif
107
108         table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
109
110         add_label_to_sizer(table, this, _("UTC offset (time zone)"), true, 1, wxALIGN_CENTRE_VERTICAL);
111         _utc_offset = new Choice(this);
112         table->Add(_utc_offset, 0, wxALIGN_CENTRE_VERTICAL | wxLEFT, DCPOMATIC_SIZER_X_GAP);
113
114         overall_sizer->Add (table, 0, wxTOP, DCPOMATIC_SIZER_GAP);
115
116         _warning = new StaticText (this, wxT(""));
117         overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
118         wxFont font = _warning->GetFont();
119         font.SetStyle(wxFONTSTYLE_ITALIC);
120         font.SetPointSize(font.GetPointSize() - 1);
121         _warning->SetForegroundColour (wxColour (255, 0, 0));
122         _warning->SetFont(font);
123
124         /* Default to UTC */
125         size_t sel = 0;
126         for (size_t i = 0; i < _offsets.size(); ++i) {
127                 _utc_offset->add(_offsets[i].name);
128                 if (_offsets[i].hour == 0 && _offsets[i].minute == 0) {
129                         sel = i;
130                 }
131         }
132
133         _utc_offset->set(sel);
134
135         /* I said I've been to the year 3000.  Not much has changed but they live underwater.  And your In-in-in-interop DCP
136            is pretty fine.
137          */
138         _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
139         _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
140
141         _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
142         _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
143         _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
144         _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
145         _utc_offset->bind(&KDMTimingPanel::changed, this);
146
147         SetSizer (overall_sizer);
148 }
149
150
151 dcp::LocalTime
152 KDMTimingPanel::from () const
153 {
154         return local_time(_from_date, _from_time, utc_offset());
155 }
156
157
158 dcp::LocalTime
159 KDMTimingPanel::local_time(wxDatePickerCtrl* date_picker, TimePicker* time_picker, dcp::UTCOffset offset)
160 {
161         auto const date = date_picker->GetValue ();
162         return dcp::LocalTime(
163                 date.GetYear(),
164                 date.GetMonth() + 1,
165                 date.GetDay(),
166                 time_picker->hours(),
167                 time_picker->minutes(),
168                 offset
169                 );
170 }
171
172
173 dcp::LocalTime
174 KDMTimingPanel::until () const
175 {
176         return local_time(_until_date, _until_time, utc_offset());
177 }
178
179
180 bool
181 KDMTimingPanel::valid () const
182 {
183         return until() > from();
184 }
185
186
187 void
188 KDMTimingPanel::changed () const
189 {
190         if (valid ()) {
191                 _warning->SetLabel (wxT (""));
192         } else {
193                 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
194         }
195
196         TimingChanged ();
197 }
198
199
200 dcp::UTCOffset
201 KDMTimingPanel::utc_offset() const
202 {
203         auto const sel = _utc_offset->get();
204         if (!sel || *sel >= int(_offsets.size())) {
205                 return {};
206         }
207
208         auto const& offset = _offsets[*sel];
209         int const minute_scale = offset.hour < 0 ? -1 : 1;
210
211         return { offset.hour, minute_scale * offset.minute };
212 }
213
214