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 "oox/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 "oox/drawingml/textbody.hxx"
30 #include "oox/drawingml/textparagraph.hxx"
31 #include "oox/drawingml/chart/datasourceconverter.hxx"
32 #include "oox/drawingml/chart/titlemodel.hxx"
33 #include "oox/helper/containerhelper.hxx"
39 // ============================================================================
41 using namespace ::com::sun::star::awt
;
42 using namespace ::com::sun::star::chart2
;
43 using namespace ::com::sun::star::chart2::data
;
44 using namespace ::com::sun::star::uno
;
46 using ::oox::core::XmlFilterBase
;
48 // ============================================================================
50 TextConverter::TextConverter( const ConverterRoot
& rParent
, TextModel
& rModel
) :
51 ConverterBase
< TextModel
>( rParent
, rModel
)
55 TextConverter::~TextConverter()
59 Reference
< XDataSequence
> TextConverter::createDataSequence( const OUString
& rRole
)
61 Reference
< XDataSequence
> xDataSeq
;
62 if( mrModel
.mxDataSeq
.is() )
64 DataSequenceConverter
aDataSeqConv( *this, *mrModel
.mxDataSeq
);
65 xDataSeq
= aDataSeqConv
.createDataSequence( rRole
);
70 Sequence
< Reference
< XFormattedString
> > TextConverter::createStringSequence(
71 const OUString
& rDefaultText
, const ModelRef
< TextBody
>& rxTextProp
, ObjectType eObjType
)
73 OSL_ENSURE( !mrModel
.mxDataSeq
|| !mrModel
.mxTextBody
, "TextConverter::createStringSequence - linked string and rich text found" );
74 ::std::vector
< Reference
< XFormattedString
> > aStringVec
;
75 if( mrModel
.mxTextBody
.is() )
77 // rich-formatted text objects can be created, but currently Chart2 is not able to show them
78 const TextParagraphVector
& rTextParas
= mrModel
.mxTextBody
->getParagraphs();
79 for( TextParagraphVector::const_iterator aPIt
= rTextParas
.begin(), aPEnd
= rTextParas
.end(); aPIt
!= aPEnd
; ++aPIt
)
81 const TextParagraph
& rTextPara
= **aPIt
;
82 const TextCharacterProperties
& rParaProps
= rTextPara
.getProperties().getTextCharacterProperties();
83 for( TextRunVector::const_iterator aRIt
= rTextPara
.getRuns().begin(), aREnd
= rTextPara
.getRuns().end(); aRIt
!= aREnd
; ++aRIt
)
85 const TextRun
& rTextRun
= **aRIt
;
86 bool bAddNewLine
= (aRIt
+ 1 == aREnd
) && (aPIt
+ 1 != aPEnd
);
87 Reference
< XFormattedString
> xFmtStr
= appendFormattedString( aStringVec
, rTextRun
.getText(), bAddNewLine
);
88 PropertySet
aPropSet( xFmtStr
);
89 TextCharacterProperties
aRunProps( rParaProps
);
90 aRunProps
.assignUsed( rTextRun
.getTextCharacterProperties() );
91 getFormatter().convertTextFormatting( aPropSet
, aRunProps
, eObjType
);
98 // try to create string from linked data
99 if( mrModel
.mxDataSeq
.is() && !mrModel
.mxDataSeq
->maData
.empty() )
100 mrModel
.mxDataSeq
->maData
.begin()->second
>>= aString
;
101 // no linked string -> fall back to default string
102 if( aString
.isEmpty() )
103 aString
= rDefaultText
;
105 // create formatted string object
106 if( !aString
.isEmpty() )
108 Reference
< XFormattedString
> xFmtStr
= appendFormattedString( aStringVec
, aString
, false );
109 PropertySet
aPropSet( xFmtStr
);
110 getFormatter().convertTextFormatting( aPropSet
, rxTextProp
, eObjType
);
114 return ContainerHelper::vectorToSequence( aStringVec
);
117 Reference
< XFormattedString
> TextConverter::appendFormattedString(
118 ::std::vector
< Reference
< XFormattedString
> >& orStringVec
, const OUString
& rString
, bool bAddNewLine
) const
120 Reference
< XFormattedString2
> xFmtStr
;
123 xFmtStr
= FormattedString::create( ConverterRoot::getComponentContext() );
124 xFmtStr
->setString( bAddNewLine
? (rString
+ OUString( sal_Unicode( '\n' ) )) : rString
);
125 orStringVec
.push_back( xFmtStr
);
133 // ============================================================================
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
)
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
);
164 OSL_ENSURE( !mrModel
.mxTextProp
|| !rText
.mxTextBody
, "TitleConverter::convertFromModel - multiple text properties" );
165 ModelRef
< TextBody
> xTextProp
= mrModel
.mxTextProp
.is() ? mrModel
.mxTextProp
: rText
.mxTextBody
;
166 getFormatter().convertTextRotation( aPropSet
, xTextProp
, true );
168 // register the title and layout data for conversion of position
169 registerTitleLayout( xTitle
, mrModel
.mxLayout
, eObjType
, nMainIdx
, nSubIdx
);
177 // ============================================================================
179 LegendConverter::LegendConverter( const ConverterRoot
& rParent
, LegendModel
& rModel
) :
180 ConverterBase
< LegendModel
>( rParent
, rModel
)
184 LegendConverter::~LegendConverter()
188 void LegendConverter::convertFromModel( const Reference
< XDiagram
>& rxDiagram
)
190 if( rxDiagram
.is() ) try
192 namespace cssc
= ::com::sun::star::chart
;
193 namespace cssc2
= ::com::sun::star::chart2
;
196 Reference
< XLegend
> xLegend( createInstance( "com.sun.star.chart2.Legend" ), UNO_QUERY_THROW
);
197 rxDiagram
->setLegend( xLegend
);
198 PropertySet
aPropSet( xLegend
);
199 aPropSet
.setProperty( PROP_Show
, true );
202 getFormatter().convertFormatting( aPropSet
, mrModel
.mxShapeProp
, mrModel
.mxTextProp
, OBJECTTYPE_LEGEND
);
204 // predefined legend position and expansion
205 cssc2::LegendPosition eLegendPos
= cssc2::LegendPosition_CUSTOM
;
206 cssc::ChartLegendExpansion eLegendExpand
= cssc::ChartLegendExpansion_CUSTOM
;
207 switch( mrModel
.mnPosition
)
210 eLegendPos
= cssc2::LegendPosition_LINE_START
;
211 eLegendExpand
= cssc::ChartLegendExpansion_HIGH
;
214 case XML_tr
: // top-right not supported
215 eLegendPos
= cssc2::LegendPosition_LINE_END
;
216 eLegendExpand
= cssc::ChartLegendExpansion_HIGH
;
219 eLegendPos
= cssc2::LegendPosition_PAGE_START
;
220 eLegendExpand
= cssc::ChartLegendExpansion_WIDE
;
223 eLegendPos
= cssc2::LegendPosition_PAGE_END
;
224 eLegendExpand
= cssc::ChartLegendExpansion_WIDE
;
228 // manual positioning and size
229 if( mrModel
.mxLayout
.get() )
231 LayoutConverter
aLayoutConv( *this, *mrModel
.mxLayout
);
232 // manual size needs ChartLegendExpansion_CUSTOM
233 if( aLayoutConv
.convertFromModel( aPropSet
) )
234 eLegendExpand
= cssc::ChartLegendExpansion_CUSTOM
;
237 // set position and expansion properties
238 aPropSet
.setProperty( PROP_AnchorPosition
, eLegendPos
);
239 aPropSet
.setProperty( PROP_Expansion
, eLegendExpand
);
246 // ============================================================================
249 } // namespace drawingml
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */