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