Bump version to 6.4.7.2.M8
[LibreOffice.git] / svx / source / unodraw / unomtabl.cxx
bloba6d56f20d4795bcb5b3f61176c20f214ccd1fb03
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 <sal/config.h>
22 #include <memory>
23 #include <set>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/drawing/PointSequence.hpp>
27 #include <svl/style.hxx>
29 #include <comphelper/sequence.hxx>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <svl/itempool.hxx>
33 #include <svl/itemset.hxx>
34 #include <svl/lstner.hxx>
35 #include <svx/xlnedit.hxx>
36 #include <svx/xlnstit.hxx>
37 #include <svx/svdmodel.hxx>
38 #include <svx/xdef.hxx>
39 #include <svx/xflhtit.hxx>
41 #include <vector>
42 #include <vcl/svapp.hxx>
45 #include <svx/unofill.hxx>
47 #include <svx/unoapi.hxx>
49 using namespace ::com::sun::star;
50 using namespace ::cppu;
52 typedef std::vector<std::unique_ptr<SfxItemSet>> ItemPoolVector;
54 class SvxUnoMarkerTable : public WeakImplHelper< container::XNameContainer, lang::XServiceInfo >,
55 public SfxListener
57 private:
58 SdrModel* mpModel;
59 SfxItemPool* mpModelPool;
61 ItemPoolVector maItemSetVector;
63 public:
64 explicit SvxUnoMarkerTable( SdrModel* pModel ) throw();
65 virtual ~SvxUnoMarkerTable() throw() override;
67 void dispose();
69 // SfxListener
70 virtual void Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) throw () override;
72 void ImplInsertByName( const OUString& aName, const uno::Any& aElement );
74 // XServiceInfo
75 virtual OUString SAL_CALL getImplementationName( ) override;
76 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
77 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
79 // XNameContainer
80 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) override;
81 virtual void SAL_CALL removeByName( const OUString& Name ) override;
83 // XNameReplace
84 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) override;
86 // XNameAccess
87 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override;
88 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override;
89 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
91 // XElementAccess
92 virtual uno::Type SAL_CALL getElementType( ) override;
93 virtual sal_Bool SAL_CALL hasElements( ) override;
96 SvxUnoMarkerTable::SvxUnoMarkerTable( SdrModel* pModel ) throw()
97 : mpModel( pModel ),
98 mpModelPool( pModel ? &pModel->GetItemPool() : nullptr )
100 if( pModel )
101 StartListening( *pModel );
104 SvxUnoMarkerTable::~SvxUnoMarkerTable() throw()
106 if( mpModel )
107 EndListening( *mpModel );
108 dispose();
111 void SvxUnoMarkerTable::dispose()
113 maItemSetVector.clear();
116 // SfxListener
117 void SvxUnoMarkerTable::Notify( SfxBroadcaster&, const SfxHint& rHint ) throw()
119 if (rHint.GetId() == SfxHintId::ThisIsAnSdrHint)
121 const SdrHint* pSdrHint = static_cast<const SdrHint*>(&rHint);
122 if( SdrHintKind::ModelCleared == pSdrHint->GetKind() )
123 dispose();
127 sal_Bool SAL_CALL SvxUnoMarkerTable::supportsService( const OUString& ServiceName )
129 return cppu::supportsService(this, ServiceName);
132 OUString SAL_CALL SvxUnoMarkerTable::getImplementationName()
134 return "SvxUnoMarkerTable";
137 uno::Sequence< OUString > SAL_CALL SvxUnoMarkerTable::getSupportedServiceNames( )
139 uno::Sequence<OUString> aSNS { "com.sun.star.drawing.MarkerTable" };
140 return aSNS;
143 void SvxUnoMarkerTable::ImplInsertByName( const OUString& aName, const uno::Any& aElement )
145 maItemSetVector.push_back(
146 std::make_unique<SfxItemSet>( *mpModelPool, svl::Items<XATTR_LINESTART, XATTR_LINEEND>{} ));
147 auto pInSet = maItemSetVector.back().get();
149 XLineEndItem aEndMarker(XATTR_LINEEND);
150 aEndMarker.SetName( aName );
151 aEndMarker.PutValue( aElement, 0 );
153 pInSet->Put( aEndMarker );
155 XLineStartItem aStartMarker(XATTR_LINESTART);
156 aStartMarker.SetName( aName );
157 aStartMarker.PutValue( aElement, 0 );
159 pInSet->Put( aStartMarker );
162 // XNameContainer
163 void SAL_CALL SvxUnoMarkerTable::insertByName( const OUString& aApiName, const uno::Any& aElement )
165 SolarMutexGuard aGuard;
167 if( hasByName( aApiName ) )
168 throw container::ElementExistException();
170 OUString aName = SvxUnogetInternalNameForItem(XATTR_LINEEND, aApiName);
172 ImplInsertByName( aName, aElement );
175 void SAL_CALL SvxUnoMarkerTable::removeByName( const OUString& aApiName )
177 SolarMutexGuard aGuard;
179 // a little quickfix for 2.0 to let applications clear api
180 // created items that are not used
181 if ( aApiName == "~clear~" )
183 dispose();
184 return;
187 OUString aName = SvxUnogetInternalNameForItem(XATTR_LINEEND, aApiName);
189 auto aIter = std::find_if(maItemSetVector.begin(), maItemSetVector.end(),
190 [&aName](const std::unique_ptr<SfxItemSet>& rpItem) {
191 const NameOrIndex *pItem = &(rpItem->Get( XATTR_LINEEND ) );
192 return pItem->GetName() == aName;
194 if (aIter != maItemSetVector.end())
196 maItemSetVector.erase( aIter );
197 return;
200 if( !hasByName( aName ) )
201 throw container::NoSuchElementException();
204 // XNameReplace
205 void SAL_CALL SvxUnoMarkerTable::replaceByName( const OUString& aApiName, const uno::Any& aElement )
207 SolarMutexGuard aGuard;
209 const OUString aName = SvxUnogetInternalNameForItem(XATTR_LINEEND, aApiName);
211 auto aIter = std::find_if(maItemSetVector.begin(), maItemSetVector.end(),
212 [&aName](const std::unique_ptr<SfxItemSet>& rpItem) {
213 const NameOrIndex *pItem = &(rpItem->Get( XATTR_LINEEND ) );
214 return pItem->GetName() == aName;
216 if (aIter != maItemSetVector.end())
218 XLineEndItem aEndMarker(XATTR_LINEEND);
219 aEndMarker.SetName( aName );
220 if( !aEndMarker.PutValue( aElement, 0 ) )
221 throw lang::IllegalArgumentException();
223 (*aIter)->Put( aEndMarker );
225 XLineStartItem aStartMarker(XATTR_LINESTART);
226 aStartMarker.SetName( aName );
227 aStartMarker.PutValue( aElement, 0 );
229 (*aIter)->Put( aStartMarker );
230 return;
233 // if it is not in our own sets, modify the pool!
234 bool bFound = false;
236 if (mpModelPool)
237 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINESTART))
239 NameOrIndex *pItem = const_cast<NameOrIndex*>(static_cast<const NameOrIndex*>(p));
240 if( pItem && pItem->GetName() == aName )
242 pItem->PutValue( aElement, 0 );
243 bFound = true;
244 break;
248 if (mpModelPool)
249 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINEEND))
251 NameOrIndex *pItem = const_cast<NameOrIndex*>(static_cast<const NameOrIndex*>(p));
252 if( pItem && pItem->GetName() == aName )
254 pItem->PutValue( aElement, 0 );
255 bFound = true;
256 break;
260 if( !bFound )
261 throw container::NoSuchElementException();
263 ImplInsertByName( aName, aElement );
266 static bool getByNameFromPool( const OUString& rSearchName, SfxItemPool const * pPool, sal_uInt16 nWhich, uno::Any& rAny )
268 if (pPool)
269 for (const SfxPoolItem* p : pPool->GetItemSurrogates(nWhich))
271 const NameOrIndex *pItem = static_cast<const NameOrIndex*>(p);
273 if( pItem && pItem->GetName() == rSearchName )
275 pItem->QueryValue( rAny );
276 return true;
280 return false;
283 // XNameAccess
284 uno::Any SAL_CALL SvxUnoMarkerTable::getByName( const OUString& aApiName )
286 SolarMutexGuard aGuard;
288 OUString aName = SvxUnogetInternalNameForItem(XATTR_LINEEND, aApiName);
290 uno::Any aAny;
292 if (mpModelPool && !aName.isEmpty())
296 if (getByNameFromPool(aName, mpModelPool, XATTR_LINESTART, aAny))
297 break;
299 if (getByNameFromPool(aName, mpModelPool, XATTR_LINEEND, aAny))
300 break;
302 throw container::NoSuchElementException();
304 while(false);
307 return aAny;
310 static void createNamesForPool( SfxItemPool const * pPool, sal_uInt16 nWhich, std::set< OUString >& rNameSet )
312 for (const SfxPoolItem* p : pPool->GetItemSurrogates(nWhich))
314 const NameOrIndex* pItem = static_cast<const NameOrIndex*>(p);
316 if( pItem == nullptr || pItem->GetName().isEmpty() )
317 continue;
319 OUString aName = SvxUnogetApiNameForItem(XATTR_LINEEND, pItem->GetName());
320 rNameSet.insert( aName );
324 uno::Sequence< OUString > SAL_CALL SvxUnoMarkerTable::getElementNames()
326 SolarMutexGuard aGuard;
328 std::set< OUString > aNameSet;
330 // search model pool for line starts
331 createNamesForPool( mpModelPool, XATTR_LINESTART, aNameSet );
333 // search model pool for line ends
334 createNamesForPool( mpModelPool, XATTR_LINEEND, aNameSet );
336 return comphelper::containerToSequence(aNameSet);
339 sal_Bool SAL_CALL SvxUnoMarkerTable::hasByName( const OUString& aName )
341 SolarMutexGuard aGuard;
343 if( aName.isEmpty() )
344 return false;
346 OUString aSearchName;
348 const NameOrIndex *pItem;
350 aSearchName = SvxUnogetInternalNameForItem(XATTR_LINESTART, aName);
351 if (mpModelPool)
352 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINESTART))
354 pItem = static_cast<const NameOrIndex*>(p);
355 if( pItem && pItem->GetName() == aSearchName )
356 return true;
359 aSearchName = SvxUnogetInternalNameForItem(XATTR_LINEEND, aName);
360 if (mpModelPool)
361 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINEEND))
363 pItem = static_cast<const NameOrIndex*>(p);
364 if( pItem && pItem->GetName() == aSearchName )
365 return true;
368 return false;
371 // XElementAccess
372 uno::Type SAL_CALL SvxUnoMarkerTable::getElementType( )
374 return cppu::UnoType<drawing::PointSequence>::get();
377 sal_Bool SAL_CALL SvxUnoMarkerTable::hasElements( )
379 SolarMutexGuard aGuard;
381 const NameOrIndex *pItem;
383 if (mpModelPool)
384 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINESTART))
386 pItem = static_cast<const NameOrIndex*>(p);
387 if( pItem && !pItem->GetName().isEmpty() )
388 return true;
391 if (mpModelPool)
392 for (const SfxPoolItem* p : mpModelPool->GetItemSurrogates(XATTR_LINEEND))
394 pItem = static_cast<const NameOrIndex*>(p);
395 if( pItem && !pItem->GetName().isEmpty() )
396 return true;
399 return false;
403 * Create a hatchtable
405 uno::Reference< uno::XInterface > SvxUnoMarkerTable_createInstance( SdrModel* pModel )
407 return *new SvxUnoMarkerTable(pModel);
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */