LibreOffice Module sw (master) 1
DateFormFieldDialog.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
11#include <IMark.hxx>
12#include <xmloff/odffields.hxx>
13#include <svl/numformat.hxx>
14#include <svl/zforlist.hxx>
15#include <svl/zformat.hxx>
16#include <doc.hxx>
17
18namespace sw
19{
21 sw::mark::IDateFieldmark* pDateField, SwDoc& rDoc)
22 : GenericDialogController(pParent, "modules/swriter/ui/dateformfielddialog.ui",
23 "DateFormFieldDialog")
24 , m_pDateField(pDateField)
25 , m_pNumberFormatter(rDoc.GetNumberFormatter())
26 , m_xFormatLB(new SwNumFormatTreeView(m_xBuilder->weld_tree_view("date_formats_treeview")))
27{
28 m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
29 m_xFormatLB->SetAutomaticLanguage(true);
30 m_xFormatLB->SetShowLanguageControl(true);
31 m_xFormatLB->SetOneArea(true);
32
33 // Set a default height
34 weld::TreeView& rTreeView = dynamic_cast<weld::TreeView&>(m_xFormatLB->get_widget());
35 rTreeView.set_size_request(rTreeView.get_preferred_size().Width(),
36 rTreeView.get_height_rows(10));
38}
39
41
43{
44 if (m_pDateField == nullptr)
45 return;
46
47 // Try to find out the current date value and replace the content
48 // with the right formatted date string
49 sw::mark::IFieldmark::parameter_map_t* pParameters = m_pDateField->GetParameters();
50 const SvNumberformat* pFormat = m_pNumberFormatter->GetEntry(m_xFormatLB->GetFormat());
51
52 // Get date value first
53 std::pair<bool, double> aResult = m_pDateField->GetCurrentDate();
54
55 // Then set the date format
56 (*pParameters)[ODF_FORMDATE_DATEFORMAT] <<= pFormat->GetFormatstring();
58 <<= LanguageTag(pFormat->GetLanguage()).getBcp47();
59
60 // Update current date
61 if (aResult.first)
62 {
63 m_pDateField->SetCurrentDate(aResult.second);
64 }
65 else
66 {
67 (*pParameters)[ODF_FORMDATE_CURRENTDATE] <<= OUString();
68 }
69}
70
72{
73 if (m_pDateField == nullptr)
74 return;
75
76 sw::mark::IFieldmark::parameter_map_t* pParameters = m_pDateField->GetParameters();
77
78 OUString sFormatString;
79 auto pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT);
80 if (pResult != pParameters->end())
81 {
82 pResult->second >>= sFormatString;
83 }
84
85 OUString sLang;
86 pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT_LANGUAGE);
87 if (pResult != pParameters->end())
88 {
89 pResult->second >>= sLang;
90 }
91
92 if (sFormatString.isEmpty() || sLang.isEmpty())
93 return;
94
95 LanguageType aLangType = LanguageTag(sLang).getLanguageType();
96 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(sFormatString, aLangType);
97 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
98 {
99 sal_Int32 nCheckPos = 0;
101 m_pNumberFormatter->PutEntry(sFormatString, nCheckPos, nType, nFormat,
102 LanguageTag(sLang).getLanguageType());
103 }
104
105 if (aLangType == LANGUAGE_DONTKNOW || nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
106 return;
107
108 if (m_xFormatLB->GetCurLanguage() == aLangType)
109 {
110 m_xFormatLB->SetAutomaticLanguage(true);
111 }
112 else
113 {
114 m_xFormatLB->SetAutomaticLanguage(false);
115 m_xFormatLB->SetLanguage(aLangType);
116
117 // Change format and change back for regenerating the list
118 m_xFormatLB->SetFormatType(SvNumFormatType::ALL);
119 m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
120 }
121 m_xFormatLB->SetDefFormat(nFormat);
122}
123
124} // namespace sw
125
126/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
LanguageType getLanguageType(bool bResolveSystem=true) const
const OUString & getBcp47(bool bResolveSystem=true) const
constexpr tools::Long Width() const
bool PutEntry(OUString &rString, sal_Int32 &nCheckPos, SvNumFormatType &nType, sal_uInt32 &nKey, LanguageType eLnge=LANGUAGE_DONTKNOW, bool bReplaceBooleanEquivalent=true)
sal_uInt32 GetEntryKey(std::u16string_view sStr, LanguageType eLnge=LANGUAGE_DONTKNOW)
const SvNumberformat * GetEntry(sal_uInt32 nKey) const
LanguageType GetLanguage() const
const OUString & GetFormatstring() const
Definition: doc.hxx:197
std::unique_ptr< SwNumFormatTreeView > m_xFormatLB
SvNumberFormatter * m_pNumberFormatter
sw::mark::IDateFieldmark * m_pDateField
DateFormFieldDialog(weld::Widget *pParent, sw::mark::IDateFieldmark *pDateField, SwDoc &rDoc)
virtual ~DateFormFieldDialog() override
virtual std::pair< bool, double > GetCurrentDate() const =0
virtual void SetCurrentDate(double fDate)=0
virtual int get_height_rows(int nRows) const=0
virtual void set_size_request(int nWidth, int nHeight)=0
virtual Size get_preferred_size() const=0
#define LANGUAGE_DONTKNOW
Dialog to specify the properties of date form field.
constexpr OUStringLiteral ODF_FORMDATE_CURRENTDATE
constexpr OUStringLiteral ODF_FORMDATE_DATEFORMAT
constexpr OUStringLiteral ODF_FORMDATE_DATEFORMAT_LANGUAGE
QPRO_FUNC_TYPE nType
SvNumFormatType
constexpr sal_uInt32 NUMBERFORMAT_ENTRY_NOT_FOUND