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 "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 <com/sun/star/chart2/RelativePosition.hpp>
36 #include <com/sun/star/drawing/Alignment.hpp>
38 #include "oox/drawingml/chart/modelbase.hxx"
43 using namespace ::com::sun::star::awt
;
44 using namespace ::com::sun::star::chart2
;
45 using namespace ::com::sun::star::chart2::data
;
46 using namespace ::com::sun::star::drawing
;
47 using namespace ::com::sun::star::uno
;
49 using ::oox::core::XmlFilterBase
;
51 TextConverter::TextConverter( const ConverterRoot
& rParent
, TextModel
& rModel
) :
52 ConverterBase
< TextModel
>( rParent
, rModel
)
56 TextConverter::~TextConverter()
60 Reference
< XDataSequence
> TextConverter::createDataSequence( const OUString
& rRole
)
62 Reference
< XDataSequence
> xDataSeq
;
63 if( mrModel
.mxDataSeq
.is() )
65 DataSequenceConverter
aDataSeqConv( *this, *mrModel
.mxDataSeq
);
66 xDataSeq
= aDataSeqConv
.createDataSequence( rRole
);
71 Sequence
< Reference
< XFormattedString
> > TextConverter::createStringSequence(
72 const OUString
& rDefaultText
, const ModelRef
< TextBody
>& rxTextProp
, ObjectType eObjType
)
74 OSL_ENSURE( !mrModel
.mxDataSeq
|| !mrModel
.mxTextBody
, "TextConverter::createStringSequence - linked string and rich text found" );
75 ::std::vector
< Reference
< XFormattedString
> > aStringVec
;
76 if( mrModel
.mxTextBody
.is() )
78 // rich-formatted text objects can be created, but currently Chart2 is not able to show them
79 const TextParagraphVector
& rTextParas
= mrModel
.mxTextBody
->getParagraphs();
80 for( TextParagraphVector::const_iterator aPIt
= rTextParas
.begin(), aPEnd
= rTextParas
.end(); aPIt
!= aPEnd
; ++aPIt
)
82 const TextParagraph
& rTextPara
= **aPIt
;
83 const TextCharacterProperties
& rParaProps
= rTextPara
.getProperties().getTextCharacterProperties();
84 for( TextRunVector::const_iterator aRIt
= rTextPara
.getRuns().begin(), aREnd
= rTextPara
.getRuns().end(); aRIt
!= aREnd
; ++aRIt
)
86 const TextRun
& rTextRun
= **aRIt
;
87 bool bAddNewLine
= (aRIt
+ 1 == aREnd
) && (aPIt
+ 1 != aPEnd
);
88 Reference
< XFormattedString
> xFmtStr
= appendFormattedString( aStringVec
, rTextRun
.getText(), bAddNewLine
);
89 PropertySet
aPropSet( xFmtStr
);
90 TextCharacterProperties
aRunProps( rParaProps
);
91 aRunProps
.assignUsed( rTextRun
.getTextCharacterProperties() );
92 getFormatter().convertTextFormatting( aPropSet
, aRunProps
, eObjType
);
99 // try to create string from linked data
100 if( mrModel
.mxDataSeq
.is() && !mrModel
.mxDataSeq
->maData
.empty() )
101 mrModel
.mxDataSeq
->maData
.begin()->second
>>= aString
;
102 // no linked string -> fall back to default string
103 if( aString
.isEmpty() )
104 aString
= rDefaultText
;
106 // create formatted string object
107 if( !aString
.isEmpty() )
109 Reference
< XFormattedString
> xFmtStr
= appendFormattedString( aStringVec
, aString
, false );
110 PropertySet
aPropSet( xFmtStr
);
111 getFormatter().convertTextFormatting( aPropSet
, rxTextProp
, eObjType
);
115 return ContainerHelper::vectorToSequence( aStringVec
);
118 Reference
< XFormattedString
> TextConverter::appendFormattedString(
119 ::std::vector
< Reference
< XFormattedString
> >& orStringVec
, const OUString
& rString
, bool bAddNewLine
) const
121 Reference
< XFormattedString2
> xFmtStr
;
124 xFmtStr
= FormattedString::create( ConverterRoot::getComponentContext() );
125 xFmtStr
->setString( bAddNewLine
? (rString
+ "\n") : rString
);
126 orStringVec
.push_back( xFmtStr
);
134 TitleConverter::TitleConverter( const ConverterRoot
& rParent
, TitleModel
& rModel
) :
135 ConverterBase
< TitleModel
>( rParent
, rModel
)
139 TitleConverter::~TitleConverter()
143 void TitleConverter::convertFromModel( const Reference
< XTitled
>& rxTitled
, const OUString
& rAutoTitle
, ObjectType eObjType
, sal_Int32 nMainIdx
, sal_Int32 nSubIdx
)
147 // create the formatted strings
148 TextModel
& rText
= mrModel
.mxText
.getOrCreate();
149 TextConverter
aTextConv( *this, rText
);
150 Sequence
< Reference
< XFormattedString
> > aStringSeq
= aTextConv
.createStringSequence( rAutoTitle
, mrModel
.mxTextProp
, eObjType
);
151 if( aStringSeq
.hasElements() ) try
153 // create the title object and set the string data
154 Reference
< XTitle
> xTitle( createInstance( "com.sun.star.chart2.Title" ), UNO_QUERY_THROW
);
155 xTitle
->setText( aStringSeq
);
156 rxTitled
->setTitleObject( xTitle
);
158 // frame formatting (text formatting already done in TextConverter::createStringSequence())
159 PropertySet
aPropSet( xTitle
);
160 getFormatter().convertFrameFormatting( aPropSet
, mrModel
.mxShapeProp
, eObjType
);
163 OSL_ENSURE( !mrModel
.mxTextProp
|| !rText
.mxTextBody
, "TitleConverter::convertFromModel - multiple text properties" );
164 ModelRef
< TextBody
> xTextProp
= mrModel
.mxTextProp
.is() ? mrModel
.mxTextProp
: rText
.mxTextBody
;
165 ObjectFormatter::convertTextRotation( aPropSet
, xTextProp
, true, mrModel
.mnDefaultRotation
);
167 // register the title and layout data for conversion of position
168 registerTitleLayout( xTitle
, mrModel
.mxLayout
, eObjType
, nMainIdx
, nSubIdx
);
176 LegendConverter::LegendConverter( const ConverterRoot
& rParent
, LegendModel
& rModel
) :
177 ConverterBase
< LegendModel
>( rParent
, rModel
)
181 LegendConverter::~LegendConverter()
185 void LegendConverter::convertFromModel( const Reference
< XDiagram
>& rxDiagram
)
187 if( rxDiagram
.is() ) try
189 namespace cssc
= ::com::sun::star::chart
;
190 namespace cssc2
= ::com::sun::star::chart2
;
193 Reference
< XLegend
> xLegend( createInstance( "com.sun.star.chart2.Legend" ), UNO_QUERY_THROW
);
194 rxDiagram
->setLegend( xLegend
);
195 PropertySet
aPropSet( xLegend
);
196 aPropSet
.setProperty( PROP_Show
, true );
199 getFormatter().convertFormatting( aPropSet
, mrModel
.mxShapeProp
, mrModel
.mxTextProp
, OBJECTTYPE_LEGEND
);
201 // predefined legend position and expansion
202 cssc2::LegendPosition eLegendPos
= cssc2::LegendPosition_CUSTOM
;
203 cssc::ChartLegendExpansion eLegendExpand
= cssc::ChartLegendExpansion_CUSTOM
;
204 RelativePosition eRelPos
;
205 bool bTopRight
=false;
206 switch( mrModel
.mnPosition
)
209 eLegendPos
= cssc2::LegendPosition_LINE_START
;
210 eLegendExpand
= cssc::ChartLegendExpansion_HIGH
;
213 eLegendPos
= cssc2::LegendPosition_LINE_END
;
214 eLegendExpand
= cssc::ChartLegendExpansion_HIGH
;
216 case XML_tr
: // top-right not supported
217 eLegendPos
= LegendPosition_CUSTOM
;
219 eRelPos
.Secondary
=0;
220 eRelPos
.Anchor
= Alignment_TOP_RIGHT
;
225 eLegendPos
= cssc2::LegendPosition_PAGE_START
;
226 eLegendExpand
= cssc::ChartLegendExpansion_WIDE
;
229 eLegendPos
= cssc2::LegendPosition_PAGE_END
;
230 eLegendExpand
= cssc::ChartLegendExpansion_WIDE
;
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
) )
240 eLegendExpand
= cssc::ChartLegendExpansion_CUSTOM
;
241 bManualLayout
= !aLayoutConv
.getAutoLayout();
244 // set position and expansion properties
245 aPropSet
.setProperty( PROP_AnchorPosition
, eLegendPos
);
246 aPropSet
.setProperty( PROP_Expansion
, eLegendExpand
);
248 if(eLegendPos
== LegendPosition_CUSTOM
&& bTopRight
&& !bManualLayout
)
249 aPropSet
.setProperty( PROP_RelativePosition
, makeAny(eRelPos
));
257 } // namespace drawingml
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */