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);
125 auto const sel = get_offsets(_offsets);
126 for (auto const& offset: _offsets) {
127 _utc_offset->add_entry(offset.name);
129 _utc_offset->set(sel);
131 /* I said I've been to the year 3000. Not much has changed but they live underwater. And your In-in-in-interop DCP
134 _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
135 _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
137 _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
138 _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
139 _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
140 _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
141 _utc_offset->bind(&KDMTimingPanel::utc_offset_changed, this);
143 SetSizer (overall_sizer);
148 KDMTimingPanel::from () const
150 return local_time(_from_date, _from_time, utc_offset());
155 KDMTimingPanel::local_time(wxDatePickerCtrl* date_picker, TimePicker* time_picker, dcp::UTCOffset offset)
157 auto const date = date_picker->GetValue ();
158 return dcp::LocalTime(
162 time_picker->hours(),
163 time_picker->minutes(),
170 KDMTimingPanel::until () const
172 return local_time(_until_date, _until_time, utc_offset());
177 KDMTimingPanel::valid () const
179 return until() > from();
184 KDMTimingPanel::changed () const
187 _warning->SetLabel (wxT (""));
189 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));
197 KDMTimingPanel::utc_offset() const
199 auto const sel = _utc_offset->get();
200 if (!sel || *sel >= int(_offsets.size())) {
204 return _offsets[*sel].offset;
209 KDMTimingPanel::utc_offset_changed()
211 _utc_offset_changed_once = true;
217 KDMTimingPanel::suggest_utc_offset(dcp::UTCOffset offset)
219 if (!_utc_offset_changed_once) {
220 for (size_t i = 0; i < _offsets.size(); ++i) {
221 if (_offsets[i].offset == offset) {