LCOV - code coverage report
Current view: top level - chart2/source/tools - TitleHelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 161 190 84.7 %
Date: 2014-11-03 Functions: 13 13 100.0 %
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 "TitleHelper.hxx"
      21             : #include "ChartModelHelper.hxx"
      22             : #include "macros.hxx"
      23             : #include "AxisHelper.hxx"
      24             : #include "DiagramHelper.hxx"
      25             : #include <com/sun/star/chart2/FormattedString.hpp>
      26             : #include <com/sun/star/chart2/XChartDocument.hpp>
      27             : #include <rtl/ustrbuf.hxx>
      28             : 
      29             : namespace chart
      30             : {
      31             : 
      32             : using namespace ::com::sun::star;
      33             : using namespace ::com::sun::star::chart2;
      34             : using ::com::sun::star::uno::Reference;
      35             : 
      36             : namespace {
      37             : 
      38       53768 : uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
      39             :       TitleHelper::eTitleType nTitleIndex
      40             :     , const uno::Reference< XDiagram >& xDiagram )
      41             : {
      42       53768 :     uno::Reference< XTitled > xResult;
      43             : 
      44       53768 :     if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION ||
      45             :         nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
      46             :     {
      47        1320 :         bool bDummy = false;
      48        1320 :         bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
      49             : 
      50        1320 :         if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
      51         660 :             nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
      52             :         else
      53         660 :             nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
      54             :     }
      55             : 
      56       53768 :     switch( nTitleIndex )
      57             :     {
      58             :         case TitleHelper::SUB_TITLE:
      59        5808 :             if( xDiagram.is())
      60        5624 :                 xResult.set( xDiagram, uno::UNO_QUERY );
      61        5808 :             break;
      62             :         case TitleHelper::X_AXIS_TITLE:
      63       18373 :             if( xDiagram.is())
      64       18189 :                 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
      65       18373 :             break;
      66             :         case TitleHelper::Y_AXIS_TITLE:
      67       18329 :             if( xDiagram.is())
      68       18145 :                 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
      69       18329 :             break;
      70             :         case TitleHelper::Z_AXIS_TITLE:
      71        3062 :             if( xDiagram.is())
      72        2878 :                 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
      73        3062 :             break;
      74             :         case TitleHelper::SECONDARY_X_AXIS_TITLE:
      75        3512 :             if( xDiagram.is())
      76        3328 :                 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
      77        3512 :             break;
      78             :         case TitleHelper::SECONDARY_Y_AXIS_TITLE:
      79        4684 :             if( xDiagram.is())
      80        4500 :                 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
      81        4684 :             break;
      82             : 
      83             :         case TitleHelper::MAIN_TITLE:
      84             :         default:
      85             :             OSL_FAIL( "Unsupported Title-Type requested" );
      86           0 :             break;
      87             :     }
      88             : 
      89       53768 :     return xResult;
      90             : }
      91             : 
      92       53768 : uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
      93             :                                               , const uno::Reference< XDiagram >& xDiagram )
      94             : {
      95       53768 :     uno::Reference< XTitled > xResult;
      96       53768 :     switch( nTitleIndex )
      97             :     {
      98             :         case TitleHelper::MAIN_TITLE:
      99             :             SAL_WARN("chart2", "should not be reached");
     100           0 :             break;
     101             :         case TitleHelper::SUB_TITLE:
     102             :         case TitleHelper::X_AXIS_TITLE:
     103             :         case TitleHelper::Y_AXIS_TITLE:
     104             :         case TitleHelper::Z_AXIS_TITLE:
     105             :         case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
     106             :         case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
     107             :         case TitleHelper::SECONDARY_X_AXIS_TITLE:
     108             :         case TitleHelper::SECONDARY_Y_AXIS_TITLE:
     109       53768 :             xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
     110       53768 :             break;
     111             :         default:
     112             :             OSL_FAIL( "Unsupported Title-Type requested" );
     113           0 :             break;
     114             :     }
     115             : 
     116       53768 :     return xResult;
     117             : }
     118             : 
     119         166 : uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
     120             :                                               , const uno::Reference< frame::XModel >& xModel )
     121             : {
     122         166 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     123             :     {
     124          74 :         uno::Reference< XTitled > xTitled( xModel, uno::UNO_QUERY );
     125          74 :         return xTitled;
     126             :     }
     127             : 
     128          92 :     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
     129         184 :     uno::Reference< XDiagram > xDiagram;
     130             : 
     131          92 :     if( xChartDoc.is())
     132          92 :         xDiagram.set( xChartDoc->getFirstDiagram());
     133             : 
     134         184 :     return lcl_getTitleParent( nTitleIndex, xDiagram );
     135             : }
     136             : 
     137             : }
     138             : 
     139        4767 : uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
     140             :                             , ChartModel& rModel )
     141             : {
     142        4767 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     143        1200 :         return rModel.getTitleObject();
     144             : 
     145        3567 :     uno::Reference< XDiagram > xDiagram( rModel.getFirstDiagram(), uno::UNO_QUERY );
     146        7134 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xDiagram ) );
     147        3567 :     if( xTitled.is())
     148        1785 :         return xTitled->getTitleObject();
     149        5349 :     return NULL;
     150             : }
     151             : 
     152       74756 : uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
     153             :                             , const uno::Reference< frame::XModel >& xModel )
     154             : {
     155       74756 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     156             :     {
     157       24647 :         uno::Reference< XTitled > xTitled( xModel, uno::UNO_QUERY );
     158       24647 :         return xTitled->getTitleObject();
     159             :     }
     160             : 
     161       50109 :     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
     162      100218 :     uno::Reference< XDiagram > xDiagram;
     163             : 
     164       50109 :     if( xChartDoc.is())
     165       50109 :         xDiagram.set( xChartDoc->getFirstDiagram());
     166             : 
     167      100218 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xDiagram ) );
     168       50109 :     if( xTitled.is())
     169       41985 :         return xTitled->getTitleObject();
     170       58233 :     return NULL;
     171             : }
     172             : 
     173         160 : uno::Reference< XTitle > TitleHelper::createTitle(
     174             :       TitleHelper::eTitleType eTitleType
     175             :     , const OUString& rTitleText
     176             :     , const uno::Reference< frame::XModel >& xModel
     177             :     , const uno::Reference< uno::XComponentContext > & xContext
     178             :     , ReferenceSizeProvider * pRefSizeProvider )
     179             : {
     180         160 :     uno::Reference< XTitle > xTitle;
     181         320 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
     182             : 
     183         160 :     if( !xTitled.is() )
     184             :     {
     185           2 :         uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
     186           4 :         uno::Reference< chart2::XAxis > xAxis;
     187           2 :         switch( eTitleType )
     188             :         {
     189             :             case TitleHelper::SECONDARY_X_AXIS_TITLE:
     190           0 :                 xAxis = AxisHelper::createAxis( 0, false, xDiagram, xContext );
     191           0 :                 break;
     192             :             case TitleHelper::SECONDARY_Y_AXIS_TITLE:
     193           0 :                 xAxis = AxisHelper::createAxis( 1, false, xDiagram, xContext );
     194           0 :                break;
     195             :             default:
     196           2 :                break;
     197             :         }
     198           2 :         uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
     199           2 :         if( xProps.is() )
     200             :         {
     201           0 :             xProps->setPropertyValue( "Show", uno::makeAny( sal_False ) );
     202           0 :             xTitled = lcl_getTitleParent( eTitleType, xModel );
     203           4 :         }
     204             :     }
     205             : 
     206         160 :     if(xTitled.is())
     207             :     {
     208         158 :         uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
     209             : 
     210         316 :         xTitle.set( xContext->getServiceManager()->createInstanceWithContext(
     211             :                         "com.sun.star.chart2.Title",
     212         158 :                         xContext ), uno::UNO_QUERY );
     213             : 
     214         158 :         if(xTitle.is())
     215             :         {
     216             :             // default char height (main: 13.0 == default)
     217         158 :             float fDefaultCharHeightSub = 11.0;
     218         158 :             float fDefaultCharHeightAxis = 9.0;
     219         158 :             switch( eTitleType )
     220             :             {
     221             :                 case TitleHelper::SUB_TITLE:
     222             :                     TitleHelper::setCompleteString(
     223          26 :                         rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
     224          26 :                     break;
     225             :                 case TitleHelper::X_AXIS_TITLE:
     226             :                 case TitleHelper::Y_AXIS_TITLE:
     227             :                 case TitleHelper::Z_AXIS_TITLE:
     228             :                 case TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION:
     229             :                 case TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION:
     230             :                 case TitleHelper::SECONDARY_X_AXIS_TITLE:
     231             :                 case TitleHelper::SECONDARY_Y_AXIS_TITLE:
     232             :                     TitleHelper::setCompleteString(
     233          62 :                         rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
     234          62 :                     break;
     235             :                 default:
     236          70 :                     TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
     237          70 :                     break;
     238             :             }
     239             : 
     240             :             // set/clear autoscale
     241         158 :             if( pRefSizeProvider )
     242           0 :                 pRefSizeProvider->setValuesAtTitle( xTitle );
     243             : 
     244         158 :             xTitled->setTitleObject( xTitle );
     245             : 
     246             :             //default rotation 90 degree for y axis title in normal coordinatesystems or for x axis title for swapped coordinatesystems
     247         158 :             if( eTitleType == TitleHelper::X_AXIS_TITLE ||
     248          98 :                 eTitleType == TitleHelper::Y_AXIS_TITLE ||
     249          98 :                 eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE ||
     250             :                 eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE )
     251             : 
     252             :             {
     253             :                 try
     254             :                 {
     255          62 :                     bool bDummy = false;
     256          62 :                     bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
     257             : 
     258          62 :                     Reference< beans::XPropertySet > xTitleProps( xTitle, uno::UNO_QUERY );
     259          62 :                     if( xTitleProps.is() )
     260             :                     {
     261          62 :                         if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
     262          48 :                             || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
     263          32 :                             || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
     264          30 :                             || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
     265             :                         {
     266          32 :                             double fNewAngleDegree = 90.0;
     267          32 :                             xTitleProps->setPropertyValue( "TextRotation", uno::makeAny( fNewAngleDegree ));
     268             :                         }
     269          62 :                     }
     270             :                 }
     271           0 :                 catch( const uno::Exception & ex )
     272             :                 {
     273             :                     ASSERT_EXCEPTION( ex );
     274             :                 }
     275             :             }
     276         158 :         }
     277             :     }
     278         320 :     return xTitle;
     279             : 
     280             : }
     281             : 
     282        4709 : OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
     283             : {
     284        4709 :     OUString aRet;
     285        4709 :     if(!xTitle.is())
     286        3914 :         return aRet;
     287        1590 :     uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
     288        1666 :     for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
     289         871 :         aRet += aStringList[nN]->getString();
     290         795 :     return aRet;
     291             : }
     292             : 
     293         282 : void TitleHelper::setCompleteString( const OUString& rNewText
     294             :                     , const uno::Reference< XTitle >& xTitle
     295             :                     , const uno::Reference< uno::XComponentContext > & xContext
     296             :                     , float * pDefaultCharHeight /* = 0 */ )
     297             : {
     298             :     //the format of the first old text portion will be maintained if there is any
     299         282 :     if(!xTitle.is())
     300         282 :         return;
     301             : 
     302         282 :     OUString aNewText = rNewText;
     303             : 
     304         282 :     bool bStacked = false;
     305         564 :     uno::Reference< beans::XPropertySet > xTitleProperties( xTitle, uno::UNO_QUERY );
     306         282 :     if( xTitleProperties.is() )
     307         282 :         xTitleProperties->getPropertyValue( "StackCharacters" ) >>= bStacked;
     308             : 
     309         282 :     if( bStacked )
     310             :     {
     311             :         //#i99841# remove linebreaks that were added for vertical stacking
     312           0 :         OUStringBuffer aUnstackedStr;
     313           0 :         OUStringBuffer aSource(rNewText);
     314             : 
     315           0 :         bool bBreakIgnored = false;
     316           0 :         sal_Int32 nLen = rNewText.getLength();
     317           0 :         for( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
     318             :         {
     319           0 :             sal_Unicode aChar = aSource[nPos];
     320           0 :             if( aChar != '\n' )
     321             :             {
     322           0 :                 aUnstackedStr.append( aChar );
     323           0 :                 bBreakIgnored = false;
     324             :             }
     325           0 :             else if( aChar == '\n' && bBreakIgnored )
     326           0 :                 aUnstackedStr.append( aChar );
     327             :             else
     328           0 :                 bBreakIgnored = true;
     329             :         }
     330           0 :         aNewText = aUnstackedStr.makeStringAndClear();
     331             :     }
     332             : 
     333         564 :     uno::Sequence< uno::Reference< XFormattedString > > aNewStringList(1);
     334             : 
     335         564 :     uno::Sequence< uno::Reference< XFormattedString > >  aOldStringList = xTitle->getText();
     336         282 :     if( aOldStringList.getLength() )
     337             :     {
     338         124 :         aNewStringList[0].set( aOldStringList[0] );
     339         124 :         aNewStringList[0]->setString( aNewText );
     340             :     }
     341             :     else
     342             :     {
     343             :         uno::Reference< chart2::XFormattedString2 > xFormattedString =
     344         158 :             chart2::FormattedString::create( xContext );
     345             : 
     346         158 :         xFormattedString->setString( aNewText );
     347         158 :         aNewStringList[0].set( xFormattedString );
     348         158 :         if( pDefaultCharHeight != 0 )
     349             :         {
     350             :             try
     351             :             {
     352          88 :                 uno::Any aFontSize( uno::makeAny( *pDefaultCharHeight ));
     353          88 :                 xFormattedString->setPropertyValue( "CharHeight", aFontSize );
     354          88 :                 xFormattedString->setPropertyValue( "CharHeightAsian", aFontSize );
     355          88 :                 xFormattedString->setPropertyValue( "CharHeightComplex", aFontSize );
     356             :             }
     357           0 :             catch( const uno::Exception & ex )
     358             :             {
     359             :                 ASSERT_EXCEPTION( ex );
     360             :             }
     361         158 :         }
     362             :     }
     363         564 :     xTitle->setText( aNewStringList );
     364             : }
     365             : 
     366           6 : void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
     367             :                     , const ::com::sun::star::uno::Reference<
     368             :                             ::com::sun::star::frame::XModel >& xModel )
     369             : {
     370           6 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
     371           6 :     if( xTitled.is())
     372             :     {
     373           6 :         xTitled->setTitleObject(NULL);
     374           6 :     }
     375           6 : }
     376             : 
     377         314 : bool TitleHelper::getTitleType( eTitleType& rType
     378             :                     , const ::com::sun::star::uno::Reference<
     379             :                         ::com::sun::star::chart2::XTitle >& xTitle
     380             :                     , ChartModel& rModel )
     381             : {
     382         314 :     if( !xTitle.is() )
     383           0 :         return false;
     384             : 
     385         314 :     Reference< chart2::XTitle > xCurrentTitle;
     386         460 :     for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
     387             :     {
     388         460 :         xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), rModel );
     389         460 :         if( xCurrentTitle == xTitle )
     390             :         {
     391         314 :             rType = static_cast<eTitleType>(nTitleType);
     392         314 :             return true;
     393             :         }
     394             :     }
     395             : 
     396           0 :     return false;
     397             : }
     398             : 
     399        3406 : bool TitleHelper::getTitleType( eTitleType& rType
     400             :                     , const ::com::sun::star::uno::Reference<
     401             :                         ::com::sun::star::chart2::XTitle >& xTitle
     402             :                     , const ::com::sun::star::uno::Reference<
     403             :                         ::com::sun::star::frame::XModel >& xModel )
     404             : {
     405        3406 :     if( !xTitle.is() || !xModel.is() )
     406           0 :         return false;
     407             : 
     408        3406 :     Reference< chart2::XTitle > xCurrentTitle;
     409        5034 :     for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
     410             :     {
     411        5034 :         xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
     412        5034 :         if( xCurrentTitle == xTitle )
     413             :         {
     414        3406 :             rType = static_cast<eTitleType>(nTitleType);
     415        3406 :             return true;
     416             :         }
     417             :     }
     418             : 
     419           0 :     return false;
     420             : }
     421             : 
     422         108 : } //namespace chart
     423             : 
     424             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10