LCOV - code coverage report
Current view: top level - chart2/source/tools - PropertyHelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 18 102 17.6 %
Date: 2015-06-13 12:38:46 Functions: 5 21 23.8 %
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 "PropertyHelper.hxx"
      21             : #include "ContainerHelper.hxx"
      22             : #include "macros.hxx"
      23             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      24             : #include <com/sun/star/container/XNameContainer.hpp>
      25             : #include <osl/diagnose.h>
      26             : 
      27             : #include <vector>
      28             : #include <algorithm>
      29             : #include <iterator>
      30             : #include <functional>
      31             : 
      32             : using namespace ::com::sun::star;
      33             : using namespace ::com::sun::star::beans;
      34             : using ::com::sun::star::uno::Any;
      35             : using ::com::sun::star::uno::Reference;
      36             : using ::com::sun::star::uno::Sequence;
      37             : 
      38             : namespace
      39             : {
      40           0 : struct lcl_EqualsElement : public ::std::unary_function< OUString, bool >
      41             : {
      42           0 :     explicit lcl_EqualsElement( const Any & rValue, const Reference< container::XNameAccess > & xAccess )
      43           0 :             : m_aValue( rValue ), m_xAccess( xAccess )
      44             :     {
      45             :         OSL_ASSERT( m_xAccess.is());
      46           0 :     }
      47             : 
      48           0 :     bool operator() ( const OUString & rName )
      49             :     {
      50             :         try
      51             :         {
      52           0 :             return (m_xAccess->getByName( rName ) == m_aValue);
      53             :         }
      54           0 :         catch( const uno::Exception & ex )
      55             :         {
      56             :             ASSERT_EXCEPTION( ex );
      57             :         }
      58           0 :         return false;
      59             :     }
      60             : 
      61             : private:
      62             :     Any m_aValue;
      63             :     Reference< container::XNameAccess > m_xAccess;
      64             : };
      65             : 
      66           0 : struct lcl_StringMatches : public ::std::unary_function< OUString ,bool >
      67             : {
      68           0 :     explicit lcl_StringMatches( const OUString & rCmpStr ) :
      69           0 :             m_aCmpStr( rCmpStr )
      70           0 :     {}
      71             : 
      72           0 :     bool operator() ( const OUString & rStr )
      73             :     {
      74           0 :         return rStr.match( m_aCmpStr );
      75             :     }
      76             : 
      77             : private:
      78             :     OUString m_aCmpStr;
      79             : };
      80             : 
      81             : struct lcl_OUStringRestToInt32 : public ::std::unary_function< OUString, sal_Int32 >
      82             : {
      83           0 :     explicit lcl_OUStringRestToInt32( sal_Int32 nPrefixLength ) :
      84           0 :             m_nPrefixLength( nPrefixLength )
      85           0 :     {}
      86           0 :     sal_Int32 operator() ( const OUString & rStr )
      87             :     {
      88           0 :         if( m_nPrefixLength > rStr.getLength() )
      89           0 :             return 0;
      90           0 :         return rStr.copy( m_nPrefixLength ).toInt32();
      91             :     }
      92             : private:
      93             :     sal_Int32 m_nPrefixLength;
      94             : };
      95             : 
      96             : /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
      97             :     line dash or line marker to the corresponding name container with a unique
      98             :     name.
      99             : 
     100             :     @param rPrefix
     101             :         The prefix used for automated name generation.
     102             : 
     103             :     @param rPreferredName
     104             :         If this string is not empty it is used as name if it is unique in the
     105             :         table. Otherwise a new name is generated using pPrefix.
     106             : 
     107             :     @return the new name under which the property was stored in the table
     108             : */
     109           0 : OUString lcl_addNamedPropertyUniqueNameToTable(
     110             :     const Any & rValue,
     111             :     const Reference< container::XNameContainer > & xNameContainer,
     112             :     const OUString & rPrefix,
     113             :     const OUString & rPreferredName )
     114             : {
     115           0 :     if( ! xNameContainer.is() ||
     116           0 :         ! rValue.hasValue() ||
     117           0 :         ( rValue.getValueType() != xNameContainer->getElementType()))
     118           0 :         return rPreferredName;
     119             : 
     120             :     try
     121             :     {
     122           0 :         Reference< container::XNameAccess > xNameAccess( xNameContainer, uno::UNO_QUERY_THROW );
     123           0 :         ::std::vector< OUString > aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess->getElementNames()));
     124             :         ::std::vector< OUString >::const_iterator aIt(
     125           0 :             ::std::find_if( aNames.begin(), aNames.end(), lcl_EqualsElement( rValue, xNameAccess )));
     126             : 
     127             :         // element not found in container
     128           0 :         if( aIt == aNames.end())
     129             :         {
     130           0 :             OUString aUniqueName;
     131             : 
     132             :             // check if preferred name is already used
     133           0 :             if( !rPreferredName.isEmpty())
     134             :             {
     135           0 :                 aIt = ::std::find( aNames.begin(), aNames.end(), rPreferredName );
     136           0 :                 if( aIt == aNames.end())
     137           0 :                     aUniqueName = rPreferredName;
     138             :             }
     139             : 
     140           0 :             if( aUniqueName.isEmpty())
     141             :             {
     142             :                 // create a unique id using the prefix plus a number
     143           0 :                 ::std::vector< sal_Int32 > aNumbers;
     144             :                 ::std::vector< OUString >::iterator aNonConstIt(
     145           0 :                     ::std::partition( aNames.begin(), aNames.end(), lcl_StringMatches( rPrefix )));
     146             :                 ::std::transform( aNames.begin(), aNonConstIt,
     147             :                                   back_inserter( aNumbers ),
     148           0 :                                   lcl_OUStringRestToInt32( rPrefix.getLength() ));
     149             :                 ::std::vector< sal_Int32 >::const_iterator aMaxIt(
     150           0 :                     ::std::max_element( aNumbers.begin(), aNumbers.end()));
     151             : 
     152           0 :                 sal_Int32 nIndex = 1;
     153           0 :                 if( aMaxIt != aNumbers.end())
     154           0 :                     nIndex = (*aMaxIt) + 1;
     155             : 
     156           0 :                 aUniqueName = rPrefix + OUString::number( nIndex );
     157             :             }
     158             : 
     159             :             OSL_ASSERT( !aUniqueName.isEmpty());
     160           0 :             xNameContainer->insertByName( aUniqueName, rValue );
     161           0 :             return aUniqueName;
     162             :         }
     163             :         else
     164             :             // element found => return name
     165           0 :             return *aIt;
     166             :     }
     167           0 :     catch( const uno::Exception & ex )
     168             :     {
     169             :         ASSERT_EXCEPTION( ex );
     170             :     }
     171             : 
     172           0 :     return rPreferredName;
     173             : }
     174             : 
     175             : } // anonymous namespace
     176             : 
     177             : namespace chart
     178             : {
     179             : namespace PropertyHelper
     180             : {
     181             : 
     182           0 : OUString addLineDashUniqueNameToTable(
     183             :     const Any & rValue,
     184             :     const Reference< lang::XMultiServiceFactory > & xFact,
     185             :     const OUString & rPreferredName )
     186             : {
     187           0 :     if( xFact.is())
     188             :     {
     189             :         Reference< container::XNameContainer > xNameCnt(
     190           0 :             xFact->createInstance( "com.sun.star.drawing.DashTable"),
     191           0 :             uno::UNO_QUERY );
     192           0 :         if( xNameCnt.is())
     193             :             return lcl_addNamedPropertyUniqueNameToTable(
     194           0 :                 rValue, xNameCnt, "ChartDash ", rPreferredName );
     195             :     }
     196           0 :     return OUString();
     197             : }
     198             : 
     199           0 : OUString addGradientUniqueNameToTable(
     200             :     const Any & rValue,
     201             :     const Reference< lang::XMultiServiceFactory > & xFact,
     202             :     const OUString & rPreferredName )
     203             : {
     204           0 :     if( xFact.is())
     205             :     {
     206             :         Reference< container::XNameContainer > xNameCnt(
     207           0 :             xFact->createInstance( "com.sun.star.drawing.GradientTable"),
     208           0 :             uno::UNO_QUERY );
     209           0 :         if( xNameCnt.is())
     210             :             return lcl_addNamedPropertyUniqueNameToTable(
     211           0 :                 rValue, xNameCnt, "ChartGradient ", rPreferredName );
     212             :     }
     213           0 :     return OUString();
     214             : }
     215             : 
     216           0 : OUString addTransparencyGradientUniqueNameToTable(
     217             :     const Any & rValue,
     218             :     const Reference< lang::XMultiServiceFactory > & xFact,
     219             :     const OUString & rPreferredName )
     220             : {
     221           0 :     if( xFact.is())
     222             :     {
     223             :         Reference< container::XNameContainer > xNameCnt(
     224           0 :             xFact->createInstance( "com.sun.star.drawing.TransparencyGradientTable"),
     225           0 :             uno::UNO_QUERY );
     226           0 :         if( xNameCnt.is())
     227             :             return lcl_addNamedPropertyUniqueNameToTable(
     228           0 :                 rValue, xNameCnt, "ChartTransparencyGradient ", rPreferredName );
     229             :     }
     230           0 :     return OUString();
     231             : }
     232             : 
     233           0 : OUString addHatchUniqueNameToTable(
     234             :     const Any & rValue,
     235             :     const Reference< lang::XMultiServiceFactory > & xFact,
     236             :     const OUString & rPreferredName )
     237             : {
     238           0 :     if( xFact.is())
     239             :     {
     240             :         Reference< container::XNameContainer > xNameCnt(
     241           0 :             xFact->createInstance( "com.sun.star.drawing.HatchTable"),
     242           0 :             uno::UNO_QUERY );
     243           0 :         if( xNameCnt.is())
     244             :             return lcl_addNamedPropertyUniqueNameToTable(
     245           0 :                 rValue, xNameCnt, "ChartHatch ", rPreferredName );
     246             :     }
     247           0 :     return OUString();
     248             : }
     249             : 
     250           0 : OUString addBitmapUniqueNameToTable(
     251             :     const Any & rValue,
     252             :     const Reference< lang::XMultiServiceFactory > & xFact,
     253             :     const OUString & rPreferredName )
     254             : {
     255           0 :     if( xFact.is())
     256             :     {
     257             :         Reference< container::XNameContainer > xNameCnt(
     258           0 :             xFact->createInstance( "com.sun.star.drawing.BitmapTable"),
     259           0 :             uno::UNO_QUERY );
     260           0 :         if( xNameCnt.is())
     261             :             return lcl_addNamedPropertyUniqueNameToTable(
     262           0 :                 rValue, xNameCnt, "ChartBitmap ", rPreferredName );
     263             :     }
     264           0 :     return OUString();
     265             : }
     266             : 
     267        6124 : void setPropertyValueAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
     268             : {
     269        6124 :     tPropertyValueMap::iterator aIt( rOutMap.find( key ));
     270        6124 :     if( aIt == rOutMap.end())
     271        5891 :         rOutMap.insert( tPropertyValueMap::value_type( key, rAny ));
     272             :     else
     273         233 :         (*aIt).second = rAny;
     274        6124 : }
     275             : 
     276             : template<>
     277        5891 :     void setPropertyValue< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
     278             : {
     279        5891 :     setPropertyValueAny( rOutMap, key, rAny );
     280        5891 : }
     281             : 
     282        5891 : void setPropertyValueDefaultAny( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const uno::Any & rAny )
     283             : {
     284             :     OSL_ENSURE( rOutMap.end() == rOutMap.find( key ), "Default already exists for property" );
     285        5891 :     setPropertyValue( rOutMap, key, rAny );
     286        5891 : }
     287             : 
     288             : template<>
     289         383 :     void setPropertyValueDefault< ::com::sun::star::uno::Any >( tPropertyValueMap & rOutMap, tPropertyValueMapKey key, const ::com::sun::star::uno::Any & rAny )
     290             : {
     291         383 :     setPropertyValueDefaultAny( rOutMap, key, rAny );
     292         383 : }
     293             : 
     294         128 : void setEmptyPropertyValueDefault( tPropertyValueMap & rOutMap, tPropertyValueMapKey key )
     295             : {
     296         128 :     setPropertyValueDefault( rOutMap, key, uno::Any());
     297         128 : }
     298             : 
     299             : } // namespace PropertyHelper
     300             : 
     301             : } //  namespace chart
     302             : 
     303             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11