Branch data Line data Source code
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 "VTitle.hxx"
21 : : #include "CommonConverters.hxx"
22 : : #include "macros.hxx"
23 : : #include "PropertyMapper.hxx"
24 : : #include "ShapeFactory.hxx"
25 : : #include "RelativeSizeHelper.hxx"
26 : : #include <com/sun/star/chart2/XFormattedString.hpp>
27 : : #include <rtl/math.hxx>
28 : : #include <com/sun/star/beans/XPropertySet.hpp>
29 : : #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
30 : : #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
31 : : #include <com/sun/star/text/ControlCharacter.hpp>
32 : : #include <com/sun/star/text/XText.hpp>
33 : : #include <com/sun/star/text/XTextCursor.hpp>
34 : :
35 : : //.............................................................................
36 : : namespace chart
37 : : {
38 : : //.............................................................................
39 : : using namespace ::com::sun::star;
40 : : using namespace ::com::sun::star::chart2;
41 : :
42 : 916 : VTitle::VTitle( const uno::Reference< XTitle > & xTitle )
43 : : : m_xTarget(NULL)
44 : : , m_xShapeFactory(NULL)
45 : : , m_xTitle(xTitle)
46 : : , m_xShape(NULL)
47 : : , m_aCID()
48 : : , m_fRotationAngleDegree(0.0)
49 : : , m_nXPos(0)
50 [ + - ][ + - ]: 916 : , m_nYPos(0)
51 : : {
52 : 916 : }
53 : :
54 : 916 : VTitle::~VTitle()
55 : : {
56 [ - + ]: 1832 : }
57 : :
58 : 916 : void VTitle::init(
59 : : const uno::Reference< drawing::XShapes >& xTargetPage
60 : : , const uno::Reference< lang::XMultiServiceFactory >& xFactory
61 : : , const rtl::OUString& rCID )
62 : : {
63 : 916 : m_xTarget = xTargetPage;
64 : 916 : m_xShapeFactory = xFactory;
65 : 916 : m_aCID = rCID;
66 : 916 : }
67 : :
68 : 712 : double VTitle::getRotationAnglePi() const
69 : : {
70 : 712 : return m_fRotationAngleDegree*F_PI/180.0;
71 : : }
72 : :
73 : 916 : awt::Size VTitle::getUnrotatedSize() const //size before rotation
74 : : {
75 : 916 : awt::Size aRet;
76 [ + - ]: 916 : if(m_xShape.is())
77 : 916 : aRet = m_xShape->getSize();
78 : 916 : return aRet;
79 : : }
80 : :
81 : 916 : awt::Size VTitle::getFinalSize() const //size after rotation
82 : : {
83 : : return ShapeFactory::getSizeAfterRotation(
84 : 916 : m_xShape, m_fRotationAngleDegree );
85 : : }
86 : :
87 : 916 : void VTitle::changePosition( const awt::Point& rPos )
88 : : {
89 [ + - ]: 916 : if(!m_xShape.is())
90 : : return;
91 [ + - ]: 916 : uno::Reference< beans::XPropertySet > xShapeProp( m_xShape, uno::UNO_QUERY );
92 [ - + ]: 916 : if(!xShapeProp.is())
93 : : return;
94 : : try
95 : : {
96 : 916 : m_nXPos = rPos.X;
97 : 916 : m_nYPos = rPos.Y;
98 : :
99 : : //set position matrix
100 : : //the matrix needs to be set at the end behind autogrow and such position influencing properties
101 [ + - ]: 916 : ::basegfx::B2DHomMatrix aM;
102 [ + - ]: 916 : aM.rotate( -m_fRotationAngleDegree*F_PI/180.0 );//#i78696#->#i80521#
103 [ + - ]: 916 : aM.translate( m_nXPos, m_nYPos);
104 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "Transformation" ), uno::makeAny( B2DHomMatrixToHomogenMatrix3(aM) ) );
[ + - ][ + - ]
[ + - ][ + - ]
[ # # ]
105 : : }
106 [ # # ]: 0 : catch( const uno::Exception& e )
107 : : {
108 : : ASSERT_EXCEPTION( e );
109 [ + - ]: 916 : }
110 : : }
111 : :
112 : 916 : void VTitle::createShapes(
113 : : const awt::Point& rPos
114 : : , const awt::Size& rReferenceSize )
115 : : {
116 : : try
117 : : {
118 [ + - ]: 916 : if(!m_xTitle.is())
119 : : return;
120 : :
121 [ + - ][ + - ]: 916 : uno::Sequence< uno::Reference< XFormattedString > > aStringList = m_xTitle->getText();
122 [ - + ]: 916 : if(aStringList.getLength()<=0)
123 : : return;
124 : :
125 : : //create shape and add to page
126 : : uno::Reference< drawing::XShape > xShape(
127 [ + - ]: 916 : m_xShapeFactory->createInstance( C2U(
128 [ + - ][ + - ]: 916 : "com.sun.star.drawing.TextShape" ) ), uno::UNO_QUERY );
[ + - ]
129 [ + - ][ + - ]: 916 : m_xTarget->add(xShape);
130 [ + - ]: 916 : m_xShape = xShape;
131 : :
132 : : //set text and text properties
133 [ + - ]: 916 : uno::Reference< text::XText > xText( xShape, uno::UNO_QUERY );
134 [ + - ][ + - ]: 916 : uno::Reference< text::XTextCursor > xTextCursor( xText->createTextCursor() );
135 [ + - ]: 916 : uno::Reference< text::XTextRange > xTextRange( xTextCursor, uno::UNO_QUERY );
136 [ + - ]: 916 : uno::Reference< beans::XPropertySet > xShapeProp( xShape, uno::UNO_QUERY );
137 [ + - ]: 916 : uno::Reference< beans::XPropertySet > xTitleProperties( m_xTitle, uno::UNO_QUERY );
138 [ + - ][ + - ]: 916 : if( !xText.is() || !xTextRange.is() || !xTextCursor.is() || !xShapeProp.is() || !xTitleProperties.is() )
[ + - ][ + - ]
[ - + ][ - + ]
139 : : return;
140 : :
141 [ + - ]: 916 : tPropertyNameValueMap aValueMap;
142 : : //fill line-, fill- and paragraph-properties into the ValueMap
143 : : {
144 [ + - ][ + - ]: 916 : tMakePropertyNameMap aNameMap = PropertyMapper::getPropertyNameMapForParagraphProperties();
145 [ + - ][ + - ]: 916 : aNameMap( PropertyMapper::getPropertyNameMapForFillAndLineProperties() );
146 : :
147 [ + - ]: 916 : PropertyMapper::getValueMap( aValueMap, aNameMap, xTitleProperties );
148 : : }
149 : :
150 : : //fill some more shape properties into the ValueMap
151 : : {
152 : 916 : drawing::TextHorizontalAdjust eHorizontalAdjust = drawing::TextHorizontalAdjust_CENTER;
153 : 916 : drawing::TextVerticalAdjust eVerticalAdjust = drawing::TextVerticalAdjust_CENTER;
154 : :
155 [ + - ][ + - ]: 916 : aValueMap.insert( tPropertyNameValueMap::value_type( C2U("TextHorizontalAdjust"), uno::makeAny(eHorizontalAdjust) ) ); // drawing::TextHorizontalAdjust
[ + - ]
156 [ + - ][ + - ]: 916 : aValueMap.insert( tPropertyNameValueMap::value_type( C2U("TextVerticalAdjust"), uno::makeAny(eVerticalAdjust) ) ); //drawing::TextVerticalAdjust
[ + - ]
157 [ + - ][ + - ]: 916 : aValueMap.insert( tPropertyNameValueMap::value_type( C2U("TextAutoGrowHeight"), uno::makeAny(sal_True) ) ); // sal_Bool
[ + - ]
158 [ + - ][ + - ]: 916 : aValueMap.insert( tPropertyNameValueMap::value_type( C2U("TextAutoGrowWidth"), uno::makeAny(sal_True) ) ); // sal_Bool
[ + - ]
159 : :
160 : : //set name/classified ObjectID (CID)
161 [ + - ]: 916 : if( !m_aCID.isEmpty() )
162 [ + - ][ + - ]: 916 : aValueMap.insert( tPropertyNameValueMap::value_type( C2U("Name"), uno::makeAny( m_aCID ) ) ); //CID rtl::OUString
[ + - ]
163 : : }
164 : :
165 : : //set global title properties
166 : : {
167 [ + - ]: 916 : tNameSequence aPropNames;
168 [ + - ]: 916 : tAnySequence aPropValues;
169 [ + - ]: 916 : PropertyMapper::getMultiPropertyListsFromValueMap( aPropNames, aPropValues, aValueMap );
170 [ + - ][ + - ]: 916 : PropertyMapper::setMultiProperties( aPropNames, aPropValues, xShapeProp );
[ + - ]
171 : : }
172 : :
173 : 916 : sal_Bool bStackCharacters(sal_False);
174 : : try
175 : : {
176 [ + - ][ + - ]: 916 : xTitleProperties->getPropertyValue( C2U( "StackCharacters" ) ) >>= bStackCharacters;
[ + - ][ # # ]
177 : : }
178 [ # # ]: 0 : catch( const uno::Exception& e )
179 : : {
180 : : ASSERT_EXCEPTION( e );
181 : : }
182 [ + + ]: 916 : if(bStackCharacters)
183 : : {
184 : : //if the characters should be stacked we use only the first character properties for code simplicity
185 [ + - ]: 81 : if( aStringList.getLength()>0 )
186 : : {
187 : 81 : rtl::OUString aLabel;
188 [ + + ]: 162 : for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
189 [ + - ][ + - ]: 81 : aLabel += aStringList[nN]->getString();
[ + - ]
190 [ + - ]: 81 : aLabel = ShapeFactory::getStackedString( aLabel, bStackCharacters );
191 : :
192 [ + - ][ + - ]: 81 : xTextCursor->gotoEnd(false);
193 [ + - ][ + - ]: 81 : xText->insertString( xTextRange, aLabel, false );
194 [ + - ][ + - ]: 81 : xTextCursor->gotoEnd(true);
195 [ + - ]: 81 : uno::Reference< beans::XPropertySet > xTargetProps( xShape, uno::UNO_QUERY );
196 [ + - ][ + - ]: 81 : uno::Reference< beans::XPropertySet > xSourceProps( aStringList[0], uno::UNO_QUERY );
197 : :
198 : : PropertyMapper::setMappedProperties( xTargetProps, xSourceProps
199 [ + - ][ + - ]: 81 : , PropertyMapper::getPropertyNameMapForCharacterProperties() );
200 : :
201 : : // adapt font size according to page size
202 : 81 : awt::Size aOldRefSize;
203 [ + - ][ + - ]: 81 : if( xTitleProperties->getPropertyValue( C2U("ReferencePageSize")) >>= aOldRefSize )
[ + - ][ - + ]
[ + - ]
204 : : {
205 [ # # ]: 0 : RelativeSizeHelper::adaptFontSizes( xTargetProps, aOldRefSize, rReferenceSize );
206 : 81 : }
207 : : }
208 : : }
209 : : else
210 : : {
211 [ + - ]: 835 : uno::Sequence< uno::Reference< text::XTextCursor > > aCursorList( aStringList.getLength() );
212 : 835 : sal_Int32 nN = 0;
213 [ + + ]: 1670 : for( nN=0; nN<aStringList.getLength();nN++ )
214 : : {
215 [ + - ][ + - ]: 835 : xTextCursor->gotoEnd(false);
216 [ + - ][ + - ]: 835 : xText->insertString( xTextRange, aStringList[nN]->getString(), false );
[ + - ][ + - ]
[ + - ]
217 [ + - ][ + - ]: 835 : xTextCursor->gotoEnd(true);
218 [ + - ][ + - ]: 835 : aCursorList[nN] = xText->createTextCursorByRange( uno::Reference< text::XTextRange >(xTextCursor,uno::UNO_QUERY) );
[ + - ][ + - ]
[ + - ]
219 : : }
220 : 835 : awt::Size aOldRefSize;
221 : : bool bHasRefPageSize =
222 [ + - ][ + - ]: 835 : ( xTitleProperties->getPropertyValue( C2U("ReferencePageSize")) >>= aOldRefSize );
[ + - ][ + - ]
223 : :
224 [ + - ]: 835 : if( aStringList.getLength()>0 )
225 : : {
226 [ + - ]: 835 : uno::Reference< beans::XPropertySet > xTargetProps( xShape, uno::UNO_QUERY );
227 [ + - ][ + - ]: 835 : uno::Reference< beans::XPropertySet > xSourceProps( aStringList[0], uno::UNO_QUERY );
228 [ + - ][ + - ]: 835 : PropertyMapper::setMappedProperties( xTargetProps, xSourceProps, PropertyMapper::getPropertyNameMapForCharacterProperties() );
229 : :
230 : : // adapt font size according to page size
231 [ - + ]: 835 : if( bHasRefPageSize )
232 : : {
233 [ # # ]: 0 : RelativeSizeHelper::adaptFontSizes( xTargetProps, aOldRefSize, rReferenceSize );
234 : 835 : }
235 [ + - ]: 835 : }
236 : : }
237 : :
238 : : // #i109336# Improve auto positioning in chart
239 : 916 : float fFontHeight = 0.0;
240 [ + - ][ + - ]: 916 : if ( xShapeProp.is() && ( xShapeProp->getPropertyValue( C2U( "CharHeight" ) ) >>= fFontHeight ) )
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + -
# # # # ]
241 : : {
242 : 916 : fFontHeight *= ( 2540.0f / 72.0f ); // pt -> 1/100 mm
243 : 916 : float fXFraction = 0.18f;
244 : 916 : sal_Int32 nXDistance = static_cast< sal_Int32 >( ::rtl::math::round( fFontHeight * fXFraction ) );
245 : 916 : float fYFraction = 0.30f;
246 : 916 : sal_Int32 nYDistance = static_cast< sal_Int32 >( ::rtl::math::round( fFontHeight * fYFraction ) );
247 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "TextLeftDistance" ), uno::makeAny( nXDistance ) );
[ + - ][ + - ]
248 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "TextRightDistance" ), uno::makeAny( nXDistance ) );
[ + - ][ + - ]
249 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "TextUpperDistance" ), uno::makeAny( nYDistance ) );
[ + - ][ + - ]
250 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "TextLowerDistance" ), uno::makeAny( nYDistance ) );
[ + - ][ + - ]
251 : : }
252 : :
253 : : try
254 : : {
255 : 916 : double fAngleDegree = 0;
256 [ + - ][ + - ]: 916 : xTitleProperties->getPropertyValue( C2U( "TextRotation" ) ) >>= fAngleDegree;
[ + - ][ # # ]
257 : 916 : m_fRotationAngleDegree += fAngleDegree;
258 : : }
259 [ # # ]: 0 : catch( const uno::Exception& e )
260 : : {
261 : : ASSERT_EXCEPTION( e );
262 : : }
263 : 916 : m_nXPos = rPos.X;
264 : 916 : m_nYPos = rPos.Y;
265 : :
266 : : //set position matrix
267 : : //the matrix needs to be set at the end behind autogrow and such position influencing properties
268 [ + - ]: 916 : ::basegfx::B2DHomMatrix aM;
269 [ + - ]: 916 : aM.rotate( -m_fRotationAngleDegree*F_PI/180.0 );//#i78696#->#i80521#
270 [ + - ]: 916 : aM.translate( m_nXPos, m_nYPos );
271 [ + - ][ + - ]: 916 : xShapeProp->setPropertyValue( C2U( "Transformation" ), uno::makeAny( B2DHomMatrixToHomogenMatrix3(aM) ) );
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ - + ]
[ - + ][ - + ]
[ - + ][ - + ]
[ + - ][ + - ]
[ # # ]
272 : : }
273 : 0 : catch( const uno::Exception& e )
274 : : {
275 : : ASSERT_EXCEPTION( e );
276 : : }
277 : : }
278 : :
279 : : //.............................................................................
280 : : } //namespace chart
281 : : //.............................................................................
282 : :
283 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|