Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / TitleHelper.cxx
blobc384d03b08e76ee09b5a01e03164ce71f218753b
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 <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>
32 namespace chart
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::chart2;
37 using ::com::sun::star::uno::Reference;
39 namespace {
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 )
50 bool bDummy = false;
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;
55 else
56 nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
59 switch( nTitleIndex )
61 case TitleHelper::SUB_TITLE:
62 if( xDiagram.is())
63 xResult.set( xDiagram, uno::UNO_QUERY );
64 break;
65 case TitleHelper::X_AXIS_TITLE:
66 if( xDiagram.is())
67 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
68 break;
69 case TitleHelper::Y_AXIS_TITLE:
70 if( xDiagram.is())
71 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
72 break;
73 case TitleHelper::Z_AXIS_TITLE:
74 if( xDiagram.is())
75 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
76 break;
77 case TitleHelper::SECONDARY_X_AXIS_TITLE:
78 if( xDiagram.is())
79 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
80 break;
81 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
82 if( xDiagram.is())
83 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
84 break;
86 case TitleHelper::MAIN_TITLE:
87 default:
88 OSL_FAIL( "Unsupported Title-Type requested" );
89 break;
92 return xResult;
95 uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
96 , const uno::Reference< XDiagram >& xDiagram )
98 uno::Reference< XTitled > xResult;
99 switch( nTitleIndex )
101 case TitleHelper::MAIN_TITLE:
102 SAL_WARN("chart2", "should not be reached");
103 break;
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 ));
113 break;
114 default:
115 OSL_FAIL( "Unsupported Title-Type requested" );
116 break;
119 return xResult;
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 );
128 return xTitled;
131 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
132 uno::Reference< XDiagram > xDiagram;
134 if( xChartDoc.is())
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 ) );
150 if( xTitled.is())
151 return xTitled->getTitleObject();
152 return nullptr;
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 );
163 else
165 uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
166 uno::Reference< XDiagram > xDiagram;
167 if( xChartDoc.is())
168 xDiagram.set( xChartDoc->getFirstDiagram());
169 xTitled = lcl_getTitleParent( nTitleIndex, xDiagram );
171 if( xTitled.is())
172 return xTitled->getTitleObject();
173 return nullptr;
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 ) );
183 if( xTitled.is())
185 css::uno::Reference<css::beans::XPropertySet> xProps(xTitled, css::uno::UNO_QUERY_THROW);
186 xProps->setPropertyValue("Visible",css::uno::Any(true));
187 return xTitled;
189 else
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 ) );
205 if( !xTitled.is() )
207 uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
208 uno::Reference< chart2::XAxis > xAxis;
209 switch( eTitleType )
211 case TitleHelper::SECONDARY_X_AXIS_TITLE:
212 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
213 break;
214 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
215 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
216 break;
217 default:
218 break;
220 uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
221 if( xProps.is() )
223 xProps->setPropertyValue( "Show", uno::Any( false ) );
224 xTitled = lcl_getTitleParent( eTitleType, xModel );
228 if(xTitled.is())
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 );
236 if(xTitle.is())
238 // default char height (main: 13.0 == default)
239 float fDefaultCharHeightSub = 11.0;
240 float fDefaultCharHeightAxis = 9.0;
241 switch( eTitleType )
243 case TitleHelper::SUB_TITLE:
244 TitleHelper::setCompleteString(
245 rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
246 break;
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 );
256 break;
257 default:
258 TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
259 break;
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 )
277 bool bDummy = false;
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");
299 return xTitle;
303 OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
305 if(!xTitle.is())
306 return OUString();
307 OUStringBuffer aRet;
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
320 if(!xTitle.is())
321 return;
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;
330 if( 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];
341 if( aChar != '\n' )
343 aUnstackedStr.append( aChar );
344 bBreakIgnored = false;
346 else if( aChar == '\n' && bBreakIgnored )
347 aUnstackedStr.append( aChar );
348 else
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 );
362 else
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 ) );
391 if( xTitled.is())
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 ) );
401 if( xTitled.is())
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 )
412 if( !xTitle.is() )
413 return false;
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);
422 return true;
426 return false;
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() )
434 return false;
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);
443 return true;
447 return false;
450 } //namespace chart
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */