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