diff options
author | Eike Rathke <erack@redhat.com> | 2020-09-25 23:26:19 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2020-09-26 11:30:21 +0200 |
commit | ebe4bbb25a9bcb4263b079a50d932384c7c7fb3e (patch) | |
tree | d9fa1ff01b56ece6f0b1f453dd55a2e0b3a6a718 /sc | |
parent | 4f963b6502674b73ecbc0ced201d15456724e977 (diff) |
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 <erack@redhat.com>
Tested-by: Jenkins
Diffstat (limited to 'sc')
-rw-r--r-- | sc/inc/validat.hxx | 10 | ||||
-rw-r--r-- | sc/source/core/data/validat.cxx | 81 |
2 files changed, 64 insertions, 27 deletions
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: |