1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "oox/drawingml/table/tableproperties.hxx"
21 #include "oox/drawingml/drawingmltypes.hxx"
22 #include <com/sun/star/table/XTable.hpp>
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/beans/XMultiPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/table/XMergeableCellRange.hpp>
27 #include <com/sun/star/table/BorderLine2.hpp>
28 #include <rtl/instance.hxx>
29 #include "oox/core/xmlfilterbase.hxx"
30 #include "oox/helper/propertyset.hxx"
32 using namespace ::oox::core
;
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::table
;
39 namespace oox
{ namespace drawingml
{ namespace table
{
41 TableProperties::TableProperties()
43 , mbFirstRow( sal_False
)
44 , mbFirstCol( sal_False
)
45 , mbLastRow( sal_False
)
46 , mbLastCol( sal_False
)
47 , mbBandRow( sal_False
)
48 , mbBandCol( sal_False
)
51 TableProperties::~TableProperties()
55 void CreateTableRows( uno::Reference
< XTableRows
> xTableRows
, const std::vector
< TableRow
>& rvTableRows
)
57 if ( rvTableRows
.size() > 1 )
58 xTableRows
->insertByIndex( 0, rvTableRows
.size() - 1 );
59 std::vector
< TableRow
>::const_iterator
aTableRowIter( rvTableRows
.begin() );
60 uno::Reference
< container::XIndexAccess
> xIndexAccess( xTableRows
, UNO_QUERY_THROW
);
61 const OUString
sHeight("Height");
62 for ( sal_Int32 n
= 0; n
< xIndexAccess
->getCount(); n
++ )
64 Reference
< XPropertySet
> xPropSet( xIndexAccess
->getByIndex( n
), UNO_QUERY_THROW
);
65 xPropSet
->setPropertyValue( sHeight
, Any( static_cast< sal_Int32
>( aTableRowIter
->getHeight() / 360 ) ) );
70 void CreateTableColumns( Reference
< XTableColumns
> xTableColumns
, const std::vector
< sal_Int32
>& rvTableGrid
)
72 if ( rvTableGrid
.size() > 1 )
73 xTableColumns
->insertByIndex( 0, rvTableGrid
.size() - 1 );
74 std::vector
< sal_Int32
>::const_iterator
aTableGridIter( rvTableGrid
.begin() );
75 uno::Reference
< container::XIndexAccess
> xIndexAccess( xTableColumns
, UNO_QUERY_THROW
);
76 const OUString
sWidth("Width");
77 for ( sal_Int32 n
= 0; n
< xIndexAccess
->getCount(); n
++ )
79 Reference
< XPropertySet
> xPropSet( xIndexAccess
->getByIndex( n
), UNO_QUERY_THROW
);
80 xPropSet
->setPropertyValue( sWidth
, Any( static_cast< sal_Int32
>( *aTableGridIter
++ / 360 ) ) );
84 void MergeCells( const uno::Reference
< XTable
>& xTable
, sal_Int32 nCol
, sal_Int32 nRow
, sal_Int32 nColSpan
, sal_Int32 nRowSpan
)
88 Reference
< XMergeableCellRange
> xRange( xTable
->createCursorByRange( xTable
->getCellRangeByPosition( nCol
, nRow
,nCol
+ nColSpan
- 1, nRow
+ nRowSpan
- 1 ) ), UNO_QUERY_THROW
);
89 if( xRange
->isMergeable() )
99 struct theDefaultTableStyle
: public ::rtl::Static
< TableStyle
, theDefaultTableStyle
> {};
102 //for pptx just has table style id
103 static void SetTableStyleProperties(TableStyle
* &pTableStyle
, const sal_Int32
& tblFillClr
,const sal_Int32
& tblTextClr
, const sal_Int32
& lineBdrClr
)
105 //whole table fill style and color
106 oox::drawingml::FillPropertiesPtr
pWholeTabFillProperties( new oox::drawingml::FillProperties
);
107 pWholeTabFillProperties
->moFillType
.set(XML_solidFill
);
108 pWholeTabFillProperties
->maFillColor
.setSchemeClr(tblFillClr
);
109 pWholeTabFillProperties
->maFillColor
.addTransformation(XML_tint
,20000);
110 pTableStyle
->getWholeTbl().getFillProperties() = pWholeTabFillProperties
;
111 //whole table text color
112 ::oox::drawingml::Color tableTextColor
;
113 tableTextColor
.setSchemeClr(tblTextClr
);
114 pTableStyle
->getWholeTbl().getTextColor() = tableTextColor
;
115 //whole table line border
116 oox::drawingml::LinePropertiesPtr
pLeftBorder( new oox::drawingml::LineProperties
);
117 pLeftBorder
->moLineWidth
= 12700;
118 pLeftBorder
->moPresetDash
= XML_sng
;
119 pLeftBorder
->maLineFill
.moFillType
.set(XML_solidFill
);
120 pLeftBorder
->maLineFill
.maFillColor
.setSchemeClr(lineBdrClr
);
121 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_left
,pLeftBorder
));
122 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_right
,pLeftBorder
));
123 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_top
,pLeftBorder
));
124 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_bottom
,pLeftBorder
));
125 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_insideH
,pLeftBorder
));
126 pTableStyle
->getWholeTbl().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_insideV
,pLeftBorder
));
129 oox::drawingml::FillPropertiesPtr
pBand1HFillProperties( new oox::drawingml::FillProperties
);
130 pBand1HFillProperties
->moFillType
.set(XML_solidFill
);
131 pBand1HFillProperties
->maFillColor
.setSchemeClr(tblFillClr
);
132 pBand1HFillProperties
->maFillColor
.addTransformation(XML_tint
,40000);
133 pTableStyle
->getBand1H().getFillProperties() = pBand1HFillProperties
;
136 pTableStyle
->getBand1V().getFillProperties() = pBand1HFillProperties
;
138 //tet bold for 1st row/last row/column
139 ::boost::optional
< sal_Bool
> textBoldStyle(sal_True
);
140 pTableStyle
->getFirstRow().getTextBoldStyle() = textBoldStyle
;
141 pTableStyle
->getLastRow().getTextBoldStyle() = textBoldStyle
;
142 pTableStyle
->getFirstCol().getTextBoldStyle() = textBoldStyle
;
143 pTableStyle
->getLastCol().getTextBoldStyle() = textBoldStyle
;
146 sal_Bool
CreateTableStyle(TableStyle
* &pTableStyle
, const OUString
& styleId
)
148 sal_Bool createdTblStyle
= sal_False
;
149 if(!styleId
.compareToAscii("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}")){ //Medium Style 2 Accenat 1
150 pTableStyle
= new TableStyle();
151 createdTblStyle
= sal_True
;
153 //fill color and type
154 oox::drawingml::FillPropertiesPtr
pFstRowFillProperties( new oox::drawingml::FillProperties
);
155 pFstRowFillProperties
->moFillType
.set(XML_solidFill
);
156 pFstRowFillProperties
->maFillColor
.setSchemeClr(XML_accent1
);
157 pTableStyle
->getFirstRow().getFillProperties() = pFstRowFillProperties
;
159 ::oox::drawingml::Color fstRowTextColor
;
160 fstRowTextColor
.setSchemeClr(XML_lt1
);
161 pTableStyle
->getFirstRow().getTextColor() = fstRowTextColor
;
163 oox::drawingml::LinePropertiesPtr
pFstBottomBorder( new oox::drawingml::LineProperties
);
164 pFstBottomBorder
->moLineWidth
= 38100;
165 pFstBottomBorder
->moPresetDash
= XML_sng
;
166 pFstBottomBorder
->maLineFill
.moFillType
.set(XML_solidFill
);
167 pFstBottomBorder
->maLineFill
.maFillColor
.setSchemeClr(XML_lt1
);
168 pTableStyle
->getFirstRow().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_bottom
,pFstBottomBorder
));
171 pTableStyle
->getLastRow().getFillProperties() = pFstRowFillProperties
;
172 pTableStyle
->getLastRow().getTextColor() = fstRowTextColor
;
173 pTableStyle
->getLastRow().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_top
,pFstBottomBorder
));
176 pTableStyle
->getFirstRow().getFillProperties() = pFstRowFillProperties
;
177 pTableStyle
->getFirstRow().getTextColor() = fstRowTextColor
;
180 pTableStyle
->getLastCol().getFillProperties() = pFstRowFillProperties
;
181 pTableStyle
->getLastCol().getTextColor() = fstRowTextColor
;
183 SetTableStyleProperties(pTableStyle
, XML_accent1
, XML_dk1
, XML_lt1
);
185 else if (!styleId
.compareToAscii("{21E4AEA4-8DFA-4A89-87EB-49C32662AFE0}")) //Medium Style 2 Accent 2
187 pTableStyle
= new TableStyle();
188 createdTblStyle
= sal_True
;
189 oox::drawingml::FillPropertiesPtr
pFstRowFillProperties( new oox::drawingml::FillProperties
);
190 pFstRowFillProperties
->moFillType
.set(XML_solidFill
);
191 pFstRowFillProperties
->maFillColor
.setSchemeClr(XML_accent2
);
192 pTableStyle
->getFirstRow().getFillProperties() = pFstRowFillProperties
;
194 ::oox::drawingml::Color fstRowTextColor
;
195 fstRowTextColor
.setSchemeClr(XML_lt1
);
196 pTableStyle
->getFirstRow().getTextColor() = fstRowTextColor
;
198 oox::drawingml::LinePropertiesPtr
pFstBottomBorder( new oox::drawingml::LineProperties
);
199 pFstBottomBorder
->moLineWidth
= 38100;
200 pFstBottomBorder
->moPresetDash
= XML_sng
;
201 pFstBottomBorder
->maLineFill
.moFillType
.set(XML_solidFill
);
202 pFstBottomBorder
->maLineFill
.maFillColor
.setSchemeClr(XML_lt1
);
203 pTableStyle
->getFirstRow().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_bottom
,pFstBottomBorder
));
205 pTableStyle
->getLastRow().getFillProperties() = pFstRowFillProperties
;
206 pTableStyle
->getLastRow().getTextColor() = fstRowTextColor
;
207 pTableStyle
->getLastRow().getLineBorders().insert(std::pair
<sal_Int32
, ::oox::drawingml::LinePropertiesPtr
>(XML_top
,pFstBottomBorder
));
209 pTableStyle
->getFirstCol().getFillProperties() = pFstRowFillProperties
;
210 pTableStyle
->getFirstCol().getTextColor() = fstRowTextColor
;
212 pTableStyle
->getLastCol().getFillProperties() = pFstRowFillProperties
;
213 pTableStyle
->getLastCol().getTextColor() = fstRowTextColor
;
215 SetTableStyleProperties(pTableStyle
, XML_accent2
, XML_dk1
, XML_lt1
);
217 else if (!styleId
.compareToAscii("{C4B1156A-380E-4F78-BDF5-A606A8083BF9}")) //Medium Style 4 Accent 4
219 pTableStyle
= new TableStyle();
220 createdTblStyle
= sal_True
;
221 SetTableStyleProperties(pTableStyle
, XML_accent4
, XML_dk1
, XML_accent4
);
224 return createdTblStyle
;
227 const TableStyle
& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase
& rFilterBase
, sal_Bool
&isCreateTabStyle
)
229 ::oox::core::XmlFilterBase
& rBase( const_cast< ::oox::core::XmlFilterBase
& >( rFilterBase
) );
231 TableStyle
* pTableStyle
= NULL
;
233 pTableStyle
= &*mpTableStyle
;
234 else if ( rBase
.getTableStyles() )
236 const std::vector
< TableStyle
>& rTableStyles( rBase
.getTableStyles()->getTableStyles() );
237 const OUString
aStyleId( getStyleId().isEmpty() ? rBase
.getTableStyles()->getDefaultStyleId() : getStyleId() );
238 std::vector
< TableStyle
>::const_iterator
aIter( rTableStyles
.begin() );
239 while( aIter
!= rTableStyles
.end() )
241 if ( const_cast< TableStyle
& >( *aIter
).getStyleId() == aStyleId
)
243 pTableStyle
= &const_cast< TableStyle
& >( *aIter
);
244 break; // we get the correct style
248 //if the pptx just has table style id, but no table style content, we will create the table style ourselves
251 isCreateTabStyle
= CreateTableStyle(pTableStyle
, aStyleId
);
256 return theDefaultTableStyle::get();
261 void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase
& rFilterBase
,
262 const Reference
< XPropertySet
>& xPropSet
, TextListStylePtr pMasterTextListStyle
)
264 uno::Reference
< XColumnRowRange
> xColumnRowRange(
265 xPropSet
->getPropertyValue("Model"), uno::UNO_QUERY_THROW
);
267 CreateTableColumns( xColumnRowRange
->getColumns(), mvTableGrid
);
268 CreateTableRows( xColumnRowRange
->getRows(), mvTableRows
);
270 sal_Bool mbOwnTblStyle
= sal_False
;
271 const TableStyle
& rTableStyle( getUsedTableStyle( rFilterBase
, mbOwnTblStyle
) );
273 const std::vector
< TableRow
>::const_iterator
aTableRowEnd( mvTableRows
.end() );
274 for (std::vector
< TableRow
>::iterator
aTableRowIter( mvTableRows
.begin() );
275 aTableRowIter
!= aTableRowEnd
; ++aTableRowIter
, ++nRow
)
277 sal_Int32 nColumn
= 0;
278 const std::vector
< TableCell
>::const_iterator
aTableCellEnd( aTableRowIter
->getTableCells().end() );
279 for (std::vector
< TableCell
>::iterator
aTableCellIter( aTableRowIter
->getTableCells().begin() );
280 aTableCellIter
!= aTableCellEnd
; ++aTableCellIter
, ++nColumn
)
282 TableCell
& rTableCell( *aTableCellIter
);
283 if ( !rTableCell
.getvMerge() && !rTableCell
.gethMerge() )
285 uno::Reference
< XTable
> xTable( xColumnRowRange
, uno::UNO_QUERY_THROW
);
286 if ( ( rTableCell
.getRowSpan() > 1 ) || ( rTableCell
.getGridSpan() > 1 ) )
287 MergeCells( xTable
, nColumn
, nRow
, rTableCell
.getGridSpan(), rTableCell
.getRowSpan() );
289 Reference
< XCellRange
> xCellRange( xTable
, UNO_QUERY_THROW
);
290 rTableCell
.pushToXCell( rFilterBase
, pMasterTextListStyle
, xCellRange
->getCellByPosition( nColumn
, nRow
), *this, rTableStyle
,
291 nColumn
, aTableRowIter
->getTableCells().size(), nRow
, mvTableRows
.size() );
296 if(mbOwnTblStyle
== sal_True
)
298 TableStyle
* pTableStyle
= (TableStyle
*)&rTableStyle
;
299 if(pTableStyle
!= NULL
)
304 mbOwnTblStyle
= sal_False
;
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */