tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / extensions / source / propctrlr / usercontrol.cxx
blob1b9a4c4095ad3dd5276de3bd6698043cd74c97ae
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 .
20 #include "usercontrol.hxx"
22 #include <com/sun/star/inspection/PropertyControlType.hpp>
23 #include <svl/numuno.hxx>
24 #include <vcl/GraphicObject.hxx>
25 #include <vcl/event.hxx>
26 #include <tools/debug.hxx>
27 #include <svl/numformat.hxx>
28 #include <svl/zformat.hxx>
29 #include <connectivity/dbconversion.hxx>
30 #include "modulepcr.hxx"
31 #include <strings.hrc>
34 namespace pcr
38 using ::com::sun::star::uno::Any;
39 using ::com::sun::star::uno::Type;
41 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
43 IMPL_LINK(OFormatSampleControl, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
45 // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
46 sal_uInt16 nKey = rKeyEvent.GetKeyCode().GetCode();
47 if ((KEY_DELETE == nKey) || (KEY_BACKSPACE == nKey))
49 m_xSpinButton->set_text(u""_ustr);
50 m_xEntry->set_text(u""_ustr);
51 setModified();
54 return true;
57 void OFormatSampleControl::SetFormatSupplier( const SvNumberFormatsSupplierObj* pSupplier )
59 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
60 if (pSupplier)
62 rFieldFormatter.TreatAsNumber(true);
64 SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
65 rFieldFormatter.SetFormatter(pFormatter);
66 rFieldFormatter.SetValue( 1234.56789 );
68 else
70 rFieldFormatter.TreatAsNumber(false);
71 rFieldFormatter.SetFormatter(nullptr);
72 m_xSpinButton->set_text( u""_ustr );
75 m_xEntry->set_text(m_xSpinButton->get_text());
78 OFormatSampleControl::OFormatSampleControl(std::unique_ptr<weld::Container> xWidget, std::unique_ptr<weld::Builder> xBuilder, bool bReadOnly)
79 : OFormatSampleControl_Base(PropertyControlType::Unknown, std::move(xBuilder), std::move(xWidget), bReadOnly)
80 , m_xSpinButton(m_xBuilder->weld_formatted_spin_button(u"sample"_ustr))
81 , m_xEntry(m_xBuilder->weld_entry(u"entry"_ustr))
83 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
84 rFieldFormatter.TreatAsNumber(true);
85 rFieldFormatter.ClearMinValue();
86 rFieldFormatter.ClearMaxValue();
87 m_xEntry->connect_key_press(LINK(this, OFormatSampleControl, KeyInputHdl));
90 void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue )
92 sal_Int32 nFormatKey = 0;
93 if ( _rValue >>= nFormatKey )
95 // else set the new format key, the text will be reformatted
96 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
97 rFieldFormatter.SetFormatKey(nFormatKey);
99 SvNumberFormatter* pNF = rFieldFormatter.GetFormatter();
100 const SvNumberformat* pEntry = pNF->GetEntry( nFormatKey );
101 OSL_ENSURE( pEntry, "OFormatSampleControl::setValue: invalid format entry!" );
103 const bool bIsTextFormat = ( pEntry && pEntry->IsTextFormat() );
104 if ( bIsTextFormat )
105 m_xSpinButton->set_text( PcrRes( RID_STR_TEXT_FORMAT ) );
106 else
107 rFieldFormatter.SetValue( pEntry ? getPreviewValue( *pEntry ) : 1234.56789 );
109 else
110 m_xSpinButton->set_text( u""_ustr );
112 m_xEntry->set_text(m_xSpinButton->get_text());
115 double OFormatSampleControl::getPreviewValue( const SvNumberformat& i_rEntry )
117 double nValue = 1234.56789;
118 switch ( i_rEntry.GetType() & ~SvNumFormatType::DEFINED )
120 case SvNumFormatType::DATE:
122 Date aCurrentDate( Date::SYSTEM );
123 static css::util::Date STANDARD_DB_DATE(30,12,1899);
124 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(aCurrentDate.GetDate()),STANDARD_DB_DATE);
126 break;
127 case SvNumFormatType::TIME:
128 case SvNumFormatType::DATETIME:
130 tools::Time aCurrentTime( tools::Time::SYSTEM );
131 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
133 break;
134 default:
135 break;
137 return nValue;
141 double OFormatSampleControl::getPreviewValue(SvNumberFormatter const * _pNF, sal_Int32 _nFormatKey)
143 const SvNumberformat* pEntry = _pNF->GetEntry(_nFormatKey);
144 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
145 double nValue = 1234.56789;
146 if ( pEntry )
147 nValue = getPreviewValue( *pEntry );
148 return nValue;
151 Any SAL_CALL OFormatSampleControl::getValue()
153 Any aPropValue;
154 if ( !m_xSpinButton->get_text().isEmpty() )
156 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
157 aPropValue <<= rFieldFormatter.GetValue();
159 return aPropValue;
162 Type SAL_CALL OFormatSampleControl::getValueType()
164 return ::cppu::UnoType<sal_Int32>::get();
167 OFormattedNumericControl::OFormattedNumericControl(std::unique_ptr<weld::FormattedSpinButton> xWidget, std::unique_ptr<weld::Builder> xBuilder, bool bReadOnly)
168 : OFormattedNumericControl_Base(PropertyControlType::Unknown, std::move(xBuilder), std::move(xWidget), bReadOnly)
170 Formatter& rFormatter = getTypedControlWindow()->GetFormatter();
171 rFormatter.TreatAsNumber(true);
172 rFormatter.ClearMinValue();
173 rFormatter.ClearMaxValue();
176 OFormattedNumericControl::~OFormattedNumericControl()
180 void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue )
182 double nValue( 0 );
183 if ( _rValue >>= nValue )
184 getTypedControlWindow()->GetFormatter().SetValue(nValue);
185 else
186 getTypedControlWindow()->set_text(u""_ustr);
189 Any SAL_CALL OFormattedNumericControl::getValue()
191 Any aPropValue;
192 if ( !getTypedControlWindow()->get_text().isEmpty() )
193 aPropValue <<= getTypedControlWindow()->GetFormatter().GetValue();
194 return aPropValue;
197 Type SAL_CALL OFormattedNumericControl::getValueType()
199 return ::cppu::UnoType<double>::get();
202 void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
204 bool bFallback = true;
206 Formatter& rFieldFormatter = getTypedControlWindow()->GetFormatter();
207 if (rDesc.pSupplier)
209 rFieldFormatter.TreatAsNumber(true);
211 SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
212 if (pFormatter != rFieldFormatter.GetFormatter())
213 rFieldFormatter.SetFormatter(pFormatter);
214 rFieldFormatter.SetFormatKey(rDesc.nKey);
216 const SvNumberformat* pEntry = rFieldFormatter.GetFormatter()->GetEntry(rFieldFormatter.GetFormatKey());
217 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
218 if ( pEntry )
220 bFallback = false;
225 if ( bFallback )
227 rFieldFormatter.TreatAsNumber(false);
228 rFieldFormatter.SetFormatter(nullptr);
229 getTypedControlWindow()->set_text(u""_ustr);
233 //= OFileUrlControl
234 OFileUrlControl::OFileUrlControl(std::unique_ptr<SvtURLBox> xWidget, std::unique_ptr<weld::Builder> xBuilder, bool bReadOnly)
235 : OFileUrlControl_Base(PropertyControlType::Unknown, std::move(xBuilder), std::move(xWidget), bReadOnly)
237 getTypedControlWindow()->DisableHistory();
238 getTypedControlWindow()->SetPlaceHolder( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ;
241 OFileUrlControl::~OFileUrlControl()
245 void SAL_CALL OFileUrlControl::setValue(const Any& rValue)
247 OUString sURL;
248 SvtURLBox* pControlWindow = getTypedControlWindow();
249 bool bSuccess = rValue >>= sURL;
250 if (bSuccess && GraphicObject::isGraphicObjectUniqueIdURL(sURL))
251 sURL = pControlWindow->GetPlaceHolder();
252 pControlWindow->set_entry_text(sURL);
255 Any SAL_CALL OFileUrlControl::getValue()
257 Any aPropValue;
258 if (!getTypedControlWindow()->get_active_text().isEmpty())
259 aPropValue <<= getTypedControlWindow()->GetURL();
260 return aPropValue;
263 Type SAL_CALL OFileUrlControl::getValueType()
265 return ::cppu::UnoType<OUString>::get();
268 IMPL_LINK_NOARG(OFileUrlControl, URLModifiedHdl, weld::ComboBox&, void)
270 editChanged();
273 } // namespace pcr
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */