LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbaformatconditions.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 6 120 5.0 %
Date: 2012-08-25 Functions: 2 20 10.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 276 0.7 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include <ooo/vba/excel/XRange.hpp>
      30                 :            : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      31                 :            : #include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
      32                 :            : #include <vector>
      33                 :            : #include "vbaformatconditions.hxx"
      34                 :            : #include "vbaformatcondition.hxx"
      35                 :            : #include "vbaworkbook.hxx"
      36                 :            : #include "vbastyles.hxx"
      37                 :            : #include "vbaglobals.hxx"
      38                 :            : using namespace ::ooo::vba;
      39                 :            : using namespace ::com::sun::star;
      40                 :            : 
      41                 :            : typedef std::vector< beans::PropertyValue > VecPropValues;
      42                 :            : 
      43                 :          3 : static rtl::OUString OPERATOR( RTL_CONSTASCII_USTRINGPARAM("Operator") );
      44                 :          3 : static rtl::OUString FORMULA1( RTL_CONSTASCII_USTRINGPARAM("Formula1") );
      45                 :          3 : static rtl::OUString FORMULA2( RTL_CONSTASCII_USTRINGPARAM("Formula2") );
      46                 :          3 : static rtl::OUString STYLENAME( RTL_CONSTASCII_USTRINGPARAM("StyleName") );
      47                 :          3 : static rtl::OUString sStyleNamePrefix( RTL_CONSTASCII_USTRINGPARAM("Excel_CondFormat") );
      48                 :            : 
      49                 :            : void SAL_CALL
      50                 :          0 : ScVbaFormatConditions::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
      51                 :            : {
      52                 :            :     try
      53                 :            :     {
      54 [ #  # ][ #  # ]:          0 :         ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
      55         [ #  # ]:          0 :         if ( !pStyles )
      56         [ #  # ]:          0 :             DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
      57 [ #  # ][ #  # ]:          0 :         sal_Int32 nCount = mxSheetConditionalEntries->getCount();
      58         [ #  # ]:          0 :         for (sal_Int32 i = nCount - 1; i >= 0; i--)
      59                 :            :         {
      60 [ #  # ][ #  # ]:          0 :             uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
                 [ #  # ]
      61 [ #  # ][ #  # ]:          0 :             pStyles->Delete(xSheetConditionalEntry->getStyleName());
                 [ #  # ]
      62 [ #  # ][ #  # ]:          0 :             mxSheetConditionalEntries->removeByIndex(i);
      63                 :          0 :         }
      64         [ #  # ]:          0 :         notifyRange();
      65                 :            :     }
      66         [ #  # ]:          0 :     catch (uno::Exception& )
      67                 :            :     {
      68         [ #  # ]:          0 :         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
      69                 :            :     }
      70                 :          0 : }
      71                 :            : 
      72                 :            : uno::Type SAL_CALL
      73                 :          0 : ScVbaFormatConditions::getElementType() throw (css::uno::RuntimeException)
      74                 :            : {
      75                 :          0 :     return excel::XFormatCondition::static_type(0);
      76                 :            : }
      77                 :            : 
      78                 :            : 
      79                 :          0 : uno::Any xSheetConditionToFormatCondition( const uno::Reference< XHelperInterface >& xRangeParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xFormatConditions, const uno::Reference< beans::XPropertySet >& xRangeProps,  const uno::Any& aObject )
      80                 :            : {
      81                 :          0 :     uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry;
      82         [ #  # ]:          0 :     aObject >>= xSheetConditionalEntry;
      83                 :            : 
      84 [ #  # ][ #  # ]:          0 :     uno::Reference< excel::XStyle > xStyle( xStyles->Item( uno::makeAny( xSheetConditionalEntry->getStyleName() ), uno::Any() ), uno::UNO_QUERY_THROW );
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      85 [ #  # ][ #  # ]:          0 :     uno::Reference< excel::XFormatCondition > xCondition = new ScVbaFormatCondition( xRangeParent, xContext,  xSheetConditionalEntry, xStyle, xFormatConditions, xRangeProps );
                 [ #  # ]
      86         [ #  # ]:          0 :     return uno::makeAny( xCondition );
      87                 :            : }
      88                 :            : 
      89                 :            : uno::Any
      90                 :          0 : ScVbaFormatConditions::createCollectionObject(const uno::Any& aObject )
      91                 :            : {
      92 [ #  # ][ #  # ]:          0 :     return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
      93                 :            : }
      94                 :            : 
      95         [ #  # ]:          0 : class EnumWrapper : public EnumerationHelper_BASE
      96                 :            : {
      97                 :            :         uno::Reference<container::XIndexAccess > m_xIndexAccess;
      98                 :            :         uno::Reference<excel::XRange > m_xParentRange;
      99                 :            :         uno::Reference<uno::XComponentContext > m_xContext;
     100                 :            :         uno::Reference<excel::XStyles > m_xStyles;
     101                 :            :         uno::Reference<excel::XFormatConditions > m_xParentCollection;
     102                 :            :         uno::Reference<beans::XPropertySet > m_xProps;
     103                 :            : 
     104                 :            :         sal_Int32 nIndex;
     105                 :            : public:
     106                 :          0 :         EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<excel::XRange >& xRange, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xCollection, const uno::Reference<beans::XPropertySet >& xProps  ) : m_xIndexAccess( xIndexAccess ), m_xParentRange( xRange ), m_xContext( xContext ), m_xStyles( xStyles ), m_xParentCollection( xCollection ), m_xProps( xProps ), nIndex( 0 ) {}
     107                 :          0 :         virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
     108                 :            :         {
     109                 :          0 :                 return ( nIndex < m_xIndexAccess->getCount() );
     110                 :            :         }
     111                 :            : 
     112                 :          0 :         virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     113                 :            :         {
     114         [ #  # ]:          0 :                 if ( nIndex < m_xIndexAccess->getCount() )
     115 [ #  # ][ #  # ]:          0 :                         return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( m_xParentRange, uno::UNO_QUERY_THROW ), m_xContext, m_xStyles, m_xParentCollection, m_xProps, m_xIndexAccess->getByIndex( nIndex++ ) );
     116         [ #  # ]:          0 :                 throw container::NoSuchElementException();
     117                 :            :         }
     118                 :            : };
     119                 :            : 
     120                 :            : uno::Reference< excel::XFormatCondition > SAL_CALL
     121                 :          0 : ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 ) throw (script::BasicErrorException, uno::RuntimeException)
     122                 :            : {
     123         [ #  # ]:          0 :     return Add( _nType, _aOperator, _aFormula1, _aFormula2, uno::Reference< excel::XStyle >() );
     124                 :            : }
     125                 :            : 
     126                 :            : uno::Reference< excel::XFormatCondition >
     127                 :          0 : ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2, const css::uno::Reference< excel::XStyle >& _xStyle  ) throw (script::BasicErrorException, uno::RuntimeException)
     128                 :            : {
     129                 :            :     // #TODO
     130                 :            :     // #FIXME
     131                 :            :     // This method will NOT handle r1c1 formulas [*]and only assumes that
     132                 :            :     // the formulas are _xlA1 based ( need to hook into calc work ths should
     133                 :            :     // address this )
     134                 :            :     // [*] reason: getA1Formula method below is just a hook and just
     135                 :            :     // returns whats it gets ( e.g. doesn't convert anything )
     136                 :          0 :     uno::Reference< excel::XStyle > xStyle( _xStyle );
     137                 :          0 :     uno::Reference< excel::XFormatCondition > xFormatCondition;
     138                 :            :     try
     139                 :            :     {
     140                 :          0 :         rtl::OUString sStyleName;
     141         [ #  # ]:          0 :         if ( !xStyle.is() )
     142                 :            :         {
     143         [ #  # ]:          0 :             sStyleName = getStyleName();
     144 [ #  # ][ #  # ]:          0 :             xStyle = mxStyles->Add(sStyleName, uno::Any() );
                 [ #  # ]
     145                 :            :         }
     146                 :            :         else
     147                 :            :         {
     148 [ #  # ][ #  # ]:          0 :             sStyleName = xStyle->getName();
     149                 :            :         }
     150                 :            : 
     151         [ #  # ]:          0 :         VecPropValues aPropertyValueVector;
     152         [ #  # ]:          0 :         sheet::ConditionOperator aType = ScVbaFormatCondition::retrieveAPIType(_nType, uno::Reference< sheet::XSheetCondition >() );
     153                 :          0 :         uno::Any aValue;
     154                 :            : 
     155         [ #  # ]:          0 :         if ( aType == sheet::ConditionOperator_FORMULA)
     156         [ #  # ]:          0 :             aValue = uno::makeAny( sheet::ConditionOperator_FORMULA );
     157                 :            :         else
     158 [ #  # ][ #  # ]:          0 :             aValue = uno::makeAny( ScVbaFormatCondition::retrieveAPIOperator(_aOperator) );
     159                 :            : 
     160                 :          0 :         beans::PropertyValue aProperty( OPERATOR, 0, aValue, beans::PropertyState_DIRECT_VALUE );
     161         [ #  # ]:          0 :         aPropertyValueVector.push_back( aProperty );
     162                 :            : 
     163         [ #  # ]:          0 :         if ( _aFormula1.hasValue() )
     164                 :            :         {
     165 [ #  # ][ #  # ]:          0 :             beans::PropertyValue aProp( FORMULA1, 0, uno::makeAny( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
     166         [ #  # ]:          0 :             aPropertyValueVector.push_back( aProp );
     167                 :            :         }
     168         [ #  # ]:          0 :         if ( _aFormula2.hasValue() )
     169                 :            :         {
     170 [ #  # ][ #  # ]:          0 :             beans::PropertyValue aProp( FORMULA2, 0, uno::makeAny( getA1Formula( _aFormula2 ) ), beans::PropertyState_DIRECT_VALUE );
     171         [ #  # ]:          0 :             aPropertyValueVector.push_back( aProp );
     172                 :            :         }
     173                 :          0 :         aProperty.Name = STYLENAME;
     174         [ #  # ]:          0 :         aProperty.Value = uno::makeAny( sStyleName );
     175                 :            : 
     176                 :            :         // convert vector to sequence
     177         [ #  # ]:          0 :         uno::Sequence< beans::PropertyValue > aPropertyValueList(aPropertyValueVector.size());
     178                 :          0 :         VecPropValues::iterator it = aPropertyValueVector.begin();
     179                 :          0 :         VecPropValues::iterator it_end = aPropertyValueVector.end();
     180 [ #  # ][ #  # ]:          0 :         for ( sal_Int32 index=0; it != it_end; ++it )
     181         [ #  # ]:          0 :             aPropertyValueList[ index++ ] = *it;
     182                 :            : 
     183 [ #  # ][ #  # ]:          0 :         mxSheetConditionalEntries->addNew(aPropertyValueList);
     184 [ #  # ][ #  # ]:          0 :         for (sal_Int32 i = mxSheetConditionalEntries->getCount()-1; i >= 0; i--)
                 [ #  # ]
     185                 :            :         {
     186 [ #  # ][ #  # ]:          0 :             uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
                 [ #  # ]
     187 [ #  # ][ #  # ]:          0 :             if (xSheetConditionalEntry->getStyleName().equals(sStyleName))
                 [ #  # ]
     188                 :            :             {
     189 [ #  # ][ #  # ]:          0 :                 xFormatCondition =  new ScVbaFormatCondition(uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, xSheetConditionalEntry, xStyle, this, mxParentRangePropertySet);
         [ #  # ][ #  # ]
                 [ #  # ]
     190         [ #  # ]:          0 :                 notifyRange();
     191                 :            :                 return xFormatCondition;
     192                 :            :             }
     193 [ #  # ][ #  # ]:          0 :         }
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     194                 :            :     }
     195         [ #  # ]:          0 :     catch (uno::Exception& )
     196                 :            :     {
     197                 :            :     }
     198         [ #  # ]:          0 :     DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
     199                 :          0 :     return xFormatCondition;
     200                 :            : }
     201                 :            : 
     202                 :            : 
     203                 :            : uno::Reference< container::XEnumeration > SAL_CALL
     204                 :          0 : ScVbaFormatConditions::createEnumeration() throw (uno::RuntimeException)
     205                 :            : {
     206 [ #  # ][ #  # ]:          0 :     return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet  );
                 [ #  # ]
     207                 :            : }
     208                 :            : 
     209                 :            : 
     210                 :            : void
     211                 :          0 : ScVbaFormatConditions::notifyRange() throw ( script::BasicErrorException )
     212                 :            : {
     213                 :            :     try
     214                 :            :     {
     215 [ #  # ][ #  # ]:          0 :         mxParentRangePropertySet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ConditionalFormat")), uno::makeAny( mxSheetConditionalEntries ));
         [ #  # ][ #  # ]
     216                 :            :     }
     217         [ #  # ]:          0 :     catch (uno::Exception& )
     218                 :            :     {
     219         [ #  # ]:          0 :         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
     220                 :            :     }
     221                 :          0 : }
     222                 :            : 
     223                 :            : rtl::OUString
     224                 :          0 : ScVbaFormatConditions::getA1Formula(const css::uno::Any& _aFormula) throw ( script::BasicErrorException )
     225                 :            : {
     226                 :            :     // #TODO, #FIXME hook-in proper formula conversion detection & logic
     227                 :          0 :     rtl::OUString sFormula;
     228         [ #  # ]:          0 :     if ( !( _aFormula >>= sFormula ) )
     229         [ #  # ]:          0 :         DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
     230                 :          0 :     return sFormula;
     231                 :            : }
     232                 :            : 
     233                 :            : rtl::OUString
     234                 :          0 : ScVbaFormatConditions::getStyleName()
     235                 :            : {
     236 [ #  # ][ #  # ]:          0 :     ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
     237         [ #  # ]:          0 :     if ( !pStyles )
     238         [ #  # ]:          0 :         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
     239         [ #  # ]:          0 :     uno::Sequence< rtl::OUString > sCellStyleNames = pStyles->getStyleNames();
     240 [ #  # ][ #  # ]:          0 :     return ContainerUtilities::getUniqueName(sCellStyleNames, sStyleNamePrefix, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("_") ));
                 [ #  # ]
     241                 :            : }
     242                 :            : 
     243                 :            : void
     244                 :          0 : ScVbaFormatConditions::removeFormatCondition( const rtl::OUString& _sStyleName, sal_Bool _bRemoveStyle) throw ( script::BasicErrorException )
     245                 :            : {
     246                 :            :     try
     247                 :            :     {
     248 [ #  # ][ #  # ]:          0 :         sal_Int32 nElems = mxSheetConditionalEntries->getCount();
     249         [ #  # ]:          0 :         for (sal_Int32 i = 0; i < nElems; i++)
     250                 :            :         {
     251 [ #  # ][ #  # ]:          0 :             uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
                 [ #  # ]
     252 [ #  # ][ #  # ]:          0 :             if (_sStyleName.equals(xSheetConditionalEntry->getStyleName()))
                 [ #  # ]
     253                 :            :             {
     254 [ #  # ][ #  # ]:          0 :                 mxSheetConditionalEntries->removeByIndex(i);
     255         [ #  # ]:          0 :                 if (_bRemoveStyle)
     256                 :            :                 {
     257 [ #  # ][ #  # ]:          0 :                     ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
     258         [ #  # ]:          0 :                     if ( !pStyles )
     259         [ #  # ]:          0 :                         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
     260         [ #  # ]:          0 :                     pStyles->Delete( _sStyleName );
     261                 :            :                 }
     262                 :          0 :                 return;
     263                 :            :             }
     264         [ #  # ]:          0 :         }
     265                 :            :     }
     266         [ #  # ]:          0 :     catch (uno::Exception& )
     267                 :            :     {
     268         [ #  # ]:          0 :         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
     269                 :            :     }
     270                 :            : }
     271                 :            : 
     272                 :            : rtl::OUString
     273                 :          0 : ScVbaFormatConditions::getServiceImplName()
     274                 :            : {
     275                 :          0 :     return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaFormatConditions"));
     276                 :            : }
     277                 :            : 
     278                 :            : uno::Sequence< rtl::OUString >
     279                 :          0 : ScVbaFormatConditions::getServiceNames()
     280                 :            : {
     281 [ #  # ][ #  # ]:          0 :     static uno::Sequence< rtl::OUString > aServiceNames;
         [ #  # ][ #  # ]
     282         [ #  # ]:          0 :     if ( aServiceNames.getLength() == 0 )
     283                 :            :     {
     284                 :          0 :         aServiceNames.realloc( 1 );
     285         [ #  # ]:          0 :         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.FormatConditions" ) );
     286                 :            :     }
     287                 :          0 :     return aServiceNames;
     288 [ +  - ][ +  - ]:          9 : }
     289                 :            : 
     290                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10