LibreOffice Module xmloff (master) 1
formcellbinding.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "formcellbinding.hxx"
21#include <com/sun/star/form/binding/XBindableValue.hpp>
22#include <com/sun/star/form/binding/XListEntrySink.hpp>
23#include <com/sun/star/frame/XModel.hpp>
24#include <com/sun/star/container/XChild.hpp>
25#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
26#include <com/sun/star/lang/XServiceInfo.hpp>
27#include <com/sun/star/lang/XMultiServiceFactory.hpp>
28#include <com/sun/star/beans/NamedValue.hpp>
29#include "strings.hxx"
30#include <osl/diagnose.h>
32
33#include <algorithm>
34
35namespace xmloff
36{
37
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::frame;
41 using namespace ::com::sun::star::sheet;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::drawing;
44 using namespace ::com::sun::star::table;
45 using namespace ::com::sun::star::form;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::form::binding;
48
49namespace
50{
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::XInterface;
53 using ::com::sun::star::container::XChild;
54 using ::com::sun::star::frame::XModel;
55 using ::com::sun::star::uno::UNO_QUERY;
56
57 template< class TYPE >
58 Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
59 {
60 Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
61 if ( xTypedNode.is() )
62 return xTypedNode;
63 else
64 {
65 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
66 if ( xChild.is() )
67 return getTypedModelNode< TYPE >( xChild->getParent() );
68 else
69 return nullptr;
70 }
71 }
72
73 Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
74 {
75 return getTypedModelNode< XModel >( _rxModelNode );
76 }
77
78 struct StringCompare
79 {
80 private:
81 const OUString & m_sReference;
82
83 public:
84 explicit StringCompare( const OUString& _rReference ) : m_sReference( _rReference ) { }
85
86 bool operator()( std::u16string_view _rCompare )
87 {
88 return ( _rCompare == m_sReference );
89 }
90 };
91}
92
93//= FormCellBindingHelper
94FormCellBindingHelper::FormCellBindingHelper( const Reference< XPropertySet >& _rxControlModel, const Reference< XModel >& _rxDocument )
95 :m_xControlModel( _rxControlModel )
96 ,m_xDocument( _rxDocument, UNO_QUERY )
97{
98 OSL_ENSURE( m_xControlModel.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
99
100 if ( !m_xDocument.is() )
101 m_xDocument.set(getDocument( m_xControlModel ), css::uno::UNO_QUERY);
102 OSL_ENSURE( m_xDocument.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
103}
104
105bool FormCellBindingHelper::livesInSpreadsheetDocument( const Reference< XPropertySet >& _rxControlModel )
106{
107 Reference< XSpreadsheetDocument > xDocument( getDocument( _rxControlModel ), UNO_QUERY );
108 return xDocument.is();
109}
110
111bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress ) const
112{
113 Any aAddress;
116 Any( _rAddressDescription ),
118 aAddress,
119 false
120 )
121 && ( aAddress >>= _rAddress );
122}
123
124bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription,
125 CellRangeAddress& /* [out] */ _rAddress ) const
126{
127 Any aAddress;
130 Any( _rAddressDescription ),
132 aAddress,
133 true
134 )
135 && ( aAddress >>= _rAddress );
136}
137
138Reference< XValueBinding > FormCellBindingHelper::createCellBindingFromStringAddress( const OUString& _rAddress, bool _bUseIntegerBinding ) const
139{
140 Reference< XValueBinding > xBinding;
141 if ( !m_xDocument.is() )
142 // very bad ...
143 return xBinding;
144
145 // get the UNO representation of the address
146 CellAddress aAddress;
147 if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
148 return xBinding;
149
151 _bUseIntegerBinding ? OUString(SERVICE_LISTINDEXCELLBINDING) : OUString(SERVICE_CELLVALUEBINDING),
153 Any( aAddress )
154 ), css::uno::UNO_QUERY);
155
156 return xBinding;
157}
158
159Reference< XListEntrySource > FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString& _rAddress ) const
160{
161 Reference< XListEntrySource > xSource;
162
163 CellRangeAddress aRangeAddress;
164 if ( !convertStringAddress( _rAddress, aRangeAddress ) )
165 return xSource;
166
167 // create a range object for this address
171 Any( aRangeAddress )
172 ), css::uno::UNO_QUERY);
173
174 return xSource;
175}
176
177OUString FormCellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
178{
179 OSL_PRECOND( !_rxBinding.is() || isCellBinding( _rxBinding ), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
180
181 OUString sAddress;
182 try
183 {
184 Reference< XPropertySet > xBindingProps( _rxBinding, UNO_QUERY );
185 OSL_ENSURE( xBindingProps.is() || !_rxBinding.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
186 if ( xBindingProps.is() )
187 {
188 CellAddress aAddress;
189 xBindingProps->getPropertyValue( PROPERTY_BOUND_CELL ) >>= aAddress;
190
191 Any aStringAddress;
193 PROPERTY_FILE_REPRESENTATION, aStringAddress, false );
194
195 aStringAddress >>= sAddress;
196 }
197 }
198 catch( const Exception& )
199 {
200 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellBinding" );
201 }
202
203 return sAddress;
204}
205
206OUString FormCellBindingHelper::getStringAddressFromCellListSource( const Reference< XListEntrySource >& _rxSource ) const
207{
208 OSL_PRECOND( !_rxSource.is() || isCellRangeListSource( _rxSource ), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
209
210 OUString sAddress;
211 try
212 {
213 Reference< XPropertySet > xSourceProps( _rxSource, UNO_QUERY );
214 OSL_ENSURE( xSourceProps.is() || !_rxSource.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
215 if ( xSourceProps.is() )
216 {
217 CellRangeAddress aRangeAddress;
218 xSourceProps->getPropertyValue( PROPERTY_LIST_CELL_RANGE ) >>= aRangeAddress;
219
220 Any aStringAddress;
222 PROPERTY_FILE_REPRESENTATION, aStringAddress, true );
223 aStringAddress >>= sAddress;
224 }
225 }
226 catch( const Exception& )
227 {
228 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellListSource" );
229 }
230
231 return sAddress;
232}
233
234bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference< XSpreadsheetDocument >& _rxDocument, const OUString& _rService )
235{
236 bool bYesItIs = false;
237
238 try
239 {
240 Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY );
241 if ( xSI.is() && xSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
242 {
243 Reference< XMultiServiceFactory > xDocumentFactory( _rxDocument, UNO_QUERY );
244 OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
245
246 if ( xDocumentFactory.is() )
247 {
248 const Sequence<OUString> aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
249
250 bYesItIs = std::any_of( aAvailableServices.begin(), aAvailableServices.end(), StringCompare( _rService ) );
251 }
252 }
253 }
254 catch( const Exception& )
255 {
256 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies" );
257 }
258
259 return bYesItIs;
260}
261
263{
265}
266
267bool FormCellBindingHelper::isListCellRangeAllowed( const Reference< XModel >& _rxDocument )
268{
270 Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
272 );
273}
274
276{
277 bool bAllow( false );
278
279 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
280 if ( xSink.is() )
281 {
283 }
284
285 return bAllow;
286}
287
289{
290 bool bAllow( false );
291
292 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
293 if ( xBindable.is() )
294 {
295 // the control can potentially be bound to an external value
296 // Does it live within a Calc document, and is able to supply CellBindings?
298 }
299
300 return bAllow;
301}
302
303bool FormCellBindingHelper::isCellBindingAllowed( const Reference< XModel >& _rxDocument )
304{
306 Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
308 );
309}
310
311bool FormCellBindingHelper::isCellBinding( const Reference< XValueBinding >& _rxBinding )
312{
314}
315
316bool FormCellBindingHelper::isCellIntegerBinding( const Reference< XValueBinding >& _rxBinding )
317{
319}
320
321bool FormCellBindingHelper::isCellRangeListSource( const Reference< XListEntrySource >& _rxSource )
322{
324}
325
326bool FormCellBindingHelper::doesComponentSupport( const Reference< XInterface >& _rxComponent, const OUString& _rService )
327{
328 Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
329 bool bDoes = xSI.is() && xSI->supportsService( _rService );
330 return bDoes;
331}
332
333Reference< XValueBinding > FormCellBindingHelper::getCurrentBinding( ) const
334{
335 Reference< XValueBinding > xBinding;
336 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
337 if ( xBindable.is() )
338 xBinding = xBindable->getValueBinding();
339 return xBinding;
340}
341
342Reference< XListEntrySource > FormCellBindingHelper::getCurrentListSource( ) const
343{
344 Reference< XListEntrySource > xSource;
345 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
346 if ( xSink.is() )
347 xSource = xSink->getListEntrySource();
348 return xSource;
349}
350
351void FormCellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
352{
353 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
354 OSL_PRECOND( xBindable.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
355 if ( xBindable.is() )
356 xBindable->setValueBinding( _rxBinding );
357}
358
359void FormCellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
360{
361 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
362 OSL_PRECOND( xSink.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
363 if ( xSink.is() )
364 xSink->setListEntrySource( _rxSource );
365}
366
367Reference< XInterface > FormCellBindingHelper::createDocumentDependentInstance( const OUString& _rService, const OUString& _rArgumentName,
368 const Any& _rArgumentValue ) const
369{
370 Reference< XInterface > xReturn;
371
372 Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
373 OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
374 if ( xDocumentFactory.is() )
375 {
376 try
377 {
378 if ( !_rArgumentName.isEmpty() )
379 {
380 NamedValue aArg;
381 aArg.Name = _rArgumentName;
382 aArg.Value = _rArgumentValue;
383
384 Sequence< Any > aArgs{ Any(aArg) };
385 xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
386 }
387 else
388 {
389 xReturn = xDocumentFactory->createInstance( _rService );
390 }
391 }
392 catch ( const Exception& )
393 {
394 OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
395 }
396 }
397 return xReturn;
398}
399
400bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString& _rInputProperty, const Any& _rInputValue,
401 const OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const
402{
403 bool bSuccess = false;
404
405 Reference< XPropertySet > xConverter(
407 _bIsRange ? OUString(SERVICE_RANGEADDRESS_CONVERSION) : OUString(SERVICE_ADDRESS_CONVERSION),
408 OUString(),
409 Any()
410 ),
411 UNO_QUERY
412 );
413 OSL_ENSURE( xConverter.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
414 if ( xConverter.is() )
415 {
416 try
417 {
418 xConverter->setPropertyValue( _rInputProperty, _rInputValue );
419 _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
420 bSuccess = true;
421 }
422 catch( const Exception& )
423 {
424 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::doConvertAddressRepresentations" );
425 }
426 }
427
428 return bSuccess;
429}
430
431} // namespace xmloff
432
433/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool convertStringAddress(const OUString &_rAddressDescription, css::table::CellAddress &_rAddress) const
creates an address object from a string representation of a cell address
bool isCellBindingAllowed() const
checks whether it's possible to bind the control model to a spreadsheet cell
css::uno::Reference< css::form::binding::XValueBinding > getCurrentBinding() const
returns the current binding of our control model, if any.
OUString getStringAddressFromCellListSource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource) const
creates a string representation for the given list source's range address
css::uno::Reference< css::form::binding::XListEntrySource > getCurrentListSource() const
returns the current external list source of the control model, if any
css::uno::Reference< css::beans::XPropertySet > m_xControlModel
void setBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding)
sets a new binding for our control model @precond the control model is bindable (which is implied by ...
static bool isCellRangeListSource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource)
checks whether a given list source is a spreadsheet cell list source
static bool livesInSpreadsheetDocument(const css::uno::Reference< css::beans::XPropertySet > &_rxControlModel)
determines whether the given control model lives in a spreadsheet document
bool isSpreadsheetDocumentWhichSupplies(const OUString &_rService) const
determines if our document is a spreadsheet document, and can supply the given service
static bool doesComponentSupport(const css::uno::Reference< css::uno::XInterface > &_rxComponent, const OUString &_rService)
checks whether a given component supports a given service
OUString getStringAddressFromCellBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding) const
creates a string representation for the given value binding's address
css::uno::Reference< css::form::binding::XValueBinding > createCellBindingFromStringAddress(const OUString &_rAddress, bool _bUseIntegerBinding) const
gets a cell binding for the given address @precond isCellBindingAllowed returns <TRUE>
static bool isCellBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding)
checks whether a given binding is a spreadsheet cell binding
FormCellBindingHelper(const css::uno::Reference< css::beans::XPropertySet > &_rxControlModel, const css::uno::Reference< css::frame::XModel > &_rxDocument)
ctor
static bool isCellIntegerBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding)
checks whether a given binding is a spreadsheet cell binding, exchanging integer values
css::uno::Reference< css::form::binding::XListEntrySource > createCellListSourceFromStringAddress(const OUString &_rAddress) const
gets a cell range list source binding for the given address
css::uno::Reference< css::uno::XInterface > createDocumentDependentInstance(const OUString &_rService, const OUString &_rArgumentName, const css::uno::Any &_rArgumentValue) const
uses the document (it's factory interface, respectively) to create a component instance
void setListSource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource)
sets a list source for our control model @precond the control model is a list sink (which is implied ...
css::uno::Reference< css::sheet::XSpreadsheetDocument > m_xDocument
bool isListCellRangeAllowed() const
checks whether it's possible to bind the control model to a range of spreadsheet cells supplying the ...
bool doConvertAddressRepresentations(const OUString &_rInputProperty, const css::uno::Any &_rInputValue, const OUString &_rOutputProperty, css::uno::Any &_rOutputValue, bool _bIsRange) const
converts an address representation into another one
Reference< XOfficeDatabaseDocument > m_xDocument
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XTypeConverter > xConverter
const OUString & m_sReference
tools::SvRef< SvBaseLink > xSink
@ Exception
constexpr OUStringLiteral SERVICE_LISTINDEXCELLBINDING
Definition: strings.hxx:147
constexpr OUStringLiteral PROPERTY_LIST_CELL_RANGE
Definition: strings.hxx:139
constexpr OUStringLiteral SERVICE_ADDRESS_CONVERSION
Definition: strings.hxx:149
constexpr OUStringLiteral SERVICE_CELLRANGELISTSOURCE
Definition: strings.hxx:148
constexpr OUStringLiteral PROPERTY_ADDRESS
Definition: strings.hxx:140
constexpr OUStringLiteral PROPERTY_FILE_REPRESENTATION
Definition: strings.hxx:141
constexpr OUStringLiteral SERVICE_RANGEADDRESS_CONVERSION
Definition: strings.hxx:150
constexpr OUStringLiteral SERVICE_CELLVALUEBINDING
Definition: strings.hxx:146
constexpr OUStringLiteral PROPERTY_BOUND_CELL
Definition: strings.hxx:138
constexpr OUStringLiteral SERVICE_SPREADSHEET_DOCUMENT
Definition: strings.hxx:145