LibreOffice Module svx (master)  1
tableundo.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
22 #include <editeng/outlobj.hxx>
23 
24 #include <cell.hxx>
25 #include "tableundo.hxx"
26 #include <svx/svdotable.hxx>
27 #include "tablerow.hxx"
28 #include "tablecolumn.hxx"
29 
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::table;
33 
34 
35 namespace sdr { namespace table {
36 
38 : SdrUndoAction(xCell->GetObject().getSdrModelFromSdrObject())
39  ,mxObjRef( xObjRef )
40  ,mxCell( xCell )
41  ,mbUndo( true )
42 {
43  if( mxCell.is() && mxObjRef.is() )
44  {
46  mxObjRef->AddObjectUser( *this );
47  }
48 }
49 
51 {
52  if( mxObjRef.is() )
53  mxObjRef->RemoveObjectUser( *this );
54  dispose();
55 }
56 
58 {
59  mxCell.clear();
60  delete maUndoData.mpProperties;
61  maUndoData.mpProperties = nullptr;
62  delete maRedoData.mpProperties;
63  maRedoData.mpProperties = nullptr;
68 }
69 
71 {
72  dispose();
73 }
74 
76 {
77  if( mxCell.is() && mbUndo )
78  {
79  if( maRedoData.mpProperties == nullptr )
81 
83  mbUndo = false;
84  }
85 }
86 
88 {
89  if( mxCell.is() && !mbUndo )
90  {
92  mbUndo = true;
93  }
94 }
95 
96 bool CellUndo::Merge( SfxUndoAction *pNextAction )
97 {
98  CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
99  return pNext && pNext->mxCell.get() == mxCell.get();
100 }
101 
102 void CellUndo::setDataToCell( const Data& rData )
103 {
104  if( rData.mpProperties )
105  mxCell->mpProperties.reset(Cell::CloneProperties( rData.mpProperties, *mxObjRef.get(), *mxCell ));
106  else
107  mxCell->mpProperties.reset();
108 
109  if( rData.mpOutlinerParaObject )
110  mxCell->SetOutlinerParaObject( std::make_unique<OutlinerParaObject>(*rData.mpOutlinerParaObject) );
111  else
112  mxCell->RemoveOutlinerParaObject();
113 
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;
120 
121  if(mxObjRef.is())
122  {
123  // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
124  // and this is done best using ReformatText() for table objects
127  }
128 }
129 
131 {
132  if( mxObjRef.is() && mxCell.is() )
133  {
134  if( mxCell->mpProperties )
135  rData.mpProperties = mxCell->CloneProperties( *mxObjRef.get(), *mxCell);
136 
137  if( mxCell->GetOutlinerParaObject() )
138  rData.mpOutlinerParaObject = new OutlinerParaObject(*mxCell->GetOutlinerParaObject());
139  else
140  rData.mpOutlinerParaObject = nullptr;
141 
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;
148  }
149 }
150 
151 
152 // class InsertRowUndo : public SdrUndoAction
153 
154 
155 static void Dispose( RowVector& rRows )
156 {
157  for( auto& rpRow : rRows )
158  rpRow->dispose();
159 }
160 
161 
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 )
167 {
168  maRows.swap( aNewRows );
169 }
170 
171 
173 {
174  if( !mbUndo )
175  Dispose( maRows );
176 }
177 
178 
180 {
181  if( mxTable.is() )
182  {
183  mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
184  mbUndo = false;
185  }
186 }
187 
188 
190 {
191  if( mxTable.is() )
192  {
193  mxTable->UndoRemoveRows( mnIndex, maRows );
194  mbUndo = true;
195  }
196 }
197 
198 
199 // class RemoveRowUndo : public SdrUndoAction
200 
201 
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 )
207 {
208  maRows.swap( aRemovedRows );
209 }
210 
211 
213 {
214  if( mbUndo )
215  Dispose( maRows );
216 }
217 
218 
220 {
221  if( mxTable.is() )
222  {
223  mxTable->UndoRemoveRows( mnIndex, maRows );
224  mbUndo = false;
225  }
226 }
227 
228 
230 {
231  if( mxTable.is() )
232  {
233  mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
234  mbUndo = true;
235  }
236 }
237 
238 
239 // class InsertColUndo : public SdrUndoAction
240 
241 
242 static void Dispose( ColumnVector& rCols )
243 {
244  for( auto& rpCol : rCols )
245  rpCol->dispose();
246 }
247 
248 
249 static void Dispose( CellVector& rCells )
250 {
251  for( auto& rpCell : rCells )
252  rpCell->dispose();
253 }
254 
255 
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 )
261 {
262  maColumns.swap( aNewCols );
263  maCells.swap( aCells );
264 }
265 
266 
268 {
269  if( !mbUndo )
270  {
271  Dispose( maColumns );
272  Dispose( maCells );
273  }
274 }
275 
276 
278 {
279  if( mxTable.is() )
280  {
281  mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
282  mbUndo = false;
283  }
284 }
285 
286 
288 {
289  if( mxTable.is() )
290  {
291  mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
292  mbUndo = true;
293  }
294 }
295 
296 
297 // class RemoveColUndo : public SdrUndoAction
298 
299 
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 )
305 {
306  maColumns.swap( aNewCols );
307  maCells.swap( aCells );
308 }
309 
310 
312 {
313  if( mbUndo )
314  {
315  Dispose( maColumns );
316  Dispose( maCells );
317  }
318 }
319 
320 
322 {
323  if( mxTable.is() )
324  {
325  mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
326  mbUndo = false;
327  }
328 }
329 
330 
332 {
333  if( mxTable.is() )
334  {
335  mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
336  mbUndo = true;
337  }
338 }
339 
340 
341 // class TableColumnUndo : public SdrUndoAction
342 
343 
345 : SdrUndoAction(xCol->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
346  ,mxCol( xCol )
347  ,mbHasRedoData( false )
348 {
349  getData( maUndoData );
350 }
351 
352 
354 {
355 }
356 
357 
359 {
360  if( !mbHasRedoData )
361  {
362  getData( maRedoData );
363  mbHasRedoData = true;
364  }
365  setData( maUndoData );
366 }
367 
368 
370 {
371  setData( maRedoData );
372 }
373 
374 
376 {
377  TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
378  return pNext && pNext->mxCol == mxCol;
379 }
380 
381 
382 void TableColumnUndo::setData( const Data& rData )
383 {
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;
390 
391  // Trigger re-layout of the table.
392  mxCol->getModel()->setModified(true);
393 }
394 
395 
397 {
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;
404 }
405 
406 
407 // class TableRowUndo : public SdrUndoAction
408 
409 
411 : SdrUndoAction(xRow->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
412  , mxRow( xRow )
413  , mbHasRedoData( false )
414 {
415  getData( maUndoData );
416 }
417 
418 
420 {
421 }
422 
423 
425 {
426  if( !mbHasRedoData )
427  {
428  getData( maRedoData );
429  mbHasRedoData = true;
430  }
431  setData( maUndoData );
432 }
433 
434 
436 {
437  setData( maRedoData );
438 }
439 
440 
441 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
442 {
443  TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
444  return pNext && pNext->mxRow == mxRow;
445 }
446 
447 
448 void TableRowUndo::setData( const Data& rData )
449 {
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;
456 
457  // Trigger re-layout of the table.
458  mxRow->getModel()->setModified(true);
459  }
460 
461 
463 {
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;
470 }
471 
472 
474 : SdrUndoAction(rTableObj.getSdrModelFromSdrObject())
475  ,mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
476  ,mbHasRedoData(false)
477 {
478  getData( maUndoData );
479 }
480 
482 {
483  if( !mbHasRedoData )
484  {
485  getData( maRedoData );
486  mbHasRedoData = true;
487  }
488  setData( maUndoData );
489 }
490 
492 {
493  setData( maRedoData );
494 }
495 
496 void TableStyleUndo::setData( const Data& rData )
497 {
498  SdrTableObj* pTableObj = mxObjRef.get();
499  if( pTableObj )
500  {
501  pTableObj->setTableStyle( rData.mxTableStyle );
502  pTableObj->setTableStyleSettings( rData.maSettings );
503  }
504 }
505 
507 {
508  SdrTableObj* pTableObj = mxObjRef.get();
509  if( pTableObj )
510  {
511  rData.maSettings = pTableObj->getTableStyleSettings();
512  rData.mxTableStyle = pTableObj->getTableStyle();
513  }
514 }
515 
516 } }
517 
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:375
virtual ~RemoveColUndo() override
Definition: tableundo.cxx:311
std::vector< TableRowRef > RowVector
Definition: celltypes.hxx:39
void setTableStyle(const css::uno::Reference< css::container::XIndexAccess > &xAutoFormatStyle)
Definition: svdotable.cxx:1280
InsertColUndo(const TableModelRef &xTable, sal_Int32 nIndex, ColumnVector &aNewCols, CellVector &aCells)
Definition: tableundo.cxx:256
RemoveColUndo(const TableModelRef &xTable, sal_Int32 nIndex, ColumnVector &aNewCols, CellVector &aCells)
Definition: tableundo.cxx:300
void RemoveObjectUser(sdr::ObjectUser &rOldUser)
Definition: svdobj.cxx:253
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:441
TableStyleSettings maSettings
Definition: tableundo.hxx:244
sal_Int32 const mnIndex
Definition: tableundo.hxx:102
Abstract base class (ABC) for all UndoActions of DrawingEngine.
Definition: svdundo.hxx:53
InsertRowUndo(const TableModelRef &xTable, sal_Int32 nIndex, RowVector &aNewRows)
Definition: tableundo.cxx:162
virtual ~InsertRowUndo() override
Definition: tableundo.cxx:172
static void Dispose(RowVector &rRows)
Definition: tableundo.cxx:155
virtual void Undo() override
Definition: tableundo.cxx:179
virtual void Redo() override
Definition: tableundo.cxx:331
virtual void Redo() override
Definition: tableundo.cxx:189
virtual void ObjectInDestruction(const SdrObject &rObject) override
Definition: tableundo.cxx:70
void AddObjectUser(sdr::ObjectUser &rNewUser)
Definition: svdobj.cxx:248
virtual void Redo() override
Definition: tableundo.cxx:87
TableStyleUndo(const SdrTableObj &rTableObj)
Definition: tableundo.cxx:473
RemoveRowUndo(const TableModelRef &xTable, sal_Int32 nIndex, RowVector &aRemovedRows)
Definition: tableundo.cxx:202
const sdr::table::TableStyleSettings & getTableStyleSettings() const
Definition: svdotable.cxx:1136
virtual void Undo() override
Definition: tableundo.cxx:219
void setData(const Data &rData)
Definition: tableundo.cxx:382
sdr::properties::TextProperties * mpProperties
Definition: tableundo.hxx:58
std::vector< TableColumnRef > ColumnVector
Definition: celltypes.hxx:40
virtual void Undo() override
Definition: tableundo.cxx:75
virtual ~RemoveRowUndo() override
Definition: tableundo.cxx:212
virtual void Redo() override
Definition: tableundo.cxx:287
CellUndo(const tools::WeakReference< SdrObject > &xObjRef, const CellRef &xCell)
Definition: tableundo.cxx:37
virtual void Redo() override
Definition: tableundo.cxx:229
tools::WeakReference< SdrObject > mxObjRef
Definition: tableundo.hxx:83
void ActionChanged() const
Definition: svdobj.cxx:283
std::vector< CellRef > CellVector
Definition: celltypes.hxx:38
tools::WeakReference< SdrTableObj > mxObjRef
Definition: tableundo.hxx:240
OutlinerParaObject * mpOutlinerParaObject
Definition: tableundo.hxx:59
sal_Int32 const mnIndex
Definition: tableundo.hxx:154
virtual void Undo() override
Definition: tableundo.cxx:321
virtual void Undo() override
Definition: tableundo.cxx:481
Abstract DrawObject.
Definition: svdobj.hxx:312
virtual void Undo() override
Definition: tableundo.cxx:277
virtual void Redo() override
Definition: tableundo.cxx:369
void setTableStyleSettings(const sdr::table::TableStyleSettings &rStyle)
Definition: svdotable.cxx:1150
bool is() const
const css::uno::Reference< css::container::XIndexAccess > & getTableStyle() const
Definition: svdotable.cxx:1292
virtual ~TableRowUndo() override
Definition: tableundo.cxx:419
virtual void Undo() override
Definition: tableundo.cxx:424
virtual void NbcReformatText()
Definition: svdobj.cxx:1775
void getData(Data &rData)
Definition: tableundo.cxx:396
TableColumnUndo(const TableColumnRef &xCol)
Definition: tableundo.cxx:344
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:96
virtual void Redo() override
Definition: tableundo.cxx:435
reference_type * get() const
void setDataToCell(const Data &rData)
Definition: tableundo.cxx:102
void getData(Data &rData)
Definition: tableundo.cxx:506
void setData(const Data &rData)
Definition: tableundo.cxx:496
sal_Int32 const mnIndex
Definition: tableundo.hxx:136
TableRowUndo(const TableRowRef &xRow)
Definition: tableundo.cxx:410
void setData(const Data &rData)
Definition: tableundo.cxx:448
virtual ~CellUndo() override
Definition: tableundo.cxx:50
SVX_DLLPRIVATE sdr::properties::TextProperties * CloneProperties(SdrObject &rNewObj, Cell &rNewCell)
Definition: cell.cxx:854
void getData(Data &rData)
Definition: tableundo.cxx:462
void getDataFromCell(Data &rData)
Definition: tableundo.cxx:130
virtual void Undo() override
Definition: tableundo.cxx:358
css::uno::Reference< css::container::XIndexAccess > mxTableStyle
Definition: tableundo.hxx:245
int mnIndex
sal_Int32 const mnIndex
Definition: tableundo.hxx:119
virtual void Redo() override
Definition: tableundo.cxx:491
virtual ~InsertColUndo() override
Definition: tableundo.cxx:267
virtual ~TableColumnUndo() override
Definition: tableundo.cxx:353