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 "TitleHelper.hxx"
21 : #include "ChartModelHelper.hxx"
22 : #include "macros.hxx"
23 : #include "AxisHelper.hxx"
24 : #include "DiagramHelper.hxx"
25 : #include <com/sun/star/chart2/XChartDocument.hpp>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : //.............................................................................
29 : namespace chart
30 : {
31 : //.............................................................................
32 :
33 : using namespace ::com::sun::star;
34 : using namespace ::com::sun::star::chart2;
35 : using ::com::sun::star::uno::Reference;
36 :
37 205 : uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
38 : TitleHelper::eTitleType nTitleIndex
39 : , const uno::Reference< XDiagram >& xDiagram )
40 : {
41 205 : uno::Reference< XTitled > xResult;
42 :
43 205 : if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION ||
44 : nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
45 : {
46 82 : bool bDummy = false;
47 82 : bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
48 :
49 82 : if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
50 41 : nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
51 : else
52 41 : nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
53 : }
54 :
55 :
56 205 : switch( nTitleIndex )
57 : {
58 : case TitleHelper::SUB_TITLE:
59 41 : if( xDiagram.is())
60 41 : xResult.set( xDiagram, uno::UNO_QUERY );
61 41 : break;
62 : case TitleHelper::X_AXIS_TITLE:
63 41 : if( xDiagram.is())
64 41 : xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
65 41 : break;
66 : case TitleHelper::Y_AXIS_TITLE:
67 41 : if( xDiagram.is())
68 41 : xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
69 41 : break;
70 : case TitleHelper::Z_AXIS_TITLE:
71 0 : if( xDiagram.is())
72 0 : xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
73 0 : break;
74 : case TitleHelper::SECONDARY_X_AXIS_TITLE:
75 41 : if( xDiagram.is())
76 41 : xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
77 41 : break;
78 : case TitleHelper::SECONDARY_Y_AXIS_TITLE:
79 41 : if( xDiagram.is())
80 41 : xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
81 41 : break;
82 :
83 : case TitleHelper::MAIN_TITLE:
84 : default:
85 : OSL_FAIL( "Unsupported Title-Type requested" );
86 0 : break;
87 : }
88 :
89 205 : return xResult;
90 : }
91 :
92 346 : uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
93 : , const uno::Reference< frame::XModel >& xModel )
94 : {
95 346 : uno::Reference< XTitled > xResult;
96 346 : uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
97 346 : uno::Reference< XDiagram > xDiagram;
98 346 : if( xChartDoc.is())
99 346 : xDiagram.set( xChartDoc->getFirstDiagram());
100 :
101 346 : switch( nTitleIndex )
102 : {
103 : case TitleHelper::MAIN_TITLE:
104 141 : xResult.set( xModel, uno::UNO_QUERY );
105 141 : break;
106 : case TitleHelper::SUB_TITLE:
107 : case TitleHelper::X_AXIS_TITLE:
108 : case TitleHelper::Y_AXIS_TITLE:
109 : case TitleHelper::Z_AXIS_TITLE:
110 : case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
111 : case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
112 : case TitleHelper::SECONDARY_X_AXIS_TITLE:
113 : case TitleHelper::SECONDARY_Y_AXIS_TITLE:
114 205 : xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
115 205 : break;
116 : default:
117 : OSL_FAIL( "Unsupported Title-Type requested" );
118 0 : break;
119 : }
120 :
121 346 : return xResult;
122 : }
123 :
124 346 : uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
125 : , const uno::Reference< frame::XModel >& xModel )
126 : {
127 346 : uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
128 346 : if( xTitled.is())
129 264 : return xTitled->getTitleObject();
130 82 : return NULL;
131 : }
132 :
133 0 : uno::Reference< XTitle > TitleHelper::createTitle(
134 : TitleHelper::eTitleType eTitleType
135 : , const rtl::OUString& rTitleText
136 : , const uno::Reference< frame::XModel >& xModel
137 : , const uno::Reference< uno::XComponentContext > & xContext
138 : , ReferenceSizeProvider * pRefSizeProvider )
139 : {
140 0 : uno::Reference< XTitle > xTitle;
141 0 : uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
142 :
143 0 : if( !xTitled.is() )
144 : {
145 0 : uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
146 0 : uno::Reference< chart2::XAxis > xAxis;
147 0 : switch( eTitleType )
148 : {
149 : case TitleHelper::SECONDARY_X_AXIS_TITLE:
150 0 : xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
151 0 : break;
152 : case TitleHelper::SECONDARY_Y_AXIS_TITLE:
153 0 : xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
154 0 : break;
155 : default:
156 0 : break;
157 : }
158 0 : uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
159 0 : if( xProps.is() )
160 : {
161 0 : xProps->setPropertyValue( C2U( "Show" ), uno::makeAny( sal_False ) );
162 0 : xTitled = lcl_getTitleParent( eTitleType, xModel );
163 0 : }
164 : }
165 :
166 0 : if(xTitled.is())
167 : {
168 0 : uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
169 :
170 0 : xTitle.set( xContext->getServiceManager()->createInstanceWithContext(
171 : C2U( "com.sun.star.chart2.Title" ),
172 0 : xContext ), uno::UNO_QUERY );
173 :
174 0 : if(xTitle.is())
175 : {
176 : // default char height (main: 13.0 == default)
177 0 : float fDefaultCharHeightSub = 11.0;
178 0 : float fDefaultCharHeightAxis = 9.0;
179 0 : switch( eTitleType )
180 : {
181 : case TitleHelper::SUB_TITLE:
182 : TitleHelper::setCompleteString(
183 0 : rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
184 0 : break;
185 : case TitleHelper::X_AXIS_TITLE:
186 : case TitleHelper::Y_AXIS_TITLE:
187 : case TitleHelper::Z_AXIS_TITLE:
188 : case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
189 : case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
190 : case TitleHelper::SECONDARY_X_AXIS_TITLE:
191 : case TitleHelper::SECONDARY_Y_AXIS_TITLE:
192 : TitleHelper::setCompleteString(
193 0 : rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
194 0 : break;
195 : default:
196 0 : TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
197 0 : break;
198 : }
199 :
200 : // set/clear autoscale
201 0 : if( pRefSizeProvider )
202 0 : pRefSizeProvider->setValuesAtTitle( xTitle );
203 :
204 0 : xTitled->setTitleObject( xTitle );
205 :
206 : //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
207 0 : if( eTitleType == TitleHelper::X_AXIS_TITLE ||
208 : eTitleType == TitleHelper::Y_AXIS_TITLE ||
209 : eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE ||
210 : eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE )
211 :
212 : {
213 : try
214 : {
215 0 : bool bDummy = false;
216 0 : bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
217 :
218 0 : Reference< beans::XPropertySet > xTitleProps( xTitle, uno::UNO_QUERY );
219 0 : if( xTitleProps.is() )
220 : {
221 0 : if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
222 : || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
223 0 : || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
224 : || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
225 : {
226 0 : double fNewAngleDegree = 90.0;
227 0 : xTitleProps->setPropertyValue( C2U( "TextRotation" ), uno::makeAny( fNewAngleDegree ));
228 : }
229 0 : }
230 : }
231 0 : catch( const uno::Exception & ex )
232 : {
233 : ASSERT_EXCEPTION( ex );
234 : }
235 : }
236 0 : }
237 : }
238 0 : return xTitle;
239 :
240 : }
241 :
242 246 : rtl::OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
243 : {
244 246 : rtl::OUString aRet;
245 246 : if(!xTitle.is())
246 : return aRet;
247 0 : uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
248 0 : for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
249 0 : aRet += aStringList[nN]->getString();
250 0 : return aRet;
251 : }
252 :
253 0 : void TitleHelper::setCompleteString( const rtl::OUString& rNewText
254 : , const uno::Reference< XTitle >& xTitle
255 : , const uno::Reference< uno::XComponentContext > & xContext
256 : , float * pDefaultCharHeight /* = 0 */ )
257 : {
258 : //the format of the first old text portion will be maintained if there is any
259 0 : if(!xTitle.is())
260 0 : return;
261 :
262 0 : rtl::OUString aNewText = rNewText;
263 :
264 0 : bool bStacked = false;
265 0 : uno::Reference< beans::XPropertySet > xTitleProperties( xTitle, uno::UNO_QUERY );
266 0 : if( xTitleProperties.is() )
267 0 : xTitleProperties->getPropertyValue( C2U( "StackCharacters" ) ) >>= bStacked;
268 :
269 0 : if( bStacked )
270 : {
271 : //#i99841# remove linebreaks that were added for vertical stacking
272 0 : rtl::OUStringBuffer aUnstackedStr;
273 0 : rtl::OUStringBuffer aSource(rNewText);
274 :
275 0 : bool bBreakIgnored = false;
276 0 : sal_Int32 nLen = rNewText.getLength();
277 0 : for( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
278 : {
279 0 : sal_Unicode aChar = aSource[nPos];
280 0 : if( aChar != '\n' )
281 : {
282 0 : aUnstackedStr.append( aChar );
283 0 : bBreakIgnored = false;
284 : }
285 0 : else if( aChar == '\n' && bBreakIgnored )
286 0 : aUnstackedStr.append( aChar );
287 : else
288 0 : bBreakIgnored = true;
289 : }
290 0 : aNewText = aUnstackedStr.makeStringAndClear();
291 : }
292 :
293 0 : uno::Sequence< uno::Reference< XFormattedString > > aNewStringList(1);
294 :
295 0 : uno::Sequence< uno::Reference< XFormattedString > > aOldStringList = xTitle->getText();
296 0 : if( aOldStringList.getLength() )
297 : {
298 0 : aNewStringList[0].set( aOldStringList[0] );
299 0 : aNewStringList[0]->setString( aNewText );
300 : }
301 : else
302 : {
303 : uno::Reference< uno::XInterface > xI(
304 0 : xContext->getServiceManager()->createInstanceWithContext(
305 0 : C2U( "com.sun.star.chart2.FormattedString" ), xContext ) );
306 0 : uno::Reference< XFormattedString > xFormattedString( xI, uno::UNO_QUERY );
307 :
308 0 : if(xFormattedString.is())
309 : {
310 0 : xFormattedString->setString( aNewText );
311 0 : aNewStringList[0].set( xFormattedString );
312 0 : if( pDefaultCharHeight != 0 )
313 : {
314 : try
315 : {
316 0 : uno::Reference< beans::XPropertySet > xProp( xFormattedString, uno::UNO_QUERY_THROW );
317 :
318 0 : uno::Any aFontSize( uno::makeAny( *pDefaultCharHeight ));
319 0 : xProp->setPropertyValue( C2U("CharHeight"), aFontSize );
320 0 : xProp->setPropertyValue( C2U("CharHeightAsian"), aFontSize );
321 0 : xProp->setPropertyValue( C2U("CharHeightComplex"), aFontSize );
322 : }
323 0 : catch( const uno::Exception & ex )
324 : {
325 : ASSERT_EXCEPTION( ex );
326 : }
327 : }
328 0 : }
329 : }
330 0 : xTitle->setText( aNewStringList );
331 : }
332 :
333 0 : void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
334 : , const ::com::sun::star::uno::Reference<
335 : ::com::sun::star::frame::XModel >& xModel )
336 : {
337 0 : uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
338 0 : if( xTitled.is())
339 : {
340 0 : xTitled->setTitleObject(NULL);
341 0 : }
342 0 : }
343 :
344 20 : bool TitleHelper::getTitleType( eTitleType& rType
345 : , const ::com::sun::star::uno::Reference<
346 : ::com::sun::star::chart2::XTitle >& xTitle
347 : , const ::com::sun::star::uno::Reference<
348 : ::com::sun::star::frame::XModel >& xModel )
349 : {
350 20 : if( !xTitle.is() || !xModel.is() )
351 0 : return false;
352 :
353 20 : Reference< chart2::XTitle > xCurrentTitle;
354 20 : for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
355 : {
356 20 : xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
357 20 : if( xCurrentTitle == xTitle )
358 : {
359 20 : rType = static_cast<eTitleType>(nTitleType);
360 20 : return true;
361 : }
362 : }
363 :
364 0 : return false;
365 : }
366 :
367 : //.............................................................................
368 : } //namespace chart
369 : //.............................................................................
370 :
371 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|