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