diff options
author | Norbert Thiebaud <nthiebaud@gmail.com> | 2012-08-20 20:42:37 -0500 |
---|---|---|
committer | Miklos Vajna <vmiklos@suse.cz> | 2012-09-14 06:59:57 +0000 |
commit | a7e949673cda3f0e2abfa49b2c22735f33205d01 (patch) | |
tree | 127ef2b9506d7ea8ba5d9741f88aa248b6749d3e /svtools/source | |
parent | 7bfbc9523fd2a6c907cb3ad0b3407ae60c66d358 (diff) |
gridfixes: #i117265# use a NumberFormatter for converting cell values to text.
This means using the Locale setup in the Options, and allows for more value types (like Date/Time) to be supported
Change-Id: I78fb08f2da88405851664e3e1dd4505c3727e56b
Reviewed-on: https://gerrit.libreoffice.org/534
Reviewed-by: Miklos Vajna <vmiklos@suse.cz>
Tested-by: Miklos Vajna <vmiklos@suse.cz>
Diffstat (limited to 'svtools/source')
-rw-r--r-- | svtools/source/table/cellvalueconversion.cxx | 455 | ||||
-rw-r--r-- | svtools/source/table/cellvalueconversion.hxx | 16 | ||||
-rw-r--r-- | svtools/source/table/gridtablerenderer.cxx | 23 | ||||
-rw-r--r-- | svtools/source/table/tablecontrol_impl.cxx | 11 | ||||
-rw-r--r-- | svtools/source/table/tabledatawindow.cxx | 3 |
5 files changed, 477 insertions, 31 deletions
diff --git a/svtools/source/table/cellvalueconversion.cxx b/svtools/source/table/cellvalueconversion.cxx index afb27ae3778a..27c9ed33d77d 100644 --- a/svtools/source/table/cellvalueconversion.cxx +++ b/svtools/source/table/cellvalueconversion.cxx @@ -27,6 +27,25 @@ #include "cellvalueconversion.hxx" +#include <com/sun/star/util/XNumberFormatter.hpp> +#include <com/sun/star/util/XNumberFormatsSupplier.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/util/Date.hpp> +#include <com/sun/star/util/Time.hpp> +#include <com/sun/star/util/DateTime.hpp> +#include <com/sun/star/util/XNumberFormatTypes.hpp> +#include <com/sun/star/util/NumberFormat.hpp> +#include <comphelper/componentcontext.hxx> +#include <rtl/strbuf.hxx> +#include <rtl/math.hxx> +#include <tools/date.hxx> +#include <tools/time.hxx> +#include <tools/diagnose_ex.h> +#include <unotools/syslocale.hxx> + +#include <boost/shared_ptr.hpp> +#include <boost/unordered_map.hpp> + //...................................................................................................................... namespace svt { @@ -34,36 +53,432 @@ namespace svt /** === begin UNO using === **/ using ::com::sun::star::uno::Any; + using ::com::sun::star::util::XNumberFormatter; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::util::XNumberFormatsSupplier; + using ::com::sun::star::beans::XPropertySet; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::util::DateTime; + using ::com::sun::star::uno::TypeClass; + using ::com::sun::star::util::XNumberFormatTypes; + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Type; + using ::com::sun::star::uno::TypeClass_BYTE; + using ::com::sun::star::uno::TypeClass_SHORT; + using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT; + using ::com::sun::star::uno::TypeClass_LONG; + using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG; + using ::com::sun::star::uno::TypeClass_HYPER; /** === end UNO using === **/ + namespace NumberFormat = ::com::sun::star::util::NumberFormat; + + typedef ::com::sun::star::util::Time UnoTime; + typedef ::com::sun::star::util::Date UnoDate; //================================================================================================================== - //= CellValueConversion + //= helper //================================================================================================================== - //------------------------------------------------------------------------------------------------------------------ - ::rtl::OUString CellValueConversion::convertToString( const Any& i_value ) + namespace { - ::rtl::OUString sConvertString; - if ( !i_value.hasValue() ) - return sConvertString; + //-------------------------------------------------------------------------------------------------------------- + double lcl_convertDateToDays( long const i_day, long const i_month, long const i_year ) + { + long const nNullDateDays = ::Date::DateToDays( 1, 1, 1900 ); + long const nValueDateDays = ::Date::DateToDays( i_day, i_month, i_year ); + + return nValueDateDays - nNullDateDays; + } + + //-------------------------------------------------------------------------------------------------------------- + double lcl_convertTimeToDays( long const i_hours, long const i_minutes, long const i_seconds, long const i_100thSeconds ) + { + return Time( i_hours, i_minutes, i_seconds, i_100thSeconds ).GetTimeInDays(); + } + } + + //================================================================================================================== + //= IValueNormalization + //================================================================================================================== + class SAL_NO_VTABLE IValueNormalization + { + public: + virtual ~IValueNormalization() { } + + /** converts the given <code>Any</code> into a <code>double</code> value to be fed into a number formatter + */ + virtual double convertToDouble( Any const & i_value ) const = 0; + + /** returns the format key to be used for formatting values + */ + virtual ::sal_Int32 getFormatKey() const = 0; + }; + + typedef ::boost::shared_ptr< IValueNormalization > PValueNormalization; + typedef ::boost::unordered_map< ::rtl::OUString, PValueNormalization, ::rtl::OUStringHash > NormalizerCache; + + //================================================================================================================== + //= CellValueConversion_Data + //================================================================================================================== + struct CellValueConversion_Data + { + ::comphelper::ComponentContext const aContext; + Reference< XNumberFormatter > xNumberFormatter; + bool bAttemptedFormatterCreation; + NormalizerCache aNormalizers; + + CellValueConversion_Data( ::comphelper::ComponentContext const & i_context ) + :aContext( i_context ) + ,xNumberFormatter() + ,bAttemptedFormatterCreation( false ) + ,aNormalizers() + { + } + }; + + //================================================================================================================== + //= StandardFormatNormalizer + //================================================================================================================== + class StandardFormatNormalizer : public IValueNormalization + { + protected: + StandardFormatNormalizer( Reference< XNumberFormatter > const & i_formatter, ::sal_Int32 const i_numberFormatType ) + :m_nFormatKey( 0 ) + { + try + { + ENSURE_OR_THROW( i_formatter.is(), "StandardFormatNormalizer: no formatter!" ); + Reference< XNumberFormatsSupplier > const xSupplier( i_formatter->getNumberFormatsSupplier(), UNO_SET_THROW ); + Reference< XNumberFormatTypes > const xTypes( xSupplier->getNumberFormats(), UNO_QUERY_THROW ); + m_nFormatKey = xTypes->getStandardFormat( i_numberFormatType, SvtSysLocale().GetLocale() ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + + virtual ::sal_Int32 getFormatKey() const + { + return m_nFormatKey; + } + + private: + ::sal_Int32 m_nFormatKey; + }; + + //================================================================================================================== + //= DoubleNormalization + //================================================================================================================== + class DoubleNormalization : public StandardFormatNormalizer + { + public: + DoubleNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER ) + { + } + + virtual double convertToDouble( Any const & i_value ) const + { + double returnValue(0); + ::rtl::math::setNan( &returnValue ); + OSL_VERIFY( i_value >>= returnValue ); + return returnValue; + } + + virtual ~DoubleNormalization() { } + }; + + //================================================================================================================== + //= IntegerNormalization + //================================================================================================================== + class IntegerNormalization : public StandardFormatNormalizer + { + public: + IntegerNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::NUMBER ) + { + } + + virtual ~IntegerNormalization() {} + + virtual double convertToDouble( Any const & i_value ) const + { + sal_Int64 value( 0 ); + OSL_VERIFY( i_value >>= value ); + return value; + } + }; + + //================================================================================================================== + //= BooleanNormalization + //================================================================================================================== + class BooleanNormalization : public StandardFormatNormalizer + { + public: + BooleanNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::LOGICAL ) + { + } + + virtual ~BooleanNormalization() {} + + virtual double convertToDouble( Any const & i_value ) const + { + bool value( false ); + OSL_VERIFY( i_value >>= value ); + return value ? 1 : 0; + } + }; + + //================================================================================================================== + //= DateTimeNormalization + //================================================================================================================== + class DateTimeNormalization : public StandardFormatNormalizer + { + public: + DateTimeNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::DATETIME ) + { + } + + virtual ~DateTimeNormalization() {} + + virtual double convertToDouble( Any const & i_value ) const + { + double returnValue(0); + ::rtl::math::setNan( &returnValue ); + // extract actual UNO value + DateTime aDateTimeValue; + ENSURE_OR_RETURN( i_value >>= aDateTimeValue, "allowed for DateTime values only", returnValue ); - // TODO: use css.script.XTypeConverter? + // date part + returnValue = lcl_convertDateToDays( aDateTimeValue.Day, aDateTimeValue.Month, aDateTimeValue.Year ); - sal_Int32 nInt = 0; - sal_Bool bBool = false; - double fDouble = 0; + // time part + returnValue += lcl_convertTimeToDays( + aDateTimeValue.Hours, aDateTimeValue.Minutes, aDateTimeValue.Seconds, aDateTimeValue.HundredthSeconds ); + // done + return returnValue; + } + }; + + //================================================================================================================== + //= DateNormalization + //================================================================================================================== + class DateNormalization : public StandardFormatNormalizer + { + public: + DateNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::DATE ) + { + } + + virtual ~DateNormalization() {} + + virtual double convertToDouble( Any const & i_value ) const + { + double returnValue(0); + ::rtl::math::setNan( &returnValue ); + + // extract + UnoDate aDateValue; + ENSURE_OR_RETURN( i_value >>= aDateValue, "allowed for Date values only", returnValue ); + + // convert + returnValue = lcl_convertDateToDays( aDateValue.Day, aDateValue.Month, aDateValue.Year ); + + // done + return returnValue; + } + }; + + //================================================================================================================== + //= TimeNormalization + //================================================================================================================== + class TimeNormalization : public StandardFormatNormalizer + { + public: + TimeNormalization( Reference< XNumberFormatter > const & i_formatter ) + :StandardFormatNormalizer( i_formatter, NumberFormat::TIME ) + { + } + + virtual ~TimeNormalization() {} + + virtual double convertToDouble( Any const & i_value ) const + { + double returnValue(0); + ::rtl::math::setNan( &returnValue ); + + // extract + UnoTime aTimeValue; + ENSURE_OR_RETURN( i_value >>= aTimeValue, "allowed for Time values only", returnValue ); + + // convert + returnValue += lcl_convertTimeToDays( + aTimeValue.Hours, aTimeValue.Minutes, aTimeValue.Seconds, aTimeValue.HundredthSeconds ); + + // done + return returnValue; + } + }; + + //================================================================================================================== + //= operations + //================================================================================================================== + namespace + { + //-------------------------------------------------------------------------------------------------------------- + bool lcl_ensureNumberFormatter( CellValueConversion_Data & io_data ) + { + if ( io_data.bAttemptedFormatterCreation ) + return io_data.xNumberFormatter.is(); + io_data.bAttemptedFormatterCreation = true; + + try + { + // a number formatter + Reference< XNumberFormatter > const xFormatter( + io_data.aContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW ); + + // a supplier of number formats + Sequence< Any > aInitArgs(1); + aInitArgs[0] <<= SvtSysLocale().GetLocale(); + + Reference< XNumberFormatsSupplier > const xSupplier( + io_data.aContext.createComponentWithArguments( "com.sun.star.util.NumberFormatsSupplier", aInitArgs ), + UNO_QUERY_THROW + ); + + // ensure a NullDate we will assume later on + UnoDate const aNullDate( 1, 1, 1900 ); + Reference< XPropertySet > const xFormatSettings( xSupplier->getNumberFormatSettings(), UNO_SET_THROW ); + xFormatSettings->setPropertyValue( ::rtl::OUString::createFromAscii( "NullDate" ), makeAny( aNullDate ) ); + + // knit + xFormatter->attachNumberFormatsSupplier( xSupplier ); + + // done + io_data.xNumberFormatter = xFormatter; + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + + return io_data.xNumberFormatter.is(); + } + + //-------------------------------------------------------------------------------------------------------------- + bool lcl_getValueNormalizer( CellValueConversion_Data & io_data, Type const & i_valueType, + PValueNormalization & o_formatter ) + { + NormalizerCache::const_iterator pos = io_data.aNormalizers.find( i_valueType.getTypeName() ); + if ( pos == io_data.aNormalizers.end() ) + { + // never encountered this type before + o_formatter.reset(); + + ::rtl::OUString const sTypeName( i_valueType.getTypeName() ); + TypeClass const eTypeClass = i_valueType.getTypeClass(); + + if ( sTypeName.equals( ::cppu::UnoType< DateTime >::get().getTypeName() ) ) + { + o_formatter.reset( new DateTimeNormalization( io_data.xNumberFormatter ) ); + } + else if ( sTypeName.equals( ::cppu::UnoType< UnoDate >::get().getTypeName() ) ) + { + o_formatter.reset( new DateNormalization( io_data.xNumberFormatter ) ); + } + else if ( sTypeName.equals( ::cppu::UnoType< UnoTime >::get().getTypeName() ) ) + { + o_formatter.reset( new TimeNormalization( io_data.xNumberFormatter ) ); + } + else if ( sTypeName.equals( ::cppu::UnoType< ::sal_Bool >::get().getTypeName() ) ) + { + o_formatter.reset( new BooleanNormalization( io_data.xNumberFormatter ) ); + } + else if ( sTypeName.equals( ::cppu::UnoType< double >::get().getTypeName() ) + || sTypeName.equals( ::cppu::UnoType< float >::get().getTypeName() ) + ) + { + o_formatter.reset( new DoubleNormalization( io_data.xNumberFormatter ) ); + } + else if ( ( eTypeClass == TypeClass_BYTE ) + || ( eTypeClass == TypeClass_SHORT ) + || ( eTypeClass == TypeClass_UNSIGNED_SHORT ) + || ( eTypeClass == TypeClass_LONG ) + || ( eTypeClass == TypeClass_UNSIGNED_LONG ) + || ( eTypeClass == TypeClass_HYPER ) + ) + { + o_formatter.reset( new IntegerNormalization( io_data.xNumberFormatter ) ); + } + else + { +#if OSL_DEBUG_LEVEL > 0 + ::rtl::OStringBuffer message( "lcl_getValueNormalizer: unsupported type '" ); + message.append( ::rtl::OUStringToOString( sTypeName, RTL_TEXTENCODING_ASCII_US ) ); + message.append( "'!" ); + OSL_ENSURE( false, message.makeStringAndClear() ); +#endif + } + io_data.aNormalizers[ sTypeName ] = o_formatter; + } + else + o_formatter = pos->second; + + return !!o_formatter; + } + } + + //================================================================================================================== + //= CellValueConversion + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + CellValueConversion::CellValueConversion( ::comphelper::ComponentContext const & i_context ) + :m_pData( new CellValueConversion_Data( i_context ) ) + { + } + + //------------------------------------------------------------------------------------------------------------------ + CellValueConversion::~CellValueConversion() + { + } + + //------------------------------------------------------------------------------------------------------------------ + ::rtl::OUString CellValueConversion::convertToString( const Any& i_value ) + { ::rtl::OUString sStringValue; - if ( i_value >>= sConvertString ) - sStringValue = sConvertString; - else if ( i_value >>= nInt ) - sStringValue = sConvertString.valueOf( nInt ); - else if ( i_value >>= bBool ) - sStringValue = sConvertString.valueOf( bBool ); - else if ( i_value >>= fDouble ) - sStringValue = sConvertString.valueOf( fDouble ); - else - OSL_ENSURE( !i_value.hasValue(), "CellValueConversion::convertToString: cannot handle the given cell content type!" ); + if ( !i_value.hasValue() ) + return sStringValue; + + if ( ! ( i_value >>= sStringValue ) ) + { + if ( lcl_ensureNumberFormatter( *m_pData ) ) + { + PValueNormalization pNormalizer; + if ( lcl_getValueNormalizer( *m_pData, i_value.getValueType(), pNormalizer ) ) + { + try + { + double const formatterCompliantValue = pNormalizer->convertToDouble( i_value ); + sal_Int32 const formatKey = pNormalizer->getFormatKey(); + sStringValue = m_pData->xNumberFormatter->convertNumberToString( + formatKey, formatterCompliantValue ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + } + } return sStringValue; } diff --git a/svtools/source/table/cellvalueconversion.hxx b/svtools/source/table/cellvalueconversion.hxx index accdf42a45c9..0ab3c4a2aa92 100644 --- a/svtools/source/table/cellvalueconversion.hxx +++ b/svtools/source/table/cellvalueconversion.hxx @@ -30,6 +30,13 @@ #include <com/sun/star/uno/Any.hxx> +#include <boost/scoped_ptr.hpp> + +namespace comphelper +{ + class ComponentContext; +} + //...................................................................................................................... namespace svt { @@ -38,10 +45,17 @@ namespace svt //================================================================================================================== //= CellValueConversion //================================================================================================================== + struct CellValueConversion_Data; class CellValueConversion { public: - static ::rtl::OUString convertToString( const ::com::sun::star::uno::Any& i_cellValue ); + CellValueConversion( ::comphelper::ComponentContext const & i_context ); + ~CellValueConversion(); + + ::rtl::OUString convertToString( const ::com::sun::star::uno::Any& i_cellValue ); + + private: + ::boost::scoped_ptr< CellValueConversion_Data > m_pData; }; //...................................................................................................................... diff --git a/svtools/source/table/gridtablerenderer.cxx b/svtools/source/table/gridtablerenderer.cxx index f4a1f1d9dcf3..1032f5049a57 100644 --- a/svtools/source/table/gridtablerenderer.cxx +++ b/svtools/source/table/gridtablerenderer.cxx @@ -32,6 +32,8 @@ #include <com/sun/star/graphic/XGraphic.hpp> +#include <comphelper/componentcontext.hxx> +#include <comphelper/processfactory.hxx> #include <tools/debug.hxx> #include <tools/diagnose_ex.h> #include <vcl/window.hxx> @@ -120,11 +122,14 @@ namespace svt { namespace table RowPos nCurrentRow; bool bUseGridLines; CachedSortIndicator aSortIndicator; + CellValueConversion aStringConverter; GridTableRenderer_Impl( ITableModel& _rModel ) :rModel( _rModel ) ,nCurrentRow( ROW_INVALID ) ,bUseGridLines( true ) + ,aSortIndicator( ) + ,aStringConverter( ::comphelper::ComponentContext( ::comphelper::getProcessServiceFactory() ) ) { } }; @@ -392,7 +397,7 @@ namespace svt { namespace table _rDevice.DrawLine( _rArea.BottomLeft(), _rArea.BottomRight() ); Any const rowHeading( m_pImpl->rModel.getRowHeading( m_pImpl->nCurrentRow ) ); - ::rtl::OUString const rowTitle( CellValueConversion::convertToString( rowHeading ) ); + ::rtl::OUString const rowTitle( m_pImpl->aStringConverter.convertToString( rowHeading ) ); if ( !rowTitle.isEmpty() ) { ::Color const textColor = lcl_getEffectiveColor( m_pImpl->rModel.getHeaderTextColor(), _rStyle, &StyleSettings::GetFieldTextColor ); @@ -528,7 +533,7 @@ namespace svt { namespace table return; } - const ::rtl::OUString sText( CellValueConversion::convertToString( aCellContent ) ); + const ::rtl::OUString sText( m_pImpl->aStringConverter.convertToString( aCellContent ) ); impl_paintCellText( i_context, sText ); } @@ -563,7 +568,7 @@ namespace svt { namespace table //------------------------------------------------------------------------------------------------------------------ bool GridTableRenderer::FitsIntoCell( Any const & i_cellContent, ColPos const i_colPos, RowPos const i_rowPos, - bool const i_active, bool const i_selected, OutputDevice& i_targetDevice, Rectangle const & i_targetArea ) + bool const i_active, bool const i_selected, OutputDevice& i_targetDevice, Rectangle const & i_targetArea ) const { if ( !i_cellContent.hasValue() ) return true; @@ -583,7 +588,7 @@ namespace svt { namespace table return true; } - ::rtl::OUString const sText( CellValueConversion::convertToString( i_cellContent ) ); + ::rtl::OUString const sText( m_pImpl->aStringConverter.convertToString( i_cellContent ) ); if ( sText.isEmpty() ) return true; @@ -604,6 +609,16 @@ namespace svt { namespace table return true; } + //------------------------------------------------------------------------------------------------------------------ + bool GridTableRenderer::GetFormattedCellString( Any const & i_cellValue, ColPos const i_colPos, RowPos const i_rowPos, ::rtl::OUString & o_cellString ) const + { + o_cellString = m_pImpl->aStringConverter.convertToString( i_cellValue ); + + OSL_UNUSED( i_colPos ); + OSL_UNUSED( i_rowPos ); + return true; + } + //...................................................................................................................... } } // namespace svt::table //...................................................................................................................... diff --git a/svtools/source/table/tablecontrol_impl.cxx b/svtools/source/table/tablecontrol_impl.cxx index 127785990301..83ae4a18e732 100644 --- a/svtools/source/table/tablecontrol_impl.cxx +++ b/svtools/source/table/tablecontrol_impl.cxx @@ -33,7 +33,6 @@ #include "tabledatawindow.hxx" #include "tablecontrol_impl.hxx" #include "tablegeometry.hxx" -#include "cellvalueconversion.hxx" #include <com/sun/star/accessibility/XAccessible.hpp> #include <com/sun/star/accessibility/AccessibleTableModelChange.hpp> @@ -2200,9 +2199,13 @@ namespace svt { namespace table //-------------------------------------------------------------------- ::rtl::OUString TableControl_Impl::getCellContentAsString( RowPos const i_row, ColPos const i_col ) { - ::com::sun::star::uno::Any content; - m_pModel->getCellContent( i_col, i_row, content ); - return CellValueConversion::convertToString( content ); + Any aCellValue; + m_pModel->getCellContent( i_col, i_row, aCellValue ); + + ::rtl::OUString sCellStringContent; + m_pModel->getRenderer()->GetFormattedCellString( aCellValue, i_col, i_row, sCellStringContent ); + + return sCellStringContent; } //-------------------------------------------------------------------- diff --git a/svtools/source/table/tabledatawindow.cxx b/svtools/source/table/tabledatawindow.cxx index 1dedd3657333..cc84f5315a74 100644 --- a/svtools/source/table/tabledatawindow.cxx +++ b/svtools/source/table/tabledatawindow.cxx @@ -31,7 +31,6 @@ #include "tabledatawindow.hxx" #include "tablecontrol_impl.hxx" #include "tablegeometry.hxx" -#include "cellvalueconversion.hxx" #include <vcl/help.hxx> @@ -134,7 +133,7 @@ namespace svt { namespace table aCellToolTip.clear(); } - sHelpText = CellValueConversion::convertToString( aCellToolTip ); + pTableModel->getRenderer()->GetFormattedCellString( aCellToolTip, hitCol, hitRow, sHelpText ); if ( sHelpText.indexOf( '\n' ) >= 0 ) nHelpStyle = QUICKHELP_TIP_STYLE_BALLOON; |