Bump version to 6.4-15
[LibreOffice.git] / svx / source / table / tableundo.cxx
blob7b1b070b4458ababbb338fe025b807022220cdbe
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/textproperties.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 { namespace table {
37 CellUndo::CellUndo( const tools::WeakReference<SdrObject>& xObjRef, const CellRef& xCell )
38 : SdrUndoAction(xCell->GetObject().getSdrModelFromSdrObject())
39 ,mxObjRef( xObjRef )
40 ,mxCell( xCell )
41 ,mbUndo( true )
43 if( mxCell.is() && mxObjRef.is() )
45 getDataFromCell( maUndoData );
46 mxObjRef->AddObjectUser( *this );
50 CellUndo::~CellUndo()
52 if( mxObjRef.is() )
53 mxObjRef->RemoveObjectUser( *this );
54 dispose();
57 void CellUndo::dispose()
59 mxCell.clear();
60 delete maUndoData.mpProperties;
61 maUndoData.mpProperties = nullptr;
62 delete maRedoData.mpProperties;
63 maRedoData.mpProperties = nullptr;
64 delete maUndoData.mpOutlinerParaObject;
65 maUndoData.mpOutlinerParaObject = nullptr;
66 delete maRedoData.mpOutlinerParaObject;
67 maRedoData.mpOutlinerParaObject = nullptr;
70 void CellUndo::ObjectInDestruction(const SdrObject& )
72 dispose();
75 void CellUndo::Undo()
77 if( mxCell.is() && mbUndo )
79 if( maRedoData.mpProperties == nullptr )
80 getDataFromCell( maRedoData );
82 setDataToCell( maUndoData );
83 mbUndo = false;
87 void CellUndo::Redo()
89 if( mxCell.is() && !mbUndo )
91 setDataToCell( maRedoData );
92 mbUndo = true;
96 bool CellUndo::Merge( SfxUndoAction *pNextAction )
98 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
99 return pNext && pNext->mxCell.get() == mxCell.get();
102 void CellUndo::setDataToCell( const Data& rData )
104 if( rData.mpProperties )
105 mxCell->mpProperties.reset(Cell::CloneProperties( rData.mpProperties, *mxObjRef.get(), *mxCell ));
106 else
107 mxCell->mpProperties.reset();
109 if( rData.mpOutlinerParaObject )
110 mxCell->SetOutlinerParaObject( std::make_unique<OutlinerParaObject>(*rData.mpOutlinerParaObject) );
111 else
112 mxCell->RemoveOutlinerParaObject();
114 mxCell->msFormula = rData.msFormula;
115 mxCell->mfValue = rData.mfValue;
116 mxCell->mnError = rData.mnError;
117 mxCell->mbMerged = rData.mbMerged;
118 mxCell->mnRowSpan = rData.mnRowSpan;
119 mxCell->mnColSpan = rData.mnColSpan;
121 if(mxObjRef.is())
123 // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
124 // and this is done best using ReformatText() for table objects
125 mxObjRef->ActionChanged();
126 mxObjRef->NbcReformatText();
130 void CellUndo::getDataFromCell( Data& rData )
132 if( mxObjRef.is() && mxCell.is() )
134 if( mxCell->mpProperties )
135 rData.mpProperties = mxCell->CloneProperties( *mxObjRef.get(), *mxCell);
137 if( mxCell->GetOutlinerParaObject() )
138 rData.mpOutlinerParaObject = new OutlinerParaObject(*mxCell->GetOutlinerParaObject());
139 else
140 rData.mpOutlinerParaObject = nullptr;
142 rData.msFormula = mxCell->msFormula;
143 rData.mfValue = mxCell->mfValue;
144 rData.mnError = mxCell->mnError;
145 rData.mbMerged = mxCell->mbMerged;
146 rData.mnRowSpan = mxCell->mnRowSpan;
147 rData.mnColSpan = mxCell->mnColSpan;
152 // class InsertRowUndo : public SdrUndoAction
155 static void Dispose( RowVector& rRows )
157 for( auto& rpRow : rRows )
158 rpRow->dispose();
162 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
163 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
164 ,mxTable( xTable )
165 ,mnIndex( nIndex )
166 ,mbUndo( true )
168 maRows.swap( aNewRows );
172 InsertRowUndo::~InsertRowUndo()
174 if( !mbUndo )
175 Dispose( maRows );
179 void InsertRowUndo::Undo()
181 if( mxTable.is() )
183 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
184 mbUndo = false;
189 void InsertRowUndo::Redo()
191 if( mxTable.is() )
193 mxTable->UndoRemoveRows( mnIndex, maRows );
194 mbUndo = true;
199 // class RemoveRowUndo : public SdrUndoAction
202 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
203 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
204 ,mxTable( xTable )
205 ,mnIndex( nIndex )
206 ,mbUndo( true )
208 maRows.swap( aRemovedRows );
212 RemoveRowUndo::~RemoveRowUndo()
214 if( mbUndo )
215 Dispose( maRows );
219 void RemoveRowUndo::Undo()
221 if( mxTable.is() )
223 mxTable->UndoRemoveRows( mnIndex, maRows );
224 mbUndo = false;
229 void RemoveRowUndo::Redo()
231 if( mxTable.is() )
233 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
234 mbUndo = true;
239 // class InsertColUndo : public SdrUndoAction
242 static void Dispose( ColumnVector& rCols )
244 for( auto& rpCol : rCols )
245 rpCol->dispose();
249 static void Dispose( CellVector& rCells )
251 for( auto& rpCell : rCells )
252 rpCell->dispose();
256 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
257 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
258 ,mxTable( xTable )
259 ,mnIndex( nIndex )
260 ,mbUndo( true )
262 maColumns.swap( aNewCols );
263 maCells.swap( aCells );
267 InsertColUndo::~InsertColUndo()
269 if( !mbUndo )
271 Dispose( maColumns );
272 Dispose( maCells );
277 void InsertColUndo::Undo()
279 if( mxTable.is() )
281 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
282 mbUndo = false;
287 void InsertColUndo::Redo()
289 if( mxTable.is() )
291 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
292 mbUndo = true;
297 // class RemoveColUndo : public SdrUndoAction
300 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
301 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
302 ,mxTable( xTable )
303 ,mnIndex( nIndex )
304 ,mbUndo( true )
306 maColumns.swap( aNewCols );
307 maCells.swap( aCells );
311 RemoveColUndo::~RemoveColUndo()
313 if( mbUndo )
315 Dispose( maColumns );
316 Dispose( maCells );
321 void RemoveColUndo::Undo()
323 if( mxTable.is() )
325 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
326 mbUndo = false;
331 void RemoveColUndo::Redo()
333 if( mxTable.is() )
335 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
336 mbUndo = true;
341 // class TableColumnUndo : public SdrUndoAction
344 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
345 : SdrUndoAction(xCol->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
346 ,mxCol( xCol )
347 ,mbHasRedoData( false )
349 getData( maUndoData );
353 TableColumnUndo::~TableColumnUndo()
358 void TableColumnUndo::Undo()
360 if( !mbHasRedoData )
362 getData( maRedoData );
363 mbHasRedoData = true;
365 setData( maUndoData );
369 void TableColumnUndo::Redo()
371 setData( maRedoData );
375 bool TableColumnUndo::Merge( SfxUndoAction *pNextAction )
377 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
378 return pNext && pNext->mxCol == mxCol;
382 void TableColumnUndo::setData( const Data& rData )
384 mxCol->mnColumn = rData.mnColumn;
385 mxCol->mnWidth = rData.mnWidth;
386 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
387 mxCol->mbIsVisible = rData.mbIsVisible;
388 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
389 mxCol->maName = rData.maName;
391 // Trigger re-layout of the table.
392 mxCol->getModel()->setModified(true);
396 void TableColumnUndo::getData( Data& rData )
398 rData.mnColumn = mxCol->mnColumn;
399 rData.mnWidth = mxCol->mnWidth;
400 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
401 rData.mbIsVisible = mxCol->mbIsVisible;
402 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
403 rData.maName = mxCol->maName;
407 // class TableRowUndo : public SdrUndoAction
410 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
411 : SdrUndoAction(xRow->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
412 , mxRow( xRow )
413 , mbHasRedoData( false )
415 getData( maUndoData );
419 TableRowUndo::~TableRowUndo()
424 void TableRowUndo::Undo()
426 if( !mbHasRedoData )
428 getData( maRedoData );
429 mbHasRedoData = true;
431 setData( maUndoData );
435 void TableRowUndo::Redo()
437 setData( maRedoData );
441 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
443 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
444 return pNext && pNext->mxRow == mxRow;
448 void TableRowUndo::setData( const Data& rData )
450 mxRow->mnRow = rData.mnRow;
451 mxRow->mnHeight = rData.mnHeight;
452 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
453 mxRow->mbIsVisible = rData.mbIsVisible;
454 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
455 mxRow->maName = rData.maName;
457 // Trigger re-layout of the table.
458 mxRow->getModel()->setModified(true);
462 void TableRowUndo::getData( Data& rData )
464 rData.mnRow = mxRow->mnRow;
465 rData.mnHeight = mxRow->mnHeight;
466 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
467 rData.mbIsVisible = mxRow->mbIsVisible;
468 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
469 rData.maName = mxRow->maName;
473 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
474 : SdrUndoAction(rTableObj.getSdrModelFromSdrObject())
475 ,mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
476 ,mbHasRedoData(false)
478 getData( maUndoData );
481 void TableStyleUndo::Undo()
483 if( !mbHasRedoData )
485 getData( maRedoData );
486 mbHasRedoData = true;
488 setData( maUndoData );
491 void TableStyleUndo::Redo()
493 setData( maRedoData );
496 void TableStyleUndo::setData( const Data& rData )
498 SdrTableObj* pTableObj = mxObjRef.get();
499 if( pTableObj )
501 pTableObj->setTableStyle( rData.mxTableStyle );
502 pTableObj->setTableStyleSettings( rData.maSettings );
506 void TableStyleUndo::getData( Data& rData )
508 SdrTableObj* pTableObj = mxObjRef.get();
509 if( pTableObj )
511 rData.maSettings = pTableObj->getTableStyleSettings();
512 rData.mxTableStyle = pTableObj->getTableStyle();
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */