lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / reportdesign / source / core / sdr / formatnormalizer.cxx
blobb1c132bd9ed5cc9e9fb4883530384f38b02590ad
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "formatnormalizer.hxx"
21 #include <RptModel.hxx>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdb/XParametersSupplier.hpp>
25 #include <com/sun/star/util/XNumberFormatTypes.hpp>
27 #include <dbaccess/dbsubcomponentcontroller.hxx>
28 #include <unotools/syslocale.hxx>
29 #include <connectivity/statementcomposer.hxx>
30 #include <connectivity/dbtools.hxx>
31 #include <tools/diagnose_ex.h>
34 namespace rptui
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::report::XFormattedField;
40 using ::com::sun::star::uno::UNO_QUERY;
41 using ::com::sun::star::sdb::XSingleSelectQueryComposer;
42 using ::com::sun::star::sdbcx::XColumnsSupplier;
43 using ::com::sun::star::container::XIndexAccess;
44 using ::com::sun::star::beans::XPropertySet;
45 using ::com::sun::star::uno::UNO_QUERY_THROW;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::sdb::XParametersSupplier;
48 using ::com::sun::star::sdbc::SQLException;
49 using ::com::sun::star::util::XNumberFormatsSupplier;
50 using ::com::sun::star::util::XNumberFormatTypes;
53 //= FormatNormalizer
56 FormatNormalizer::FormatNormalizer( const OReportModel& _rModel )
57 :m_rModel( _rModel )
58 ,m_xReportDefinition( )
59 ,m_bFieldListDirty( true )
64 FormatNormalizer::~FormatNormalizer()
69 void FormatNormalizer::notifyPropertyChange( const css::beans::PropertyChangeEvent& _rEvent )
71 if ( !impl_lateInit() )
72 return;
74 if ( ( _rEvent.Source == m_xReportDefinition ) && m_xReportDefinition.is() )
76 impl_onDefinitionPropertyChange( _rEvent.PropertyName );
77 return;
80 Reference< XFormattedField > xFormatted( _rEvent.Source, UNO_QUERY );
81 if ( xFormatted.is() )
82 impl_onFormattedProperttyChange( xFormatted, _rEvent.PropertyName );
86 void FormatNormalizer::notifyElementInserted( const css::uno::Reference< css::uno::XInterface >& _rxElement )
88 if ( !impl_lateInit() )
89 return;
91 Reference< XFormattedField > xFormatted( _rxElement, UNO_QUERY );
92 if ( !xFormatted.is() )
93 return;
95 impl_adjustFormatToDataFieldType_nothrow( xFormatted );
99 bool FormatNormalizer::impl_lateInit()
101 if ( m_xReportDefinition.is() )
102 return true;
104 m_xReportDefinition = m_rModel.getReportDefinition();
105 return m_xReportDefinition.is();
109 void FormatNormalizer::impl_onDefinitionPropertyChange( const OUString& _rChangedPropName )
111 if ( _rChangedPropName != "Command" && _rChangedPropName != "CommandType" && _rChangedPropName != "EscapeProcessing" )
112 // nothing we're interested in
113 return;
114 m_bFieldListDirty = true;
118 void FormatNormalizer::impl_onFormattedProperttyChange( const Reference< XFormattedField >& _rxFormatted, const OUString& _rChangedPropName )
120 if ( _rChangedPropName != "DataField" )
121 // nothing we're interested in
122 return;
124 impl_adjustFormatToDataFieldType_nothrow( _rxFormatted );
128 namespace
130 void lcl_collectFields_throw( const Reference< XIndexAccess >& _rxColumns, FormatNormalizer::FieldList& _inout_rFields )
134 sal_Int32 nCount( _rxColumns->getCount() );
135 _inout_rFields.reserve( _inout_rFields.size() + static_cast<size_t>(nCount) );
137 Reference< XPropertySet > xColumn;
138 FormatNormalizer::Field aField;
140 for ( sal_Int32 i=0; i<nCount; ++i )
142 xColumn.set( _rxColumns->getByIndex( i ), UNO_QUERY_THROW );
143 OSL_VERIFY( xColumn->getPropertyValue("Name") >>= aField.sName );
144 OSL_VERIFY( xColumn->getPropertyValue("Type") >>= aField.nDataType );
145 OSL_VERIFY( xColumn->getPropertyValue("Scale") >>= aField.nScale );
146 OSL_VERIFY( xColumn->getPropertyValue("IsCurrency") >>= aField.bIsCurrency );
147 _inout_rFields.push_back( aField );
150 catch( const Exception& )
152 DBG_UNHANDLED_EXCEPTION("reportdesign");
158 bool FormatNormalizer::impl_ensureUpToDateFieldList_nothrow()
160 if ( !m_bFieldListDirty )
161 return true;
162 m_aFields.resize( 0 );
164 OSL_PRECOND( m_xReportDefinition.is(), "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no report definition!" );
165 if ( !m_xReportDefinition.is() )
166 return false;
168 ::dbaui::DBSubComponentController* pController( m_rModel.getController() );
169 OSL_ENSURE( pController, "FormatNormalizer::impl_ensureUpToDateFieldList_nothrow: no controller? how can *this* happen?!" );
170 if ( !pController )
171 return false;
175 ::dbtools::StatementComposer aComposer( pController->getConnection(), m_xReportDefinition->getCommand(),
176 m_xReportDefinition->getCommandType(), m_xReportDefinition->getEscapeProcessing() );
178 Reference< XSingleSelectQueryComposer > xComposer( aComposer.getComposer() );
179 if ( !xComposer.is() )
180 return false;
183 Reference< XColumnsSupplier > xSuppCols( xComposer, UNO_QUERY_THROW );
184 Reference< XIndexAccess > xColumns( xSuppCols->getColumns(), UNO_QUERY_THROW );
185 lcl_collectFields_throw( xColumns, m_aFields );
187 Reference< XParametersSupplier > xSuppParams( xComposer, UNO_QUERY_THROW );
188 Reference< XIndexAccess > xParams( xSuppParams->getParameters(), UNO_QUERY_THROW );
189 lcl_collectFields_throw( xParams, m_aFields );
191 catch( const SQLException& )
193 // silence it. This might happen for instance when the user sets an non-existent table,
194 // or things like this
196 catch( const Exception& )
198 DBG_UNHANDLED_EXCEPTION("reportdesign");
201 m_bFieldListDirty = false;
202 return true;
206 void FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow( const Reference< XFormattedField >& _rxFormatted )
208 if ( !impl_ensureUpToDateFieldList_nothrow() )
209 // unable to obtain a recent field list
210 return;
214 sal_Int32 nFormatKey = _rxFormatted->getFormatKey();
215 if ( nFormatKey != 0 )
216 // it's not the "standard numeric" format -> not interested in
217 return;
219 OUString sDataField( _rxFormatted->getDataField() );
220 const OUString sFieldPrefix( "field:[" );
221 if ( sDataField.indexOf( sFieldPrefix ) != 0 )
222 // not bound to a table field
223 // TODO: we might also do this kind of thing for functions and expressions ...
224 return;
225 if ( !sDataField.endsWith("]") )
227 // last character is not the closing brace
228 OSL_FAIL( "FormatNormalizer::impl_adjustFormatToDataFieldType_nothrow: suspicious data field value!" );
229 return;
231 sDataField = sDataField.copy( sFieldPrefix.getLength(), sDataField.getLength() - sFieldPrefix.getLength() - 1 );
233 FieldList::const_iterator field = m_aFields.begin();
234 for ( ; field != m_aFields.end(); ++field )
236 if ( field->sName == sDataField )
237 break;
239 if ( field == m_aFields.end() )
240 // unknown field
241 return;
243 Reference< XNumberFormatsSupplier > xSuppNumFmts( _rxFormatted->getFormatsSupplier(), UNO_QUERY_THROW );
244 Reference< XNumberFormatTypes > xNumFmtTypes( xSuppNumFmts->getNumberFormats(), UNO_QUERY_THROW );
246 nFormatKey = ::dbtools::getDefaultNumberFormat( field->nDataType, field->nScale, field->bIsCurrency, xNumFmtTypes,
247 SvtSysLocale().GetLanguageTag().getLocale() );
248 _rxFormatted->setFormatKey( nFormatKey );
250 catch( const Exception& )
252 DBG_UNHANDLED_EXCEPTION("reportdesign");
257 } // namespace rptui
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */