tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / xmloff / source / forms / gridcolumnproptranslator.cxx
blob6e11bf5bc4f77dd5476c89028b6fa45e76c0ab75
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 "gridcolumnproptranslator.hxx"
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <com/sun/star/awt/TextAlign.hpp>
24 #include <com/sun/star/style/ParagraphAdjust.hpp>
25 #include <osl/diagnose.h>
26 #include <cppuhelper/implbase.hxx>
28 #include <algorithm>
30 namespace xmloff
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::style;
40 namespace
42 constexpr OUString PARA_ADJUST = u"ParaAdjust"_ustr;
44 constexpr OUString ALIGN = u"Align"_ustr;
46 sal_Int32 findStringElement( const Sequence< OUString >& _rNames, const OUString& _rName )
48 const OUString* pPos = ::std::find( _rNames.begin(), _rNames.end(), _rName );
49 if ( pPos != _rNames.end() )
50 return pPos - _rNames.begin();
51 return -1;
54 struct AlignmentTranslationEntry
56 ParagraphAdjust nParagraphValue;
57 sal_Int16 nControlValue;
59 const AlignmentTranslations[] =
61 // note that order matters:
62 // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
63 // and use the first matching entry
64 { ParagraphAdjust_LEFT, awt::TextAlign::LEFT },
65 { ParagraphAdjust_CENTER, awt::TextAlign::CENTER },
66 { ParagraphAdjust_RIGHT, awt::TextAlign::RIGHT },
67 { ParagraphAdjust_BLOCK, awt::TextAlign::RIGHT },
68 { ParagraphAdjust_STRETCH, awt::TextAlign::LEFT },
69 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE, awt::TextAlign::LEFT },
70 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE, -1 }
73 void valueAlignToParaAdjust(Any& rValue)
75 sal_Int16 nValue = 0;
76 rValue >>= nValue;
77 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
78 while (-1 != pTranslation->nControlValue)
80 if ( nValue == pTranslation->nControlValue )
82 rValue <<= pTranslation->nParagraphValue;
83 return;
85 ++pTranslation;
87 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
90 void valueParaAdjustToAlign(Any& rValue)
92 sal_Int32 nValue = 0;
93 rValue >>= nValue;
94 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
95 while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
97 if ( static_cast<ParagraphAdjust>(nValue) == pTranslation->nParagraphValue)
99 rValue <<= pTranslation->nControlValue;
100 return;
102 ++pTranslation;
104 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
107 //= OMergedPropertySetInfo
108 typedef ::cppu::WeakImplHelper < XPropertySetInfo
109 > OMergedPropertySetInfo_Base;
110 class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
112 private:
113 Reference< XPropertySetInfo > m_xMasterInfo;
115 public:
116 explicit OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
118 protected:
119 virtual ~OMergedPropertySetInfo() override;
121 // XPropertySetInfo
122 virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties( ) override;
123 virtual css::beans::Property SAL_CALL getPropertyByName( const OUString& aName ) override;
124 virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) override;
127 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
128 :m_xMasterInfo( _rxMasterInfo )
130 OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
133 OMergedPropertySetInfo::~OMergedPropertySetInfo()
137 Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties( )
139 // add a "ParaAdjust" property to the master properties
140 Sequence< Property > aProperties;
141 if ( m_xMasterInfo.is() )
142 aProperties = m_xMasterInfo->getProperties();
144 sal_Int32 nOldLength = aProperties.getLength();
145 aProperties.realloc( nOldLength + 1 );
146 aProperties.getArray()[ nOldLength ] = getPropertyByName( PARA_ADJUST );
148 return aProperties;
151 Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const OUString& aName )
153 if ( aName == PARA_ADJUST )
154 return Property( PARA_ADJUST, -1,
155 ::cppu::UnoType<ParagraphAdjust>::get(), 0 );
157 if ( !m_xMasterInfo.is() )
158 return Property();
160 return m_xMasterInfo->getPropertyByName( aName );
163 sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const OUString& Name )
165 if ( Name == PARA_ADJUST )
166 return true;
168 if ( !m_xMasterInfo.is() )
169 return false;
171 return m_xMasterInfo->hasPropertyByName( Name );
175 //= OGridColumnPropertyTranslator
176 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
177 :m_xGridColumn( _rxGridColumn )
179 OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
182 OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
186 Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo( )
188 Reference< XPropertySetInfo > xColumnPropInfo;
189 if ( m_xGridColumn.is() )
190 xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
191 return new OMergedPropertySetInfo( xColumnPropInfo );
194 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const OUString& _rPropertyName, const Any& aValue )
196 // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
197 // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
199 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
200 throw UnknownPropertyException( _rPropertyName, *this );
202 Sequence< OUString > aNames( &_rPropertyName, 1 );
203 Sequence< Any > aValues( &aValue, 1 );
204 setPropertyValues( aNames, aValues );
207 Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const OUString& PropertyName )
209 Sequence< OUString > aNames( &PropertyName, 1 );
210 Sequence< Any > aValues = getPropertyValues( aNames );
211 OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
212 if ( aValues.getLength() == 1 )
213 return aValues[0];
214 return Any();
217 void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
219 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
222 void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
224 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
227 void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
229 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
232 void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
234 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
237 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
239 if ( !m_xGridColumn.is() )
240 return;
242 // if there's ever the need for more than one property being translated, then we should
243 // certainly have a more clever implementation than this ...
245 Sequence< OUString > aTranslatedNames( aPropertyNames );
246 Sequence< Any > aTranslatedValues( aValues );
248 sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
249 if ( nParaAlignPos != -1 )
251 if (aTranslatedNames.getLength() != aTranslatedValues.getLength())
252 throw css::lang::IllegalArgumentException(
253 u"lengths do not match"_ustr, getXWeak(), -1);
254 aTranslatedNames.getArray()[ nParaAlignPos ] = ALIGN;
255 valueParaAdjustToAlign( aTranslatedValues.getArray()[ nParaAlignPos ] );
258 m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
261 Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< OUString >& aPropertyNames )
263 Sequence< Any > aValues( aPropertyNames.getLength() );
264 if ( !m_xGridColumn.is() )
265 return aValues;
267 Sequence< OUString > aTranslatedNames( aPropertyNames );
268 sal_Int32 nAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
269 if ( nAlignPos != -1 )
270 aTranslatedNames.getArray()[ nAlignPos ] = ALIGN;
272 aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
273 if ( nAlignPos != -1 )
274 valueAlignToParaAdjust( aValues.getArray()[ nAlignPos ] );
276 return aValues;
279 void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
281 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
284 void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
286 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
289 void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
291 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
294 } // namespace xmloff
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */