LCOV - code coverage report
Current view: top level - sw/source/core/layout - sortedobjs.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 91 104 87.5 %
Date: 2014-04-11 Functions: 10 10 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 <sortedobjs.hxx>
      21             : 
      22             : #include <algorithm>
      23             : #include <anchoredobject.hxx>
      24             : #include <fmtanchr.hxx>
      25             : #include <fmtsrnd.hxx>
      26             : #include <fmtwrapinfluenceonobjpos.hxx>
      27             : #include <frmfmt.hxx>
      28             : #include <pam.hxx>
      29             : #include <svx/svdobj.hxx>
      30             : #include <IDocumentDrawModelAccess.hxx>
      31             : 
      32             : using namespace ::com::sun::star;
      33             : 
      34        2613 : SwSortedObjs::SwSortedObjs()
      35             : {
      36        2613 : }
      37             : 
      38        2613 : SwSortedObjs::~SwSortedObjs()
      39             : {
      40        2613 : }
      41             : 
      42     2174598 : sal_uInt32 SwSortedObjs::Count() const
      43             : {
      44     2174598 :     return maSortedObjLst.size();
      45             : }
      46             : 
      47     1096628 : SwAnchoredObject* SwSortedObjs::operator[]( sal_uInt32 _nIndex ) const
      48             : {
      49     1096628 :     SwAnchoredObject* pAnchoredObj = 0L;
      50             : 
      51     1096628 :     if ( _nIndex >= Count() )
      52             :     {
      53             :         OSL_FAIL( "<SwSortedObjs::operator[]> - index out of range" );
      54             :     }
      55             :     else
      56             :     {
      57     1096628 :         pAnchoredObj = maSortedObjLst[ _nIndex ];
      58             :     }
      59             : 
      60     1096628 :     return pAnchoredObj;
      61             : }
      62             : 
      63             : struct ObjAnchorOrder
      64             : {
      65        4455 :     bool operator()( const SwAnchoredObject* _pListedAnchoredObj,
      66             :                      const SwAnchoredObject* _pNewAnchoredObj )
      67             :     {
      68             :         // get attributes of listed object
      69        4455 :         const SwFrmFmt& rFmtListed = _pListedAnchoredObj->GetFrmFmt();
      70        4455 :         const SwFmtAnchor* pAnchorListed = &(rFmtListed.GetAnchor());
      71             : 
      72             :         // get attributes of new object
      73        4455 :         const SwFrmFmt& rFmtNew = _pNewAnchoredObj->GetFrmFmt();
      74        4455 :         const SwFmtAnchor* pAnchorNew = &(rFmtNew.GetAnchor());
      75             : 
      76             :         // check for to-page anchored objects
      77        5110 :         if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
      78         655 :             (pAnchorNew   ->GetAnchorId() != FLY_AT_PAGE))
      79             :         {
      80          51 :             return true;
      81             :         }
      82        8204 :         else if ((pAnchorListed->GetAnchorId() != FLY_AT_PAGE) &&
      83        3800 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_PAGE))
      84             :         {
      85           3 :             return false;
      86             :         }
      87        5005 :         else if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
      88         604 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_PAGE))
      89             :         {
      90         604 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
      91             :         }
      92             : 
      93             :         // Both objects aren't anchored to page.
      94             :         // Thus, check for to-fly anchored objects
      95        3797 :         if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
      96           0 :             (pAnchorNew   ->GetAnchorId() != FLY_AT_FLY))
      97             :         {
      98           0 :             return true;
      99             :         }
     100        7594 :         else if ((pAnchorListed->GetAnchorId() != FLY_AT_FLY) &&
     101        3797 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_FLY))
     102             :         {
     103           0 :             return false;
     104             :         }
     105        3797 :         else if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
     106           0 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_FLY))
     107             :         {
     108           0 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     109             :         }
     110             : 
     111             :         // Both objects aren't anchor to page or to fly
     112             :         // Thus, compare content anchor nodes, if existing.
     113        3797 :         const SwPosition* pCntntAnchorListed = pAnchorListed->GetCntntAnchor();
     114        3797 :         const SwPosition* pCntntAnchorNew = pAnchorNew->GetCntntAnchor();
     115        7594 :         if ( pCntntAnchorListed && pCntntAnchorNew &&
     116        3797 :              pCntntAnchorListed->nNode != pCntntAnchorNew->nNode )
     117             :         {
     118         354 :             return pCntntAnchorListed->nNode < pCntntAnchorNew->nNode;
     119             :         }
     120             : 
     121             :         // objects anchored at the same content.
     122             :         // --> OD 2006-11-29 #???# - objects have to be ordered by anchor node position
     123             :         // Thus, compare content anchor node positions and anchor type,
     124             :         // if not anchored at-paragraph
     125        7373 :         if ((pAnchorListed->GetAnchorId() != FLY_AT_PARA) &&
     126         963 :             (pAnchorNew   ->GetAnchorId() != FLY_AT_PARA) &&
     127        3919 :              pCntntAnchorListed && pCntntAnchorNew )
     128             :         {
     129         476 :             if ( pCntntAnchorListed->nContent != pCntntAnchorNew->nContent )
     130             :             {
     131         337 :                 return pCntntAnchorListed->nContent < pCntntAnchorNew->nContent;
     132             :             }
     133         275 :             else if ((pAnchorListed->GetAnchorId() == FLY_AT_CHAR) &&
     134         136 :                      (pAnchorNew   ->GetAnchorId() == FLY_AS_CHAR))
     135             :             {
     136           0 :                 return true;
     137             :             }
     138         142 :             else if ((pAnchorListed->GetAnchorId() == FLY_AS_CHAR) &&
     139           3 :                      (pAnchorNew   ->GetAnchorId() == FLY_AT_CHAR))
     140             :             {
     141           3 :                 return false;
     142             :             }
     143             :         }
     144             : 
     145             :         // objects anchored at the same content and at the same content anchor
     146             :         // node position with the same anchor type
     147             :         // Thus, compare its wrapping style including its layer
     148        3103 :         const IDocumentDrawModelAccess* pIDDMA = rFmtListed.getIDocumentDrawModelAccess();
     149        3103 :         const SdrLayerID nHellId = pIDDMA->GetHellId();
     150        3103 :         const SdrLayerID nInvisibleHellId = pIDDMA->GetInvisibleHellId();
     151             :         const bool bWrapThroughOrHellListed =
     152        3189 :                     rFmtListed.GetSurround().GetSurround() == SURROUND_THROUGHT ||
     153        3189 :                     _pListedAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
     154        3189 :                     _pListedAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
     155             :         const bool bWrapThroughOrHellNew =
     156        3201 :                     rFmtNew.GetSurround().GetSurround() == SURROUND_THROUGHT ||
     157        3201 :                     _pNewAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
     158        3201 :                     _pNewAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
     159        3103 :         if ( bWrapThroughOrHellListed != bWrapThroughOrHellNew )
     160             :         {
     161          12 :             if ( bWrapThroughOrHellListed )
     162          12 :                 return false;
     163             :             else
     164           0 :                 return true;
     165             :         }
     166        3091 :         else if ( bWrapThroughOrHellListed && bWrapThroughOrHellNew )
     167             :         {
     168        3017 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     169             :         }
     170             : 
     171             :         // objects anchored at the same content with a set text wrapping
     172             :         // Thus, compare wrap influences on object position
     173             :         const SwFmtWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosListed =
     174          74 :                                         &(rFmtListed.GetWrapInfluenceOnObjPos());
     175             :         const SwFmtWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosNew =
     176          74 :                                         &(rFmtNew.GetWrapInfluenceOnObjPos());
     177             :         // #i35017# - handle ITERATIVE as ONCE_SUCCESSIVE
     178         148 :         if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true ) !=
     179          74 :                 pWrapInfluenceOnObjPosNew->GetWrapInfluenceOnObjPos( true ) )
     180             :         {
     181             :             // #i35017# - constant name has changed
     182           0 :             if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true )
     183             :                             == text::WrapInfluenceOnPosition::ONCE_SUCCESSIVE )
     184           0 :                 return true;
     185             :             else
     186           0 :                 return false;
     187             :         }
     188             : 
     189             :         // objects anchored at the same content position/page/fly with same
     190             :         // wrap influence.
     191             :         // Thus, compare anchor order number
     192          74 :         return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     193             :     }
     194             : };
     195             : 
     196        3850 : bool SwSortedObjs::Insert( SwAnchoredObject& _rAnchoredObj )
     197             : {
     198             :     // #i51941#
     199        3850 :     if ( Contains( _rAnchoredObj ) )
     200             :     {
     201             :         // list already contains object
     202             :         OSL_FAIL( "<SwSortedObjs::Insert()> - already contains object" );
     203           0 :         return true;
     204             :     }
     205             : 
     206             :     // find insert position
     207             :     std::vector< SwAnchoredObject* >::iterator aInsPosIter =
     208             :         std::lower_bound( maSortedObjLst.begin(), maSortedObjLst.end(),
     209        3850 :                           &_rAnchoredObj, ObjAnchorOrder() );
     210             : 
     211             :     // insert object into list
     212        3850 :     maSortedObjLst.insert( aInsPosIter, &_rAnchoredObj );
     213             : 
     214        3850 :     return Contains( _rAnchoredObj );
     215             : }
     216             : 
     217        2748 : bool SwSortedObjs::Remove( SwAnchoredObject& _rAnchoredObj )
     218             : {
     219        2748 :     bool bRet = true;
     220             : 
     221             :     std::vector< SwAnchoredObject* >::iterator aDelPosIter =
     222        2748 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     223             : 
     224        2748 :     if ( aDelPosIter == maSortedObjLst.end() )
     225             :     {
     226             :         // object not found.
     227           0 :         bRet = false;
     228             :         OSL_FAIL( "<SwSortedObjs::Remove()> - object not found" );
     229             :     }
     230             :     else
     231             :     {
     232        2748 :         maSortedObjLst.erase( aDelPosIter );
     233             :     }
     234             : 
     235        2748 :     return bRet;
     236             : }
     237             : 
     238        7904 : bool SwSortedObjs::Contains( const SwAnchoredObject& _rAnchoredObj ) const
     239             : {
     240             :     std::vector< SwAnchoredObject* >::const_iterator aIter =
     241        7904 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     242             : 
     243        7904 :     return aIter != maSortedObjLst.end();
     244             : }
     245             : 
     246         143 : bool SwSortedObjs::Update( SwAnchoredObject& _rAnchoredObj )
     247             : {
     248         143 :     if ( !Contains( _rAnchoredObj ) )
     249             :     {
     250             :         // given anchored object not found in list
     251             :         OSL_FAIL( "<SwSortedObjs::Update(..) - sorted list doesn't contain given anchored object" );
     252           0 :         return false;
     253             :     }
     254             : 
     255         143 :     if ( Count() == 1 )
     256             :     {
     257             :         // given anchored object is the only one in the list.
     258          82 :         return true;
     259             :     }
     260             : 
     261          61 :     Remove( _rAnchoredObj );
     262          61 :     Insert( _rAnchoredObj );
     263             : 
     264          61 :     return Contains( _rAnchoredObj );
     265             : }
     266             : 
     267        7253 : sal_uInt32 SwSortedObjs::ListPosOf( const SwAnchoredObject& _rAnchoredObj ) const
     268             : {
     269        7253 :     sal_uInt32 nRetLstPos = Count();
     270             : 
     271             :     std::vector< SwAnchoredObject* >::const_iterator aIter =
     272        7253 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     273             : 
     274        7253 :     if ( aIter != maSortedObjLst.end() )
     275             :     {
     276             :         // #i51941#
     277             :         std::vector< SwAnchoredObject* >::difference_type nPos =
     278        7253 :                                                 aIter - maSortedObjLst.begin();
     279        7253 :         nRetLstPos = sal_uInt32( nPos );
     280             :     }
     281             : 
     282        7253 :     return nRetLstPos;
     283             : }
     284             : 
     285             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10