Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / xml / xmlcoli.cxx
blob1d4d0966d707f9cb636fad22c908cf63cf7a61d1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "xmlcoli.hxx"
21 #include "xmlimprt.hxx"
22 #include "xmlstyli.hxx"
23 #include <document.hxx>
24 #include <docuno.hxx>
25 #include <olinetab.hxx>
26 #include <sheetdata.hxx>
27 #include <unonames.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/families.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include <com/sun/star/sheet/XSpreadsheet.hpp>
33 #include <com/sun/star/table/XColumnRowRange.hpp>
34 #include <com/sun/star/sheet/XPrintAreas.hpp>
35 #include <comphelper/servicehelper.hxx>
37 using namespace com::sun::star;
38 using namespace xmloff::token;
40 ScXMLTableColContext::ScXMLTableColContext( ScXMLImport& rImport,
41 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList ) :
42 ScXMLImportContext( rImport ),
43 sVisibility(GetXMLToken(XML_VISIBLE))
45 nColCount = 1;
46 if ( rAttrList.is() )
48 for (auto &aIter : *rAttrList)
50 switch (aIter.getToken())
52 case XML_ELEMENT( TABLE, XML_NUMBER_COLUMNS_REPEATED ):
54 nColCount = std::max<sal_Int32>(aIter.toInt32(), 1);
55 nColCount = std::min<sal_Int32>(nColCount, MAXCOLCOUNT);
57 break;
58 case XML_ELEMENT( TABLE, XML_STYLE_NAME ):
60 sStyleName = aIter.toString();
62 break;
63 case XML_ELEMENT( TABLE, XML_VISIBILITY ):
65 sVisibility = aIter.toString();
67 break;
68 case XML_ELEMENT( TABLE, XML_DEFAULT_CELL_STYLE_NAME ):
70 sCellStyleName = aIter.toString();
72 break;
78 ScXMLTableColContext::~ScXMLTableColContext()
82 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL ScXMLTableColContext::createFastChildContext(
83 sal_Int32 /*nElement*/, const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/ )
85 return new SvXMLImportContext( GetImport() );
88 void SAL_CALL ScXMLTableColContext::endFastElement( sal_Int32 /*nElement*/ )
90 ScXMLImport& rXMLImport = GetScImport();
91 ScDocument* pDoc = rXMLImport.GetDocument();
92 SCTAB nSheet = rXMLImport.GetTables().GetCurrentSheet();
93 sal_Int32 nCurrentColumn = rXMLImport.GetTables().GetCurrentColCount();
94 uno::Reference<sheet::XSpreadsheet> xSheet(rXMLImport.GetTables().GetCurrentXSheet());
95 if(xSheet.is())
97 sal_Int32 nLastColumn(nCurrentColumn + nColCount - 1);
98 if (nLastColumn > pDoc->MaxCol())
99 nLastColumn = pDoc->MaxCol();
100 if (nCurrentColumn > pDoc->MaxCol())
101 nCurrentColumn = pDoc->MaxCol();
102 uno::Reference<table::XColumnRowRange> xColumnRowRange (xSheet->getCellRangeByPosition(nCurrentColumn, 0, nLastColumn, 0), uno::UNO_QUERY);
103 if (xColumnRowRange.is())
105 uno::Reference <beans::XPropertySet> xColumnProperties(xColumnRowRange->getColumns(), uno::UNO_QUERY);
106 if (xColumnProperties.is())
108 if (!sStyleName.isEmpty())
110 XMLTableStylesContext *pStyles = static_cast<XMLTableStylesContext *>(rXMLImport.GetAutoStyles());
111 if ( pStyles )
113 XMLTableStyleContext* pStyle = const_cast<XMLTableStyleContext*>(static_cast<const XMLTableStyleContext *>(pStyles->FindStyleChildContext(
114 XML_STYLE_FAMILY_TABLE_COLUMN, sStyleName, true)));
115 if (pStyle)
117 pStyle->FillPropertySet(xColumnProperties);
119 if ( nSheet != pStyle->GetLastSheet() )
121 ScSheetSaveData* pSheetData = comphelper::getUnoTunnelImplementation<ScModelObj>(rXMLImport.GetModel())->GetSheetSaveData();
122 pSheetData->AddColumnStyle( sStyleName, ScAddress( static_cast<SCCOL>(nCurrentColumn), 0, nSheet ) );
123 pStyle->SetLastSheet(nSheet);
128 bool bValue(true);
129 if (!IsXMLToken(sVisibility, XML_VISIBLE))
130 bValue = false;
131 xColumnProperties->setPropertyValue(SC_UNONAME_CELLVIS, uno::makeAny(bValue));
136 // #i57915# ScXMLImport::SetStyleToRange can't handle empty style names.
137 // The default for a column if there is no attribute is the style "Default" (programmatic API name).
138 if ( sCellStyleName.isEmpty() )
139 sCellStyleName = "Default";
141 GetScImport().GetTables().AddColStyle(nColCount, sCellStyleName);
144 ScXMLTableColsContext::ScXMLTableColsContext( ScXMLImport& rImport,
145 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList,
146 const bool bTempHeader, const bool bTempGroup) :
147 ScXMLImportContext( rImport ),
148 nHeaderStartCol(0),
149 nHeaderEndCol(0),
150 nGroupStartCol(0),
151 nGroupEndCol(0),
152 bHeader(bTempHeader),
153 bGroup(bTempGroup),
154 bGroupDisplay(true)
156 // don't have any attributes
157 if (bHeader)
158 nHeaderStartCol = rImport.GetTables().GetCurrentColCount();
159 else if (bGroup)
161 nGroupStartCol = rImport.GetTables().GetCurrentColCount();
162 if ( rAttrList.is() )
164 auto aIter( rAttrList->find( XML_ELEMENT( TABLE, XML_DISPLAY ) ) );
165 if ( aIter != rAttrList->end() && IsXMLToken(aIter, XML_FALSE) )
166 bGroupDisplay = false;
171 ScXMLTableColsContext::~ScXMLTableColsContext()
175 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL ScXMLTableColsContext::createFastChildContext(
176 sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
178 SvXMLImportContext *pContext = nullptr;
179 sax_fastparser::FastAttributeList *pAttribList =
180 sax_fastparser::FastAttributeList::castToFastAttributeList( xAttrList );
182 switch (nElement)
184 case XML_ELEMENT( TABLE, XML_TABLE_COLUMN_GROUP ):
185 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
186 false, true );
187 break;
188 case XML_ELEMENT( TABLE, XML_TABLE_HEADER_COLUMNS ):
189 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
190 true, false );
191 break;
192 case XML_ELEMENT( TABLE, XML_TABLE_COLUMNS ):
193 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
194 false, false );
195 break;
196 case XML_ELEMENT( TABLE, XML_TABLE_COLUMN ):
197 pContext = new ScXMLTableColContext( GetScImport(), pAttribList );
198 break;
201 if( !pContext )
202 pContext = new SvXMLImportContext( GetImport() );
204 return pContext;
207 void SAL_CALL ScXMLTableColsContext::endFastElement( sal_Int32 /*nElement*/ )
209 ScXMLImport& rXMLImport = GetScImport();
210 if (bHeader)
212 nHeaderEndCol = rXMLImport.GetTables().GetCurrentColCount();
213 nHeaderEndCol--;
214 if (nHeaderStartCol <= nHeaderEndCol)
216 uno::Reference <sheet::XPrintAreas> xPrintAreas (rXMLImport.GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
217 if (xPrintAreas.is())
219 if (!xPrintAreas->getPrintTitleColumns())
221 xPrintAreas->setPrintTitleColumns(true);
222 table::CellRangeAddress aColumnHeaderRange;
223 aColumnHeaderRange.StartColumn = nHeaderStartCol;
224 aColumnHeaderRange.EndColumn = nHeaderEndCol;
225 xPrintAreas->setTitleColumns(aColumnHeaderRange);
227 else
229 table::CellRangeAddress aColumnHeaderRange(xPrintAreas->getTitleColumns());
230 aColumnHeaderRange.EndColumn = nHeaderEndCol;
231 xPrintAreas->setTitleColumns(aColumnHeaderRange);
236 else if (bGroup)
238 SCTAB nSheet = rXMLImport.GetTables().GetCurrentSheet();
239 nGroupEndCol = rXMLImport.GetTables().GetCurrentColCount();
240 nGroupEndCol--;
241 if (nGroupStartCol <= nGroupEndCol)
243 ScDocument* pDoc = GetScImport().GetDocument();
244 if (pDoc)
246 ScXMLImport::MutexGuard aGuard(GetScImport());
247 ScOutlineTable* pOutlineTable = pDoc->GetOutlineTable(nSheet, true);
248 if (pOutlineTable)
250 ScOutlineArray& rColArray = pOutlineTable->GetColArray();
251 bool bResized;
252 rColArray.Insert(static_cast<SCCOL>(nGroupStartCol), static_cast<SCCOL>(nGroupEndCol), bResized, !bGroupDisplay);
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */