Update ooo320-m1
[ooovba.git] / oox / source / drawingml / chart / titleconverter.cxx
blob7c866666c8444a80540bb56d86cce24457cdd333
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: titleconverter.cxx,v $
11 * $Revision: 1.4 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "oox/drawingml/chart/titleconverter.hxx"
33 #include <com/sun/star/chart2/XDiagram.hpp>
34 #include <com/sun/star/chart2/XFormattedString.hpp>
35 #include <com/sun/star/chart2/XLegend.hpp>
36 #include <com/sun/star/chart2/XTitle.hpp>
37 #include <com/sun/star/chart2/XTitled.hpp>
38 #include "oox/drawingml/textbody.hxx"
39 #include "oox/drawingml/textparagraph.hxx"
40 #include "oox/drawingml/chart/datasourceconverter.hxx"
41 #include "oox/drawingml/chart/titlemodel.hxx"
43 using ::rtl::OUString;
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::Sequence;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::uno::UNO_QUERY_THROW;
48 using ::com::sun::star::chart2::XDiagram;
49 using ::com::sun::star::chart2::XFormattedString;
50 using ::com::sun::star::chart2::XLegend;
51 using ::com::sun::star::chart2::XTitle;
52 using ::com::sun::star::chart2::XTitled;
53 using ::com::sun::star::chart2::data::XDataSequence;
54 using ::oox::core::XmlFilterBase;
56 namespace oox {
57 namespace drawingml {
58 namespace chart {
60 // ============================================================================
62 TextConverter::TextConverter( const ConverterRoot& rParent, TextModel& rModel ) :
63 ConverterBase< TextModel >( rParent, rModel )
67 TextConverter::~TextConverter()
71 Reference< XDataSequence > TextConverter::createDataSequence( const OUString& rRole )
73 Reference< XDataSequence > xDataSeq;
74 if( mrModel.mxDataSeq.is() )
76 DataSequenceConverter aDataSeqConv( *this, *mrModel.mxDataSeq );
77 xDataSeq = aDataSeqConv.createDataSequence( rRole );
79 return xDataSeq;
82 Sequence< Reference< XFormattedString > > TextConverter::createStringSequence(
83 const OUString& rDefaultText, const ModelRef< TextBody >& rxTextProp, ObjectType eObjType )
85 OSL_ENSURE( !mrModel.mxDataSeq || !mrModel.mxTextBody, "TextConverter::createStringSequence - linked string and rich text found" );
86 ::std::vector< Reference< XFormattedString > > aStringVec;
87 if( mrModel.mxTextBody.is() )
89 // rich-formatted text objects can be created, but currently Chart2 is not able to show them
90 const TextParagraphVector& rTextParas = mrModel.mxTextBody->getParagraphs();
91 for( TextParagraphVector::const_iterator aPIt = rTextParas.begin(), aPEnd = rTextParas.end(); aPIt != aPEnd; ++aPIt )
93 const TextParagraph& rTextPara = **aPIt;
94 const TextCharacterProperties& rParaProps = rTextPara.getProperties().getTextCharacterProperties();
95 for( TextRunVector::const_iterator aRIt = rTextPara.getRuns().begin(), aREnd = rTextPara.getRuns().end(); aRIt != aREnd; ++aRIt )
97 const TextRun& rTextRun = **aRIt;
98 bool bAddNewLine = (aRIt + 1 == aREnd) && (aPIt + 1 != aPEnd);
99 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, rTextRun.getText(), bAddNewLine );
100 PropertySet aPropSet( xFmtStr );
101 TextCharacterProperties aRunProps( rParaProps );
102 aRunProps.assignUsed( rTextRun.getTextCharacterProperties() );
103 getFormatter().convertTextFormatting( aPropSet, aRunProps, eObjType );
107 else
109 OUString aString;
110 // try to create string from linked data
111 if( mrModel.mxDataSeq.is() && !mrModel.mxDataSeq->maData.empty() )
112 mrModel.mxDataSeq->maData.begin()->second >>= aString;
113 // no linked string -> fall back to default string
114 if( aString.getLength() == 0 )
115 aString = rDefaultText;
117 // create formatted string object
118 if( aString.getLength() > 0 )
120 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, aString, false );
121 PropertySet aPropSet( xFmtStr );
122 getFormatter().convertTextFormatting( aPropSet, rxTextProp, eObjType );
126 return ContainerHelper::vectorToSequence( aStringVec );
129 Reference< XFormattedString > TextConverter::appendFormattedString(
130 ::std::vector< Reference< XFormattedString > >& orStringVec, const OUString& rString, bool bAddNewLine ) const
132 Reference< XFormattedString > xFmtStr;
135 xFmtStr.set( ConverterRoot::createInstance( CREATE_OUSTRING( "com.sun.star.chart2.FormattedString" ) ), UNO_QUERY_THROW );
136 xFmtStr->setString( bAddNewLine ? (rString + OUString( sal_Unicode( '\n' ) )) : rString );
137 orStringVec.push_back( xFmtStr );
139 catch( Exception& )
142 return xFmtStr;
145 // ============================================================================
147 TitleConverter::TitleConverter( const ConverterRoot& rParent, TitleModel& rModel ) :
148 ConverterBase< TitleModel >( rParent, rModel )
152 TitleConverter::~TitleConverter()
156 void TitleConverter::convertFromModel( const Reference< XTitled >& rxTitled, const OUString& rAutoTitle, ObjectType eObjType )
158 if( rxTitled.is() )
160 // create the formatted strings
161 TextModel& rText = mrModel.mxText.getOrCreate();
162 TextConverter aTextConv( *this, rText );
163 Sequence< Reference< XFormattedString > > aStringSeq = aTextConv.createStringSequence( rAutoTitle, mrModel.mxTextProp, eObjType );
164 if( aStringSeq.hasElements() ) try
166 // create the title object and set the string data
167 Reference< XTitle > xTitle( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.Title" ) ), UNO_QUERY_THROW );
168 xTitle->setText( aStringSeq );
169 rxTitled->setTitleObject( xTitle );
171 // frame formatting (text formatting already done in TextConverter::createStringSequence())
172 PropertySet aPropSet( xTitle );
173 getFormatter().convertFrameFormatting( aPropSet, mrModel.mxShapeProp, eObjType );
175 // frame rotation
176 OSL_ENSURE( !mrModel.mxTextProp || !rText.mxTextBody, "TitleConverter::convertFromModel - multiple text properties" );
177 ModelRef< TextBody > xTextProp = mrModel.mxTextProp.is() ? mrModel.mxTextProp : rText.mxTextBody;
178 getFormatter().convertTextRotation( aPropSet, xTextProp, true );
180 catch( Exception& )
186 // ============================================================================
188 LegendConverter::LegendConverter( const ConverterRoot& rParent, LegendModel& rModel ) :
189 ConverterBase< LegendModel >( rParent, rModel )
193 LegendConverter::~LegendConverter()
197 void LegendConverter::convertFromModel( const Reference< XDiagram >& rxDiagram )
199 if( rxDiagram.is() ) try
201 // create the legend
202 Reference< XLegend > xLegend( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.Legend" ) ), UNO_QUERY_THROW );
203 rxDiagram->setLegend( xLegend );
205 // legend formatting
206 PropertySet aPropSet( xLegend );
207 getFormatter().convertFormatting( aPropSet, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_LEGEND );
209 catch( Exception& )
214 // ============================================================================
216 } // namespace chart
217 } // namespace drawingml
218 } // namespace oox