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 
25 using namespace com::sun::star;
26 using ::std::list;
27 
28 void ScMyStyleNumberFormats::AddStyleNumberFormat(const OUString& rStyleName, const sal_Int32 nNumberFormat)
29 {
30  aSet.insert(ScMyStyleNumberFormat(rStyleName, nNumberFormat));
31 }
32 
33 sal_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 
51 void 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 
112 void ScMyStyleRanges::AddCurrencyRange(const ScRange& rRange, const std::optional<OUString> & pCurrency)
113 {
114  if (!pCurrencyList)
115  pCurrencyList.reset( new ScMyCurrencyStylesSet );
116  ScMyCurrencyStyle aStyle;
117  if (pCurrency)
118  aStyle.sCurrency = *pCurrency;
119  auto itPair = pCurrencyList->insert(aStyle);
120  itPair.first->mpRanges->AddAndPartialCombine(rRange);
121 }
122 
123 void 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 
156 void 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 
224 ScMyStylesMap::iterator ScMyStylesImportHelper::GetIterator(const OUString & rStyleName)
225 {
226  auto it = aCellStyles.find(rStyleName);
227  if (it == aCellStyles.end())
228  it = aCellStyles.emplace_hint(it, rStyleName, std::make_unique<ScMyStyleRanges>());
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
295  ResetAttributes();
296 }
297 
298 void 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(aColDefaultStyles.size() + nRepeat);
303  for (sal_Int32 i = 0; i < nRepeat; ++i)
304  aColDefaultStyles.push_back(aItr);
305 }
306 
307 void ScMyStylesImportHelper::SetRowStyle(const OUString& sStyleName)
308 {
309  aRowDefaultStyle = GetIterator(sStyleName);
310 }
311 
312 void 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;
362  ResetAttributes();
363  bPrevRangeAdded = false;
364  }
365 }
366 
368 {
369  ScRange aScRange( rAddress, rAddress );
370  AddRange(aScRange);
371 }
372 
373 void 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: */
void InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
void AddCell(const ScAddress &rAddress)
ScAddress aStart
Definition: address.hxx:500
void AddRange(const ScRange &rRange, const sal_Int16 nType)
std::optional< OUString > pPrevCurrency
SCROW Row() const
Definition: address.hxx:262
void AddCurrencyRange(const ScRange &rRange, const std::optional< OUString > &pCurrency)
std::optional< OUString > pPrevStyleName
constexpr OUStringLiteral PERCENT(u"Percent")
static void SetStylesToRanges(const ScRangeList &rList, const OUString *pStyleName, const sal_Int16 nCellType, const OUString *pCurrency, ScXMLImport &rImport)
ScAddress aEnd
Definition: address.hxx:501
void SetRowStyle(const OUString &rStyleName)
void InsertCol(const sal_Int32 nCol, const sal_Int32 nTab)
void SetAttributes(std::optional< OUString > pStyleName, std::optional< OUString > pCurrency, const sal_Int16 nCellType)
ScMyStylesImportHelper(ScXMLImport &rImport)
void SetRow(SCROW nRowP)
Definition: address.hxx:275
void SetCol(SCCOL nColP)
Definition: address.hxx:279
int i
void AddSingleRange(const ScRange &rRange)
sal_Int16 SCCOL
Definition: types.hxx:22
void SetStyleToRanges()
Definition: xmlimprt.cxx:1200
Use this class to manage solar mutex locking instead of calling LockSolarMutex() and UnlockSolarMutex...
Definition: xmlimprt.hxx:392
enumrange< T >::Iterator end(enumrange< T >)
SCCOL Col() const
Definition: address.hxx:267
void AddColumnStyle(const OUString &rStyleName, const sal_Int32 nColumn, const sal_Int32 nRepeat)
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:401
ScMyStylesMap::iterator aRowDefaultStyle
void AddStyleNumberFormat(const OUString &rStyleName, const sal_Int32 nNumberFormat)
sal_Int32 GetStyleNumberFormat(const OUString &rStyleName)
std::vector< ScMyStylesMap::iterator > aColDefaultStyles
std::set< ScMyCurrencyStyle, LessCurrencyStyle > ScMyCurrencyStylesSet
std::optional< OUString > pCurrency
std::optional< OUString > pStyleName
ScMyStylesMap::iterator GetIterator(const OUString &rStyleName)
void AddDefaultRange(const ScRange &rRange)