xref: /AOO41X/main/oox/source/drawingml/table/tableproperties.cxx (revision ca5ec2004b000a7d9aaa8381be8ac2853e3b1dc7)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/drawingml/table/tableproperties.hxx"
25cdf0e10cSrcweir #include "oox/drawingml/drawingmltypes.hxx"
26cdf0e10cSrcweir #include <com/sun/star/table/XTable.hpp>
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCellRange.hpp>
31cdf0e10cSrcweir #include <com/sun/star/table/BorderLine.hpp>
32cdf0e10cSrcweir #include "oox/core/xmlfilterbase.hxx"
33cdf0e10cSrcweir #include "oox/helper/propertyset.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using rtl::OUString;
36cdf0e10cSrcweir using namespace ::oox::core;
37cdf0e10cSrcweir using namespace ::com::sun::star;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star::beans;
40cdf0e10cSrcweir using namespace ::com::sun::star::table;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace oox { namespace drawingml { namespace table {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir TableProperties::TableProperties()
46cdf0e10cSrcweir : mbRtl( sal_False )
47cdf0e10cSrcweir , mbFirstRow( sal_False )
48cdf0e10cSrcweir , mbFirstCol( sal_False )
49cdf0e10cSrcweir , mbLastRow( sal_False )
50cdf0e10cSrcweir , mbLastCol( sal_False )
51cdf0e10cSrcweir , mbBandRow( sal_False )
52cdf0e10cSrcweir , mbBandCol( sal_False )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir }
55cdf0e10cSrcweir TableProperties::~TableProperties()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir void TableProperties::apply( const TablePropertiesPtr& /* rSourceTableProperties */ )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir void CreateTableRows( uno::Reference< XTableRows > xTableRows, const std::vector< TableRow >& rvTableRows )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	if ( rvTableRows.size() > 1 )
66cdf0e10cSrcweir 		xTableRows->insertByIndex( 0, rvTableRows.size() - 1 );
67cdf0e10cSrcweir 	std::vector< TableRow >::const_iterator aTableRowIter( rvTableRows.begin() );
68cdf0e10cSrcweir 	uno::Reference< container::XIndexAccess > xIndexAccess( xTableRows, UNO_QUERY_THROW );
69cdf0e10cSrcweir 	for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
70cdf0e10cSrcweir 	{
71cdf0e10cSrcweir 		static const rtl::OUString	sHeight( RTL_CONSTASCII_USTRINGPARAM ( "Height" ) );
72cdf0e10cSrcweir 		Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
73cdf0e10cSrcweir 		xPropSet->setPropertyValue( sHeight, Any( static_cast< sal_Int32 >( aTableRowIter->getHeight() / 360 ) ) );
74cdf0e10cSrcweir 		aTableRowIter++;
75cdf0e10cSrcweir 	}
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir void CreateTableColumns( Reference< XTableColumns > xTableColumns, const std::vector< sal_Int32 >& rvTableGrid )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	if ( rvTableGrid.size() > 1 )
81cdf0e10cSrcweir 		xTableColumns->insertByIndex( 0, rvTableGrid.size() - 1 );
82cdf0e10cSrcweir 	std::vector< sal_Int32 >::const_iterator aTableGridIter( rvTableGrid.begin() );
83cdf0e10cSrcweir 	uno::Reference< container::XIndexAccess > xIndexAccess( xTableColumns, UNO_QUERY_THROW );
84cdf0e10cSrcweir 	for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 		static const rtl::OUString	sWidth( RTL_CONSTASCII_USTRINGPARAM ( "Width" ) );
87cdf0e10cSrcweir 		Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
88cdf0e10cSrcweir 		xPropSet->setPropertyValue( sWidth, Any( static_cast< sal_Int32 >( *aTableGridIter++ / 360 ) ) );
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir void MergeCells( const uno::Reference< XTable >& xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir    if( xTable.is() ) try
95cdf0e10cSrcweir    {
96cdf0e10cSrcweir        Reference< XMergeableCellRange > xRange( xTable->createCursorByRange( xTable->getCellRangeByPosition( nCol, nRow,nCol + nColSpan - 1, nRow + nRowSpan - 1 ) ), UNO_QUERY_THROW );
97cdf0e10cSrcweir        if( xRange->isMergeable() )
98cdf0e10cSrcweir                xRange->merge();
99cdf0e10cSrcweir    }
100cdf0e10cSrcweir    catch( Exception& )
101cdf0e10cSrcweir    {
102cdf0e10cSrcweir    }
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir static TableStyle* pDefaultTableStyle = new TableStyle();
106cdf0e10cSrcweir 
107cdf0e10cSrcweir const TableStyle& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase& rFilterBase )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	::oox::core::XmlFilterBase& rBase( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ) );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	TableStyle* pTableStyle = NULL;
112cdf0e10cSrcweir 	if ( mpTableStyle )
113cdf0e10cSrcweir 		pTableStyle = &*mpTableStyle;
114cdf0e10cSrcweir 	else if ( rBase.getTableStyles() )
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		const std::vector< TableStyle >& rTableStyles( rBase.getTableStyles()->getTableStyles() );
117cdf0e10cSrcweir 		const rtl::OUString aStyleId( getStyleId().getLength() ? getStyleId() : rBase.getTableStyles()->getDefaultStyleId() );
118cdf0e10cSrcweir 		std::vector< TableStyle >::const_iterator aIter( rTableStyles.begin() );
119cdf0e10cSrcweir 		while( aIter != rTableStyles.end() )
120cdf0e10cSrcweir 		{
121cdf0e10cSrcweir 			if ( const_cast< TableStyle& >( *aIter ).getStyleId() == aStyleId )
122cdf0e10cSrcweir 			{
123cdf0e10cSrcweir 				pTableStyle = &const_cast< TableStyle& >( *aIter );
124cdf0e10cSrcweir 				break;	// we get the correct style
125cdf0e10cSrcweir 			}
126cdf0e10cSrcweir 			aIter++;
127cdf0e10cSrcweir 		}
128cdf0e10cSrcweir 	}
129cdf0e10cSrcweir 	if ( !pTableStyle )
130cdf0e10cSrcweir 		pTableStyle = pDefaultTableStyle;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	return *pTableStyle;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase& rFilterBase,
136cdf0e10cSrcweir 	const Reference < XPropertySet >& xPropSet, TextListStylePtr pMasterTextListStyle )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir 	TableStyleListPtr( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ).getTableStyles() );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	uno::Reference< XColumnRowRange > xColumnRowRange(
141cdf0e10cSrcweir  		xPropSet->getPropertyValue( OUString(RTL_CONSTASCII_USTRINGPARAM("Model") ) ), uno::UNO_QUERY_THROW );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	CreateTableColumns( xColumnRowRange->getColumns(), mvTableGrid );
144cdf0e10cSrcweir 	CreateTableRows( xColumnRowRange->getRows(), mvTableRows );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	const TableStyle& rTableStyle( getUsedTableStyle( rFilterBase ) );
147cdf0e10cSrcweir 	sal_Int32 nRow = 0;
148cdf0e10cSrcweir 	std::vector< TableRow >::iterator aTableRowIter( mvTableRows.begin() );
149cdf0e10cSrcweir 	while( aTableRowIter != mvTableRows.end() )
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 		sal_Int32 nColumn = 0;
152cdf0e10cSrcweir 		std::vector< TableCell >::iterator aTableCellIter( aTableRowIter->getTableCells().begin() );
153cdf0e10cSrcweir 		while( aTableCellIter != aTableRowIter->getTableCells().end() )
154cdf0e10cSrcweir 		{
155cdf0e10cSrcweir 			TableCell& rTableCell( *aTableCellIter );
156cdf0e10cSrcweir 			if ( !rTableCell.getvMerge() && !rTableCell.gethMerge() )
157cdf0e10cSrcweir 			{
158cdf0e10cSrcweir 				uno::Reference< XTable > xTable( xColumnRowRange, uno::UNO_QUERY_THROW );
159cdf0e10cSrcweir 				if ( ( rTableCell.getRowSpan() > 1 ) || ( rTableCell.getGridSpan() > 1 ) )
160cdf0e10cSrcweir 					MergeCells( xTable, nColumn, nRow, rTableCell.getGridSpan(), rTableCell.getRowSpan() );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 				Reference< XCellRange > xCellRange( xTable, UNO_QUERY_THROW );
163cdf0e10cSrcweir 				rTableCell.pushToXCell( rFilterBase, pMasterTextListStyle, xCellRange->getCellByPosition( nColumn, nRow ), *this, rTableStyle,
164cdf0e10cSrcweir 					nColumn, aTableRowIter->getTableCells().size(), nRow, mvTableRows.size() );
165cdf0e10cSrcweir 			}
166cdf0e10cSrcweir 			nColumn++;
167cdf0e10cSrcweir 			aTableCellIter++;
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 		nRow++;
170cdf0e10cSrcweir 		aTableRowIter++;
171cdf0e10cSrcweir 	}
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir } } }
175