Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/forms/gridcolumnproptranslator.cxx
Line
Count
Source
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 "gridcolumnproptranslator.hxx"
21
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>
27
28
#include <algorithm>
29
30
namespace xmloff
31
{
32
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;
39
40
    namespace
41
    {
42
        constexpr OUString PARA_ADJUST = u"ParaAdjust"_ustr;
43
44
        constexpr OUString ALIGN = u"Align"_ustr;
45
46
        sal_Int32 findStringElement( const Sequence< OUString >& _rNames, const OUString& _rName )
47
0
        {
48
0
            const OUString* pPos = ::std::find( _rNames.begin(), _rNames.end(), _rName );
49
0
            if ( pPos != _rNames.end() )
50
0
                return pPos - _rNames.begin();
51
0
            return -1;
52
0
        }
53
54
        struct AlignmentTranslationEntry
55
        {
56
            ParagraphAdjust nParagraphValue;
57
            sal_Int16       nControlValue;
58
        }
59
        const AlignmentTranslations[] =
60
        {
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 }
71
        };
72
73
        void valueAlignToParaAdjust(Any& rValue)
74
0
        {
75
0
            sal_Int16 nValue = 0;
76
0
            rValue >>= nValue;
77
0
            const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
78
0
            while (-1 != pTranslation->nControlValue)
79
0
            {
80
0
                if ( nValue == pTranslation->nControlValue )
81
0
                {
82
0
                    rValue <<= pTranslation->nParagraphValue;
83
0
                    return;
84
0
                }
85
0
                ++pTranslation;
86
0
            }
87
0
            OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
88
0
        }
89
90
        void valueParaAdjustToAlign(Any& rValue)
91
0
        {
92
0
            sal_Int32 nValue = 0;
93
0
            rValue >>= nValue;
94
0
            const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
95
0
            while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
96
0
            {
97
0
                if ( static_cast<ParagraphAdjust>(nValue) == pTranslation->nParagraphValue)
98
0
                {
99
0
                    rValue <<= pTranslation->nControlValue;
100
0
                    return;
101
0
                }
102
0
                ++pTranslation;
103
0
            }
104
0
            OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
105
0
        }
106
107
        //= OMergedPropertySetInfo
108
        typedef ::cppu::WeakImplHelper  <   XPropertySetInfo
109
                                            >   OMergedPropertySetInfo_Base;
110
        class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
111
        {
112
        private:
113
            Reference< XPropertySetInfo >   m_xMasterInfo;
114
115
        public:
116
            explicit OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
117
118
        protected:
119
            virtual ~OMergedPropertySetInfo() override;
120
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;
125
        };
126
127
        OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
128
0
            :m_xMasterInfo( _rxMasterInfo )
129
0
        {
130
0
            OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
131
0
        }
132
133
        OMergedPropertySetInfo::~OMergedPropertySetInfo()
134
0
        {
135
0
        }
136
137
        Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties(  )
138
0
        {
139
            // add a "ParaAdjust" property to the master properties
140
0
            Sequence< Property > aProperties;
141
0
            if ( m_xMasterInfo.is() )
142
0
                aProperties = m_xMasterInfo->getProperties();
143
144
0
            sal_Int32 nOldLength = aProperties.getLength();
145
0
            aProperties.realloc( nOldLength + 1 );
146
0
            aProperties.getArray()[ nOldLength ] = getPropertyByName( PARA_ADJUST );
147
148
0
            return aProperties;
149
0
        }
150
151
        Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const OUString& aName )
152
0
        {
153
0
            if ( aName == PARA_ADJUST )
154
0
                return Property( PARA_ADJUST, -1,
155
0
                    ::cppu::UnoType<ParagraphAdjust>::get(), 0 );
156
157
0
            if ( !m_xMasterInfo.is() )
158
0
                return Property();
159
160
0
            return m_xMasterInfo->getPropertyByName( aName );
161
0
        }
162
163
        sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const OUString& Name )
164
0
        {
165
0
            if ( Name == PARA_ADJUST )
166
0
                return true;
167
168
0
            if ( !m_xMasterInfo.is() )
169
0
                return false;
170
171
0
            return m_xMasterInfo->hasPropertyByName( Name );
172
0
        }
173
    }
174
175
    //= OGridColumnPropertyTranslator
176
    OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
177
0
        :m_xGridColumn( _rxGridColumn )
178
0
    {
179
0
        OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
180
0
    }
181
182
    OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
183
0
    {
184
0
    }
185
186
    Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo(  )
187
0
    {
188
0
        Reference< XPropertySetInfo > xColumnPropInfo;
189
0
        if ( m_xGridColumn.is() )
190
0
            xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
191
0
        return new OMergedPropertySetInfo( xColumnPropInfo );
192
0
    }
193
194
    void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const OUString& _rPropertyName, const Any& aValue )
195
0
    {
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.
198
199
0
        if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
200
0
            throw UnknownPropertyException( _rPropertyName, *this );
201
202
0
        Sequence< OUString > aNames( &_rPropertyName, 1 );
203
0
        Sequence< Any >             aValues( &aValue, 1 );
204
0
        setPropertyValues( aNames, aValues );
205
0
    }
206
207
    Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const OUString& PropertyName )
208
0
    {
209
0
        Sequence< OUString > aNames( &PropertyName, 1 );
210
0
        Sequence< Any > aValues = getPropertyValues( aNames );
211
0
        OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
212
0
        if ( aValues.getLength() == 1 )
213
0
            return aValues[0];
214
0
        return Any();
215
0
    }
216
217
    void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
218
0
    {
219
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
220
0
    }
221
222
    void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
223
0
    {
224
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
225
0
    }
226
227
    void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
228
0
    {
229
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
230
0
    }
231
232
    void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
233
0
    {
234
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
235
0
    }
236
237
    void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
238
0
    {
239
0
        if ( !m_xGridColumn.is() )
240
0
            return;
241
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 ...
244
245
0
        Sequence< OUString > aTranslatedNames( aPropertyNames );
246
0
        Sequence< Any >             aTranslatedValues( aValues );
247
248
0
        sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
249
0
        if ( nParaAlignPos != -1 )
250
0
        {
251
0
            if (aTranslatedNames.getLength() != aTranslatedValues.getLength())
252
0
                    throw css::lang::IllegalArgumentException(
253
0
                        u"lengths do not match"_ustr, getXWeak(), -1);
254
0
            aTranslatedNames.getArray()[ nParaAlignPos ] = ALIGN;
255
0
            valueParaAdjustToAlign( aTranslatedValues.getArray()[ nParaAlignPos ] );
256
0
        }
257
258
0
        m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
259
0
    }
260
261
    Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< OUString >& aPropertyNames )
262
0
    {
263
0
        Sequence< Any > aValues( aPropertyNames.getLength() );
264
0
        if ( !m_xGridColumn.is() )
265
0
            return aValues;
266
267
0
        Sequence< OUString > aTranslatedNames( aPropertyNames );
268
0
        sal_Int32 nAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
269
0
        if ( nAlignPos != -1 )
270
0
            aTranslatedNames.getArray()[ nAlignPos ] = ALIGN;
271
272
0
        aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
273
0
        if ( nAlignPos != -1 )
274
0
            valueAlignToParaAdjust( aValues.getArray()[ nAlignPos ] );
275
276
0
        return aValues;
277
0
    }
278
279
    void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
280
0
    {
281
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
282
0
    }
283
284
    void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
285
0
    {
286
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
287
0
    }
288
289
    void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
290
0
    {
291
0
        OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
292
0
    }
293
294
} // namespace xmloff
295
296
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */