Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / drawingml / chart / titleconverter.cxx
blob4f700c32725aa69a2e89dd93a94cf6b383a8d10a
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 <osl/diagnose.h>
30 #include <drawingml/textbody.hxx>
31 #include <drawingml/textparagraph.hxx>
32 #include <drawingml/chart/datasourceconverter.hxx>
33 #include <drawingml/chart/titlemodel.hxx>
34 #include <oox/helper/containerhelper.hxx>
35 #include <oox/token/properties.hxx>
36 #include <oox/token/tokens.hxx>
37 #include <com/sun/star/chart2/RelativePosition.hpp>
38 #include <com/sun/star/drawing/Alignment.hpp>
40 #include <oox/drawingml/chart/modelbase.hxx>
41 namespace oox {
42 namespace drawingml {
43 namespace chart {
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::chart2;
47 using namespace ::com::sun::star::chart2::data;
48 using namespace ::com::sun::star::drawing;
49 using namespace ::com::sun::star::uno;
52 TextConverter::TextConverter( const ConverterRoot& rParent, TextModel& rModel ) :
53 ConverterBase< TextModel >( rParent, rModel )
57 TextConverter::~TextConverter()
61 Reference< XDataSequence > TextConverter::createDataSequence( const OUString& rRole )
63 Reference< XDataSequence > xDataSeq;
64 if( mrModel.mxDataSeq.is() )
66 DataSequenceConverter aDataSeqConv( *this, *mrModel.mxDataSeq );
67 xDataSeq = aDataSeqConv.createDataSequence( rRole );
69 return xDataSeq;
72 Sequence< Reference< XFormattedString > > TextConverter::createStringSequence(
73 const OUString& rDefaultText, const ModelRef< TextBody >& rxTextProp, ObjectType eObjType )
75 OSL_ENSURE( !mrModel.mxDataSeq || !mrModel.mxTextBody, "TextConverter::createStringSequence - linked string and rich text found" );
76 ::std::vector< Reference< XFormattedString > > aStringVec;
77 if( mrModel.mxTextBody.is() )
79 // rich-formatted text objects can be created, but currently Chart2 is not able to show them
80 const TextParagraphVector& rTextParas = mrModel.mxTextBody->getParagraphs();
81 for( TextParagraphVector::const_iterator aPIt = rTextParas.begin(), aPEnd = rTextParas.end(); aPIt != aPEnd; ++aPIt )
83 const TextParagraph& rTextPara = **aPIt;
84 const TextCharacterProperties& rParaProps = rTextPara.getProperties().getTextCharacterProperties();
85 for( TextRunVector::const_iterator aRIt = rTextPara.getRuns().begin(), aREnd = rTextPara.getRuns().end(); aRIt != aREnd; ++aRIt )
87 const TextRun& rTextRun = **aRIt;
88 bool bAddNewLine = ((aRIt + 1 == aREnd) && (aPIt + 1 != aPEnd)) || rTextRun.isLineBreak();
89 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, rTextRun.getText(), bAddNewLine );
90 PropertySet aPropSet( xFmtStr );
91 TextCharacterProperties aRunProps( rParaProps );
92 aRunProps.assignUsed( rTextRun.getTextCharacterProperties() );
93 getFormatter().convertTextFormatting( aPropSet, aRunProps, eObjType );
97 else
99 OUString aString;
100 // try to create string from linked data
101 if( mrModel.mxDataSeq.is() && !mrModel.mxDataSeq->maData.empty() )
102 mrModel.mxDataSeq->maData.begin()->second >>= aString;
103 // no linked string -> fall back to default string
104 if( aString.isEmpty() )
105 aString = rDefaultText;
107 // create formatted string object
108 if( !aString.isEmpty() )
110 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, aString, false );
111 PropertySet aPropSet( xFmtStr );
112 getFormatter().convertTextFormatting( aPropSet, rxTextProp, eObjType );
116 return ContainerHelper::vectorToSequence( aStringVec );
119 Reference< XFormattedString > TextConverter::appendFormattedString(
120 ::std::vector< Reference< XFormattedString > >& orStringVec, const OUString& rString, bool bAddNewLine ) const
122 Reference< XFormattedString2 > xFmtStr;
125 xFmtStr = FormattedString::create( ConverterRoot::getComponentContext() );
126 xFmtStr->setString( bAddNewLine ? (rString + "\n") : rString );
127 orStringVec.emplace_back(xFmtStr );
129 catch( Exception& )
132 return xFmtStr;
135 TitleConverter::TitleConverter( const ConverterRoot& rParent, TitleModel& rModel ) :
136 ConverterBase< TitleModel >( rParent, rModel )
140 TitleConverter::~TitleConverter()
144 void TitleConverter::convertFromModel( const Reference< XTitled >& rxTitled, const OUString& rAutoTitle, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
146 if( rxTitled.is() )
148 // create the formatted strings
149 TextModel& rText = mrModel.mxText.getOrCreate();
150 TextConverter aTextConv( *this, rText );
151 Sequence< Reference< XFormattedString > > aStringSeq = aTextConv.createStringSequence( rAutoTitle, mrModel.mxTextProp, eObjType );
152 if( aStringSeq.hasElements() ) try
154 // create the title object and set the string data
155 Reference< XTitle > xTitle( createInstance( "com.sun.star.chart2.Title" ), UNO_QUERY_THROW );
156 xTitle->setText( aStringSeq );
157 rxTitled->setTitleObject( xTitle );
159 // frame formatting (text formatting already done in TextConverter::createStringSequence())
160 PropertySet aPropSet( xTitle );
161 getFormatter().convertFrameFormatting( aPropSet, mrModel.mxShapeProp, eObjType );
163 // frame rotation
164 OSL_ENSURE( !mrModel.mxTextProp || !rText.mxTextBody, "TitleConverter::convertFromModel - multiple text properties" );
165 ModelRef< TextBody > xTextProp = mrModel.mxTextProp.is() ? mrModel.mxTextProp : rText.mxTextBody;
166 ObjectFormatter::convertTextRotation( aPropSet, xTextProp, true, mrModel.mnDefaultRotation );
168 // register the title and layout data for conversion of position
169 registerTitleLayout( xTitle, mrModel.mxLayout, eObjType, nMainIdx, nSubIdx );
171 catch( Exception& )
177 LegendConverter::LegendConverter( const ConverterRoot& rParent, LegendModel& rModel ) :
178 ConverterBase< LegendModel >( rParent, rModel )
182 LegendConverter::~LegendConverter()
186 void LegendConverter::convertFromModel( const Reference< XDiagram >& rxDiagram )
188 if( rxDiagram.is() ) try
190 namespace cssc = ::com::sun::star::chart;
191 namespace cssc2 = ::com::sun::star::chart2;
193 // create the legend
194 Reference< XLegend > xLegend( createInstance( "com.sun.star.chart2.Legend" ), UNO_QUERY_THROW );
195 rxDiagram->setLegend( xLegend );
196 PropertySet aPropSet( xLegend );
197 aPropSet.setProperty( PROP_Show, true );
199 // legend formatting
200 getFormatter().convertFormatting( aPropSet, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_LEGEND );
202 // predefined legend position and expansion
203 cssc2::LegendPosition eLegendPos = cssc2::LegendPosition_CUSTOM;
204 cssc::ChartLegendExpansion eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
205 RelativePosition eRelPos;
206 bool bTopRight=false;
207 switch( mrModel.mnPosition )
209 case XML_l:
210 eLegendPos = cssc2::LegendPosition_LINE_START;
211 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
212 break;
213 case XML_r:
214 eLegendPos = cssc2::LegendPosition_LINE_END;
215 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
216 break;
217 case XML_tr: // top-right not supported
218 eLegendPos = LegendPosition_CUSTOM;
219 eRelPos.Primary = 1;
220 eRelPos.Secondary =0;
221 eRelPos.Anchor = Alignment_TOP_RIGHT;
222 bTopRight=true;
223 break;
224 case XML_t:
225 eLegendPos = cssc2::LegendPosition_PAGE_START;
226 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
227 break;
228 case XML_b:
229 eLegendPos = cssc2::LegendPosition_PAGE_END;
230 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
231 break;
233 bool bManualLayout=false;
234 // manual positioning and size
235 if( mrModel.mxLayout.get() )
237 LayoutConverter aLayoutConv( *this, *mrModel.mxLayout );
238 // manual size needs ChartLegendExpansion_CUSTOM
239 if( aLayoutConv.convertFromModel( aPropSet ) )
241 eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
243 bManualLayout = !aLayoutConv.getAutoLayout();
246 // set position and expansion properties
247 aPropSet.setProperty( PROP_AnchorPosition, eLegendPos );
248 aPropSet.setProperty( PROP_Expansion, eLegendExpand );
250 if(eLegendPos == LegendPosition_CUSTOM && bTopRight && !bManualLayout)
251 aPropSet.setProperty( PROP_RelativePosition , makeAny(eRelPos));
253 catch( Exception& )
258 } // namespace chart
259 } // namespace drawingml
260 } // namespace oox
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */