LCOV - code coverage report
Current view: top level - oox/source/drawingml/table - tableproperties.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 68 171 39.8 %
Date: 2014-11-03 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      20             : #include "drawingml/table/tableproperties.hxx"
      21             : #include "drawingml/table/tablestylelist.hxx"
      22             : #include "oox/drawingml/drawingmltypes.hxx"
      23             : #include <com/sun/star/table/XTable.hpp>
      24             : #include <com/sun/star/container/XNameContainer.hpp>
      25             : #include <com/sun/star/beans/XMultiPropertySet.hpp>
      26             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      27             : #include <com/sun/star/table/XMergeableCellRange.hpp>
      28             : #include <com/sun/star/table/BorderLine2.hpp>
      29             : #include <rtl/instance.hxx>
      30             : #include "oox/core/xmlfilterbase.hxx"
      31             : #include "oox/helper/propertyset.hxx"
      32             : 
      33             : using namespace ::oox::core;
      34             : using namespace ::com::sun::star;
      35             : using namespace ::com::sun::star::uno;
      36             : using namespace ::com::sun::star::beans;
      37             : using namespace ::com::sun::star::table;
      38             : 
      39             : namespace oox { namespace drawingml { namespace table {
      40             : 
      41           6 : TableProperties::TableProperties()
      42             : : mbRtl( false )
      43             : , mbFirstRow( false )
      44             : , mbFirstCol( false )
      45             : , mbLastRow( false )
      46             : , mbLastCol( false )
      47             : , mbBandRow( false )
      48           6 : , mbBandCol( false )
      49             : {
      50           6 : }
      51           6 : TableProperties::~TableProperties()
      52             : {
      53           6 : }
      54             : 
      55           6 : void CreateTableRows( uno::Reference< XTableRows > xTableRows, const std::vector< TableRow >& rvTableRows )
      56             : {
      57           6 :     if ( rvTableRows.size() > 1 )
      58           6 :         xTableRows->insertByIndex( 0, rvTableRows.size() - 1 );
      59           6 :     std::vector< TableRow >::const_iterator aTableRowIter( rvTableRows.begin() );
      60           6 :     uno::Reference< container::XIndexAccess > xIndexAccess( xTableRows, UNO_QUERY_THROW );
      61          12 :     const OUString sHeight("Height");
      62          40 :     for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
      63             :     {
      64          34 :         Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
      65          34 :         xPropSet->setPropertyValue( sHeight, Any( static_cast< sal_Int32 >( aTableRowIter->getHeight() / 360 ) ) );
      66          34 :         ++aTableRowIter;
      67          40 :     }
      68           6 : }
      69             : 
      70           6 : void CreateTableColumns( Reference< XTableColumns > xTableColumns, const std::vector< sal_Int32 >& rvTableGrid )
      71             : {
      72           6 :     if ( rvTableGrid.size() > 1 )
      73           6 :         xTableColumns->insertByIndex( 0, rvTableGrid.size() - 1 );
      74           6 :     std::vector< sal_Int32 >::const_iterator aTableGridIter( rvTableGrid.begin() );
      75           6 :     uno::Reference< container::XIndexAccess > xIndexAccess( xTableColumns, UNO_QUERY_THROW );
      76          12 :     const OUString sWidth("Width");
      77          22 :     for ( sal_Int32 n = 0; n < xIndexAccess->getCount(); n++ )
      78             :     {
      79          16 :         Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
      80          16 :         xPropSet->setPropertyValue( sWidth, Any( static_cast< sal_Int32 >( *aTableGridIter++ / 360 ) ) );
      81          22 :     }
      82           6 : }
      83             : 
      84           0 : void MergeCells( const uno::Reference< XTable >& xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
      85             : {
      86           0 :    if( xTable.is() ) try
      87             :    {
      88           0 :        Reference< XMergeableCellRange > xRange( xTable->createCursorByRange( xTable->getCellRangeByPosition( nCol, nRow,nCol + nColSpan - 1, nRow + nRowSpan - 1 ) ), UNO_QUERY_THROW );
      89           0 :        if( xRange->isMergeable() )
      90           0 :                xRange->merge();
      91             :    }
      92           0 :    catch( Exception& )
      93             :    {
      94             :    }
      95           0 : }
      96             : 
      97             : namespace
      98             : {
      99             :     struct theDefaultTableStyle : public ::rtl::Static< TableStyle, theDefaultTableStyle > {};
     100             : }
     101             : 
     102             : //for pptx just has table style id
     103           0 : static void SetTableStyleProperties(TableStyle* &pTableStyle , const sal_Int32& tblFillClr,const sal_Int32& tblTextClr, const sal_Int32& lineBdrClr)
     104             : {
     105             :     //whole table fill style and color
     106           0 :     oox::drawingml::FillPropertiesPtr pWholeTabFillProperties( new oox::drawingml::FillProperties );
     107           0 :     pWholeTabFillProperties->moFillType.set(XML_solidFill);
     108           0 :     pWholeTabFillProperties->maFillColor.setSchemeClr(tblFillClr);
     109           0 :     pWholeTabFillProperties->maFillColor.addTransformation(XML_tint,20000);
     110           0 :     pTableStyle->getWholeTbl().getFillProperties() = pWholeTabFillProperties;
     111             :     //whole table text color
     112           0 :     ::oox::drawingml::Color tableTextColor;
     113           0 :     tableTextColor.setSchemeClr(tblTextClr);
     114           0 :     pTableStyle->getWholeTbl().getTextColor() = tableTextColor;
     115             :     //whole table line border
     116           0 :     oox::drawingml::LinePropertiesPtr pLeftBorder( new oox::drawingml::LineProperties);
     117           0 :     pLeftBorder->moLineWidth = 12700;
     118           0 :     pLeftBorder->moPresetDash = XML_sng;
     119           0 :     pLeftBorder->maLineFill.moFillType.set(XML_solidFill);
     120           0 :     pLeftBorder->maLineFill.maFillColor.setSchemeClr(lineBdrClr);
     121           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_left,pLeftBorder));
     122           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_right,pLeftBorder));
     123           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_top,pLeftBorder));
     124           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_bottom,pLeftBorder));
     125           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_insideH,pLeftBorder));
     126           0 :     pTableStyle->getWholeTbl().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_insideV,pLeftBorder));
     127             : 
     128             :     //Band1H style
     129           0 :     oox::drawingml::FillPropertiesPtr pBand1HFillProperties( new oox::drawingml::FillProperties );
     130           0 :     pBand1HFillProperties->moFillType.set(XML_solidFill);
     131           0 :     pBand1HFillProperties->maFillColor.setSchemeClr(tblFillClr);
     132           0 :     pBand1HFillProperties->maFillColor.addTransformation(XML_tint,40000);
     133           0 :     pTableStyle->getBand1H().getFillProperties() = pBand1HFillProperties;
     134             : 
     135             :     //Band1V style
     136           0 :     pTableStyle->getBand1V().getFillProperties() = pBand1HFillProperties;
     137             : 
     138             :     //tet bold for 1st row/last row/column
     139           0 :     ::boost::optional< sal_Bool > textBoldStyle(sal_True);
     140           0 :     pTableStyle->getFirstRow().getTextBoldStyle() = textBoldStyle;
     141           0 :     pTableStyle->getLastRow().getTextBoldStyle() = textBoldStyle;
     142           0 :     pTableStyle->getFirstCol().getTextBoldStyle() = textBoldStyle;
     143           0 :     pTableStyle->getLastCol().getTextBoldStyle() = textBoldStyle;
     144           0 : }
     145             : 
     146           0 :  bool CreateTableStyle(TableStyle* &pTableStyle , const OUString& styleId)
     147             : {
     148           0 :     bool createdTblStyle = false;
     149           0 :     if(styleId.equalsAscii("{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}")){           //Medium Style 2 Accenat 1
     150           0 :         pTableStyle = new TableStyle();
     151           0 :         createdTblStyle = true;
     152             :         //first row style
     153             :         //fill color and type
     154           0 :         oox::drawingml::FillPropertiesPtr pFstRowFillProperties( new oox::drawingml::FillProperties );
     155           0 :         pFstRowFillProperties->moFillType.set(XML_solidFill);
     156           0 :         pFstRowFillProperties->maFillColor.setSchemeClr(XML_accent1);
     157           0 :         pTableStyle->getFirstRow().getFillProperties() = pFstRowFillProperties;
     158             :         //text color
     159           0 :         ::oox::drawingml::Color fstRowTextColor;
     160           0 :         fstRowTextColor.setSchemeClr(XML_lt1);
     161           0 :         pTableStyle->getFirstRow().getTextColor() = fstRowTextColor;
     162             :         //bottom line border
     163           0 :         oox::drawingml::LinePropertiesPtr pFstBottomBorder( new oox::drawingml::LineProperties);
     164           0 :         pFstBottomBorder->moLineWidth = 38100;
     165           0 :         pFstBottomBorder->moPresetDash = XML_sng;
     166           0 :         pFstBottomBorder->maLineFill.moFillType.set(XML_solidFill);
     167           0 :         pFstBottomBorder->maLineFill.maFillColor.setSchemeClr(XML_lt1);
     168           0 :         pTableStyle->getFirstRow().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_bottom,pFstBottomBorder));
     169             : 
     170             :         //last row style
     171           0 :         pTableStyle->getLastRow().getFillProperties() = pFstRowFillProperties;
     172           0 :         pTableStyle->getLastRow().getTextColor() = fstRowTextColor;
     173           0 :         pTableStyle->getLastRow().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_top,pFstBottomBorder));
     174             : 
     175             :         //first column style
     176           0 :         pTableStyle->getFirstRow().getFillProperties() = pFstRowFillProperties;
     177           0 :         pTableStyle->getFirstRow().getTextColor() = fstRowTextColor;
     178             : 
     179             :         //last column style
     180           0 :         pTableStyle->getLastCol().getFillProperties() = pFstRowFillProperties;
     181           0 :         pTableStyle->getLastCol().getTextColor() = fstRowTextColor;
     182             : 
     183           0 :         SetTableStyleProperties(pTableStyle, XML_accent1, XML_dk1, XML_lt1);
     184             :     }
     185           0 :     else if (styleId.equalsAscii("{21E4AEA4-8DFA-4A89-87EB-49C32662AFE0}"))         //Medium Style 2 Accent 2
     186             :     {
     187           0 :         pTableStyle = new TableStyle();
     188           0 :         createdTblStyle = true;
     189           0 :         oox::drawingml::FillPropertiesPtr pFstRowFillProperties( new oox::drawingml::FillProperties );
     190           0 :         pFstRowFillProperties->moFillType.set(XML_solidFill);
     191           0 :         pFstRowFillProperties->maFillColor.setSchemeClr(XML_accent2);
     192           0 :         pTableStyle->getFirstRow().getFillProperties() = pFstRowFillProperties;
     193             : 
     194           0 :         ::oox::drawingml::Color fstRowTextColor;
     195           0 :         fstRowTextColor.setSchemeClr(XML_lt1);
     196           0 :         pTableStyle->getFirstRow().getTextColor() = fstRowTextColor;
     197             : 
     198           0 :         oox::drawingml::LinePropertiesPtr pFstBottomBorder( new oox::drawingml::LineProperties);
     199           0 :         pFstBottomBorder->moLineWidth = 38100;
     200           0 :         pFstBottomBorder->moPresetDash = XML_sng;
     201           0 :         pFstBottomBorder->maLineFill.moFillType.set(XML_solidFill);
     202           0 :         pFstBottomBorder->maLineFill.maFillColor.setSchemeClr(XML_lt1);
     203           0 :         pTableStyle->getFirstRow().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_bottom,pFstBottomBorder));
     204             : 
     205           0 :         pTableStyle->getLastRow().getFillProperties() = pFstRowFillProperties;
     206           0 :         pTableStyle->getLastRow().getTextColor() = fstRowTextColor;
     207           0 :         pTableStyle->getLastRow().getLineBorders().insert(std::pair<sal_Int32, ::oox::drawingml::LinePropertiesPtr>(XML_top,pFstBottomBorder));
     208             : 
     209           0 :         pTableStyle->getFirstCol().getFillProperties() = pFstRowFillProperties;
     210           0 :         pTableStyle->getFirstCol().getTextColor() = fstRowTextColor;
     211             : 
     212           0 :         pTableStyle->getLastCol().getFillProperties() = pFstRowFillProperties;
     213           0 :         pTableStyle->getLastCol().getTextColor() = fstRowTextColor;
     214             : 
     215           0 :         SetTableStyleProperties(pTableStyle, XML_accent2, XML_dk1, XML_lt1);
     216             :     }
     217           0 :     else if (styleId.equalsAscii("{C4B1156A-380E-4F78-BDF5-A606A8083BF9}"))         //Medium Style 4 Accent 4
     218             :     {
     219           0 :         pTableStyle = new TableStyle();
     220           0 :         createdTblStyle = true;
     221           0 :         SetTableStyleProperties(pTableStyle, XML_accent4, XML_dk1, XML_accent4);
     222             :     }
     223             : 
     224           0 :     return createdTblStyle;
     225             : }
     226             : 
     227           6 : const TableStyle& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase& rFilterBase, bool &isCreateTabStyle )
     228             : {
     229           6 :     ::oox::core::XmlFilterBase& rBase( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ) );
     230             : 
     231           6 :     TableStyle* pTableStyle = NULL;
     232           6 :     if ( mpTableStyle )
     233           0 :         pTableStyle = &*mpTableStyle;
     234           6 :     else if ( rBase.getTableStyles() )
     235             :     {
     236           6 :         const std::vector< TableStyle >& rTableStyles( rBase.getTableStyles()->getTableStyles() );
     237           6 :         const OUString aStyleId( getStyleId().isEmpty() ? rBase.getTableStyles()->getDefaultStyleId() : getStyleId() );
     238           6 :         std::vector< TableStyle >::const_iterator aIter( rTableStyles.begin() );
     239          18 :         while( aIter != rTableStyles.end() )
     240             :         {
     241          12 :             if ( const_cast< TableStyle& >( *aIter ).getStyleId() == aStyleId )
     242             :             {
     243           6 :                 pTableStyle = &const_cast< TableStyle& >( *aIter );
     244           6 :                 break;  // we get the correct style
     245             :             }
     246           6 :             ++aIter;
     247             :         }
     248             :         //if the pptx just has table style id, but no table style content, we will create the table style ourselves
     249           6 :         if ( !pTableStyle )
     250             :         {
     251           0 :             isCreateTabStyle = CreateTableStyle(pTableStyle , aStyleId);
     252           6 :         }
     253             :     }
     254             : 
     255           6 :     if ( !pTableStyle )
     256           0 :         return theDefaultTableStyle::get();
     257             : 
     258           6 :     return *pTableStyle;
     259             : }
     260             : 
     261           6 : void TableProperties::pushToPropSet( const ::oox::core::XmlFilterBase& rFilterBase,
     262             :     const Reference < XPropertySet >& xPropSet, TextListStylePtr pMasterTextListStyle )
     263             : {
     264             :     uno::Reference< XColumnRowRange > xColumnRowRange(
     265           6 :          xPropSet->getPropertyValue("Model"), uno::UNO_QUERY_THROW );
     266             : 
     267           6 :     CreateTableColumns( xColumnRowRange->getColumns(), mvTableGrid );
     268           6 :     CreateTableRows( xColumnRowRange->getRows(), mvTableRows );
     269             : 
     270           6 :     bool mbOwnTblStyle = false;
     271           6 :     const TableStyle& rTableStyle( getUsedTableStyle( rFilterBase, mbOwnTblStyle ) );
     272           6 :     sal_Int32 nRow = 0;
     273           6 :     const std::vector< TableRow >::const_iterator aTableRowEnd( mvTableRows.end() );
     274          40 :     for (std::vector< TableRow >::iterator aTableRowIter( mvTableRows.begin() );
     275             :          aTableRowIter != aTableRowEnd ; ++aTableRowIter, ++nRow)
     276             :     {
     277          34 :         sal_Int32 nColumn = 0;
     278          34 :         const std::vector< TableCell >::const_iterator aTableCellEnd( aTableRowIter->getTableCells().end() );
     279         122 :         for (std::vector< TableCell >::iterator aTableCellIter( aTableRowIter->getTableCells().begin() );
     280             :             aTableCellIter != aTableCellEnd ; ++aTableCellIter, ++nColumn)
     281             :         {
     282          88 :             TableCell& rTableCell( *aTableCellIter );
     283          88 :             if ( !rTableCell.getvMerge() && !rTableCell.gethMerge() )
     284             :             {
     285          88 :                 uno::Reference< XTable > xTable( xColumnRowRange, uno::UNO_QUERY_THROW );
     286          88 :                 if ( ( rTableCell.getRowSpan() > 1 ) || ( rTableCell.getGridSpan() > 1 ) )
     287           0 :                     MergeCells( xTable, nColumn, nRow, rTableCell.getGridSpan(), rTableCell.getRowSpan() );
     288             : 
     289         176 :                 Reference< XCellRange > xCellRange( xTable, UNO_QUERY_THROW );
     290          88 :                 rTableCell.pushToXCell( rFilterBase, pMasterTextListStyle, xCellRange->getCellByPosition( nColumn, nRow ), *this, rTableStyle,
     291         264 :                     nColumn, aTableRowIter->getTableCells().size()-1, nRow, mvTableRows.size()-1 );
     292             :             }
     293             :         }
     294             :     }
     295             : 
     296           6 :     if(mbOwnTblStyle)
     297             :     {
     298           0 :         TableStyle* pTableStyle = (TableStyle*)&rTableStyle;
     299           0 :         if(pTableStyle != NULL)
     300             :         {
     301           0 :             delete pTableStyle;
     302           0 :             pTableStyle = NULL;
     303             :         }
     304           0 :         mbOwnTblStyle = false;
     305           6 :     }
     306           6 : }
     307             : 
     308         408 : } } }
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10