Coverage Report

Created: 2026-07-10 11:04

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 <forms/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_START,            awt::TextAlign::LEFT     },
70
            { ParagraphAdjust_END,              awt::TextAlign::RIGHT    },
71
            { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE,  awt::TextAlign::LEFT     },
72
            { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE,  -1 }
73
        };
74
75
        void valueAlignToParaAdjust(Any& rValue)
76
0
        {
77
0
            sal_Int16 nValue = 0;
78
0
            rValue >>= nValue;
79
0
            const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
80
0
            while (-1 != pTranslation->nControlValue)
81
0
            {
82
0
                if ( nValue == pTranslation->nControlValue )
83
0
                {
84
0
                    rValue <<= pTranslation->nParagraphValue;
85
0
                    return;
86
0
                }
87
0
                ++pTranslation;
88
0
            }
89
0
            OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
90
0
        }
91
92
        void valueParaAdjustToAlign(Any& rValue)
93
0
        {
94
0
            sal_Int32 nValue = 0;
95
0
            rValue >>= nValue;
96
0
            const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
97
0
            while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
98
0
            {
99
0
                if ( static_cast<ParagraphAdjust>(nValue) == pTranslation->nParagraphValue)
100
0
                {
101
0
                    rValue <<= pTranslation->nControlValue;
102
0
                    return;
103
0
                }
104
0
                ++pTranslation;
105
0
            }
106
0
            OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
107
0
        }
108
109
        //= OMergedPropertySetInfo
110
        typedef ::cppu::WeakImplHelper  <   XPropertySetInfo
111
                                            >   OMergedPropertySetInfo_Base;
112
        class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
113
        {
114
        private:
115
            Reference< XPropertySetInfo >   m_xMasterInfo;
116
117
        public:
118
            explicit OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
119
120
        protected:
121
            virtual ~OMergedPropertySetInfo() override;
122
123
            // XPropertySetInfo
124
            virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties(  ) override;
125
            virtual css::beans::Property SAL_CALL getPropertyByName( const OUString& aName ) override;
126
            virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) override;
127
        };
128
129
        OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
130
0
            :m_xMasterInfo( _rxMasterInfo )
131
0
        {
132
0
            OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
133
0
        }
134
135
        OMergedPropertySetInfo::~OMergedPropertySetInfo()
136
0
        {
137
0
        }
138
139
        Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties(  )
140
0
        {
141
            // add a "ParaAdjust" property to the master properties
142
0
            Sequence< Property > aProperties;
143
0
            if ( m_xMasterInfo.is() )
144
0
                aProperties = m_xMasterInfo->getProperties();
145
146
0
            sal_Int32 nOldLength = aProperties.getLength();
147
0
            aProperties.realloc( nOldLength + 1 );
148
0
            aProperties.getArray()[ nOldLength ] = getPropertyByName( PARA_ADJUST );
149
150
0
            return aProperties;
151
0
        }
152
153
        Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const OUString& aName )
154
0
        {
155
0
            if ( aName == PARA_ADJUST )
156
0
                return Property( PARA_ADJUST, -1,
157
0
                    ::cppu::UnoType<ParagraphAdjust>::get(), 0 );
158
159
0
            if ( !m_xMasterInfo.is() )
160
0
                return Property();
161
162
0
            return m_xMasterInfo->getPropertyByName( aName );
163
0
        }
164
165
        sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const OUString& Name )
166
0
        {
167
0
            if ( Name == PARA_ADJUST )
168
0
                return true;
169
170
0
            if ( !m_xMasterInfo.is() )
171
0
                return false;
172
173
0
            return m_xMasterInfo->hasPropertyByName( Name );
174
0
        }
175
    }
176
177
    //= OGridColumnPropertyTranslator
178
    OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
179
0
        :m_xGridColumn( _rxGridColumn )
180
0
    {
181
0
        OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
182
0
    }
183
184
    OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
185
0
    {
186
0
    }
187
188
    Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo(  )
189
0
    {
190
0
        Reference< XPropertySetInfo > xColumnPropInfo;
191
0
        if ( m_xGridColumn.is() )
192
0
            xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
193
0
        return new OMergedPropertySetInfo( xColumnPropInfo );
194
0
    }
195
196
    void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const OUString& _rPropertyName, const Any& aValue )
197
0
    {
198
        // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
199
        // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
200
201
0
        if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
202
0
            throw UnknownPropertyException( _rPropertyName, *this );
203
204
0
        Sequence< OUString > aNames( &_rPropertyName, 1 );
205
0
        Sequence< Any >             aValues( &aValue, 1 );
206
0
        setPropertyValues( aNames, aValues );
207
0
    }
208
209
    Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const OUString& PropertyName )
210
0
    {
211
0
        Sequence< OUString > aNames( &PropertyName, 1 );
212
0
        Sequence< Any > aValues = getPropertyValues( aNames );
213
0
        OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
214
0
        if ( aValues.getLength() == 1 )
215
0
            return aValues[0];
216
0
        return Any();
217
0
    }
218
219
    void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
220
0
    {
221
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
222
0
    }
223
224
    void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
225
0
    {
226
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
227
0
    }
228
229
    void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
230
0
    {
231
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
232
0
    }
233
234
    void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
235
0
    {
236
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
237
0
    }
238
239
    void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
240
0
    {
241
0
        if ( !m_xGridColumn.is() )
242
0
            return;
243
244
        // if there's ever the need for more than one property being translated, then we should
245
        // certainly have a more clever implementation than this ...
246
247
0
        Sequence< OUString > aTranslatedNames( aPropertyNames );
248
0
        Sequence< Any >             aTranslatedValues( aValues );
249
250
0
        sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
251
0
        if ( nParaAlignPos != -1 )
252
0
        {
253
0
            if (aTranslatedNames.getLength() != aTranslatedValues.getLength())
254
0
                    throw css::lang::IllegalArgumentException(
255
0
                        u"lengths do not match"_ustr, getXWeak(), -1);
256
0
            aTranslatedNames.getArray()[ nParaAlignPos ] = ALIGN;
257
0
            valueParaAdjustToAlign( aTranslatedValues.getArray()[ nParaAlignPos ] );
258
0
        }
259
260
0
        m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
261
0
    }
262
263
    Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< OUString >& aPropertyNames )
264
0
    {
265
0
        Sequence< Any > aValues( aPropertyNames.getLength() );
266
0
        if ( !m_xGridColumn.is() )
267
0
            return aValues;
268
269
0
        Sequence< OUString > aTranslatedNames( aPropertyNames );
270
0
        sal_Int32 nAlignPos = findStringElement( aTranslatedNames, PARA_ADJUST );
271
0
        if ( nAlignPos != -1 )
272
0
            aTranslatedNames.getArray()[ nAlignPos ] = ALIGN;
273
274
0
        aValues = m_xGridColumn->getPropertyValues( aTranslatedNames );
275
0
        if ( nAlignPos != -1 )
276
0
            valueAlignToParaAdjust( aValues.getArray()[ nAlignPos ] );
277
278
0
        return aValues;
279
0
    }
280
281
    void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
282
0
    {
283
0
        OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
284
0
    }
285
286
    void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
287
0
    {
288
0
        OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
289
0
    }
290
291
    void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
292
0
    {
293
0
        OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
294
0
    }
295
296
} // namespace xmloff
297
298
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */