LCOV - code coverage report
Current view: top level - chart2/source/tools - TitleHelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 160 189 84.7 %
Date: 2015-06-13 12:38:46 Functions: 11 11 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       31253 : uno::Reference< XTitled > lcl_getTitleParentFromDiagram(
      39             :       TitleHelper::eTitleType nTitleIndex
      40             :     , const uno::Reference< XDiagram >& xDiagram )
      41             : {
      42       31253 :     uno::Reference< XTitled > xResult;
      43             : 
      44       31253 :     if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_X_AXIS_POSITION ||
      45             :         nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
      46             :     {
      47        2164 :         bool bDummy = false;
      48        2164 :         bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
      49             : 
      50        2164 :         if( nTitleIndex == TitleHelper::TITLE_AT_STANDARD_Y_AXIS_POSITION )
      51        1082 :             nTitleIndex = bIsVertical ? TitleHelper::X_AXIS_TITLE : TitleHelper::Y_AXIS_TITLE;
      52             :         else
      53        1082 :             nTitleIndex = bIsVertical ? TitleHelper::Y_AXIS_TITLE : TitleHelper::X_AXIS_TITLE;
      54             :     }
      55             : 
      56       31253 :     switch( nTitleIndex )
      57             :     {
      58             :         case TitleHelper::SUB_TITLE:
      59        4023 :             if( xDiagram.is())
      60        3939 :                 xResult.set( xDiagram, uno::UNO_QUERY );
      61        4023 :             break;
      62             :         case TitleHelper::X_AXIS_TITLE:
      63        9996 :             if( xDiagram.is())
      64        9912 :                 xResult.set( AxisHelper::getAxis( 0, true, xDiagram ), uno::UNO_QUERY );
      65        9996 :             break;
      66             :         case TitleHelper::Y_AXIS_TITLE:
      67       10042 :             if( xDiagram.is())
      68        9958 :                 xResult.set( AxisHelper::getAxis( 1, true, xDiagram ), uno::UNO_QUERY );
      69       10042 :             break;
      70             :         case TitleHelper::Z_AXIS_TITLE:
      71        1598 :             if( xDiagram.is())
      72        1514 :                 xResult.set( AxisHelper::getAxis( 2, true, xDiagram ), uno::UNO_QUERY );
      73        1598 :             break;
      74             :         case TitleHelper::SECONDARY_X_AXIS_TITLE:
      75        2512 :             if( xDiagram.is())
      76        2428 :                 xResult.set( AxisHelper::getAxis( 0, false, xDiagram ), uno::UNO_QUERY );
      77        2512 :             break;
      78             :         case TitleHelper::SECONDARY_Y_AXIS_TITLE:
      79        3082 :             if( xDiagram.is())
      80        2998 :                 xResult.set( AxisHelper::getAxis( 1, false, xDiagram ), uno::UNO_QUERY );
      81        3082 :             break;
      82             : 
      83             :         case TitleHelper::MAIN_TITLE:
      84             :         default:
      85             :             OSL_FAIL( "Unsupported Title-Type requested" );
      86           0 :             break;
      87             :     }
      88             : 
      89       31253 :     return xResult;
      90             : }
      91             : 
      92       31253 : uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
      93             :                                               , const uno::Reference< XDiagram >& xDiagram )
      94             : {
      95       31253 :     uno::Reference< XTitled > xResult;
      96       31253 :     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       31253 :             xResult.set( lcl_getTitleParentFromDiagram( nTitleIndex, xDiagram ));
     110       31253 :             break;
     111             :         default:
     112             :             OSL_FAIL( "Unsupported Title-Type requested" );
     113           0 :             break;
     114             :     }
     115             : 
     116       31253 :     return xResult;
     117             : }
     118             : 
     119          90 : uno::Reference< XTitled > lcl_getTitleParent( TitleHelper::eTitleType nTitleIndex
     120             :                                               , const uno::Reference< frame::XModel >& xModel )
     121             : {
     122          90 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     123             :     {
     124          41 :         uno::Reference< XTitled > xTitled( xModel, uno::UNO_QUERY );
     125          41 :         return xTitled;
     126             :     }
     127             : 
     128          49 :     uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
     129          98 :     uno::Reference< XDiagram > xDiagram;
     130             : 
     131          49 :     if( xChartDoc.is())
     132          49 :         xDiagram.set( xChartDoc->getFirstDiagram());
     133             : 
     134          98 :     return lcl_getTitleParent( nTitleIndex, xDiagram );
     135             : }
     136             : 
     137             : }
     138             : 
     139        7968 : uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
     140             :                             , ChartModel& rModel )
     141             : {
     142        7968 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     143        2081 :         return rModel.getTitleObject();
     144             : 
     145        5887 :     uno::Reference< XDiagram > xDiagram( rModel.getFirstDiagram(), uno::UNO_QUERY );
     146       11774 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xDiagram ) );
     147        5887 :     if( xTitled.is())
     148        3599 :         return xTitled->getTitleObject();
     149        8175 :     return NULL;
     150             : }
     151             : 
     152       49683 : uno::Reference< XTitle > TitleHelper::getTitle( TitleHelper::eTitleType nTitleIndex
     153             :                             , const uno::Reference< frame::XModel >& xModel )
     154             : {
     155       49683 :     uno::Reference< XTitled > xTitled;
     156       49683 :     if(nTitleIndex == TitleHelper::MAIN_TITLE)
     157             :     {
     158       24366 :         xTitled.set( xModel, uno::UNO_QUERY );
     159             :     }
     160             :     else
     161             :     {
     162       25317 :         uno::Reference< XChartDocument > xChartDoc( xModel, uno::UNO_QUERY );
     163       50634 :         uno::Reference< XDiagram > xDiagram;
     164       25317 :         if( xChartDoc.is())
     165       25317 :             xDiagram.set( xChartDoc->getFirstDiagram());
     166       50634 :         xTitled = lcl_getTitleParent( nTitleIndex, xDiagram );
     167             :     }
     168       49683 :     if( xTitled.is())
     169       45543 :         return xTitled->getTitleObject();
     170        4140 :     return NULL;
     171             : }
     172             : 
     173          87 : 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          87 :     uno::Reference< XTitle > xTitle;
     181         174 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( eTitleType, xModel ) );
     182             : 
     183          87 :     if( !xTitled.is() )
     184             :     {
     185           1 :         uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
     186           2 :         uno::Reference< chart2::XAxis > xAxis;
     187           1 :         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           1 :                break;
     197             :         }
     198           1 :         uno::Reference< beans::XPropertySet > xProps( xAxis, uno::UNO_QUERY );
     199           1 :         if( xProps.is() )
     200             :         {
     201           0 :             xProps->setPropertyValue( "Show", uno::makeAny( sal_False ) );
     202           0 :             xTitled = lcl_getTitleParent( eTitleType, xModel );
     203           2 :         }
     204             :     }
     205             : 
     206          87 :     if(xTitled.is())
     207             :     {
     208          86 :         uno::Reference< XDiagram > xDiagram( ChartModelHelper::findDiagram( xModel ) );
     209             : 
     210         172 :         xTitle.set( xContext->getServiceManager()->createInstanceWithContext(
     211             :                         "com.sun.star.chart2.Title",
     212          86 :                         xContext ), uno::UNO_QUERY );
     213             : 
     214          86 :         if(xTitle.is())
     215             :         {
     216             :             // default char height (main: 13.0 == default)
     217          86 :             float fDefaultCharHeightSub = 11.0;
     218          86 :             float fDefaultCharHeightAxis = 9.0;
     219          86 :             switch( eTitleType )
     220             :             {
     221             :                 case TitleHelper::SUB_TITLE:
     222             :                     TitleHelper::setCompleteString(
     223          31 :                         rTitleText, xTitle, xContext, & fDefaultCharHeightSub );
     224          31 :                     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          16 :                         rTitleText, xTitle, xContext, & fDefaultCharHeightAxis );
     234          16 :                     break;
     235             :                 default:
     236          39 :                     TitleHelper::setCompleteString( rTitleText, xTitle, xContext );
     237          39 :                     break;
     238             :             }
     239             : 
     240             :             // set/clear autoscale
     241          86 :             if( pRefSizeProvider )
     242           0 :                 pRefSizeProvider->setValuesAtTitle( xTitle );
     243             : 
     244          86 :             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          86 :             if( eTitleType == TitleHelper::X_AXIS_TITLE ||
     248          70 :                 eTitleType == TitleHelper::Y_AXIS_TITLE ||
     249          70 :                 eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE ||
     250             :                 eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE )
     251             : 
     252             :             {
     253             :                 try
     254             :                 {
     255          16 :                     bool bDummy = false;
     256          16 :                     bool bIsVertical = DiagramHelper::getVertical( xDiagram, bDummy, bDummy );
     257             : 
     258          16 :                     Reference< beans::XPropertySet > xTitleProps( xTitle, uno::UNO_QUERY );
     259          16 :                     if( xTitleProps.is() )
     260             :                     {
     261          16 :                         if( (!bIsVertical && eTitleType == TitleHelper::Y_AXIS_TITLE)
     262          11 :                             || (bIsVertical && eTitleType == TitleHelper::X_AXIS_TITLE)
     263           8 :                             || (!bIsVertical && eTitleType == TitleHelper::SECONDARY_Y_AXIS_TITLE)
     264           8 :                             || (bIsVertical && eTitleType == TitleHelper::SECONDARY_X_AXIS_TITLE) )
     265             :                         {
     266           8 :                             double fNewAngleDegree = 90.0;
     267           8 :                             xTitleProps->setPropertyValue( "TextRotation", uno::makeAny( fNewAngleDegree ));
     268             :                         }
     269          16 :                     }
     270             :                 }
     271           0 :                 catch( const uno::Exception & ex )
     272             :                 {
     273             :                     ASSERT_EXCEPTION( ex );
     274             :                 }
     275             :             }
     276          86 :         }
     277             :     }
     278         174 :     return xTitle;
     279             : 
     280             : }
     281             : 
     282        6847 : OUString TitleHelper::getCompleteString( const uno::Reference< XTitle >& xTitle )
     283             : {
     284        6847 :     OUString aRet;
     285        6847 :     if(!xTitle.is())
     286        5666 :         return aRet;
     287        2362 :     uno::Sequence< uno::Reference< XFormattedString > > aStringList = xTitle->getText();
     288        2407 :     for( sal_Int32 nN=0; nN<aStringList.getLength();nN++ )
     289        1226 :         aRet += aStringList[nN]->getString();
     290        1181 :     return aRet;
     291             : }
     292             : 
     293         137 : 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         137 :     if(!xTitle.is())
     300         137 :         return;
     301             : 
     302         137 :     OUString aNewText = rNewText;
     303             : 
     304         137 :     bool bStacked = false;
     305         274 :     uno::Reference< beans::XPropertySet > xTitleProperties( xTitle, uno::UNO_QUERY );
     306         137 :     if( xTitleProperties.is() )
     307         137 :         xTitleProperties->getPropertyValue( "StackCharacters" ) >>= bStacked;
     308             : 
     309         137 :     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         274 :     uno::Sequence< uno::Reference< XFormattedString > > aNewStringList(1);
     334             : 
     335         274 :     uno::Sequence< uno::Reference< XFormattedString > >  aOldStringList = xTitle->getText();
     336         137 :     if( aOldStringList.getLength() )
     337             :     {
     338          51 :         aNewStringList[0].set( aOldStringList[0] );
     339          51 :         aNewStringList[0]->setString( aNewText );
     340             :     }
     341             :     else
     342             :     {
     343             :         uno::Reference< chart2::XFormattedString2 > xFormattedString =
     344          86 :             chart2::FormattedString::create( xContext );
     345             : 
     346          86 :         xFormattedString->setString( aNewText );
     347          86 :         aNewStringList[0].set( xFormattedString );
     348          86 :         if( pDefaultCharHeight != 0 )
     349             :         {
     350             :             try
     351             :             {
     352          47 :                 uno::Any aFontSize( uno::makeAny( *pDefaultCharHeight ));
     353          47 :                 xFormattedString->setPropertyValue( "CharHeight", aFontSize );
     354          47 :                 xFormattedString->setPropertyValue( "CharHeightAsian", aFontSize );
     355          47 :                 xFormattedString->setPropertyValue( "CharHeightComplex", aFontSize );
     356             :             }
     357           0 :             catch( const uno::Exception & ex )
     358             :             {
     359             :                 ASSERT_EXCEPTION( ex );
     360             :             }
     361          86 :         }
     362             :     }
     363         274 :     xTitle->setText( aNewStringList );
     364             : }
     365             : 
     366           3 : void TitleHelper::removeTitle( TitleHelper::eTitleType nTitleIndex
     367             :                     , const ::com::sun::star::uno::Reference<
     368             :                             ::com::sun::star::frame::XModel >& xModel )
     369             : {
     370           3 :     uno::Reference< XTitled > xTitled( lcl_getTitleParent( nTitleIndex, xModel ) );
     371           3 :     if( xTitled.is())
     372             :     {
     373           3 :         xTitled->setTitleObject(NULL);
     374           3 :     }
     375           3 : }
     376             : 
     377         905 : bool TitleHelper::getTitleType( eTitleType& rType
     378             :                     , const ::com::sun::star::uno::Reference<
     379             :                         ::com::sun::star::chart2::XTitle >& xTitle
     380             :                     , ChartModel& rModel )
     381             : {
     382         905 :     if( !xTitle.is() )
     383           0 :         return false;
     384             : 
     385         905 :     Reference< chart2::XTitle > xCurrentTitle;
     386        1330 :     for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
     387             :     {
     388        1330 :         xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), rModel );
     389        1330 :         if( xCurrentTitle == xTitle )
     390             :         {
     391         905 :             rType = static_cast<eTitleType>(nTitleType);
     392         905 :             return true;
     393             :         }
     394             :     }
     395             : 
     396           0 :     return false;
     397             : }
     398             : 
     399        1750 : 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        1750 :     if( !xTitle.is() || !xModel.is() )
     406           0 :         return false;
     407             : 
     408        1750 :     Reference< chart2::XTitle > xCurrentTitle;
     409        2555 :     for( sal_Int32 nTitleType = TITLE_BEGIN; nTitleType < NORMAL_TITLE_END; nTitleType++ )
     410             :     {
     411        2555 :         xCurrentTitle = TitleHelper::getTitle( static_cast<eTitleType>(nTitleType), xModel );
     412        2555 :         if( xCurrentTitle == xTitle )
     413             :         {
     414        1750 :             rType = static_cast<eTitleType>(nTitleType);
     415        1750 :             return true;
     416             :         }
     417             :     }
     418             : 
     419           0 :     return false;
     420             : }
     421             : 
     422             : } //namespace chart
     423             : 
     424             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11