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
;
32 using namespace ::com::sun::star::table
;
35 namespace sdr::table
{
37 CellUndo::CellUndo( SdrObject
* pObjRef
, const CellRef
& xCell
)
38 : SdrUndoAction(xCell
->GetObject().getSdrModelFromSdrObject())
43 if( mxCell
.is() && pObjRef
)
45 getDataFromCell( maUndoData
);
46 pObjRef
->AddObjectUser( *this );
52 if( auto pObj
= mxObjRef
.get() )
53 pObj
->RemoveObjectUser( *this );
57 void CellUndo::dispose()
60 maUndoData
.mxProperties
.reset();
61 maRedoData
.mxProperties
.reset();
62 maUndoData
.mpOutlinerParaObject
.reset();
63 maRedoData
.mpOutlinerParaObject
.reset();
66 void CellUndo::ObjectInDestruction(const SdrObject
& )
73 if( mxCell
.is() && mbUndo
)
75 if( !maRedoData
.mxProperties
)
76 getDataFromCell( maRedoData
);
78 setDataToCell( maUndoData
);
85 if( mxCell
.is() && !mbUndo
)
87 setDataToCell( maRedoData
);
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() ));
103 mxCell
->mpProperties
.reset();
105 if( rData
.mpOutlinerParaObject
)
106 mxCell
->SetOutlinerParaObject( *rData
.mpOutlinerParaObject
);
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()) )
131 if( mxCell
->mpProperties
)
132 rData
.mxProperties
.reset( mxCell
->CloneProperties( *mxObjRef
.get(), *mxCell
) );
134 if( mxCell
->GetOutlinerParaObject() )
135 rData
.mpOutlinerParaObject
= *mxCell
->GetOutlinerParaObject();
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
)
158 InsertRowUndo::InsertRowUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, RowVector
& aNewRows
)
159 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
164 maRows
.swap( aNewRows
);
168 InsertRowUndo::~InsertRowUndo()
175 void InsertRowUndo::Undo()
179 mxTable
->UndoInsertRows( mnIndex
, sal::static_int_cast
< sal_Int32
>( maRows
.size() ) );
185 void InsertRowUndo::Redo()
189 mxTable
->UndoRemoveRows( mnIndex
, maRows
);
195 // class RemoveRowUndo : public SdrUndoAction
198 RemoveRowUndo::RemoveRowUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, RowVector
& aRemovedRows
)
199 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
204 maRows
.swap( aRemovedRows
);
208 RemoveRowUndo::~RemoveRowUndo()
215 void RemoveRowUndo::Undo()
219 mxTable
->UndoRemoveRows( mnIndex
, maRows
);
225 void RemoveRowUndo::Redo()
229 mxTable
->UndoInsertRows( mnIndex
, sal::static_int_cast
< sal_Int32
>( maRows
.size() ) );
235 // class InsertColUndo : public SdrUndoAction
238 static void Dispose( ColumnVector
& rCols
)
240 for( auto& rpCol
: rCols
)
245 static void Dispose( CellVector
& rCells
)
247 for( auto& rpCell
: rCells
)
252 InsertColUndo::InsertColUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, ColumnVector
& aNewCols
, CellVector
& aCells
)
253 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
258 maColumns
.swap( aNewCols
);
259 maCells
.swap( aCells
);
263 InsertColUndo::~InsertColUndo()
267 Dispose( maColumns
);
273 void InsertColUndo::Undo()
277 mxTable
->UndoInsertColumns( mnIndex
, sal::static_int_cast
< sal_Int32
>( maColumns
.size() ) );
283 void InsertColUndo::Redo()
287 mxTable
->UndoRemoveColumns( mnIndex
, maColumns
, maCells
);
293 // class RemoveColUndo : public SdrUndoAction
296 RemoveColUndo::RemoveColUndo( const TableModelRef
& xTable
, sal_Int32 nIndex
, ColumnVector
& aNewCols
, CellVector
& aCells
)
297 : SdrUndoAction(xTable
->getSdrTableObj()->getSdrModelFromSdrObject())
302 maColumns
.swap( aNewCols
);
303 maCells
.swap( aCells
);
307 RemoveColUndo::~RemoveColUndo()
311 Dispose( maColumns
);
317 void RemoveColUndo::Undo()
321 mxTable
->UndoRemoveColumns( mnIndex
, maColumns
, maCells
);
327 void RemoveColUndo::Redo()
331 mxTable
->UndoInsertColumns( mnIndex
, sal::static_int_cast
< sal_Int32
>( maColumns
.size() ) );
337 // class TableColumnUndo : public SdrUndoAction
340 TableColumnUndo::TableColumnUndo( const TableColumnRef
& xCol
)
341 : SdrUndoAction(xCol
->mxTableModel
->getSdrTableObj()->getSdrModelFromSdrObject())
343 ,mbHasRedoData( false )
345 getData( maUndoData
);
349 TableColumnUndo::~TableColumnUndo()
354 void TableColumnUndo::Undo()
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())
409 , mbHasRedoData( false )
411 getData( maUndoData
);
415 TableRowUndo::~TableRowUndo()
420 void TableRowUndo::Undo()
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()
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();
497 pTableObj
->setTableStyle( rData
.mxTableStyle
);
498 pTableObj
->setTableStyleSettings( rData
.maSettings
);
502 void TableStyleUndo::getData( Data
& rData
)
504 rtl::Reference
<SdrTableObj
> pTableObj
= mxObjRef
.get();
507 rData
.maSettings
= pTableObj
->getTableStyleSettings();
508 rData
.mxTableStyle
= pTableObj
->getTableStyle();
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */