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 <strings.hrc>
22 #include "TEditControl.hxx"
23 #include <TableRow.hxx>
24 #include <TableController.hxx>
25 #include <TableDesignView.hxx>
26 #include <FieldDescriptions.hxx>
27 #include <svx/svxids.hrc>
30 using namespace dbaui
;
31 using namespace ::svt
;
34 OTableDesignUndoAct::OTableDesignUndoAct(OTableRowView
* pOwner
, TranslateId pCommentID
)
35 : OCommentUndoAction(pCommentID
)
36 , m_pTabDgnCtrl(pOwner
)
38 m_pTabDgnCtrl
->m_nCurUndoActId
++;
41 OTableDesignUndoAct::~OTableDesignUndoAct()
45 void OTableDesignUndoAct::Undo()
47 m_pTabDgnCtrl
->m_nCurUndoActId
--;
49 // doc has not been modified if first undo was reverted
50 if( m_pTabDgnCtrl
->m_nCurUndoActId
== 0 )
52 m_pTabDgnCtrl
->GetView()->getController().setModified(false);
53 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
57 void OTableDesignUndoAct::Redo()
59 m_pTabDgnCtrl
->m_nCurUndoActId
++;
61 // restore Modified-flag after Redo of first Undo-action
62 if( m_pTabDgnCtrl
->m_nCurUndoActId
> 0 )
64 m_pTabDgnCtrl
->GetView()->getController().setModified(true);
65 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
69 OTableDesignCellUndoAct::OTableDesignCellUndoAct( OTableRowView
* pOwner
, sal_Int32 nRowID
, sal_uInt16 nColumn
) :
70 OTableDesignUndoAct( pOwner
,STR_TABED_UNDO_CELLMODIFIED
)
74 // read text at position (m_nRow, m_nCol)
75 m_sOldText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
78 OTableDesignCellUndoAct::~OTableDesignCellUndoAct()
82 void OTableDesignCellUndoAct::Undo()
84 // store text at old line and restore the old one
85 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
86 m_sNewText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
87 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sOldText
);
88 // line has not been modified if the first Undo was reverted
89 if (m_pTabDgnCtrl
->GetCurUndoActId() == 1)
91 CellControllerRef xController
= m_pTabDgnCtrl
->Controller();
92 if ( xController
.is() )
93 xController
->SaveValue();
94 m_pTabDgnCtrl
->GetView()->getController().setModified(false);
98 OTableDesignUndoAct::Undo();
101 void OTableDesignCellUndoAct::Redo()
104 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
105 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sNewText
);
107 OTableDesignUndoAct::Redo();
110 OTableEditorUndoAct::OTableEditorUndoAct(OTableEditorCtrl
* pOwner
, TranslateId pCommentID
)
111 : OTableDesignUndoAct(pOwner
, pCommentID
)
116 OTableEditorUndoAct::~OTableEditorUndoAct()
120 OTableEditorTypeSelUndoAct::OTableEditorTypeSelUndoAct( OTableEditorCtrl
* pOwner
, sal_Int32 nRowID
, sal_uInt16 nColumn
, TOTypeInfoSP _pOldType
)
121 :OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_TYPE_CHANGED
)
124 ,m_pOldType(std::move( _pOldType
))
128 OTableEditorTypeSelUndoAct::~OTableEditorTypeSelUndoAct()
132 void OTableEditorTypeSelUndoAct::Undo()
135 OFieldDescription
* pFieldDesc
= pTabEdCtrl
->GetFieldDescr(m_nRow
);
137 m_pNewType
= pFieldDesc
->getTypeInfo();
139 m_pNewType
= TOTypeInfoSP();
140 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pOldType
);
141 pTabEdCtrl
->SwitchType( m_pOldType
);
143 OTableEditorUndoAct::Undo();
146 void OTableEditorTypeSelUndoAct::Redo()
149 pTabEdCtrl
->GoToRowColumnId( m_nRow
,m_nCol
);
150 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pNewType
);
152 OTableEditorUndoAct::Redo();
155 OTableEditorDelUndoAct::OTableEditorDelUndoAct( OTableEditorCtrl
* pOwner
) :
156 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_ROWDELETED
)
158 // fill DeletedRowList
159 std::vector
< std::shared_ptr
<OTableRow
> >* pOriginalRows
= pOwner
->GetRowList();
160 sal_Int32 nIndex
= pOwner
->FirstSelectedRow();
161 std::shared_ptr
<OTableRow
> pOriginalRow
;
162 std::shared_ptr
<OTableRow
> pNewRow
;
164 while( nIndex
!= SFX_ENDOFSELECTION
)
166 pOriginalRow
= (*pOriginalRows
)[nIndex
];
167 pNewRow
= std::make_shared
<OTableRow
>( *pOriginalRow
, nIndex
);
168 m_aDeletedRows
.push_back( pNewRow
);
170 nIndex
= pOwner
->NextSelectedRow();
174 OTableEditorDelUndoAct::~OTableEditorDelUndoAct()
176 m_aDeletedRows
.clear();
179 void OTableEditorDelUndoAct::Undo()
181 // Insert the deleted line
184 std::shared_ptr
<OTableRow
> pNewOrigRow
;
185 std::vector
< std::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
187 for (auto const& deletedRow
: m_aDeletedRows
)
189 pNewOrigRow
= std::make_shared
<OTableRow
>( *deletedRow
);
190 nPos
= deletedRow
->GetPos();
191 pOriginalRows
->insert( pOriginalRows
->begin()+nPos
,pNewOrigRow
);
194 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
195 pTabEdCtrl
->Invalidate();
196 OTableEditorUndoAct::Undo();
199 void OTableEditorDelUndoAct::Redo()
202 std::vector
< std::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
204 for (auto const& deletedRow
: m_aDeletedRows
)
206 auto it
= pOriginalRows
->begin() + deletedRow
->GetPos();
207 pOriginalRows
->erase(it
);
210 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
211 pTabEdCtrl
->Invalidate();
212 OTableEditorUndoAct::Redo();
215 OTableEditorInsUndoAct::OTableEditorInsUndoAct( OTableEditorCtrl
* pOwner
,
216 tools::Long nInsertPosition
,
217 std::vector
< std::shared_ptr
<OTableRow
> >&& _vInsertedRows
)
218 :OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_ROWINSERTED
)
219 ,m_vInsertedRows(std::move(_vInsertedRows
))
220 ,m_nInsPos( nInsertPosition
)
224 OTableEditorInsUndoAct::~OTableEditorInsUndoAct()
226 m_vInsertedRows
.clear();
229 void OTableEditorInsUndoAct::Undo()
231 // delete lines again
232 std::vector
< std::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
233 pOriginalRows
->erase(pOriginalRows
->begin() + m_nInsPos
, pOriginalRows
->begin() + m_nInsPos
+ m_vInsertedRows
.size());
235 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_vInsertedRows
.size() );
236 pTabEdCtrl
->InvalidateHandleColumn();
238 OTableEditorUndoAct::Undo();
241 void OTableEditorInsUndoAct::Redo()
243 // insert lines again
244 sal_Int32 nInsertRow
= m_nInsPos
;
245 std::shared_ptr
<OTableRow
> pRow
;
246 std::vector
< std::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
247 for (auto const& insertedRow
: m_vInsertedRows
)
249 pRow
= std::make_shared
<OTableRow
>( *insertedRow
);
250 pRowList
->insert( pRowList
->begin()+nInsertRow
,pRow
);
254 pTabEdCtrl
->RowInserted( m_nInsPos
, m_vInsertedRows
.size() );
255 pTabEdCtrl
->InvalidateHandleColumn();
257 OTableEditorUndoAct::Redo();
260 OTableEditorInsNewUndoAct::OTableEditorInsNewUndoAct( OTableEditorCtrl
* pOwner
, sal_Int32 nInsertPosition
, sal_Int32 nInsertedRows
) :
261 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_NEWROWINSERTED
)
262 ,m_nInsPos( nInsertPosition
)
263 ,m_nInsRows( nInsertedRows
)
267 OTableEditorInsNewUndoAct::~OTableEditorInsNewUndoAct()
271 void OTableEditorInsNewUndoAct::Undo()
273 // delete inserted lines
274 std::vector
< std::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
276 pOriginalRows
->erase(pOriginalRows
->begin() + m_nInsPos
, pOriginalRows
->begin() + m_nInsPos
+ m_nInsRows
);
278 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_nInsRows
);
279 pTabEdCtrl
->InvalidateHandleColumn();
281 OTableEditorUndoAct::Undo();
284 void OTableEditorInsNewUndoAct::Redo()
286 // insert lines again
287 std::vector
< std::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
289 for( tools::Long i
=m_nInsPos
; i
<(m_nInsPos
+m_nInsRows
); i
++ )
290 pRowList
->insert( pRowList
->begin()+i
,std::make_shared
<OTableRow
>() );
292 pTabEdCtrl
->RowInserted( m_nInsPos
, m_nInsRows
);
293 pTabEdCtrl
->InvalidateHandleColumn();
295 OTableEditorUndoAct::Redo();
298 OPrimKeyUndoAct::OPrimKeyUndoAct( OTableEditorCtrl
* pOwner
, const MultiSelection
& aDeletedKeys
, const MultiSelection
& aInsertedKeys
) :
299 OTableEditorUndoAct( pOwner
,STR_TABLEDESIGN_UNDO_PRIMKEY
)
300 ,m_aDelKeys( aDeletedKeys
)
301 ,m_aInsKeys( aInsertedKeys
)
302 ,m_pEditorCtrl( pOwner
)
306 OPrimKeyUndoAct::~OPrimKeyUndoAct()
310 void OPrimKeyUndoAct::Undo()
312 std::vector
< std::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
313 std::shared_ptr
<OTableRow
> pRow
;
316 // delete inserted keys
317 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= tools::Long(SFX_ENDOFSELECTION
); nIndex
=m_aInsKeys
.NextSelected() )
319 OSL_ENSURE(nIndex
<= static_cast<tools::Long
>(pRowList
->size()),"Index for undo isn't valid!");
320 pRow
= (*pRowList
)[nIndex
];
321 pRow
->SetPrimaryKey( false );
324 // restore deleted keys
325 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= tools::Long(SFX_ENDOFSELECTION
); nIndex
=m_aDelKeys
.NextSelected() )
327 OSL_ENSURE(nIndex
<= static_cast<tools::Long
>(pRowList
->size()),"Index for undo isn't valid!");
328 pRow
= (*pRowList
)[nIndex
];
329 pRow
->SetPrimaryKey( true );
332 m_pEditorCtrl
->InvalidateHandleColumn();
333 OTableEditorUndoAct::Undo();
336 void OPrimKeyUndoAct::Redo()
338 std::vector
< std::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
341 // delete the deleted keys
342 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= tools::Long(SFX_ENDOFSELECTION
); nIndex
=m_aDelKeys
.NextSelected() )
343 (*pRowList
)[nIndex
]->SetPrimaryKey( false );
345 // restore the inserted keys
346 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= tools::Long(SFX_ENDOFSELECTION
); nIndex
=m_aInsKeys
.NextSelected() )
347 (*pRowList
)[nIndex
]->SetPrimaryKey( true );
349 m_pEditorCtrl
->InvalidateHandleColumn();
350 OTableEditorUndoAct::Redo();
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */