LCOV - code coverage report
Current view: top level - oox/source/drawingml/chart - titleconverter.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 105 115 91.3 %
Date: 2014-11-03 Functions: 13 16 81.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "drawingml/chart/titleconverter.hxx"
      21             : 
      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 "drawingml/textbody.hxx"
      30             : #include "drawingml/textparagraph.hxx"
      31             : #include "drawingml/chart/datasourceconverter.hxx"
      32             : #include "drawingml/chart/titlemodel.hxx"
      33             : #include "oox/helper/containerhelper.hxx"
      34             : #include <com/sun/star/chart2/RelativePosition.hpp>
      35             : #include <com/sun/star/drawing/Alignment.hpp>
      36             : 
      37             : #include "oox/drawingml/chart/modelbase.hxx"
      38             : namespace oox {
      39             : namespace drawingml {
      40             : namespace chart {
      41             : 
      42             : using namespace ::com::sun::star::awt;
      43             : using namespace ::com::sun::star::chart2;
      44             : using namespace ::com::sun::star::chart2::data;
      45             : using namespace ::com::sun::star::drawing;
      46             : using namespace ::com::sun::star::uno;
      47             : 
      48             : using ::oox::core::XmlFilterBase;
      49             : 
      50         572 : TextConverter::TextConverter( const ConverterRoot& rParent, TextModel& rModel ) :
      51         572 :     ConverterBase< TextModel >( rParent, rModel )
      52             : {
      53         572 : }
      54             : 
      55         572 : TextConverter::~TextConverter()
      56             : {
      57         572 : }
      58             : 
      59         478 : Reference< XDataSequence > TextConverter::createDataSequence( const OUString& rRole )
      60             : {
      61         478 :     Reference< XDataSequence > xDataSeq;
      62         478 :     if( mrModel.mxDataSeq.is() )
      63             :     {
      64         478 :         DataSequenceConverter aDataSeqConv( *this, *mrModel.mxDataSeq );
      65         478 :         xDataSeq = aDataSeqConv.createDataSequence( rRole );
      66             :     }
      67         478 :     return xDataSeq;
      68             : }
      69             : 
      70          94 : Sequence< Reference< XFormattedString > > TextConverter::createStringSequence(
      71             :         const OUString& rDefaultText, const ModelRef< TextBody >& rxTextProp, ObjectType eObjType )
      72             : {
      73             :     OSL_ENSURE( !mrModel.mxDataSeq || !mrModel.mxTextBody, "TextConverter::createStringSequence - linked string and rich text found" );
      74          94 :     ::std::vector< Reference< XFormattedString > > aStringVec;
      75          94 :     if( mrModel.mxTextBody.is() )
      76             :     {
      77             :         // rich-formatted text objects can be created, but currently Chart2 is not able to show them
      78          56 :         const TextParagraphVector& rTextParas = mrModel.mxTextBody->getParagraphs();
      79         112 :         for( TextParagraphVector::const_iterator aPIt = rTextParas.begin(), aPEnd = rTextParas.end(); aPIt != aPEnd; ++aPIt )
      80             :         {
      81          56 :             const TextParagraph& rTextPara = **aPIt;
      82          56 :             const TextCharacterProperties& rParaProps = rTextPara.getProperties().getTextCharacterProperties();
      83         140 :             for( TextRunVector::const_iterator aRIt = rTextPara.getRuns().begin(), aREnd = rTextPara.getRuns().end(); aRIt != aREnd; ++aRIt )
      84             :             {
      85          84 :                 const TextRun& rTextRun = **aRIt;
      86          84 :                 bool bAddNewLine = (aRIt + 1 == aREnd) && (aPIt + 1 != aPEnd);
      87          84 :                 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, rTextRun.getText(), bAddNewLine );
      88         168 :                 PropertySet aPropSet( xFmtStr );
      89         168 :                 TextCharacterProperties aRunProps( rParaProps );
      90          84 :                 aRunProps.assignUsed( rTextRun.getTextCharacterProperties() );
      91          84 :                 getFormatter().convertTextFormatting( aPropSet, aRunProps, eObjType );
      92          84 :             }
      93             :         }
      94             :     }
      95             :     else
      96             :     {
      97          38 :         OUString aString;
      98             :         // try to create string from linked data
      99          38 :         if( mrModel.mxDataSeq.is() && !mrModel.mxDataSeq->maData.empty() )
     100           0 :             mrModel.mxDataSeq->maData.begin()->second >>= aString;
     101             :         // no linked string -> fall back to default string
     102          38 :         if( aString.isEmpty() )
     103          38 :             aString = rDefaultText;
     104             : 
     105             :         // create formatted string object
     106          38 :         if( !aString.isEmpty() )
     107             :         {
     108          38 :             Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, aString, false );
     109          76 :             PropertySet aPropSet( xFmtStr );
     110          76 :             getFormatter().convertTextFormatting( aPropSet, rxTextProp, eObjType );
     111          38 :         }
     112             :     }
     113             : 
     114          94 :     return ContainerHelper::vectorToSequence( aStringVec );
     115             : }
     116             : 
     117         122 : Reference< XFormattedString > TextConverter::appendFormattedString(
     118             :         ::std::vector< Reference< XFormattedString > >& orStringVec, const OUString& rString, bool bAddNewLine ) const
     119             : {
     120         122 :     Reference< XFormattedString2 > xFmtStr;
     121             :     try
     122             :     {
     123         122 :         xFmtStr = FormattedString::create( ConverterRoot::getComponentContext() );
     124         122 :         xFmtStr->setString( bAddNewLine ? (rString + OUString( '\n' )) : rString );
     125         122 :         orStringVec.push_back( xFmtStr );
     126             :     }
     127           0 :     catch( Exception& )
     128             :     {
     129             :     }
     130         122 :     return xFmtStr;
     131             : }
     132             : 
     133          94 : TitleConverter::TitleConverter( const ConverterRoot& rParent, TitleModel& rModel ) :
     134          94 :     ConverterBase< TitleModel >( rParent, rModel )
     135             : {
     136          94 : }
     137             : 
     138          94 : TitleConverter::~TitleConverter()
     139             : {
     140          94 : }
     141             : 
     142          94 : void TitleConverter::convertFromModel( const Reference< XTitled >& rxTitled, const OUString& rAutoTitle, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
     143             : {
     144          94 :     if( rxTitled.is() )
     145             :     {
     146             :         // create the formatted strings
     147          94 :         TextModel& rText = mrModel.mxText.getOrCreate();
     148          94 :         TextConverter aTextConv( *this, rText );
     149         188 :         Sequence< Reference< XFormattedString > > aStringSeq = aTextConv.createStringSequence( rAutoTitle, mrModel.mxTextProp, eObjType );
     150          94 :         if( aStringSeq.hasElements() ) try
     151             :         {
     152             :             // create the title object and set the string data
     153          94 :             Reference< XTitle > xTitle( createInstance( "com.sun.star.chart2.Title" ), UNO_QUERY_THROW );
     154          94 :             xTitle->setText( aStringSeq );
     155          94 :             rxTitled->setTitleObject( xTitle );
     156             : 
     157             :             // frame formatting (text formatting already done in TextConverter::createStringSequence())
     158         188 :             PropertySet aPropSet( xTitle );
     159          94 :             getFormatter().convertFrameFormatting( aPropSet, mrModel.mxShapeProp, eObjType );
     160             : 
     161             :             // frame rotation
     162             :             OSL_ENSURE( !mrModel.mxTextProp || !rText.mxTextBody, "TitleConverter::convertFromModel - multiple text properties" );
     163         188 :             ModelRef< TextBody > xTextProp = mrModel.mxTextProp.is() ? mrModel.mxTextProp : rText.mxTextBody;
     164          94 :             getFormatter().convertTextRotation( aPropSet, xTextProp, true );
     165             : 
     166             :             // register the title and layout data for conversion of position
     167         188 :             registerTitleLayout( xTitle, mrModel.mxLayout, eObjType, nMainIdx, nSubIdx );
     168             :         }
     169           0 :         catch( Exception& )
     170             :         {
     171          94 :         }
     172             :     }
     173          94 : }
     174             : 
     175         182 : LegendConverter::LegendConverter( const ConverterRoot& rParent, LegendModel& rModel ) :
     176         182 :     ConverterBase< LegendModel >( rParent, rModel )
     177             : {
     178         182 : }
     179             : 
     180         182 : LegendConverter::~LegendConverter()
     181             : {
     182         182 : }
     183             : 
     184         182 : void LegendConverter::convertFromModel( const Reference< XDiagram >& rxDiagram )
     185             : {
     186         182 :     if( rxDiagram.is() ) try
     187             :     {
     188             :         namespace cssc = ::com::sun::star::chart;
     189             :         namespace cssc2 = ::com::sun::star::chart2;
     190             : 
     191             :         // create the legend
     192         182 :         Reference< XLegend > xLegend( createInstance( "com.sun.star.chart2.Legend" ), UNO_QUERY_THROW );
     193         182 :         rxDiagram->setLegend( xLegend );
     194         364 :         PropertySet aPropSet( xLegend );
     195         182 :         aPropSet.setProperty( PROP_Show, true );
     196             : 
     197             :         // legend formatting
     198         182 :         getFormatter().convertFormatting( aPropSet, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_LEGEND );
     199             : 
     200             :         // predefined legend position and expansion
     201         182 :         cssc2::LegendPosition eLegendPos = cssc2::LegendPosition_CUSTOM;
     202         182 :         cssc::ChartLegendExpansion eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
     203         182 :         RelativePosition eRelPos;
     204         182 :         bool bTopRight=false;
     205         182 :         switch( mrModel.mnPosition )
     206             :         {
     207             :             case XML_l:
     208           4 :                 eLegendPos = cssc2::LegendPosition_LINE_START;
     209           4 :                 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
     210           4 :             break;
     211             :             case XML_r:
     212         154 :                 eLegendPos = cssc2::LegendPosition_LINE_END;
     213         154 :                 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
     214         154 :                 break;
     215             :             case XML_tr:    // top-right not supported
     216           0 :                 eLegendPos = LegendPosition_CUSTOM;
     217           0 :                 eRelPos.Primary = 1;
     218           0 :                 eRelPos.Secondary =0;
     219           0 :                 eRelPos.Anchor = Alignment_TOP_RIGHT;
     220           0 :                 bTopRight=true;
     221             : 
     222           0 :             break;
     223             :             case XML_t:
     224           4 :                 eLegendPos = cssc2::LegendPosition_PAGE_START;
     225           4 :                 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
     226           4 :             break;
     227             :             case XML_b:
     228          20 :                 eLegendPos = cssc2::LegendPosition_PAGE_END;
     229          20 :                 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
     230          20 :             break;
     231             :         }
     232         182 :         bool bManualLayout=false;
     233             :         // manual positioning and size
     234         182 :         if( mrModel.mxLayout.get() )
     235             :         {
     236          82 :             LayoutConverter aLayoutConv( *this, *mrModel.mxLayout );
     237             :             // manual size needs ChartLegendExpansion_CUSTOM
     238          82 :             if( aLayoutConv.convertFromModel( aPropSet ) )
     239           4 :                 eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
     240          82 :             bManualLayout = !aLayoutConv.getAutoLayout();
     241             :         }
     242             : 
     243             :         // set position and expansion properties
     244         182 :         aPropSet.setProperty( PROP_AnchorPosition, eLegendPos );
     245         182 :         aPropSet.setProperty( PROP_Expansion, eLegendExpand );
     246             : 
     247         182 :         if(eLegendPos == LegendPosition_CUSTOM && bTopRight && bManualLayout==false)
     248         182 :             aPropSet.setProperty( PROP_RelativePosition , makeAny(eRelPos));
     249             :     }
     250           0 :     catch( Exception& )
     251             :     {
     252             :     }
     253         182 : }
     254             : 
     255             : } // namespace chart
     256             : } // namespace drawingml
     257         408 : } // namespace oox
     258             : 
     259             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10