LCOV - code coverage report
Current view: top level - sw/source/core/txtnode - modeltoviewhelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 148 150 98.7 %
Date: 2015-06-13 12:38:46 Functions: 14 14 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 <tools/multisel.hxx>
      21             : #include <doc.hxx>
      22             : #include <IMark.hxx>
      23             : #include <fldbas.hxx>
      24             : #include <fmtfld.hxx>
      25             : #include <fmtftn.hxx>
      26             : #include <modeltoviewhelper.hxx>
      27             : #include <ndtxt.hxx>
      28             : #include <pam.hxx>
      29             : #include <txatbase.hxx>
      30             : #include <txtfld.hxx>
      31             : #include <txtftn.hxx>
      32             : #include <scriptinfo.hxx>
      33             : #include <set>
      34             : #include <vector>
      35             : 
      36        9051 : struct FieldResult
      37             : {
      38             :     sal_Int32 m_nFieldPos;
      39             :     OUString m_sExpand;
      40             :     enum { NONE, FIELD, FOOTNOTE } m_eType;
      41        3017 :     explicit FieldResult(sal_Int32 const nPos)
      42        3017 :         : m_nFieldPos(nPos), m_eType(NONE)
      43        3017 :     { }
      44             : };
      45             : 
      46             : class sortfieldresults :
      47             :     public std::binary_function<const FieldResult&, const FieldResult&, bool>
      48             : {
      49             : public:
      50        8047 :     bool operator()(const FieldResult &rOne, const FieldResult &rTwo) const
      51             :     {
      52        8047 :         return rOne.m_nFieldPos < rTwo.m_nFieldPos;
      53             :     }
      54             : };
      55             : 
      56             : typedef std::set<FieldResult, sortfieldresults> FieldResultSet;
      57             : 
      58       26185 : struct block
      59             : {
      60             :     sal_Int32 m_nStart;
      61             :     sal_Int32 m_nLen;
      62             :     bool m_bVisible;
      63             :     FieldResultSet m_aAttrs;
      64        8703 :     block(sal_Int32 nStart, sal_Int32 nLen, bool bVisible)
      65        8703 :         : m_nStart(nStart), m_nLen(nLen), m_bVisible(bVisible)
      66             :     {
      67        8703 :     }
      68             : };
      69             : 
      70             : struct containsPos
      71             : {
      72             :     const sal_Int32 m_nPos;
      73        3017 :     explicit containsPos(const sal_Int32 nPos)
      74        3017 :         : m_nPos(nPos)
      75             :     {
      76        3017 :     }
      77        3033 :     bool operator() (const block& rIn) const
      78             :     {
      79        3033 :         return m_nPos >= rIn.m_nStart && m_nPos < rIn.m_nStart + rIn.m_nLen;
      80             :     }
      81             : };
      82             : 
      83        9135 : ModelToViewHelper::ModelToViewHelper(const SwTextNode &rNode, ExpandMode eMode)
      84             : {
      85        9135 :     const OUString& rNodeText = rNode.GetText();
      86        9135 :     m_aRetText = rNodeText;
      87             : 
      88        9135 :     if (eMode == ExpandMode::PassThrough)
      89        9136 :         return;
      90             : 
      91        9134 :     Range aRange( 0, rNodeText.isEmpty() ? 0 : rNodeText.getLength() - 1);
      92        9134 :     MultiSelection aHiddenMulti(aRange);
      93             : 
      94        9134 :     if (eMode & ExpandMode::HideInvisible)
      95        5442 :         SwScriptInfo::selectHiddenTextProperty(rNode, aHiddenMulti);
      96             : 
      97        9134 :     if (eMode & ExpandMode::HideDeletions)
      98        5442 :         SwScriptInfo::selectRedLineDeleted(rNode, aHiddenMulti);
      99             : 
     100       18268 :     std::vector<block> aBlocks;
     101             : 
     102        9134 :     sal_Int32 nShownStart = 0;
     103        9153 :     for (size_t i = 0; i < aHiddenMulti.GetRangeCount(); ++i)
     104             :     {
     105          19 :         const Range& rRange = aHiddenMulti.GetRange(i);
     106          19 :         const sal_Int32 nHiddenStart = rRange.Min();
     107          19 :         const sal_Int32 nHiddenEnd = rRange.Max() + 1;
     108          19 :         const sal_Int32 nHiddenLen = nHiddenEnd - nHiddenStart;
     109             : 
     110          19 :         const sal_Int32 nShownEnd = nHiddenStart;
     111          19 :         const sal_Int32 nShownLen = nShownEnd - nShownStart;
     112             : 
     113          19 :         if (nShownLen)
     114          14 :             aBlocks.push_back(block(nShownStart, nShownLen, true));
     115             : 
     116          19 :         if (nHiddenLen)
     117          19 :             aBlocks.push_back(block(nHiddenStart, nHiddenLen, false));
     118             : 
     119          19 :         nShownStart = nHiddenEnd;
     120             :     }
     121             : 
     122        9134 :     sal_Int32 nTrailingShownLen = rNodeText.getLength() - nShownStart;
     123        9134 :     if (nTrailingShownLen)
     124        8670 :         aBlocks.push_back(block(nShownStart, nTrailingShownLen, true));
     125             : 
     126        9134 :     if (eMode & ExpandMode::ExpandFields || eMode & ExpandMode::ExpandFootnote)
     127             :     {
     128             :         //first the normal fields, get their position in the node and what the text they expand
     129             :         //to is
     130        9131 :         const SwpHints* pSwpHints2 = rNode.GetpSwpHints();
     131       15455 :         for ( size_t i = 0; pSwpHints2 && i < pSwpHints2->Count(); ++i )
     132             :         {
     133        6324 :             const SwTextAttr* pAttr = (*pSwpHints2)[i];
     134        6324 :             if (pAttr->HasDummyChar())
     135             :             {
     136        3021 :                 const sal_Int32 nDummyCharPos = pAttr->GetStart();
     137        3021 :                 if (aHiddenMulti.IsSelected(nDummyCharPos))
     138           6 :                     continue;
     139             :                 std::vector<block>::iterator aFind = std::find_if(aBlocks.begin(),
     140        3015 :                     aBlocks.end(), containsPos(nDummyCharPos));
     141        3015 :                 if (aFind != aBlocks.end())
     142             :                 {
     143        3015 :                     FieldResult aFieldResult(nDummyCharPos);
     144        3015 :                     switch (pAttr->Which())
     145             :                     {
     146             :                         case RES_TXTATR_FIELD:
     147             :                         case RES_TXTATR_ANNOTATION:
     148         454 :                             if (eMode & ExpandMode::ExpandFields)
     149             :                             {
     150        1816 :                                 aFieldResult.m_sExpand = (eMode & ExpandMode::ReplaceMode)
     151             :                                     ? OUString(CHAR_ZWSP)
     152         454 :                                     : static_txtattr_cast<SwTextField const*>(pAttr)->
     153        1362 :                                       GetFormatField().GetField()->ExpandField(true);
     154         454 :                                 aFieldResult.m_eType = FieldResult::FIELD;
     155             :                             }
     156         454 :                             break;
     157             :                         case RES_TXTATR_FTN:
     158         107 :                             if (eMode & ExpandMode::ExpandFootnote)
     159             :                             {
     160         103 :                                 const SwFormatFootnote& rFootnote = static_cast<SwTextFootnote const*>(pAttr)->GetFootnote();
     161         103 :                                 const SwDoc *pDoc = rNode.GetDoc();
     162         309 :                                 aFieldResult.m_sExpand = (eMode & ExpandMode::ReplaceMode)
     163             :                                     ? OUString(CHAR_ZWSP)
     164         206 :                                     : rFootnote.GetViewNumStr(*pDoc);
     165         103 :                                 aFieldResult.m_eType = FieldResult::FOOTNOTE;
     166             :                             }
     167         107 :                             break;
     168             :                         default:
     169        2454 :                             break;
     170             :                     }
     171        3015 :                     aFind->m_aAttrs.insert(aFieldResult);
     172             :                 }
     173             :             }
     174             :         }
     175             : 
     176        9131 :         if (eMode & ExpandMode::ExpandFields)
     177             :         {
     178             :             //now get the dropdown formfields, get their position in the node and what the text they expand
     179             :             //to is
     180        9131 :             SwPaM aPaM(rNode, 0, rNode, rNode.Len());
     181             :             std::vector<sw::mark::IFieldmark*> aDropDowns =
     182       18262 :                 rNode.GetDoc()->getIDocumentMarkAccess()->getDropDownsFor(aPaM);
     183             : 
     184        9133 :             for (std::vector<sw::mark::IFieldmark*>::iterator aI = aDropDowns.begin(), aEnd = aDropDowns.end();
     185             :                 aI != aEnd; ++aI)
     186             :             {
     187           2 :                 sw::mark::IFieldmark *pMark = *aI;
     188           2 :                 const sal_Int32 nDummyCharPos = pMark->GetMarkPos().nContent.GetIndex()-1;
     189           2 :                 if (aHiddenMulti.IsSelected(nDummyCharPos))
     190           0 :                     continue;
     191             :                 std::vector<block>::iterator aFind = std::find_if(aBlocks.begin(), aBlocks.end(),
     192           2 :                     containsPos(nDummyCharPos));
     193           2 :                 if (aFind != aBlocks.end())
     194             :                 {
     195           2 :                     FieldResult aFieldResult(nDummyCharPos);
     196           6 :                     aFieldResult.m_sExpand = (eMode & ExpandMode::ReplaceMode)
     197             :                         ? OUString(CHAR_ZWSP)
     198           4 :                         : sw::mark::ExpandFieldmark(pMark);
     199           2 :                     aFieldResult.m_eType = FieldResult::FIELD;
     200           2 :                     aFind->m_aAttrs.insert(aFieldResult);
     201             :                 }
     202        9131 :             }
     203             :         }
     204             :     }
     205             : 
     206             :     //store the end of each range in the model and where that end of range
     207             :     //maps to in the view
     208        9134 :     sal_Int32 nOffset = 0;
     209       17837 :     for (std::vector<block>::iterator i = aBlocks.begin(); i != aBlocks.end(); ++i)
     210             :     {
     211        8703 :         const sal_Int32 nBlockLen = i->m_nLen;
     212        8703 :         if (!nBlockLen)
     213           0 :             continue;
     214        8703 :         const sal_Int32 nBlockStart = i->m_nStart;
     215        8703 :         const sal_Int32 nBlockEnd = nBlockStart + nBlockLen;
     216             : 
     217        8703 :         if (!i->m_bVisible)
     218             :         {
     219          19 :             sal_Int32 const modelBlockPos(nBlockEnd);
     220          19 :             sal_Int32 const viewBlockPos(nBlockStart + nOffset);
     221          19 :             m_aMap.push_back(ConversionMapEntry(modelBlockPos, viewBlockPos, false));
     222             : 
     223          19 :             m_aRetText = m_aRetText.replaceAt(nOffset + nBlockStart, nBlockLen, OUString());
     224          19 :             nOffset -= nBlockLen;
     225             :         }
     226             :         else
     227             :         {
     228       11701 :             for (FieldResultSet::iterator j = i->m_aAttrs.begin(); j != i->m_aAttrs.end(); ++j)
     229             :             {
     230        3017 :                 sal_Int32 const modelFieldPos(j->m_nFieldPos);
     231        3017 :                 sal_Int32 const viewFieldPos(j->m_nFieldPos + nOffset);
     232        3017 :                 m_aMap.push_back( ConversionMapEntry(modelFieldPos, viewFieldPos, true) );
     233             : 
     234        3017 :                 m_aRetText = m_aRetText.replaceAt(viewFieldPos, 1, j->m_sExpand);
     235        3017 :                 nOffset += j->m_sExpand.getLength() - 1;
     236             : 
     237        3017 :                 switch (j->m_eType)
     238             :                 {
     239             :                     case FieldResult::FIELD:
     240         456 :                         m_FieldPositions.push_back(viewFieldPos);
     241         456 :                     break;
     242             :                     case FieldResult::FOOTNOTE:
     243         103 :                         m_FootnotePositions.push_back(viewFieldPos);
     244         103 :                     break;
     245             :                     case FieldResult::NONE: /*ignore*/
     246        2458 :                     break;
     247             :                 }
     248             :             }
     249             : 
     250        8684 :             sal_Int32 const modelEndBlock(nBlockEnd);
     251        8684 :             sal_Int32 const viewFieldPos(nBlockEnd + nOffset);
     252        8684 :             m_aMap.push_back(ConversionMapEntry(modelEndBlock, viewFieldPos, true));
     253             :         }
     254        9134 :     }
     255             : }
     256             : 
     257             : /** Converts a model position into a view position
     258             : */
     259       17304 : sal_Int32 ModelToViewHelper::ConvertToViewPosition( sal_Int32 nModelPos ) const
     260             : {
     261             :     // Search for entry after nPos:
     262       17304 :     ConversionMap::const_iterator aIter;
     263             : 
     264       19779 :     for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
     265             :     {
     266       18863 :         if (aIter->m_nModelPos >= nModelPos)
     267             :         {
     268             :             //if it's an invisible portion, map all contained positions
     269             :             //to the anchor viewpos
     270       16388 :             if (!aIter->m_bVisible)
     271           5 :                 return aIter->m_nViewPos;
     272             : 
     273             :             //if it's a visible portion, then the view position is the anchor
     274             :             //viewpos - the offset of the input modelpos from the anchor
     275             :             //modelpos
     276       16383 :             const sal_Int32 nOffsetFromEnd = aIter->m_nModelPos - nModelPos;
     277       16383 :             return aIter->m_nViewPos - nOffsetFromEnd;
     278             :         }
     279             :     }
     280             : 
     281         916 :     return nModelPos;
     282             : }
     283             : 
     284             : /** Converts a view position into a model position
     285             : */
     286   139856909 : ModelToViewHelper::ModelPosition ModelToViewHelper::ConvertToModelPosition( sal_Int32 nViewPos ) const
     287             : {
     288   139856909 :     ModelPosition aRet;
     289   139856909 :     aRet.mnPos = nViewPos;
     290             : 
     291             :     // Search for entry after nPos:
     292   139856909 :     ConversionMap::const_iterator aIter;
     293   139857328 :     for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
     294             :     {
     295      263528 :         if (aIter->m_nViewPos > nViewPos)
     296             :         {
     297      263109 :             const sal_Int32 nPosModel  = aIter->m_nModelPos;
     298      263109 :             const sal_Int32 nPosExpand = aIter->m_nViewPos;
     299             : 
     300             :             // If nViewPos is in front of first field, we are finished.
     301      263109 :             if ( aIter == m_aMap.begin() )
     302      262810 :                 break;
     303             : 
     304         299 :             --aIter;
     305             : 
     306             :             // nPrevPosModel is the field position
     307         299 :             const sal_Int32 nPrevPosModel  = aIter->m_nModelPos;
     308         299 :             const sal_Int32 nPrevPosExpand = aIter->m_nViewPos;
     309             : 
     310         299 :             const sal_Int32 nLengthModel  = nPosModel - nPrevPosModel;
     311         299 :             const sal_Int32 nLengthExpand = nPosExpand - nPrevPosExpand;
     312             : 
     313         299 :             const sal_Int32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
     314         299 :             const sal_Int32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
     315             : 
     316             :             // Check if nPos is outside of field:
     317         299 :             if ( nFieldEndExpand <= nViewPos )
     318             :             {
     319             :                 // nPos is outside of field:
     320         130 :                 const sal_Int32 nDistToField = nViewPos - nFieldEndExpand + 1;
     321         130 :                 aRet.mnPos = nPrevPosModel + nDistToField;
     322             :             }
     323             :             else
     324             :             {
     325             :                 // nViewPos is inside a field:
     326         169 :                 aRet.mnPos = nPrevPosModel;
     327         169 :                 aRet.mnSubPos = nViewPos - nPrevPosExpand;
     328         169 :                 aRet.mbIsField = true;
     329             :             }
     330             : 
     331         299 :             break;
     332             :         }
     333             :     }
     334             : 
     335   139856909 :     return aRet;
     336         177 : }
     337             : 
     338             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11