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