LCOV - code coverage report
Current view: top level - sw/source/core/undo - unsort.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 24 116 20.7 %
Date: 2015-06-13 12:38:46 Functions: 7 12 58.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 <UndoSort.hxx>
      21             : #include <doc.hxx>
      22             : #include <swundo.hxx>
      23             : #include <pam.hxx>
      24             : #include <swtable.hxx>
      25             : #include <ndtxt.hxx>
      26             : #include <UndoCore.hxx>
      27             : #include <UndoTable.hxx>
      28             : #include <sortopt.hxx>
      29             : #include <docsort.hxx>
      30             : #include <redline.hxx>
      31             : #include <node2lay.hxx>
      32             : 
      33             : // Undo for Sorting
      34          38 : SwSortUndoElement::~SwSortUndoElement()
      35             : {
      36             :     // are there string pointers saved?
      37          38 :     if( 0xffffffff != SORT_TXT_TBL.TXT.nKenn ) // Kenn(ung) = identifier
      38             :     {
      39          38 :         delete SORT_TXT_TBL.TBL.pSource;
      40          38 :         delete SORT_TXT_TBL.TBL.pTarget;
      41             :     }
      42          38 : }
      43             : 
      44           0 : SwUndoSort::SwUndoSort(const SwPaM& rRg, const SwSortOptions& rOpt)
      45             :     : SwUndo(UNDO_SORT_TXT)
      46             :     , SwUndRng(rRg)
      47             :     , pUndoTableAttr(0)
      48             :     , pRedlData(0)
      49           0 :     , nTableNd(0)
      50             : {
      51           0 :     pSortOpt = new SwSortOptions(rOpt);
      52           0 : }
      53             : 
      54           4 : SwUndoSort::SwUndoSort( sal_uLong nStt, sal_uLong nEnd, const SwTableNode& rTableNd,
      55             :                         const SwSortOptions& rOpt, bool bSaveTable )
      56           4 :     : SwUndo(UNDO_SORT_TBL), pUndoTableAttr( 0 ), pRedlData( 0 )
      57             : {
      58           4 :     nSttNode = nStt;
      59           4 :     nEndNode = nEnd;
      60           4 :     nTableNd   = rTableNd.GetIndex();
      61             : 
      62           4 :     pSortOpt = new SwSortOptions(rOpt);
      63           4 :     if( bSaveTable )
      64           4 :         pUndoTableAttr = new SwUndoAttrTable( rTableNd );
      65           4 : }
      66             : 
      67          12 : SwUndoSort::~SwUndoSort()
      68             : {
      69           4 :     delete pSortOpt;
      70           4 :     delete pUndoTableAttr;
      71           4 :     delete pRedlData;
      72           8 : }
      73             : 
      74           0 : void SwUndoSort::UndoImpl(::sw::UndoRedoContext & rContext)
      75             : {
      76           0 :     SwDoc & rDoc = rContext.GetDoc();
      77           0 :     if(pSortOpt->bTable)
      78             :     {
      79             :         // Undo for Table
      80           0 :         RemoveIdxFromSection( rDoc, nSttNode, &nEndNode );
      81             : 
      82           0 :         if( pUndoTableAttr )
      83             :         {
      84           0 :             pUndoTableAttr->UndoImpl(rContext);
      85             :         }
      86             : 
      87           0 :         SwTableNode* pTableNd = rDoc.GetNodes()[ nTableNd ]->GetTableNode();
      88             : 
      89             :         // #i37739# A simple 'MakeFrms' after the node sorting
      90             :         // does not work if the table is inside a frame and has no prev/next.
      91           0 :         SwNode2Layout aNode2Layout( *pTableNd );
      92             : 
      93           0 :         pTableNd->DelFrms();
      94           0 :         const SwTable& rTable = pTableNd->GetTable();
      95             : 
      96           0 :         SwMovedBoxes aMovedList;
      97           0 :         for( size_t i=0; i < aSortList.size(); i++)
      98             :         {
      99             :             const SwTableBox* pSource = rTable.GetTableBox(
     100           0 :                     *aSortList[i].SORT_TXT_TBL.TBL.pSource );
     101             :             const SwTableBox* pTarget = rTable.GetTableBox(
     102           0 :                     *aSortList[i].SORT_TXT_TBL.TBL.pTarget );
     103             : 
     104             :             // move back
     105             :             MoveCell(&rDoc, pTarget, pSource,
     106           0 :                      USHRT_MAX != aMovedList.GetPos(pSource) );
     107             : 
     108             :             // store moved entry in list
     109           0 :             aMovedList.push_back(pTarget);
     110             :         }
     111             : 
     112             :         // Restore table frames:
     113             :         // #i37739# A simple 'MakeFrms' after the node sorting
     114             :         // does not work if the table is inside a frame and has no prev/next.
     115           0 :         const sal_uLong nIdx = pTableNd->GetIndex();
     116           0 :         aNode2Layout.RestoreUpperFrms( rDoc.GetNodes(), nIdx, nIdx + 1 );
     117             :     }
     118             :     else
     119             :     {
     120             :         // Undo for Text
     121           0 :         SwPaM & rPam( AddUndoRedoPaM(rContext) );
     122           0 :         RemoveIdxFromRange(rPam, true);
     123             : 
     124             :         // create index for (sorted) positions
     125             :         // The IndexList must be created based on (asc.) sorted SourcePosition.
     126           0 :         SwUndoSortList aIdxList;
     127             : 
     128           0 :         for( size_t i = 0; i < aSortList.size(); ++i)
     129           0 :             for( size_t ii=0; ii < aSortList.size(); ++ii )
     130           0 :                 if( aSortList[ii].SORT_TXT_TBL.TXT.nSource == nSttNode + i )
     131             :                 {
     132             :                     SwNodeIndex* pIdx = new SwNodeIndex( rDoc.GetNodes(),
     133           0 :                         aSortList[ii].SORT_TXT_TBL.TXT.nTarget );
     134           0 :                     aIdxList.insert( aIdxList.begin() + i, pIdx );
     135           0 :                     break;
     136             :                 }
     137             : 
     138           0 :         for(size_t i=0; i < aSortList.size(); ++i)
     139             :         {
     140           0 :             SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i );
     141           0 :             SwNodeRange aRg( *aIdxList[i], 0, *aIdxList[i], 1 );
     142           0 :             rDoc.getIDocumentContentOperations().MoveNodeRange(aRg, aIdx,
     143           0 :                 SwMoveFlags::DEFAULT);
     144           0 :         }
     145             :         // delete indices
     146           0 :         for(SwUndoSortList::const_iterator it = aIdxList.begin(); it != aIdxList.end(); ++it)
     147           0 :             delete *it;
     148           0 :         aIdxList.clear();
     149           0 :         SetPaM(rPam, true);
     150             :     }
     151           0 : }
     152             : 
     153           0 : void SwUndoSort::RedoImpl(::sw::UndoRedoContext & rContext)
     154             : {
     155           0 :     SwDoc & rDoc = rContext.GetDoc();
     156             : 
     157           0 :     if(pSortOpt->bTable)
     158             :     {
     159             :         // Redo for Table
     160           0 :         RemoveIdxFromSection( rDoc, nSttNode, &nEndNode );
     161             : 
     162           0 :         SwTableNode* pTableNd = rDoc.GetNodes()[ nTableNd ]->GetTableNode();
     163             : 
     164             :         // #i37739# A simple 'MakeFrms' after the node sorting
     165             :         // does not work if the table is inside a frame and has no prev/next.
     166           0 :         SwNode2Layout aNode2Layout( *pTableNd );
     167             : 
     168           0 :         pTableNd->DelFrms();
     169           0 :         const SwTable& rTable = pTableNd->GetTable();
     170             : 
     171           0 :         SwMovedBoxes aMovedList;
     172           0 :         for(size_t i=0; i < aSortList.size(); ++i)
     173             :         {
     174             :             const SwTableBox* pSource = rTable.GetTableBox(
     175           0 :                     *aSortList[i].SORT_TXT_TBL.TBL.pSource );
     176             :             const SwTableBox* pTarget = rTable.GetTableBox(
     177           0 :                     *aSortList[i].SORT_TXT_TBL.TBL.pTarget );
     178             : 
     179             :             // move back
     180             :             MoveCell(&rDoc, pSource, pTarget,
     181           0 :                      USHRT_MAX != aMovedList.GetPos( pTarget ) );
     182             :             // store moved entry in list
     183           0 :             aMovedList.push_back( pSource );
     184             :         }
     185             : 
     186           0 :         if( pUndoTableAttr )
     187             :         {
     188           0 :             pUndoTableAttr->RedoImpl(rContext);
     189             :         }
     190             : 
     191             :         // Restore table frames:
     192             :         // #i37739# A simple 'MakeFrms' after the node sorting
     193             :         // does not work if the table is inside a frame and has no prev/next.
     194           0 :         const sal_uLong nIdx = pTableNd->GetIndex();
     195           0 :         aNode2Layout.RestoreUpperFrms( rDoc.GetNodes(), nIdx, nIdx + 1 );
     196             :     }
     197             :     else
     198             :     {
     199             :         // Redo for Text
     200           0 :         SwPaM & rPam( AddUndoRedoPaM(rContext) );
     201           0 :         SetPaM(rPam);
     202           0 :         RemoveIdxFromRange(rPam, true);
     203             : 
     204           0 :         SwUndoSortList aIdxList;
     205             : 
     206           0 :         for( size_t i = 0; i < aSortList.size(); ++i)
     207             :         {   // current position is starting point
     208             :             SwNodeIndex* pIdx = new SwNodeIndex( rDoc.GetNodes(),
     209           0 :                     aSortList[i].SORT_TXT_TBL.TXT.nSource);
     210           0 :             aIdxList.insert( aIdxList.begin() + i, pIdx );
     211             :         }
     212             : 
     213           0 :         for( size_t i = 0; i < aSortList.size(); ++i)
     214             :         {
     215           0 :             SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i);
     216           0 :             SwNodeRange aRg( *aIdxList[i], 0, *aIdxList[i], 1 );
     217           0 :             rDoc.getIDocumentContentOperations().MoveNodeRange(aRg, aIdx,
     218           0 :                 SwMoveFlags::DEFAULT);
     219           0 :         }
     220             :         // delete indices
     221           0 :         for(SwUndoSortList::const_iterator it = aIdxList.begin(); it != aIdxList.end(); ++it)
     222           0 :             delete *it;
     223           0 :         aIdxList.clear();
     224           0 :         SetPaM(rPam, true);
     225           0 :         SwTextNode const*const pTNd = rPam.GetNode().GetTextNode();
     226           0 :         if( pTNd )
     227             :         {
     228           0 :             rPam.GetPoint()->nContent = pTNd->GetText().getLength();
     229           0 :         }
     230             :     }
     231           0 : }
     232             : 
     233           0 : void SwUndoSort::RepeatImpl(::sw::RepeatContext & rContext)
     234             : {
     235             :     // table not repeat capable
     236           0 :     if(!pSortOpt->bTable)
     237             :     {
     238           0 :         SwPaM *const pPam = & rContext.GetRepeatPaM();
     239           0 :         SwDoc& rDoc = *pPam->GetDoc();
     240             : 
     241           0 :         if( !rDoc.IsIdxInTable( pPam->Start()->nNode ) )
     242           0 :             rDoc.SortText(*pPam, *pSortOpt);
     243             :     }
     244           0 : }
     245             : 
     246          38 : void SwUndoSort::Insert( const OUString& rOrgPos, const OUString& rNewPos)
     247             : {
     248          38 :     SwSortUndoElement* pEle = new SwSortUndoElement(rOrgPos, rNewPos);
     249          38 :     aSortList.push_back( pEle );
     250          38 : }
     251             : 
     252           0 : void SwUndoSort::Insert( sal_uLong nOrgPos, sal_uLong nNewPos)
     253             : {
     254           0 :     SwSortUndoElement* pEle = new SwSortUndoElement(nOrgPos, nNewPos);
     255           0 :     aSortList.push_back( pEle );
     256         177 : }
     257             : 
     258             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11