/src/libreoffice/sc/source/ui/undo/refundo.cxx
Line | Count | Source |
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 <refundo.hxx> |
21 | | #include <document.hxx> |
22 | | #include <dbdata.hxx> |
23 | | #include <rangenam.hxx> |
24 | | #include <detdata.hxx> |
25 | | #include <prnsave.hxx> |
26 | | #include <chartlis.hxx> |
27 | | #include <dpobject.hxx> |
28 | | #include <areasave.hxx> |
29 | | #include <unoreflist.hxx> |
30 | | #include <scopetools.hxx> |
31 | | #include <refupdatecontext.hxx> |
32 | | |
33 | | ScRefUndoData::ScRefUndoData( ScDocument& rDoc, bool bForceDBSnapshot ) : |
34 | 0 | pPrintRanges(rDoc.CreatePrintRangeSaver()) |
35 | 0 | { |
36 | | // An empty database-range collection is normally skipped (nothing to |
37 | | // restore). A caller that may add a range to an empty collection during a |
38 | | // paste passes bForceDBSnapshot so the empty pre-state stays restorable. |
39 | | // DeleteUnchanged() drops it again if the operation changed nothing. |
40 | 0 | const ScDBCollection* pOldDBColl = rDoc.GetDBCollection(); |
41 | 0 | if (pOldDBColl && (bForceDBSnapshot || !pOldDBColl->empty())) |
42 | 0 | pDBCollection.reset(new ScDBCollection(*pOldDBColl)); |
43 | |
|
44 | 0 | const ScRangeName& rOldRanges = rDoc.GetRangeName(); |
45 | 0 | if (!rOldRanges.empty()) |
46 | 0 | pRangeName.reset(new ScRangeName(rOldRanges)); |
47 | | |
48 | | // when handling Pivot solely keep the range? |
49 | |
|
50 | 0 | const ScDPCollection* pOldDP = rDoc.GetDPCollection(); |
51 | 0 | if (pOldDP && pOldDP->GetCount()) |
52 | 0 | pDPCollection.reset(new ScDPCollection(*pOldDP)); |
53 | |
|
54 | 0 | const ScDetOpList* pOldDetOp = rDoc.GetDetOpList(); |
55 | 0 | if (pOldDetOp && pOldDetOp->Count()) |
56 | 0 | pDetOpList.reset(new ScDetOpList(*pOldDetOp)); |
57 | |
|
58 | 0 | const ScChartListenerCollection* pOldChartLisColl = rDoc.GetChartListenerCollection(); |
59 | 0 | if (pOldChartLisColl) |
60 | 0 | pChartListenerCollection.reset(new ScChartListenerCollection(*pOldChartLisColl)); |
61 | |
|
62 | 0 | pAreaLinks = ScAreaLinkSaveCollection::CreateFromDoc(rDoc); // returns NULL if empty |
63 | |
|
64 | 0 | rDoc.BeginUnoRefUndo(); |
65 | 0 | } |
66 | | |
67 | | ScRefUndoData::~ScRefUndoData() |
68 | 0 | { |
69 | 0 | pDBCollection.reset(); |
70 | 0 | pRangeName.reset(); |
71 | 0 | pPrintRanges.reset(); |
72 | 0 | pDPCollection.reset(); |
73 | 0 | pDetOpList.reset(); |
74 | 0 | pChartListenerCollection.reset(); |
75 | 0 | pAreaLinks.reset(); |
76 | 0 | } |
77 | | |
78 | | void ScRefUndoData::DeleteUnchanged( ScDocument& rDoc ) |
79 | 0 | { |
80 | 0 | if (pDBCollection) |
81 | 0 | { |
82 | 0 | ScDBCollection* pNewDBColl = rDoc.GetDBCollection(); |
83 | 0 | if ( pNewDBColl && *pDBCollection == *pNewDBColl ) |
84 | 0 | pDBCollection.reset(); |
85 | 0 | } |
86 | 0 | if (pRangeName) |
87 | 0 | { |
88 | 0 | ScRangeName& rNewRanges = rDoc.GetRangeName(); |
89 | 0 | if ( *pRangeName == rNewRanges ) |
90 | 0 | pRangeName.reset(); |
91 | 0 | } |
92 | |
|
93 | 0 | if (pPrintRanges) |
94 | 0 | { |
95 | 0 | std::unique_ptr<ScPrintRangeSaver> pNewRanges = rDoc.CreatePrintRangeSaver(); |
96 | 0 | if ( pNewRanges && *pPrintRanges == *pNewRanges ) |
97 | 0 | pPrintRanges.reset(); |
98 | 0 | } |
99 | |
|
100 | 0 | if (pDPCollection) |
101 | 0 | { |
102 | 0 | ScDPCollection* pNewDP = rDoc.GetDPCollection(); //! const |
103 | 0 | if ( pNewDP && pDPCollection->RefsEqual(*pNewDP) ) |
104 | 0 | pDPCollection.reset(); |
105 | 0 | } |
106 | |
|
107 | 0 | if (pDetOpList) |
108 | 0 | { |
109 | 0 | ScDetOpList* pNewDetOp = rDoc.GetDetOpList(); |
110 | 0 | if ( pNewDetOp && *pDetOpList == *pNewDetOp ) |
111 | 0 | pDetOpList.reset(); |
112 | 0 | } |
113 | |
|
114 | 0 | if ( pChartListenerCollection ) |
115 | 0 | { |
116 | 0 | ScChartListenerCollection* pNewChartListenerCollection = |
117 | 0 | rDoc.GetChartListenerCollection(); |
118 | 0 | if ( pNewChartListenerCollection && |
119 | 0 | *pChartListenerCollection == *pNewChartListenerCollection ) |
120 | 0 | pChartListenerCollection.reset(); |
121 | 0 | } |
122 | |
|
123 | 0 | if (pAreaLinks) |
124 | 0 | { |
125 | 0 | if ( pAreaLinks->IsEqual( rDoc ) ) |
126 | 0 | pAreaLinks.reset(); |
127 | 0 | } |
128 | |
|
129 | 0 | if ( rDoc.HasUnoRefUndo() ) |
130 | 0 | { |
131 | 0 | pUnoRefs = rDoc.EndUnoRefUndo(); |
132 | 0 | if ( pUnoRefs && pUnoRefs->IsEmpty() ) |
133 | 0 | { |
134 | 0 | pUnoRefs.reset(); |
135 | 0 | } |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | | void ScRefUndoData::DoUndo( ScDocument& rDoc, bool bUndoRefFirst ) |
140 | 0 | { |
141 | 0 | if (pDBCollection) |
142 | 0 | rDoc.SetDBCollection( std::unique_ptr<ScDBCollection>(new ScDBCollection(*pDBCollection)) ); |
143 | 0 | if (pRangeName) |
144 | 0 | rDoc.SetRangeName( std::unique_ptr<ScRangeName>(new ScRangeName(*pRangeName)) ); |
145 | |
|
146 | 0 | if (pPrintRanges) |
147 | 0 | rDoc.RestorePrintRanges(*pPrintRanges); |
148 | |
|
149 | 0 | if (pDPCollection) |
150 | 0 | { |
151 | 0 | ScDPCollection* pDocDP = rDoc.GetDPCollection(); |
152 | 0 | if (pDocDP) |
153 | 0 | pDPCollection->WriteRefsTo( *pDocDP ); |
154 | 0 | } |
155 | |
|
156 | 0 | if (pDetOpList) |
157 | 0 | rDoc.SetDetOpList( std::unique_ptr<ScDetOpList>(new ScDetOpList(*pDetOpList)) ); |
158 | | |
159 | | // bUndoRefFirst is bSetChartRangeLists |
160 | 0 | if ( pChartListenerCollection ) |
161 | 0 | rDoc.SetChartListenerCollection( std::make_unique<ScChartListenerCollection>( |
162 | 0 | *pChartListenerCollection ), bUndoRefFirst ); |
163 | |
|
164 | 0 | if (pDBCollection || pRangeName) |
165 | 0 | { |
166 | 0 | sc::AutoCalcSwitch aACSwitch(rDoc, false); |
167 | 0 | rDoc.CompileAll(); |
168 | |
|
169 | 0 | sc::SetFormulaDirtyContext aCxt; |
170 | 0 | rDoc.SetAllFormulasDirty(aCxt); |
171 | 0 | } |
172 | |
|
173 | 0 | if (pAreaLinks) |
174 | 0 | pAreaLinks->Restore( rDoc ); |
175 | |
|
176 | 0 | if ( pUnoRefs ) |
177 | 0 | pUnoRefs->Undo( rDoc ); |
178 | 0 | } |
179 | | |
180 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |