LibreOffice Module extensions (master) 1
usercontrol.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
20#include "usercontrol.hxx"
21
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>
30#include "modulepcr.hxx"
31#include <strings.hrc>
32
33
34namespace pcr
35{
36
37
38 using ::com::sun::star::uno::Any;
39 using ::com::sun::star::uno::Type;
40
41 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
42
43 IMPL_LINK(OFormatSampleControl, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
44 {
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))
48 {
49 m_xSpinButton->set_text("");
50 m_xEntry->set_text("");
51 setModified();
52 }
53
54 return true;
55 }
56
58 {
59 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
60 if (pSupplier)
61 {
62 rFieldFormatter.TreatAsNumber(true);
63
64 SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
65 rFieldFormatter.SetFormatter(pFormatter);
66 rFieldFormatter.SetValue( 1234.56789 );
67 }
68 else
69 {
70 rFieldFormatter.TreatAsNumber(false);
71 rFieldFormatter.SetFormatter(nullptr);
72 m_xSpinButton->set_text( "" );
73 }
74
75 m_xEntry->set_text(m_xSpinButton->get_text());
76 }
77
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("sample"))
81 , m_xEntry(m_xBuilder->weld_entry("entry"))
82 {
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));
88 }
89
90 void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue )
91 {
92 sal_Int32 nFormatKey = 0;
93 if ( _rValue >>= nFormatKey )
94 {
95 // else set the new format key, the text will be reformatted
96 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
97 rFieldFormatter.SetFormatKey(nFormatKey);
98
99 SvNumberFormatter* pNF = rFieldFormatter.GetFormatter();
100 const SvNumberformat* pEntry = pNF->GetEntry( nFormatKey );
101 OSL_ENSURE( pEntry, "OFormatSampleControl::setValue: invalid format entry!" );
102
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 );
108 }
109 else
110 m_xSpinButton->set_text( "" );
111
112 m_xEntry->set_text(m_xSpinButton->get_text());
113 }
114
116 {
117 double nValue = 1234.56789;
118 switch ( i_rEntry.GetType() & ~SvNumFormatType::DEFINED )
119 {
120 case SvNumFormatType::DATE:
121 {
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);
125 }
126 break;
127 case SvNumFormatType::TIME:
128 case SvNumFormatType::DATETIME:
129 {
130 tools::Time aCurrentTime( tools::Time::SYSTEM );
131 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
132 }
133 break;
134 default:
135 break;
136 }
137 return nValue;
138 }
139
140
141 double OFormatSampleControl::getPreviewValue(SvNumberFormatter const * _pNF, sal_Int32 _nFormatKey)
142 {
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;
149 }
150
152 {
153 Any aPropValue;
154 if ( !m_xSpinButton->get_text().isEmpty() )
155 {
156 Formatter& rFieldFormatter = m_xSpinButton->GetFormatter();
157 aPropValue <<= rFieldFormatter.GetValue();
158 }
159 return aPropValue;
160 }
161
163 {
164 return ::cppu::UnoType<sal_Int32>::get();
165 }
166
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)
169 {
170 Formatter& rFormatter = getTypedControlWindow()->GetFormatter();
171 rFormatter.TreatAsNumber(true);
172 rFormatter.ClearMinValue();
173 rFormatter.ClearMaxValue();
174 }
175
177 {
178 }
179
180 void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue )
181 {
182 double nValue( 0 );
183 if ( _rValue >>= nValue )
184 getTypedControlWindow()->GetFormatter().SetValue(nValue);
185 else
186 getTypedControlWindow()->set_text("");
187 }
188
190 {
191 Any aPropValue;
192 if ( !getTypedControlWindow()->get_text().isEmpty() )
193 aPropValue <<= getTypedControlWindow()->GetFormatter().GetValue();
194 return aPropValue;
195 }
196
198 {
199 return ::cppu::UnoType<double>::get();
200 }
201
203 {
204 bool bFallback = true;
205
206 Formatter& rFieldFormatter = getTypedControlWindow()->GetFormatter();
207 if (rDesc.pSupplier)
208 {
209 rFieldFormatter.TreatAsNumber(true);
210
211 SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
212 if (pFormatter != rFieldFormatter.GetFormatter())
213 rFieldFormatter.SetFormatter(pFormatter);
214 rFieldFormatter.SetFormatKey(rDesc.nKey);
215
216 const SvNumberformat* pEntry = rFieldFormatter.GetFormatter()->GetEntry(rFieldFormatter.GetFormatKey());
217 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
218 if ( pEntry )
219 {
220 bFallback = false;
221 }
222
223 }
224
225 if ( bFallback )
226 {
227 rFieldFormatter.TreatAsNumber(false);
228 rFieldFormatter.SetFormatter(nullptr);
229 getTypedControlWindow()->set_text("");
230 }
231 }
232
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)
236 {
237 getTypedControlWindow()->DisableHistory();
238 getTypedControlWindow()->SetPlaceHolder( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ;
239 }
240
242 {
243 }
244
245 void SAL_CALL OFileUrlControl::setValue(const Any& rValue)
246 {
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);
253 }
254
256 {
257 Any aPropValue;
258 if (!getTypedControlWindow()->get_active_text().isEmpty())
259 aPropValue <<= getTypedControlWindow()->GetURL();
260 return aPropValue;
261 }
262
264 {
265 return ::cppu::UnoType<OUString>::get();
266 }
267
269 {
270 editChanged();
271 }
272
273} // namespace pcr
274
275
276/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 GetDate() const
void SetValue(double dVal)
void TreatAsNumber(bool bDoSo)
virtual void ClearMinValue()
void SetFormatter(SvNumberFormatter *pFormatter, bool bResetFormat=true)
void SetFormatKey(sal_uLong nFormatKey)
sal_uLong GetFormatKey() const
virtual void ClearMaxValue()
SvNumberFormatter * GetFormatter() const
double GetValue()
static bool isGraphicObjectUniqueIdURL(std::u16string_view rURL)
SvNumberFormatter * GetNumberFormatter() const
const SvNumberformat * GetEntry(sal_uInt32 nKey) const
bool IsTextFormat() const
SvNumFormatType GetType() const
void set_entry_text(const OUString &rStr)
const OUString & GetPlaceHolder() const
implements a base class for <type scope="css::inspection">XPropertyControl</type> implementations
TControlWindow * getTypedControlWindow()
OFileUrlControl(std::unique_ptr< SvtURLBox > xWidget, std::unique_ptr< weld::Builder > xBuilder, bool bReadOnly)
virtual css::uno::Any SAL_CALL getValue() override
virtual ~OFileUrlControl() override
virtual css::uno::Type SAL_CALL getValueType() override
virtual void SAL_CALL setValue(const css::uno::Any &_value) override
void SetFormatSupplier(const SvNumberFormatsSupplierObj *pSupplier)
Definition: usercontrol.cxx:57
std::unique_ptr< weld::FormattedSpinButton > m_xSpinButton
Definition: usercontrol.hxx:35
OFormatSampleControl(std::unique_ptr< weld::Container > xWidget, std::unique_ptr< weld::Builder > xBuilder, bool bReadOnly)
Definition: usercontrol.cxx:78
static double getPreviewValue(SvNumberFormatter const *pNF, sal_Int32 nFormatKey)
returns the default preview value for the given format key
std::unique_ptr< weld::Entry > m_xEntry
Definition: usercontrol.hxx:36
virtual css::uno::Type SAL_CALL getValueType() override
virtual void SAL_CALL setValue(const css::uno::Any &_value) override
Definition: usercontrol.cxx:90
virtual css::uno::Any SAL_CALL getValue() override
virtual css::uno::Type SAL_CALL getValueType() override
void SetFormatDescription(const FormatDescription &rDesc)
virtual void SAL_CALL setValue(const css::uno::Any &_value) override
OFormattedNumericControl(std::unique_ptr< weld::FormattedSpinButton > xWidget, std::unique_ptr< weld::Builder > xBuilder, bool bReadOnly)
virtual css::uno::Any SAL_CALL getValue() override
virtual ~OFormattedNumericControl() override
sal_Int64 GetTime() const
#define DBG_ASSERT(sCon, aError)
sal_Int16 nValue
bool bReadOnly
constexpr sal_uInt16 KEY_DELETE
constexpr sal_uInt16 KEY_BACKSPACE
Type
Unknown
a property handler for any virtual string properties
Definition: browserline.cxx:39
OUString PcrRes(TranslateId aId)
Definition: modulepcr.cxx:26
IMPL_LINK(OBrowserLine, OnButtonClicked, weld::Button &, rButton, void)
IMPL_LINK_NOARG(OBrowserLine, OnButtonFocus, weld::Widget &, void)
SvNumberFormatsSupplierObj * pSupplier
Definition: usercontrol.hxx:82