nss: upgrade to release 3.73
[LibreOffice.git] / oox / source / drawingml / chart / titleconverter.cxx
blobf39929134b636c25654378ffe43b93169cb092ef
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 <drawingml/chart/titleconverter.hxx>
22 #include <com/sun/star/chart/ChartLegendExpansion.hpp>
23 #include <com/sun/star/chart2/FormattedString.hpp>
24 #include <com/sun/star/chart2/LegendPosition.hpp>
25 #include <com/sun/star/chart2/XDiagram.hpp>
26 #include <com/sun/star/chart2/XLegend.hpp>
27 #include <com/sun/star/chart2/XTitle.hpp>
28 #include <com/sun/star/chart2/XTitled.hpp>
29 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
30 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
31 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
32 #include <osl/diagnose.h>
33 #include <drawingml/textbody.hxx>
34 #include <drawingml/textparagraph.hxx>
35 #include <drawingml/chart/datasourceconverter.hxx>
36 #include <drawingml/chart/titlemodel.hxx>
37 #include <oox/helper/containerhelper.hxx>
38 #include <oox/token/properties.hxx>
39 #include <oox/token/tokens.hxx>
40 #include <com/sun/star/chart2/RelativePosition.hpp>
41 #include <com/sun/star/drawing/Alignment.hpp>
43 #include <oox/drawingml/chart/modelbase.hxx>
44 namespace oox::drawingml::chart {
46 using namespace ::com::sun::star::awt;
47 using namespace ::com::sun::star::chart2;
48 using namespace ::com::sun::star::chart2::data;
49 using namespace ::com::sun::star::drawing;
50 using namespace ::com::sun::star::uno;
53 TextConverter::TextConverter( const ConverterRoot& rParent, TextModel& rModel ) :
54 ConverterBase< TextModel >( rParent, rModel )
58 TextConverter::~TextConverter()
62 Reference< XDataSequence > TextConverter::createDataSequence( const OUString& rRole )
64 Reference< XDataSequence > xDataSeq;
65 if( mrModel.mxDataSeq.is() )
67 DataSequenceConverter aDataSeqConv( *this, *mrModel.mxDataSeq );
68 xDataSeq = aDataSeqConv.createDataSequence( rRole );
70 return xDataSeq;
73 Sequence< Reference< XFormattedString > > TextConverter::createStringSequence(
74 const OUString& rDefaultText, const ModelRef< TextBody >& rxTextProp, ObjectType eObjType )
76 OSL_ENSURE( !mrModel.mxDataSeq || !mrModel.mxTextBody, "TextConverter::createStringSequence - linked string and rich text found" );
77 ::std::vector< Reference< XFormattedString > > aStringVec;
78 if( mrModel.mxTextBody.is() )
80 // rich-formatted text objects can be created, but currently Chart2 is not able to show them
81 const TextParagraphVector& rTextParas = mrModel.mxTextBody->getParagraphs();
82 for( TextParagraphVector::const_iterator aPIt = rTextParas.begin(), aPEnd = rTextParas.end(); aPIt != aPEnd; ++aPIt )
84 const TextParagraph& rTextPara = **aPIt;
85 const TextCharacterProperties& rParaProps = rTextPara.getProperties().getTextCharacterProperties();
86 for( TextRunVector::const_iterator aRIt = rTextPara.getRuns().begin(), aREnd = rTextPara.getRuns().end(); aRIt != aREnd; ++aRIt )
88 const TextRun& rTextRun = **aRIt;
89 bool bAddNewLine = ((aRIt + 1 == aREnd) && (aPIt + 1 != aPEnd)) || rTextRun.isLineBreak();
90 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, rTextRun.getText(), bAddNewLine );
91 PropertySet aPropSet( xFmtStr );
92 TextCharacterProperties aRunProps( rParaProps );
93 aRunProps.assignUsed( rTextRun.getTextCharacterProperties() );
94 getFormatter().convertTextFormatting( aPropSet, aRunProps, eObjType );
98 else
100 OUString aString;
101 // try to create string from linked data
102 if( mrModel.mxDataSeq.is() && !mrModel.mxDataSeq->maData.empty() )
103 mrModel.mxDataSeq->maData.begin()->second >>= aString;
104 // no linked string -> fall back to default string
105 if( aString.isEmpty() )
106 aString = rDefaultText;
108 // create formatted string object
109 if( !aString.isEmpty() )
111 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, aString, false );
112 PropertySet aPropSet( xFmtStr );
113 getFormatter().convertTextFormatting( aPropSet, rxTextProp, eObjType );
117 return ContainerHelper::vectorToSequence( aStringVec );
120 Reference< XFormattedString > TextConverter::appendFormattedString(
121 ::std::vector< Reference< XFormattedString > >& orStringVec, const OUString& rString, bool bAddNewLine ) const
123 Reference< XFormattedString2 > xFmtStr;
126 xFmtStr = FormattedString::create( ConverterRoot::getComponentContext() );
127 xFmtStr->setString( bAddNewLine ? (rString + "\n") : rString );
128 orStringVec.emplace_back(xFmtStr );
130 catch( Exception& )
133 return xFmtStr;
136 TitleConverter::TitleConverter( const ConverterRoot& rParent, TitleModel& rModel ) :
137 ConverterBase< TitleModel >( rParent, rModel )
141 TitleConverter::~TitleConverter()
145 void TitleConverter::convertFromModel( const Reference< XTitled >& rxTitled, const OUString& rAutoTitle, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
147 if( !rxTitled.is() )
148 return;
150 // create the formatted strings
151 TextModel& rText = mrModel.mxText.getOrCreate();
152 TextConverter aTextConv( *this, rText );
153 Sequence< Reference< XFormattedString > > aStringSeq = aTextConv.createStringSequence( rAutoTitle, mrModel.mxTextProp, eObjType );
154 if( !aStringSeq.hasElements() )
155 return;
159 // create the title object and set the string data
160 Reference< XTitle > xTitle( createInstance( "com.sun.star.chart2.Title" ), UNO_QUERY_THROW );
161 xTitle->setText( aStringSeq );
162 rxTitled->setTitleObject( xTitle );
164 // frame formatting (text formatting already done in TextConverter::createStringSequence())
165 PropertySet aPropSet( xTitle );
166 getFormatter().convertFrameFormatting( aPropSet, mrModel.mxShapeProp, eObjType );
168 // frame rotation
169 OSL_ENSURE( !mrModel.mxTextProp || !rText.mxTextBody, "TitleConverter::convertFromModel - multiple text properties" );
170 ModelRef< TextBody > xTextProp = mrModel.mxTextProp.is() ? mrModel.mxTextProp : rText.mxTextBody;
171 ObjectFormatter::convertTextRotation( aPropSet, xTextProp, true, mrModel.mnDefaultRotation );
173 // register the title and layout data for conversion of position
174 registerTitleLayout( xTitle, mrModel.mxLayout, eObjType, nMainIdx, nSubIdx );
176 catch( Exception& )
181 LegendConverter::LegendConverter( const ConverterRoot& rParent, LegendModel& rModel ) :
182 ConverterBase< LegendModel >( rParent, rModel )
186 LegendConverter::~LegendConverter()
190 void LegendConverter::convertFromModel( const Reference< XDiagram >& rxDiagram )
192 if( !rxDiagram.is() )
193 return;
197 namespace cssc = ::com::sun::star::chart;
198 namespace cssc2 = ::com::sun::star::chart2;
200 // create the legend
201 Reference< XLegend > xLegend( createInstance( "com.sun.star.chart2.Legend" ), UNO_QUERY_THROW );
202 rxDiagram->setLegend( xLegend );
203 PropertySet aPropSet( xLegend );
204 aPropSet.setProperty( PROP_Show, true );
206 // legend formatting
207 getFormatter().convertFormatting( aPropSet, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_LEGEND );
209 // predefined legend position and expansion
210 cssc2::LegendPosition eLegendPos = cssc2::LegendPosition_LINE_END;
211 cssc::ChartLegendExpansion eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
212 RelativePosition eRelPos;
213 bool bTopRight=false;
214 switch( mrModel.mnPosition )
216 case XML_l:
217 eLegendPos = cssc2::LegendPosition_LINE_START;
218 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
219 break;
220 case XML_r:
221 eLegendPos = cssc2::LegendPosition_LINE_END;
222 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
223 break;
224 case XML_tr: // top-right not supported
225 eRelPos.Primary = 1;
226 eRelPos.Secondary =0;
227 eRelPos.Anchor = Alignment_TOP_RIGHT;
228 bTopRight=true;
229 break;
230 case XML_t:
231 eLegendPos = cssc2::LegendPosition_PAGE_START;
232 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
233 break;
234 case XML_b:
235 eLegendPos = cssc2::LegendPosition_PAGE_END;
236 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
237 break;
239 bool bManualLayout=false;
240 // manual positioning and size
241 if( mrModel.mxLayout )
243 LayoutConverter aLayoutConv( *this, *mrModel.mxLayout );
244 // manual size needs ChartLegendExpansion_CUSTOM
245 if( aLayoutConv.convertFromModel( aPropSet ) )
247 eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
249 bManualLayout = !aLayoutConv.getAutoLayout();
252 // set position and expansion properties
253 aPropSet.setProperty( PROP_AnchorPosition, eLegendPos );
254 aPropSet.setProperty( PROP_Expansion, eLegendExpand );
256 if (bTopRight && !bManualLayout)
257 aPropSet.setProperty( PROP_RelativePosition , makeAny(eRelPos));
259 aPropSet.setProperty(PROP_Overlay, mrModel.mbOverlay);
261 if (mrModel.maLegendEntries.size() > 0)
262 legendEntriesFormatting(rxDiagram);
264 catch( Exception& )
269 void LegendConverter::legendEntriesFormatting(const Reference<XDiagram>& rxDiagram)
271 Reference<XCoordinateSystemContainer> xCooSysContainer(rxDiagram, UNO_QUERY_THROW);
272 const Sequence<Reference<XCoordinateSystem>> xCooSysSequence(xCooSysContainer->getCoordinateSystems());
273 if (!xCooSysSequence.hasElements())
274 return;
276 sal_Int32 nIndex = 0;
277 for (const auto& rCooSys : xCooSysSequence)
279 PropertySet aCooSysProp(rCooSys);
280 bool bSwapXAndY = aCooSysProp.getBoolProperty(PROP_SwapXAndYAxis);
282 Reference<XChartTypeContainer> xChartTypeContainer(rCooSys, UNO_QUERY_THROW);
283 const Sequence<Reference<XChartType>> xChartTypeSequence(xChartTypeContainer->getChartTypes());
284 if (!xChartTypeSequence.hasElements())
285 continue;
287 for (const auto& rCT : xChartTypeSequence)
289 Reference<XDataSeriesContainer> xDSCont(rCT, UNO_QUERY);
290 if (!xDSCont.is())
291 continue;
293 bool bIsPie
294 = rCT->getChartType().equalsIgnoreAsciiCase("com.sun.star.chart2.PieChartType");
295 if (bIsPie)
297 PropertySet xChartTypeProp(rCT);
298 bIsPie = !xChartTypeProp.getBoolProperty(PROP_UseRings);
300 const Sequence<Reference<XDataSeries>> aDataSeriesSeq = xDSCont->getDataSeries();
301 if (bSwapXAndY)
302 nIndex += aDataSeriesSeq.getLength() - 1;
303 for (const auto& rDataSeries : aDataSeriesSeq)
305 PropertySet aSeriesProp(rDataSeries);
306 bool bVaryColorsByPoint = aSeriesProp.getBoolProperty(PROP_VaryColorsByPoint);
308 if (bVaryColorsByPoint || bIsPie)
310 Reference<XDataSource> xDSrc(rDataSeries, UNO_QUERY);
311 if (!xDSrc.is())
312 continue;
314 const Sequence<Reference<XLabeledDataSequence> > aDataSeqs = xDSrc->getDataSequences();
315 std::vector<sal_Int32> deletedLegendEntries;
316 sal_Int32 j = 0;
317 for (const auto& rDataSeq : aDataSeqs)
319 Reference<XDataSequence> xValues = rDataSeq->getValues();
320 if (!xValues.is())
321 continue;
323 sal_Int32 nDataSeqSize = xValues->getData().getLength();
324 for (sal_Int32 i = 0; i < nDataSeqSize; ++i)
326 for (const auto& rLegendEntry : mrModel.maLegendEntries)
328 if (nIndex == rLegendEntry->mnLegendEntryIdx && rLegendEntry->mbLabelDeleted)
330 deletedLegendEntries.push_back(j + i);
331 break;
334 nIndex++;
336 j += nDataSeqSize;
338 if (deletedLegendEntries.size() > 0)
339 aSeriesProp.setProperty(PROP_DeletedLegendEntries, ContainerHelper::vectorToSequence(deletedLegendEntries));
341 else
343 for (const auto& rLegendEntry : mrModel.maLegendEntries)
345 if (nIndex == rLegendEntry->mnLegendEntryIdx)
347 aSeriesProp.setProperty(PROP_ShowLegendEntry, !rLegendEntry->mbLabelDeleted);
348 break;
351 bSwapXAndY ? nIndex-- : nIndex++;
354 if (bSwapXAndY)
355 nIndex += aDataSeriesSeq.getLength() + 1;
360 } // namespace oox::drawingml::chart
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */