Bump for 3.6-28
[LibreOffice.git] / chart2 / source / tools / TitleHelper.cxx
blob7a7a6ae585b2483e3f4ad80249dbb297e777bbe4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "TitleHelper.hxx"
30 #include "ChartModelHelper.hxx"
31 #include "macros.hxx"
32 #include "AxisHelper.hxx"
33 #include "DiagramHelper.hxx"
34 #include <com/sun/star/chart2/XChartDocument.hpp>
35 #include <rtl/ustrbuf.hxx>
37 //.............................................................................
38 namespace chart
40 //.............................................................................
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::chart2;
44 using ::com::sun::star::uno::Reference;
46 uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
47 TitleHelper::eTitleType nTitleIndex
48 , const uno::Reference< XDiagram >& xDiagram )
50 uno::Reference< XTitled > xResult;
52 if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION ||
53 nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
55 bool bDummy = false;
56 bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
58 if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
59 nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
60 else
61 nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
65 switch( nTitleIndex )
67 case TitleHelper::SUB_TITLE:
68 if( xDiagram.is())
69 xResult.set( xDiagram, uno::UNO_QUERY );
70 break;
71 case TitleHelper::X_AXIS_TITLE:
72 if( xDiagram.is())
73 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
74 break;
75 case TitleHelper::Y_AXIS_TITLE:
76 if( xDiagram.is())
77 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
78 break;
79 case TitleHelper::Z_AXIS_TITLE:
80 if( xDiagram.is())
81 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
82 break;
83 case TitleHelper::SECONDARY_X_AXIS_TITLE:
84 if( xDiagram.is())
85 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
86 break;
87 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
88 if( xDiagram.is())
89 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
90 break;
92 case TitleHelper::MAIN_TITLE:
93 default:
94 OSL_FAIL( "Unsupported Title-Type requested" );
95 break;
98 return xResult;
101 uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
102 , const uno::Reference< frame::XModel >& xModel )
104 uno::Reference< XTitled > xResult;
105 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
106 uno::Reference< XDiagram > xDiagram;
107 if( xChartDoc.is())
108 xDiagram.set( xChartDoc->getFirstDiagram());
110 switch( nTitleIndex )
112 case TitleHelper::MAIN_TITLE:
113 xResult.set( xModel, uno::UNO_QUERY );
114 break;
115 case TitleHelper::SUB_TITLE:
116 case TitleHelper::X_AXIS_TITLE:
117 case TitleHelper::Y_AXIS_TITLE:
118 case TitleHelper::Z_AXIS_TITLE:
119 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
120 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
121 case TitleHelper::SECONDARY_X_AXIS_TITLE:
122 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
123 xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
124 break;
125 default:
126 OSL_FAIL( "Unsupported Title-Type requested" );
127 break;
130 return xResult;
133 uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
134 , const uno::Reference< frame::XModel >& xModel )
136 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
137 if( xTitled.is())
138 return xTitled->getTitleObject();
139 return NULL;
142 uno::Reference< XTitle > TitleHelper::createTitle(
143 TitleHelper::eTitleType eTitleType
144 , const rtl::OUString& rTitleText
145 , const uno::Reference< frame::XModel >& xModel
146 , const uno::Reference< uno::XComponentContext > & xContext
147 , ReferenceSizeProvider * pRefSizeProvider )
149 uno::Reference< XTitle > xTitle;
150 uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
152 if( !xTitled.is() )
154 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
155 uno::Reference< chart2::XAxis > xAxis;
156 switch( eTitleType )
158 case TitleHelper::SECONDARY_X_AXIS_TITLE:
159 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
160 break;
161 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
162 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
163 break;
164 default:
165 break;
167 uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
168 if( xProps.is() )
170 xProps->setPropertyValue( C2U( "Show" ), uno::makeAny( sal_False ) );
171 xTitled = lcl_getTitleParent( eTitleType, xModel );
175 if(xTitled.is())
177 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
179 xTitle.set( xContext->getServiceManager()->createInstanceWithContext(
180 C2U( "com.sun.star.chart2.Title" ),
181 xContext ), uno::UNO_QUERY );
183 if(xTitle.is())
185 // default char height (main: 13.0 == default)
186 float fDefaultCharHeightSub = 11.0;
187 float fDefaultCharHeightAxis = 9.0;
188 switch( eTitleType )
190 case TitleHelper::SUB_TITLE:
191 TitleHelper::setCompleteString(
192 rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
193 break;
194 case TitleHelper::X_AXIS_TITLE:
195 case TitleHelper::Y_AXIS_TITLE:
196 case TitleHelper::Z_AXIS_TITLE:
197 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
198 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
199 case TitleHelper::SECONDARY_X_AXIS_TITLE:
200 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
201 TitleHelper::setCompleteString(
202 rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
203 break;
204 default:
205 TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
206 break;
209 // set/clear autoscale
210 if( pRefSizeProvider )
211 pRefSizeProvider->setValuesAtTitle( xTitle );
213 xTitled->setTitleObject( xTitle );
215 //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
216 if( eTitleType == TitleHelper::X_AXIS_TITLE ||
217 eTitleType == TitleHelper::Y_AXIS_TITLE ||
218 eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE ||
219 eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE )
224 bool bDummy = false;
225 bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
227 Reference< beans::XPropertySet > xTitleProps( xTitle, uno::UNO_QUERY );
228 if( xTitleProps.is() )
230 if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
231 || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
232 || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
233 || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
235 double fNewAngleDegree = 90.0;
236 xTitleProps->setPropertyValue( C2U( "TextRotation" ), uno::makeAny( fNewAngleDegree ));
240 catch( const uno::Exception & ex )
242 ASSERT_EXCEPTION( ex );
247 return xTitle;
251 rtl::OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
253 rtl::OUString aRet;
254 if(!xTitle.is())
255 return aRet;
256 uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
257 for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
258 aRet += aStringList[nN]->getString();
259 return aRet;
262 void TitleHelper::setCompleteString( const rtl::OUString& rNewText
263 , const uno::Reference< XTitle >& xTitle
264 , const uno::Reference< uno::XComponentContext > & xContext
265 , float * pDefaultCharHeight /* = 0 */ )
267 //the format of the first old text portion will be maintained if there is any
268 if(!xTitle.is())
269 return;
271 rtl::OUString aNewText = rNewText;
273 bool bStacked = false;
274 uno::Reference< beans::XPropertySet > xTitleProperties( xTitle, uno::UNO_QUERY );
275 if( xTitleProperties.is() )
276 xTitleProperties->getPropertyValue( C2U( "StackCharacters" ) ) >>= bStacked;
278 if( bStacked )
280 //#i99841# remove linebreaks that were added for vertical stacking
281 rtl::OUStringBuffer aUnstackedStr;
282 rtl::OUStringBuffer aSource(rNewText);
284 bool bBreakIgnored = false;
285 sal_Int32 nLen = rNewText.getLength();
286 for( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
288 sal_Unicode aChar = aSource[nPos];
289 if( aChar != '\n' )
291 aUnstackedStr.append( aChar );
292 bBreakIgnored = false;
294 else if( aChar == '\n' && bBreakIgnored )
295 aUnstackedStr.append( aChar );
296 else
297 bBreakIgnored = true;
299 aNewText = aUnstackedStr.makeStringAndClear();
302 uno::Sequence< uno::Reference< XFormattedString > > aNewStringList(1);
304 uno::Sequence< uno::Reference< XFormattedString > > aOldStringList = xTitle->getText();
305 if( aOldStringList.getLength() )
307 aNewStringList[0].set( aOldStringList[0] );
308 aNewStringList[0]->setString( aNewText );
310 else
312 uno::Reference< uno::XInterface > xI(
313 xContext->getServiceManager()->createInstanceWithContext(
314 C2U( "com.sun.star.chart2.FormattedString" ), xContext ) );
315 uno::Reference< XFormattedString > xFormattedString( xI, uno::UNO_QUERY );
317 if(xFormattedString.is())
319 xFormattedString->setString( aNewText );
320 aNewStringList[0].set( xFormattedString );
321 if( pDefaultCharHeight != 0 )
325 uno::Reference< beans::XPropertySet > xProp( xFormattedString, uno::UNO_QUERY_THROW );
327 uno::Any aFontSize( uno::makeAny( *pDefaultCharHeight ));
328 xProp->setPropertyValue( C2U("CharHeight"), aFontSize );
329 xProp->setPropertyValue( C2U("CharHeightAsian"), aFontSize );
330 xProp->setPropertyValue( C2U("CharHeightComplex"), aFontSize );
332 catch( const uno::Exception & ex )
334 ASSERT_EXCEPTION( ex );
339 xTitle->setText( aNewStringList );
342 void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
343 , const ::com::sun::star::uno::Reference<
344 ::com::sun::star::frame::XModel >& xModel )
346 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
347 if( xTitled.is())
349 xTitled->setTitleObject(NULL);
353 bool TitleHelper::getTitleType( eTitleType& rType
354 , const ::com::sun::star::uno::Reference<
355 ::com::sun::star::chart2::XTitle >& xTitle
356 , const ::com::sun::star::uno::Reference<
357 ::com::sun::star::frame::XModel >& xModel )
359 if( !xTitle.is() || !xModel.is() )
360 return false;
362 Reference< chart2::XTitle > xCurrentTitle;
363 for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
365 xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
366 if( xCurrentTitle == xTitle )
368 rType = static_cast<eTitleType>(nTitleType);
369 return true;
373 return false;
376 //.............................................................................
377 } //namespace chart
378 //.............................................................................
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */