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
34 : SwUndo(SwUndoId::SORT_TXT, &rRg.GetDoc())
35 , SwUndRng(rRg)
36 , m_nTableNode(0)
37{
38 m_pSortOptions.reset( new SwSortOptions(rOpt) );
39}
40
42 const SwSortOptions& rOpt, bool bSaveTable )
43 : SwUndo(SwUndoId::SORT_TBL, &rTableNd.GetDoc())
44{
45 m_nSttNode = nStt;
46 m_nEndNode = nEnd;
47 m_nTableNode = rTableNd.GetIndex();
48
49 m_pSortOptions.reset( new SwSortOptions(rOpt) );
50 if( bSaveTable )
51 m_pUndoAttrTable.reset( new SwUndoAttrTable( rTableNd ) );
52}
53
55{
56 m_pSortOptions.reset();
57 m_pUndoAttrTable.reset();
58}
59
61{
62 SwDoc & rDoc = rContext.GetDoc();
63 if(m_pSortOptions->bTable)
64 {
65 // Undo for Table
67
69 {
70 m_pUndoAttrTable->UndoImpl(rContext);
71 }
72
73 SwTableNode* pTableNd = rDoc.GetNodes()[ m_nTableNode ]->GetTableNode();
74
75 // #i37739# A simple 'MakeFrames' after the node sorting
76 // does not work if the table is inside a frame and has no prev/next.
77 SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
78
79 pTableNd->DelFrames();
80 const SwTable& rTable = pTableNd->GetTable();
81
82 SwMovedBoxes aMovedList;
83 for (std::unique_ptr<SwSortUndoElement> const& pElement : m_SortList)
84 {
85 const SwTableBox* pSource = rTable.GetTableBox(pElement->maSourceString);
86 const SwTableBox* pTarget = rTable.GetTableBox(pElement->maTargetString);
87
88 // move back
89 MoveCell(&rDoc, pTarget, pSource,
90 USHRT_MAX != aMovedList.GetPos(pSource) );
91
92 // store moved entry in list
93 aMovedList.push_back(pTarget);
94 }
95
96 // Restore table frames:
97 // #i37739# A simple 'MakeFrames' after the node sorting
98 // does not work if the table is inside a frame and has no prev/next.
99 const SwNodeOffset nIdx = pTableNd->GetIndex();
100 aNode2Layout.RestoreUpperFrames( rDoc.GetNodes(), nIdx, nIdx + 1 );
101 }
102 else
103 {
104 // Undo for Text
105 SwPaM & rPam( AddUndoRedoPaM(rContext) );
106 RemoveIdxFromRange(rPam, true);
107
108 // create index for (sorted) positions
109 // The IndexList must be created based on (asc.) sorted SourcePosition.
110 std::vector<SwNodeIndex> aIdxList;
111 aIdxList.reserve(m_SortList.size());
112
113 for (size_t i = 0; i < m_SortList.size(); ++i)
114 {
115 for (std::unique_ptr<SwSortUndoElement> const& pElement : m_SortList)
116 {
117 if (pElement->mnSourceNodeOffset == (m_nSttNode + SwNodeOffset(i)))
118 {
119 aIdxList.push_back( SwNodeIndex(rDoc.GetNodes(), pElement->mnTargetNodeOffset));
120 break;
121 }
122 }
123 }
124
125 for (size_t i = 0; i < m_SortList.size(); ++i)
126 {
127 SwNodeIndex aIdx( rDoc.GetNodes(), m_nSttNode + SwNodeOffset(i) );
128 SwNodeRange aRg( aIdxList[i], SwNodeOffset(0), aIdxList[i], SwNodeOffset(1) );
131 }
132 // delete indices
133 aIdxList.clear();
134 SetPaM(rPam, true);
135 }
136}
137
139{
140 SwDoc & rDoc = rContext.GetDoc();
141
142 if(m_pSortOptions->bTable)
143 {
144 // Redo for Table
146
147 SwTableNode* pTableNd = rDoc.GetNodes()[ m_nTableNode ]->GetTableNode();
148
149 // #i37739# A simple 'MakeFrames' after the node sorting
150 // does not work if the table is inside a frame and has no prev/next.
151 SwNode2LayoutSaveUpperFrames aNode2Layout(*pTableNd);
152
153 pTableNd->DelFrames();
154 const SwTable& rTable = pTableNd->GetTable();
155
156 SwMovedBoxes aMovedList;
157 for (std::unique_ptr<SwSortUndoElement> const& pElement: m_SortList)
158 {
159 const SwTableBox* pSource = rTable.GetTableBox(pElement->maSourceString);
160 const SwTableBox* pTarget = rTable.GetTableBox(pElement->maTargetString);
161
162 // move back
163 MoveCell(&rDoc, pSource, pTarget,
164 USHRT_MAX != aMovedList.GetPos( pTarget ) );
165 // store moved entry in list
166 aMovedList.push_back( pSource );
167 }
168
169 if( m_pUndoAttrTable )
170 {
171 m_pUndoAttrTable->RedoImpl(rContext);
172 }
173
174 // Restore table frames:
175 // #i37739# A simple 'MakeFrames' after the node sorting
176 // does not work if the table is inside a frame and has no prev/next.
177 const SwNodeOffset nIdx = pTableNd->GetIndex();
178 aNode2Layout.RestoreUpperFrames( rDoc.GetNodes(), nIdx, nIdx + 1 );
179 }
180 else
181 {
182 // Redo for Text
183 SwPaM & rPam( AddUndoRedoPaM(rContext) );
184 SetPaM(rPam);
185 RemoveIdxFromRange(rPam, true);
186
187 std::vector<SwNodeIndex> aIdxList;
188 aIdxList.reserve(m_SortList.size());
189
190 for (size_t i = 0; i < m_SortList.size(); ++i)
191 { // current position is starting point
192 aIdxList.push_back( SwNodeIndex(rDoc.GetNodes(), m_SortList[i]->mnSourceNodeOffset) );
193 }
194
195 for (size_t i = 0; i < m_SortList.size(); ++i)
196 {
197 SwNodeIndex aIdx( rDoc.GetNodes(), m_nSttNode + SwNodeOffset(i));
198 SwNodeRange aRg( aIdxList[i], SwNodeOffset(0), aIdxList[i], SwNodeOffset(1) );
201 }
202 // delete indices
203 aIdxList.clear();
204 SetPaM(rPam, true);
205 SwTextNode const*const pTNd = rPam.GetPointNode().GetTextNode();
206 if( pTNd )
207 {
208 rPam.GetPoint()->SetContent( pTNd->GetText().getLength() );
209 }
210 }
211}
212
214{
215 // table not repeat capable
216 if(!m_pSortOptions->bTable)
217 {
218 SwPaM *const pPam = & rContext.GetRepeatPaM();
219 SwDoc& rDoc = pPam->GetDoc();
220
221 if( !SwDoc::IsInTable( pPam->Start()->GetNode() ) )
222 rDoc.SortText(*pPam, *m_pSortOptions);
223 }
224}
225
226void SwUndoSort::Insert( const OUString& rOrgPos, const OUString& rNewPos)
227{
228 m_SortList.push_back(std::make_unique<SwSortUndoElement>(rOrgPos, rNewPos));
229}
230
232{
233 m_SortList.push_back(std::make_unique<SwSortUndoElement>(nOrgPos, nNewPos));
234}
235
236/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool MoveNodeRange(SwNodeRange &, SwNode &, SwMoveFlags)=0
Definition: doc.hxx:197
static SwTableNode * IsInTable(const SwNode &)
Definition: ndtbl.cxx:219
bool SortText(const SwPaM &, const SwSortOptions &)
Sort Text in the Document.
Definition: docsort.cxx:283
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
SwNodes & GetNodes()
Definition: doc.hxx:422
sal_uInt16 GetPos(const SwTableBox *pTableBox) const
Definition: docsort.cxx:927
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:123
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:901
SwNodeOffset GetIndex() const
Definition: node.hxx:312
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
SwNode & GetPointNode() const
Definition: pam.hxx:275
SwDoc & GetDoc() const
Definition: pam.hxx:291
const SwPosition * GetPoint() const
Definition: pam.hxx:253
const SwPosition * Start() const
Definition: pam.hxx:258
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:443
const SwTable & GetTable() const
Definition: node.hxx:542
void DelFrames(SwRootFrame const *pLayout=nullptr)
Method deletes all views of document for the node.
Definition: ndtbl.cxx:2461
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:1344
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
const OUString & GetText() const
Definition: ndtxt.hxx:244
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:213
virtual ~SwUndoSort() override
Definition: unsort.cxx:54
std::unique_ptr< SwSortOptions > m_pSortOptions
Definition: UndoSort.hxx:55
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:138
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unsort.cxx:60
SwNodeOffset m_nTableNode
Definition: UndoSort.hxx:58
SwUndoSort(const SwPaM &, const SwSortOptions &)
Definition: unsort.cxx:33
void Insert(const OUString &rOrgPos, const OUString &rNewPos)
Definition: unsort.cxx:226
std::vector< std::unique_ptr< SwSortUndoElement > > m_SortList
Definition: UndoSort.hxx:56
std::unique_ptr< SwUndoAttrTable > m_pUndoAttrTable
Definition: UndoSort.hxx:57
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:674
int i
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
SwNode & GetNode() const
Definition: pam.hxx:81
void SetContent(sal_Int32 nContentIndex)
Set content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:267
SwUndoId
Definition: swundo.hxx:30