tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / core / data / simpleformulacalc.cxx
blobcd2559e9f22f65bf0629ea021a3d21dbb541913d
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/.
8 */
10 #include <memory>
11 #include <simpleformulacalc.hxx>
12 #include <document.hxx>
13 #include <docsh.hxx>
14 #include <tokenarray.hxx>
15 #include <interpre.hxx>
16 #include <compiler.hxx>
17 #include <sfx2/linkmgr.hxx>
19 #define DISPLAY_LEN 66
21 ScSimpleFormulaCalculator::ScSimpleFormulaCalculator( ScDocument& rDoc, const ScAddress& rAddr,
22 const OUString& rFormula, bool bMatrixFormula, formula::FormulaGrammar::Grammar eGram )
23 : mnFormatType(SvNumFormatType::ALL)
24 , mbCalculated(false)
25 , maAddr(rAddr)
26 , mrDoc(rDoc)
27 , maGram(eGram)
28 , mbMatrixResult(false)
29 , mbLimitString(false)
30 , mbMatrixFormula(bMatrixFormula)
32 // compile already here
33 ScCompiler aComp(mrDoc, maAddr, eGram, true, bMatrixFormula);
34 mpCode = aComp.CompileString(rFormula);
35 if(mpCode->GetCodeError() == FormulaError::NONE && mpCode->GetLen())
36 aComp.CompileTokenArray();
39 ScSimpleFormulaCalculator::~ScSimpleFormulaCalculator()
43 void ScSimpleFormulaCalculator::Calculate()
45 if(mbCalculated)
46 return;
48 mbCalculated = true;
50 ScInterpreter aInt(mrDoc.GetFormulaCell( maAddr ), mrDoc, mrDoc.GetNonThreadedContext(), maAddr, *mpCode);
51 if (mbMatrixFormula)
52 aInt.AssertFormulaMatrix();
54 sfx2::LinkManager aNewLinkMgr( mrDoc.GetDocumentShell() );
55 aInt.SetLinkManager( &aNewLinkMgr );
57 formula::StackVar aIntType = aInt.Interpret();
58 if ( aIntType == formula::svMatrixCell )
60 ScCompiler aComp(mrDoc, maAddr, maGram);
61 OUStringBuffer aStr;
62 aComp.CreateStringFromToken(aStr, aInt.GetResultToken().get());
64 mbMatrixResult = true;
66 if (mbLimitString)
68 const sal_Unicode cCol = ScCompiler::GetNativeSymbol(ocArrayColSep)[0];
69 const sal_Unicode cRow = ScCompiler::GetNativeSymbol(ocArrayRowSep)[0];
70 const sal_Int32 n = aStr.getLength();
71 for (sal_Int32 i = DISPLAY_LEN; i < n; ++i)
73 const sal_Unicode c = aStr[i];
74 if (c == cCol || c == cRow)
76 aStr.truncate(i+1);
77 aStr.append("...");
78 break;
83 maMatrixFormulaResult = aStr.makeStringAndClear();
85 mnFormatType = aInt.GetRetFormatType();
86 maResult.SetToken(aInt.GetResultToken().get());
89 bool ScSimpleFormulaCalculator::IsValue()
91 Calculate();
93 if (mbMatrixResult)
94 return false;
96 return maResult.IsValue();
99 bool ScSimpleFormulaCalculator::IsMatrix()
101 Calculate();
103 return mbMatrixResult;
106 FormulaError ScSimpleFormulaCalculator::GetErrCode()
108 Calculate();
110 FormulaError nErr = mpCode->GetCodeError();
111 if (nErr != FormulaError::NONE)
112 return nErr;
113 return maResult.GetResultError();
116 double ScSimpleFormulaCalculator::GetValue()
118 Calculate();
120 if ((mpCode->GetCodeError() == FormulaError::NONE) &&
121 maResult.GetResultError() == FormulaError::NONE)
122 return maResult.GetDouble();
124 return 0.0;
127 svl::SharedString ScSimpleFormulaCalculator::GetString()
129 Calculate();
131 if (mbMatrixResult)
132 return svl::SharedString( maMatrixFormulaResult); // string not interned
134 if ((mpCode->GetCodeError() == FormulaError::NONE) &&
135 maResult.GetResultError() == FormulaError::NONE)
136 return maResult.GetString();
138 return svl::SharedString::getEmptyString();
141 bool ScSimpleFormulaCalculator::HasColRowName() const
143 return formula::FormulaTokenArrayPlainIterator(*mpCode).GetNextColRowName() != nullptr;
146 ScTokenArray* ScSimpleFormulaCalculator::GetCode()
148 return mpCode.get();
151 void ScSimpleFormulaCalculator::SetLimitString(bool bLimitString)
153 mbLimitString = bLimitString;
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */