LCOV - code coverage report
Current view: top level - sw/source/core/layout - sortedobjs.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 106 115 92.2 %
Date: 2015-06-13 12:38:46 Functions: 14 15 93.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 <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        4429 : SwSortedObjs::SwSortedObjs()
      35             : {
      36        4429 : }
      37             : 
      38        4429 : SwSortedObjs::~SwSortedObjs()
      39             : {
      40        4429 : }
      41             : 
      42    20955519 : size_t SwSortedObjs::size() const
      43             : {
      44    20955519 :     return maSortedObjLst.size();
      45             : }
      46             : 
      47     7680745 : SwAnchoredObject* SwSortedObjs::operator[]( size_t _nIndex ) const
      48             : {
      49     7680745 :     SwAnchoredObject* pAnchoredObj = 0L;
      50             : 
      51     7680745 :     if ( _nIndex >= size() )
      52             :     {
      53             :         OSL_FAIL( "<SwSortedObjs::operator[]> - index out of range" );
      54             :     }
      55             :     else
      56             :     {
      57     7680745 :         pAnchoredObj = maSortedObjLst[ _nIndex ];
      58             :     }
      59             : 
      60     7680745 :     return pAnchoredObj;
      61             : }
      62             : 
      63             : namespace
      64             : {
      65       15454 :     int GetAnchorWeight(RndStdIds eAnchor)
      66             :     {
      67       15454 :         if (eAnchor == FLY_AT_CHAR)
      68         701 :             return 0;
      69       14753 :         if (eAnchor == FLY_AS_CHAR)
      70          29 :             return 1;
      71       14724 :         return 2;
      72             :     }
      73             : }
      74             : 
      75             : struct ObjAnchorOrder
      76             : {
      77        9742 :     bool operator()( const SwAnchoredObject* _pListedAnchoredObj,
      78             :                      const SwAnchoredObject* _pNewAnchoredObj )
      79             :     {
      80             :         // get attributes of listed object
      81        9742 :         const SwFrameFormat& rFormatListed = _pListedAnchoredObj->GetFrameFormat();
      82        9742 :         const SwFormatAnchor* pAnchorListed = &(rFormatListed.GetAnchor());
      83             : 
      84             :         // get attributes of new object
      85        9742 :         const SwFrameFormat& rFormatNew = _pNewAnchoredObj->GetFrameFormat();
      86        9742 :         const SwFormatAnchor* pAnchorNew = &(rFormatNew.GetAnchor());
      87             : 
      88             :         // check for to-page anchored objects
      89       10456 :         if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
      90         714 :             (pAnchorNew   ->GetAnchorId() != FLY_AT_PAGE))
      91             :         {
      92          24 :             return true;
      93             :         }
      94       18746 :         else if ((pAnchorListed->GetAnchorId() != FLY_AT_PAGE) &&
      95        9028 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_PAGE))
      96             :         {
      97          21 :             return false;
      98             :         }
      99       10387 :         else if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
     100         690 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_PAGE))
     101             :         {
     102         690 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     103             :         }
     104             : 
     105             :         // Both objects aren't anchored to page.
     106             :         // Thus, check for to-fly anchored objects
     107        9098 :         if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
     108          91 :             (pAnchorNew   ->GetAnchorId() != FLY_AT_FLY))
     109             :         {
     110           4 :             return true;
     111             :         }
     112       17919 :         else if ((pAnchorListed->GetAnchorId() != FLY_AT_FLY) &&
     113        8916 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_FLY))
     114             :         {
     115           0 :             return false;
     116             :         }
     117        9090 :         else if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
     118          87 :                  (pAnchorNew   ->GetAnchorId() == FLY_AT_FLY))
     119             :         {
     120          87 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     121             :         }
     122             : 
     123             :         // Both objects aren't anchor to page or to fly
     124             :         // Thus, compare content anchor nodes, if existing.
     125        8916 :         const SwPosition* pContentAnchorListed = pAnchorListed->GetContentAnchor();
     126        8916 :         const SwPosition* pContentAnchorNew = pAnchorNew->GetContentAnchor();
     127       17832 :         if ( pContentAnchorListed && pContentAnchorNew &&
     128        8916 :              pContentAnchorListed->nNode != pContentAnchorNew->nNode )
     129             :         {
     130         839 :             return pContentAnchorListed->nNode < pContentAnchorNew->nNode;
     131             :         }
     132             : 
     133             :         // objects anchored at the same content.
     134             :         // --> OD 2006-11-29 #???# - objects have to be ordered by anchor node position
     135             :         // Thus, compare content anchor node positions and anchor type,
     136             :         // if not anchored at-paragraph
     137        8077 :         if (pContentAnchorListed && pContentAnchorNew)
     138             :         {
     139        8077 :             sal_Int32 nListedIndex = pAnchorListed->GetAnchorId() != FLY_AT_PARA ?
     140        8077 :                 pContentAnchorListed->nContent.GetIndex() : 0;
     141        8077 :             sal_Int32 nNewIndex = pAnchorNew->GetAnchorId() != FLY_AT_PARA ?
     142        8077 :                 pContentAnchorNew->nContent.GetIndex() : 0;
     143        8077 :             if (nListedIndex != nNewIndex)
     144             :             {
     145         350 :                 return nListedIndex < nNewIndex;
     146             :             }
     147             :         }
     148             : 
     149        7727 :         int nAnchorListedWeight = GetAnchorWeight(pAnchorListed->GetAnchorId());
     150        7727 :         int nAnchorNewWeight = GetAnchorWeight(pAnchorNew->GetAnchorId());
     151        7727 :         if (nAnchorListedWeight != nAnchorNewWeight)
     152             :         {
     153          99 :             return nAnchorListedWeight < nAnchorNewWeight;
     154             :         }
     155             : 
     156             :         // objects anchored at the same content and at the same content anchor
     157             :         // node position with the same anchor type
     158             :         // Thus, compare its wrapping style including its layer
     159        7628 :         const IDocumentDrawModelAccess* pIDDMA = rFormatListed.getIDocumentDrawModelAccess();
     160        7628 :         const SdrLayerID nHellId = pIDDMA->GetHellId();
     161        7628 :         const SdrLayerID nInvisibleHellId = pIDDMA->GetInvisibleHellId();
     162             :         const bool bWrapThroughOrHellListed =
     163        8250 :                     rFormatListed.GetSurround().GetSurround() == SURROUND_THROUGHT ||
     164        8204 :                     _pListedAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
     165        8204 :                     _pListedAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
     166             :         const bool bWrapThroughOrHellNew =
     167        8237 :                     rFormatNew.GetSurround().GetSurround() == SURROUND_THROUGHT ||
     168        8133 :                     _pNewAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
     169        8133 :                     _pNewAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
     170        7628 :         if ( bWrapThroughOrHellListed != bWrapThroughOrHellNew )
     171             :         {
     172         259 :             if ( bWrapThroughOrHellListed )
     173         103 :                 return false;
     174             :             else
     175         156 :                 return true;
     176             :         }
     177        7369 :         else if ( bWrapThroughOrHellListed && bWrapThroughOrHellNew )
     178             :         {
     179        6994 :             return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     180             :         }
     181             : 
     182             :         // objects anchored at the same content with a set text wrapping
     183             :         // Thus, compare wrap influences on object position
     184             :         const SwFormatWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosListed =
     185         375 :                                         &(rFormatListed.GetWrapInfluenceOnObjPos());
     186             :         const SwFormatWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosNew =
     187         375 :                                         &(rFormatNew.GetWrapInfluenceOnObjPos());
     188             :         // #i35017# - handle ITERATIVE as ONCE_SUCCESSIVE
     189         750 :         if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true ) !=
     190         375 :                 pWrapInfluenceOnObjPosNew->GetWrapInfluenceOnObjPos( true ) )
     191             :         {
     192             :             // #i35017# - constant name has changed
     193           0 :             if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true )
     194             :                             == text::WrapInfluenceOnPosition::ONCE_SUCCESSIVE )
     195           0 :                 return true;
     196             :             else
     197           0 :                 return false;
     198             :         }
     199             : 
     200             :         // objects anchored at the same content position/page/fly with same
     201             :         // wrap influence.
     202             :         // Thus, compare anchor order number
     203         375 :         return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
     204             :     }
     205             : };
     206             : 
     207           0 : bool SwSortedObjs::is_sorted() const
     208             : {
     209           0 :     return std::is_sorted(maSortedObjLst.begin(), maSortedObjLst.end(), ObjAnchorOrder());
     210             : }
     211             : 
     212        7638 : bool SwSortedObjs::Insert( SwAnchoredObject& _rAnchoredObj )
     213             : {
     214             :     // #i51941#
     215        7638 :     if ( Contains( _rAnchoredObj ) )
     216             :     {
     217             :         // list already contains object
     218             :         OSL_FAIL( "<SwSortedObjs::Insert()> - already contains object" );
     219           0 :         return true;
     220             :     }
     221             : 
     222             :     // find insert position
     223             :     std::vector< SwAnchoredObject* >::iterator aInsPosIter =
     224             :         std::lower_bound( maSortedObjLst.begin(), maSortedObjLst.end(),
     225        7638 :                           &_rAnchoredObj, ObjAnchorOrder() );
     226             : 
     227             :     // insert object into list
     228        7638 :     maSortedObjLst.insert( aInsPosIter, &_rAnchoredObj );
     229             : 
     230        7638 :     return Contains( _rAnchoredObj );
     231             : }
     232             : 
     233        5127 : bool SwSortedObjs::Remove( SwAnchoredObject& _rAnchoredObj )
     234             : {
     235        5127 :     bool bRet = true;
     236             : 
     237             :     std::vector< SwAnchoredObject* >::iterator aDelPosIter =
     238        5127 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     239             : 
     240        5127 :     if ( aDelPosIter == maSortedObjLst.end() )
     241             :     {
     242             :         // object not found.
     243           0 :         bRet = false;
     244             :         OSL_FAIL( "<SwSortedObjs::Remove()> - object not found" );
     245             :     }
     246             :     else
     247             :     {
     248        5127 :         maSortedObjLst.erase( aDelPosIter );
     249             :     }
     250             : 
     251        5127 :     return bRet;
     252             : }
     253             : 
     254       15564 : bool SwSortedObjs::Contains( const SwAnchoredObject& _rAnchoredObj ) const
     255             : {
     256             :     std::vector< SwAnchoredObject* >::const_iterator aIter =
     257       15564 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     258             : 
     259       15564 :     return aIter != maSortedObjLst.end();
     260             : }
     261             : 
     262         151 : bool SwSortedObjs::Update( SwAnchoredObject& _rAnchoredObj )
     263             : {
     264         151 :     if ( !Contains( _rAnchoredObj ) )
     265             :     {
     266             :         // given anchored object not found in list
     267             :         OSL_FAIL( "<SwSortedObjs::Update(..) - sorted list doesn't contain given anchored object" );
     268           0 :         return false;
     269             :     }
     270             : 
     271         151 :     if ( size() == 1 )
     272             :     {
     273             :         // given anchored object is the only one in the list.
     274          22 :         return true;
     275             :     }
     276             : 
     277         129 :     Remove( _rAnchoredObj );
     278         129 :     Insert( _rAnchoredObj );
     279             : 
     280         129 :     return Contains( _rAnchoredObj );
     281             : }
     282             : 
     283         252 : void SwSortedObjs::UpdateAll()
     284             : {
     285         252 :     std::stable_sort(maSortedObjLst.begin(), maSortedObjLst.end(), ObjAnchorOrder());
     286         252 : }
     287             : 
     288       14198 : size_t SwSortedObjs::ListPosOf( const SwAnchoredObject& _rAnchoredObj ) const
     289             : {
     290             :     std::vector< SwAnchoredObject* >::const_iterator aIter =
     291       14198 :         std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
     292             : 
     293       14198 :     if ( aIter != maSortedObjLst.end() )
     294             :     {
     295             :         // #i51941#
     296             :         std::vector< SwAnchoredObject* >::difference_type nPos =
     297       14196 :                                                 aIter - maSortedObjLst.begin();
     298       14196 :         return static_cast<size_t>( nPos );
     299             :     }
     300             : 
     301           2 :     return size();
     302         177 : }
     303             : 
     304             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11