From 0abbf64dc1ebe9f12523a69ce1cfd25fe189d869 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Mon, 13 Jul 2020 15:14:21 +0100 Subject: weld TimeControl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Idb19639eb39bc83fb8bdc7c2181bf63e9c522d18 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/98670 Tested-by: Caolán McNamara Reviewed-by: Caolán McNamara --- vcl/source/app/weldutils.cxx | 90 +++++++++++++++++++++++++++++++++++++++++++ vcl/source/control/field2.cxx | 81 ++++++++++++++++++++++++++++++++++++++ vcl/source/window/builder.cxx | 11 ++---- 3 files changed, 175 insertions(+), 7 deletions(-) (limited to 'vcl') diff --git a/vcl/source/app/weldutils.cxx b/vcl/source/app/weldutils.cxx index acf1ba3473b5..21161e396db8 100644 --- a/vcl/source/app/weldutils.cxx +++ b/vcl/source/app/weldutils.cxx @@ -294,12 +294,19 @@ void DoubleNumericFormatter::ResetConformanceTester() LongCurrencyFormatter::LongCurrencyFormatter(weld::Entry& rEntry) : EntryFormatter(rEntry) + , m_bThousandSep(true) { + Init(); } LongCurrencyFormatter::LongCurrencyFormatter(weld::FormattedSpinButton& rSpinButton) : EntryFormatter(rSpinButton) , m_bThousandSep(true) +{ + Init(); +} + +void LongCurrencyFormatter::Init() { SetOutputHdl(LINK(this, LongCurrencyFormatter, FormatOutputHdl)); SetInputHdl(LINK(this, LongCurrencyFormatter, ParseInputHdl)); @@ -318,6 +325,89 @@ void LongCurrencyFormatter::SetCurrencySymbol(const OUString& rStr) } LongCurrencyFormatter::~LongCurrencyFormatter() = default; + +TimeFormatter::TimeFormatter(weld::Entry& rEntry) + : EntryFormatter(rEntry) + , m_eFormat(TimeFieldFormat::F_NONE) + , m_eTimeFormat(TimeFormat::Hour24) + , m_bDuration(false) +{ + Init(); +} + +TimeFormatter::TimeFormatter(weld::FormattedSpinButton& rSpinButton) + : EntryFormatter(rSpinButton) + , m_eFormat(TimeFieldFormat::F_NONE) + , m_eTimeFormat(TimeFormat::Hour24) + , m_bDuration(false) +{ + Init(); +} + +void TimeFormatter::Init() +{ + SetOutputHdl(LINK(this, TimeFormatter, FormatOutputHdl)); + SetInputHdl(LINK(this, TimeFormatter, ParseInputHdl)); + + SetMin(tools::Time(0, 0)); + SetMax(tools::Time(23, 59, 59, 999999999)); + + // so the spin size can depend on which zone the cursor is in + get_widget().connect_cursor_position(LINK(this, TimeFormatter, CursorChangedHdl)); + // and set the initial spin size + CursorChangedHdl(get_widget()); +} + +void TimeFormatter::SetExtFormat(ExtTimeFieldFormat eFormat) +{ + switch (eFormat) + { + case ExtTimeFieldFormat::Short24H: + { + m_eTimeFormat = TimeFormat::Hour24; + m_bDuration = false; + m_eFormat = TimeFieldFormat::F_NONE; + } + break; + case ExtTimeFieldFormat::Long24H: + { + m_eTimeFormat = TimeFormat::Hour24; + m_bDuration = false; + m_eFormat = TimeFieldFormat::F_SEC; + } + break; + case ExtTimeFieldFormat::Short12H: + { + m_eTimeFormat = TimeFormat::Hour12; + m_bDuration = false; + m_eFormat = TimeFieldFormat::F_NONE; + } + break; + case ExtTimeFieldFormat::Long12H: + { + m_eTimeFormat = TimeFormat::Hour12; + m_bDuration = false; + m_eFormat = TimeFieldFormat::F_SEC; + } + break; + case ExtTimeFieldFormat::ShortDuration: + { + m_bDuration = true; + m_eFormat = TimeFieldFormat::F_NONE; + } + break; + case ExtTimeFieldFormat::LongDuration: + { + m_bDuration = true; + m_eFormat = TimeFieldFormat::F_SEC; + } + break; + } + + ReFormat(); +} + +TimeFormatter::~TimeFormatter() = default; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/control/field2.cxx b/vcl/source/control/field2.cxx index 6b69d19c9ff1..2388daa6bfb7 100644 --- a/vcl/source/control/field2.cxx +++ b/vcl/source/control/field2.cxx @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -2705,4 +2706,84 @@ void TimeBox::ReformatAll() SetUpdateMode( true ); } +namespace weld +{ + tools::Time TimeFormatter::ConvertValue(int nValue) + { + tools::Time aTime(0); + aTime.MakeTimeFromMS(nValue); + return aTime; + } + + int TimeFormatter::ConvertValue(const tools::Time& rTime) + { + return rTime.GetMSFromTime(); + } + + void TimeFormatter::SetTime(const tools::Time& rTime) + { + SetValue(ConvertValue(rTime)); + } + + tools::Time TimeFormatter::GetTime() + { + return ConvertValue(GetValue()); + } + + void TimeFormatter::SetMin(const tools::Time& rNewMin) + { + SetMinValue(ConvertValue(rNewMin)); + } + + void TimeFormatter::SetMax(const tools::Time& rNewMax) + { + SetMaxValue(ConvertValue(rNewMax)); + } + + OUString TimeFormatter::FormatNumber(int nValue) const + { + const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); + return ::TimeFormatter::FormatTime(ConvertValue(nValue), m_eFormat, m_eTimeFormat, m_bDuration, rLocaleData); + } + + IMPL_LINK_NOARG(TimeFormatter, FormatOutputHdl, LinkParamNone*, bool) + { + OUString sText = FormatNumber(GetValue()); + ImplSetTextImpl(sText, nullptr); + return true; + } + + IMPL_LINK(TimeFormatter, ParseInputHdl, sal_Int64*, result, TriState) + { + const LocaleDataWrapper& rLocaleDataWrapper = Application::GetSettings().GetLocaleDataWrapper(); + + tools::Time aResult(0); + bool bRet = ::TimeFormatter::TextToTime(GetEntryText(), aResult, m_eFormat, m_bDuration, rLocaleDataWrapper); + if (bRet) + *result = ConvertValue(aResult); + + return bRet ? TRISTATE_TRUE : TRISTATE_FALSE; + } + + IMPL_LINK(TimeFormatter, CursorChangedHdl, weld::Entry&, rEntry, void) + { + int nStartPos, nEndPos; + rEntry.get_selection_bounds(nStartPos, nEndPos); + + const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); + const int nTimeArea = ::TimeFormatter::GetTimeArea(m_eFormat, GetEntryText(), nEndPos, rLocaleData); + + int nIncrements = 1; + + if (nTimeArea == 1) + nIncrements = 1000 * 60 * 60; + else if (nTimeArea == 2) + nIncrements = 1000 * 60; + else if (nTimeArea == 3) + nIncrements = 1000; + + SetSpinSize(nIncrements); + } +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/window/builder.cxx b/vcl/source/window/builder.cxx index 5738891c6c7f..368c4292fd06 100644 --- a/vcl/source/window/builder.cxx +++ b/vcl/source/window/builder.cxx @@ -360,8 +360,8 @@ namespace weld m_xSpinButton->get_selection_bounds(nStartPos, nEndPos); const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); - const int nTimeArea = TimeFormatter::GetTimeArea(m_eFormat, m_xSpinButton->get_text(), nEndPos, - rLocaleData); + const int nTimeArea = ::TimeFormatter::GetTimeArea(m_eFormat, m_xSpinButton->get_text(), nEndPos, + rLocaleData); int nIncrements = 1; @@ -390,12 +390,9 @@ namespace weld IMPL_LINK(TimeSpinButton, spin_button_input, int*, result, bool) { - int nStartPos, nEndPos; - m_xSpinButton->get_selection_bounds(nStartPos, nEndPos); - const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); tools::Time aResult(0); - bool bRet = TimeFormatter::TextToTime(m_xSpinButton->get_text(), aResult, m_eFormat, true, rLocaleData); + bool bRet = ::TimeFormatter::TextToTime(m_xSpinButton->get_text(), aResult, m_eFormat, true, rLocaleData); if (bRet) *result = ConvertValue(aResult); return bRet; @@ -426,7 +423,7 @@ namespace weld OUString TimeSpinButton::format_number(int nValue) const { const LocaleDataWrapper& rLocaleData = Application::GetSettings().GetLocaleDataWrapper(); - return TimeFormatter::FormatTime(ConvertValue(nValue), m_eFormat, TimeFormat::Hour24, true, rLocaleData); + return ::TimeFormatter::FormatTime(ConvertValue(nValue), m_eFormat, TimeFormat::Hour24, true, rLocaleData); } EntryTreeView::EntryTreeView(std::unique_ptr xEntry, std::unique_ptr xTreeView) -- cgit