LibreOffice Module oox (master) 1
tableproperties.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
23#include <com/sun/star/table/XTable.hpp>
24#include <com/sun/star/table/XMergeableCellRange.hpp>
27
28using namespace ::oox::core;
29using namespace ::com::sun::star;
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::beans;
32using namespace ::com::sun::star::table;
33
34namespace oox::drawingml::table {
35
37: mbFirstRow( false )
38, mbFirstCol( false )
39, mbLastRow( false )
40, mbLastCol( false )
41, mbBandRow( false )
42, mbBandCol( false )
43{
45}
46
47static void CreateTableRows( const uno::Reference< XTableRows >& xTableRows, const std::vector< TableRow >& rvTableRows )
48{
49 if ( rvTableRows.size() > 1 )
50 xTableRows->insertByIndex( 0, rvTableRows.size() - 1 );
51 std::vector< TableRow >::const_iterator aTableRowIter( rvTableRows.begin() );
52 uno::Reference< container::XIndexAccess > xIndexAccess( xTableRows, UNO_QUERY_THROW );
53 sal_Int32 nCols = std::min<sal_Int32>(xIndexAccess->getCount(), rvTableRows.size());
54 for (sal_Int32 n = 0; n < nCols; ++n)
55 {
56 Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
57 xPropSet->setPropertyValue( "Height", Any( static_cast< sal_Int32 >( aTableRowIter->getHeight() / 360 ) ) );
58 ++aTableRowIter;
59 }
60}
61
62static void CreateTableColumns( const Reference< XTableColumns >& xTableColumns, const std::vector< sal_Int32 >& rvTableGrid )
63{
64 if ( rvTableGrid.size() > 1 )
65 xTableColumns->insertByIndex( 0, rvTableGrid.size() - 1 );
66 std::vector< sal_Int32 >::const_iterator aTableGridIter( rvTableGrid.begin() );
67 uno::Reference< container::XIndexAccess > xIndexAccess( xTableColumns, UNO_QUERY_THROW );
68 sal_Int32 nCols = std::min<sal_Int32>(xIndexAccess->getCount(), rvTableGrid.size());
69 for (sal_Int32 n = 0; n < nCols; ++n)
70 {
71 Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
72 xPropSet->setPropertyValue( "Width", Any( static_cast< sal_Int32 >( *aTableGridIter++ / 360 ) ) );
73 }
74}
75
76static void MergeCells( const uno::Reference< XTable >& xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
77{
78 if( xTable.is() ) try
79 {
80 Reference< XMergeableCellRange > xRange( xTable->createCursorByRange( xTable->getCellRangeByPosition( nCol, nRow,nCol + nColSpan - 1, nRow + nRowSpan - 1 ) ), UNO_QUERY_THROW );
81 if( xRange->isMergeable() )
82 xRange->merge();
83 }
84 catch( Exception& )
85 {
86 }
87}
88
89const TableStyle& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase& rFilterBase, std::unique_ptr<TableStyle>& rTableStyleToDelete )
90{
91 ::oox::core::XmlFilterBase& rBase( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ) );
92
93 TableStyle* pTableStyle = nullptr;
94 if ( mpTableStyle )
95 pTableStyle = &*mpTableStyle;
96 else if ( !getStyleId().isEmpty() && rBase.getTableStyles() )
97 {
98 const std::vector< TableStyle >& rTableStyles( rBase.getTableStyles()->getTableStyles() );
99 const OUString aStyleId( getStyleId() );
100
101 for (auto const& tableStyle : rTableStyles)
102 {
103 if ( const_cast< TableStyle& >(tableStyle).getStyleId() == aStyleId )
104 {
105 pTableStyle = &const_cast< TableStyle& >(tableStyle);
106 break; // we get the correct style
107 }
108 }
109 //if the pptx just has table style id, but no table style content, we will create the table style ourselves
110 if (!pTableStyle)
111 {
112 rTableStyleToDelete = CreateTableStyle(aStyleId);
113 pTableStyle = rTableStyleToDelete.get();
114 }
115 }
116
117 if ( !pTableStyle )
118 {
119 static TableStyle theDefaultTableStyle;
120 return theDefaultTableStyle;
121 }
122
123 return *pTableStyle;
124}
125
126void TableProperties::pushToPropSet(const ::oox::core::XmlFilterBase& rFilterBase,
127 const Reference<XPropertySet>& xPropSet,
128 const TextListStylePtr& pMasterTextListStyle)
129{
130 uno::Reference<XColumnRowRange> xColumnRowRange(xPropSet->getPropertyValue("Model"),
131 uno::UNO_QUERY_THROW);
132
133 CreateTableColumns(xColumnRowRange->getColumns(), mvTableGrid);
134 CreateTableRows(xColumnRowRange->getRows(), mvTableRows);
135
136 std::unique_ptr<TableStyle> xTableStyleToDelete;
137 const TableStyle& rTableStyle(getUsedTableStyle(rFilterBase, xTableStyleToDelete));
138 sal_Int32 nRow = 0;
139
140 for (auto& tableRow : mvTableRows)
141 {
142 sal_Int32 nColumn = 0;
143 sal_Int32 nColumnSize = tableRow.getTableCells().size();
144 sal_Int32 nRemovedColumn = 0;
145 sal_Int32 nRemovedRow = 0;
146
147 for (sal_Int32 nColIndex = 0; nColIndex < nColumnSize; nColIndex++)
148 {
149 TableCell& rTableCell(tableRow.getTableCells().at(nColIndex));
150
151 if (!rTableCell.getvMerge() && !rTableCell.gethMerge())
152 {
153 uno::Reference<XTable> xTable(xColumnRowRange, uno::UNO_QUERY_THROW);
154 bool bMerged = false;
155
156 if ((rTableCell.getRowSpan() > 1) || (rTableCell.getGridSpan() > 1))
157 {
158 MergeCells(xTable, nColumn, nRow, rTableCell.getGridSpan(),
159 rTableCell.getRowSpan());
160
161 if (rTableCell.getGridSpan() > 1)
162 {
163 nRemovedColumn = (rTableCell.getGridSpan() - 1);
164 // MergeCells removes columns. Our loop does not know about those
165 // removed columns and we skip handling those removed columns.
166 nColIndex += nRemovedColumn;
167 // It will adjust new column number after push current column's
168 // props with pushToXCell.
169 bMerged = true;
170 }
171
172 if (rTableCell.getRowSpan() > 1)
173 nRemovedRow = (rTableCell.getRowSpan() - 1);
174 }
175
176 Reference<XCellRange> xCellRange(xTable, UNO_QUERY_THROW);
177 Reference<XCell> xCell;
178
179 if (nRemovedColumn)
180 {
181 try
182 {
183 xCell = xCellRange->getCellByPosition(nColumn, nRow);
184 }
185 // Exception can come from TableModel::getCellByPosition when a column
186 // is removed while merging columns. So adjust again here.
187 catch (Exception&)
188 {
189 xCell = xCellRange->getCellByPosition(nColumn - nRemovedColumn, nRow);
190 }
191 }
192 else
193 xCell = xCellRange->getCellByPosition(nColumn, nRow);
194
195
196 sal_Int32 nMaxCol = tableRow.getTableCells().size() - nRemovedColumn - 1;
197 sal_Int32 nMaxRow = mvTableRows.size() - nRemovedRow - 1;
198
199 rTableCell.pushToXCell(rFilterBase, pMasterTextListStyle, xCell, *this, rTableStyle,
200 nColumn, nMaxCol, nRow, nMaxRow);
201
202 if (bMerged)
203 nColumn += nRemovedColumn;
204
205 nRemovedRow = 0;
206 }
207 ++nColumn;
208 }
209 ++nRow;
210 }
211
212 xTableStyleToDelete.reset();
213}
214}
215
216/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ::oox::drawingml::table::TableStyleListPtr getTableStyles()=0
Has to be implemented by each filter to return the table style list.
void setUnused()
Sets the color to unused state.
Definition: color.cxx:250
sal_Int32 getGridSpan() const
Definition: tablecell.hxx:46
sal_Int32 getRowSpan() const
Definition: tablecell.hxx:44
void pushToXCell(const ::oox::core::XmlFilterBase &rFilterBase, const ::oox::drawingml::TextListStylePtr &pMasterTextListStyle, const css::uno::Reference< css::table::XCell > &rxCell, const TableProperties &rTableProperties, const TableStyle &rTable, sal_Int32 nColumn, sal_Int32 nMaxColumn, sal_Int32 nRow, sal_Int32 nMaxRow)
Definition: tablecell.cxx:262
const TableStyle & getUsedTableStyle(const ::oox::core::XmlFilterBase &rFilterBase, std::unique_ptr< TableStyle > &rTableStyleToDelete)
std::shared_ptr< TableStyle > mpTableStyle
void pushToPropSet(const ::oox::core::XmlFilterBase &rFilterBase, const css::uno::Reference< css::beans::XPropertySet > &xPropSet, const ::oox::drawingml::TextListStylePtr &pMasterTextListStyle)
sal_Int64 n
@ Exception
static void CreateTableRows(const uno::Reference< XTableRows > &xTableRows, const std::vector< TableRow > &rvTableRows)
static void CreateTableColumns(const Reference< XTableColumns > &xTableColumns, const std::vector< sal_Int32 > &rvTableGrid)
static void MergeCells(const uno::Reference< XTable > &xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan)
std::shared_ptr< TextListStyle > TextListStylePtr
std::unique_ptr< TableStyle > CreateTableStyle(const OUString &styleId)