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
45 : SwUndo(SwUndoId::SORT_TXT, &rRg.GetDoc())
46 , SwUndRng(rRg)
47 , m_nTableNode(0)
48{
49 m_pSortOptions.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 m_nTableNode = rTableNd.GetIndex();
59
60 m_pSortOptions.reset( new SwSortOptions(rOpt) );
61 if( bSaveTable )
62 m_pUndoAttrTable.reset( new SwUndoAttrTable( rTableNd ) );
63}
64
66{
67 m_pSortOptions.reset();
68 m_pUndoAttrTable.reset();
69}
70
72{
73 SwDoc & rDoc = rContext.GetDoc();
74 if(m_pSortOptions->bTable)
75 {
76 // Undo for Table
78
80 {
81 m_pUndoAttrTable->UndoImpl(rContext);
82 }
83
84 SwTableNode* pTableNd = rDoc.GetNodes()[ m_nTableNode ]->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 SwNodeOffset 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 == sal_Int32(m_nSttNode + SwNodeOffset(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 + SwNodeOffset(i) );
142 SwNodeRange aRg( aIdxList[i], SwNodeOffset(0), aIdxList[i], SwNodeOffset(1) );
145 }
146 // delete indices
147 aIdxList.clear();
148 SetPaM(rPam, true);
149 }
150}
151
153{
154 SwDoc & rDoc = rContext.GetDoc();
155
156 if(m_pSortOptions->bTable)
157 {
158 // Redo for Table
160
161 SwTableNode* pTableNd = rDoc.GetNodes()[ m_nTableNode ]->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( m_pUndoAttrTable )
186 {
187 m_pUndoAttrTable->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 SwNodeOffset 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 + SwNodeOffset(i));
215 SwNodeRange aRg( aIdxList[i], SwNodeOffset(0), aIdxList[i], SwNodeOffset(1) );
218 }
219 // delete indices
220 aIdxList.clear();
221 SetPaM(rPam, true);
222 SwTextNode const*const pTNd = rPam.GetPointNode().GetTextNode();
223 if( pTNd )
224 {
225 rPam.GetPoint()->nContent = pTNd->GetText().getLength();
226 }
227 }
228}
229
231{
232 // table not repeat capable
233 if(!m_pSortOptions->bTable)
234 {
235 SwPaM *const pPam = & rContext.GetRepeatPaM();
236 SwDoc& rDoc = pPam->GetDoc();
237
238 if( !SwDoc::IsInTable( pPam->Start()->GetNode() ) )
239 rDoc.SortText(*pPam, *m_pSortOptions);
240 }
241}
242
243void SwUndoSort::Insert( const OUString& rOrgPos, const OUString& rNewPos)
244{
245 m_SortList.push_back(std::make_unique< SwSortUndoElement>(rOrgPos, rNewPos));
246}
247
249{
250 m_SortList.push_back(std::make_unique<SwSortUndoElement>(nOrgPos, nNewPos));
251}
252
253/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool MoveNodeRange(SwNodeRange &, SwNode &, SwMoveFlags)=0
Definition: doc.hxx:192
static SwTableNode * IsInTable(const SwNode &)
Definition: ndtbl.cxx:221
bool SortText(const SwPaM &, const SwSortOptions &)
Sort Text in the Document.
Definition: docsort.cxx:283
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:316
SwNodes & GetNodes()
Definition: doc.hxx:413
sal_uInt16 GetPos(const SwTableBox *pTableBox) const
Definition: docsort.cxx:930
void push_back(const SwTableBox *&rpTableBox)
Definition: docsort.hxx:45
void RestoreUpperFrames(SwNodes &rNds, SwNodeOffset nStt, SwNodeOffset nEnd)
Definition: node2lay.cxx:489
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNode & GetNode() const
Definition: ndindex.hxx:136
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:877
SwNodeOffset GetIndex() const
Definition: node.hxx:296
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:187
SwNode & GetPointNode() const
Definition: pam.hxx:283
SwDoc & GetDoc() const
Definition: pam.hxx:299
const SwPosition * GetPoint() const
Definition: pam.hxx:261
const SwPosition * Start() const
Definition: pam.hxx:266
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:419
const SwTable & GetTable() const
Definition: node.hxx:522
void DelFrames(SwRootFrame const *pLayout=nullptr)
Method deletes all views of document for the node.
Definition: ndtbl.cxx:2428
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:113
const SwTableBox * GetTableBox(const OUString &rName, const bool bPerformValidCheck=false) const
Definition: swtable.cxx:1340
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:89
const OUString & GetText() const
Definition: ndtxt.hxx:227
SwPaM & AddUndoRedoPaM(::sw::UndoRedoContext &, bool const bCorrToContent=false) const
Definition: undobj.cxx:100
void SetPaM(SwPaM &, bool bCorrToContent=false) const
Definition: undobj.cxx:80
SwNodeOffset m_nSttNode
Definition: undobj.hxx:231
SwNodeOffset m_nEndNode
Definition: undobj.hxx:231
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unsort.cxx:230
virtual ~SwUndoSort() override
Definition: unsort.cxx:65
std::unique_ptr< SwSortOptions > m_pSortOptions
Definition: UndoSort.hxx:65
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:152
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:71
SwNodeOffset m_nTableNode
Definition: UndoSort.hxx:68
SwUndoSort(const SwPaM &, const SwSortOptions &)
Definition: unsort.cxx:44
void Insert(const OUString &rOrgPos, const OUString &rNewPos)
Definition: unsort.cxx:243
std::vector< std::unique_ptr< SwSortUndoElement > > m_SortList
Definition: UndoSort.hxx:66
std::unique_ptr< SwUndoAttrTable > m_pUndoAttrTable
Definition: UndoSort.hxx:67
static void RemoveIdxFromRange(SwPaM &rPam, bool bMoveNext)
Definition: undobj.cxx:118
static void RemoveIdxFromSection(SwDoc &, SwNodeOffset nSttIdx, const SwNodeOffset *pEndIdx=nullptr)
Definition: undobj.cxx:108
SwPaM & GetRepeatPaM()
Definition: UndoCore.hxx:134
SwDoc & GetDoc() const
Definition: UndoCore.hxx:95
void MoveCell(SwDoc *pDoc, const SwTableBox *pSource, const SwTableBox *pTar, bool bMovedBefore, SwUndoSort *pUD)
Move a single Cell.
Definition: docsort.cxx:677
int i
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
SwNode & GetNode() const
Definition: pam.hxx:80
SwContentIndex nContent
Definition: pam.hxx:39
union SwSortUndoElement::@9 SORT_TXT_TBL
SwUndoId
Definition: swundo.hxx:30