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::table {
36 
37 CellUndo::CellUndo( SdrObject* pObjRef, const CellRef& xCell )
38 : SdrUndoAction(xCell->GetObject().getSdrModelFromSdrObject())
39  ,mxObjRef( pObjRef )
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;
66 }
67 
69 {
70  dispose();
71 }
72 
74 {
75  if( mxCell.is() && mbUndo )
76  {
77  if( maRedoData.mpProperties == nullptr )
79 
81  mbUndo = false;
82  }
83 }
84 
86 {
87  if( mxCell.is() && !mbUndo )
88  {
90  mbUndo = true;
91  }
92 }
93 
94 bool CellUndo::Merge( SfxUndoAction *pNextAction )
95 {
96  CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
97  return pNext && pNext->mxCell.get() == mxCell.get();
98 }
99 
100 void CellUndo::setDataToCell( const Data& rData )
101 {
102  if( rData.mpProperties )
103  mxCell->mpProperties.reset(Cell::CloneProperties( rData.mpProperties, *mxObjRef, *mxCell ));
104  else
105  mxCell->mpProperties.reset();
106 
107  if( rData.mpOutlinerParaObject )
108  mxCell->SetOutlinerParaObject( *rData.mpOutlinerParaObject );
109  else
110  mxCell->RemoveOutlinerParaObject();
111 
112  mxCell->msFormula = rData.msFormula;
113  mxCell->mfValue = rData.mfValue;
114  mxCell->mnError = rData.mnError;
115  mxCell->mbMerged = rData.mbMerged;
116  mxCell->mnRowSpan = rData.mnRowSpan;
117  mxCell->mnColSpan = rData.mnColSpan;
118 
119  if(mxObjRef.is())
120  {
121  // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
122  // and this is done best using ReformatText() for table objects
125  }
126 }
127 
129 {
130  if( !(mxObjRef.is() && mxCell.is()) )
131  return;
132 
133  if( mxCell->mpProperties )
134  rData.mpProperties = mxCell->CloneProperties( *mxObjRef, *mxCell);
135 
136  if( mxCell->GetOutlinerParaObject() )
137  rData.mpOutlinerParaObject = *mxCell->GetOutlinerParaObject();
138  else
139  rData.mpOutlinerParaObject.reset();
140 
141  rData.msFormula = mxCell->msFormula;
142  rData.mfValue = mxCell->mfValue;
143  rData.mnError = mxCell->mnError;
144  rData.mbMerged = mxCell->mbMerged;
145  rData.mnRowSpan = mxCell->mnRowSpan;
146  rData.mnColSpan = mxCell->mnColSpan;
147 }
148 
149 
150 // class InsertRowUndo : public SdrUndoAction
151 
152 
153 static void Dispose( RowVector& rRows )
154 {
155  for( auto& rpRow : rRows )
156  rpRow->dispose();
157 }
158 
159 
160 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
161 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
162  ,mxTable( xTable )
163  ,mnIndex( nIndex )
164  ,mbUndo( true )
165 {
166  maRows.swap( aNewRows );
167 }
168 
169 
171 {
172  if( !mbUndo )
173  Dispose( maRows );
174 }
175 
176 
178 {
179  if( mxTable.is() )
180  {
181  mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
182  mbUndo = false;
183  }
184 }
185 
186 
188 {
189  if( mxTable.is() )
190  {
191  mxTable->UndoRemoveRows( mnIndex, maRows );
192  mbUndo = true;
193  }
194 }
195 
196 
197 // class RemoveRowUndo : public SdrUndoAction
198 
199 
200 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
201 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
202  ,mxTable( xTable )
203  ,mnIndex( nIndex )
204  ,mbUndo( true )
205 {
206  maRows.swap( aRemovedRows );
207 }
208 
209 
211 {
212  if( mbUndo )
213  Dispose( maRows );
214 }
215 
216 
218 {
219  if( mxTable.is() )
220  {
221  mxTable->UndoRemoveRows( mnIndex, maRows );
222  mbUndo = false;
223  }
224 }
225 
226 
228 {
229  if( mxTable.is() )
230  {
231  mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
232  mbUndo = true;
233  }
234 }
235 
236 
237 // class InsertColUndo : public SdrUndoAction
238 
239 
240 static void Dispose( ColumnVector& rCols )
241 {
242  for( auto& rpCol : rCols )
243  rpCol->dispose();
244 }
245 
246 
247 static void Dispose( CellVector& rCells )
248 {
249  for( auto& rpCell : rCells )
250  rpCell->dispose();
251 }
252 
253 
254 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
255 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
256  ,mxTable( xTable )
257  ,mnIndex( nIndex )
258  ,mbUndo( true )
259 {
260  maColumns.swap( aNewCols );
261  maCells.swap( aCells );
262 }
263 
264 
266 {
267  if( !mbUndo )
268  {
269  Dispose( maColumns );
270  Dispose( maCells );
271  }
272 }
273 
274 
276 {
277  if( mxTable.is() )
278  {
279  mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
280  mbUndo = false;
281  }
282 }
283 
284 
286 {
287  if( mxTable.is() )
288  {
289  mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
290  mbUndo = true;
291  }
292 }
293 
294 
295 // class RemoveColUndo : public SdrUndoAction
296 
297 
298 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
299 : SdrUndoAction(xTable->getSdrTableObj()->getSdrModelFromSdrObject())
300  ,mxTable( xTable )
301  ,mnIndex( nIndex )
302  ,mbUndo( true )
303 {
304  maColumns.swap( aNewCols );
305  maCells.swap( aCells );
306 }
307 
308 
310 {
311  if( mbUndo )
312  {
313  Dispose( maColumns );
314  Dispose( maCells );
315  }
316 }
317 
318 
320 {
321  if( mxTable.is() )
322  {
323  mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
324  mbUndo = false;
325  }
326 }
327 
328 
330 {
331  if( mxTable.is() )
332  {
333  mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
334  mbUndo = true;
335  }
336 }
337 
338 
339 // class TableColumnUndo : public SdrUndoAction
340 
341 
343 : SdrUndoAction(xCol->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
344  ,mxCol( xCol )
345  ,mbHasRedoData( false )
346 {
347  getData( maUndoData );
348 }
349 
350 
352 {
353 }
354 
355 
357 {
358  if( !mbHasRedoData )
359  {
360  getData( maRedoData );
361  mbHasRedoData = true;
362  }
363  setData( maUndoData );
364 }
365 
366 
368 {
369  setData( maRedoData );
370 }
371 
372 
374 {
375  TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
376  return pNext && pNext->mxCol == mxCol;
377 }
378 
379 
380 void TableColumnUndo::setData( const Data& rData )
381 {
382  mxCol->mnColumn = rData.mnColumn;
383  mxCol->mnWidth = rData.mnWidth;
384  mxCol->mbOptimalWidth = rData.mbOptimalWidth;
385  mxCol->mbIsVisible = rData.mbIsVisible;
386  mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
387  mxCol->maName = rData.maName;
388 
389  // Trigger re-layout of the table.
390  mxCol->getModel()->setModified(true);
391 }
392 
393 
395 {
396  rData.mnColumn = mxCol->mnColumn;
397  rData.mnWidth = mxCol->mnWidth;
398  rData.mbOptimalWidth = mxCol->mbOptimalWidth;
399  rData.mbIsVisible = mxCol->mbIsVisible;
400  rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
401  rData.maName = mxCol->maName;
402 }
403 
404 
405 // class TableRowUndo : public SdrUndoAction
406 
407 
409 : SdrUndoAction(xRow->mxTableModel->getSdrTableObj()->getSdrModelFromSdrObject())
410  , mxRow( xRow )
411  , mbHasRedoData( false )
412 {
413  getData( maUndoData );
414 }
415 
416 
418 {
419 }
420 
421 
423 {
424  if( !mbHasRedoData )
425  {
426  getData( maRedoData );
427  mbHasRedoData = true;
428  }
429  setData( maUndoData );
430 }
431 
432 
434 {
435  setData( maRedoData );
436 }
437 
438 
439 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
440 {
441  TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
442  return pNext && pNext->mxRow == mxRow;
443 }
444 
445 
446 void TableRowUndo::setData( const Data& rData )
447 {
448  mxRow->mnRow = rData.mnRow;
449  mxRow->mnHeight = rData.mnHeight;
450  mxRow->mbOptimalHeight = rData.mbOptimalHeight;
451  mxRow->mbIsVisible = rData.mbIsVisible;
452  mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
453  mxRow->maName = rData.maName;
454 
455  // Trigger re-layout of the table.
456  mxRow->getModel()->setModified(true);
457  }
458 
459 
461 {
462  rData.mnRow = mxRow->mnRow;
463  rData.mnHeight = mxRow->mnHeight;
464  rData.mbOptimalHeight = mxRow->mbOptimalHeight;
465  rData.mbIsVisible = mxRow->mbIsVisible;
466  rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
467  rData.maName = mxRow->maName;
468 }
469 
470 
472 : SdrUndoAction(rTableObj.getSdrModelFromSdrObject())
473  ,mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
474  ,mbHasRedoData(false)
475 {
476  getData( maUndoData );
477 }
478 
480 {
481  if( !mbHasRedoData )
482  {
483  getData( maRedoData );
484  mbHasRedoData = true;
485  }
486  setData( maUndoData );
487 }
488 
490 {
491  setData( maRedoData );
492 }
493 
494 void TableStyleUndo::setData( const Data& rData )
495 {
496  SdrTableObj* pTableObj = mxObjRef.get();
497  if( pTableObj )
498  {
499  pTableObj->setTableStyle( rData.mxTableStyle );
500  pTableObj->setTableStyleSettings( rData.maSettings );
501  }
502 }
503 
505 {
506  SdrTableObj* pTableObj = mxObjRef.get();
507  if( pTableObj )
508  {
509  rData.maSettings = pTableObj->getTableStyleSettings();
510  rData.mxTableStyle = pTableObj->getTableStyle();
511  }
512 }
513 
514 }
515 
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:373
std::vector< CellRef > CellVector
Definition: celltypes.hxx:38
virtual ~RemoveColUndo() override
Definition: tableundo.cxx:309
void setTableStyle(const css::uno::Reference< css::container::XIndexAccess > &xAutoFormatStyle)
Definition: svdotable.cxx:1307
InsertColUndo(const TableModelRef &xTable, sal_Int32 nIndex, ColumnVector &aNewCols, CellVector &aCells)
Definition: tableundo.cxx:254
RemoveColUndo(const TableModelRef &xTable, sal_Int32 nIndex, ColumnVector &aNewCols, CellVector &aCells)
Definition: tableundo.cxx:298
void RemoveObjectUser(sdr::ObjectUser &rOldUser)
Definition: svdobj.cxx:233
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:439
TableStyleSettings maSettings
Definition: tableundo.hxx:242
Abstract base class (ABC) for all UndoActions of DrawingEngine.
Definition: svdundo.hxx:56
InsertRowUndo(const TableModelRef &xTable, sal_Int32 nIndex, RowVector &aNewRows)
Definition: tableundo.cxx:160
virtual ~InsertRowUndo() override
Definition: tableundo.cxx:170
static void Dispose(RowVector &rRows)
Definition: tableundo.cxx:153
exports com.sun.star. table
virtual void Undo() override
Definition: tableundo.cxx:177
virtual void Redo() override
Definition: tableundo.cxx:329
virtual void Redo() override
Definition: tableundo.cxx:187
std::optional< OutlinerParaObject > mpOutlinerParaObject
Definition: tableundo.hxx:58
virtual void ObjectInDestruction(const SdrObject &rObject) override
Definition: tableundo.cxx:68
void AddObjectUser(sdr::ObjectUser &rNewUser)
Definition: svdobj.cxx:228
virtual void Redo() override
Definition: tableundo.cxx:85
TableStyleUndo(const SdrTableObj &rTableObj)
Definition: tableundo.cxx:471
RemoveRowUndo(const TableModelRef &xTable, sal_Int32 nIndex, RowVector &aRemovedRows)
Definition: tableundo.cxx:200
const sdr::table::TableStyleSettings & getTableStyleSettings() const
Definition: svdotable.cxx:1163
virtual void Undo() override
Definition: tableundo.cxx:217
void setData(const Data &rData)
Definition: tableundo.cxx:380
sdr::properties::TextProperties * mpProperties
Definition: tableundo.hxx:57
virtual void Undo() override
Definition: tableundo.cxx:73
virtual ~RemoveRowUndo() override
Definition: tableundo.cxx:210
std::vector< TableColumnRef > ColumnVector
Definition: celltypes.hxx:40
virtual void Redo() override
Definition: tableundo.cxx:285
virtual void Redo() override
Definition: tableundo.cxx:227
tools::WeakReference< SdrObject > mxObjRef
Definition: tableundo.hxx:81
std::vector< TableRowRef > RowVector
Definition: celltypes.hxx:39
void ActionChanged() const
Definition: svdobj.cxx:263
tools::WeakReference< SdrTableObj > mxObjRef
Definition: tableundo.hxx:238
virtual void Undo() override
Definition: tableundo.cxx:319
virtual void Undo() override
Definition: tableundo.cxx:479
Abstract DrawObject.
Definition: svdobj.hxx:259
virtual void Undo() override
Definition: tableundo.cxx:275
virtual void Redo() override
Definition: tableundo.cxx:367
void setTableStyleSettings(const sdr::table::TableStyleSettings &rStyle)
Definition: svdotable.cxx:1177
bool is() const
const css::uno::Reference< css::container::XIndexAccess > & getTableStyle() const
Definition: svdotable.cxx:1319
virtual ~TableRowUndo() override
Definition: tableundo.cxx:417
virtual void Undo() override
Definition: tableundo.cxx:422
virtual void NbcReformatText()
Definition: svdobj.cxx:1842
void getData(Data &rData)
Definition: tableundo.cxx:394
TableColumnUndo(const TableColumnRef &xCol)
Definition: tableundo.cxx:342
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: tableundo.cxx:94
virtual void Redo() override
Definition: tableundo.cxx:433
void setDataToCell(const Data &rData)
Definition: tableundo.cxx:100
void getData(Data &rData)
Definition: tableundo.cxx:504
void setData(const Data &rData)
Definition: tableundo.cxx:494
TableRowUndo(const TableRowRef &xRow)
Definition: tableundo.cxx:408
void setData(const Data &rData)
Definition: tableundo.cxx:446
virtual ~CellUndo() override
Definition: tableundo.cxx:50
CellUndo(SdrObject *pObj, const CellRef &xCell)
Definition: tableundo.cxx:37
void getData(Data &rData)
Definition: tableundo.cxx:460
void getDataFromCell(Data &rData)
Definition: tableundo.cxx:128
virtual void Undo() override
Definition: tableundo.cxx:356
css::uno::Reference< css::container::XIndexAccess > mxTableStyle
Definition: tableundo.hxx:243
int mnIndex
virtual void Redo() override
Definition: tableundo.cxx:489
virtual ~InsertColUndo() override
Definition: tableundo.cxx:265
virtual ~TableColumnUndo() override
Definition: tableundo.cxx:351