LCOV - code coverage report
Current view: top level - sw/source/core/txtnode - modeltoviewhelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 139 140 99.3 %
Date: 2014-04-11 Functions: 12 12 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        6654 : struct FieldResult
      37             : {
      38             :     sal_Int32 m_nFieldPos;
      39             :     OUString m_sExpand;
      40             :     enum { NONE, FIELD, FOOTNOTE } m_eType;
      41        2218 :     explicit FieldResult(sal_Int32 const nPos)
      42        2218 :         : m_nFieldPos(nPos), m_eType(NONE)
      43        2218 :     { }
      44             : };
      45             : 
      46             : class sortfieldresults :
      47             :     public std::binary_function<const FieldResult&, const FieldResult&, bool>
      48             : {
      49             : public:
      50        6773 :     bool operator()(const FieldResult &rOne, const FieldResult &rTwo) const
      51             :     {
      52        6773 :         return rOne.m_nFieldPos < rTwo.m_nFieldPos;
      53             :     }
      54             : };
      55             : 
      56             : typedef std::set<FieldResult, sortfieldresults> FieldResultSet;
      57             : 
      58       10097 : struct block
      59             : {
      60             :     sal_Int32 m_nStart;
      61             :     sal_Int32 m_nLen;
      62             :     bool m_bVisible;
      63             :     FieldResultSet m_aAttrs;
      64        3347 :     block(sal_Int32 nStart, sal_Int32 nLen, bool bVisible)
      65        3347 :         : m_nStart(nStart), m_nLen(nLen), m_bVisible(bVisible)
      66             :     {
      67        3347 :     }
      68             : };
      69             : 
      70             : struct containsPos
      71             : {
      72             :     const sal_Int32 m_nPos;
      73        2218 :     containsPos(const sal_Int32 nPos)
      74        2218 :         : m_nPos(nPos)
      75             :     {
      76        2218 :     }
      77        2234 :     bool operator() (const block& rIn) const
      78             :     {
      79        2234 :         return m_nPos >= rIn.m_nStart && m_nPos < rIn.m_nStart + rIn.m_nLen;
      80             :     }
      81             : };
      82             : 
      83        3366 : ModelToViewHelper::ModelToViewHelper(const SwTxtNode &rNode, sal_uInt16 eMode)
      84             : {
      85        3366 :     const OUString& rNodeText = rNode.GetTxt();
      86        3366 :     m_aRetText = rNodeText;
      87             : 
      88        3366 :     if (eMode == PASSTHROUGH)
      89        3367 :         return;
      90             : 
      91        3365 :     Range aRange( 0, rNodeText.isEmpty() ? 0 : rNodeText.getLength() - 1);
      92        3365 :     MultiSelection aHiddenMulti(aRange);
      93             : 
      94        3365 :     if (eMode & HIDEINVISIBLE)
      95         879 :         SwScriptInfo::selectHiddenTextProperty(rNode, aHiddenMulti);
      96             : 
      97        3365 :     if (eMode & HIDEREDLINED)
      98         879 :         SwScriptInfo::selectRedLineDeleted(rNode, aHiddenMulti);
      99             : 
     100        6730 :     std::vector<block> aBlocks;
     101             : 
     102        3365 :     sal_Int32 nShownStart = 0;
     103        3375 :     for (size_t i = 0; i < aHiddenMulti.GetRangeCount(); ++i)
     104             :     {
     105          10 :         const Range& rRange = aHiddenMulti.GetRange(i);
     106          10 :         const sal_Int32 nHiddenStart = rRange.Min();
     107          10 :         const sal_Int32 nHiddenEnd = rRange.Max() + 1;
     108          10 :         const sal_Int32 nHiddenLen = nHiddenEnd - nHiddenStart;
     109             : 
     110          10 :         const sal_Int32 nShownEnd = nHiddenStart;
     111          10 :         const sal_Int32 nShownLen = nShownEnd - nShownStart;
     112             : 
     113          10 :         if (nShownLen)
     114          10 :             aBlocks.push_back(block(nShownStart, nShownLen, true));
     115             : 
     116          10 :         if (nHiddenLen)
     117          10 :             aBlocks.push_back(block(nHiddenStart, nHiddenLen, false));
     118             : 
     119          10 :         nShownStart = nHiddenEnd;
     120             :     }
     121             : 
     122        3365 :     sal_Int32 nTrailingShownLen = rNodeText.getLength() - nShownStart;
     123        3365 :     if (nTrailingShownLen)
     124        3327 :         aBlocks.push_back(block(nShownStart, nTrailingShownLen, true));
     125             : 
     126        3365 :     if (eMode & EXPANDFIELDS || eMode & EXPANDFOOTNOTE)
     127             :     {
     128             :         //first the normal fields, get their position in the node and what the text they expand
     129             :         //to is
     130        3362 :         const SwpHints* pSwpHints2 = rNode.GetpSwpHints();
     131        7287 :         for ( sal_uInt16 i = 0; pSwpHints2 && i < pSwpHints2->Count(); ++i )
     132             :         {
     133        3925 :             const SwTxtAttr* pAttr = (*pSwpHints2)[i];
     134        3925 :             if (pAttr->HasDummyChar())
     135             :             {
     136        2220 :                 const sal_Int32 nDummyCharPos = *pAttr->GetStart();
     137        2220 :                 if (aHiddenMulti.IsSelected(nDummyCharPos))
     138           4 :                     continue;
     139             :                 std::vector<block>::iterator aFind = std::find_if(aBlocks.begin(),
     140        2216 :                     aBlocks.end(), containsPos(nDummyCharPos));
     141        2216 :                 if (aFind != aBlocks.end())
     142             :                 {
     143        2216 :                     FieldResult aFieldResult(nDummyCharPos);
     144        2216 :                     switch (pAttr->Which())
     145             :                     {
     146             :                         case RES_TXTATR_FIELD:
     147             :                         case RES_TXTATR_ANNOTATION:
     148         102 :                             if (eMode & EXPANDFIELDS)
     149             :                             {
     150         306 :                                 aFieldResult.m_sExpand = (eMode & REPLACEMODE)
     151             :                                     ? OUString(CHAR_ZWSP)
     152             :                                     : static_cast<SwTxtFld const*>(pAttr)->
     153         204 :                                       GetFmtFld().GetField()->ExpandField(true);
     154         102 :                                 aFieldResult.m_eType = FieldResult::FIELD;
     155             :                             }
     156         102 :                             break;
     157             :                         case RES_TXTATR_FTN:
     158          80 :                             if (eMode & EXPANDFOOTNOTE)
     159             :                             {
     160          76 :                                 const SwFmtFtn& rFtn = static_cast<SwTxtFtn const*>(pAttr)->GetFtn();
     161          76 :                                 const SwDoc *pDoc = rNode.GetDoc();
     162         152 :                                 aFieldResult.m_sExpand = (eMode & REPLACEMODE)
     163             :                                     ? OUString(CHAR_ZWSP)
     164          76 :                                     : rFtn.GetViewNumStr(*pDoc);
     165          76 :                                 aFieldResult.m_eType = FieldResult::FOOTNOTE;
     166             :                             }
     167          80 :                             break;
     168             :                         default:
     169        2034 :                             break;
     170             :                     }
     171        2216 :                     aFind->m_aAttrs.insert(aFieldResult);
     172             :                 }
     173             :             }
     174             :         }
     175             : 
     176        3362 :         if (eMode & EXPANDFIELDS)
     177             :         {
     178             :             //now get the dropdown formfields, get their position in the node and what the text they expand
     179             :             //to is
     180        3362 :             SwPaM aPaM(rNode, 0, rNode, rNode.Len());
     181             :             std::vector<sw::mark::IFieldmark*> aDropDowns =
     182        6724 :                 rNode.GetDoc()->getIDocumentMarkAccess()->getDropDownsFor(aPaM);
     183             : 
     184        3364 :             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           4 :                     aFieldResult.m_sExpand = (eMode & REPLACEMODE)
     197             :                         ? OUString(CHAR_ZWSP)
     198           2 :                         : sw::mark::ExpandFieldmark(pMark);
     199           2 :                     aFieldResult.m_eType = FieldResult::FIELD;
     200           2 :                     aFind->m_aAttrs.insert(aFieldResult);
     201             :                 }
     202        3362 :             }
     203             :         }
     204             :     }
     205             : 
     206        3365 :     sal_Int32 nOffset = 0;
     207        6712 :     for (std::vector<block>::iterator i = aBlocks.begin(); i != aBlocks.end(); ++i)
     208             :     {
     209        3347 :         if (!i->m_bVisible)
     210             :         {
     211          10 :             const sal_Int32 nHiddenStart = i->m_nStart;
     212          10 :             const sal_Int32 nHiddenLen = i->m_nLen;
     213             : 
     214          10 :             m_aRetText = m_aRetText.replaceAt( nOffset + nHiddenStart, nHiddenLen, OUString() );
     215          10 :             m_aMap.push_back( ConversionMapEntry( nHiddenStart, nOffset + nHiddenStart ) );
     216          10 :             nOffset -= nHiddenLen;
     217             :         }
     218             :         else
     219             :         {
     220        5555 :             for (FieldResultSet::iterator j = i->m_aAttrs.begin(); j != i->m_aAttrs.end(); ++j)
     221             :             {
     222        2218 :                 sal_Int32 const viewPos(nOffset + j->m_nFieldPos);
     223        2218 :                 m_aRetText = m_aRetText.replaceAt(viewPos, 1, j->m_sExpand);
     224        2218 :                 m_aMap.push_back( ConversionMapEntry(j->m_nFieldPos, viewPos) );
     225        2218 :                 switch (j->m_eType)
     226             :                 {
     227             :                     case FieldResult::FIELD:
     228         104 :                         m_FieldPositions.push_back(viewPos);
     229         104 :                     break;
     230             :                     case FieldResult::FOOTNOTE:
     231          76 :                         m_FootnotePositions.push_back(viewPos);
     232          76 :                     break;
     233             :                     case FieldResult::NONE: /*ignore*/
     234        2038 :                     break;
     235             :                 }
     236        2218 :                 nOffset += j->m_sExpand.getLength() - 1;
     237             :             }
     238             :         }
     239             :     }
     240             : 
     241        3365 :     if ( !m_aMap.empty() )
     242        4126 :         m_aMap.push_back( ConversionMapEntry( rNodeText.getLength()+1, m_aRetText.getLength()+1 ) );
     243             : }
     244             : 
     245             : /** Converts a model position into a view position
     246             : */
     247        6084 : sal_Int32 ModelToViewHelper::ConvertToViewPosition( sal_Int32 nModelPos ) const
     248             : {
     249             :     // Search for entry after nPos:
     250        6084 :     ConversionMap::const_iterator aIter;
     251        7937 :     for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
     252             :     {
     253        2729 :         if ( (*aIter).first >= nModelPos )
     254             :         {
     255         876 :             const sal_Int32 nPosModel  = (*aIter).first;
     256         876 :             const sal_Int32 nPosExpand = (*aIter).second;
     257             : 
     258         876 :             const sal_Int32 nDistToNextModel  = nPosModel - nModelPos;
     259         876 :             return nPosExpand - nDistToNextModel;
     260             :         }
     261             :     }
     262             : 
     263        5208 :     return nModelPos;
     264             : }
     265             : 
     266             : /** Converts a view position into a model position
     267             : */
     268       20925 : ModelToViewHelper::ModelPosition ModelToViewHelper::ConvertToModelPosition( sal_Int32 nViewPos ) const
     269             : {
     270       20925 :     ModelPosition aRet;
     271       20925 :     aRet.mnPos = nViewPos;
     272             : 
     273             :     // Search for entry after nPos:
     274       20925 :     ConversionMap::const_iterator aIter;
     275       21015 :     for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
     276             :     {
     277         235 :         if ( (*aIter).second > nViewPos )
     278             :         {
     279         145 :             const sal_Int32 nPosModel  = (*aIter).first;
     280         145 :             const sal_Int32 nPosExpand = (*aIter).second;
     281             : 
     282             :             // If nViewPos is in front of first field, we are finished.
     283         145 :             if ( aIter == m_aMap.begin() )
     284          94 :                 break;
     285             : 
     286          51 :             --aIter;
     287             : 
     288             :             // nPrevPosModel is the field position
     289          51 :             const sal_Int32 nPrevPosModel  = (*aIter).first;
     290          51 :             const sal_Int32 nPrevPosExpand = (*aIter).second;
     291             : 
     292          51 :             const sal_Int32 nLengthModel  = nPosModel - nPrevPosModel;
     293          51 :             const sal_Int32 nLengthExpand = nPosExpand - nPrevPosExpand;
     294             : 
     295          51 :             const sal_Int32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
     296          51 :             const sal_Int32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
     297             : 
     298             :             // Check if nPos is outside of field:
     299          51 :             if ( nFieldEndExpand <= nViewPos )
     300             :             {
     301             :                 // nPos is outside of field:
     302          20 :                 const sal_Int32 nDistToField = nViewPos - nFieldEndExpand + 1;
     303          20 :                 aRet.mnPos = nPrevPosModel + nDistToField;
     304             :             }
     305             :             else
     306             :             {
     307             :                 // nViewPos is inside a field:
     308          31 :                 aRet.mnPos = nPrevPosModel;
     309          31 :                 aRet.mnSubPos = nViewPos - nPrevPosExpand;
     310          31 :                 aRet.mbIsField = true;
     311             :             }
     312             : 
     313          51 :             break;
     314             :         }
     315             :     }
     316             : 
     317       20925 :     return aRet;
     318             : }
     319             : 
     320             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10