Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / table / tablerow.cxx
blob90fa84acb9c95c7fea016ffffca57956f3b2f7a6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <cell.hxx>
25 #include "tablerow.hxx"
26 #include "tableundo.hxx"
27 #include <sdr/properties/cellproperties.hxx>
28 #include <svx/svdmodel.hxx>
29 #include <svx/svdotable.hxx>
30 #include <utility>
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::table;
37 using namespace ::com::sun::star::beans;
40 namespace sdr::table {
42 const sal_Int32 Property_Height = 0;
43 const sal_Int32 Property_OptimalHeight = 1;
44 const sal_Int32 Property_IsVisible = 2;
45 const sal_Int32 Property_IsStartOfNewPage = 3;
47 TableRow::TableRow( TableModelRef xTableModel, sal_Int32 nRow, sal_Int32 nColumns )
48 : TableRowBase( getStaticPropertySetInfo() )
49 , mxTableModel(std::move( xTableModel ))
50 , mnRow( nRow )
51 , mnHeight( 0 )
52 , mbOptimalHeight( true )
53 , mbIsVisible( true )
54 , mbIsStartOfNewPage( false )
56 if( nColumns < 20 )
57 maCells.reserve( 20 );
59 if( nColumns )
61 maCells.resize( nColumns );
62 while( nColumns-- )
63 maCells[ nColumns ] = mxTableModel->createCell();
68 TableRow::~TableRow()
73 void TableRow::dispose()
75 mxTableModel.clear();
76 if( !maCells.empty() )
78 for( auto& rpCell : maCells )
79 rpCell->dispose();
80 CellVector().swap(maCells);
85 void TableRow::throwIfDisposed() const
87 if( !mxTableModel.is() )
88 throw DisposedException();
92 TableRow& TableRow::operator=( const TableRow& r )
94 mnHeight = r.mnHeight;
95 mbOptimalHeight = r.mbOptimalHeight;
96 mbIsVisible = r.mbIsVisible;
97 mbIsStartOfNewPage = r.mbIsStartOfNewPage;
98 maName = r.maName;
99 mnRow = r.mnRow;
101 return *this;
105 void TableRow::insertColumns( sal_Int32 nIndex, sal_Int32 nCount, CellVector::iterator const * pIter /* = 0 */ )
107 throwIfDisposed();
108 if( !nCount )
109 return;
111 if( nIndex >= static_cast< sal_Int32 >( maCells.size() ) )
112 nIndex = static_cast< sal_Int32 >( maCells.size() );
113 if ( pIter )
114 maCells.insert( maCells.begin() + nIndex, *pIter, (*pIter) + nCount );
115 else
117 maCells.reserve( std::max<size_t>(maCells.size() + nCount, maCells.size() * 2) );
118 for ( sal_Int32 i = 0; i < nCount; i++ )
119 maCells.insert( maCells.begin() + nIndex + i, mxTableModel->createCell() );
124 void TableRow::removeColumns( sal_Int32 nIndex, sal_Int32 nCount )
126 throwIfDisposed();
127 if( (nCount < 0) || ( nIndex < 0))
128 return;
130 if( (nIndex + nCount) < static_cast< sal_Int32 >( maCells.size() ) )
132 CellVector::iterator aBegin( maCells.begin() );
133 std::advance(aBegin, nIndex);
135 if( nCount > 1 )
137 CellVector::iterator aEnd( aBegin );
138 while( nCount-- && (aEnd != maCells.end()) )
139 ++aEnd;
140 maCells.erase( aBegin, aEnd );
142 else
144 maCells.erase( aBegin );
147 else
149 maCells.resize( nIndex );
153 const TableModelRef& TableRow::getModel() const
155 return mxTableModel;
158 // XCellRange
161 Reference< XCell > SAL_CALL TableRow::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow )
163 throwIfDisposed();
164 if( nRow != 0 )
165 throw IndexOutOfBoundsException();
167 return mxTableModel->getCellByPosition( nColumn, mnRow );
171 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
173 throwIfDisposed();
174 if( (nLeft >= 0 ) && (nTop == 0) && (nRight >= nLeft) && (nBottom == 0) )
176 return mxTableModel->getCellRangeByPosition( nLeft, mnRow, nRight, mnRow );
178 throw IndexOutOfBoundsException();
182 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByName( const OUString& /*aRange*/ )
184 throwIfDisposed();
185 return Reference< XCellRange >();
189 // XNamed
192 OUString SAL_CALL TableRow::getName()
194 return maName;
198 void SAL_CALL TableRow::setName( const OUString& aName )
200 maName = aName;
204 // XFastPropertySet
207 void SAL_CALL TableRow::setFastPropertyValue( sal_Int32 nHandle, const Any& aValue )
209 if(!mxTableModel.is() || nullptr == mxTableModel->getSdrTableObj())
210 return;
212 SdrTableObj& rTableObj(*mxTableModel->getSdrTableObj());
213 SdrModel& rModel(rTableObj.getSdrModelFromSdrObject());
214 bool bOk(false);
215 bool bChange(false);
216 std::unique_ptr<TableRowUndo> pUndo;
217 const bool bUndo(rTableObj.IsInserted() && rModel.IsUndoEnabled());
219 if( bUndo )
221 TableRowRef xThis( this );
222 pUndo.reset(new TableRowUndo( xThis ));
225 switch( nHandle )
227 case Property_Height:
229 sal_Int32 nHeight = mnHeight;
230 bOk = aValue >>= nHeight;
231 if( bOk && (mnHeight != nHeight) )
233 mnHeight = nHeight;
234 mbOptimalHeight = mnHeight == 0;
235 bChange = true;
237 break;
240 case Property_OptimalHeight:
242 bool bOptimalHeight = mbOptimalHeight;
243 bOk = aValue >>= bOptimalHeight;
244 if( bOk && (mbOptimalHeight != bOptimalHeight) )
246 mbOptimalHeight = bOptimalHeight;
247 if( bOptimalHeight )
248 mnHeight = 0;
249 bChange = true;
251 break;
253 case Property_IsVisible:
255 bool bIsVisible = mbIsVisible;
256 bOk = aValue >>= bIsVisible;
257 if( bOk && (mbIsVisible != bIsVisible) )
259 mbIsVisible = bIsVisible;
260 bChange = true;
262 break;
265 case Property_IsStartOfNewPage:
267 bool bIsStartOfNewPage = mbIsStartOfNewPage;
268 bOk = aValue >>= bIsStartOfNewPage;
269 if( bOk && (mbIsStartOfNewPage != bIsStartOfNewPage) )
271 mbIsStartOfNewPage = bIsStartOfNewPage;
272 bChange = true;
274 break;
276 default:
277 throw UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
280 if( !bOk )
282 throw IllegalArgumentException();
285 if( bChange )
287 if( pUndo )
289 rModel.AddUndo( std::move(pUndo) );
291 mxTableModel->setModified(true);
296 Any SAL_CALL TableRow::getFastPropertyValue( sal_Int32 nHandle )
298 switch( nHandle )
300 case Property_Height: return Any( mnHeight );
301 case Property_OptimalHeight: return Any( mbOptimalHeight );
302 case Property_IsVisible: return Any( mbIsVisible );
303 case Property_IsStartOfNewPage: return Any( mbIsStartOfNewPage );
304 default: throw UnknownPropertyException( OUString::number(nHandle), static_cast<cppu::OWeakObject*>(this));
309 rtl::Reference< FastPropertySetInfo > TableRow::getStaticPropertySetInfo()
311 static rtl::Reference<FastPropertySetInfo> xInfo = []() {
312 PropertyVector aProperties(6);
314 aProperties[0].Name = "Height";
315 aProperties[0].Handle = Property_Height;
316 aProperties[0].Type = ::cppu::UnoType<sal_Int32>::get();
317 aProperties[0].Attributes = 0;
319 aProperties[1].Name = "OptimalHeight";
320 aProperties[1].Handle = Property_OptimalHeight;
321 aProperties[1].Type = cppu::UnoType<bool>::get();
322 aProperties[1].Attributes = 0;
324 aProperties[2].Name = "IsVisible";
325 aProperties[2].Handle = Property_IsVisible;
326 aProperties[2].Type = cppu::UnoType<bool>::get();
327 aProperties[2].Attributes = 0;
329 aProperties[3].Name = "IsStartOfNewPage";
330 aProperties[3].Handle = Property_IsStartOfNewPage;
331 aProperties[3].Type = cppu::UnoType<bool>::get();
332 aProperties[3].Attributes = 0;
334 aProperties[4].Name = "Size";
335 aProperties[4].Handle = Property_Height;
336 aProperties[4].Type = ::cppu::UnoType<sal_Int32>::get();
337 aProperties[4].Attributes = 0;
339 aProperties[5].Name = "OptimalSize";
340 aProperties[5].Handle = Property_OptimalHeight;
341 aProperties[5].Type = cppu::UnoType<bool>::get();
342 aProperties[5].Attributes = 0;
344 return rtl::Reference<FastPropertySetInfo>(new FastPropertySetInfo(aProperties));
345 }();
347 return xInfo;
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */