LCOV - code coverage report
Current view: top level - svl/source/items - aeitem.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 47 92 51.1 %
Date: 2014-04-11 Functions: 18 28 64.3 %
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 <rtl/ustring.hxx>
      21             : #include <svl/aeitem.hxx>
      22             : #include <vector>
      23             : 
      24       13758 : TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
      25             : 
      26             : 
      27             : 
      28        5266 : struct SfxAllEnumValue_Impl
      29             : {
      30             :     sal_uInt16 nValue;
      31             :     OUString aText;
      32             : };
      33             : 
      34        2635 : class SfxAllEnumValueArr : public std::vector<SfxAllEnumValue_Impl*>
      35             : {
      36             : public:
      37        2631 :     ~SfxAllEnumValueArr()
      38        2631 :     {
      39        5262 :         for( const_iterator it = begin(); it != end(); ++it )
      40        2631 :             delete *it;
      41        2631 :     }
      42             : };
      43             : 
      44             : 
      45             : 
      46         765 : SfxAllEnumItem::SfxAllEnumItem() :
      47             :     SfxEnumItem(),
      48             :     pValues( 0 ),
      49         765 :     pDisabledValues( 0 )
      50             : {
      51         765 : }
      52             : 
      53             : 
      54             : 
      55         811 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
      56             :     SfxEnumItem(which, nVal),
      57             :     pValues( 0 ),
      58         811 :     pDisabledValues( 0 )
      59             : {
      60         811 :     InsertValue( nVal );
      61         811 : }
      62             : 
      63             : 
      64             : 
      65           0 : SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
      66             :     SfxEnumItem(which, rStream),
      67             :     pValues( 0 ),
      68           0 :     pDisabledValues( 0 )
      69             : {
      70           0 :     InsertValue( GetValue() );
      71           0 : }
      72             : 
      73             : 
      74             : 
      75             : 
      76        1005 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
      77             :     SfxEnumItem(which, 0),
      78             :     pValues( 0 ),
      79        1005 :     pDisabledValues( 0 )
      80             : {
      81        1005 : }
      82             : 
      83             : 
      84             : 
      85             : 
      86        1824 : SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
      87             :     SfxEnumItem(rCopy),
      88             :     pValues(0),
      89        1824 :     pDisabledValues( 0 )
      90             : {
      91        1824 :     if ( !rCopy.pValues )
      92        1824 :         return;
      93             : 
      94        1824 :     pValues = new SfxAllEnumValueArr;
      95             : 
      96        3648 :     for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->size(); ++nPos )
      97             :     {
      98        1824 :         SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
      99        1824 :         pVal->nValue = (*rCopy.pValues)[nPos]->nValue;
     100        1824 :         pVal->aText = (*rCopy.pValues)[nPos]->aText;
     101        1824 :         pValues->insert( pValues->begin() + nPos, pVal );
     102             :     }
     103             : 
     104        1824 :     if( rCopy.pDisabledValues )
     105             :     {
     106           0 :         pDisabledValues = new std::vector<sal_uInt16>( *(rCopy.pDisabledValues) );
     107             :     }
     108             : }
     109             : 
     110             : 
     111             : 
     112       11804 : SfxAllEnumItem::~SfxAllEnumItem()
     113             : {
     114        4205 :     delete pValues;
     115        4205 :     delete pDisabledValues;
     116        7599 : }
     117             : 
     118             : 
     119             : 
     120           0 : sal_uInt16 SfxAllEnumItem::GetValueCount() const
     121             : {
     122           0 :     return pValues ? pValues->size() : 0;
     123             : }
     124             : 
     125             : 
     126             : 
     127           0 : OUString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
     128             : {
     129             :     DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
     130           0 :     return (*pValues)[nPos]->aText;
     131             : }
     132             : 
     133             : 
     134             : 
     135           0 : sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
     136             : {
     137             :     DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
     138           0 :     return (*pValues)[nPos]->nValue;
     139             : }
     140             : 
     141             : 
     142             : 
     143        1824 : SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
     144             : {
     145        1824 :     return new SfxAllEnumItem(*this);
     146             : }
     147             : 
     148             : 
     149             : 
     150           0 : SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
     151             : {
     152           0 :     return new SfxAllEnumItem( Which(), rStream );
     153             : }
     154             : 
     155             : 
     156             : 
     157             : 
     158         811 : sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
     159             : 
     160             : /*  [Beschreibung]
     161             : 
     162             :     Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
     163             :     diese interne Methode bei nicht vorhandenen Values die Position,
     164             :     an der der Wert liegen w"urde.
     165             : */
     166             : 
     167             : {
     168         811 :     if ( !pValues )
     169           0 :         return 0;
     170             : 
     171             :     //!O: binaere Suche oder SortArray verwenden
     172             :     sal_uInt16 nPos;
     173         811 :     for ( nPos = 0; nPos < pValues->size(); ++nPos )
     174           0 :         if ( (*pValues)[nPos]->nValue >= nVal )
     175           0 :             return nPos;
     176         811 :     return nPos;
     177             : }
     178             : 
     179             : 
     180             : 
     181           0 : sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
     182             : 
     183             : /*  [Beschreibung]
     184             : 
     185             :     Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
     186             :     immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
     187             :     Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
     188             : */
     189             : 
     190             : {
     191           0 :     if ( !pValues || pValues->empty() )
     192           0 :         return nValue;
     193             : 
     194           0 :     return SfxEnumItem::GetPosByValue( nValue );
     195             : }
     196             : 
     197             : 
     198             : 
     199           0 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const OUString &rValue )
     200             : {
     201           0 :     SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
     202           0 :     pVal->nValue = nValue;
     203           0 :     pVal->aText = rValue;
     204           0 :     if ( !pValues )
     205           0 :         pValues = new SfxAllEnumValueArr;
     206           0 :     else if ( GetPosByValue( nValue ) != USHRT_MAX )
     207             :         // remove when exists
     208           0 :         RemoveValue( nValue );
     209             :     // then insert
     210           0 :     pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
     211           0 : }
     212             : 
     213             : 
     214             : 
     215         811 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
     216             : {
     217         811 :     SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
     218         811 :     pVal->nValue = nValue;
     219         811 :     pVal->aText = OUString::number(nValue);
     220         811 :     if ( !pValues )
     221         811 :         pValues = new SfxAllEnumValueArr;
     222             : 
     223         811 :     pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
     224         811 : }
     225             : 
     226           0 : void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
     227             : {
     228           0 :     if ( !pDisabledValues )
     229           0 :         pDisabledValues = new std::vector<sal_uInt16>;
     230             : 
     231           0 :     pDisabledValues->push_back( nValue );
     232           0 : }
     233             : 
     234           0 : bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
     235             : {
     236           0 :     if ( pDisabledValues )
     237             :     {
     238           0 :         for ( size_t i=0; i<pDisabledValues->size(); i++ )
     239           0 :             if ( (*pDisabledValues)[i] == nValue )
     240           0 :                 return false;
     241             :     }
     242             : 
     243           0 :     return true;
     244             : }
     245             : 
     246             : 
     247             : 
     248           0 : void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
     249             : {
     250           0 :     sal_uInt16 nPos = GetPosByValue(nValue);
     251             :     DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
     252           0 :     pValues->erase( pValues->begin() + nPos );
     253           0 : }
     254             : 
     255             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10