1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 using namespace dbaui
;
31 using namespace ::svt
;
33 TYPEINIT1( OCommentUndoAction
, SfxUndoAction
);
34 TYPEINIT1( OTableDesignUndoAct
, OCommentUndoAction
);
35 TYPEINIT1( OTableEditorUndoAct
, OTableDesignUndoAct
);
36 TYPEINIT1( OTableDesignCellUndoAct
, OTableDesignUndoAct
);
37 TYPEINIT1( OTableEditorTypeSelUndoAct
, OTableEditorUndoAct
);
38 TYPEINIT1( OTableEditorDelUndoAct
, OTableEditorUndoAct
);
39 TYPEINIT1( OTableEditorInsUndoAct
, OTableEditorUndoAct
);
40 TYPEINIT1( OTableEditorInsNewUndoAct
, OTableEditorUndoAct
);
41 TYPEINIT1( OPrimKeyUndoAct
, OTableEditorUndoAct
);
43 // class OTableDesignUndoAct
44 OTableDesignUndoAct::OTableDesignUndoAct( OTableRowView
* pOwner
,sal_uInt16 nCommentID
) : OCommentUndoAction(nCommentID
)
45 ,m_pTabDgnCtrl( pOwner
)
47 m_pTabDgnCtrl
->m_nCurUndoActId
++;
50 OTableDesignUndoAct::~OTableDesignUndoAct()
54 void OTableDesignUndoAct::Undo()
56 m_pTabDgnCtrl
->m_nCurUndoActId
--;
58 // doc has not been modified if first undo was reverted
59 if( m_pTabDgnCtrl
->m_nCurUndoActId
== 0 )
61 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_False
);
62 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
66 void OTableDesignUndoAct::Redo()
68 m_pTabDgnCtrl
->m_nCurUndoActId
++;
70 // restore Modifed-flag after Redo of first Undo-action
71 if( m_pTabDgnCtrl
->m_nCurUndoActId
> 0 )
73 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_True
);
74 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
78 // class OTableDesignCellUndoAct
79 OTableDesignCellUndoAct::OTableDesignCellUndoAct( OTableRowView
* pOwner
, long nRowID
, sal_uInt16 nColumn
) :
80 OTableDesignUndoAct( pOwner
,STR_TABED_UNDO_CELLMODIFIED
)
84 // read text at position (m_nRow, m_nCol)
85 m_sOldText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
88 OTableDesignCellUndoAct::~OTableDesignCellUndoAct()
92 void OTableDesignCellUndoAct::Undo()
94 // store text at old line and restore the old one
95 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
96 m_sNewText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
97 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sOldText
);
98 // line has not been modified if the first Undo was reverted
99 if (m_pTabDgnCtrl
->GetCurUndoActId() == 1)
101 CellControllerRef xController
= m_pTabDgnCtrl
->Controller();
102 if ( xController
.Is() )
103 xController
->ClearModified();
104 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_False
);
108 OTableDesignUndoAct::Undo();
111 void OTableDesignCellUndoAct::Redo()
114 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
115 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sNewText
);
117 OTableDesignUndoAct::Redo();
120 // class OTableEditorUndoAct
121 OTableEditorUndoAct::OTableEditorUndoAct( OTableEditorCtrl
* pOwner
,sal_uInt16 _nCommentID
) :
122 OTableDesignUndoAct( pOwner
,_nCommentID
)
127 OTableEditorUndoAct::~OTableEditorUndoAct()
131 // class OTableEditorTypeSelUndoAct
132 OTableEditorTypeSelUndoAct::OTableEditorTypeSelUndoAct( OTableEditorCtrl
* pOwner
, long nRowID
, sal_uInt16 nColumn
, const TOTypeInfoSP
& _pOldType
)
133 :OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_TYPE_CHANGED
)
136 ,m_pOldType( _pOldType
)
140 OTableEditorTypeSelUndoAct::~OTableEditorTypeSelUndoAct()
144 void OTableEditorTypeSelUndoAct::Undo()
147 OFieldDescription
* pFieldDesc
= pTabEdCtrl
->GetFieldDescr(m_nRow
);
149 m_pNewType
= pFieldDesc
->getTypeInfo();
151 m_pNewType
= TOTypeInfoSP();
152 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pOldType
);
153 pTabEdCtrl
->SwitchType( m_pOldType
);
155 OTableEditorUndoAct::Undo();
158 void OTableEditorTypeSelUndoAct::Redo()
161 pTabEdCtrl
->GoToRowColumnId( m_nRow
,m_nCol
);
162 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pNewType
);
164 OTableEditorUndoAct::Redo();
167 // class OTableEditorDelUndoAct
168 OTableEditorDelUndoAct::OTableEditorDelUndoAct( OTableEditorCtrl
* pOwner
) :
169 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_ROWDELETED
)
171 // fill DeletedRowList
172 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pOwner
->GetRowList();
173 long nIndex
= pOwner
->FirstSelectedRow();
174 ::boost::shared_ptr
<OTableRow
> pOriginalRow
;
175 ::boost::shared_ptr
<OTableRow
> pNewRow
;
179 pOriginalRow
= (*pOriginalRows
)[nIndex
];
180 pNewRow
.reset(new OTableRow( *pOriginalRow
, nIndex
));
181 m_aDeletedRows
.push_back( pNewRow
);
183 nIndex
= pOwner
->NextSelectedRow();
187 OTableEditorDelUndoAct::~OTableEditorDelUndoAct()
189 m_aDeletedRows
.clear();
192 void OTableEditorDelUndoAct::Undo()
194 // Insert the deleted line
196 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_aDeletedRows
.begin();
197 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_aDeletedRows
.end();
199 ::boost::shared_ptr
<OTableRow
> pNewOrigRow
;
200 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
202 for(;aIter
!= aEnd
;++aIter
)
204 pNewOrigRow
.reset(new OTableRow( **aIter
));
205 nPos
= (*aIter
)->GetPos();
206 pOriginalRows
->insert( pOriginalRows
->begin()+nPos
,pNewOrigRow
);
209 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
210 pTabEdCtrl
->Invalidate();
211 OTableEditorUndoAct::Undo();
214 void OTableEditorDelUndoAct::Redo()
218 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_aDeletedRows
.begin();
219 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_aDeletedRows
.end();
220 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
222 for(;aIter
!= aEnd
;++aIter
)
224 nPos
= (*aIter
)->GetPos();
225 pOriginalRows
->erase( pOriginalRows
->begin()+nPos
);
228 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
229 pTabEdCtrl
->Invalidate();
230 OTableEditorUndoAct::Redo();
233 // class OTableEditorInsUndoAct
234 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 ,m_nInsPos( nInsertPosition
)
243 OTableEditorInsUndoAct::~OTableEditorInsUndoAct()
245 m_vInsertedRows
.clear();
248 void OTableEditorInsUndoAct::Undo()
250 // delete lines again
251 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
252 for( long i
=(m_nInsPos
+m_vInsertedRows
.size()-1); i
>(m_nInsPos
-1); i
-- )
254 pOriginalRows
->erase(pOriginalRows
->begin()+i
);
257 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_vInsertedRows
.size(), true );
258 pTabEdCtrl
->InvalidateHandleColumn();
260 OTableEditorUndoAct::Undo();
263 void OTableEditorInsUndoAct::Redo()
265 // insert lines again
266 long nInsertRow
= m_nInsPos
;
267 ::boost::shared_ptr
<OTableRow
> pRow
;
268 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_vInsertedRows
.begin();
269 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_vInsertedRows
.end();
270 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
271 for(;aIter
!= aEnd
;++aIter
)
273 pRow
.reset(new OTableRow( **aIter
));
274 pRowList
->insert( pRowList
->begin()+nInsertRow
,pRow
);
278 pTabEdCtrl
->RowInserted( m_nInsPos
, m_vInsertedRows
.size(), true );
279 pTabEdCtrl
->InvalidateHandleColumn();
281 OTableEditorUndoAct::Redo();
284 // class OTableEditorInsNewUndoAct
285 OTableEditorInsNewUndoAct::OTableEditorInsNewUndoAct( OTableEditorCtrl
* pOwner
, long nInsertPosition
, long nInsertedRows
) :
286 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_NEWROWINSERTED
)
287 ,m_nInsPos( nInsertPosition
)
288 ,m_nInsRows( nInsertedRows
)
292 OTableEditorInsNewUndoAct::~OTableEditorInsNewUndoAct()
296 void OTableEditorInsNewUndoAct::Undo()
298 // delete inserted lines
299 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
301 for( long i
=(m_nInsPos
+m_nInsRows
-1); i
>(m_nInsPos
-1); i
-- )
303 pOriginalRows
->erase(pOriginalRows
->begin()+i
);
306 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_nInsRows
, true );
307 pTabEdCtrl
->InvalidateHandleColumn();
309 OTableEditorUndoAct::Undo();
312 void OTableEditorInsNewUndoAct::Redo()
314 // insert lines again
315 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
317 for( long i
=m_nInsPos
; i
<(m_nInsPos
+m_nInsRows
); i
++ )
318 pRowList
->insert( pRowList
->begin()+i
,::boost::shared_ptr
<OTableRow
>(new OTableRow()) );
320 pTabEdCtrl
->RowInserted( m_nInsPos
, m_nInsRows
, true );
321 pTabEdCtrl
->InvalidateHandleColumn();
323 OTableEditorUndoAct::Redo();
326 // class OPrimKeyUndoAct
327 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 ,m_pEditorCtrl( pOwner
)
335 OPrimKeyUndoAct::~OPrimKeyUndoAct()
339 void OPrimKeyUndoAct::Undo()
341 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
342 ::boost::shared_ptr
<OTableRow
> pRow
;
345 // delete inserted keys
346 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aInsKeys
.NextSelected() )
348 OSL_ENSURE(nIndex
<= static_cast<long>(pRowList
->size()),"Index for undo isn't valid!");
349 pRow
= (*pRowList
)[nIndex
];
350 pRow
->SetPrimaryKey( false );
353 // restore deleted keys
354 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aDelKeys
.NextSelected() )
356 OSL_ENSURE(nIndex
<= static_cast<long>(pRowList
->size()),"Index for undo isn't valid!");
357 pRow
= (*pRowList
)[nIndex
];
358 pRow
->SetPrimaryKey( true );
361 m_pEditorCtrl
->InvalidateHandleColumn();
362 OTableEditorUndoAct::Undo();
365 void OPrimKeyUndoAct::Redo()
367 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
370 // delete the deleted keys
371 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aDelKeys
.NextSelected() )
372 (*pRowList
)[nIndex
]->SetPrimaryKey( false );
374 // restore the inserted keys
375 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aInsKeys
.NextSelected() )
376 (*pRowList
)[nIndex
]->SetPrimaryKey( true );
378 m_pEditorCtrl
->InvalidateHandleColumn();
379 OTableEditorUndoAct::Redo();
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */