LibreOffice Module sw (master)  1
flddat.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 #include <o3tl/temporary.hxx>
24 #include <tools/datetime.hxx>
25 #include <svl/zforlist.hxx>
26 #include <com/sun/star/util/DateTime.hpp>
27 #include <doc.hxx>
28 #include <fldbas.hxx>
29 #include <flddat.hxx>
30 #include <unofldmid.h>
31 
32 using namespace ::com::sun::star;
33 
35  : SwValueFieldType( pInitDoc, SwFieldIds::DateTime )
36 {}
37 
38 std::unique_ptr<SwFieldType> SwDateTimeFieldType::Copy() const
39 {
40  return std::make_unique<SwDateTimeFieldType>(GetDoc());
41 }
42 
44  : SwValueField(pInitType, nFormat, nLng, 0.0),
45  m_nSubType(nSub),
46  m_nOffset(0)
47 {
48  if (!nFormat)
49  {
50  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
51  if (m_nSubType & DATEFLD)
53  else
55  }
56  if (IsFixed())
57  {
58  DateTime aDateTime( DateTime::SYSTEM );
59  SetDateTime(aDateTime);
60  }
61 }
62 
63 OUString SwDateTimeField::ExpandImpl(SwRootFrame const*const) const
64 {
65  double fVal;
66 
67  if (!(IsFixed()))
68  {
69  DateTime aDateTime( DateTime::SYSTEM );
70  fVal = GetDateTime(GetDoc(), aDateTime);
71  }
72  else
73  fVal = GetValue();
74 
75  if (m_nOffset)
76  fVal += m_nOffset * ( 60 / 86400.0 );
77 
78  return ExpandValue(fVal, GetFormat(), GetLanguage());
79 }
80 
81 std::unique_ptr<SwField> SwDateTimeField::Copy() const
82 {
83  std::unique_ptr<SwDateTimeField> pTmp(
84  new SwDateTimeField(static_cast<SwDateTimeFieldType*>(GetTyp()), m_nSubType,
85  GetFormat(), GetLanguage()) );
86 
87  pTmp->SetValue(GetValue());
88  pTmp->SetOffset(m_nOffset);
89  pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
90 
91  return std::unique_ptr<SwField>(pTmp.release());
92 }
93 
94 sal_uInt16 SwDateTimeField::GetSubType() const
95 {
96  return m_nSubType;
97 }
98 
99 void SwDateTimeField::SetSubType(sal_uInt16 nType)
100 {
101  m_nSubType = nType;
102 }
103 
104 void SwDateTimeField::SetPar2(const OUString& rStr)
105 {
106  m_nOffset = rStr.toInt32();
107 }
108 
109 OUString SwDateTimeField::GetPar2() const
110 {
111  if (m_nOffset)
112  return OUString::number(m_nOffset);
113  return OUString();
114 }
115 
117 {
118  SetValue(GetDateTime(GetDoc(), rDT));
119 }
120 
121 double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
122 {
123  SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
124  const Date& rNullDate = pFormatter->GetNullDate();
125 
126  double fResult = rDT - DateTime(rNullDate);
127 
128  return fResult;
129 }
130 
132 {
133  if (IsFixed())
134  return SwValueField::GetValue();
135  else
137 }
138 
140 {
141  SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
142  const Date& rNullDate = pFormatter->GetNullDate();
143 
144  long nVal = static_cast<long>( GetValue() );
145 
146  Date aDate = rNullDate + nVal;
147 
148  return aDate;
149 }
150 
152 {
153  double fFract = modf(GetValue(), &o3tl::temporary(double()));
154  DateTime aDT( DateTime::EMPTY );
155  aDT.AddTime(fFract);
156  return static_cast<tools::Time>(aDT);
157 }
158 
159 bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
160 {
161  switch( nWhichId )
162  {
163  case FIELD_PROP_BOOL1:
164  rVal <<= IsFixed();
165  break;
166  case FIELD_PROP_BOOL2:
167  rVal <<= (m_nSubType & DATEFLD) != 0;
168  break;
169  case FIELD_PROP_FORMAT:
170  rVal <<= static_cast<sal_Int32>(GetFormat());
171  break;
172  case FIELD_PROP_SUBTYPE:
173  rVal <<= static_cast<sal_Int32>(m_nOffset);
174  break;
176  {
177  DateTime aDateTime(GetDate(), GetTime());
178  rVal <<= aDateTime.GetUNODateTime();
179  }
180  break;
181  default:
182  return SwField::QueryValue(rVal, nWhichId);
183  }
184  return true;
185 }
186 
187 bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
188 {
189  sal_Int32 nTmp = 0;
190  switch( nWhichId )
191  {
192  case FIELD_PROP_BOOL1:
193  if(*o3tl::doAccess<bool>(rVal))
194  m_nSubType |= FIXEDFLD;
195  else
196  m_nSubType &= ~FIXEDFLD;
197  break;
198  case FIELD_PROP_BOOL2:
199  m_nSubType &= ~(DATEFLD|TIMEFLD);
200  m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
201  break;
202  case FIELD_PROP_FORMAT:
203  rVal >>= nTmp;
204  ChangeFormat(nTmp);
205  break;
206  case FIELD_PROP_SUBTYPE:
207  rVal >>= nTmp;
208  m_nOffset = nTmp;
209  break;
211  {
212  util::DateTime aDateTimeValue;
213  if(!(rVal >>= aDateTimeValue))
214  return false;
215  DateTime aDateTime( DateTime::EMPTY );
216  aDateTime.SetNanoSec(aDateTimeValue.NanoSeconds);
217  aDateTime.SetSec(aDateTimeValue.Seconds);
218  aDateTime.SetMin(aDateTimeValue.Minutes);
219  aDateTime.SetHour(aDateTimeValue.Hours);
220  aDateTime.SetDay(aDateTimeValue.Day);
221  aDateTime.SetMonth(aDateTimeValue.Month);
222  aDateTime.SetYear(aDateTimeValue.Year);
223  SetDateTime(aDateTime);
224  }
225  break;
226  default:
227  return SwField::PutValue(rVal, nWhichId);
228  }
229  return true;
230 }
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual double GetValue() const override
Definition: flddat.cxx:131
virtual void SetSubType(sal_uInt16 nSub) override
Definition: flddat.cxx:99
#define FIELD_PROP_DATE_TIME
Definition: unofldmid.h:38
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:393
void SetSec(sal_uInt16 nNewSec)
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:388
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1401
sal_uInt32 GetFormatIndex(NfIndexTableOffset, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual OUString GetPar2() const override
Definition: flddat.cxx:109
sal_uIntPtr sal_uLong
virtual sal_uInt16 GetSubType() const override
Definition: flddat.cxx:94
bool IsFixed() const
Definition: fldbas.cxx:372
Definition: doc.hxx:185
SwDoc * GetDoc() const
Definition: fldbas.hxx:444
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
#define FIELD_PROP_SUBTYPE
Definition: unofldmid.h:27
void SetDateTime(const DateTime &rDT)
Definition: flddat.cxx:116
virtual std::unique_ptr< SwField > Copy() const override
Definition: flddat.cxx:81
sal_uInt16 m_nSubType
Definition: flddat.hxx:47
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
static double GetDateTime(SwDoc *pDoc, const DateTime &rDT)
Definition: flddat.cxx:121
void SetHour(sal_uInt16 nNewHour)
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:306
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: flddat.cxx:38
virtual void SetPar2(const OUString &rStr) override
Definition: flddat.cxx:104
void SetYear(sal_Int16 nNewYear)
long m_nOffset
Definition: flddat.hxx:48
NF_TIME_HHMMSS
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:293
css::util::DateTime GetUNODateTime() const
void SetDay(sal_uInt16 nNewDay)
#define FIELD_PROP_BOOL2
Definition: unofldmid.h:29
SwFieldIds
Definition: fldbas.hxx:38
Date GetDate() const
Definition: flddat.cxx:139
SwDoc * GetDoc() const
Definition: fldbas.hxx:410
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:370
tools::Time GetTime() const
Definition: flddat.cxx:151
SwFieldType * GetTyp() const
Definition: fldbas.hxx:383
constexpr T & temporary(T &&x)
SwDateTimeField(SwDateTimeFieldType *pType, sal_uInt16 nSubType=DATEFLD, sal_uLong nFormat=0, LanguageType nLng=LANGUAGE_SYSTEM)
Definition: flddat.cxx:43
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:399
NF_DATE_SYSTEM_SHORT
void AddTime(double fTimeInDays)
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: flddat.cxx:63
SwDateTimeFieldType(SwDoc *pDoc)
Definition: flddat.cxx:34
void SetNanoSec(sal_uInt32 nNewNanoSec)
void SetMin(sal_uInt16 nNewMin)
void ChangeFormat(sal_uInt32 n)
Definition: fldbas.cxx:367
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:672
const Date & GetNullDate() const
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:451
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: flddat.cxx:187
virtual double GetValue() const
Definition: fldbas.cxx:667
void SetMonth(sal_uInt16 nNewMonth)
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: flddat.cxx:159