LCOV - code coverage report
Current view: top level - libreoffice/oox/source/drawingml/chart - titleconverter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 103 1.0 %
Date: 2012-12-27 Functions: 2 16 12.5 %
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 "oox/drawingml/chart/titleconverter.hxx"
      21             : 
      22             : #include <com/sun/star/chart/ChartLegendExpansion.hpp>
      23             : #include <com/sun/star/chart2/LegendPosition.hpp>
      24             : #include <com/sun/star/chart2/XDiagram.hpp>
      25             : #include <com/sun/star/chart2/XFormattedString.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"
      34             : 
      35             : namespace oox {
      36             : namespace drawingml {
      37             : namespace chart {
      38             : 
      39             : // ============================================================================
      40             : 
      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;
      45             : 
      46             : using ::oox::core::XmlFilterBase;
      47             : 
      48             : // ============================================================================
      49             : 
      50           0 : TextConverter::TextConverter( const ConverterRoot& rParent, TextModel& rModel ) :
      51           0 :     ConverterBase< TextModel >( rParent, rModel )
      52             : {
      53           0 : }
      54             : 
      55           0 : TextConverter::~TextConverter()
      56             : {
      57           0 : }
      58             : 
      59           0 : Reference< XDataSequence > TextConverter::createDataSequence( const OUString& rRole )
      60             : {
      61           0 :     Reference< XDataSequence > xDataSeq;
      62           0 :     if( mrModel.mxDataSeq.is() )
      63             :     {
      64           0 :         DataSequenceConverter aDataSeqConv( *this, *mrModel.mxDataSeq );
      65           0 :         xDataSeq = aDataSeqConv.createDataSequence( rRole );
      66             :     }
      67           0 :     return xDataSeq;
      68             : }
      69             : 
      70           0 : 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           0 :     ::std::vector< Reference< XFormattedString > > aStringVec;
      75           0 :     if( mrModel.mxTextBody.is() )
      76             :     {
      77             :         // rich-formatted text objects can be created, but currently Chart2 is not able to show them
      78           0 :         const TextParagraphVector& rTextParas = mrModel.mxTextBody->getParagraphs();
      79           0 :         for( TextParagraphVector::const_iterator aPIt = rTextParas.begin(), aPEnd = rTextParas.end(); aPIt != aPEnd; ++aPIt )
      80             :         {
      81           0 :             const TextParagraph& rTextPara = **aPIt;
      82           0 :             const TextCharacterProperties& rParaProps = rTextPara.getProperties().getTextCharacterProperties();
      83           0 :             for( TextRunVector::const_iterator aRIt = rTextPara.getRuns().begin(), aREnd = rTextPara.getRuns().end(); aRIt != aREnd; ++aRIt )
      84             :             {
      85           0 :                 const TextRun& rTextRun = **aRIt;
      86           0 :                 bool bAddNewLine = (aRIt + 1 == aREnd) && (aPIt + 1 != aPEnd);
      87           0 :                 Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, rTextRun.getText(), bAddNewLine );
      88           0 :                 PropertySet aPropSet( xFmtStr );
      89           0 :                 TextCharacterProperties aRunProps( rParaProps );
      90           0 :                 aRunProps.assignUsed( rTextRun.getTextCharacterProperties() );
      91           0 :                 getFormatter().convertTextFormatting( aPropSet, aRunProps, eObjType );
      92           0 :             }
      93             :         }
      94             :     }
      95             :     else
      96             :     {
      97           0 :         OUString aString;
      98             :         // try to create string from linked data
      99           0 :         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           0 :         if( aString.isEmpty() )
     103           0 :             aString = rDefaultText;
     104             : 
     105             :         // create formatted string object
     106           0 :         if( !aString.isEmpty() )
     107             :         {
     108           0 :             Reference< XFormattedString > xFmtStr = appendFormattedString( aStringVec, aString, false );
     109           0 :             PropertySet aPropSet( xFmtStr );
     110           0 :             getFormatter().convertTextFormatting( aPropSet, rxTextProp, eObjType );
     111           0 :         }
     112             :     }
     113             : 
     114           0 :     return ContainerHelper::vectorToSequence( aStringVec );
     115             : }
     116             : 
     117           0 : Reference< XFormattedString > TextConverter::appendFormattedString(
     118             :         ::std::vector< Reference< XFormattedString > >& orStringVec, const OUString& rString, bool bAddNewLine ) const
     119             : {
     120           0 :     Reference< XFormattedString > xFmtStr;
     121             :     try
     122             :     {
     123           0 :         xFmtStr.set( ConverterRoot::createInstance( CREATE_OUSTRING( "com.sun.star.chart2.FormattedString" ) ), UNO_QUERY_THROW );
     124           0 :         xFmtStr->setString( bAddNewLine ? (rString + OUString( sal_Unicode( '\n' ) )) : rString );
     125           0 :         orStringVec.push_back( xFmtStr );
     126             :     }
     127           0 :     catch( Exception& )
     128             :     {
     129             :     }
     130           0 :     return xFmtStr;
     131             : }
     132             : 
     133             : // ============================================================================
     134             : 
     135           0 : TitleConverter::TitleConverter( const ConverterRoot& rParent, TitleModel& rModel ) :
     136           0 :     ConverterBase< TitleModel >( rParent, rModel )
     137             : {
     138           0 : }
     139             : 
     140           0 : TitleConverter::~TitleConverter()
     141             : {
     142           0 : }
     143             : 
     144           0 : void TitleConverter::convertFromModel( const Reference< XTitled >& rxTitled, const OUString& rAutoTitle, ObjectType eObjType, sal_Int32 nMainIdx, sal_Int32 nSubIdx )
     145             : {
     146           0 :     if( rxTitled.is() )
     147             :     {
     148             :         // create the formatted strings
     149           0 :         TextModel& rText = mrModel.mxText.getOrCreate();
     150           0 :         TextConverter aTextConv( *this, rText );
     151           0 :         Sequence< Reference< XFormattedString > > aStringSeq = aTextConv.createStringSequence( rAutoTitle, mrModel.mxTextProp, eObjType );
     152           0 :         if( aStringSeq.hasElements() ) try
     153             :         {
     154             :             // create the title object and set the string data
     155           0 :             Reference< XTitle > xTitle( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.Title" ) ), UNO_QUERY_THROW );
     156           0 :             xTitle->setText( aStringSeq );
     157           0 :             rxTitled->setTitleObject( xTitle );
     158             : 
     159             :             // frame formatting (text formatting already done in TextConverter::createStringSequence())
     160           0 :             PropertySet aPropSet( xTitle );
     161           0 :             getFormatter().convertFrameFormatting( aPropSet, mrModel.mxShapeProp, eObjType );
     162             : 
     163             :             // frame rotation
     164             :             OSL_ENSURE( !mrModel.mxTextProp || !rText.mxTextBody, "TitleConverter::convertFromModel - multiple text properties" );
     165           0 :             ModelRef< TextBody > xTextProp = mrModel.mxTextProp.is() ? mrModel.mxTextProp : rText.mxTextBody;
     166           0 :             getFormatter().convertTextRotation( aPropSet, xTextProp, true );
     167             : 
     168             :             // register the title and layout data for conversion of position
     169           0 :             registerTitleLayout( xTitle, mrModel.mxLayout, eObjType, nMainIdx, nSubIdx );
     170             :         }
     171           0 :         catch( Exception& )
     172             :         {
     173           0 :         }
     174             :     }
     175           0 : }
     176             : 
     177             : // ============================================================================
     178             : 
     179           0 : LegendConverter::LegendConverter( const ConverterRoot& rParent, LegendModel& rModel ) :
     180           0 :     ConverterBase< LegendModel >( rParent, rModel )
     181             : {
     182           0 : }
     183             : 
     184           0 : LegendConverter::~LegendConverter()
     185             : {
     186           0 : }
     187             : 
     188           0 : void LegendConverter::convertFromModel( const Reference< XDiagram >& rxDiagram )
     189             : {
     190           0 :     if( rxDiagram.is() ) try
     191             :     {
     192             :         namespace cssc = ::com::sun::star::chart;
     193             :         namespace cssc2 = ::com::sun::star::chart2;
     194             : 
     195             :         // create the legend
     196           0 :         Reference< XLegend > xLegend( createInstance( CREATE_OUSTRING( "com.sun.star.chart2.Legend" ) ), UNO_QUERY_THROW );
     197           0 :         rxDiagram->setLegend( xLegend );
     198           0 :         PropertySet aPropSet( xLegend );
     199           0 :         aPropSet.setProperty( PROP_Show, true );
     200             : 
     201             :         // legend formatting
     202           0 :         getFormatter().convertFormatting( aPropSet, mrModel.mxShapeProp, mrModel.mxTextProp, OBJECTTYPE_LEGEND );
     203             : 
     204             :         // predefined legend position and expansion
     205           0 :         cssc2::LegendPosition eLegendPos = cssc2::LegendPosition_CUSTOM;
     206           0 :         cssc::ChartLegendExpansion eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
     207           0 :         switch( mrModel.mnPosition )
     208             :         {
     209             :             case XML_l:
     210           0 :                 eLegendPos = cssc2::LegendPosition_LINE_START;
     211           0 :                 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
     212           0 :             break;
     213             :             case XML_r:
     214             :             case XML_tr:    // top-right not supported
     215           0 :                 eLegendPos = cssc2::LegendPosition_LINE_END;
     216           0 :                 eLegendExpand = cssc::ChartLegendExpansion_HIGH;
     217           0 :             break;
     218             :             case XML_t:
     219           0 :                 eLegendPos = cssc2::LegendPosition_PAGE_START;
     220           0 :                 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
     221           0 :             break;
     222             :             case XML_b:
     223           0 :                 eLegendPos = cssc2::LegendPosition_PAGE_END;
     224           0 :                 eLegendExpand = cssc::ChartLegendExpansion_WIDE;
     225           0 :             break;
     226             :         }
     227             : 
     228             :         // manual positioning and size
     229           0 :         if( mrModel.mxLayout.get() )
     230             :         {
     231           0 :             LayoutConverter aLayoutConv( *this, *mrModel.mxLayout );
     232             :             // manual size needs ChartLegendExpansion_CUSTOM
     233           0 :             if( aLayoutConv.convertFromModel( aPropSet ) )
     234           0 :                 eLegendExpand = cssc::ChartLegendExpansion_CUSTOM;
     235             :         }
     236             : 
     237             :         // set position and expansion properties
     238           0 :         aPropSet.setProperty( PROP_AnchorPosition, eLegendPos );
     239           0 :         aPropSet.setProperty( PROP_Expansion, eLegendExpand );
     240             :     }
     241           0 :     catch( Exception& )
     242             :     {
     243             :     }
     244           0 : }
     245             : 
     246             : // ============================================================================
     247             : 
     248             : } // namespace chart
     249             : } // namespace drawingml
     250          51 : } // namespace oox
     251             : 
     252             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10