LibreOffice Module svx (master)  1
tablerow.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 
21 #include <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 
24 #include <cell.hxx>
25 #include "tablerow.hxx"
26 #include "tableundo.hxx"
27 #include <svx/svdmodel.hxx>
28 #include <svx/svdotable.hxx>
29 
30 
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::container;
34 using namespace ::com::sun::star::table;
35 using namespace ::com::sun::star::beans;
36 
37 
38 namespace sdr::table {
39 
40 const sal_Int32 Property_Height = 0;
41 const sal_Int32 Property_OptimalHeight = 1;
42 const sal_Int32 Property_IsVisible = 2;
43 const sal_Int32 Property_IsStartOfNewPage = 3;
44 
45 TableRow::TableRow( const TableModelRef& xTableModel, sal_Int32 nRow, sal_Int32 nColumns )
46 : TableRowBase( getStaticPropertySetInfo() )
47 , mxTableModel( xTableModel )
48 , mnRow( nRow )
49 , mnHeight( 0 )
50 , mbOptimalHeight( true )
51 , mbIsVisible( true )
52 , mbIsStartOfNewPage( false )
53 {
54  if( nColumns < 20 )
55  maCells.reserve( 20 );
56 
57  if( nColumns )
58  {
59  maCells.resize( nColumns );
60  while( nColumns-- )
61  maCells[ nColumns ] = mxTableModel->createCell();
62  }
63 }
64 
65 
67 {
68 }
69 
70 
72 {
73  mxTableModel.clear();
74  if( !maCells.empty() )
75  {
76  for( auto& rpCell : maCells )
77  rpCell->dispose();
78  CellVector().swap(maCells);
79  }
80 }
81 
82 
84 {
85  if( !mxTableModel.is() )
86  throw DisposedException();
87 }
88 
89 
91 {
92  mnHeight = r.mnHeight;
96  maName = r.maName;
97  mnRow = r.mnRow;
98 
99  return *this;
100 }
101 
102 
103 void TableRow::insertColumns( sal_Int32 nIndex, sal_Int32 nCount, CellVector::iterator const * pIter /* = 0 */ )
104 {
105  throwIfDisposed();
106  if( nCount )
107  {
108  if( nIndex >= static_cast< sal_Int32 >( maCells.size() ) )
109  nIndex = static_cast< sal_Int32 >( maCells.size() );
110  if ( pIter )
111  maCells.insert( maCells.begin() + nIndex, *pIter, (*pIter) + nCount );
112  else
113  {
114  maCells.reserve( maCells.size() + nCount );
115  for ( sal_Int32 i = 0; i < nCount; i++ )
116  maCells.insert( maCells.begin() + nIndex + i, mxTableModel->createCell() );
117  }
118  }
119 }
120 
121 
122 void TableRow::removeColumns( sal_Int32 nIndex, sal_Int32 nCount )
123 {
124  throwIfDisposed();
125  if( (nCount >= 0) && ( nIndex >= 0) )
126  {
127  if( (nIndex + nCount) < static_cast< sal_Int32 >( maCells.size() ) )
128  {
129  CellVector::iterator aBegin( maCells.begin() );
130  std::advance(aBegin, nIndex);
131 
132  if( nCount > 1 )
133  {
134  CellVector::iterator aEnd( aBegin );
135  while( nCount-- && (aEnd != maCells.end()) )
136  ++aEnd;
137  maCells.erase( aBegin, aEnd );
138  }
139  else
140  {
141  maCells.erase( aBegin );
142  }
143  }
144  else
145  {
146  maCells.resize( nIndex );
147  }
148  }
149 }
150 
152 {
153  return mxTableModel;
154 }
155 
156 // XCellRange
157 
158 
159 Reference< XCell > SAL_CALL TableRow::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow )
160 {
161  throwIfDisposed();
162  if( nRow != 0 )
163  throw IndexOutOfBoundsException();
164 
165  return mxTableModel->getCellByPosition( nColumn, mnRow );
166 }
167 
168 
169 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
170 {
171  throwIfDisposed();
172  if( (nLeft >= 0 ) && (nTop == 0) && (nRight >= nLeft) && (nBottom == 0) )
173  {
174  return mxTableModel->getCellRangeByPosition( nLeft, mnRow, nRight, mnRow );
175  }
176  throw IndexOutOfBoundsException();
177 }
178 
179 
180 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByName( const OUString& /*aRange*/ )
181 {
182  throwIfDisposed();
183  return Reference< XCellRange >();
184 }
185 
186 
187 // XNamed
188 
189 
190 OUString SAL_CALL TableRow::getName()
191 {
192  return maName;
193 }
194 
195 
196 void SAL_CALL TableRow::setName( const OUString& aName )
197 {
198  maName = aName;
199 }
200 
201 
202 // XFastPropertySet
203 
204 
205 void SAL_CALL TableRow::setFastPropertyValue( sal_Int32 nHandle, const Any& aValue )
206 {
207  if(!mxTableModel.is() || nullptr == mxTableModel->getSdrTableObj())
208  return;
209 
210  SdrTableObj& rTableObj(*mxTableModel->getSdrTableObj());
211  SdrModel& rModel(rTableObj.getSdrModelFromSdrObject());
212  bool bOk(false);
213  bool bChange(false);
214  std::unique_ptr<TableRowUndo> pUndo;
215  const bool bUndo(rTableObj.IsInserted() && rModel.IsUndoEnabled());
216 
217  if( bUndo )
218  {
219  TableRowRef xThis( this );
220  pUndo.reset(new TableRowUndo( xThis ));
221  }
222 
223  switch( nHandle )
224  {
225  case Property_Height:
226  {
227  sal_Int32 nHeight = mnHeight;
228  bOk = aValue >>= nHeight;
229  if( bOk && (mnHeight != nHeight) )
230  {
231  mnHeight = nHeight;
232  mbOptimalHeight = mnHeight == 0;
233  bChange = true;
234  }
235  break;
236  }
237 
238  case Property_OptimalHeight:
239  {
240  bool bOptimalHeight = mbOptimalHeight;
241  bOk = aValue >>= bOptimalHeight;
242  if( bOk && (mbOptimalHeight != bOptimalHeight) )
243  {
244  mbOptimalHeight = bOptimalHeight;
245  if( bOptimalHeight )
246  mnHeight = 0;
247  bChange = true;
248  }
249  break;
250  }
251  case Property_IsVisible:
252  {
253  bool bIsVisible = mbIsVisible;
254  bOk = aValue >>= bIsVisible;
255  if( bOk && (mbIsVisible != bIsVisible) )
256  {
257  mbIsVisible = bIsVisible;
258  bChange = true;
259  }
260  break;
261  }
262 
263  case Property_IsStartOfNewPage:
264  {
265  bool bIsStartOfNewPage = mbIsStartOfNewPage;
266  bOk = aValue >>= bIsStartOfNewPage;
267  if( bOk && (mbIsStartOfNewPage != bIsStartOfNewPage) )
268  {
269  mbIsStartOfNewPage = bIsStartOfNewPage;
270  bChange = true;
271  }
272  break;
273  }
274  default:
275  throw UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
276  }
277 
278  if( !bOk )
279  {
280  throw IllegalArgumentException();
281  }
282 
283  if( bChange )
284  {
285  if( pUndo )
286  {
287  rModel.AddUndo( std::move(pUndo) );
288  }
289  mxTableModel->setModified(true);
290  }
291 }
292 
293 
294 Any SAL_CALL TableRow::getFastPropertyValue( sal_Int32 nHandle )
295 {
296  switch( nHandle )
297  {
298  case Property_Height: return Any( mnHeight );
299  case Property_OptimalHeight: return Any( mbOptimalHeight );
300  case Property_IsVisible: return Any( mbIsVisible );
301  case Property_IsStartOfNewPage: return Any( mbIsStartOfNewPage );
302  default: throw UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
303  }
304 }
305 
306 
308 {
309  static rtl::Reference<FastPropertySetInfo> xInfo = []() {
311 
312  aProperties[0].Name = "Height";
313  aProperties[0].Handle = Property_Height;
314  aProperties[0].Type = ::cppu::UnoType<sal_Int32>::get();
315  aProperties[0].Attributes = 0;
316 
317  aProperties[1].Name = "OptimalHeight";
318  aProperties[1].Handle = Property_OptimalHeight;
319  aProperties[1].Type = cppu::UnoType<bool>::get();
320  aProperties[1].Attributes = 0;
321 
322  aProperties[2].Name = "IsVisible";
323  aProperties[2].Handle = Property_IsVisible;
324  aProperties[2].Type = cppu::UnoType<bool>::get();
325  aProperties[2].Attributes = 0;
326 
327  aProperties[3].Name = "IsStartOfNewPage";
328  aProperties[3].Handle = Property_IsStartOfNewPage;
329  aProperties[3].Type = cppu::UnoType<bool>::get();
330  aProperties[3].Attributes = 0;
331 
332  aProperties[4].Name = "Size";
333  aProperties[4].Handle = Property_Height;
334  aProperties[4].Type = ::cppu::UnoType<sal_Int32>::get();
335  aProperties[4].Attributes = 0;
336 
337  aProperties[5].Name = "OptimalSize";
338  aProperties[5].Handle = Property_OptimalHeight;
339  aProperties[5].Type = cppu::UnoType<bool>::get();
340  aProperties[5].Attributes = 0;
341 
343  }();
344 
345  return xInfo;
346 }
347 
348 
349 }
350 
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double mnHeight
TableModelRef mxTableModel
Definition: tablerow.hxx:70
virtual css::uno::Reference< css::table::XCell > SAL_CALL getCellByPosition(sal_Int32 nColumn, sal_Int32 nRow) override
Definition: tablerow.cxx:159
std::vector< css::beans::Property > PropertyVector
Definition: propertyset.hxx:35
const sal_Int32 Property_IsStartOfNewPage
Definition: tablecolumn.cxx:43
void insertColumns(sal_Int32 nIndex, sal_Int32 nCount, CellVector::iterator const *pIter)
Definition: tablerow.cxx:103
const sal_Int32 Property_IsVisible
Definition: tablecolumn.cxx:42
PropertiesInfo aProperties
const TableModelRef & getModel() const
Reference to the table model containing this row.
Definition: tablerow.cxx:151
void removeColumns(sal_Int32 nIndex, sal_Int32 nCount)
Definition: tablerow.cxx:122
friend class TableRowUndo
Definition: tablerow.hxx:38
int i
virtual OUString SAL_CALL getName() override
Definition: tablerow.cxx:190
virtual void SAL_CALL setFastPropertyValue(::sal_Int32 nHandle, const css::uno::Any &aValue) override
Definition: tablerow.cxx:205
std::vector< CellRef > CellVector
Definition: celltypes.hxx:38
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByPosition(sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom) override
Definition: tablerow.cxx:169
css::uno::Type const & get()
static rtl::Reference< FastPropertySetInfo > getStaticPropertySetInfo()
Definition: tablerow.cxx:307
::cppu::ImplInheritanceHelper< FastPropertySet, css::table::XCellRange, css::container::XNamed > TableRowBase
Definition: tablerow.hxx:33
virtual css::uno::Any SAL_CALL getFastPropertyValue(::sal_Int32 nHandle) override
Definition: tablerow.cxx:294
virtual void SAL_CALL setName(const OUString &aName) override
Definition: tablerow.cxx:196
size_t mnRow
const sal_Int32 Property_Height
Definition: tablerow.cxx:40
virtual ~TableRow() override
Definition: tablerow.cxx:66
CellVector maCells
Definition: tablerow.hxx:71
sal_Int32 mnHeight
Definition: tablerow.hxx:73
void throwIfDisposed() const
Definition: tablerow.cxx:83
TableRow & operator=(const TableRow &)
Definition: tablerow.cxx:90
const sal_Int32 Property_OptimalHeight
Definition: tablerow.cxx:41
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByName(const OUString &aRange) override
Definition: tablerow.cxx:180
TableRow(const TableModelRef &xTableModel, sal_Int32 nRow, sal_Int32 nColumns)
Definition: tablerow.cxx:45