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 <vcl/event.hxx>
12 #include <IMark.hxx>
13 #include <xmloff/odffields.hxx>
14 #include <svl/zforlist.hxx>
15 #include <svl/zformat.hxx>
16 #include <svx/numfmtsh.hxx>
17 #include <doc.hxx>
18 
19 namespace sw
20 {
22  sw::mark::IDateFieldmark* pDateField, SwDoc* pDoc)
23  : GenericDialogController(pParent, "modules/swriter/ui/dateformfielddialog.ui",
24  "DateFormFieldDialog")
25  , m_pDateField(pDateField)
26  , m_pNumberFormatter(pDoc->GetNumberFormatter())
27  , m_xFormatLB(new SwNumFormatTreeView(m_xBuilder->weld_tree_view("date_formats_treeview")))
28 {
29  m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
30  m_xFormatLB->SetAutomaticLanguage(true);
31  m_xFormatLB->SetShowLanguageControl(true);
32  m_xFormatLB->SetOneArea(true);
33 
34  // Set a default height
35  weld::TreeView& rTreeView = dynamic_cast<weld::TreeView&>(m_xFormatLB->get_widget());
36  rTreeView.set_size_request(rTreeView.get_preferred_size().Width(),
37  rTreeView.get_height_rows(10));
38  InitControls();
39 }
40 
42 
44 {
45  if (m_pDateField != nullptr)
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 }
71 
73 {
74  if (m_pDateField != nullptr)
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  {
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  {
106  if (m_xFormatLB->GetCurLanguage() == aLangType)
107  {
108  m_xFormatLB->SetAutomaticLanguage(true);
109  }
110  else
111  {
112  m_xFormatLB->SetAutomaticLanguage(false);
113  m_xFormatLB->SetLanguage(aLangType);
114 
115  // Change format and change back for regenerating the list
116  m_xFormatLB->SetFormatType(SvNumFormatType::ALL);
117  m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
118  }
119  m_xFormatLB->SetDefFormat(nFormat);
120  }
121  }
122  }
123 }
124 
125 } // namespace sw
126 
127 /* 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:185
#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
QPRO_FUNC_TYPE const nType
#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
sal_uInt32 GetEntryKey(const OUString &sStr, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual void set_size_request(int nWidth, int nHeight)=0
LanguageType GetLanguage() const