sc/inc/validat.hxx | 10 ++++ sc/source/core/data/validat.cxx | 81 ++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 27 deletions(-)
New commits: commit ebe4bbb25a9bcb4263b079a50d932384c7c7fb3e Author: Eike Rathke <er...@redhat.com> AuthorDate: Fri Sep 25 23:26:19 2020 +0200 Commit: Eike Rathke <er...@redhat.com> CommitDate: Sat Sep 26 11:30:21 2020 +0200 Consolidate, factor out common duplicated code, tdf#128797 follow-up Change-Id: I939c9f88a6cf09e1caa87131562ad67e389c2710 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103470 Reviewed-by: Eike Rathke <er...@redhat.com> Tested-by: Jenkins diff --git a/sc/inc/validat.hxx b/sc/inc/validat.hxx index 19f88ddce34a..7ab014f0f92f 100644 --- a/sc/inc/validat.hxx +++ b/sc/inc/validat.hxx @@ -34,6 +34,7 @@ struct RefUpdateContext; class ScPatternAttr; class ScTokenArray; class ScTypedStrData; +struct ScValidationDataIsNumeric; enum ScValidationMode { @@ -183,6 +184,15 @@ private: /** Tests, if contents of pCell occur in cell range referenced by own formula, or in a string list. */ bool IsListValid( ScRefCellValue& rCell, const ScAddress& rPos ) const; + + /** Tests, if string or numeric data has valid text length. + @param pDataNumeric + nullptr if string data to be tested, else for numeric data a + properly initialized ScValidationDataIsNumeric struct, see + implementation. + */ + bool IsDataValidTextLen( const OUString& rTest, const ScAddress& rPos, + ScValidationDataIsNumeric* pDataNumeric ) const; }; // list of conditions: diff --git a/sc/source/core/data/validat.cxx b/sc/source/core/data/validat.cxx index 2cb6a6aad909..7ed29dd577bc 100644 --- a/sc/source/core/data/validat.cxx +++ b/sc/source/core/data/validat.cxx @@ -476,6 +476,53 @@ bool ScValidationData::IsDataValidCustom( return bRet; } +/** To test numeric data text length in IsDataValidTextLen(). + + If mpFormatter is not set, it is obtained from the document and the format + key is determined from the cell position's attribute pattern. + */ +struct ScValidationDataIsNumeric +{ + SvNumberFormatter* mpFormatter; + double mfVal; + sal_uInt32 mnFormat; + + ScValidationDataIsNumeric( double fVal, SvNumberFormatter* pFormatter = nullptr, sal_uInt32 nFormat = 0 ) + : mpFormatter(pFormatter), mfVal(fVal), mnFormat(nFormat) + { + } + + void init( const ScDocument& rDoc, const ScAddress& rPos ) + { + const ScPatternAttr* pPattern = rDoc.GetPattern( rPos.Col(), rPos.Row(), rPos.Tab()); + mpFormatter = rDoc.GetFormatTable(); + mnFormat = pPattern->GetNumberFormat( mpFormatter); + } +}; + +bool ScValidationData::IsDataValidTextLen( const OUString& rTest, const ScAddress& rPos, + ScValidationDataIsNumeric* pDataNumeric ) const +{ + sal_Int32 nLen; + if (!pDataNumeric) + nLen = rTest.getLength(); + else + { + if (!pDataNumeric->mpFormatter) + pDataNumeric->init( *GetDocument(), rPos); + + // For numeric values use the resulting input line string to + // determine length, otherwise an once accepted value maybe could + // not be edited again, for example abbreviated dates or leading + // zeros or trailing zeros after decimal separator change length. + OUString aStr; + pDataNumeric->mpFormatter->GetInputLineString( pDataNumeric->mfVal, pDataNumeric->mnFormat, aStr); + nLen = aStr.getLength(); + } + ScRefCellValue aTmpCell( static_cast<double>(nLen)); + return IsCellValid( aTmpCell, rPos); +} + bool ScValidationData::IsDataValid( const OUString& rTest, const ScPatternAttr& rPattern, const ScAddress& rPos ) const { @@ -498,21 +545,13 @@ bool ScValidationData::IsDataValid( bool bRet; if (SC_VALID_TEXTLEN == eDataMode) { - double nLenVal; if (!bIsVal) - nLenVal = static_cast<double>(rTest.getLength()); + bRet = IsDataValidTextLen( rTest, rPos, nullptr); else { - // For numeric values use the resulting input line string to - // determine length, otherwise an once accepted value maybe could - // not be edited again, for example abbreviated dates or leading - // zeros or trailing zeros after decimal separator change length. - OUString aStr; - pFormatter->GetInputLineString( nVal, nFormat, aStr); - nLenVal = static_cast<double>( aStr.getLength() ); + ScValidationDataIsNumeric aDataNumeric( nVal, pFormatter, nFormat); + bRet = IsDataValidTextLen( rTest, rPos, &aDataNumeric); } - ScRefCellValue aTmpCell(nLenVal); - bRet = IsCellValid(aTmpCell, rPos); } else { @@ -589,25 +628,13 @@ bool ScValidationData::IsDataValid( ScRefCellValue& rCell, const ScAddress& rPos break; case SC_VALID_TEXTLEN: - { - double nLenVal; - bOk = !bIsVal; // only Text - if ( bOk ) - { - nLenVal = static_cast<double>(aString.getLength()); - } + if (!bIsVal) + bOk = IsDataValidTextLen( aString, rPos, nullptr); else { - const ScPatternAttr* pPattern - = mpDoc->GetPattern(rPos.Col(), rPos.Row(), rPos.Tab()); - SvNumberFormatter* pFormatter = GetDocument()->GetFormatTable(); - sal_uInt32 nFormat = pPattern->GetNumberFormat(pFormatter); - pFormatter->GetInputLineString(nVal, nFormat, aString); - nLenVal = static_cast<double>(aString.getLength()); + ScValidationDataIsNumeric aDataNumeric( nVal); + bOk = IsDataValidTextLen( aString, rPos, &aDataNumeric); } - ScRefCellValue aTmpCell(nLenVal); - bOk = IsCellValid(aTmpCell, rPos); - } break; default: _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits