bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / TitleHelper.cxx
bloba8a0b0074990edf4d51476712ff399af495d671c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
29 //.............................................................................
30 namespace chart
32 //.............................................................................
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::chart2;
36 using ::com::sun::star::uno::Reference;
38 uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
39 TitleHelper::eTitleType nTitleIndex
40 , const uno::Reference< XDiagram >& xDiagram )
42 uno::Reference< XTitled > xResult;
44 if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION ||
45 nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
47 bool bDummy = false;
48 bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
50 if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
51 nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
52 else
53 nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
57 switch( nTitleIndex )
59 case TitleHelper::SUB_TITLE:
60 if( xDiagram.is())
61 xResult.set( xDiagram, uno::UNO_QUERY );
62 break;
63 case TitleHelper::X_AXIS_TITLE:
64 if( xDiagram.is())
65 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
66 break;
67 case TitleHelper::Y_AXIS_TITLE:
68 if( xDiagram.is())
69 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
70 break;
71 case TitleHelper::Z_AXIS_TITLE:
72 if( xDiagram.is())
73 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
74 break;
75 case TitleHelper::SECONDARY_X_AXIS_TITLE:
76 if( xDiagram.is())
77 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
78 break;
79 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
80 if( xDiagram.is())
81 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
82 break;
84 case TitleHelper::MAIN_TITLE:
85 default:
86 OSL_FAIL( "Unsupported Title-Type requested" );
87 break;
90 return xResult;
93 uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
94 , const uno::Reference< frame::XModel >& xModel )
96 uno::Reference< XTitled > xResult;
97 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
98 uno::Reference< XDiagram > xDiagram;
99 if( xChartDoc.is())
100 xDiagram.set( xChartDoc->getFirstDiagram());
102 switch( nTitleIndex )
104 case TitleHelper::MAIN_TITLE:
105 xResult.set( xModel, uno::UNO_QUERY );
106 break;
107 case TitleHelper::SUB_TITLE:
108 case TitleHelper::X_AXIS_TITLE:
109 case TitleHelper::Y_AXIS_TITLE:
110 case TitleHelper::Z_AXIS_TITLE:
111 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
112 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
113 case TitleHelper::SECONDARY_X_AXIS_TITLE:
114 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
115 xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
116 break;
117 default:
118 OSL_FAIL( "Unsupported Title-Type requested" );
119 break;
122 return xResult;
125 uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
126 , const uno::Reference< frame::XModel >& xModel )
128 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
129 if( xTitled.is())
130 return xTitled->getTitleObject();
131 return NULL;
134 uno::Reference< XTitle > TitleHelper::createTitle(
135 TitleHelper::eTitleType eTitleType
136 , const OUString& rTitleText
137 , const uno::Reference< frame::XModel >& xModel
138 , const uno::Reference< uno::XComponentContext > & xContext
139 , ReferenceSizeProvider * pRefSizeProvider )
141 uno::Reference< XTitle > xTitle;
142 uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
144 if( !xTitled.is() )
146 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
147 uno::Reference< chart2::XAxis > xAxis;
148 switch( eTitleType )
150 case TitleHelper::SECONDARY_X_AXIS_TITLE:
151 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
152 break;
153 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
154 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
155 break;
156 default:
157 break;
159 uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
160 if( xProps.is() )
162 xProps->setPropertyValue( "Show", uno::makeAny( sal_False ) );
163 xTitled = lcl_getTitleParent( eTitleType, xModel );
167 if(xTitled.is())
169 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
171 xTitle.set( xContext->getServiceManager()->createInstanceWithContext(
172 "com.sun.star.chart2.Title",
173 xContext ), uno::UNO_QUERY );
175 if(xTitle.is())
177 // default char height (main: 13.0 == default)
178 float fDefaultCharHeightSub = 11.0;
179 float fDefaultCharHeightAxis = 9.0;
180 switch( eTitleType )
182 case TitleHelper::SUB_TITLE:
183 TitleHelper::setCompleteString(
184 rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
185 break;
186 case TitleHelper::X_AXIS_TITLE:
187 case TitleHelper::Y_AXIS_TITLE:
188 case TitleHelper::Z_AXIS_TITLE:
189 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
190 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
191 case TitleHelper::SECONDARY_X_AXIS_TITLE:
192 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
193 TitleHelper::setCompleteString(
194 rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
195 break;
196 default:
197 TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
198 break;
201 // set/clear autoscale
202 if( pRefSizeProvider )
203 pRefSizeProvider->setValuesAtTitle( xTitle );
205 xTitled->setTitleObject( xTitle );
207 //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
208 if( eTitleType == TitleHelper::X_AXIS_TITLE ||
209 eTitleType == TitleHelper::Y_AXIS_TITLE ||
210 eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE ||
211 eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE )
216 bool bDummy = false;
217 bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
219 Reference< beans::XPropertySet > xTitleProps( xTitle, uno::UNO_QUERY );
220 if( xTitleProps.is() )
222 if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
223 || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
224 || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
225 || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
227 double fNewAngleDegree = 90.0;
228 xTitleProps->setPropertyValue( "TextRotation", uno::makeAny( fNewAngleDegree ));
232 catch( const uno::Exception & ex )
234 ASSERT_EXCEPTION( ex );
239 return xTitle;
243 OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
245 OUString aRet;
246 if(!xTitle.is())
247 return aRet;
248 uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
249 for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
250 aRet += aStringList[nN]->getString();
251 return aRet;
254 void TitleHelper::setCompleteString( const OUString& rNewText
255 , const uno::Reference< XTitle >& xTitle
256 , const uno::Reference< uno::XComponentContext > & xContext
257 , float * pDefaultCharHeight /* = 0 */ )
259 //the format of the first old text portion will be maintained if there is any
260 if(!xTitle.is())
261 return;
263 OUString aNewText = rNewText;
265 bool bStacked = false;
266 uno::Reference< beans::XPropertySet > xTitleProperties( xTitle, uno::UNO_QUERY );
267 if( xTitleProperties.is() )
268 xTitleProperties->getPropertyValue( "StackCharacters" ) >>= bStacked;
270 if( bStacked )
272 //#i99841# remove linebreaks that were added for vertical stacking
273 OUStringBuffer aUnstackedStr;
274 OUStringBuffer aSource(rNewText);
276 bool bBreakIgnored = false;
277 sal_Int32 nLen = rNewText.getLength();
278 for( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
280 sal_Unicode aChar = aSource[nPos];
281 if( aChar != '\n' )
283 aUnstackedStr.append( aChar );
284 bBreakIgnored = false;
286 else if( aChar == '\n' && bBreakIgnored )
287 aUnstackedStr.append( aChar );
288 else
289 bBreakIgnored = true;
291 aNewText = aUnstackedStr.makeStringAndClear();
294 uno::Sequence< uno::Reference< XFormattedString > > aNewStringList(1);
296 uno::Sequence< uno::Reference< XFormattedString > > aOldStringList = xTitle->getText();
297 if( aOldStringList.getLength() )
299 aNewStringList[0].set( aOldStringList[0] );
300 aNewStringList[0]->setString( aNewText );
302 else
304 uno::Reference< chart2::XFormattedString2 > xFormattedString =
305 chart2::FormattedString::create( xContext );
307 xFormattedString->setString( aNewText );
308 aNewStringList[0].set( xFormattedString );
309 if( pDefaultCharHeight != 0 )
313 uno::Any aFontSize( uno::makeAny( *pDefaultCharHeight ));
314 xFormattedString->setPropertyValue( "CharHeight", aFontSize );
315 xFormattedString->setPropertyValue( "CharHeightAsian", aFontSize );
316 xFormattedString->setPropertyValue( "CharHeightComplex", aFontSize );
318 catch( const uno::Exception & ex )
320 ASSERT_EXCEPTION( ex );
324 xTitle->setText( aNewStringList );
327 void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
328 , const ::com::sun::star::uno::Reference<
329 ::com::sun::star::frame::XModel >& xModel )
331 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
332 if( xTitled.is())
334 xTitled->setTitleObject(NULL);
338 bool TitleHelper::getTitleType( eTitleType& rType
339 , const ::com::sun::star::uno::Reference<
340 ::com::sun::star::chart2::XTitle >& xTitle
341 , const ::com::sun::star::uno::Reference<
342 ::com::sun::star::frame::XModel >& xModel )
344 if( !xTitle.is() || !xModel.is() )
345 return false;
347 Reference< chart2::XTitle > xCurrentTitle;
348 for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
350 xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
351 if( xCurrentTitle == xTitle )
353 rType = static_cast<eTitleType>(nTitleType);
354 return true;
358 return false;
361 //.............................................................................
362 } //namespace chart
363 //.............................................................................
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */