Line data Source code
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 <tools/debug.hxx>
22 : #include "RptResId.hrc"
23 : #include "rptui_slotid.hrc"
24 : #include "ModuleHelper.hxx"
25 : #include "helpids.hrc"
26 : #include <vcl/msgbox.hxx>
27 : #include <connectivity/dbconversion.hxx>
28 : #include <unotools/syslocale.hxx>
29 : #include "UITools.hxx"
30 : #include "RptDef.hxx"
31 : #include "uistrings.hrc"
32 : #include "ReportController.hxx"
33 : #include <com/sun/star/report/XFormattedField.hpp>
34 : #include <com/sun/star/util/Time.hpp>
35 : #include <com/sun/star/util/NumberFormat.hpp>
36 : #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
37 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
38 : #include <com/sun/star/i18n/NumberFormatIndex.hpp>
39 : #include <comphelper/numbers.hxx>
40 : #include <algorithm>
41 :
42 : namespace rptui
43 : {
44 : using namespace ::com::sun::star;
45 : using namespace ::comphelper;
46 :
47 :
48 : // class ODateTimeDialog
49 :
50 0 : ODateTimeDialog::ODateTimeDialog( vcl::Window* _pParent
51 : ,const uno::Reference< report::XSection >& _xHoldAlive
52 : ,OReportController* _pController)
53 : : ModalDialog( _pParent, "DateTimeDialog" , "modules/dbreport/ui/datetimedialog.ui" )
54 :
55 : , m_aDateControlling()
56 : , m_aTimeControlling()
57 : , m_pController(_pController)
58 0 : , m_xHoldAlive(_xHoldAlive)
59 : {
60 0 : get(m_pDate,"date");
61 0 : get(m_pFTDateFormat,"datelistbox_label");
62 0 : get(m_pDateListBox,"datelistbox");
63 0 : get(m_pTime,"time");
64 0 : get(m_pFTTimeFormat,"timelistbox_label");
65 0 : get(m_pTimeListBox,"timelistbox");
66 0 : get(m_pPB_OK,"ok");
67 :
68 :
69 : try
70 : {
71 0 : SvtSysLocale aSysLocale;
72 0 : m_nLocale = aSysLocale.GetLanguageTag().getLocale();
73 : // Fill listbox with all well known date types
74 0 : InsertEntry(util::NumberFormat::DATE);
75 0 : InsertEntry(util::NumberFormat::TIME);
76 : }
77 0 : catch (const uno::Exception&)
78 : {
79 : }
80 :
81 0 : m_pDateListBox->SelectEntryPos(0);
82 :
83 0 : m_pTimeListBox->SelectEntryPos(0);
84 :
85 : // use nice enhancement, to toggle enable/disable if a checkbox is checked or not
86 0 : m_aDateControlling.enableOnCheckMark( *m_pDate, *m_pFTDateFormat, *m_pDateListBox);
87 0 : m_aTimeControlling.enableOnCheckMark( *m_pTime, *m_pFTTimeFormat, *m_pTimeListBox);
88 :
89 0 : CheckBox* aCheckBoxes[] = { m_pDate,m_pTime};
90 0 : for ( size_t i = 0 ; i < SAL_N_ELEMENTS(aCheckBoxes); ++i)
91 0 : aCheckBoxes[i]->SetClickHdl(LINK(this,ODateTimeDialog,CBClickHdl));
92 :
93 0 : }
94 :
95 0 : void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId)
96 : {
97 0 : const bool bTime = util::NumberFormat::TIME == _nNumberFormatId;
98 0 : ListBox* pListBox = m_pDateListBox;
99 0 : if ( bTime )
100 0 : pListBox = m_pTimeListBox;
101 :
102 0 : const uno::Reference< util::XNumberFormatter> xNumberFormatter = m_pController->getReportNumberFormatter();
103 0 : const uno::Reference< util::XNumberFormats> xFormats = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats();
104 0 : const uno::Sequence<sal_Int32> aFormatKeys = xFormats->queryKeys(_nNumberFormatId,m_nLocale,sal_True);
105 0 : const sal_Int32* pIter = aFormatKeys.getConstArray();
106 0 : const sal_Int32* pEnd = pIter + aFormatKeys.getLength();
107 0 : for(;pIter != pEnd;++pIter)
108 : {
109 0 : const sal_Int16 nPos = pListBox->InsertEntry(getFormatStringByKey(*pIter,xFormats,bTime));
110 0 : pListBox->SetEntryData(nPos, reinterpret_cast<void*>(*pIter));
111 0 : }
112 0 : }
113 :
114 0 : ODateTimeDialog::~ODateTimeDialog()
115 : {
116 0 : }
117 :
118 0 : short ODateTimeDialog::Execute()
119 : {
120 0 : short nRet = ModalDialog::Execute();
121 0 : if ( nRet == RET_OK && (m_pDate->IsChecked() || m_pTime->IsChecked()) )
122 : {
123 : try
124 : {
125 0 : sal_Int32 nLength = 0;
126 0 : uno::Sequence<beans::PropertyValue> aValues( 6 );
127 0 : aValues[nLength].Name = PROPERTY_SECTION;
128 0 : aValues[nLength++].Value <<= m_xHoldAlive;
129 :
130 0 : aValues[nLength].Name = PROPERTY_TIME_STATE;
131 0 : aValues[nLength++].Value <<= m_pTime->IsChecked();
132 :
133 0 : aValues[nLength].Name = PROPERTY_DATE_STATE;
134 0 : aValues[nLength++].Value <<= m_pDate->IsChecked();
135 :
136 0 : aValues[nLength].Name = PROPERTY_FORMATKEYDATE;
137 0 : aValues[nLength++].Value <<= getFormatKey(true);
138 :
139 0 : aValues[nLength].Name = PROPERTY_FORMATKEYTIME;
140 0 : aValues[nLength++].Value <<= getFormatKey(false);
141 :
142 0 : sal_Int32 nWidth = 0;
143 0 : if ( m_pDate->IsChecked() )
144 : {
145 0 : OUString sDateFormat = m_pDateListBox->GetEntry( m_pDateListBox->GetSelectEntryPos() );
146 0 : nWidth = LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM);
147 : }
148 0 : if ( m_pTime->IsChecked() )
149 : {
150 0 : OUString sDateFormat = m_pTimeListBox->GetEntry( m_pTimeListBox->GetSelectEntryPos() );
151 0 : nWidth = ::std::max<sal_Int32>(LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM),nWidth);
152 : }
153 :
154 0 : if ( nWidth > 4000 )
155 : {
156 0 : aValues[nLength].Name = PROPERTY_WIDTH;
157 0 : aValues[nLength++].Value <<= nWidth;
158 : }
159 :
160 0 : m_pController->executeChecked(SID_DATETIME,aValues);
161 : }
162 0 : catch (const uno::Exception&)
163 : {
164 0 : nRet = RET_NO;
165 : }
166 : }
167 0 : return nRet;
168 : }
169 :
170 0 : OUString ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey,const uno::Reference< util::XNumberFormats>& _xFormats,bool _bTime)
171 : {
172 0 : uno::Reference< beans::XPropertySet> xFormSet = _xFormats->getByKey(_nNumberFormatKey);
173 : OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
174 0 : OUString sFormat;
175 0 : xFormSet->getPropertyValue("FormatString") >>= sFormat;
176 :
177 0 : double nValue = 0;
178 0 : if ( _bTime )
179 : {
180 0 : tools::Time aCurrentTime( tools::Time::SYSTEM );
181 0 : nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
182 : }
183 : else
184 : {
185 0 : Date aCurrentDate( Date::SYSTEM );
186 0 : static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
187 0 : nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
188 : }
189 :
190 0 : uno::Reference< util::XNumberFormatPreviewer> xPreviewer(m_pController->getReportNumberFormatter(),uno::UNO_QUERY);
191 : OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
192 0 : return xPreviewer->convertNumberToPreviewString(sFormat,nValue,m_nLocale,sal_True);
193 : }
194 :
195 0 : IMPL_LINK( ODateTimeDialog, CBClickHdl, CheckBox*, _pBox )
196 : {
197 : (void)_pBox;
198 :
199 0 : if ( _pBox == m_pDate || _pBox == m_pTime)
200 : {
201 0 : bool bDate = m_pDate->IsChecked();
202 0 : bool bTime = m_pTime->IsChecked();
203 0 : if (!bDate && !bTime)
204 : {
205 0 : m_pPB_OK->Disable();
206 : }
207 : else
208 : {
209 0 : m_pPB_OK->Enable();
210 : }
211 : }
212 0 : return 1L;
213 : }
214 :
215 0 : sal_Int32 ODateTimeDialog::getFormatKey(bool _bDate) const
216 : {
217 : sal_Int32 nFormatKey;
218 0 : if ( _bDate )
219 : {
220 0 : nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pDateListBox->GetEntryData( m_pDateListBox->GetSelectEntryPos() )));
221 : }
222 : else
223 : {
224 0 : nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pTimeListBox->GetEntryData( m_pTimeListBox->GetSelectEntryPos() )));
225 : }
226 0 : return nFormatKey;
227 : }
228 :
229 6 : } // rptui
230 :
231 :
232 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|