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