LibreOffice Module sw (master)  1
tblcalc.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 <sal/config.h>
21 
22 #include <o3tl/any.hxx>
23 
24 #include <calc.hxx>
25 #include <doc.hxx>
26 #include <ndtxt.hxx>
27 #include <fmtfld.hxx>
28 #include <txtfld.hxx>
29 #include <expfld.hxx>
30 #include <unofldmid.h>
31 
32 using namespace ::com::sun::star;
33 
35  : SwValueFieldType( pDocPtr, SwFieldIds::Table )
36 {}
37 
38 std::unique_ptr<SwFieldType> SwTableFieldType::Copy() const
39 {
40  return std::make_unique<SwTableFieldType>(GetDoc());
41 }
42 
44 {
45  if( rCalcPara.m_rCalc.IsCalcError() ) // stop if there is already an error set
46  return;
47 
48  // create pointers from box name
49  BoxNmToPtr( rCalcPara.m_pTable );
50  OUString sFormula( MakeFormula( rCalcPara ));
51  SetValue( rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble() );
52  ChgValid( !rCalcPara.IsStackOverflow() ); // is the value again valid?
53 }
54 
55 SwTableField::SwTableField( SwTableFieldType* pInitType, const OUString& rFormel,
56  sal_uInt16 nType, sal_uLong nFormat )
57  : SwValueField( pInitType, nFormat ), SwTableFormula( rFormel ),
58  m_nSubType(nType)
59 {
60  m_sExpand = "0";
61 }
62 
63 std::unique_ptr<SwField> SwTableField::Copy() const
64 {
65  std::unique_ptr<SwTableField> pTmp(new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
67  pTmp->m_sExpand = m_sExpand;
68  pTmp->SwValueField::SetValue(GetValue());
69  pTmp->SwTableFormula::operator=( *this );
70  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
71  return std::unique_ptr<SwField>(pTmp.release());
72 }
73 
75 {
76  return GetTyp()->GetName() + " " + const_cast<SwTableField *>(this)->GetCommand();
77 }
78 
81 {
82  auto pFormat = GetTyp()->FindFormatForField(this);
83  return pFormat ? &pFormat->GetTextField()->GetTextNode() : nullptr;
84 }
85 
87 {
88  if (EXTRNL_NAME != GetNameType())
89  {
90  SwNode const*const pNd = GetNodeOfFormula();
91  SwTableNode const*const pTableNd = pNd ? pNd->FindTableNode() : nullptr;
92  if (pTableNd)
93  {
94  PtrToBoxNm( &pTableNd->GetTable() );
95  }
96  }
97  return (EXTRNL_NAME == GetNameType())
99  : OUString();
100 }
101 
102 OUString SwTableField::ExpandImpl(SwRootFrame const*const) const
103 {
105  {
106  return const_cast<SwTableField *>(this)->GetCommand();
107  }
108 
110  {
111  // it is a string
112  return m_sExpand.copy(1, m_sExpand.getLength()-2);
113  }
114 
115  return m_sExpand;
116 }
117 
118 sal_uInt16 SwTableField::GetSubType() const
119 {
120  return m_nSubType;
121 }
122 
123 void SwTableField::SetSubType(sal_uInt16 nType)
124 {
125  m_nSubType = nType;
126 }
127 
128 void SwTableField::SetValue( const double& rVal )
129 {
131  m_sExpand = static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
132 }
133 
134 OUString SwTableField::GetPar2() const
135 {
137 }
138 
139 void SwTableField::SetPar2(const OUString& rStr)
140 {
141  SetFormula( rStr );
142 }
143 
144 bool SwTableField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
145 {
146  bool bRet = true;
147  switch ( nWhichId )
148  {
149  case FIELD_PROP_PAR2:
150  {
151  sal_uInt16 nOldSubType = m_nSubType;
152  SwTableField* pThis = const_cast<SwTableField*>(this);
154  rAny <<= ExpandImpl(nullptr);
155  pThis->m_nSubType = nOldSubType;
156  }
157  break;
158  case FIELD_PROP_BOOL1:
159  rAny <<= 0 != (nsSwExtendedSubType::SUB_CMD & m_nSubType);
160  break;
161  case FIELD_PROP_PAR1:
162  rAny <<= m_sExpand;
163  break;
164  case FIELD_PROP_FORMAT:
165  rAny <<= static_cast<sal_Int32>(GetFormat());
166  break;
167  default:
168  bRet = false;
169  }
170  return bRet;
171 }
172 
173 bool SwTableField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
174 {
175  bool bRet = true;
176  switch ( nWhichId )
177  {
178  case FIELD_PROP_PAR2:
179  {
180  OUString sTmp;
181  rAny >>= sTmp;
182  SetFormula( sTmp );
183  }
184  break;
185  case FIELD_PROP_BOOL1:
186  if(*o3tl::doAccess<bool>(rAny))
188  else
190  break;
191  case FIELD_PROP_PAR1:
192  {
193  OUString sTmp;
194  rAny >>= sTmp;
195  ChgExpStr( sTmp );
196  }
197  break;
198  case FIELD_PROP_FORMAT:
199  {
200  sal_Int32 nTmp = 0;
201  rAny >>= nTmp;
202  SetFormat(nTmp);
203  }
204  break;
205  default:
206  bRet = false;
207  }
208  return bRet;
209 }
210 
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_uInt16 GetSubType() const override
Definition: tblcalc.cxx:118
void CalcField(SwTableCalcPara &rCalcPara)
Definition: tblcalc.cxx:43
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:402
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:397
sal_uIntPtr sal_uLong
const SwTable * m_pTable
current table
Definition: cellfml.hxx:48
bool IsCalcError() const
Definition: calc.hxx:246
virtual void SetValue(const double &rVal) override
Definition: tblcalc.cxx:128
Definition: doc.hxx:186
SwFormatField * FindFormatForField(const SwField *) const
Definition: fldbas.cxx:171
SwTableFieldType(SwDoc *pDocPtr)
Definition: tblcalc.cxx:34
virtual void SetSubType(sal_uInt16 nType) override
Definition: tblcalc.cxx:123
OUString MakeFormula(SwTableCalcPara &rCalcPara) const
Definition: cellfml.hxx:106
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: tblcalc.cxx:144
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: tblcalc.cxx:173
void BoxNmToPtr(const SwTable *pTable)
create from the external formula the internal
Definition: cellfml.cxx:596
OUString GetCommand()
Definition: tblcalc.cxx:86
OUString m_sExpand
Definition: expfld.hxx:375
NameType GetNameType() const
Definition: cellfml.hxx:133
const SwTextField * GetTextField() const
Definition: fmtfld.hxx:128
virtual void SetPar2(const OUString &rStr) override
Definition: tblcalc.cxx:139
const SwTable & GetTable() const
Definition: node.hxx:500
Implementation in tblcalc.cxx.
Definition: expfld.hxx:366
sal_uInt16 m_nSubType
Definition: expfld.hxx:376
void SetFormula(const OUString &rNew)
Definition: cellfml.hxx:139
SW_DLLPUBLIC void PtrToBoxNm(const SwTable *pTable)
create from the internal formula (for CORE) the external formula (for UI)
Definition: cellfml.cxx:571
virtual OUString GetFieldName() const override
get name or content
Definition: tblcalc.cxx:74
double GetDouble() const
Definition: calc.cxx:1468
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:211
SwFieldIds
Definition: fldbas.hxx:44
virtual OUString GetPar2() const override
The formula.
Definition: tblcalc.cxx:134
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:303
bool IsStackOverflow() const
Definition: cellfml.hxx:54
SwDoc * GetDoc() const
Definition: fldbas.hxx:419
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:379
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: tblcalc.cxx:38
SwFieldType * GetTyp() const
Definition: fldbas.hxx:392
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:408
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:358
void ChgValid(bool bNew)
Definition: cellfml.hxx:136
const OUString & GetFormula() const
Definition: cellfml.hxx:138
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:136
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
SwTableField(SwTableFieldType *, const OUString &rFormel, sal_uInt16 nSubType, sal_uLong nFormat)
Definition: tblcalc.cxx:55
const SwGetSetExpType GSE_FORMULA
Formula.
Definition: fldbas.hxx:205
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:355
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: tblcalc.cxx:102
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:202
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:721
virtual const SwNode * GetNodeOfFormula() const override
Search TextNode containing the field.
Definition: tblcalc.cxx:80
SwTextNode & GetTextNode() const
Definition: txtfld.hxx:53
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:460
virtual double GetValue() const
Definition: fldbas.cxx:716
void ChgExpStr(const OUString &rStr)
Definition: expfld.hxx:394
SwCalc & m_rCalc
current Calculator
Definition: cellfml.hxx:47
virtual std::unique_ptr< SwField > Copy() const override
Definition: tblcalc.cxx:63
Base class of the Writer document model elements.
Definition: node.hxx:79