LibreOffice Module sc (master) 1
XMLStylesImportHelper.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
21#include "xmlimprt.hxx"
22#include <com/sun/star/util/NumberFormat.hpp>
23#include <osl/diagnose.h>
24
25using namespace com::sun::star;
26
27void ScMyStyleNumberFormats::AddStyleNumberFormat(const OUString& rStyleName, const sal_Int32 nNumberFormat)
28{
29 aSet.insert(ScMyStyleNumberFormat(rStyleName, nNumberFormat));
30}
31
32sal_Int32 ScMyStyleNumberFormats::GetStyleNumberFormat(const OUString& rStyleName)
33{
34 ScMyStyleNumberFormat aStyleNumberFormat(rStyleName);
35 ScMyStyleNumberFormatSet::iterator aItr(aSet.find(aStyleNumberFormat));
36 if (aItr == aSet.end())
37 return -1;
38 else
39 return aItr->nNumberFormat;
40}
41
43{
44}
45
47{
48}
49
50void ScMyStyleRanges::AddRange(const ScRange& rRange, const sal_Int16 nType)
51{
52 switch (nType)
53 {
54 case util::NumberFormat::NUMBER:
55 {
56 if (!mpNumberList)
57 mpNumberList = std::make_shared<ScRangeList>();
58 mpNumberList->AddAndPartialCombine(rRange);
59 }
60 break;
61 case util::NumberFormat::TEXT:
62 {
63 if (!mpTextList)
64 mpTextList = std::make_shared<ScRangeList>();
65 mpTextList->AddAndPartialCombine(rRange);
66 }
67 break;
68 case util::NumberFormat::TIME:
69 {
70 if (!mpTimeList)
71 mpTimeList = std::make_shared<ScRangeList>();
72 mpTimeList->AddAndPartialCombine(rRange);
73 }
74 break;
75 case util::NumberFormat::DATETIME:
76 {
77 if (!mpDateTimeList)
78 mpDateTimeList = std::make_shared<ScRangeList>();
79 mpDateTimeList->AddAndPartialCombine(rRange);
80 }
81 break;
83 {
84 if (!mpPercentList)
85 mpPercentList = std::make_shared<ScRangeList>();
86 mpPercentList->AddAndPartialCombine(rRange);
87 }
88 break;
89 case util::NumberFormat::LOGICAL:
90 {
91 if (!mpLogicalList)
92 mpLogicalList = std::make_shared<ScRangeList>();
93 mpLogicalList->AddAndPartialCombine(rRange);
94 }
95 break;
96 case util::NumberFormat::UNDEFINED:
97 {
98 if (!mpUndefinedList)
99 mpUndefinedList = std::make_shared<ScRangeList>();
100 mpUndefinedList->AddAndPartialCombine(rRange);
101 }
102 break;
103 default:
104 {
105 OSL_FAIL("wrong type");
106 }
107 break;
108 }
109}
110
111void ScMyStyleRanges::AddCurrencyRange(const ScRange& rRange, const std::optional<OUString> & pCurrency)
112{
113 if (!pCurrencyList)
115 ScMyCurrencyStyle aStyle;
116 if (pCurrency)
117 aStyle.sCurrency = *pCurrency;
118 auto itPair = pCurrencyList->insert(aStyle);
119 itPair.first->mpRanges->AddAndPartialCombine(rRange);
120}
121
122void ScMyStyleRanges::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
123{
124 if (mpTextList)
125 mpTextList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
126 if (mpNumberList)
127 mpNumberList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
128 if (mpTimeList)
129 mpTimeList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
130 if (mpDateTimeList)
131 mpDateTimeList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
132 if (mpPercentList)
133 mpPercentList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
134 if (mpLogicalList)
135 mpLogicalList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
136 if (mpUndefinedList)
137 mpUndefinedList->InsertCol(static_cast<SCCOL>(nTab), static_cast<SCTAB>(nCol));
138
139 if (pCurrencyList)
140 {
141 for (auto& rCurrency : *pCurrencyList)
142 {
143 rCurrency.mpRanges->InsertCol(static_cast<SCCOL>(nCol), static_cast<SCTAB>(nTab));
144 }
145 }
146}
147
149 const OUString* pStyleName, const sal_Int16 nCellType,
150 const OUString* pCurrency, ScXMLImport& rImport)
151{
152 rImport.SetStyleToRanges(rRanges, pStyleName, nCellType, pCurrency);
153}
154
155void ScMyStyleRanges::SetStylesToRanges(const OUString* pStyleName, ScXMLImport& rImport)
156{
157 if (mpNumberList)
158 {
159 SetStylesToRanges(*mpNumberList, pStyleName, util::NumberFormat::NUMBER, nullptr, rImport);
160 mpNumberList.reset();
161 }
162 if (mpTextList)
163 {
164 SetStylesToRanges(*mpTextList, pStyleName, util::NumberFormat::TEXT, nullptr, rImport);
165 mpTextList.reset();
166 }
167 if (mpTimeList)
168 {
169 SetStylesToRanges(*mpTimeList, pStyleName, util::NumberFormat::TIME, nullptr, rImport);
170 mpTimeList.reset();
171 }
172 if (mpDateTimeList)
173 {
174 SetStylesToRanges(*mpDateTimeList, pStyleName, util::NumberFormat::DATETIME, nullptr, rImport);
175 mpDateTimeList.reset();
176 }
177 if (mpPercentList)
178 {
179 SetStylesToRanges(*mpPercentList, pStyleName, util::NumberFormat::PERCENT, nullptr, rImport);
180 mpPercentList.reset();
181 }
182 if (mpLogicalList)
183 {
184 SetStylesToRanges(*mpLogicalList, pStyleName, util::NumberFormat::LOGICAL, nullptr, rImport);
185 mpLogicalList.reset();
186 }
187 if (mpUndefinedList)
188 {
189 SetStylesToRanges(*mpUndefinedList, pStyleName, util::NumberFormat::UNDEFINED, nullptr, rImport);
190 mpUndefinedList.reset();
191 }
192 if (pCurrencyList)
193 {
194 for (const auto& rCurrency : *pCurrencyList)
195 {
196 SetStylesToRanges(*rCurrency.mpRanges, pStyleName, util::NumberFormat::CURRENCY, &rCurrency.sCurrency, rImport);
197 }
198 }
199}
200
202 :
203 aRowDefaultStyle(aCellStyles.end()),
204 rImport(rTempImport),
205 nCellType(0),
206 nPrevCellType(0),
207 bPrevRangeAdded(true)
208{
209}
210
212{
213}
214
216{
217 pPrevStyleName = std::move(pStyleName);
218 pPrevCurrency = std::move(pCurrency);
220 nCellType = 0;
221}
222
223ScMyStylesMap::iterator ScMyStylesImportHelper::GetIterator(const OUString & rStyleName)
224{
225 auto it = aCellStyles.find(rStyleName);
226 if (it == aCellStyles.end())
227 it = aCellStyles.emplace_hint(it, std::piecewise_construct,
228 std::forward_as_tuple(rStyleName), std::forward_as_tuple());
229 return it;
230}
231
233{
234 OSL_ENSURE(aRowDefaultStyle != aCellStyles.end(), "no row default style");
235 if (aRowDefaultStyle->first.isEmpty())
236 {
237 SCCOL nStartCol(rRange.aStart.Col());
238 SCCOL nEndCol(rRange.aEnd.Col());
239 if (aColDefaultStyles.size() > sal::static_int_cast<sal_uInt32>(nStartCol))
240 {
241 ScMyStylesMap::iterator aPrevItr(aColDefaultStyles[nStartCol]);
242 for (SCCOL i = nStartCol + 1; (i <= nEndCol) && (i < sal::static_int_cast<SCCOL>(aColDefaultStyles.size())); ++i)
243 {
244 if (aPrevItr != aColDefaultStyles[i])
245 {
246 OSL_ENSURE(aPrevItr != aCellStyles.end(), "no column default style");
247 ScRange aRange(rRange);
248 aRange.aStart.SetCol(nStartCol);
249 aRange.aEnd.SetCol(i - 1);
250 pPrevStyleName = aPrevItr->first;
251 AddSingleRange(aRange);
252 nStartCol = i;
253 aPrevItr = aColDefaultStyles[i];
254 }
255 }
256 if (aPrevItr != aCellStyles.end())
257 {
258 ScRange aRange(rRange);
259 aRange.aStart.SetCol(nStartCol);
260 pPrevStyleName = aPrevItr->first;
261 AddSingleRange(aRange);
262 }
263 else
264 {
265 OSL_FAIL("no column default style");
266 }
267 }
268 else
269 {
270 OSL_FAIL("too many columns");
271 }
272 }
273 else
274 {
276 AddSingleRange(rRange);
277 }
278}
279
281{
282 ScMyStylesMap::iterator aItr(GetIterator(*pPrevStyleName));
283 if (nPrevCellType != util::NumberFormat::CURRENCY)
284 aItr->second.AddRange(rRange, nPrevCellType);
285 else
286 aItr->second.AddCurrencyRange(rRange, pPrevCurrency);
287}
288
290{
291 if (pPrevStyleName && !pPrevStyleName->isEmpty())
293 else
296}
297
298void ScMyStylesImportHelper::AddColumnStyle(const OUString& sStyleName, const sal_Int32 nColumn, const sal_Int32 nRepeat)
299{
300 OSL_ENSURE(static_cast<sal_uInt32>(nColumn) == aColDefaultStyles.size(), "some columns are absent");
301 ScMyStylesMap::iterator aItr(GetIterator(sStyleName));
302 aColDefaultStyles.reserve(std::max<size_t>(aColDefaultStyles.size() + nRepeat, aColDefaultStyles.size() * 2));
303 for (sal_Int32 i = 0; i < nRepeat; ++i)
304 aColDefaultStyles.push_back(aItr);
305}
306
307void ScMyStylesImportHelper::SetRowStyle(const OUString& sStyleName)
308{
309 aRowDefaultStyle = GetIterator(sStyleName);
310}
311
312void ScMyStylesImportHelper::SetAttributes(std::optional<OUString> pStyleNameP,
313 std::optional<OUString> pCurrencyP, const sal_Int16 nCellTypeP)
314{
315 pStyleName = std::move(pStyleNameP);
316 pCurrency = std::move(pCurrencyP);
317 nCellType = nCellTypeP;
318}
319
321{
322 if (!bPrevRangeAdded)
323 {
324 bool bAddRange(false);
325 if (nCellType == nPrevCellType &&
328 {
329 if (rRange.aStart.Row() == aPrevRange.aStart.Row())
330 {
331 if (rRange.aEnd.Row() == aPrevRange.aEnd.Row())
332 {
333 OSL_ENSURE(aPrevRange.aEnd.Col() + 1 == rRange.aStart.Col(), "something went wrong");
334 aPrevRange.aEnd.SetCol(rRange.aEnd.Col());
335 }
336 else
337 bAddRange = true;
338 }
339 else
340 {
341 if (rRange.aStart.Col() == aPrevRange.aStart.Col() &&
342 rRange.aEnd.Col() == aPrevRange.aEnd.Col())
343 {
344 OSL_ENSURE(aPrevRange.aEnd.Row() + 1 == rRange.aStart.Row(), "something went wrong");
345 aPrevRange.aEnd.SetRow(rRange.aEnd.Row());
346 }
347 else
348 bAddRange = true;
349 }
350 }
351 else
352 bAddRange = true;
353 if (bAddRange)
354 {
355 AddRange();
356 aPrevRange = rRange;
357 }
358 }
359 else
360 {
361 aPrevRange = rRange;
363 bPrevRangeAdded = false;
364 }
365}
366
368{
369 ScRange aScRange( rAddress, rAddress );
370 AddRange(aScRange);
371}
372
373void ScMyStylesImportHelper::InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
374{
376 for (auto& rCellStyle : aCellStyles)
377 {
378 rCellStyle.second.InsertCol(nCol, nTab);
379 }
380}
381
383{
384 if (!bPrevRangeAdded)
385 {
386 AddRange();
387 bPrevRangeAdded = true;
388 }
389}
390
392{
393 for (auto& rCellStyle : aCellStyles)
394 {
395 rCellStyle.second.SetStylesToRanges(&rCellStyle.first, rImport);
396 }
397 aColDefaultStyles.clear();
398 aCellStyles.clear();
399}
400
401/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::set< ScMyCurrencyStyle, LessCurrencyStyle > ScMyCurrencyStylesSet
void SetCol(SCCOL nColP)
Definition: address.hxx:291
SCROW Row() const
Definition: address.hxx:274
void SetRow(SCROW nRowP)
Definition: address.hxx:287
SCCOL Col() const
Definition: address.hxx:279
sal_Int32 GetStyleNumberFormat(const OUString &rStyleName)
ScMyStyleNumberFormatSet aSet
void AddStyleNumberFormat(const OUString &rStyleName, const sal_Int32 nNumberFormat)
void AddRange(const ScRange &rRange, const sal_Int16 nType)
static void SetStylesToRanges(const ScRangeList &rList, const OUString *pStyleName, const sal_Int16 nCellType, const OUString *pCurrency, ScXMLImport &rImport)
std::shared_ptr< ScRangeList > mpLogicalList
std::shared_ptr< ScRangeList > mpTextList
std::shared_ptr< ScRangeList > mpDateTimeList
std::unique_ptr< ScMyCurrencyStylesSet > pCurrencyList
void AddCurrencyRange(const ScRange &rRange, const std::optional< OUString > &pCurrency)
std::shared_ptr< ScRangeList > mpPercentList
std::shared_ptr< ScRangeList > mpUndefinedList
std::shared_ptr< ScRangeList > mpTimeList
std::shared_ptr< ScRangeList > mpNumberList
void InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
ScMyStylesMap::iterator aRowDefaultStyle
void AddDefaultRange(const ScRange &rRange)
void AddSingleRange(const ScRange &rRange)
std::optional< OUString > pStyleName
std::optional< OUString > pCurrency
void AddColumnStyle(const OUString &rStyleName, const sal_Int32 nColumn, const sal_Int32 nRepeat)
void InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
void SetRowStyle(const OUString &rStyleName)
void SetAttributes(std::optional< OUString > pStyleName, std::optional< OUString > pCurrency, const sal_Int16 nCellType)
std::optional< OUString > pPrevCurrency
std::optional< OUString > pPrevStyleName
ScMyStylesMap::iterator GetIterator(const OUString &rStyleName)
ScMyStylesImportHelper(ScXMLImport &rImport)
std::vector< ScMyStylesMap::iterator > aColDefaultStyles
void AddCell(const ScAddress &rAddress)
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:289
void SetStyleToRanges()
Definition: xmlimprt.cxx:977
int i
end
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
constexpr OUStringLiteral PERCENT(u"Percent")