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