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 <ChartModel.hxx>
22 #include <ChartModelHelper.hxx>
23 #include <AxisHelper.hxx>
24 #include <DiagramHelper.hxx>
25 #include <ReferenceSizeProvider.hxx>
26 #include <com/sun/star/chart2/FormattedString.hpp>
27 #include <com/sun/star/chart2/XChartDocument.hpp>
28 #include <rtl/ustrbuf.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <sal/log.hxx>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::chart2
;
37 using ::com::sun::star::uno::Reference
;
41 uno::Reference
< XTitled
> lcl_getTitleParentFromDiagram(
42 TitleHelper::eTitleType nTitleIndex
43 , const uno::Reference
< XDiagram
>& xDiagram
)
45 uno::Reference
< XTitled
> xResult
;
47 if( nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
||
48 nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
)
51 bool bIsVertical
= DiagramHelper::getVertical( xDiagram
, bDummy
, bDummy
);
53 if( nTitleIndex
== TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
)
54 nTitleIndex
= bIsVertical
? TitleHelper::X_AXIS_TITLE
: TitleHelper::Y_AXIS_TITLE
;
56 nTitleIndex
= bIsVertical
? TitleHelper::Y_AXIS_TITLE
: TitleHelper::X_AXIS_TITLE
;
61 case TitleHelper::SUB_TITLE
:
63 xResult
.set( xDiagram
, uno::UNO_QUERY
);
65 case TitleHelper::X_AXIS_TITLE
:
67 xResult
.set( AxisHelper::getAxis( 0, true, xDiagram
), uno::UNO_QUERY
);
69 case TitleHelper::Y_AXIS_TITLE
:
71 xResult
.set( AxisHelper::getAxis( 1, true, xDiagram
), uno::UNO_QUERY
);
73 case TitleHelper::Z_AXIS_TITLE
:
75 xResult
.set( AxisHelper::getAxis( 2, true, xDiagram
), uno::UNO_QUERY
);
77 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
79 xResult
.set( AxisHelper::getAxis( 0, false, xDiagram
), uno::UNO_QUERY
);
81 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
83 xResult
.set( AxisHelper::getAxis( 1, false, xDiagram
), uno::UNO_QUERY
);
86 case TitleHelper::MAIN_TITLE
:
88 OSL_FAIL( "Unsupported Title-Type requested" );
95 uno::Reference
< XTitled
> lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
96 , const uno::Reference
< XDiagram
>& xDiagram
)
98 uno::Reference
< XTitled
> xResult
;
101 case TitleHelper::MAIN_TITLE
:
102 SAL_WARN("chart2", "should not be reached");
104 case TitleHelper::SUB_TITLE
:
105 case TitleHelper::X_AXIS_TITLE
:
106 case TitleHelper::Y_AXIS_TITLE
:
107 case TitleHelper::Z_AXIS_TITLE
:
108 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
:
109 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
:
110 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
111 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
112 xResult
.set( lcl_getTitleParentFromDiagram( nTitleIndex
, xDiagram
));
115 OSL_FAIL( "Unsupported Title-Type requested" );
122 uno::Reference
< XTitled
> lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
123 , const uno::Reference
< frame::XModel
>& xModel
)
125 if(nTitleIndex
== TitleHelper::MAIN_TITLE
)
127 uno::Reference
< XTitled
> xTitled( xModel
, uno::UNO_QUERY
);
131 uno::Reference
< XChartDocument
> xChartDoc( xModel
, uno::UNO_QUERY
);
132 uno::Reference
< XDiagram
> xDiagram
;
135 xDiagram
.set( xChartDoc
->getFirstDiagram());
137 return lcl_getTitleParent( nTitleIndex
, xDiagram
);
142 uno::Reference
< XTitle
> TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
143 , ChartModel
& rModel
)
145 if(nTitleIndex
== TitleHelper::MAIN_TITLE
)
146 return rModel
.getTitleObject();
148 uno::Reference
< XDiagram
> xDiagram
= rModel
.getFirstDiagram();
149 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( nTitleIndex
, xDiagram
) );
151 return xTitled
->getTitleObject();
155 uno::Reference
< XTitle
> TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
156 , const uno::Reference
< frame::XModel
>& xModel
)
158 uno::Reference
< XTitled
> xTitled
;
159 if(nTitleIndex
== TitleHelper::MAIN_TITLE
)
161 xTitled
.set( xModel
, uno::UNO_QUERY
);
165 uno::Reference
< XChartDocument
> xChartDoc( xModel
, uno::UNO_QUERY
);
166 uno::Reference
< XDiagram
> xDiagram
;
168 xDiagram
.set( xChartDoc
->getFirstDiagram());
169 xTitled
= lcl_getTitleParent( nTitleIndex
, xDiagram
);
172 return xTitled
->getTitleObject();
176 uno::Reference
< XTitle
> TitleHelper::createOrShowTitle(
177 TitleHelper::eTitleType eTitleType
178 , const OUString
& rTitleText
179 , const uno::Reference
< frame::XModel
>& xModel
180 , const uno::Reference
< uno::XComponentContext
> & xContext
)
182 uno::Reference
< chart2::XTitle
> xTitled( TitleHelper::getTitle( eTitleType
, xModel
) );
185 css::uno::Reference
<css::beans::XPropertySet
> xProps(xTitled
, css::uno::UNO_QUERY_THROW
);
186 xProps
->setPropertyValue("Visible",css::uno::Any(true));
191 return createTitle(eTitleType
, rTitleText
, xModel
, xContext
, nullptr/*pRefSizeProvider*/);
195 uno::Reference
< XTitle
> TitleHelper::createTitle(
196 TitleHelper::eTitleType eTitleType
197 , const OUString
& rTitleText
198 , const uno::Reference
< frame::XModel
>& xModel
199 , const uno::Reference
< uno::XComponentContext
> & xContext
200 , ReferenceSizeProvider
* pRefSizeProvider
)
202 uno::Reference
< XTitle
> xTitle
;
203 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( eTitleType
, xModel
) );
207 uno::Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( xModel
) );
208 uno::Reference
< chart2::XAxis
> xAxis
;
211 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
212 xAxis
= AxisHelper::createAxis( 0, false, xDiagram
, xContext
);
214 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
215 xAxis
= AxisHelper::createAxis( 1, false, xDiagram
, xContext
);
220 uno::Reference
< beans::XPropertySet
> xProps( xAxis
, uno::UNO_QUERY
);
223 xProps
->setPropertyValue( "Show", uno::Any( false ) );
224 xTitled
= lcl_getTitleParent( eTitleType
, xModel
);
230 uno::Reference
< XDiagram
> xDiagram( ChartModelHelper::findDiagram( xModel
) );
232 xTitle
.set( xContext
->getServiceManager()->createInstanceWithContext(
233 "com.sun.star.chart2.Title",
234 xContext
), uno::UNO_QUERY
);
238 // default char height (main: 13.0 == default)
239 float fDefaultCharHeightSub
= 11.0;
240 float fDefaultCharHeightAxis
= 9.0;
243 case TitleHelper::SUB_TITLE
:
244 TitleHelper::setCompleteString(
245 rTitleText
, xTitle
, xContext
, & fDefaultCharHeightSub
);
247 case TitleHelper::X_AXIS_TITLE
:
248 case TitleHelper::Y_AXIS_TITLE
:
249 case TitleHelper::Z_AXIS_TITLE
:
250 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION
:
251 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION
:
252 case TitleHelper::SECONDARY_X_AXIS_TITLE
:
253 case TitleHelper::SECONDARY_Y_AXIS_TITLE
:
254 TitleHelper::setCompleteString(
255 rTitleText
, xTitle
, xContext
, & fDefaultCharHeightAxis
);
258 TitleHelper::setCompleteString( rTitleText
, xTitle
, xContext
);
262 // set/clear autoscale
263 if( pRefSizeProvider
)
264 pRefSizeProvider
->setValuesAtTitle( xTitle
);
266 xTitled
->setTitleObject( xTitle
);
268 //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
269 if( eTitleType
== TitleHelper::X_AXIS_TITLE
||
270 eTitleType
== TitleHelper::Y_AXIS_TITLE
||
271 eTitleType
== TitleHelper::SECONDARY_X_AXIS_TITLE
||
272 eTitleType
== TitleHelper::SECONDARY_Y_AXIS_TITLE
)
278 bool bIsVertical
= DiagramHelper::getVertical( xDiagram
, bDummy
, bDummy
);
280 Reference
< beans::XPropertySet
> xTitleProps( xTitle
, uno::UNO_QUERY
);
281 if( xTitleProps
.is() )
283 if( (!bIsVertical
&& eTitleType
== TitleHelper::Y_AXIS_TITLE
)
284 || (bIsVertical
&& eTitleType
== TitleHelper::X_AXIS_TITLE
)
285 || (!bIsVertical
&& eTitleType
== TitleHelper::SECONDARY_Y_AXIS_TITLE
)
286 || (bIsVertical
&& eTitleType
== TitleHelper::SECONDARY_X_AXIS_TITLE
) )
288 xTitleProps
->setPropertyValue( "TextRotation", uno::Any( 90.0 ));
292 catch( const uno::Exception
& )
294 DBG_UNHANDLED_EXCEPTION("chart2");
303 OUString
TitleHelper::getCompleteString( const uno::Reference
< XTitle
>& xTitle
)
308 uno::Sequence
< uno::Reference
< XFormattedString
> > aStringList
= xTitle
->getText();
309 for( sal_Int32 nN
=0; nN
<aStringList
.getLength();nN
++ )
310 aRet
.append( aStringList
[nN
]->getString() );
311 return aRet
.makeStringAndClear();
314 void TitleHelper::setCompleteString( const OUString
& rNewText
315 , const uno::Reference
< XTitle
>& xTitle
316 , const uno::Reference
< uno::XComponentContext
> & xContext
317 , const float * pDefaultCharHeight
/* = 0 */ )
319 //the format of the first old text portion will be maintained if there is any
323 OUString aNewText
= rNewText
;
325 bool bStacked
= false;
326 uno::Reference
< beans::XPropertySet
> xTitleProperties( xTitle
, uno::UNO_QUERY
);
327 if( xTitleProperties
.is() )
328 xTitleProperties
->getPropertyValue( "StackCharacters" ) >>= bStacked
;
332 //#i99841# remove linebreaks that were added for vertical stacking
333 OUStringBuffer aUnstackedStr
;
334 OUStringBuffer
aSource(rNewText
);
336 bool bBreakIgnored
= false;
337 sal_Int32 nLen
= rNewText
.getLength();
338 for( sal_Int32 nPos
= 0; nPos
< nLen
; ++nPos
)
340 sal_Unicode aChar
= aSource
[nPos
];
343 aUnstackedStr
.append( aChar
);
344 bBreakIgnored
= false;
346 else if( aChar
== '\n' && bBreakIgnored
)
347 aUnstackedStr
.append( aChar
);
349 bBreakIgnored
= true;
351 aNewText
= aUnstackedStr
.makeStringAndClear();
354 uno::Sequence
< uno::Reference
< XFormattedString
> > aNewStringList(1);
356 uno::Sequence
< uno::Reference
< XFormattedString
> > aOldStringList
= xTitle
->getText();
357 if( aOldStringList
.hasElements() )
359 aNewStringList
[0].set( aOldStringList
[0] );
360 aNewStringList
[0]->setString( aNewText
);
364 uno::Reference
< chart2::XFormattedString2
> xFormattedString
=
365 chart2::FormattedString::create( xContext
);
367 xFormattedString
->setString( aNewText
);
368 aNewStringList
[0].set( xFormattedString
);
369 if( pDefaultCharHeight
!= nullptr )
373 uno::Any
aFontSize( *pDefaultCharHeight
);
374 xFormattedString
->setPropertyValue( "CharHeight", aFontSize
);
375 xFormattedString
->setPropertyValue( "CharHeightAsian", aFontSize
);
376 xFormattedString
->setPropertyValue( "CharHeightComplex", aFontSize
);
378 catch( const uno::Exception
& )
380 DBG_UNHANDLED_EXCEPTION("chart2");
384 xTitle
->setText( aNewStringList
);
387 void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
388 , const css::uno::Reference
< css::frame::XModel
>& xModel
)
390 uno::Reference
< XTitled
> xTitled( lcl_getTitleParent( nTitleIndex
, xModel
) );
393 xTitled
->setTitleObject(nullptr);
397 void TitleHelper::hideTitle( TitleHelper::eTitleType nTitleIndex
398 , const css::uno::Reference
< css::frame::XModel
>& xModel
)
400 uno::Reference
< chart2::XTitle
> xTitled( TitleHelper::getTitle( nTitleIndex
, xModel
) );
403 css::uno::Reference
<css::beans::XPropertySet
> xProps(xTitled
, css::uno::UNO_QUERY_THROW
);
404 xProps
->setPropertyValue("Visible",css::uno::Any(false));
408 bool TitleHelper::getTitleType( eTitleType
& rType
409 , const css::uno::Reference
< css::chart2::XTitle
>& xTitle
410 , ChartModel
& rModel
)
415 Reference
< chart2::XTitle
> xCurrentTitle
;
416 for( sal_Int32 nTitleType
= TITLE_BEGIN
; nTitleType
< NORMAL_TITLE_END
; nTitleType
++ )
418 xCurrentTitle
= TitleHelper::getTitle( static_cast<eTitleType
>(nTitleType
), rModel
);
419 if( xCurrentTitle
== xTitle
)
421 rType
= static_cast<eTitleType
>(nTitleType
);
429 bool TitleHelper::getTitleType( eTitleType
& rType
430 , const css::uno::Reference
< css::chart2::XTitle
>& xTitle
431 , const css::uno::Reference
< css::frame::XModel
>& xModel
)
433 if( !xTitle
.is() || !xModel
.is() )
436 Reference
< chart2::XTitle
> xCurrentTitle
;
437 for( sal_Int32 nTitleType
= TITLE_BEGIN
; nTitleType
< NORMAL_TITLE_END
; nTitleType
++ )
439 xCurrentTitle
= TitleHelper::getTitle( static_cast<eTitleType
>(nTitleType
), xModel
);
440 if( xCurrentTitle
== xTitle
)
442 rType
= static_cast<eTitleType
>(nTitleType
);
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */