tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / forms / source / component / entrylisthelper.cxx
blob1ec63819f7b3a59e7a69c2d4ac5cec46a0ed1b9a
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 "entrylisthelper.hxx"
21 #include <FormComponent.hxx>
23 #include <o3tl/safeint.hxx>
24 #include <osl/diagnose.h>
25 #include <comphelper/sequence.hxx>
26 #include <comphelper/property.hxx>
27 #include <com/sun/star/form/binding/XListEntryTypedSource.hpp>
28 #include <algorithm>
31 namespace frm
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::form::binding;
40 OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
41 :m_rControlModel( _rControlModel )
42 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
47 OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
48 :m_rControlModel( _rControlModel )
49 ,m_xListSource ( _rSource.m_xListSource )
50 ,m_aStringItems( _rSource.m_aStringItems )
51 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
56 OEntryListHelper::~OEntryListHelper( )
61 void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource )
63 ControlModelLock aLock( m_rControlModel );
65 // disconnect from the current external list source
66 disconnectExternalListSource();
68 // and connect to the new one
69 if ( _rxSource.is() )
70 connectExternalListSource( _rxSource, aLock );
74 Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource( )
76 return m_xListSource;
80 void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent )
82 ControlModelLock aLock( m_rControlModel );
84 OSL_ENSURE( _rEvent.Source == m_xListSource,
85 "OEntryListHelper::entryChanged: where did this come from?" );
86 OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() ),
87 "OEntryListHelper::entryChanged: invalid index!" );
88 OSL_ENSURE( _rEvent.Entries.getLength() == 1,
89 "OEntryListHelper::entryChanged: invalid string list!" );
91 if ( ( _rEvent.Position >= 0 )
92 && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() )
93 && _rEvent.Entries.hasElements()
96 m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
97 if (m_aTypedItems.hasElements())
98 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
99 stringItemListChanged( aLock );
104 void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent )
106 ControlModelLock aLock( m_rControlModel );
108 OSL_ENSURE( _rEvent.Source == m_xListSource,
109 "OEntryListHelper::entryRangeInserted: where did this come from?" );
110 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() ) && _rEvent.Entries.hasElements(),
111 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
113 if ( ( _rEvent.Position > 0 )
114 && ( o3tl::make_unsigned(_rEvent.Position) < m_aStringItems.size() )
115 && _rEvent.Entries.hasElements()
118 m_aStringItems.insert(m_aStringItems.begin() + _rEvent.Position, _rEvent.Entries.begin(), _rEvent.Entries.end());
119 if (m_aTypedItems.hasElements())
120 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
121 stringItemListChanged( aLock );
126 void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent )
128 ControlModelLock aLock( m_rControlModel );
130 OSL_ENSURE( _rEvent.Source == m_xListSource,
131 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
132 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= static_cast<sal_Int32>(m_aStringItems.size()) ),
133 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
135 if ( !(( _rEvent.Position > 0 )
136 && ( _rEvent.Count > 0 )
137 && ( _rEvent.Position + _rEvent.Count <= static_cast<sal_Int32>(m_aStringItems.size()) ))
139 return;
141 m_aStringItems.erase(m_aStringItems.begin() + _rEvent.Position,
142 m_aStringItems.begin() + _rEvent.Position + _rEvent.Count );
143 if (_rEvent.Position + _rEvent.Count <= m_aTypedItems.getLength())
145 Sequence<Any> aTmp( m_aTypedItems.getLength() - _rEvent.Count );
146 auto aTmpRange = asNonConstRange(aTmp);
147 sal_Int32 nStop = _rEvent.Position;
148 sal_Int32 i = 0;
149 for ( ; i < nStop; ++i)
151 aTmpRange[i] = m_aTypedItems[i];
153 nStop = aTmp.getLength();
154 for (sal_Int32 j = _rEvent.Position + _rEvent.Count; i < nStop; ++i, ++j)
156 aTmpRange[i] = m_aTypedItems[j];
158 m_aTypedItems = std::move(aTmp);
160 else if (m_aTypedItems.hasElements())
162 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
164 stringItemListChanged( aLock );
168 void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent )
170 ControlModelLock aLock( m_rControlModel );
172 OSL_ENSURE( _rEvent.Source == m_xListSource,
173 "OEntryListHelper::allEntriesChanged: where did this come from?" );
175 if ( _rEvent.Source == m_xListSource )
177 impl_lock_refreshList( aLock );
181 // XRefreshable
183 void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener)
185 if ( _rxListener.is() )
186 m_aRefreshListeners.addInterface( _rxListener );
190 void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener)
192 if ( _rxListener.is() )
193 m_aRefreshListeners.removeInterface( _rxListener );
197 void SAL_CALL OEntryListHelper::refresh()
200 ControlModelLock aLock( m_rControlModel );
201 impl_lock_refreshList( aLock );
204 EventObject aEvt( static_cast< XRefreshable* >( this ) );
205 m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
209 void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
211 if ( hasExternalListSource() )
212 obtainListSourceEntries( _rInstanceLock );
213 else
214 refreshInternalEntryList();
218 bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
220 if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
222 disconnectExternalListSource( );
223 return true;
225 return false;
229 void OEntryListHelper::disposing( )
231 EventObject aEvt( static_cast< XRefreshable* >( this ) );
232 m_aRefreshListeners.disposeAndClear(aEvt);
234 if ( hasExternalListSource( ) )
235 disconnectExternalListSource( );
239 void OEntryListHelper::disconnectExternalListSource( )
241 if ( m_xListSource.is() )
242 m_xListSource->removeListEntryListener( this );
244 m_xListSource.clear();
248 void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
250 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
251 OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
253 // remember it
254 m_xListSource = _rxSource;
256 // initially fill our item list
257 if ( m_xListSource.is() )
259 // be notified when the list changes ...
260 m_xListSource->addListEntryListener( this );
262 obtainListSourceEntries( _rInstanceLock );
267 void OEntryListHelper::obtainListSourceEntries( ControlModelLock& _rInstanceLock )
269 Reference< XListEntryTypedSource > xTyped;
270 xTyped.set( m_xListSource, UNO_QUERY);
271 if (xTyped.is())
273 comphelper::sequenceToContainer( m_aStringItems, xTyped->getAllListEntriesTyped( m_aTypedItems));
275 else
277 comphelper::sequenceToContainer( m_aStringItems, m_xListSource->getAllListEntries());
278 if (m_aTypedItems.hasElements())
279 m_aTypedItems = Sequence<Any>();
281 stringItemListChanged( _rInstanceLock );
285 bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
286 Any& _rOldValue, const Any& _rValue )
288 if ( hasExternalListSource() )
289 throw IllegalArgumentException( );
290 // TODO: error message
292 return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, comphelper::containerToSequence(m_aStringItems) );
296 void OEntryListHelper::setNewStringItemList( const css::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
298 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
299 css::uno::Sequence<OUString> aTmp;
300 OSL_VERIFY( _rValue >>= aTmp );
301 comphelper::sequenceToContainer(m_aStringItems, aTmp);
302 if (m_aTypedItems.hasElements())
303 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
304 stringItemListChanged( _rInstanceLock );
308 void OEntryListHelper::setNewTypedItemList( const css::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
310 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewTypedItemList: this should never have survived convertNewListSourceProperty!" );
311 if (!(_rValue >>= m_aTypedItems ))
313 OSL_VERIFY(false);
314 if (m_aTypedItems.hasElements())
315 m_aTypedItems = Sequence<Any>(); // doesn't match anymore
317 // Sets both properties, assuming that TypedItemList belongs to StringItemList.
318 stringItemListChanged( _rInstanceLock );
322 } // namespace frm
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */