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 "kdm_timing_panel.h"
23 #include "static_text.h"
24 #include "time_picker.h"
26 #include "lib/config.h"
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <wx/datectrl.h>
30 #include <wx/dateevt.h>
31 LIBDCP_ENABLE_WARNINGS
38 KDMTimingPanel::KDMTimingPanel (wxWindow* parent)
39 : wxPanel (parent, wxID_ANY)
41 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
44 /* wxDatePickerCtrl is too small with the GTK3 backend so we need to make it bigger with some fudge factors */
45 wxClientDC dc (parent);
46 auto size = dc.GetTextExtent(wxT("99/99/9999"));
47 size.SetWidth (size.GetWidth() * 1.75);
50 auto size = wxDefaultSize;
53 auto table = new wxBoxSizer (wxHORIZONTAL);
54 add_label_to_sizer (table, this, _("From"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
57 _from_date = new wxDatePickerCtrl (this, wxID_ANY, from, wxDefaultPosition, size);
59 /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
60 table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
62 table->Add (_from_date, 0, wxALIGN_CENTER_VERTICAL);
66 _from_time = new TimePickerText (this, from);
68 _from_time = new TimePickerSpin (this, from);
71 table->Add (_from_time, 0, wxALIGN_CENTER_VERTICAL);
73 add_label_to_sizer (table, this, _("until"), false, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
76 auto const duration = Config::instance()->default_kdm_duration();
77 switch (duration.unit) {
78 case RoughDuration::Unit::DAYS:
79 to.Add(wxDateSpan(0, 0, 0, duration.duration));
81 case RoughDuration::Unit::WEEKS:
82 to.Add(wxDateSpan(0, 0, duration.duration, 0));
84 case RoughDuration::Unit::MONTHS:
85 to.Add(wxDateSpan(0, duration.duration, 0, 0));
87 case RoughDuration::Unit::YEARS:
88 to.Add(wxDateSpan(duration.duration, 0, 0, 0));
92 _until_date = new wxDatePickerCtrl (this, wxID_ANY, to, wxDefaultPosition, size);
94 /* Hack to tweak alignment, which I can't get right by "proper" means for some reason */
95 table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4);
97 table->Add (_until_date, 0, wxALIGN_CENTER_VERTICAL);
101 _until_time = new TimePickerText (this, to);
103 _until_time = new TimePickerSpin (this, to);
106 table->Add (_until_time, 0, wxALIGN_CENTER_VERTICAL);
108 overall_sizer->Add (table, 0, wxTOP, DCPOMATIC_SIZER_GAP);
110 _warning = new StaticText (this, wxT(""));
111 overall_sizer->Add (_warning, 0, wxTOP, DCPOMATIC_SIZER_GAP);
112 wxFont font = _warning->GetFont();
113 font.SetStyle(wxFONTSTYLE_ITALIC);
114 font.SetPointSize(font.GetPointSize() - 1);
115 _warning->SetForegroundColour (wxColour (255, 0, 0));
116 _warning->SetFont(font);
118 /* I said I've been to the year 3000. Not much has changed but they live underwater. And your In-in-in-interop DCP
121 _from_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
122 _until_date->SetRange(wxDateTime(1, wxDateTime::Jan, 1900, 0, 0, 0, 0), wxDateTime(31, wxDateTime::Dec, 3000, 0, 0, 0, 0));
124 _from_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
125 _until_date->Bind (wxEVT_DATE_CHANGED, bind (&KDMTimingPanel::changed, this));
126 _from_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
127 _until_time->Changed.connect (bind (&KDMTimingPanel::changed, this));
129 SetSizer (overall_sizer);
133 boost::posix_time::ptime
134 KDMTimingPanel::from () const
136 return posix_time (_from_date, _from_time);
140 boost::posix_time::ptime
141 KDMTimingPanel::posix_time (wxDatePickerCtrl* date_picker, TimePicker* time_picker)
143 auto const date = date_picker->GetValue ();
144 return boost::posix_time::ptime (
145 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
146 boost::posix_time::time_duration (time_picker->hours(), time_picker->minutes(), 0)
151 boost::posix_time::ptime
152 KDMTimingPanel::until () const
154 return posix_time (_until_date, _until_time);
159 KDMTimingPanel::valid () const
161 return until() > from();
166 KDMTimingPanel::changed () const
169 _warning->SetLabel (wxT (""));
171 _warning->SetLabel (_("The 'until' time must be after the 'from' time."));