LibreOffice Module reportdesign (master)  1
DateTime.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 #include <DateTime.hxx>
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <rptui_slotid.hrc>
23 #include <unotools/syslocale.hxx>
25 #include <vcl/svapp.hxx>
26 #include <strings.hxx>
27 #include <ReportController.hxx>
28 #include <com/sun/star/util/NumberFormat.hpp>
29 #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
30 #include <algorithm>
31 
32 namespace rptui
33 {
34 using namespace ::com::sun::star;
35 using namespace ::comphelper;
36 
37 
38 
39 ODateTimeDialog::ODateTimeDialog(weld::Window* _pParent, const uno::Reference< report::XSection >& _xHoldAlive,
40  OReportController* _pController)
41  : GenericDialogController(_pParent, "modules/dbreport/ui/datetimedialog.ui", "DateTimeDialog")
42 
43  , m_pController(_pController)
44  , m_xHoldAlive(_xHoldAlive)
45  , m_xDate(m_xBuilder->weld_check_button("date"))
46  , m_xFTDateFormat(m_xBuilder->weld_label("datelistbox_label"))
47  , m_xDateListBox(m_xBuilder->weld_combo_box("datelistbox"))
48  , m_xTime(m_xBuilder->weld_check_button("time"))
49  , m_xFTTimeFormat(m_xBuilder->weld_label("timelistbox_label"))
50  , m_xTimeListBox(m_xBuilder->weld_combo_box("timelistbox"))
51  , m_xPB_OK(m_xBuilder->weld_button("ok"))
52 {
53  try
54  {
55  SvtSysLocale aSysLocale;
56  m_nLocale = aSysLocale.GetLanguageTag().getLocale();
57  // Fill listbox with all well known date types
58  InsertEntry(util::NumberFormat::DATE);
59  InsertEntry(util::NumberFormat::TIME);
60  }
61  catch (const uno::Exception&)
62  {
63  }
64 
65  m_xDateListBox->set_active(0);
66  m_xTimeListBox->set_active(0);
67 
68  weld::CheckButton* aCheckBoxes[] = { m_xDate.get(), m_xTime.get() };
69  for (weld::CheckButton* pCheckBox : aCheckBoxes)
70  pCheckBox->connect_toggled(LINK(this,ODateTimeDialog,CBClickHdl));
71  CBClickHdl(*m_xTime);
72 }
73 
74 void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId)
75 {
76  const bool bTime = util::NumberFormat::TIME == _nNumberFormatId;
77  weld::ComboBox* pListBox = m_xDateListBox.get();
78  if (bTime)
79  pListBox = m_xTimeListBox.get();
80 
81  const uno::Reference< util::XNumberFormatter> xNumberFormatter = m_pController->getReportNumberFormatter();
82  const uno::Reference< util::XNumberFormats> xFormats = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats();
83  const uno::Sequence<sal_Int32> aFormatKeys = xFormats->queryKeys(_nNumberFormatId,m_nLocale,true);
84  for (const sal_Int32 nFormatKey : aFormatKeys)
85  {
86  pListBox->append(OUString::number(nFormatKey), getFormatStringByKey(nFormatKey,xFormats,bTime));
87  }
88 }
89 
91 {
92  short nRet = GenericDialogController::run();
93  if (nRet == RET_OK && (m_xDate->get_active() || m_xTime->get_active()))
94  {
95  try
96  {
97  sal_Int32 nLength = 0;
98  uno::Sequence<beans::PropertyValue> aValues( 6 );
99  aValues[nLength].Name = PROPERTY_SECTION;
100  aValues[nLength++].Value <<= m_xHoldAlive;
101 
102  aValues[nLength].Name = PROPERTY_TIME_STATE;
103  aValues[nLength++].Value <<= m_xTime->get_active();
104 
105  aValues[nLength].Name = PROPERTY_DATE_STATE;
106  aValues[nLength++].Value <<= m_xDate->get_active();
107 
108  aValues[nLength].Name = PROPERTY_FORMATKEYDATE;
109  aValues[nLength++].Value <<= getFormatKey(true);
110 
111  aValues[nLength].Name = PROPERTY_FORMATKEYTIME;
112  aValues[nLength++].Value <<= getFormatKey(false);
113 
115  sal_Int32 nWidth = 0;
116  if ( m_xDate->get_active() )
117  {
118  OUString sDateFormat = m_xDateListBox->get_active_text();
119  nWidth = OutputDevice::LogicToLogic(pDefDev->PixelToLogic(Size(pDefDev->GetCtrlTextWidth(sDateFormat),0)).Width(),
120  pDefDev->GetMapMode().GetMapUnit(),MapUnit::Map100thMM);
121  }
122  if ( m_xTime->get_active() )
123  {
124  OUString sDateFormat = m_xTimeListBox->get_active_text();
125  nWidth = ::std::max<sal_Int32>(OutputDevice::LogicToLogic(pDefDev->PixelToLogic(Size(pDefDev->GetCtrlTextWidth(sDateFormat),0)).Width(),
126  pDefDev->GetMapMode().GetMapUnit(),MapUnit::Map100thMM),nWidth);
127  }
128 
129  if ( nWidth > 4000 )
130  {
131  aValues[nLength].Name = PROPERTY_WIDTH;
132  aValues[nLength++].Value <<= nWidth;
133  }
134 
135  m_pController->executeChecked(SID_DATETIME,aValues);
136  }
137  catch (const uno::Exception&)
138  {
139  nRet = RET_NO;
140  }
141  }
142  return nRet;
143 }
144 
145 OUString ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey,const uno::Reference< util::XNumberFormats>& _xFormats,bool _bTime)
146 {
147  uno::Reference< beans::XPropertySet> xFormSet = _xFormats->getByKey(_nNumberFormatKey);
148  OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
149  OUString sFormat;
150  xFormSet->getPropertyValue("FormatString") >>= sFormat;
151 
152  double nValue = 0;
153  if ( _bTime )
154  {
155  tools::Time aCurrentTime( tools::Time::SYSTEM );
156  nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
157  }
158  else
159  {
160  Date aCurrentDate( Date::SYSTEM );
161  static css::util::Date STANDARD_DB_DATE(30,12,1899);
162  nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(aCurrentDate.GetDate()),STANDARD_DB_DATE);
163  }
164 
165  uno::Reference< util::XNumberFormatPreviewer> xPreviewer(m_pController->getReportNumberFormatter(),uno::UNO_QUERY);
166  OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
167  return xPreviewer->convertNumberToPreviewString(sFormat,nValue,m_nLocale,true);
168 }
169 
171 {
172  const bool bDate = m_xDate->get_active();
173  m_xFTDateFormat->set_sensitive(bDate);
174  m_xDateListBox->set_sensitive(bDate);
175 
176  const bool bTime = m_xTime->get_active();
177  m_xFTTimeFormat->set_sensitive(bTime);
178  m_xTimeListBox->set_sensitive(bTime);
179 
180  if (!bDate && !bTime)
181  {
182  m_xPB_OK->set_sensitive(false);
183  }
184  else
185  {
186  m_xPB_OK->set_sensitive(true);
187  }
188 }
189 
190 sal_Int32 ODateTimeDialog::getFormatKey(bool _bDate) const
191 {
192  sal_Int32 nFormatKey;
193  if ( _bDate )
194  {
195  nFormatKey = m_xDateListBox->get_active_id().toInt32();
196  }
197  else
198  {
199  nFormatKey = m_xTimeListBox->get_active_id().toInt32();
200  }
201  return nFormatKey;
202 }
203 
204 } // rptui
205 
206 
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define PROPERTY_TIME_STATE
Definition: strings.hxx:115
sal_Int32 getFormatKey(bool _bDate) const
returns the number format key
Definition: DateTime.cxx:190
virtual short run() override
Definition: DateTime.cxx:90
Point LogicToLogic(const Point &rPtSource, const MapMode *pMapModeSource, const MapMode *pMapModeDest) const
const MapMode & GetMapMode() const
std::unique_ptr< weld::ComboBox > m_xTimeListBox
Definition: DateTime.hxx:47
void append(const OUString &rId, const OUString &rStr)
#define PROPERTY_WIDTH
Definition: strings.hxx:73
virtual void connect_toggled(const Link< ToggleButton &, void > &rLink)
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
OOO_DLLPUBLIC_DBTOOLS css::util::Date toDate(double dVal, const css::util::Date &_rNullDate=getStandardDate())
static OutputDevice * GetDefaultDevice()
RET_NO
tools::Long GetCtrlTextWidth(const OUString &rStr, const SalLayoutGlyphs *pLayoutCache=nullptr) const
sal_Int32 GetDate() const
css::lang::Locale m_nLocale
Definition: DateTime.hxx:40
IMPL_LINK_NOARG(OAddFieldWindow, FocusChangeHdl, weld::Container &, void)
Definition: AddField.cxx:120
#define PROPERTY_FORMATKEYDATE
Definition: strings.hxx:265
MapUnit GetMapUnit() const
#define PROPERTY_SECTION
Definition: strings.hxx:188
std::unique_ptr< weld::CheckButton > m_xDate
Definition: DateTime.hxx:42
Point PixelToLogic(const Point &rDevicePt) const
::rptui::OReportController * m_pController
Definition: DateTime.hxx:37
css::uno::Reference< css::report::XSection > m_xHoldAlive
Definition: DateTime.hxx:39
void InsertEntry(sal_Int16 _nNumberFormatId)
Definition: DateTime.cxx:74
#define PROPERTY_FORMATKEYTIME
Definition: strings.hxx:266
RET_OK
OOO_DLLPUBLIC_DBTOOLS css::util::Time toTime(double dVal, short nDigits=9)
const css::uno::Reference< css::util::XNumberFormatter > & getReportNumberFormatter() const
returns the number formatter
sal_Int64 GetTime() const
#define PROPERTY_DATE_STATE
Definition: strings.hxx:116
std::unique_ptr< weld::ComboBox > m_xDateListBox
Definition: DateTime.hxx:44
const LanguageTag & GetLanguageTag() const
sal_Int32 nLength
std::unique_ptr< weld::CheckButton > m_xTime
Definition: DateTime.hxx:45
ODateTimeDialog(const ODateTimeDialog &)=delete
sal_Int16 nValue
OUString getFormatStringByKey(::sal_Int32 _nNumberFormatKey, const css::uno::Reference< css::util::XNumberFormats > &_xFormats, bool _bTime)
returns the format string
Definition: DateTime.cxx:145