1 #include "common/common_pch.h"
2
3 #include <QDateTimeEdit>
4
5 #include "common/qt.h"
6 #include "mkvtoolnix-gui/header_editor/time_value_page.h"
7 #include "mkvtoolnix-gui/main_window/main_window.h"
8 #include "mkvtoolnix-gui/util/date_time.h"
9 #include "mkvtoolnix-gui/util/settings.h"
10
11 namespace mtx::gui::HeaderEditor {
12
13 using namespace mtx::gui;
14
TimeValuePage(Tab & parent,PageBase & topLevelPage,EbmlMaster & master,EbmlCallbacks const & callbacks,translatable_string_c const & title,translatable_string_c const & description)15 TimeValuePage::TimeValuePage(Tab &parent,
16 PageBase &topLevelPage,
17 EbmlMaster &master,
18 EbmlCallbacks const &callbacks,
19 translatable_string_c const &title,
20 translatable_string_c const &description)
21 : ValuePage{parent, topLevelPage, master, callbacks, ValueType::Timestamp, title, description}
22 {
23 connect(MainWindow::get(), &MainWindow::preferencesChanged, this, &TimeValuePage::showInRequestedTimeSpec);
24 }
25
~TimeValuePage()26 TimeValuePage::~TimeValuePage() {
27 }
28
29 QWidget *
createInputControl()30 TimeValuePage::createInputControl() {
31 auto &cfg = Util::Settings::get();
32
33 if (m_element)
34 m_originalValueUTC = QDateTime::fromSecsSinceEpoch(static_cast<EbmlDate *>(m_element)->GetEpochDate(), Qt::UTC);
35
36 m_dteValue = new QDateTimeEdit{this};
37 m_dteValue->setCalendarPopup(true);
38 m_dteValue->setTimeSpec(cfg.m_headerEditorDateTimeInUTC ? Qt::UTC : Qt::LocalTime);
39 m_dteValue->setDateTime(cfg.m_headerEditorDateTimeInUTC ? m_originalValueUTC : m_originalValueUTC.toLocalTime());
40 m_dteValue->setDisplayFormat(Q("yyyy-MM-dd hh:mm:ss"));
41
42 return m_dteValue;
43 }
44
45 QString
originalValueAsString() const46 TimeValuePage::originalValueAsString()
47 const {
48 auto &cfg = Util::Settings::get();
49
50 return Util::displayableDate(cfg.m_headerEditorDateTimeInUTC ? m_originalValueUTC : m_originalValueUTC.toLocalTime());
51 }
52
53 QString
currentValueAsString() const54 TimeValuePage::currentValueAsString()
55 const {
56 return Util::displayableDate(m_dteValue->dateTime());
57 }
58
59 void
resetValue()60 TimeValuePage::resetValue() {
61 auto &cfg = Util::Settings::get();
62
63 m_dteValue->setDateTime(cfg.m_headerEditorDateTimeInUTC ? m_originalValueUTC : m_originalValueUTC.toLocalTime());
64 }
65
66 bool
validateValue() const67 TimeValuePage::validateValue()
68 const {
69 return m_dteValue->dateTime().isValid();
70 }
71
72 void
copyValueToElement()73 TimeValuePage::copyValueToElement() {
74 static_cast<EbmlDate *>(m_element)->SetEpochDate(m_dteValue->dateTime().toUTC().toSecsSinceEpoch());
75 }
76
77 void
showInRequestedTimeSpec()78 TimeValuePage::showInRequestedTimeSpec() {
79 auto &cfg = Util::Settings::get();
80 auto current = m_dteValue->dateTime();
81
82 m_dteValue->setTimeSpec(cfg.m_headerEditorDateTimeInUTC ? Qt::UTC : Qt::LocalTime);
83 m_dteValue->setDateTime(cfg.m_headerEditorDateTimeInUTC ? current.toUTC() : current.toLocalTime());
84 }
85
86 QString
note() const87 TimeValuePage::note()
88 const {
89 auto &cfg = Util::Settings::get();
90 auto now = QDateTime::currentDateTime();
91
92 return cfg.m_headerEditorDateTimeInUTC || !now.offsetFromUtc() ? QY("The date & time shown is in UTC.") : QY("The date & time shown is in your local time zone.");
93 }
94
95 }
96