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