Line data Source code
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 "TableUndo.hxx"
21 : #include "dbu_tbl.hrc"
22 : #include "TEditControl.hxx"
23 : #include "TableRow.hxx"
24 : #include "browserids.hxx"
25 : #include "TableController.hxx"
26 : #include "TableDesignView.hxx"
27 : #include "FieldDescriptions.hxx"
28 : #include <tools/debug.hxx>
29 :
30 : using namespace dbaui;
31 : using namespace ::svt;
32 :
33 0 : TYPEINIT1( OCommentUndoAction, SfxUndoAction );
34 0 : TYPEINIT1( OTableDesignUndoAct, OCommentUndoAction );
35 0 : TYPEINIT1( OTableEditorUndoAct, OTableDesignUndoAct );
36 0 : TYPEINIT1( OTableDesignCellUndoAct, OTableDesignUndoAct );
37 0 : TYPEINIT1( OTableEditorTypeSelUndoAct, OTableEditorUndoAct );
38 0 : TYPEINIT1( OTableEditorDelUndoAct, OTableEditorUndoAct );
39 0 : TYPEINIT1( OTableEditorInsUndoAct, OTableEditorUndoAct );
40 0 : TYPEINIT1( OTableEditorInsNewUndoAct, OTableEditorUndoAct );
41 0 : TYPEINIT1( OPrimKeyUndoAct, OTableEditorUndoAct );
42 :
43 : // class OTableDesignUndoAct
44 0 : OTableDesignUndoAct::OTableDesignUndoAct( OTableRowView* pOwner,sal_uInt16 nCommentID ) : OCommentUndoAction(nCommentID)
45 0 : ,m_pTabDgnCtrl( pOwner )
46 : {
47 0 : m_pTabDgnCtrl->m_nCurUndoActId++;
48 0 : }
49 :
50 0 : OTableDesignUndoAct::~OTableDesignUndoAct()
51 : {
52 0 : }
53 :
54 0 : void OTableDesignUndoAct::Undo()
55 : {
56 0 : m_pTabDgnCtrl->m_nCurUndoActId--;
57 :
58 : // doc has not been modified if first undo was reverted
59 0 : if( m_pTabDgnCtrl->m_nCurUndoActId == 0 )
60 : {
61 0 : m_pTabDgnCtrl->GetView()->getController().setModified(sal_False);
62 0 : m_pTabDgnCtrl->GetView()->getController().InvalidateFeature(SID_SAVEDOC);
63 : }
64 0 : }
65 :
66 0 : void OTableDesignUndoAct::Redo()
67 : {
68 0 : m_pTabDgnCtrl->m_nCurUndoActId++;
69 :
70 : // restore Modifed-flag after Redo of first Undo-action
71 0 : if( m_pTabDgnCtrl->m_nCurUndoActId > 0 )
72 : {
73 0 : m_pTabDgnCtrl->GetView()->getController().setModified(sal_True);
74 0 : m_pTabDgnCtrl->GetView()->getController().InvalidateFeature(SID_SAVEDOC);
75 : }
76 0 : }
77 :
78 : // class OTableDesignCellUndoAct
79 0 : OTableDesignCellUndoAct::OTableDesignCellUndoAct( OTableRowView* pOwner, long nRowID, sal_uInt16 nColumn ) :
80 : OTableDesignUndoAct( pOwner ,STR_TABED_UNDO_CELLMODIFIED)
81 : ,m_nCol( nColumn )
82 0 : ,m_nRow( nRowID )
83 : {
84 : // read text at position (m_nRow, m_nCol)
85 0 : m_sOldText = m_pTabDgnCtrl->GetCellData( m_nRow, m_nCol );
86 0 : }
87 :
88 0 : OTableDesignCellUndoAct::~OTableDesignCellUndoAct()
89 : {
90 0 : }
91 :
92 0 : void OTableDesignCellUndoAct::Undo()
93 : {
94 : // store text at old line and restore the old one
95 0 : m_pTabDgnCtrl->ActivateCell( m_nRow, m_nCol );
96 0 : m_sNewText = m_pTabDgnCtrl->GetCellData( m_nRow, m_nCol );
97 0 : m_pTabDgnCtrl->SetCellData( m_nRow, m_nCol, m_sOldText );
98 : // line has not been modified if the first Undo was reverted
99 0 : if (m_pTabDgnCtrl->GetCurUndoActId() == 1)
100 : {
101 0 : CellControllerRef xController = m_pTabDgnCtrl->Controller();
102 0 : if ( xController.Is() )
103 0 : xController->ClearModified();
104 0 : m_pTabDgnCtrl->GetView()->getController().setModified(sal_False);
105 :
106 : }
107 :
108 0 : OTableDesignUndoAct::Undo();
109 0 : }
110 :
111 0 : void OTableDesignCellUndoAct::Redo()
112 : {
113 : // restore new text
114 0 : m_pTabDgnCtrl->ActivateCell( m_nRow, m_nCol );
115 0 : m_pTabDgnCtrl->SetCellData( m_nRow, m_nCol, m_sNewText );
116 :
117 0 : OTableDesignUndoAct::Redo();
118 0 : }
119 :
120 : // class OTableEditorUndoAct
121 0 : OTableEditorUndoAct::OTableEditorUndoAct( OTableEditorCtrl* pOwner,sal_uInt16 _nCommentID ) :
122 : OTableDesignUndoAct( pOwner ,_nCommentID)
123 0 : ,pTabEdCtrl(pOwner)
124 : {
125 0 : }
126 :
127 0 : OTableEditorUndoAct::~OTableEditorUndoAct()
128 : {
129 0 : }
130 :
131 : // class OTableEditorTypeSelUndoAct
132 0 : OTableEditorTypeSelUndoAct::OTableEditorTypeSelUndoAct( OTableEditorCtrl* pOwner, long nRowID, sal_uInt16 nColumn, const TOTypeInfoSP& _pOldType )
133 : :OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_TYPE_CHANGED)
134 : ,m_nCol( nColumn )
135 : ,m_nRow( nRowID )
136 0 : ,m_pOldType( _pOldType )
137 : {
138 0 : }
139 :
140 0 : OTableEditorTypeSelUndoAct::~OTableEditorTypeSelUndoAct()
141 : {
142 0 : }
143 :
144 0 : void OTableEditorTypeSelUndoAct::Undo()
145 : {
146 : // restore type
147 0 : OFieldDescription* pFieldDesc = pTabEdCtrl->GetFieldDescr(m_nRow);
148 0 : if(pFieldDesc)
149 0 : m_pNewType = pFieldDesc->getTypeInfo();
150 : else
151 0 : m_pNewType = TOTypeInfoSP();
152 0 : pTabEdCtrl->SetCellData(m_nRow,m_nCol,m_pOldType);
153 0 : pTabEdCtrl->SwitchType( m_pOldType );
154 :
155 0 : OTableEditorUndoAct::Undo();
156 0 : }
157 :
158 0 : void OTableEditorTypeSelUndoAct::Redo()
159 : {
160 : // new type
161 0 : pTabEdCtrl->GoToRowColumnId( m_nRow ,m_nCol);
162 0 : pTabEdCtrl->SetCellData(m_nRow,m_nCol,m_pNewType);
163 :
164 0 : OTableEditorUndoAct::Redo();
165 0 : }
166 :
167 : // class OTableEditorDelUndoAct
168 0 : OTableEditorDelUndoAct::OTableEditorDelUndoAct( OTableEditorCtrl* pOwner) :
169 0 : OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_ROWDELETED)
170 : {
171 : // fill DeletedRowList
172 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pOwner->GetRowList();
173 0 : long nIndex = pOwner->FirstSelectedRow();
174 0 : ::boost::shared_ptr<OTableRow> pOriginalRow;
175 0 : ::boost::shared_ptr<OTableRow> pNewRow;
176 :
177 0 : while( nIndex >= 0 )
178 : {
179 0 : pOriginalRow = (*pOriginalRows)[nIndex];
180 0 : pNewRow.reset(new OTableRow( *pOriginalRow, nIndex ));
181 0 : m_aDeletedRows.push_back( pNewRow);
182 :
183 0 : nIndex = pOwner->NextSelectedRow();
184 0 : }
185 0 : }
186 :
187 0 : OTableEditorDelUndoAct::~OTableEditorDelUndoAct()
188 : {
189 0 : m_aDeletedRows.clear();
190 0 : }
191 :
192 0 : void OTableEditorDelUndoAct::Undo()
193 : {
194 : // Insert the deleted line
195 : sal_uLong nPos;
196 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_aDeletedRows.begin();
197 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_aDeletedRows.end();
198 :
199 0 : ::boost::shared_ptr<OTableRow> pNewOrigRow;
200 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
201 :
202 0 : for(;aIter != aEnd;++aIter)
203 : {
204 0 : pNewOrigRow.reset(new OTableRow( **aIter ));
205 0 : nPos = (*aIter)->GetPos();
206 0 : pOriginalRows->insert( pOriginalRows->begin()+nPos,pNewOrigRow);
207 : }
208 :
209 0 : pTabEdCtrl->DisplayData(pTabEdCtrl->GetCurRow());
210 0 : pTabEdCtrl->Invalidate();
211 0 : OTableEditorUndoAct::Undo();
212 0 : }
213 :
214 0 : void OTableEditorDelUndoAct::Redo()
215 : {
216 : // delete line again
217 : sal_uLong nPos;
218 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_aDeletedRows.begin();
219 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_aDeletedRows.end();
220 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
221 :
222 0 : for(;aIter != aEnd;++aIter)
223 : {
224 0 : nPos = (*aIter)->GetPos();
225 0 : pOriginalRows->erase( pOriginalRows->begin()+nPos );
226 : }
227 :
228 0 : pTabEdCtrl->DisplayData(pTabEdCtrl->GetCurRow());
229 0 : pTabEdCtrl->Invalidate();
230 0 : OTableEditorUndoAct::Redo();
231 0 : }
232 :
233 : // class OTableEditorInsUndoAct
234 0 : OTableEditorInsUndoAct::OTableEditorInsUndoAct( OTableEditorCtrl* pOwner,
235 : long nInsertPosition ,
236 : const ::std::vector< ::boost::shared_ptr<OTableRow> >& _vInsertedRows)
237 : :OTableEditorUndoAct( pOwner,STR_TABED_UNDO_ROWINSERTED )
238 : ,m_vInsertedRows(_vInsertedRows)
239 0 : ,m_nInsPos( nInsertPosition )
240 : {
241 0 : }
242 :
243 0 : OTableEditorInsUndoAct::~OTableEditorInsUndoAct()
244 : {
245 0 : m_vInsertedRows.clear();
246 0 : }
247 :
248 0 : void OTableEditorInsUndoAct::Undo()
249 : {
250 : // delete lines again
251 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
252 0 : for( long i=(m_nInsPos+m_vInsertedRows.size()-1); i>(m_nInsPos-1); i-- )
253 : {
254 0 : pOriginalRows->erase(pOriginalRows->begin()+i);
255 : }
256 :
257 0 : pTabEdCtrl->RowRemoved( m_nInsPos, m_vInsertedRows.size(), true );
258 0 : pTabEdCtrl->InvalidateHandleColumn();
259 :
260 0 : OTableEditorUndoAct::Undo();
261 0 : }
262 :
263 0 : void OTableEditorInsUndoAct::Redo()
264 : {
265 : // insert lines again
266 0 : long nInsertRow = m_nInsPos;
267 0 : ::boost::shared_ptr<OTableRow> pRow;
268 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_vInsertedRows.begin();
269 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_vInsertedRows.end();
270 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
271 0 : for(;aIter != aEnd;++aIter)
272 : {
273 0 : pRow.reset(new OTableRow( **aIter ));
274 0 : pRowList->insert( pRowList->begin()+nInsertRow ,pRow );
275 0 : nInsertRow++;
276 : }
277 :
278 0 : pTabEdCtrl->RowInserted( m_nInsPos, m_vInsertedRows.size(), true );
279 0 : pTabEdCtrl->InvalidateHandleColumn();
280 :
281 0 : OTableEditorUndoAct::Redo();
282 0 : }
283 :
284 : // class OTableEditorInsNewUndoAct
285 0 : OTableEditorInsNewUndoAct::OTableEditorInsNewUndoAct( OTableEditorCtrl* pOwner, long nInsertPosition, long nInsertedRows ) :
286 : OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_NEWROWINSERTED)
287 : ,m_nInsPos( nInsertPosition )
288 0 : ,m_nInsRows( nInsertedRows )
289 : {
290 0 : }
291 :
292 0 : OTableEditorInsNewUndoAct::~OTableEditorInsNewUndoAct()
293 : {
294 0 : }
295 :
296 0 : void OTableEditorInsNewUndoAct::Undo()
297 : {
298 : // delete inserted lines
299 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
300 :
301 0 : for( long i=(m_nInsPos+m_nInsRows-1); i>(m_nInsPos-1); i-- )
302 : {
303 0 : pOriginalRows->erase(pOriginalRows->begin()+i);
304 : }
305 :
306 0 : pTabEdCtrl->RowRemoved( m_nInsPos, m_nInsRows, true );
307 0 : pTabEdCtrl->InvalidateHandleColumn();
308 :
309 0 : OTableEditorUndoAct::Undo();
310 0 : }
311 :
312 0 : void OTableEditorInsNewUndoAct::Redo()
313 : {
314 : // insert lines again
315 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
316 :
317 0 : for( long i=m_nInsPos; i<(m_nInsPos+m_nInsRows); i++ )
318 0 : pRowList->insert( pRowList->begin()+i,::boost::shared_ptr<OTableRow>(new OTableRow()) );
319 :
320 0 : pTabEdCtrl->RowInserted( m_nInsPos, m_nInsRows, true );
321 0 : pTabEdCtrl->InvalidateHandleColumn();
322 :
323 0 : OTableEditorUndoAct::Redo();
324 0 : }
325 :
326 : // class OPrimKeyUndoAct
327 0 : OPrimKeyUndoAct::OPrimKeyUndoAct( OTableEditorCtrl* pOwner, const MultiSelection& aDeletedKeys, const MultiSelection& aInsertedKeys) :
328 : OTableEditorUndoAct( pOwner ,STR_TABLEDESIGN_UNDO_PRIMKEY)
329 : ,m_aDelKeys( aDeletedKeys )
330 : ,m_aInsKeys( aInsertedKeys )
331 0 : ,m_pEditorCtrl( pOwner )
332 : {
333 0 : }
334 :
335 0 : OPrimKeyUndoAct::~OPrimKeyUndoAct()
336 : {
337 0 : }
338 :
339 0 : void OPrimKeyUndoAct::Undo()
340 : {
341 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
342 0 : ::boost::shared_ptr<OTableRow> pRow;
343 : long nIndex;
344 :
345 : // delete inserted keys
346 0 : for( nIndex = m_aInsKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aInsKeys.NextSelected() )
347 : {
348 : OSL_ENSURE(nIndex <= static_cast<long>(pRowList->size()),"Index for undo isn't valid!");
349 0 : pRow = (*pRowList)[nIndex];
350 0 : pRow->SetPrimaryKey( false );
351 : }
352 :
353 : // restore deleted keys
354 0 : for( nIndex = m_aDelKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aDelKeys.NextSelected() )
355 : {
356 : OSL_ENSURE(nIndex <= static_cast<long>(pRowList->size()),"Index for undo isn't valid!");
357 0 : pRow = (*pRowList)[nIndex];
358 0 : pRow->SetPrimaryKey( true );
359 : }
360 :
361 0 : m_pEditorCtrl->InvalidateHandleColumn();
362 0 : OTableEditorUndoAct::Undo();
363 0 : }
364 :
365 0 : void OPrimKeyUndoAct::Redo()
366 : {
367 0 : ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
368 : long nIndex;
369 :
370 : // delete the deleted keys
371 0 : for( nIndex = m_aDelKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aDelKeys.NextSelected() )
372 0 : (*pRowList)[nIndex]->SetPrimaryKey( false );
373 :
374 : // restore the inserted keys
375 0 : for( nIndex = m_aInsKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aInsKeys.NextSelected() )
376 0 : (*pRowList)[nIndex]->SetPrimaryKey( true );
377 :
378 0 : m_pEditorCtrl->InvalidateHandleColumn();
379 0 : OTableEditorUndoAct::Redo();
380 36 : }
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|