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/numformat.hxx>
26#include <svl/zforlist.hxx>
27#include <com/sun/star/util/DateTime.hpp>
28#include <doc.hxx>
29#include <fldbas.hxx>
30#include <flddat.hxx>
31#include <unofldmid.h>
32
33using namespace ::com::sun::star;
34
37{}
38
39std::unique_ptr<SwFieldType> SwDateTimeFieldType::Copy() const
40{
41 return std::make_unique<SwDateTimeFieldType>(GetDoc());
42}
43
45 : SwValueField(pInitType, nFormat, nLng, 0.0),
46 m_nSubType(nSub),
47 m_nOffset(0)
48{
49 if (!nFormat)
50 {
52 if (m_nSubType & DATEFLD)
54 else
56 }
57 if (IsFixed())
58 {
59 DateTime aDateTime( DateTime::SYSTEM );
60 SetDateTime(aDateTime);
61 }
62}
63
64OUString SwDateTimeField::ExpandImpl(SwRootFrame const*const) const
65{
66 if (getenv("STABLE_FIELDS_HACK"))
67 {
68 const_cast<SwDateTimeField*>(this)->m_nSubType |= FIXEDFLD; //HACK
69 }
70
71 double fVal;
72
73 if (!(IsFixed()))
74 {
75 DateTime aDateTime( DateTime::SYSTEM );
76 fVal = GetDateTime(*GetDoc(), aDateTime);
77 }
78 else
79 fVal = GetValue();
80
81 if (m_nOffset)
82 fVal += m_nOffset * ( 60 / 86400.0 );
83
84 return ExpandValue(fVal, GetFormat(), GetLanguage());
85}
86
87std::unique_ptr<SwField> SwDateTimeField::Copy() const
88{
89 std::unique_ptr<SwDateTimeField> pTmp(
91 GetFormat(), GetLanguage()) );
92
93 pTmp->SetValue(GetValue());
94 pTmp->SetOffset(m_nOffset);
95 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
96
97 return std::unique_ptr<SwField>(pTmp.release());
98}
99
101{
102 return m_nSubType;
103}
104
105void SwDateTimeField::SetSubType(sal_uInt16 nType)
106{
108}
109
110void SwDateTimeField::SetPar2(const OUString& rStr)
111{
112 m_nOffset = rStr.toInt32();
113}
114
116{
117 if (m_nOffset)
118 return OUString::number(m_nOffset);
119 return OUString();
120}
121
123{
124 SetValue(GetDateTime(*GetDoc(), rDT));
125}
126
128{
129 SvNumberFormatter* pFormatter = rDoc.GetNumberFormatter();
130 const Date& rNullDate = pFormatter->GetNullDate();
131
132 double fResult = DateTime::Sub(rDT, DateTime(rNullDate));
133
134 return fResult;
135}
136
138{
139 if (getenv("STABLE_FIELDS_HACK"))
140 {
141 const_cast<SwDateTimeField*>(this)->m_nSubType |= FIXEDFLD; //HACK
142 }
143
144 if (IsFixed())
145 return SwValueField::GetValue();
146 else
148}
149
151{
153 const Date& rNullDate = pFormatter->GetNullDate();
154
155 tools::Long nVal = static_cast<tools::Long>( GetValue() );
156
157 Date aDate = rNullDate + nVal;
158
159 return aDate;
160}
161
163{
164 double fFract = modf(GetValue(), &o3tl::temporary(double()));
166 aDT.AddTime(fFract);
167 return static_cast<tools::Time>(aDT);
168}
169
170bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
171{
172 switch( nWhichId )
173 {
174 case FIELD_PROP_BOOL1:
175 rVal <<= IsFixed();
176 break;
177 case FIELD_PROP_BOOL2:
178 rVal <<= (m_nSubType & DATEFLD) != 0;
179 break;
181 rVal <<= static_cast<sal_Int32>(GetFormat());
182 break;
184 rVal <<= static_cast<sal_Int32>(m_nOffset);
185 break;
187 {
188 DateTime aDateTime(GetDate(), GetTime());
189 rVal <<= aDateTime.GetUNODateTime();
190 }
191 break;
192 default:
193 return SwField::QueryValue(rVal, nWhichId);
194 }
195 return true;
196}
197
198bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
199{
200 sal_Int32 nTmp = 0;
201 switch( nWhichId )
202 {
203 case FIELD_PROP_BOOL1:
204 if(*o3tl::doAccess<bool>(rVal))
206 else
207 m_nSubType &= ~FIXEDFLD;
208 break;
209 case FIELD_PROP_BOOL2:
211 m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
212 break;
214 rVal >>= nTmp;
215 ChangeFormat(nTmp);
216 break;
218 rVal >>= nTmp;
219 m_nOffset = nTmp;
220 break;
222 {
223 util::DateTime aDateTimeValue;
224 if(!(rVal >>= aDateTimeValue))
225 return false;
226 DateTime aDateTime(aDateTimeValue);
227 SetDateTime(aDateTime);
228 }
229 break;
230 default:
231 return SwField::PutValue(rVal, nWhichId);
232 }
233 return true;
234}
235
236/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddTime(double fTimeInDays)
css::util::DateTime GetUNODateTime() const
static double Sub(const DateTime &rDateTime1, const DateTime &rDateTime2)
const Date & GetNullDate() const
sal_uInt32 GetFormatIndex(NfIndexTableOffset, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: flddat.cxx:39
SwDateTimeFieldType(SwDoc *pDoc)
Definition: flddat.cxx:35
virtual sal_uInt16 GetSubType() const override
Definition: flddat.cxx:100
virtual std::unique_ptr< SwField > Copy() const override
Definition: flddat.cxx:87
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: flddat.cxx:64
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nMId) const override
Definition: flddat.cxx:170
Date GetDate() const
Definition: flddat.cxx:150
tools::Long m_nOffset
Definition: flddat.hxx:48
tools::Time GetTime() const
Definition: flddat.cxx:162
static double GetDateTime(SwDoc &rDoc, const DateTime &rDT)
Definition: flddat.cxx:127
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: flddat.cxx:198
virtual void SetSubType(sal_uInt16 nSub) override
Definition: flddat.cxx:105
virtual double GetValue() const override
Definition: flddat.cxx:137
SwDateTimeField(SwDateTimeFieldType *pType, sal_uInt16 nSubType=DATEFLD, sal_uLong nFormat=0, LanguageType nLng=LANGUAGE_SYSTEM)
Definition: flddat.cxx:44
sal_uInt16 m_nSubType
Definition: flddat.hxx:47
void SetDateTime(const DateTime &rDT)
Definition: flddat.cxx:122
virtual void SetPar2(const OUString &rStr) override
Definition: flddat.cxx:110
virtual OUString GetPar2() const override
Definition: flddat.cxx:115
Definition: doc.hxx:197
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1429
void ChangeFormat(sal_uInt32 n)
Definition: fldbas.cxx:454
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:387
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:407
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:364
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:382
SwFieldType * GetTyp() const
Definition: fldbas.hxx:402
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:412
bool IsFixed() const
Definition: fldbas.cxx:459
The root element of a Writer document layout.
Definition: rootfrm.hxx:85
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:419
SwDoc * GetDoc() const
Definition: fldbas.hxx:429
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:472
virtual double GetValue() const
Definition: fldbas.cxx:799
SwDoc * GetDoc() const
Definition: fldbas.hxx:465
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:804
@ FIXEDFLD
Definition: fldbas.hxx:233
@ DATEFLD
Definition: fldbas.hxx:234
@ TIMEFLD
Definition: fldbas.hxx:235
SwFieldIds
Definition: fldbas.hxx:49
constexpr T & temporary(T &&x)
long Long
QPRO_FUNC_TYPE nType
sal_uIntPtr sal_uLong
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
#define FIELD_PROP_DATE_TIME
Definition: unofldmid.h:38
#define FIELD_PROP_BOOL2
Definition: unofldmid.h:29
#define FIELD_PROP_SUBTYPE
Definition: unofldmid.h:27
#define FIELD_PROP_FORMAT
Definition: unofldmid.h:26
NF_DATE_SYSTEM_SHORT
NF_TIME_HHMMSS