Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / table / tableundo.cxx
blob060cd2ae4f6f63330d79b8d01ceea352d4c65b74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <sdr/properties/cellproperties.hxx>
22 #include <editeng/outlobj.hxx>
24 #include <cell.hxx>
25 #include "tableundo.hxx"
26 #include <svx/svdotable.hxx>
27 #include "tablerow.hxx"
28 #include "tablecolumn.hxx"
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::table;
35 namespace sdr::table {
37 CellUndo::CellUndo( SdrObject* pObjRef, const CellRef& xCell )
38 : SdrUndoAction(xCell->GetObject().getSdrModelFromSdrObject())
39 ,mxObjRef( pObjRef )
40 ,mxCell( xCell )
41 ,mbUndo( true )
43 if( mxCell.is() && pObjRef )
45 getDataFromCell( maUndoData );
46 pObjRef->AddObjectUser( *this );
50 CellUndo::~CellUndo()
52 if( auto pObj = mxObjRef.get() )
53 pObj->RemoveObjectUser( *this );
54 dispose();
57 void CellUndo::dispose()
59 mxCell.clear();
60 maUndoData.mxProperties.reset();
61 maRedoData.mxProperties.reset();
62 maUndoData.mpOutlinerParaObject.reset();
63 maRedoData.mpOutlinerParaObject.reset();
66 void CellUndo::ObjectInDestruction(const SdrObject& )
68 dispose();
71 void CellUndo::Undo()
73 if( mxCell.is() && mbUndo )
75 if( !maRedoData.mxProperties )
76 getDataFromCell( maRedoData );
78 setDataToCell( maUndoData );
79 mbUndo = false;
83 void CellUndo::Redo()
85 if( mxCell.is() && !mbUndo )
87 setDataToCell( maRedoData );
88 mbUndo = true;
92 bool CellUndo::Merge( SfxUndoAction *pNextAction )
94 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
95 return pNext && pNext->mxCell.get() == mxCell.get();
98 void CellUndo::setDataToCell( const Data& rData )
100 if( rData.mxProperties )
101 mxCell->mpProperties.reset(new properties::CellProperties( *rData.mxProperties, *mxObjRef.get(), mxCell.get() ));
102 else
103 mxCell->mpProperties.reset();
105 if( rData.mpOutlinerParaObject )
106 mxCell->SetOutlinerParaObject( *rData.mpOutlinerParaObject );
107 else
108 mxCell->RemoveOutlinerParaObject();
110 mxCell->msFormula = rData.msFormula;
111 mxCell->mfValue = rData.mfValue;
112 mxCell->mnError = rData.mnError;
113 mxCell->mbMerged = rData.mbMerged;
114 mxCell->mnRowSpan = rData.mnRowSpan;
115 mxCell->mnColSpan = rData.mnColSpan;
117 if(auto pObj = mxObjRef.get())
119 // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
120 // and this is done best using ReformatText() for table objects
121 pObj->ActionChanged();
122 pObj->NbcReformatText();
126 void CellUndo::getDataFromCell( Data& rData )
128 if( !(mxObjRef.get().is() && mxCell.is()) )
129 return;
131 if( mxCell->mpProperties )
132 rData.mxProperties.reset( mxCell->CloneProperties( *mxObjRef.get(), *mxCell) );
134 if( mxCell->GetOutlinerParaObject() )
135 rData.mpOutlinerParaObject = *mxCell->GetOutlinerParaObject();
136 else
137 rData.mpOutlinerParaObject.reset();
139 rData.msFormula = mxCell->msFormula;
140 rData.mfValue = mxCell->mfValue;
141 rData.mnError = mxCell->mnError;
142 rData.mbMerged = mxCell->mbMerged;
143 rData.mnRowSpan = mxCell->mnRowSpan;
144 rData.mnColSpan = mxCell->mnColSpan;
148 // class InsertRowUndo : public SdrUndoAction
151 static void Dispose( RowVector& rRows )
153 for( auto& rpRow : rRows )
154 rpRow->dispose();
158 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
159 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
160 ,mxTable( xTable )
161 ,mnIndex( nIndex )
162 ,mbUndo( true )
164 maRows.swap( aNewRows );
168 InsertRowUndo::~InsertRowUndo()
170 if( !mbUndo )
171 Dispose( maRows );
175 void InsertRowUndo::Undo()
177 if( mxTable.is() )
179 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
180 mbUndo = false;
185 void InsertRowUndo::Redo()
187 if( mxTable.is() )
189 mxTable->UndoRemoveRows( mnIndex, maRows );
190 mbUndo = true;
195 // class RemoveRowUndo : public SdrUndoAction
198 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
199 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
200 ,mxTable( xTable )
201 ,mnIndex( nIndex )
202 ,mbUndo( true )
204 maRows.swap( aRemovedRows );
208 RemoveRowUndo::~RemoveRowUndo()
210 if( mbUndo )
211 Dispose( maRows );
215 void RemoveRowUndo::Undo()
217 if( mxTable.is() )
219 mxTable->UndoRemoveRows( mnIndex, maRows );
220 mbUndo = false;
225 void RemoveRowUndo::Redo()
227 if( mxTable.is() )
229 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
230 mbUndo = true;
235 // class InsertColUndo : public SdrUndoAction
238 static void Dispose( ColumnVector& rCols )
240 for( auto& rpCol : rCols )
241 rpCol->dispose();
245 static void Dispose( CellVector& rCells )
247 for( auto& rpCell : rCells )
248 rpCell->dispose();
252 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
253 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
254 ,mxTable( xTable )
255 ,mnIndex( nIndex )
256 ,mbUndo( true )
258 maColumns.swap( aNewCols );
259 maCells.swap( aCells );
263 InsertColUndo::~InsertColUndo()
265 if( !mbUndo )
267 Dispose( maColumns );
268 Dispose( maCells );
273 void InsertColUndo::Undo()
275 if( mxTable.is() )
277 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
278 mbUndo = false;
283 void InsertColUndo::Redo()
285 if( mxTable.is() )
287 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
288 mbUndo = true;
293 // class RemoveColUndo : public SdrUndoAction
296 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
297 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
298 ,mxTable( xTable )
299 ,mnIndex( nIndex )
300 ,mbUndo( true )
302 maColumns.swap( aNewCols );
303 maCells.swap( aCells );
307 RemoveColUndo::~RemoveColUndo()
309 if( mbUndo )
311 Dispose( maColumns );
312 Dispose( maCells );
317 void RemoveColUndo::Undo()
319 if( mxTable.is() )
321 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
322 mbUndo = false;
327 void RemoveColUndo::Redo()
329 if( mxTable.is() )
331 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
332 mbUndo = true;
337 // class TableColumnUndo : public SdrUndoAction
340 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
341 : SdrUndoAction(xCol->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
342 ,mxCol( xCol )
343 ,mbHasRedoData( false )
345 getData( maUndoData );
349 TableColumnUndo::~TableColumnUndo()
354 void TableColumnUndo::Undo()
356 if( !mbHasRedoData )
358 getData( maRedoData );
359 mbHasRedoData = true;
361 setData( maUndoData );
365 void TableColumnUndo::Redo()
367 setData( maRedoData );
371 bool TableColumnUndo::Merge( SfxUndoAction *pNextAction )
373 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
374 return pNext && pNext->mxCol == mxCol;
378 void TableColumnUndo::setData( const Data& rData )
380 mxCol->mnColumn = rData.mnColumn;
381 mxCol->mnWidth = rData.mnWidth;
382 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
383 mxCol->mbIsVisible = rData.mbIsVisible;
384 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
385 mxCol->maName = rData.maName;
387 // Trigger re-layout of the table.
388 mxCol->getModel()->setModified(true);
392 void TableColumnUndo::getData( Data& rData )
394 rData.mnColumn = mxCol->mnColumn;
395 rData.mnWidth = mxCol->mnWidth;
396 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
397 rData.mbIsVisible = mxCol->mbIsVisible;
398 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
399 rData.maName = mxCol->maName;
403 // class TableRowUndo : public SdrUndoAction
406 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
407 : SdrUndoAction(xRow->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
408 , mxRow( xRow )
409 , mbHasRedoData( false )
411 getData( maUndoData );
415 TableRowUndo::~TableRowUndo()
420 void TableRowUndo::Undo()
422 if( !mbHasRedoData )
424 getData( maRedoData );
425 mbHasRedoData = true;
427 setData( maUndoData );
431 void TableRowUndo::Redo()
433 setData( maRedoData );
437 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
439 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
440 return pNext && pNext->mxRow == mxRow;
444 void TableRowUndo::setData( const Data& rData )
446 mxRow->mnRow = rData.mnRow;
447 mxRow->mnHeight = rData.mnHeight;
448 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
449 mxRow->mbIsVisible = rData.mbIsVisible;
450 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
451 mxRow->maName = rData.maName;
453 // Trigger re-layout of the table.
454 mxRow->getModel()->setModified(true);
458 void TableRowUndo::getData( Data& rData )
460 rData.mnRow = mxRow->mnRow;
461 rData.mnHeight = mxRow->mnHeight;
462 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
463 rData.mbIsVisible = mxRow->mbIsVisible;
464 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
465 rData.maName = mxRow->maName;
469 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
470 : SdrUndoAction(rTableObj.getSdrModelFromSdrObject())
471 ,mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
472 ,mbHasRedoData(false)
474 getData( maUndoData );
477 void TableStyleUndo::Undo()
479 if( !mbHasRedoData )
481 getData( maRedoData );
482 mbHasRedoData = true;
484 setData( maUndoData );
487 void TableStyleUndo::Redo()
489 setData( maRedoData );
492 void TableStyleUndo::setData( const Data& rData )
494 rtl::Reference<SdrTableObj> pTableObj = mxObjRef.get();
495 if( pTableObj )
497 pTableObj->setTableStyle( rData.mxTableStyle );
498 pTableObj->setTableStyleSettings( rData.maSettings );
502 void TableStyleUndo::getData( Data& rData )
504 rtl::Reference<SdrTableObj> pTableObj = mxObjRef.get();
505 if( pTableObj )
507 rData.maSettings = pTableObj->getTableStyleSettings();
508 rData.mxTableStyle = pTableObj->getTableStyle();
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */