2 Copyright (C) 2015-2020 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 "dcpomatic_choice.h"
23 #include "kdm_timing_panel.h"
24 #include "static_text.h"
25 #include "time_picker.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
40 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
41 : wxPanel (parent, wxID_ANY)
43 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
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);
52 auto size = wxDefaultSize;
55 auto table = new wxBoxSizer (wxHORIZONTAL);
56 add_label_to_sizer (table, this, _("From"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
59 _from_date = new wxDatePickerCtrl (this, wxID_ANY, from, wxDefaultPosition, size);
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);
64 table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
68 _from_time = new TimePickerText (this, from);
70 _from_time = new TimePickerSpin (this, from);
73 table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
75 add_label_to_sizer (table, this, _("until"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
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));
83 case RoughDuration::Unit::WEEKS:
84 to.Add(wxDateSpan(0, 0, duration.duration, 0));
86 case RoughDuration::Unit::MONTHS:
87 to.Add(wxDateSpan(0, duration.duration, 0, 0));
89 case RoughDuration::Unit::YEARS:
90 to.Add(wxDateSpan(duration.duration, 0, 0, 0));
94 _until_date = new wxDatePickerCtrl (this, wxID_ANY, to, wxDefaultPosition, size);
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);
99 table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
103 _until_time = new TimePickerText (this, to);
105 _until_time = new TimePickerSpin (this, to);
108 table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
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);
114 overall_sizer->Add (table, 0, wxTOP, DCPOMATIC_SIZER_GAP);
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);
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) {
133 _utc_offset->set(sel);
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
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));
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);
147 SetSizer (overall_sizer);
152 KDMTimingPanel::from () const
154 return local_time(_from_date, _from_time, utc_offset());
159 KDMTimingPanel::local_time(wxDatePickerCtrl* date_picker, TimePicker* time_picker, dcp::UTCOffset offset)
161 auto const date = date_picker->GetValue ();
162 return dcp::LocalTime(
166 time_picker->hours(),
167 time_picker->minutes(),
174 KDMTimingPanel::until () const
176 return local_time(_until_date, _until_time, utc_offset());
181 KDMTimingPanel::valid () const
183 return until() > from();
188 KDMTimingPanel::changed () const
191 _warning->SetLabel (wxT (""));
193 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
201 KDMTimingPanel::utc_offset() const
203 auto const sel = _utc_offset->get();
204 if (!sel || *sel >= int(_offsets.size())) {
208 auto const& offset = _offsets[*sel];
209 int const minute_scale = offset.hour < 0 ? -1 : 1;
211 return { offset.hour, minute_scale * offset.minute };