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 double fVal;
67
68 if (!(IsFixed()))
69 {
70 DateTime aDateTime( DateTime::SYSTEM );
71 fVal = GetDateTime(*GetDoc(), aDateTime);
72 }
73 else
74 fVal = GetValue();
75
76 if (m_nOffset)
77 fVal += m_nOffset * ( 60 / 86400.0 );
78
79 return ExpandValue(fVal, GetFormat(), GetLanguage());
80}
81
82std::unique_ptr<SwField> SwDateTimeField::Copy() const
83{
84 std::unique_ptr<SwDateTimeField> pTmp(
86 GetFormat(), GetLanguage()) );
87
88 pTmp->SetValue(GetValue());
89 pTmp->SetOffset(m_nOffset);
90 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
91
92 return std::unique_ptr<SwField>(pTmp.release());
93}
94
96{
97 return m_nSubType;
98}
99
100void SwDateTimeField::SetSubType(sal_uInt16 nType)
101{
103}
104
105void SwDateTimeField::SetPar2(const OUString& rStr)
106{
107 m_nOffset = rStr.toInt32();
108}
109
111{
112 if (m_nOffset)
113 return OUString::number(m_nOffset);
114 return OUString();
115}
116
118{
119 SetValue(GetDateTime(*GetDoc(), rDT));
120}
121
123{
124 SvNumberFormatter* pFormatter = rDoc.GetNumberFormatter();
125 const Date& rNullDate = pFormatter->GetNullDate();
126
127 double fResult = rDT - DateTime(rNullDate);
128
129 return fResult;
130}
131
133{
134 if (IsFixed())
135 return SwValueField::GetValue();
136 else
138}
139
141{
143 const Date& rNullDate = pFormatter->GetNullDate();
144
145 tools::Long nVal = static_cast<tools::Long>( GetValue() );
146
147 Date aDate = rNullDate + nVal;
148
149 return aDate;
150}
151
153{
154 double fFract = modf(GetValue(), &o3tl::temporary(double()));
156 aDT.AddTime(fFract);
157 return static_cast<tools::Time>(aDT);
158}
159
160bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
161{
162 switch( nWhichId )
163 {
164 case FIELD_PROP_BOOL1:
165 rVal <<= IsFixed();
166 break;
167 case FIELD_PROP_BOOL2:
168 rVal <<= (m_nSubType & DATEFLD) != 0;
169 break;
171 rVal <<= static_cast<sal_Int32>(GetFormat());
172 break;
174 rVal <<= static_cast<sal_Int32>(m_nOffset);
175 break;
177 {
178 DateTime aDateTime(GetDate(), GetTime());
179 rVal <<= aDateTime.GetUNODateTime();
180 }
181 break;
182 default:
183 return SwField::QueryValue(rVal, nWhichId);
184 }
185 return true;
186}
187
188bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
189{
190 sal_Int32 nTmp = 0;
191 switch( nWhichId )
192 {
193 case FIELD_PROP_BOOL1:
194 if(*o3tl::doAccess<bool>(rVal))
196 else
197 m_nSubType &= ~FIXEDFLD;
198 break;
199 case FIELD_PROP_BOOL2:
201 m_nSubType |= *o3tl::doAccess<bool>(rVal) ? DATEFLD : TIMEFLD;
202 break;
204 rVal >>= nTmp;
205 ChangeFormat(nTmp);
206 break;
208 rVal >>= nTmp;
209 m_nOffset = nTmp;
210 break;
212 {
213 util::DateTime aDateTimeValue;
214 if(!(rVal >>= aDateTimeValue))
215 return false;
216 DateTime aDateTime(aDateTimeValue);
217 SetDateTime(aDateTime);
218 }
219 break;
220 default:
221 return SwField::PutValue(rVal, nWhichId);
222 }
223 return true;
224}
225
226/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddTime(double fTimeInDays)
css::util::DateTime GetUNODateTime() const
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:95
virtual std::unique_ptr< SwField > Copy() const override
Definition: flddat.cxx:82
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:160
Date GetDate() const
Definition: flddat.cxx:140
tools::Long m_nOffset
Definition: flddat.hxx:48
tools::Time GetTime() const
Definition: flddat.cxx:152
static double GetDateTime(SwDoc &rDoc, const DateTime &rDT)
Definition: flddat.cxx:122
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nMId) override
Definition: flddat.cxx:188
virtual void SetSubType(sal_uInt16 nSub) override
Definition: flddat.cxx:100
virtual double GetValue() const override
Definition: flddat.cxx:132
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:117
virtual void SetPar2(const OUString &rStr) override
Definition: flddat.cxx:105
virtual OUString GetPar2() const override
Definition: flddat.cxx:110
Definition: doc.hxx:192
SvNumberFormatter * GetNumberFormatter(bool bCreate=true)
Definition: doc.hxx:1414
void ChangeFormat(sal_uInt32 n)
Definition: fldbas.cxx:447
bool IsAutomaticLanguage() const
Definition: fldbas.hxx:383
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:403
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const
Definition: fldbas.cxx:351
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId)
Definition: fldbas.cxx:369
SwFieldType * GetTyp() const
Definition: fldbas.hxx:398
LanguageType GetLanguage() const
Language at field position.
Definition: fldbas.hxx:408
bool IsFixed() const
Definition: fldbas.cxx:452
The root element of a Writer document layout.
Definition: rootfrm.hxx:82
Fields containing values that have to be formatted via number formatter.
Definition: fldbas.hxx:415
SwDoc * GetDoc() const
Definition: fldbas.hxx:425
OUString ExpandValue(const double &rVal, sal_uInt32 nFormat, LanguageType nLng) const
Definition: fldbas.hxx:466
virtual double GetValue() const
Definition: fldbas.cxx:745
SwDoc * GetDoc() const
Definition: fldbas.hxx:459
virtual void SetValue(const double &rVal)
Definition: fldbas.cxx:750
@ FIXEDFLD
Definition: fldbas.hxx:229
@ DATEFLD
Definition: fldbas.hxx:230
@ TIMEFLD
Definition: fldbas.hxx:231
SwFieldIds
Definition: fldbas.hxx:45
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