LibreOffice Module sw (master)  1
unsort.cxx
Go to the documentation of this file.
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 <memory>
21 #include <UndoSort.hxx>
22 #include <doc.hxx>
23 #include <swundo.hxx>
24 #include <pam.hxx>
25 #include <swtable.hxx>
26 #include <ndtxt.hxx>
27 #include <UndoCore.hxx>
28 #include <UndoTable.hxx>
29 #include <sortopt.hxx>
30 #include <docsort.hxx>
31 #include <redline.hxx>
32 #include <node2lay.hxx>
33 
34 // Undo for Sorting
36 {
37  // are there string pointers saved?
38  if( 0xffffffff != SORT_TXT_TBL.TXT.nID )
39  {
40  delete SORT_TXT_TBL.TBL.pSource;
41  delete SORT_TXT_TBL.TBL.pTarget;
42  }
43 }
44 
45 SwUndoSort::SwUndoSort(const SwPaM& rRg, const SwSortOptions& rOpt)
46  : SwUndo(SwUndoId::SORT_TXT, rRg.GetDoc())
47  , SwUndRng(rRg)
48  , nTableNd(0)
49 {
50  pSortOpt.reset( new SwSortOptions(rOpt) );
51 }
52 
54  const SwSortOptions& rOpt, bool bSaveTable )
55  : SwUndo(SwUndoId::SORT_TBL, rTableNd.GetDoc())
56 {
57  nSttNode = nStt;
58  nEndNode = nEnd;
59  nTableNd = rTableNd.GetIndex();
60 
61  pSortOpt.reset( new SwSortOptions(rOpt) );
62  if( bSaveTable )
63  pUndoTableAttr.reset( new SwUndoAttrTable( rTableNd ) );
64 }
65 
67 {
68  pSortOpt.reset();
69  pUndoTableAttr.reset();
70 }
71 
73 {
74  SwDoc & rDoc = rContext.GetDoc();
75  if(pSortOpt->bTable)
76  {
77  // Undo for Table
79 
80  if( pUndoTableAttr )
81  {
82  pUndoTableAttr->UndoImpl(rContext);
83  }
84 
85  SwTableNode* pTableNd = rDoc.GetNodes()[ nTableNd ]->GetTableNode();
86 
87  // #i37739# A simple 'MakeFrames' after the node sorting
88  // does not work if the table is inside a frame and has no prev/next.
89  SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
90 
91  pTableNd->DelFrames();
92  const SwTable& rTable = pTableNd->GetTable();
93 
94  SwMovedBoxes aMovedList;
95  for (std::unique_ptr<SwSortUndoElement> & i : m_SortList)
96  {
97  const SwTableBox* pSource = rTable.GetTableBox(
98  *i->SORT_TXT_TBL.TBL.pSource );
99  const SwTableBox* pTarget = rTable.GetTableBox(
100  *i->SORT_TXT_TBL.TBL.pTarget );
101 
102  // move back
103  MoveCell(&rDoc, pTarget, pSource,
104  USHRT_MAX != aMovedList.GetPos(pSource) );
105 
106  // store moved entry in list
107  aMovedList.push_back(pTarget);
108  }
109 
110  // Restore table frames:
111  // #i37739# A simple 'MakeFrames' after the node sorting
112  // does not work if the table is inside a frame and has no prev/next.
113  const sal_uLong nIdx = pTableNd->GetIndex();
114  aNode2Layout.RestoreUpperFrames( rDoc.GetNodes(), nIdx, nIdx + 1 );
115  }
116  else
117  {
118  // Undo for Text
119  SwPaM & rPam( AddUndoRedoPaM(rContext) );
120  RemoveIdxFromRange(rPam, true);
121 
122  // create index for (sorted) positions
123  // The IndexList must be created based on (asc.) sorted SourcePosition.
124  std::vector<SwNodeIndex> aIdxList;
125  aIdxList.reserve(m_SortList.size());
126 
127  for (size_t i = 0; i < m_SortList.size(); ++i)
128  {
129  for (std::unique_ptr<SwSortUndoElement> & j : m_SortList)
130  {
131  if (j->SORT_TXT_TBL.TXT.nSource == nSttNode + i)
132  {
133  aIdxList.push_back( SwNodeIndex( rDoc.GetNodes(),
134  j->SORT_TXT_TBL.TXT.nTarget ) );
135  break;
136  }
137  }
138  }
139 
140  for (size_t i = 0; i < m_SortList.size(); ++i)
141  {
142  SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i );
143  SwNodeRange aRg( aIdxList[i], 0, aIdxList[i], 1 );
146  }
147  // delete indices
148  aIdxList.clear();
149  SetPaM(rPam, true);
150  }
151 }
152 
154 {
155  SwDoc & rDoc = rContext.GetDoc();
156 
157  if(pSortOpt->bTable)
158  {
159  // Redo for Table
161 
162  SwTableNode* pTableNd = rDoc.GetNodes()[ nTableNd ]->GetTableNode();
163 
164  // #i37739# A simple 'MakeFrames' after the node sorting
165  // does not work if the table is inside a frame and has no prev/next.
166  SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
167 
168  pTableNd->DelFrames();
169  const SwTable& rTable = pTableNd->GetTable();
170 
171  SwMovedBoxes aMovedList;
172  for (std::unique_ptr<SwSortUndoElement> & i : m_SortList)
173  {
174  const SwTableBox* pSource = rTable.GetTableBox(
175  *i->SORT_TXT_TBL.TBL.pSource );
176  const SwTableBox* pTarget = rTable.GetTableBox(
177  *i->SORT_TXT_TBL.TBL.pTarget );
178 
179  // move back
180  MoveCell(&rDoc, pSource, pTarget,
181  USHRT_MAX != aMovedList.GetPos( pTarget ) );
182  // store moved entry in list
183  aMovedList.push_back( pSource );
184  }
185 
186  if( pUndoTableAttr )
187  {
188  pUndoTableAttr->RedoImpl(rContext);
189  }
190 
191  // Restore table frames:
192  // #i37739# A simple 'MakeFrames' after the node sorting
193  // does not work if the table is inside a frame and has no prev/next.
194  const sal_uLong nIdx = pTableNd->GetIndex();
195  aNode2Layout.RestoreUpperFrames( rDoc.GetNodes(), nIdx, nIdx + 1 );
196  }
197  else
198  {
199  // Redo for Text
200  SwPaM & rPam( AddUndoRedoPaM(rContext) );
201  SetPaM(rPam);
202  RemoveIdxFromRange(rPam, true);
203 
204  std::vector<SwNodeIndex> aIdxList;
205  aIdxList.reserve(m_SortList.size());
206 
207  for (size_t i = 0; i < m_SortList.size(); ++i)
208  { // current position is starting point
209  aIdxList.push_back( SwNodeIndex( rDoc.GetNodes(),
210  m_SortList[i]->SORT_TXT_TBL.TXT.nSource) );
211  }
212 
213  for (size_t i = 0; i < m_SortList.size(); ++i)
214  {
215  SwNodeIndex aIdx( rDoc.GetNodes(), nSttNode + i);
216  SwNodeRange aRg( aIdxList[i], 0, aIdxList[i], 1 );
219  }
220  // delete indices
221  aIdxList.clear();
222  SetPaM(rPam, true);
223  SwTextNode const*const pTNd = rPam.GetNode().GetTextNode();
224  if( pTNd )
225  {
226  rPam.GetPoint()->nContent = pTNd->GetText().getLength();
227  }
228  }
229 }
230 
232 {
233  // table not repeat capable
234  if(!pSortOpt->bTable)
235  {
236  SwPaM *const pPam = & rContext.GetRepeatPaM();
237  SwDoc& rDoc = *pPam->GetDoc();
238 
239  if( !rDoc.IsIdxInTable( pPam->Start()->nNode ) )
240  rDoc.SortText(*pPam, *pSortOpt);
241  }
242 }
243 
244 void SwUndoSort::Insert( const OUString& rOrgPos, const OUString& rNewPos)
245 {
246  m_SortList.push_back(std::make_unique< SwSortUndoElement>(rOrgPos, rNewPos));
247 }
248 
249 void SwUndoSort::Insert( sal_uLong nOrgPos, sal_uLong nNewPos)
250 {
251  m_SortList.push_back(std::make_unique<SwSortUndoElement>(nOrgPos, nNewPos));
252 }
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwUndoSort(const SwPaM &, const SwSortOptions &)
Definition: unsort.cxx:45
SwPaM & AddUndoRedoPaM(::sw::UndoRedoContext &, bool const bCorrToContent=false) const
Definition: undobj.cxx:106
sal_uLong GetIndex() const
Definition: node.hxx:282
SwNode & GetNode(bool bPoint=true) const
Definition: pam.hxx:223
const SwTableBox * GetTableBox(const OUString &rName, const bool bPerformValidCheck=false) const
Definition: swtable.cxx:1344
void DelFrames(SwRootFrame const *pLayout=nullptr)
Method deletes all views of document for the node.
Definition: ndtbl.cxx:2415
const OUString & GetText() const
Definition: ndtxt.hxx:211
SwUndoId
Definition: swundo.hxx:29
SwNodeIndex nNode
Definition: pam.hxx:37
std::unique_ptr< SwUndoAttrTable > pUndoTableAttr
Definition: UndoSort.hxx:64
void MoveCell(SwDoc *pDoc, const SwTableBox *pSource, const SwTableBox *pTar, bool bMovedBefore, SwUndoSort *pUD)
Move a single Cell.
Definition: docsort.cxx:688
sal_uIntPtr sal_uLong
SwPaM & GetRepeatPaM()
Definition: UndoCore.hxx:128
Definition: doc.hxx:185
virtual ~SwUndoSort() override
Definition: unsort.cxx:66
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unsort.cxx:231
static void RemoveIdxFromSection(SwDoc &, sal_uLong nSttIdx, const sal_uLong *pEndIdx=nullptr)
Definition: undobj.cxx:114
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:347
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:72
sal_uLong nEndNode
Definition: undobj.hxx:216
sal_uInt16 GetPos(const SwTableBox *pTableBox) const
Definition: docsort.cxx:938
std::unique_ptr< SwSortOptions > pSortOpt
Definition: UndoSort.hxx:62
SwIndex nContent
Definition: pam.hxx:38
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:153
const SwTable & GetTable() const
Definition: node.hxx:497
void push_back(const SwTableBox *&rpTableBox)
Definition: docsort.hxx:54
union SwSortUndoElement::@10 SORT_TXT_TBL
SwDoc & GetDoc() const
Definition: UndoCore.hxx:94
void Insert(const OUString &rOrgPos, const OUString &rNewPos)
Definition: unsort.cxx:244
static void RemoveIdxFromRange(SwPaM &rPam, bool bMoveNext)
Definition: undobj.cxx:124
bool SortText(const SwPaM &, const SwSortOptions &)
Sort Text in the Document.
Definition: docsort.cxx:292
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
const SwPosition * GetPoint() const
Definition: pam.hxx:207
std::vector< std::unique_ptr< SwSortUndoElement > > m_SortList
Definition: UndoSort.hxx:63
int i
sal_uLong nTableNd
Definition: UndoSort.hxx:65
SwDoc * GetDoc() const
Definition: pam.hxx:243
sal_uLong nSttNode
Definition: undobj.hxx:216
Marks a node in the document model.
Definition: ndindex.hxx:31
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:110
const SwPosition * Start() const
Definition: pam.hxx:212
void RestoreUpperFrames(SwNodes &rNds, sal_uLong nStt, sal_uLong nEnd)
Definition: node2lay.cxx:439
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
void SetPaM(SwPaM &, bool bCorrToContent=false) const
Definition: undobj.cxx:78
SwTableNode * IsIdxInTable(const SwNodeIndex &rIdx)
Definition: ndtbl.cxx:216
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
SwNodes & GetNodes()
Definition: doc.hxx:403
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:386
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:843