update emoji autocorrect entries from po-files
[LibreOffice.git] / reportdesign / source / ui / dlg / DateTime.cxx
blobcaad1e3bf1aafa547c53ffcf339547a4c69202ce
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
42 namespace rptui
44 using namespace ::com::sun::star;
45 using namespace ::comphelper;
48 // class ODateTimeDialog
50 ODateTimeDialog::ODateTimeDialog( vcl::Window* _pParent
51 ,const uno::Reference< report::XSection >& _xHoldAlive
52 ,OReportController* _pController)
53 : ModalDialog( _pParent, "DateTimeDialog" , "modules/dbreport/ui/datetimedialog.ui" )
55 , m_aDateControlling()
56 , m_aTimeControlling()
57 , m_pController(_pController)
58 , m_xHoldAlive(_xHoldAlive)
60 get(m_pDate,"date");
61 get(m_pFTDateFormat,"datelistbox_label");
62 get(m_pDateListBox,"datelistbox");
63 get(m_pTime,"time");
64 get(m_pFTTimeFormat,"timelistbox_label");
65 get(m_pTimeListBox,"timelistbox");
66 get(m_pPB_OK,"ok");
69 try
71 SvtSysLocale aSysLocale;
72 m_nLocale = aSysLocale.GetLanguageTag().getLocale();
73 // Fill listbox with all well known date types
74 InsertEntry(util::NumberFormat::DATE);
75 InsertEntry(util::NumberFormat::TIME);
77 catch (const uno::Exception&)
81 m_pDateListBox->SelectEntryPos(0);
83 m_pTimeListBox->SelectEntryPos(0);
85 // use nice enhancement, to toggle enable/disable if a checkbox is checked or not
86 m_aDateControlling.enableOnCheckMark( *m_pDate, *m_pFTDateFormat, *m_pDateListBox);
87 m_aTimeControlling.enableOnCheckMark( *m_pTime, *m_pFTTimeFormat, *m_pTimeListBox);
89 CheckBox* aCheckBoxes[] = { m_pDate,m_pTime};
90 for ( size_t i = 0 ; i < SAL_N_ELEMENTS(aCheckBoxes); ++i)
91 aCheckBoxes[i]->SetClickHdl(LINK(this,ODateTimeDialog,CBClickHdl));
95 void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId)
97 const bool bTime = util::NumberFormat::TIME == _nNumberFormatId;
98 ListBox* pListBox = m_pDateListBox;
99 if ( bTime )
100 pListBox = m_pTimeListBox;
102 const uno::Reference< util::XNumberFormatter> xNumberFormatter = m_pController->getReportNumberFormatter();
103 const uno::Reference< util::XNumberFormats> xFormats = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats();
104 const uno::Sequence<sal_Int32> aFormatKeys = xFormats->queryKeys(_nNumberFormatId,m_nLocale,sal_True);
105 const sal_Int32* pIter = aFormatKeys.getConstArray();
106 const sal_Int32* pEnd = pIter + aFormatKeys.getLength();
107 for(;pIter != pEnd;++pIter)
109 const sal_Int16 nPos = pListBox->InsertEntry(getFormatStringByKey(*pIter,xFormats,bTime));
110 pListBox->SetEntryData(nPos, reinterpret_cast<void*>(*pIter));
114 ODateTimeDialog::~ODateTimeDialog()
116 disposeOnce();
119 void ODateTimeDialog::dispose()
121 m_pDate.clear();
122 m_pFTDateFormat.clear();
123 m_pDateListBox.clear();
124 m_pTime.clear();
125 m_pFTTimeFormat.clear();
126 m_pTimeListBox.clear();
127 m_pPB_OK.clear();
128 ModalDialog::dispose();
131 short ODateTimeDialog::Execute()
133 short nRet = ModalDialog::Execute();
134 if ( nRet == RET_OK && (m_pDate->IsChecked() || m_pTime->IsChecked()) )
138 sal_Int32 nLength = 0;
139 uno::Sequence<beans::PropertyValue> aValues( 6 );
140 aValues[nLength].Name = PROPERTY_SECTION;
141 aValues[nLength++].Value <<= m_xHoldAlive;
143 aValues[nLength].Name = PROPERTY_TIME_STATE;
144 aValues[nLength++].Value <<= m_pTime->IsChecked();
146 aValues[nLength].Name = PROPERTY_DATE_STATE;
147 aValues[nLength++].Value <<= m_pDate->IsChecked();
149 aValues[nLength].Name = PROPERTY_FORMATKEYDATE;
150 aValues[nLength++].Value <<= getFormatKey(true);
152 aValues[nLength].Name = PROPERTY_FORMATKEYTIME;
153 aValues[nLength++].Value <<= getFormatKey(false);
155 sal_Int32 nWidth = 0;
156 if ( m_pDate->IsChecked() )
158 OUString sDateFormat = m_pDateListBox->GetSelectEntry();
159 nWidth = LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM);
161 if ( m_pTime->IsChecked() )
163 OUString sDateFormat = m_pTimeListBox->GetSelectEntry();
164 nWidth = ::std::max<sal_Int32>(LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM),nWidth);
167 if ( nWidth > 4000 )
169 aValues[nLength].Name = PROPERTY_WIDTH;
170 aValues[nLength++].Value <<= nWidth;
173 m_pController->executeChecked(SID_DATETIME,aValues);
175 catch (const uno::Exception&)
177 nRet = RET_NO;
180 return nRet;
183 OUString ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey,const uno::Reference< util::XNumberFormats>& _xFormats,bool _bTime)
185 uno::Reference< beans::XPropertySet> xFormSet = _xFormats->getByKey(_nNumberFormatKey);
186 OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
187 OUString sFormat;
188 xFormSet->getPropertyValue("FormatString") >>= sFormat;
190 double nValue = 0;
191 if ( _bTime )
193 tools::Time aCurrentTime( tools::Time::SYSTEM );
194 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
196 else
198 Date aCurrentDate( Date::SYSTEM );
199 static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
200 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
203 uno::Reference< util::XNumberFormatPreviewer> xPreviewer(m_pController->getReportNumberFormatter(),uno::UNO_QUERY);
204 OSL_ENSURE(xPreviewer.is(),"XNumberFormatPreviewer is null!");
205 return xPreviewer->convertNumberToPreviewString(sFormat,nValue,m_nLocale,sal_True);
208 IMPL_LINK( ODateTimeDialog, CBClickHdl, CheckBox*, _pBox )
210 (void)_pBox;
212 if ( _pBox == m_pDate || _pBox == m_pTime)
214 bool bDate = m_pDate->IsChecked();
215 bool bTime = m_pTime->IsChecked();
216 if (!bDate && !bTime)
218 m_pPB_OK->Disable();
220 else
222 m_pPB_OK->Enable();
225 return 1L;
228 sal_Int32 ODateTimeDialog::getFormatKey(bool _bDate) const
230 sal_Int32 nFormatKey;
231 if ( _bDate )
233 nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pDateListBox->GetSelectEntryData()));
235 else
237 nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_pTimeListBox->GetSelectEntryData()));
239 return nFormatKey;
242 } // rptui
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */