LibreOffice Module sc (master)  1
exctools.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 
20 #include <osl/diagnose.h>
21 #include <document.hxx>
22 #include <attrib.hxx>
23 #include <scextopt.hxx>
24 #include <olinetab.hxx>
25 
26 #include <root.hxx>
27 #include <excimp8.hxx>
28 #include <namebuff.hxx>
29 #include <otlnbuff.hxx>
30 #include <formel.hxx>
31 #include <xilink.hxx>
32 
33 #include <memory>
34 #include <vector>
35 
37 {
38  eDateiTyp = BiffX;
39  pFmlaConverter = nullptr;
40 
41  pPrintRanges.reset( new ScRangeListTabs );
42  pPrintTitles.reset( new ScRangeListTabs );
43 
44  pTabId = nullptr;
45  pUserBViewList = nullptr;
46 
47  pIR = nullptr;
48  pER = nullptr;
49  pColRowBuff = nullptr;
50 }
51 
53 {
54  pExtSheetBuff.reset();
55  pShrfmlaBuff.reset();
56  pExtNameBuff.reset();
57  pAutoFilterBuffer.reset();
58  pPrintRanges.reset();
59  pPrintTitles.reset();
60 }
61 
63  maLevels(0, nNewSize, 0),
64  mpOutlineArray(nullptr),
65  mnEndPos(nNewSize),
66  mnMaxLevel(0),
67  mbButtonAfter(true)
68 {
69 }
70 
72 {
73 }
74 
75 void XclImpOutlineBuffer::SetLevel( SCSIZE nIndex, sal_uInt8 nVal, bool bCollapsed )
76 {
77  maLevels.insert_back(nIndex, nIndex+1, nVal);
78  if (nVal > mnMaxLevel)
79  mnMaxLevel = nVal;
80  if (bCollapsed)
81  maCollapsedPosSet.insert(nIndex);
82 }
83 
85 {
86  mpOutlineArray = pOArray;
87 }
88 
90 {
91  if (!mpOutlineArray)
92  return;
93 
94  ::std::vector<SCSIZE> aOutlineStack;
95  aOutlineStack.reserve(mnMaxLevel);
96  for (const auto& [nPos, nLevel] : maLevels)
97  {
98  if (nPos >= mnEndPos)
99  {
100  // Don't go beyond the max allowed position.
101  OSL_ENSURE(aOutlineStack.empty(), "XclImpOutlineBuffer::MakeScOutline: outline stack not empty but expected to be.");
102  break;
103  }
104  sal_uInt8 nCurLevel = static_cast<sal_uInt8>(aOutlineStack.size());
105  if (nLevel > nCurLevel)
106  {
107  for (sal_uInt8 i = 0; i < nLevel - nCurLevel; ++i)
108  aOutlineStack.push_back(nPos);
109  }
110  else
111  {
112  OSL_ENSURE(nLevel <= nCurLevel, "XclImpOutlineBuffer::MakeScOutline: unexpected level!");
113  for (sal_uInt8 i = 0; i < nCurLevel - nLevel; ++i)
114  {
115  if (aOutlineStack.empty())
116  {
117  // Something is wrong.
118  return;
119  }
120  SCSIZE nFirstPos = aOutlineStack.back();
121  aOutlineStack.pop_back();
122  bool bCollapsed = false;
123  if (mbButtonAfter)
124  bCollapsed = maCollapsedPosSet.count(nPos) > 0;
125  else if (nFirstPos > 0)
126  bCollapsed = maCollapsedPosSet.count(nFirstPos-1) > 0;
127 
128  bool bDummy;
129  mpOutlineArray->Insert(nFirstPos, nPos-1, bDummy, bCollapsed);
130  }
131  }
132  }
133 }
134 
135 void XclImpOutlineBuffer::SetLevelRange( SCSIZE nF, SCSIZE nL, sal_uInt8 nVal, bool bCollapsed )
136 {
137  if (nF > nL)
138  // invalid range
139  return;
140 
141  maLevels.insert_back(nF, nL+1, nVal);
142 
143  if (bCollapsed)
144  maCollapsedPosSet.insert(nF);
145 }
146 
147 void XclImpOutlineBuffer::SetButtonMode( bool bRightOrUnder )
148 {
149  mbButtonAfter = bRightOrUnder;
150 }
151 
152 ExcScenarioCell::ExcScenarioCell( const sal_uInt16 nC, const sal_uInt16 nR )
153  : nCol( nC ), nRow( nR )
154 {
155 }
156 
158  : nTab( rR.pIR->GetCurrScTab() )
159 {
160  sal_uInt16 nCref;
161  sal_uInt8 nName, nComment;
162 
163  nCref = rIn.ReaduInt16();
164  nProtected = rIn.ReaduInt8();
165  rIn.Ignore( 1 ); // Hide
166  nName = rIn.ReaduInt8();
167  nComment = rIn.ReaduInt8();
168  rIn.Ignore( 1 ); // instead of nUser!
169 
170  if( nName )
171  aName = rIn.ReadUniString( nName );
172  else
173  {
174  aName = "Scenery";
175  rIn.Ignore( 1 );
176  }
177 
178  rIn.ReadUniString(); // username
179 
180  if( nComment )
181  aComment = rIn.ReadUniString();
182 
183  sal_uInt16 n = nCref;
184  sal_uInt16 nC, nR;
185  aEntries.reserve(n);
186  while( n )
187  {
188  nR = rIn.ReaduInt16();
189  nC = rIn.ReaduInt16();
190 
191  aEntries.emplace_back( nC, nR );
192 
193  n--;
194  }
195 
196  for (auto& rEntry : aEntries)
197  rEntry.SetValue(rIn.ReadUniString());
198 }
199 
200 void ExcScenario::Apply( const XclImpRoot& rRoot, const bool bLast )
201 {
202  ScDocument& r = rRoot.GetDoc();
203  OUString aSzenName( aName );
204  sal_uInt16 nNewTab = nTab + 1;
205 
206  if( !r.InsertTab( nNewTab, aSzenName ) )
207  return;
208 
209  r.SetScenario( nNewTab, true );
210  // do not show scenario frames
213  /* | ScScenarioFlags::ShowFrame*/
214  r.SetScenarioData( nNewTab, aComment, COL_LIGHTGRAY, nFlags);
215 
216  for (const auto& rEntry : aEntries)
217  {
218  sal_uInt16 nCol = rEntry.nCol;
219  sal_uInt16 nRow = rEntry.nRow;
220  OUString aVal = rEntry.GetValue();
221 
222  r.ApplyFlagsTab( nCol, nRow, nCol, nRow, nNewTab, ScMF::Scenario );
223 
224  r.SetString( nCol, nRow, nNewTab, aVal );
225  }
226 
227  if( bLast )
228  r.SetActiveScenario( nNewTab, true );
229 
230  // modify what the Active tab is set to if the new
231  // scenario tab occurs before the active tab.
232  ScExtDocSettings& rDocSett = rRoot.GetExtDocOptions().GetDocSettings();
233  if( (static_cast< SCCOL >( nTab ) < rDocSett.mnDisplTab) && (rDocSett.mnDisplTab < MAXTAB) )
234  ++rDocSett.mnDisplTab;
235  rRoot.GetTabInfo().InsertScTab( nNewTab );
236 }
237 
238 void ExcScenarioList::Apply( const XclImpRoot& rRoot )
239 {
240  sal_uInt16 n = static_cast<sal_uInt16>(aEntries.size());
241 
242  std::vector< std::unique_ptr<ExcScenario> >::reverse_iterator iter;
243  for (iter = aEntries.rbegin(); iter != aEntries.rend(); ++iter)
244  {
245  n--;
246  (*iter)->Apply(rRoot, n == nLastScenario);
247  }
248 }
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetOutlineArray(ScOutlineArray *pOArray)
Definition: exctools.cxx:84
SC_DLLPUBLIC void SetScenario(SCTAB nTab, bool bFlag)
Definition: documen3.cxx:407
std::unique_ptr< SharedFormulaBuffer > pShrfmlaBuff
Definition: root.hxx:51
std::unique_ptr< ScRangeListTabs > pPrintRanges
Definition: root.hxx:58
OUString aName
Definition: excscen.hxx:54
ScScenarioFlags
Definition: global.hxx:236
ExcScenario(XclImpStream &rIn, const RootData &rRoot)
Definition: exctools.cxx:157
sal_uInt8 mnMaxLevel
Definition: otlnbuff.hxx:47
SC_DLLPUBLIC bool ApplyFlagsTab(SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow, SCTAB nTab, ScMF nFlags)
Definition: document.cxx:5015
sal_Int64 n
XclImpOutlineBuffer(SCSIZE nNewSize)
Definition: exctools.cxx:62
const ScExtDocSettings & GetDocSettings() const
Definition: scextopt.cxx:170
constexpr::Color COL_LIGHTGRAY(0xC0, 0xC0, 0xC0)
void SetLevel(SCSIZE nIndex, sal_uInt8 nVal, bool bCollapsed)
Definition: exctools.cxx:75
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:278
void SetButtonMode(bool bRightOrUnder)
Definition: exctools.cxx:147
OutlineLevels maLevels
Definition: otlnbuff.hxx:43
sal_uInt8 nProtected
Definition: excscen.hxx:56
XclExpChTrTabId * pTabId
Definition: root.hxx:62
size_t SCSIZE
size_t typedef to be able to find places where code was changed from USHORT to size_t and is used to ...
Definition: address.hxx:45
XclExpUserBViewList * pUserBViewList
Definition: root.hxx:63
sal_uInt16 nLastScenario
Definition: excscen.hxx:67
std::unique_ptr< ScRangeListTabs > pPrintTitles
Definition: root.hxx:59
SC_DLLPUBLIC bool SetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString &rString, const ScSetStringParam *pParam=nullptr)
Definition: document.cxx:3366
std::unique_ptr< XclImpAutoFilterBuffer > pAutoFilterBuffer
Definition: root.hxx:57
SC_DLLPUBLIC void SetScenarioData(SCTAB nTab, const OUString &rComment, const Color &rColor, ScScenarioFlags nFlags)
Definition: documen3.cxx:418
ScOutlineArray * mpOutlineArray
Definition: otlnbuff.hxx:45
XclImpTabInfo & GetTabInfo() const
Returns the buffer that contains the sheet creation order.
Definition: xiroot.cxx:178
int i
SC_DLLPUBLIC void SetActiveScenario(SCTAB nTab, bool bActive)
Definition: documen3.cxx:863
ScExtDocOptions & GetExtDocOptions() const
Returns the extended document options.
Definition: xlroot.cxx:421
~RootData()
Definition: exctools.cxx:52
Extended settings for the document, used in import/export filters.
Definition: scextopt.hxx:28
::std::set< SCSIZE > maCollapsedPosSet
Definition: otlnbuff.hxx:44
SC_DLLPUBLIC bool InsertTab(SCTAB nPos, const OUString &rName, bool bExternalDocument=false, bool bUndoDeleteTab=false)
Definition: document.cxx:504
void InsertScTab(SCTAB nScTab)
Inserts a Calc sheet index (increases all following sheet indexes).
Definition: xilink.cxx:231
const SCTAB MAXTAB
Definition: address.hxx:71
sal_uInt16 ReaduInt16()
Definition: xistream.cxx:648
XclExpRoot * pER
Definition: root.hxx:66
const sal_uInt16 nTab
Definition: excscen.hxx:57
std::vector< std::unique_ptr< ExcScenario > > aEntries
Definition: excscen.hxx:68
OUString ReadUniString(sal_uInt16 nChars, sal_uInt8 nFlags)
Reads ext.
Definition: xistream.cxx:886
void SetLevelRange(SCSIZE nF, SCSIZE nL, sal_uInt8 nVal, bool bCollapsed)
Definition: exctools.cxx:135
std::unique_ptr< ExtNameBuff > pExtNameBuff
Definition: root.hxx:52
void Ignore(std::size_t nBytes)
Seeks forward inside the current record.
Definition: xistream.cxx:795
unsigned char sal_uInt8
sal_uInt8 ReaduInt8()
Definition: xistream.cxx:616
XclImpRoot * pIR
Definition: root.hxx:65
field button for datapilot
std::vector< ExcScenarioCell > aEntries
Definition: excscen.hxx:58
ExcelToSc * pFmlaConverter
Definition: root.hxx:53
ExcScenarioCell(const sal_uInt16 nC, const sal_uInt16 nR)
Definition: exctools.cxx:152
RootData()
Definition: exctools.cxx:36
void Apply(const XclImpRoot &rRoot, const bool bLast)
Definition: exctools.cxx:200
OUString aComment
Definition: excscen.hxx:55
void Apply(const XclImpRoot &rRoot)
Definition: exctools.cxx:238
This class is used to import record oriented streams.
Definition: xistream.hxx:278
BiffTyp eDateiTyp
Definition: root.hxx:49
XclImpColRowSettings * pColRowBuff
Definition: root.hxx:54
Access to global data from other classes.
Definition: xiroot.hxx:126
bool Insert(SCCOLROW nStartPos, SCCOLROW nEndPos, bool &rSizeChanged, bool bHidden=false)
Definition: olinetab.cxx:197
std::unique_ptr< ExtSheetBuffer > pExtSheetBuff
Definition: root.hxx:50
sal_uInt16 nPos
SCTAB mnDisplTab
Index of displayed sheet.
Definition: scextopt.hxx:33