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