Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / xml / xmlcoli.cxx
blob4e52dfa413ada58d4cef85c0db4792e919d2f2ed
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/xmlnamespace.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 nColCount(1),
44 sVisibility(GetXMLToken(XML_VISIBLE))
46 if ( !rAttrList.is() )
47 return;
49 for (auto &aIter : *rAttrList)
51 switch (aIter.getToken())
53 case XML_ELEMENT( TABLE, XML_NUMBER_COLUMNS_REPEATED ):
55 nColCount = std::max<sal_Int32>(aIter.toInt32(), 1);
56 nColCount = std::min<sal_Int32>(nColCount, rImport.GetDocument()->GetSheetLimits().GetMaxColCount() );
58 break;
59 case XML_ELEMENT( TABLE, XML_STYLE_NAME ):
61 sStyleName = aIter.toString();
63 break;
64 case XML_ELEMENT( TABLE, XML_VISIBILITY ):
66 sVisibility = aIter.toString();
68 break;
69 case XML_ELEMENT( TABLE, XML_DEFAULT_CELL_STYLE_NAME ):
71 sCellStyleName = aIter.toString();
73 break;
78 ScXMLTableColContext::~ScXMLTableColContext()
82 void SAL_CALL ScXMLTableColContext::endFastElement( sal_Int32 /*nElement*/ )
84 ScXMLImport& rXMLImport = GetScImport();
85 ScDocument* pDoc = rXMLImport.GetDocument();
86 SCTAB nSheet = rXMLImport.GetTables().GetCurrentSheet();
87 sal_Int32 nCurrentColumn = rXMLImport.GetTables().GetCurrentColCount();
88 uno::Reference<sheet::XSpreadsheet> xSheet(rXMLImport.GetTables().GetCurrentXSheet());
89 if(xSheet.is())
91 sal_Int32 nLastColumn(nCurrentColumn + nColCount - 1);
92 if (nLastColumn > pDoc->MaxCol())
93 nLastColumn = pDoc->MaxCol();
94 if (nCurrentColumn > pDoc->MaxCol())
95 nCurrentColumn = pDoc->MaxCol();
96 uno::Reference<table::XColumnRowRange> xColumnRowRange (xSheet->getCellRangeByPosition(nCurrentColumn, 0, nLastColumn, 0), uno::UNO_QUERY);
97 if (xColumnRowRange.is())
99 uno::Reference <beans::XPropertySet> xColumnProperties(xColumnRowRange->getColumns(), uno::UNO_QUERY);
100 if (xColumnProperties.is())
102 if (!sStyleName.isEmpty())
104 XMLTableStylesContext *pStyles = static_cast<XMLTableStylesContext *>(rXMLImport.GetAutoStyles());
105 if ( pStyles )
107 XMLTableStyleContext* pStyle = const_cast<XMLTableStyleContext*>(static_cast<const XMLTableStyleContext *>(pStyles->FindStyleChildContext(
108 XmlStyleFamily::TABLE_COLUMN, sStyleName, true)));
109 if (pStyle)
111 pStyle->FillPropertySet(xColumnProperties);
113 if ( nSheet != pStyle->GetLastSheet() )
115 ScSheetSaveData* pSheetData = comphelper::getFromUnoTunnel<ScModelObj>(rXMLImport.GetModel())->GetSheetSaveData();
116 pSheetData->AddColumnStyle( sStyleName, ScAddress( static_cast<SCCOL>(nCurrentColumn), 0, nSheet ) );
117 pStyle->SetLastSheet(nSheet);
122 bool bValue(true);
123 if (!IsXMLToken(sVisibility, XML_VISIBLE))
124 bValue = false;
125 xColumnProperties->setPropertyValue(SC_UNONAME_CELLVIS, uno::Any(bValue));
130 // #i57915# ScXMLImport::SetStyleToRange can't handle empty style names.
131 // The default for a column if there is no attribute is the style "Default" (programmatic API name).
132 if ( sCellStyleName.isEmpty() )
133 sCellStyleName = "Default";
135 GetScImport().GetTables().AddColStyle(nColCount, sCellStyleName);
138 ScXMLTableColsContext::ScXMLTableColsContext( ScXMLImport& rImport,
139 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList,
140 const bool bTempHeader, const bool bTempGroup) :
141 ScXMLImportContext( rImport ),
142 nHeaderStartCol(0),
143 nHeaderEndCol(0),
144 nGroupStartCol(0),
145 nGroupEndCol(0),
146 bHeader(bTempHeader),
147 bGroup(bTempGroup),
148 bGroupDisplay(true)
150 // don't have any attributes
151 if (bHeader)
152 nHeaderStartCol = rImport.GetTables().GetCurrentColCount();
153 else if (bGroup)
155 nGroupStartCol = rImport.GetTables().GetCurrentColCount();
156 if ( rAttrList.is() )
158 auto aIter( rAttrList->find( XML_ELEMENT( TABLE, XML_DISPLAY ) ) );
159 if ( aIter != rAttrList->end() && IsXMLToken(aIter, XML_FALSE) )
160 bGroupDisplay = false;
165 ScXMLTableColsContext::~ScXMLTableColsContext()
169 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL ScXMLTableColsContext::createFastChildContext(
170 sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
172 SvXMLImportContext *pContext = nullptr;
173 sax_fastparser::FastAttributeList *pAttribList =
174 &sax_fastparser::castToFastAttributeList( xAttrList );
176 switch (nElement)
178 case XML_ELEMENT( TABLE, XML_TABLE_COLUMN_GROUP ):
179 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
180 false, true );
181 break;
182 case XML_ELEMENT( TABLE, XML_TABLE_HEADER_COLUMNS ):
183 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
184 true, false );
185 break;
186 case XML_ELEMENT( TABLE, XML_TABLE_COLUMNS ):
187 pContext = new ScXMLTableColsContext( GetScImport(), pAttribList,
188 false, false );
189 break;
190 case XML_ELEMENT( TABLE, XML_TABLE_COLUMN ):
191 pContext = new ScXMLTableColContext( GetScImport(), pAttribList );
192 break;
195 return pContext;
198 void SAL_CALL ScXMLTableColsContext::endFastElement( sal_Int32 /*nElement*/ )
200 ScXMLImport& rXMLImport = GetScImport();
201 if (bHeader)
203 nHeaderEndCol = rXMLImport.GetTables().GetCurrentColCount();
204 nHeaderEndCol--;
205 if (nHeaderStartCol <= nHeaderEndCol)
207 uno::Reference <sheet::XPrintAreas> xPrintAreas (rXMLImport.GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
208 if (xPrintAreas.is())
210 if (!xPrintAreas->getPrintTitleColumns())
212 xPrintAreas->setPrintTitleColumns(true);
213 table::CellRangeAddress aColumnHeaderRange;
214 aColumnHeaderRange.StartColumn = nHeaderStartCol;
215 aColumnHeaderRange.EndColumn = nHeaderEndCol;
216 xPrintAreas->setTitleColumns(aColumnHeaderRange);
218 else
220 table::CellRangeAddress aColumnHeaderRange(xPrintAreas->getTitleColumns());
221 aColumnHeaderRange.EndColumn = nHeaderEndCol;
222 xPrintAreas->setTitleColumns(aColumnHeaderRange);
227 else if (bGroup)
229 SCTAB nSheet = rXMLImport.GetTables().GetCurrentSheet();
230 nGroupEndCol = rXMLImport.GetTables().GetCurrentColCount();
231 nGroupEndCol--;
232 if (nGroupStartCol <= nGroupEndCol)
234 ScDocument* pDoc = GetScImport().GetDocument();
235 if (pDoc)
237 ScXMLImport::MutexGuard aGuard(GetScImport());
238 ScOutlineTable* pOutlineTable = pDoc->GetOutlineTable(nSheet, true);
239 if (pOutlineTable)
241 ScOutlineArray& rColArray = pOutlineTable->GetColArray();
242 bool bResized;
243 rColArray.Insert(static_cast<SCCOL>(nGroupStartCol), static_cast<SCCOL>(nGroupEndCol), bResized, !bGroupDisplay);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */