From b2869ca51c2e30128456cbdee7e526bacd9cc905 Mon Sep 17 00:00:00 2001 From: Sarper Akdemir Date: Sun, 9 May 2021 19:46:21 +0300 Subject: tdf#59323: ooxml import: add OOXML to LO datetime helper Added static helper functions getLOTimeFormat and getLODateFormat to TextField class for mapping datetime field types to SvxDateFomat and SvxTimeFormat. Change-Id: I9c1553cc89d47855dc7af06a8ea995de01692ded Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117001 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- oox/inc/drawingml/textfield.hxx | 13 ++++ oox/source/drawingml/textfield.cxx | 141 +++++++++++++++++++++---------------- 2 files changed, 94 insertions(+), 60 deletions(-) (limited to 'oox') diff --git a/oox/inc/drawingml/textfield.hxx b/oox/inc/drawingml/textfield.hxx index f365238bd54b..c26a8518c509 100644 --- a/oox/inc/drawingml/textfield.hxx +++ b/oox/inc/drawingml/textfield.hxx @@ -23,6 +23,9 @@ #include #include +enum class SvxTimeFormat; +enum class SvxDateFormat; + namespace oox::drawingml { struct TextCharacterProperties; @@ -48,6 +51,16 @@ public: const TextCharacterProperties& rTextCharacterStyle, float nDefaultCharHeight) const override; + /** Gets the corresponding LO Date format for given OOXML datetime field type + * + * @param rDateTimeType PPTX datetime field type e.g. datetime3 + */ + static SvxDateFormat getLODateFormat( std::u16string_view rDateTimeType ); + /** Gets the corresponding LO Time format for given OOXML datetime field type + * + * @param rDateTimeType PPTX datetime field type e.g. datetime3 + */ + static SvxTimeFormat getLOTimeFormat( std::u16string_view rDateTimeType ); private: TextParagraphProperties maTextParagraphProperties; OUString msType; diff --git a/oox/source/drawingml/textfield.cxx b/oox/source/drawingml/textfield.cxx index 7425e0416fec..e590c5c7a2cb 100644 --- a/oox/source/drawingml/textfield.cxx +++ b/oox/source/drawingml/textfield.cxx @@ -32,6 +32,7 @@ #include #include #include +#include using namespace ::com::sun::star; using namespace ::com::sun::star::uno; @@ -71,68 +72,28 @@ void lclCreateTextFields( std::vector< Reference< XTextField > > & aFields, aFields.emplace_back( xIface, UNO_QUERY ); return; } - bool bIsDate = true; - int idx = p.toInt32(); - sal_uInt16 nNumFmt; - xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" ); - aFields.emplace_back( xIface, UNO_QUERY ); - Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW ); - - // here we should format the field properly. waiting after #i81091. - switch( idx ) + + SvxDateFormat eDateFormat = TextField::getLODateFormat(sType); + if (eDateFormat != SvxDateFormat::AppDefault) { - case 1: // Date dd/mm/yyyy - // this is the default format... - nNumFmt = 5; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - case 2: // Date Day, Month dd, yyyy - break; - case 3: // Date dd Month yyyy - nNumFmt = 3; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - case 4: // Date Month dd, yyyy - break; - case 5: // Date dd-Mon-yy - break; - case 6: // Date Month yy - break; - case 7: // Date Mon-yy - break; - case 8: // DateTime dd/mm/yyyy H:MM PM - lclCreateTextFields( aFields, xModel, "datetime12" ); - break; - case 9: // DateTime dd/mm/yy H:MM:SS PM - lclCreateTextFields( aFields, xModel, "datetime13" ); - break; - case 10: // Time H:MM - bIsDate = false; - nNumFmt = 3; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - case 11: // Time H:MM:SS - bIsDate = false; - // this is the default format - nNumFmt = 2; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - case 12: // Time H:MM PM - bIsDate = false; - nNumFmt = 6; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - case 13: // Time H:MM:SS PM - bIsDate = false; - nNumFmt = 7; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); - break; - default: - nNumFmt = 2; - xProps->setPropertyValue("NumberFormat", makeAny(nNumFmt)); + xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" ); + aFields.emplace_back( xIface, UNO_QUERY ); + Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW ); + xProps->setPropertyValue("NumberFormat", Any(static_cast(eDateFormat))); + xProps->setPropertyValue("IsDate", Any(true)); + xProps->setPropertyValue("IsFixed", Any(false)); + } + + SvxTimeFormat eTimeFormat = TextField::getLOTimeFormat(sType); + if (eTimeFormat != SvxTimeFormat::AppDefault) + { + xIface = xFactory->createInstance( "com.sun.star.text.TextField.DateTime" ); + aFields.emplace_back( xIface, UNO_QUERY ); + Reference< XPropertySet > xProps( xIface, UNO_QUERY_THROW ); + xProps->setPropertyValue("NumberFormat", Any(static_cast(eTimeFormat))); + xProps->setPropertyValue("IsDate", Any(false)); + xProps->setPropertyValue("IsFixed", Any(false)); } - xProps->setPropertyValue( "IsDate", makeAny( bIsDate ) ); - xProps->setPropertyValue( "IsFixed", makeAny( false ) ); } catch(const Exception &) { @@ -245,6 +206,66 @@ sal_Int32 TextField::insertAt( return nCharHeight; } +SvxDateFormat TextField::getLODateFormat(std::u16string_view rDateTimeType) +{ + OString aDateTimeNum = OUStringToOString(rDateTimeType.substr(8), RTL_TEXTENCODING_UTF8); + + if( aDateTimeNum.isEmpty() ) // "datetime" + return SvxDateFormat::StdSmall; + + int nDateTimeNum = aDateTimeNum.toInt32(); + + switch( nDateTimeNum ) + { + case 1: // Date dd/mm/yyyy + case 2: // Date Day, Month dd, yyyy + case 5: // Date dd-Mon-yy + case 6: // Date Month yy + case 7: // Date Mon-yy + case 8: // DateTime dd/mm/yyyy H:MM PM + case 9: // DateTime dd/mm/yyyy H:MM:SS PM + return SvxDateFormat::B; + case 3: // Date dd Month yyyy + return SvxDateFormat::StdBig; + case 4: // Date Month dd, yyyy + return SvxDateFormat::StdSmall; + case 10: // Time H:MM + case 11: // Time H:MM:SS + case 12: // Time H:MM PM + case 13: // Time H:MM:SS PM + default: + return SvxDateFormat::AppDefault; + } +} + +SvxTimeFormat TextField::getLOTimeFormat(std::u16string_view rDateTimeType) +{ + OString aDateTimeNum = OUStringToOString(rDateTimeType.substr(8), RTL_TEXTENCODING_UTF8); + int nDateTimeNum = aDateTimeNum.toInt32(); + + switch( nDateTimeNum ) + { + case 8: // DateTime dd/mm/yyyy H:MM PM + case 12: // Time H:MM PM + return SvxTimeFormat::HH12_MM; + case 9: // DateTime dd/mm/yyyy H:MM:SS PM + case 13: // Time H:MM:SS PM + return SvxTimeFormat::HH12_MM_SS; + case 10: // Time H:MM + return SvxTimeFormat::HH24_MM; + case 11: // Time H:MM:SS + return SvxTimeFormat::Standard; + case 1: // Date dd/mm/yyyy + case 2: // Date Day, Month dd, yyyy + case 3: // Date dd Month yyyy + case 4: // Date Month dd, yyyy + case 5: // Date dd-Mon-yy + case 6: // Date Month yy + case 7: // Date Mon-yy + default: + return SvxTimeFormat::AppDefault; + } +} } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit