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 
10 #include <DateFormFieldDialog.hxx>
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 
18 namespace 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));
37  InitControls();
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
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();
57  (*pParameters)[ODF_FORMDATE_DATEFORMAT_LANGUAGE]
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 
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: */
std::map< OUString, css::uno::Any > parameter_map_t
Definition: IMark.hxx:94
SvNumberFormatter * m_pNumberFormatter
virtual std::pair< bool, double > GetCurrentDate() const =0
LanguageType getLanguageType(bool bResolveSystem=true) const
#define ODF_FORMDATE_CURRENTDATE
Definition: doc.hxx:188
#define ODF_FORMDATE_DATEFORMAT_LANGUAGE
Dialog to specify the properties of date form field.
const OUString & GetFormatstring() 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)
constexpr sal_uInt32 NUMBERFORMAT_ENTRY_NOT_FOUND
virtual parameter_map_t * GetParameters()=0
virtual void SetCurrentDate(double fDate)=0
sw::mark::IDateFieldmark * m_pDateField
#define LANGUAGE_DONTKNOW
SvNumFormatType
sal_uInt32 GetEntryKey(std::u16string_view sStr, LanguageType eLnge=LANGUAGE_DONTKNOW)
#define ODF_FORMDATE_DATEFORMAT
std::unique_ptr< SwNumFormatTreeView > m_xFormatLB
virtual Size get_preferred_size() const =0
virtual ~DateFormFieldDialog() override
const SvNumberformat * GetEntry(sal_uInt32 nKey) const
virtual int get_height_rows(int nRows) const =0
QPRO_FUNC_TYPE nType
DateFormFieldDialog(weld::Widget *pParent, sw::mark::IDateFieldmark *pDateField, SwDoc &rDoc)
virtual void set_size_request(int nWidth, int nHeight)=0
LanguageType GetLanguage() const