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