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  if( !GetTyp()->HasWriterListeners() )
84  return nullptr;
85 
87  for( SwFormatField* pFormatField = aIter.First(); pFormatField; pFormatField = aIter.Next() )
88  if( this == pFormatField->GetField() )
89  return &pFormatField->GetTextField()->GetTextNode();
90  return nullptr;
91 }
92 
94 {
95  if (EXTRNL_NAME != GetNameType())
96  {
97  SwNode const*const pNd = GetNodeOfFormula();
98  SwTableNode const*const pTableNd = pNd ? pNd->FindTableNode() : nullptr;
99  if (pTableNd)
100  {
101  PtrToBoxNm( &pTableNd->GetTable() );
102  }
103  }
104  return (EXTRNL_NAME == GetNameType())
106  : OUString();
107 }
108 
109 OUString SwTableField::ExpandImpl(SwRootFrame const*const) const
110 {
112  {
113  return const_cast<SwTableField *>(this)->GetCommand();
114  }
115 
117  {
118  // it is a string
119  return m_sExpand.copy(1, m_sExpand.getLength()-2);
120  }
121 
122  return m_sExpand;
123 }
124 
125 sal_uInt16 SwTableField::GetSubType() const
126 {
127  return m_nSubType;
128 }
129 
130 void SwTableField::SetSubType(sal_uInt16 nType)
131 {
132  m_nSubType = nType;
133 }
134 
135 void SwTableField::SetValue( const double& rVal )
136 {
138  m_sExpand = static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
139 }
140 
141 OUString SwTableField::GetPar2() const
142 {
144 }
145 
146 void SwTableField::SetPar2(const OUString& rStr)
147 {
148  SetFormula( rStr );
149 }
150 
151 bool SwTableField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
152 {
153  bool bRet = true;
154  switch ( nWhichId )
155  {
156  case FIELD_PROP_PAR2:
157  {
158  sal_uInt16 nOldSubType = m_nSubType;
159  SwTableField* pThis = const_cast<SwTableField*>(this);
161  rAny <<= ExpandImpl(nullptr);
162  pThis->m_nSubType = nOldSubType;
163  }
164  break;
165  case FIELD_PROP_BOOL1:
166  rAny <<= 0 != (nsSwExtendedSubType::SUB_CMD & m_nSubType);
167  break;
168  case FIELD_PROP_PAR1:
169  rAny <<= m_sExpand;
170  break;
171  case FIELD_PROP_FORMAT:
172  rAny <<= static_cast<sal_Int32>(GetFormat());
173  break;
174  default:
175  bRet = false;
176  }
177  return bRet;
178 }
179 
180 bool SwTableField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
181 {
182  bool bRet = true;
183  switch ( nWhichId )
184  {
185  case FIELD_PROP_PAR2:
186  {
187  OUString sTmp;
188  rAny >>= sTmp;
189  SetFormula( sTmp );
190  }
191  break;
192  case FIELD_PROP_BOOL1:
193  if(*o3tl::doAccess<bool>(rAny))
195  else
197  break;
198  case FIELD_PROP_PAR1:
199  {
200  OUString sTmp;
201  rAny >>= sTmp;
202  ChgExpStr( sTmp );
203  }
204  break;
205  case FIELD_PROP_FORMAT:
206  {
207  sal_Int32 nTmp = 0;
208  rAny >>= nTmp;
209  SetFormat(nTmp);
210  }
211  break;
212  default:
213  bRet = false;
214  }
215  return bRet;
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_uInt16 GetSubType() const override
Definition: tblcalc.cxx:125
void CalcField(SwTableCalcPara &rCalcPara)
Definition: tblcalc.cxx:44
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:393
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:388
sal_uIntPtr sal_uLong
const SwTable * m_pTable
current table
Definition: cellfml.hxx:46
bool IsCalcError() const
Definition: calc.hxx:238
virtual void SetValue(const double &rVal) override
Definition: tblcalc.cxx:135
Definition: doc.hxx:185
TElementType * Next()
Definition: calbck.hxx:373
SwTableFieldType(SwDoc *pDocPtr)
Definition: tblcalc.cxx:35
virtual void SetSubType(sal_uInt16 nType) override
Definition: tblcalc.cxx:130
OUString MakeFormula(SwTableCalcPara &rCalcPara) const
Definition: cellfml.hxx:104
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: tblcalc.cxx:151
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: tblcalc.cxx:180
void BoxNmToPtr(const SwTable *pTable)
create from the external formula the internal
Definition: cellfml.cxx:567
OUString GetCommand()
Definition: tblcalc.cxx:93
OUString m_sExpand
Definition: expfld.hxx:375
NameType GetNameType() const
Definition: cellfml.hxx:131
virtual void SetPar2(const OUString &rStr) override
Definition: tblcalc.cxx:146
const SwTable & GetTable() const
Definition: node.hxx:497
Implementation in tblcalc.cxx.
Definition: expfld.hxx:366
sal_uInt16 m_nSubType
Definition: expfld.hxx:376
void SetFormula(const OUString &rNew)
Definition: cellfml.hxx:137
void PtrToBoxNm(const SwTable *pTable)
create from the internal formula (for CORE) the external formula (for UI)
Definition: cellfml.cxx:542
virtual OUString GetFieldName() const override
get name or content
Definition: tblcalc.cxx:75
double GetDouble() const
Definition: calc.cxx:1410
const SwExtendedSubType SUB_CMD
Show command.
Definition: fldbas.hxx:205
TElementType * First()
Definition: calbck.hxx:342
SwFieldIds
Definition: fldbas.hxx:38
virtual OUString GetPar2() const override
The formula.
Definition: tblcalc.cxx:141
void SetFormat(sal_uInt32 const nSet)
Definition: fldbas.hxx:294
bool IsStackOverflow() const
Definition: cellfml.hxx:52
SwDoc * GetDoc() const
Definition: fldbas.hxx:410
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:370
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: tblcalc.cxx:39
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:399
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:347
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:199
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:349
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: tblcalc.cxx:109
const SwGetSetExpType GSE_STRING
String.
Definition: fldbas.hxx:196
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:672
virtual const SwNode * GetNodeOfFormula() const override
Search TextNode containing the field.
Definition: tblcalc.cxx:81
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:451
virtual double GetValue() const
Definition: fldbas.cxx:667
void ChgExpStr(const OUString &rStr)
Definition: expfld.hxx:394
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