LibreOffice Module chart2 (master) 1
TitleHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <TitleHelper.hxx>
21#include <Title.hxx>
22#include <ChartModel.hxx>
23#include <ChartModelHelper.hxx>
24#include <Axis.hxx>
25#include <AxisHelper.hxx>
26#include <DiagramHelper.hxx>
27#include <Diagram.hxx>
29#include <com/sun/star/chart2/FormattedString.hpp>
30#include <rtl/ustrbuf.hxx>
32#include <sal/log.hxx>
33
34namespace chart
35{
36
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::chart2;
39using ::com::sun::star::uno::Reference;
40
41namespace {
42
43uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
44 TitleHelper::eTitleType nTitleIndex
45 , const rtl::Reference< Diagram >& xDiagram )
46{
47 uno::Reference< XTitled > xResult;
48
51 {
52 bool bDummy = false;
53 bool bIsVertical = xDiagram && xDiagram->getVertical( bDummy, bDummy );
54
56 nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
57 else
58 nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
59 }
60
61 switch( nTitleIndex )
62 {
64 if( xDiagram.is())
65 xResult = xDiagram;
66 break;
68 if( xDiagram.is())
69 xResult = AxisHelper::getAxis( 0, true, xDiagram );
70 break;
72 if( xDiagram.is())
73 xResult = AxisHelper::getAxis( 1, true, xDiagram );
74 break;
76 if( xDiagram.is())
77 xResult = AxisHelper::getAxis( 2, true, xDiagram );
78 break;
80 if( xDiagram.is())
81 xResult = AxisHelper::getAxis( 0, false, xDiagram );
82 break;
84 if( xDiagram.is())
85 xResult = AxisHelper::getAxis( 1, false, xDiagram );
86 break;
87
89 default:
90 OSL_FAIL( "Unsupported Title-Type requested" );
91 break;
92 }
93
94 return xResult;
95}
96
97uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
98 , const rtl::Reference< Diagram >& xDiagram )
99{
100 uno::Reference< XTitled > xResult;
101 switch( nTitleIndex )
102 {
104 SAL_WARN("chart2", "should not be reached");
105 break;
114 xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
115 break;
116 default:
117 OSL_FAIL( "Unsupported Title-Type requested" );
118 break;
119 }
120
121 return xResult;
122}
123
124uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
126{
127 if(nTitleIndex == TitleHelper::MAIN_TITLE)
128 {
129 return xModel;
130 }
131
133
134 if( xModel.is())
135 xDiagram = xModel->getFirstChartDiagram();
136
137 return lcl_getTitleParent( nTitleIndex, xDiagram );
138}
139
140}
141
143 , ChartModel& rModel )
144{
145 if(nTitleIndex == TitleHelper::MAIN_TITLE)
146 return rModel.getTitleObject2();
147
148 rtl::Reference< Diagram > xDiagram = rModel.getFirstChartDiagram();
149 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xDiagram ) );
150 if( !xTitled )
151 return nullptr;
152 uno::Reference<XTitle> xTitle = xTitled->getTitleObject();
153 auto pTitle = dynamic_cast<Title*>(xTitle.get());
154 assert(!xTitle || pTitle);
155 return pTitle;
156}
157
159 , const rtl::Reference<ChartModel>& xModel )
160{
162 if(nTitleIndex == TitleHelper::MAIN_TITLE)
163 {
164 xTitled = xModel;
165 }
166 else
167 {
169 if( xModel.is())
170 xDiagram = xModel->getFirstChartDiagram();
171 xTitled = lcl_getTitleParent( nTitleIndex, xDiagram );
172 }
173 if( !xTitled )
174 return nullptr;
175 uno::Reference<XTitle> xTitle = xTitled->getTitleObject();
176 Title* pTitle = dynamic_cast<Title*>(xTitle.get());
177 assert(!xTitle || pTitle);
178 return pTitle;
179}
180
183 , const OUString& rTitleText
184 , const rtl::Reference<ChartModel>& xModel
186{
188 if( xTitled.is())
189 {
190 xTitled->setPropertyValue("Visible",css::uno::Any(true));
191 return xTitled;
192 }
193 else
194 {
195 return createTitle(eTitleType, rTitleText, xModel, xContext, nullptr/*pRefSizeProvider*/);
196 }
197}
198
201 , const OUString& rTitleText
202 , const rtl::Reference<ChartModel>& xModel
204 , ReferenceSizeProvider * pRefSizeProvider )
205{
207 uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
208
209 if( !xTitled.is() )
210 {
211 rtl::Reference< Diagram > xDiagram( xModel->getFirstChartDiagram() );
213 switch( eTitleType )
214 {
216 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
217 break;
219 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
220 break;
221 default:
222 break;
223 }
224 if( xAxis.is() )
225 {
226 xAxis->setPropertyValue( "Show", uno::Any( false ) );
227 xTitled = lcl_getTitleParent( eTitleType, xModel );
228 }
229 }
230
231 if(xTitled.is())
232 {
233 rtl::Reference< Diagram > xDiagram( xModel->getFirstChartDiagram() );
234
235 xTitle = new ::chart::Title();
236
237 // default char height (main: 13.0 == default)
238 float fDefaultCharHeightSub = 11.0;
239 float fDefaultCharHeightAxis = 9.0;
240 switch( eTitleType )
241 {
244 rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
245 break;
254 rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
255 break;
256 default:
257 TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
258 break;
259 }
260
261 // set/clear autoscale
262 if( pRefSizeProvider )
263 pRefSizeProvider->setValuesAtTitle( xTitle );
264
265 xTitled->setTitleObject( xTitle );
266
267 //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
272
273 {
274 try
275 {
276 bool bDummy = false;
277 bool bIsVertical = xDiagram->getVertical( bDummy, bDummy );
278
279 if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
280 || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
281 || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
282 || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
283 {
284 xTitle->setPropertyValue( "TextRotation", uno::Any( 90.0 ));
285 }
286 }
287 catch( const uno::Exception & )
288 {
289 DBG_UNHANDLED_EXCEPTION("chart2");
290 }
291 }
292 }
293 return xTitle;
294
295}
296
298{
299 if(!xTitle.is())
300 return OUString();
301 OUStringBuffer aRet;
303 for( uno::Reference< XFormattedString > const & formattedStr : aStringList )
304 aRet.append( formattedStr->getString() );
305 return aRet.makeStringAndClear();
306}
307
308void TitleHelper::setCompleteString( const OUString& rNewText
309 , const rtl::Reference< Title >& xTitle
311 , const float * pDefaultCharHeight /* = 0 */ )
312{
313 //the format of the first old text portion will be maintained if there is any
314 if(!xTitle.is())
315 return;
316
317 OUString aNewText = rNewText;
318
319 bool bStacked = false;
320 if( xTitle.is() )
321 xTitle->getPropertyValue( "StackCharacters" ) >>= bStacked;
322
323 if( bStacked )
324 {
325 //#i99841# remove linebreaks that were added for vertical stacking
326 OUStringBuffer aUnstackedStr;
327 OUStringBuffer aSource(rNewText);
328
329 bool bBreakIgnored = false;
330 sal_Int32 nLen = rNewText.getLength();
331 for( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
332 {
333 sal_Unicode aChar = aSource[nPos];
334 if( aChar != '\n' )
335 {
336 aUnstackedStr.append( aChar );
337 bBreakIgnored = false;
338 }
339 else if( aChar == '\n' && bBreakIgnored )
340 aUnstackedStr.append( aChar );
341 else
342 bBreakIgnored = true;
343 }
344 aNewText = aUnstackedStr.makeStringAndClear();
345 }
346
348
349 uno::Sequence< uno::Reference< XFormattedString > > aOldStringList = xTitle->getText();
350 if( aOldStringList.hasElements() )
351 {
352 aNewStringList = { aOldStringList[0] };
353 aNewStringList[0]->setString( aNewText );
354 }
355 else
356 {
358 chart2::FormattedString::create( xContext );
359
360 xFormattedString->setString( aNewText );
361 aNewStringList = { xFormattedString };
362 if( pDefaultCharHeight != nullptr )
363 {
364 try
365 {
366 uno::Any aFontSize( *pDefaultCharHeight );
367 xFormattedString->setPropertyValue( "CharHeight", aFontSize );
368 xFormattedString->setPropertyValue( "CharHeightAsian", aFontSize );
369 xFormattedString->setPropertyValue( "CharHeightComplex", aFontSize );
370 }
371 catch( const uno::Exception & )
372 {
373 DBG_UNHANDLED_EXCEPTION("chart2");
374 }
375 }
376 }
377 xTitle->setText( aNewStringList );
378}
379
381 , const rtl::Reference<ChartModel>& xModel )
382{
383 uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
384 if( xTitled.is())
385 {
386 xTitled->setTitleObject(nullptr);
387 }
388}
389
391 , const rtl::Reference< Title >& xTitle
392 , const rtl::Reference<ChartModel>& xModel )
393{
394 if( !xTitle.is() || !xModel.is() )
395 return false;
396
397 rtl::Reference< Title > xCurrentTitle;
398 for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
399 {
400 xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
401 if( xCurrentTitle == xTitle )
402 {
403 rType = static_cast<eTitleType>(nTitleType);
404 return true;
405 }
406 }
407
408 return false;
409}
410
412 , const rtl::Reference<ChartModel>& xModel)
413{
415 if( xTitled.is())
416 {
417 css::uno::Reference<css::beans::XPropertySet> xProps(xTitled, css::uno::UNO_QUERY_THROW);
418 xProps->setPropertyValue("Visible",css::uno::Any(false));
419 }
420}
421
422} //namespace chart
423
424/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nPos
static rtl::Reference< ::chart::Axis > getAxis(sal_Int32 nDimensionIndex, bool bMainAxis, const rtl::Reference< ::chart::Diagram > &xDiagram)
static rtl::Reference< ::chart::Axis > createAxis(sal_Int32 nDimensionIndex, bool bMainAxis, const rtl::Reference< ::chart::Diagram > &xDiagram, const css::uno::Reference< css::uno::XComponentContext > &xContext, ReferenceSizeProvider *pRefSizeProvider=nullptr)
SAL_DLLPRIVATE void setValuesAtTitle(const css::uno::Reference< css::chart2::XTitle > &xTitle)
Sets the ReferencePageSize according to the internal settings of this class at the XTitle,...
static rtl::Reference< ::chart::Title > createOrShowTitle(eTitleType nTitleIndex, const OUString &rTitleText, const rtl::Reference< ::chart::ChartModel > &xModel, const css::uno::Reference< css::uno::XComponentContext > &xContex)
static void removeTitle(eTitleType nTitleIndex, const rtl::Reference< ::chart::ChartModel > &xModel)
static void hideTitle(eTitleType nTitleIndex, const rtl::Reference< ::chart::ChartModel > &xModel)
static rtl::Reference< ::chart::Title > getTitle(eTitleType nTitleIndex, ChartModel &rModel)
static rtl::Reference< ::chart::Title > createTitle(eTitleType nTitleIndex, const OUString &rTitleText, const rtl::Reference< ::chart::ChartModel > &xModel, const css::uno::Reference< css::uno::XComponentContext > &xContext, ReferenceSizeProvider *pRefSizeProvider=nullptr)
static OUString getCompleteString(const rtl::Reference< ::chart::Title > &xTitle)
static void setCompleteString(const OUString &rNewText, const rtl::Reference< ::chart::Title > &xTitle, const css::uno::Reference< css::uno::XComponentContext > &xContext, const float *pDefaultCharHeight=nullptr)
static bool getTitleType(eTitleType &rType, const rtl::Reference< ::chart::Title > &xTitle, const rtl::Reference< ::chart::ChartModel > &xModel)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
Title
Reference< XModel > xModel
sal_uInt16 sal_Unicode
const std::u16string_view aStringList[]