1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: PropertyHelper.cxx,v $
* $Revision: 1.7 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_chart2.hxx"
#include "PropertyHelper.hxx"
#include "ContainerHelper.hxx"
#include "macros.hxx"
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <vector>
#include <algorithm>
#include <functional>
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using ::rtl::OUString;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
namespace
{
struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
{
explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
: m_aValue( rValue ), m_xAccess( xAccess )
{
OSL_ASSERT( m_xAccess.is());
}
bool operator() ( const OUString & rName )
{
try
{
return (m_xAccess->getByName( rName ) == m_aValue);
}
catch( const uno::Exception & ex )
{
ASSERT_EXCEPTION( ex );
}
return false;
}
private:
Any m_aValue;
Reference< container::XNameAccess > m_xAccess;
};
struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
{
lcl_StringMatches( const OUString & rCmpStr ) :
m_aCmpStr( rCmpStr )
{}
bool operator() ( const OUString & rStr )
{
return rStr.match( m_aCmpStr );
}
private:
OUString m_aCmpStr;
};
struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
{
lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
m_nPrefixLength( nPrefixLength )
{}
sal_Int32 operator() ( const OUString & rStr )
{
if( m_nPrefixLength > rStr.getLength() )
return 0;
return rStr.copy( m_nPrefixLength ).toInt32( 10 /* radix */ );
}
private:
sal_Int32 m_nPrefixLength;
};
/** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
line dash or line marker to the corresponding name container with a unique
name.
@param rPrefix
The prefix used for automated name generation.
@param rPreferredName
If this string is not empty it is used as name if it is unique in the
table. Otherwise a new name is generated using pPrefix.
@return the new name under which the property was stored in the table
*/
OUString lcl_addNamedPropertyUniqueNameToTable(
const Any & rValue,
const Reference< container::XNameContainer > & xNameContainer,
const OUString & rPrefix,
const OUString & rPreferredName )
{
if( ! xNameContainer.is() ||
! rValue.hasValue() ||
( rValue.getValueType() != xNameContainer->getElementType()))
return rPreferredName;
try
{
Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
::std::vector< OUString >::const_iterator aIt(
::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
// element not found in container
if( aIt == aNames.end())
{
OUString aUniqueName;
// check if preferred name is already used
if( rPreferredName.getLength())
{
aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
if( aIt == aNames.end())
aUniqueName = rPreferredName;
}
if( ! aUniqueName.getLength())
{
// create a unique id using the prefix plus a number
::std::vector< sal_Int32 > aNumbers;
::std::vector< OUString >::iterator aNonConstIt(
::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
::std::transform( aNames.begin(), aNonConstIt,
back_inserter( aNumbers ),
lcl_OUStringRestToInt32( rPrefix.getLength() ));
::std::vector< sal_Int32 >::const_iterator aMaxIt(
::std::max_element( aNumbers.begin(), aNumbers.end()));
sal_Int32 nIndex = 1;
if( aMaxIt != aNumbers.end())
nIndex = (*aMaxIt) + 1;
aUniqueName = rPrefix + OUString::valueOf( nIndex );
}
OSL_ASSERT( aUniqueName.getLength());
xNameContainer->insertByName( aUniqueName, rValue );
return aUniqueName;
}
else
// element found => return name
return *aIt;
}
catch( const uno::Exception & ex )
{
ASSERT_EXCEPTION( ex );
}
return rPreferredName;
}
} // anonymous namespace
namespace chart
{
namespace PropertyHelper
{
OUString addLineDashUniqueNameToTable(
const Any & rValue,
const Reference< lang::XMultiServiceFactory > & xFact,
const OUString & rPreferredName )
{
if( xFact.is())
{
Reference< container::XNameContainer > xNameCnt(
xFact->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
uno::UNO_QUERY );
if( xNameCnt.is())
return lcl_addNamedPropertyUniqueNameToTable(
rValue, xNameCnt, C2U( "ChartDash " ), rPreferredName );
}
return OUString();
}
OUString addGradientUniqueNameToTable(
const Any & rValue,
const Reference< lang::XMultiServiceFactory > & xFact,
const OUString & rPreferredName )
{
if( xFact.is())
{
Reference< container::XNameContainer > xNameCnt(
xFact->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
uno::UNO_QUERY );
if( xNameCnt.is())
return lcl_addNamedPropertyUniqueNameToTable(
rValue, xNameCnt, C2U( "ChartGradient " ), rPreferredName );
}
return OUString();
}
OUString addTransparencyGradientUniqueNameToTable(
const Any & rValue,
const Reference< lang::XMultiServiceFactory > & xFact,
const OUString & rPreferredName )
{
if( xFact.is())
{
Reference< container::XNameContainer > xNameCnt(
xFact->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
uno::UNO_QUERY );
if( xNameCnt.is())
return lcl_addNamedPropertyUniqueNameToTable(
rValue, xNameCnt, C2U( "ChartTransparencyGradient " ), rPreferredName );
}
return OUString();
}
OUString addHatchUniqueNameToTable(
const Any & rValue,
const Reference< lang::XMultiServiceFactory > & xFact,
const OUString & rPreferredName )
{
if( xFact.is())
{
Reference< container::XNameContainer > xNameCnt(
xFact->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
uno::UNO_QUERY );
if( xNameCnt.is())
return lcl_addNamedPropertyUniqueNameToTable(
rValue, xNameCnt, C2U( "ChartHatch " ), rPreferredName );
}
return OUString();
}
OUString addBitmapUniqueNameToTable(
const Any & rValue,
const Reference< lang::XMultiServiceFactory > & xFact,
const OUString & rPreferredName )
{
if( xFact.is())
{
Reference< container::XNameContainer > xNameCnt(
xFact->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
uno::UNO_QUERY );
if( xNameCnt.is())
return lcl_addNamedPropertyUniqueNameToTable(
rValue, xNameCnt, C2U( "ChartBitmap " ), rPreferredName );
}
return OUString();
}
// ----------------------------------------
void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
{
tPropertyValueMap::iterator aIt( rOutMap.find( key ));
if( aIt == rOutMap.end())
rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
else
(*aIt).second = rAny;
}
template<>
void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
{
setPropertyValueAny( rOutMap, key, rAny );
}
void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
{
OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
setPropertyValue( rOutMap, key, rAny );
}
template<>
void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
{
setPropertyValueDefaultAny( rOutMap, key, rAny );
}
void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
{
setPropertyValueDefault( rOutMap, key, uno::Any());
}
} // namespace PropertyHelper
} // namespace chart
|