xref: /AOO41X/main/oox/source/drawingml/customshapeproperties.cxx (revision d742e3ecb95e5eaf7ad7d7fb5fd2d734bf0a66a6)
1ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ca5ec200SAndrew Rist  * distributed with this work for additional information
6ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18ca5ec200SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20ca5ec200SAndrew Rist  *************************************************************/
21ca5ec200SAndrew Rist 
22ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/drawingml/customshapeproperties.hxx"
25cdf0e10cSrcweir #include "oox/helper/helper.hxx"
26cdf0e10cSrcweir #include "oox/helper/propertymap.hxx"
27cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
28cdf0e10cSrcweir #include <com/sun/star/awt/Rectangle.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicTransformer.hpp>
32cdf0e10cSrcweir #include <com/sun/star/drawing/XShape.hpp>
33cdf0e10cSrcweir #include <com/sun/star/drawing/XEnhancedCustomShapeDefaulter.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using rtl::OUString;
36cdf0e10cSrcweir using namespace ::oox::core;
37cdf0e10cSrcweir using namespace ::com::sun::star;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star::beans;
40cdf0e10cSrcweir using namespace ::com::sun::star::graphic;
41cdf0e10cSrcweir using namespace ::com::sun::star::drawing;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace oox { namespace drawingml {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir CustomShapeProperties::CustomShapeProperties()
46cdf0e10cSrcweir : mbMirroredX	( sal_False )
47cdf0e10cSrcweir , mbMirroredY	( sal_False )
48*d742e3ecSArmin Le Grand , mnTextRotation(0) // #119920# Add missing extra text rotation
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
51cdf0e10cSrcweir CustomShapeProperties::~CustomShapeProperties()
52cdf0e10cSrcweir {
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir sal_Int32 CustomShapeProperties::SetCustomShapeGuideValue( std::vector< CustomShapeGuide >& rGuideList, const CustomShapeGuide& rGuide )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	sal_uInt32 nIndex = 0;
58cdf0e10cSrcweir 	for( ; nIndex < rGuideList.size(); nIndex++ )
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		if ( rGuideList[ nIndex ].maName == rGuide.maName )
61cdf0e10cSrcweir 			break;
62cdf0e10cSrcweir 	}
63cdf0e10cSrcweir 	if ( nIndex == rGuideList.size() )
64cdf0e10cSrcweir 		rGuideList.push_back( rGuide );
65cdf0e10cSrcweir 	return static_cast< sal_Int32 >( nIndex );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir // returns the index into the guidelist for a given formula name,
69cdf0e10cSrcweir // if the return value is < 0 then the guide value could not be found
70cdf0e10cSrcweir sal_Int32 CustomShapeProperties::GetCustomShapeGuideValue( const std::vector< CustomShapeGuide >& rGuideList, const rtl::OUString& rFormulaName )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	sal_Int32 nIndex = 0;
73cdf0e10cSrcweir 	for( ; nIndex < static_cast< sal_Int32 >( rGuideList.size() ); nIndex++ )
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		if ( rGuideList[ nIndex ].maName == rFormulaName )
76cdf0e10cSrcweir 			break;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 	if ( nIndex == static_cast< sal_Int32 >( rGuideList.size() ) )
79cdf0e10cSrcweir 		nIndex = -1;
80cdf0e10cSrcweir 	return nIndex;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir void CustomShapeProperties::apply( const CustomShapePropertiesPtr& /* rSourceCustomShapeProperties */ )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	// not sure if this needs to be implemented
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir void CustomShapeProperties::pushToPropSet( const ::oox::core::FilterBase& /* rFilterBase */,
89cdf0e10cSrcweir 	const Reference < XPropertySet >& xPropSet, const Reference < XShape > & xShape ) const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	if ( maShapePresetType.getLength() )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		//const uno::Reference < drawing::XShape > xShape( xPropSet, UNO_QUERY );
94cdf0e10cSrcweir         Reference< drawing::XEnhancedCustomShapeDefaulter > xDefaulter( xShape, UNO_QUERY );
95cdf0e10cSrcweir         if( xDefaulter.is() )
96cdf0e10cSrcweir             xDefaulter->createCustomShapeDefaults( maShapePresetType );
97cdf0e10cSrcweir 
98*d742e3ecSArmin Le Grand         PropertyMap aPropertyMap;
99*d742e3ecSArmin Le Grand 
100*d742e3ecSArmin Le Grand         aPropertyMap[ PROP_MirroredX ] <<= Any( mbMirroredX );
101*d742e3ecSArmin Le Grand         aPropertyMap[ PROP_MirroredY ] <<= Any( mbMirroredY );
102*d742e3ecSArmin Le Grand 
103*d742e3ecSArmin Le Grand         if(mnTextRotation)
104*d742e3ecSArmin Le Grand         {
105*d742e3ecSArmin Le Grand             // #119920# Handle missing text rotation
106*d742e3ecSArmin Le Grand             aPropertyMap[ PROP_TextRotateAngle ] <<= Any(mnTextRotation);
107*d742e3ecSArmin Le Grand         }
108*d742e3ecSArmin Le Grand 
109*d742e3ecSArmin Le Grand         // converting the vector to a sequence
110*d742e3ecSArmin Le Grand         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
111*d742e3ecSArmin Le Grand         PropertySet aPropSet( xPropSet );
112*d742e3ecSArmin Le Grand         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
113*d742e3ecSArmin Le Grand 
114cdf0e10cSrcweir         if ( maAdjustmentGuideList.size() )
115cdf0e10cSrcweir 		{
116cdf0e10cSrcweir 			const OUString sType = CREATE_OUSTRING( "Type" );
117cdf0e10cSrcweir 			const OUString sCustomShapeGeometry( RTL_CONSTASCII_USTRINGPARAM( "CustomShapeGeometry" ) );
118cdf0e10cSrcweir 			uno::Any aGeoPropSet = xPropSet->getPropertyValue( sCustomShapeGeometry );
119cdf0e10cSrcweir 			uno::Sequence< beans::PropertyValue > aGeoPropSeq;
120cdf0e10cSrcweir 			if ( aGeoPropSet >>= aGeoPropSeq )
121cdf0e10cSrcweir 			{
122cdf0e10cSrcweir 				sal_Int32 i, nCount = aGeoPropSeq.getLength();
123cdf0e10cSrcweir 				for ( i = 0; i < nCount; i++ )
124cdf0e10cSrcweir 				{
125cdf0e10cSrcweir 					const rtl::OUString sAdjustmentValues( RTL_CONSTASCII_USTRINGPARAM( "AdjustmentValues" ) );
126cdf0e10cSrcweir 					if ( aGeoPropSeq[ i ].Name.equals( sAdjustmentValues ) )
127cdf0e10cSrcweir 					{
128cdf0e10cSrcweir 						uno::Sequence< com::sun::star::drawing::EnhancedCustomShapeAdjustmentValue > aAdjustmentSeq;
129cdf0e10cSrcweir 						if ( aGeoPropSeq[ i ].Value >>= aAdjustmentSeq )
130cdf0e10cSrcweir 						{
131cdf0e10cSrcweir 							std::vector< CustomShapeGuide >::const_iterator aIter( maAdjustmentGuideList.begin() );
132cdf0e10cSrcweir 							while( aIter != maAdjustmentGuideList.end() )
133cdf0e10cSrcweir 							{
134cdf0e10cSrcweir 								if ( (*aIter).maName.getLength() > 3 )
135cdf0e10cSrcweir 								{
136cdf0e10cSrcweir 									sal_Int32 nAdjustmentIndex = (*aIter).maName.copy( 3 ).toInt32() - 1;
137cdf0e10cSrcweir 									if ( ( nAdjustmentIndex >= 0 ) && ( nAdjustmentIndex < aAdjustmentSeq.getLength() ) )
138cdf0e10cSrcweir 									{
139cdf0e10cSrcweir 										EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
140cdf0e10cSrcweir 										aAdjustmentVal.Value <<= (*aIter).maFormula.toInt32();
141cdf0e10cSrcweir 										aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
142cdf0e10cSrcweir 										aAdjustmentSeq[ nAdjustmentIndex ] = aAdjustmentVal;
143cdf0e10cSrcweir 									}
144cdf0e10cSrcweir 								}
145cdf0e10cSrcweir 								aIter++;
146cdf0e10cSrcweir 							}
147cdf0e10cSrcweir 							aGeoPropSeq[ i ].Value <<= aAdjustmentSeq;
148cdf0e10cSrcweir 							xPropSet->setPropertyValue( sCustomShapeGeometry, Any( aGeoPropSeq ) );
149cdf0e10cSrcweir 						}
150cdf0e10cSrcweir 					}
151cdf0e10cSrcweir 					else if ( aGeoPropSeq[ i ].Name.equals( sType ) )
152cdf0e10cSrcweir 					{
153cdf0e10cSrcweir 						aGeoPropSeq[ i ].Value <<= maShapePresetType;
154cdf0e10cSrcweir 					}
155cdf0e10cSrcweir 				}
156cdf0e10cSrcweir 			}
157cdf0e10cSrcweir 		}
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 	else
160cdf0e10cSrcweir 	{
161cdf0e10cSrcweir 		sal_uInt32 i;
162cdf0e10cSrcweir 		PropertyMap aPropertyMap;
163cdf0e10cSrcweir         aPropertyMap[ PROP_Type ] <<= CREATE_OUSTRING( "non-primitive" );
164cdf0e10cSrcweir 		aPropertyMap[ PROP_MirroredX ] <<= Any( mbMirroredX );
165cdf0e10cSrcweir 		aPropertyMap[ PROP_MirroredY ] <<= Any( mbMirroredY );
166*d742e3ecSArmin Le Grand 
167*d742e3ecSArmin Le Grand         if(mnTextRotation)
168*d742e3ecSArmin Le Grand         {
169*d742e3ecSArmin Le Grand             aPropertyMap[ PROP_TextRotation ] <<= Any(mnTextRotation);
170*d742e3ecSArmin Le Grand         }
171*d742e3ecSArmin Le Grand 
172cdf0e10cSrcweir 		awt::Size aSize( xShape->getSize() );
173cdf0e10cSrcweir 		awt::Rectangle aViewBox( 0, 0, aSize.Width * 360, aSize.Height * 360 );
174cdf0e10cSrcweir 		if ( maPath2DList.size() )
175cdf0e10cSrcweir 		{	// TODO: each polygon may have its own size, but I think it is rather been used
176cdf0e10cSrcweir 			// so we are only taking care of the first
177cdf0e10cSrcweir 			if ( maPath2DList[ 0 ].w )
178cdf0e10cSrcweir 				aViewBox.Width = static_cast< sal_Int32 >( maPath2DList[ 0 ].w );
179cdf0e10cSrcweir 			if ( maPath2DList[ 0 ].h )
180cdf0e10cSrcweir 				aViewBox.Height = static_cast< sal_Int32 >( maPath2DList[ 0 ].h );
181cdf0e10cSrcweir 		}
182cdf0e10cSrcweir 		aPropertyMap[ PROP_ViewBox ] <<= aViewBox;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 		Sequence< EnhancedCustomShapeAdjustmentValue > aAdjustmentValues( maAdjustmentGuideList.size() );
185cdf0e10cSrcweir 		for ( i = 0; i < maAdjustmentGuideList.size(); i++ )
186cdf0e10cSrcweir 		{
187cdf0e10cSrcweir 			EnhancedCustomShapeAdjustmentValue aAdjustmentVal;
188cdf0e10cSrcweir 			aAdjustmentVal.Value <<= maAdjustmentGuideList[ i ].maFormula.toInt32();
189cdf0e10cSrcweir 			aAdjustmentVal.State = PropertyState_DIRECT_VALUE;
190cdf0e10cSrcweir 			aAdjustmentValues[ i ] = aAdjustmentVal;
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		aPropertyMap[ PROP_AdjustmentValues ] <<= aAdjustmentValues;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 		Sequence< rtl::OUString > aEquations( maGuideList.size() );
195cdf0e10cSrcweir 		for ( i = 0; i < maGuideList.size(); i++ )
196cdf0e10cSrcweir 			aEquations[ i ] = maGuideList[ i ].maFormula;
197cdf0e10cSrcweir 		aPropertyMap[ PROP_Equations ] <<= aEquations;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		PropertyMap aPath;
200cdf0e10cSrcweir 		Sequence< EnhancedCustomShapeSegment > aSegments( maSegments.size() );
201cdf0e10cSrcweir 		for ( i = 0; i < maSegments.size(); i++ )
202cdf0e10cSrcweir 			aSegments[ i ] = maSegments[ i ];
203cdf0e10cSrcweir 		aPath[ PROP_Segments ] <<= aSegments;
204cdf0e10cSrcweir 		sal_uInt32 j, k, nParameterPairs = 0;
205cdf0e10cSrcweir 		for ( i = 0; i < maPath2DList.size(); i++ )
206cdf0e10cSrcweir 			nParameterPairs += maPath2DList[ i ].parameter.size();
207cdf0e10cSrcweir 		Sequence< EnhancedCustomShapeParameterPair > aParameterPairs( nParameterPairs );
208cdf0e10cSrcweir 		for ( i = 0, k = 0; i < maPath2DList.size(); i++ )
209cdf0e10cSrcweir 			for ( j = 0; j < maPath2DList[ i ].parameter.size(); j++ )
210cdf0e10cSrcweir 				aParameterPairs[ k++ ] = maPath2DList[ i ].parameter[ j ];
211cdf0e10cSrcweir 		aPath[ PROP_Coordinates ] <<= aParameterPairs;
212cdf0e10cSrcweir 		Sequence< PropertyValue > aPathSequence = aPath.makePropertyValueSequence();
213cdf0e10cSrcweir 		aPropertyMap[ PROP_Path ] <<= aPathSequence;
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 		Sequence< PropertyValues > aHandles( maAdjustHandleList.size() );
216cdf0e10cSrcweir 		for ( i = 0; i < maAdjustHandleList.size(); i++ )
217cdf0e10cSrcweir 		{
218cdf0e10cSrcweir 			PropertyMap aHandle;
219cdf0e10cSrcweir 			// maAdjustmentHandle[ i ].gdRef1 ... maAdjustmentHandle[ i ].gdRef2 ... :(
220cdf0e10cSrcweir 			// gdRef1 && gdRef2 -> we do not offer such reference, so it is difficult
221cdf0e10cSrcweir 			// to determine the correct adjustment handle that should be updated with the adjustment
222cdf0e10cSrcweir 			// position. here is the solution: the adjustment value that is used within the position
223cdf0e10cSrcweir 			// has to be updated, in case the position is a formula the first usage of a
224cdf0e10cSrcweir 			// adjument value is decisive
225cdf0e10cSrcweir 			if ( maAdjustHandleList[ i ].polar )
226cdf0e10cSrcweir 			{
227cdf0e10cSrcweir 				aHandle[ PROP_Position ] <<= maAdjustHandleList[ i ].pos;
228cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].min1.has() )
229cdf0e10cSrcweir 					aHandle[ PROP_RadiusRangeMinimum ] <<= maAdjustHandleList[ i ].min1.get();
230cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].max1.has() )
231cdf0e10cSrcweir 					aHandle[ PROP_RadiusRangeMaximum ] <<= maAdjustHandleList[ i ].max1.get();
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 				/* TODO: AngleMin & AngleMax
234cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].min2.has() )
235cdf0e10cSrcweir 					aHandle[ PROP_ ] = maAdjustHandleList[ i ].min2.get();
236cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].max2.has() )
237cdf0e10cSrcweir 					aHandle[ PROP_ ] = maAdjustHandleList[ i ].max2.get();
238cdf0e10cSrcweir 				*/
239cdf0e10cSrcweir 			}
240cdf0e10cSrcweir 			else
241cdf0e10cSrcweir 			{
242cdf0e10cSrcweir 				aHandle[ PROP_Position ] <<= maAdjustHandleList[ i ].pos;
243cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].gdRef1.has() )
244cdf0e10cSrcweir 				{
245cdf0e10cSrcweir 					// TODO: PROP_RefX and PROP_RefY are not yet part of our file format,
246cdf0e10cSrcweir 					// so the handles will not work after save/reload
247cdf0e10cSrcweir 					sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef1.get() );
248cdf0e10cSrcweir 					if ( nIndex >= 0 )
249cdf0e10cSrcweir 						aHandle[ PROP_RefX ] <<= nIndex;
250cdf0e10cSrcweir 				}
251cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].gdRef2.has() )
252cdf0e10cSrcweir 				{
253cdf0e10cSrcweir 					sal_Int32 nIndex = GetCustomShapeGuideValue( maAdjustmentGuideList, maAdjustHandleList[ i ].gdRef2.get() );
254cdf0e10cSrcweir 					if ( nIndex >= 0 )
255cdf0e10cSrcweir 						aHandle[ PROP_RefY ] <<= nIndex;
256cdf0e10cSrcweir 				}
257cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].min1.has() )
258cdf0e10cSrcweir 					aHandle[ PROP_RangeXMinimum ] <<= maAdjustHandleList[ i ].min1.get();
259cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].max1.has() )
260cdf0e10cSrcweir 					aHandle[ PROP_RangeXMaximum ] <<= maAdjustHandleList[ i ].max1.get();
261cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].min2.has() )
262cdf0e10cSrcweir 					aHandle[ PROP_RangeYMinimum ] <<= maAdjustHandleList[ i ].min2.get();
263cdf0e10cSrcweir 				if ( maAdjustHandleList[ i ].max2.has() )
264cdf0e10cSrcweir 					aHandle[ PROP_RangeYMaximum ] <<= maAdjustHandleList[ i ].max2.get();
265cdf0e10cSrcweir 			}
266cdf0e10cSrcweir 			aHandles[ i ] = aHandle.makePropertyValueSequence();
267cdf0e10cSrcweir 		}
268cdf0e10cSrcweir 		aPropertyMap[ PROP_Handles ] <<= aHandles;
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 		// converting the vector to a sequence
271cdf0e10cSrcweir         Sequence< PropertyValue > aSeq = aPropertyMap.makePropertyValueSequence();
272cdf0e10cSrcweir         PropertySet aPropSet( xPropSet );
273cdf0e10cSrcweir         aPropSet.setProperty( PROP_CustomShapeGeometry, aSeq );
274cdf0e10cSrcweir 	}
275cdf0e10cSrcweir }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir double CustomShapeProperties::getValue( const std::vector< CustomShapeGuide >& rGuideList, sal_uInt32 nIndex ) const
278cdf0e10cSrcweir {
279cdf0e10cSrcweir 	double fRet = 0.0;
280cdf0e10cSrcweir 	if ( nIndex < rGuideList.size() )
281cdf0e10cSrcweir 	{
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	}
284cdf0e10cSrcweir 	return fRet;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir } }
288