Get the style color and number just once
[LibreOffice.git] / svx / source / table / tableundo.cxx
blob754c06482759a6cc7e31ef7a85e275bd081cea24
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;
34 namespace sdr::table {
36 CellUndo::CellUndo( SdrObject* pObjRef, const CellRef& xCell )
37 : SdrUndoAction(xCell->GetObject().getSdrModelFromSdrObject())
38 ,mxObjRef( pObjRef )
39 ,mxCell( xCell )
40 ,mbUndo( true )
42 if( mxCell.is() && pObjRef )
44 getDataFromCell( maUndoData );
45 pObjRef->AddObjectUser( *this );
49 CellUndo::~CellUndo()
51 if( auto pObj = mxObjRef.get() )
52 pObj->RemoveObjectUser( *this );
53 dispose();
56 void CellUndo::dispose()
58 mxCell.clear();
59 maUndoData.mxProperties.reset();
60 maRedoData.mxProperties.reset();
61 maUndoData.mpOutlinerParaObject.reset();
62 maRedoData.mpOutlinerParaObject.reset();
65 void CellUndo::ObjectInDestruction(const SdrObject& )
67 dispose();
70 void CellUndo::Undo()
72 if( mxCell.is() && mbUndo )
74 if( !maRedoData.mxProperties )
75 getDataFromCell( maRedoData );
77 setDataToCell( maUndoData );
78 mbUndo = false;
82 void CellUndo::Redo()
84 if( mxCell.is() && !mbUndo )
86 setDataToCell( maRedoData );
87 mbUndo = true;
91 bool CellUndo::Merge( SfxUndoAction *pNextAction )
93 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
94 return pNext && pNext->mxCell.get() == mxCell.get();
97 void CellUndo::setDataToCell( const Data& rData )
99 if( rData.mxProperties )
100 mxCell->mpProperties.reset(new properties::CellProperties( *rData.mxProperties, *mxObjRef.get(), mxCell.get() ));
101 else
102 mxCell->mpProperties.reset();
104 if( rData.mpOutlinerParaObject )
105 mxCell->SetOutlinerParaObject( *rData.mpOutlinerParaObject );
106 else
107 mxCell->RemoveOutlinerParaObject();
109 mxCell->msFormula = rData.msFormula;
110 mxCell->mfValue = rData.mfValue;
111 mxCell->mnError = rData.mnError;
112 mxCell->mbMerged = rData.mbMerged;
113 mxCell->mnRowSpan = rData.mnRowSpan;
114 mxCell->mnColSpan = rData.mnColSpan;
116 if(auto pObj = mxObjRef.get())
118 // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
119 // and this is done best using ReformatText() for table objects
120 pObj->ActionChanged();
121 pObj->NbcReformatText();
125 void CellUndo::getDataFromCell( Data& rData )
127 if( !(mxObjRef.get().is() && mxCell.is()) )
128 return;
130 if( mxCell->mpProperties )
131 rData.mxProperties.reset( mxCell->CloneProperties( *mxObjRef.get(), *mxCell) );
133 if( mxCell->GetOutlinerParaObject() )
134 rData.mpOutlinerParaObject = *mxCell->GetOutlinerParaObject();
135 else
136 rData.mpOutlinerParaObject.reset();
138 rData.msFormula = mxCell->msFormula;
139 rData.mfValue = mxCell->mfValue;
140 rData.mnError = mxCell->mnError;
141 rData.mbMerged = mxCell->mbMerged;
142 rData.mnRowSpan = mxCell->mnRowSpan;
143 rData.mnColSpan = mxCell->mnColSpan;
147 // class InsertRowUndo : public SdrUndoAction
150 static void Dispose( RowVector& rRows )
152 for( auto& rpRow : rRows )
153 rpRow->dispose();
157 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
158 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
159 ,mxTable( xTable )
160 ,mnIndex( nIndex )
161 ,mbUndo( true )
163 maRows.swap( aNewRows );
167 InsertRowUndo::~InsertRowUndo()
169 if( !mbUndo )
170 Dispose( maRows );
174 void InsertRowUndo::Undo()
176 if( mxTable.is() )
178 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
179 mbUndo = false;
184 void InsertRowUndo::Redo()
186 if( mxTable.is() )
188 mxTable->UndoRemoveRows( mnIndex, maRows );
189 mbUndo = true;
194 // class RemoveRowUndo : public SdrUndoAction
197 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
198 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
199 ,mxTable( xTable )
200 ,mnIndex( nIndex )
201 ,mbUndo( true )
203 maRows.swap( aRemovedRows );
207 RemoveRowUndo::~RemoveRowUndo()
209 if( mbUndo )
210 Dispose( maRows );
214 void RemoveRowUndo::Undo()
216 if( mxTable.is() )
218 mxTable->UndoRemoveRows( mnIndex, maRows );
219 mbUndo = false;
224 void RemoveRowUndo::Redo()
226 if( mxTable.is() )
228 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
229 mbUndo = true;
234 // class InsertColUndo : public SdrUndoAction
237 static void Dispose( ColumnVector& rCols )
239 for( auto& rpCol : rCols )
240 rpCol->dispose();
244 static void Dispose( CellVector& rCells )
246 for( auto& rpCell : rCells )
247 rpCell->dispose();
251 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
252 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
253 ,mxTable( xTable )
254 ,mnIndex( nIndex )
255 ,mbUndo( true )
257 maColumns.swap( aNewCols );
258 maCells.swap( aCells );
262 InsertColUndo::~InsertColUndo()
264 if( !mbUndo )
266 Dispose( maColumns );
267 Dispose( maCells );
272 void InsertColUndo::Undo()
274 if( mxTable.is() )
276 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
277 mbUndo = false;
282 void InsertColUndo::Redo()
284 if( mxTable.is() )
286 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
287 mbUndo = true;
292 // class RemoveColUndo : public SdrUndoAction
295 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
296 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
297 ,mxTable( xTable )
298 ,mnIndex( nIndex )
299 ,mbUndo( true )
301 maColumns.swap( aNewCols );
302 maCells.swap( aCells );
306 RemoveColUndo::~RemoveColUndo()
308 if( mbUndo )
310 Dispose( maColumns );
311 Dispose( maCells );
316 void RemoveColUndo::Undo()
318 if( mxTable.is() )
320 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
321 mbUndo = false;
326 void RemoveColUndo::Redo()
328 if( mxTable.is() )
330 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
331 mbUndo = true;
336 // class TableColumnUndo : public SdrUndoAction
339 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
340 : SdrUndoAction(xCol->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
341 ,mxCol( xCol )
342 ,mbHasRedoData( false )
344 getData( maUndoData );
348 TableColumnUndo::~TableColumnUndo()
353 void TableColumnUndo::Undo()
355 if( !mbHasRedoData )
357 getData( maRedoData );
358 mbHasRedoData = true;
360 setData( maUndoData );
364 void TableColumnUndo::Redo()
366 setData( maRedoData );
370 bool TableColumnUndo::Merge( SfxUndoAction *pNextAction )
372 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
373 return pNext && pNext->mxCol == mxCol;
377 void TableColumnUndo::setData( const Data& rData )
379 mxCol->mnColumn = rData.mnColumn;
380 mxCol->mnWidth = rData.mnWidth;
381 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
382 mxCol->mbIsVisible = rData.mbIsVisible;
383 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
384 mxCol->maName = rData.maName;
386 // Trigger re-layout of the table.
387 mxCol->getModel()->setModified(true);
391 void TableColumnUndo::getData( Data& rData )
393 rData.mnColumn = mxCol->mnColumn;
394 rData.mnWidth = mxCol->mnWidth;
395 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
396 rData.mbIsVisible = mxCol->mbIsVisible;
397 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
398 rData.maName = mxCol->maName;
402 // class TableRowUndo : public SdrUndoAction
405 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
406 : SdrUndoAction(xRow->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
407 , mxRow( xRow )
408 , mbHasRedoData( false )
410 getData( maUndoData );
414 TableRowUndo::~TableRowUndo()
419 void TableRowUndo::Undo()
421 if( !mbHasRedoData )
423 getData( maRedoData );
424 mbHasRedoData = true;
426 setData( maUndoData );
430 void TableRowUndo::Redo()
432 setData( maRedoData );
436 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
438 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
439 return pNext && pNext->mxRow == mxRow;
443 void TableRowUndo::setData( const Data& rData )
445 mxRow->mnRow = rData.mnRow;
446 mxRow->mnHeight = rData.mnHeight;
447 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
448 mxRow->mbIsVisible = rData.mbIsVisible;
449 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
450 mxRow->maName = rData.maName;
452 // Trigger re-layout of the table.
453 mxRow->getModel()->setModified(true);
457 void TableRowUndo::getData( Data& rData )
459 rData.mnRow = mxRow->mnRow;
460 rData.mnHeight = mxRow->mnHeight;
461 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
462 rData.mbIsVisible = mxRow->mbIsVisible;
463 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
464 rData.maName = mxRow->maName;
468 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
469 : SdrUndoAction(rTableObj.getSdrModelFromSdrObject())
470 ,mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
471 ,mbHasRedoData(false)
473 getData( maUndoData );
476 void TableStyleUndo::Undo()
478 if( !mbHasRedoData )
480 getData( maRedoData );
481 mbHasRedoData = true;
483 setData( maUndoData );
486 void TableStyleUndo::Redo()
488 setData( maRedoData );
491 void TableStyleUndo::setData( const Data& rData )
493 rtl::Reference<SdrTableObj> pTableObj = mxObjRef.get();
494 if( pTableObj )
496 pTableObj->setTableStyle( rData.mxTableStyle );
497 pTableObj->setTableStyleSettings( rData.maSettings );
501 void TableStyleUndo::getData( Data& rData )
503 rtl::Reference<SdrTableObj> pTableObj = mxObjRef.get();
504 if( pTableObj )
506 rData.maSettings = pTableObj->getTableStyleSettings();
507 rData.mxTableStyle = pTableObj->getTableStyle();
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */