1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
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 //.............................................................................
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
)
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
;
53 nTitleIndex
= bIsVertical
? TitleHelper::Y_AXIS_TITLE
: TitleHelper::X_AXIS_TITLE
;
59 case TitleHelper::SUB_TITLE
:
61 xResult
.set( xDiagram
, uno::UNO_QUERY
);
63 case TitleHelper::X_AXIS_TITLE
:
65 xResult
.set( AxisHelper::getAxis( 0, true, xDiagram
), uno::UNO_QUERY
);
67 case TitleHelper::Y_AXIS_TITLE
:
69 xResult
.set( AxisHelper::getAxis( 1, true, xDiagram
), uno::UNO_QUERY
);
71 case TitleHelper::Z_AXIS_TITLE
:
73 xResult
.set( AxisHelper::getAxis( 2, true, xDiagram
), uno::UNO_QUERY
);
75 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
77 xResult
.set( AxisHelper::getAxis( 0, false, xDiagram
), uno::UNO_QUERY
);
79 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
81 xResult
.set( AxisHelper::getAxis( 1, false, xDiagram
), uno::UNO_QUERY
);
84 case TitleHelper::MAIN_TITLE
:
86 OSL_FAIL( "Unsupported Title-Type requested" );
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
;
100 xDiagram
.set( xChartDoc
->getFirstDiagram());
102 switch( nTitleIndex
)
104 case TitleHelper::MAIN_TITLE
:
105 xResult
.set( xModel
, uno::UNO_QUERY
);
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
));
118 OSL_FAIL( "Unsupported Title-Type requested" );
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
) );
130 return xTitled
->getTitleObject();
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
) );
146 uno::Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( xModel
) );
147 uno::Reference
< chart2::XAxis
> xAxis
;
150 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
151 xAxis
= AxisHelper::createAxis( 0, false, xDiagram
, xContext
);
153 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
154 xAxis
= AxisHelper::createAxis( 1, false, xDiagram
, xContext
);
159 uno::Reference
< beans::XPropertySet
> xProps( xAxis
, uno::UNO_QUERY
);
162 xProps
->setPropertyValue( "Show", uno::makeAny( sal_False
) );
163 xTitled
= lcl_getTitleParent( eTitleType
, xModel
);
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
);
177 // default char height (main: 13.0 == default)
178 float fDefaultCharHeightSub
= 11.0;
179 float fDefaultCharHeightAxis
= 9.0;
182 case TitleHelper::SUB_TITLE
:
183 TitleHelper::setCompleteString(
184 rTitleText
, xTitle
, xContext
, & fDefaultCharHeightSub
);
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
);
197 TitleHelper::setCompleteString( rTitleText
, xTitle
, xContext
);
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
)
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
);
243 OUString
TitleHelper::getCompleteString( const uno::Reference
< XTitle
>& xTitle
)
248 uno::Sequence
< uno::Reference
< XFormattedString
> > aStringList
= xTitle
->getText();
249 for( sal_Int32 nN
=0; nN
<aStringList
.getLength();nN
++ )
250 aRet
+= aStringList
[nN
]->getString();
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
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
;
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
];
283 aUnstackedStr
.append( aChar
);
284 bBreakIgnored
= false;
286 else if( aChar
== '\n' && bBreakIgnored
)
287 aUnstackedStr
.append( aChar
);
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
);
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
) );
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() )
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
);
361 //.............................................................................
363 //.............................................................................
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */