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 .
21 #include <sdr/properties/cellproperties.hxx>
22 #include <editeng/outlobj.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())
42 if( mxCell
.is() && pObjRef
)
44 getDataFromCell( maUndoData
);
45 pObjRef
->AddObjectUser( *this );
51 if( auto pObj
= mxObjRef
.get() )
52 pObj
->RemoveObjectUser( *this );
56 void CellUndo::dispose()
59 maUndoData
.mxProperties
.reset();
60 maRedoData
.mxProperties
.reset();
61 maUndoData
.mpOutlinerParaObject
.reset();
62 maRedoData
.mpOutlinerParaObject
.reset();
65 void CellUndo::ObjectInDestruction(const SdrObject
& )
72 if( mxCell
.is() && mbUndo
)
74 if( !maRedoData
.mxProperties
)
75 getDataFromCell( maRedoData
);
77 setDataToCell( maUndoData
);
84 if( mxCell
.is() && !mbUndo
)
86 setDataToCell( maRedoData
);
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() ));
102 mxCell
->mpProperties
.reset();
104 if( rData
.mpOutlinerParaObject
)
105 mxCell
->SetOutlinerParaObject( *rData
.mpOutlinerParaObject
);
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()) )
130 if( mxCell
->mpProperties
)
131 rData
.mxProperties
.reset( mxCell
->CloneProperties( *mxObjRef
.get(), *mxCell
) );
133 if( mxCell
->GetOutlinerParaObject() )
134 rData
.mpOutlinerParaObject
= *mxCell
->GetOutlinerParaObject();
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
)
157 InsertRowUndo::InsertRowUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, RowVector
& aNewRows
)
158 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
163 maRows
.swap( aNewRows
);
167 InsertRowUndo::~InsertRowUndo()
174 void InsertRowUndo::Undo()
178 mxTable
->UndoInsertRows( mnIndex
, sal::static_int_cast
< sal_Int32
>( maRows
.size() ) );
184 void InsertRowUndo::Redo()
188 mxTable
->UndoRemoveRows( mnIndex
, maRows
);
194 // class RemoveRowUndo : public SdrUndoAction
197 RemoveRowUndo::RemoveRowUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, RowVector
& aRemovedRows
)
198 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
203 maRows
.swap( aRemovedRows
);
207 RemoveRowUndo::~RemoveRowUndo()
214 void RemoveRowUndo::Undo()
218 mxTable
->UndoRemoveRows( mnIndex
, maRows
);
224 void RemoveRowUndo::Redo()
228 mxTable
->UndoInsertRows( mnIndex
, sal::static_int_cast
< sal_Int32
>( maRows
.size() ) );
234 // class InsertColUndo : public SdrUndoAction
237 static void Dispose( ColumnVector
& rCols
)
239 for( auto& rpCol
: rCols
)
244 static void Dispose( CellVector
& rCells
)
246 for( auto& rpCell
: rCells
)
251 InsertColUndo::InsertColUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, ColumnVector
& aNewCols
, CellVector
& aCells
)
252 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
257 maColumns
.swap( aNewCols
);
258 maCells
.swap( aCells
);
262 InsertColUndo::~InsertColUndo()
266 Dispose( maColumns
);
272 void InsertColUndo::Undo()
276 mxTable
->UndoInsertColumns( mnIndex
, sal::static_int_cast
< sal_Int32
>( maColumns
.size() ) );
282 void InsertColUndo::Redo()
286 mxTable
->UndoRemoveColumns( mnIndex
, maColumns
, maCells
);
292 // class RemoveColUndo : public SdrUndoAction
295 RemoveColUndo::RemoveColUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, ColumnVector
& aNewCols
, CellVector
& aCells
)
296 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
301 maColumns
.swap( aNewCols
);
302 maCells
.swap( aCells
);
306 RemoveColUndo::~RemoveColUndo()
310 Dispose( maColumns
);
316 void RemoveColUndo::Undo()
320 mxTable
->UndoRemoveColumns( mnIndex
, maColumns
, maCells
);
326 void RemoveColUndo::Redo()
330 mxTable
->UndoInsertColumns( mnIndex
, sal::static_int_cast
< sal_Int32
>( maColumns
.size() ) );
336 // class TableColumnUndo : public SdrUndoAction
339 TableColumnUndo::TableColumnUndo( const TableColumnRef
& xCol
)
340 : SdrUndoAction(xCol
->mxTableModel
->getSdrTableObj()->getSdrModelFromSdrObject())
342 ,mbHasRedoData( false )
344 getData( maUndoData
);
348 TableColumnUndo::~TableColumnUndo()
353 void TableColumnUndo::Undo()
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())
408 , mbHasRedoData( false )
410 getData( maUndoData
);
414 TableRowUndo::~TableRowUndo()
419 void TableRowUndo::Undo()
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()
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();
496 pTableObj
->setTableStyle( rData
.mxTableStyle
);
497 pTableObj
->setTableStyleSettings( rData
.maSettings
);
501 void TableStyleUndo::getData( Data
& rData
)
503 rtl::Reference
<SdrTableObj
> pTableObj
= mxObjRef
.get();
506 rData
.maSettings
= pTableObj
->getTableStyleSettings();
507 rData
.mxTableStyle
= pTableObj
->getTableStyle();
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */