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