1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <ItemConverter.hxx>
21 #include <com/sun/star/lang/XComponent.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <osl/diagnose.h>
24 #include <svl/itempool.hxx>
25 #include <svl/itemiter.hxx>
26 #include <svl/whiter.hxx>
27 #include <svx/svxids.hrc>
28 #include <tools/diagnose_ex.h>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star
;
33 namespace chart::wrapper
{
35 ItemConverter::ItemConverter(
36 const uno::Reference
< beans::XPropertySet
> & rPropertySet
,
37 SfxItemPool
& rItemPool
) :
38 m_xPropertySet( rPropertySet
),
39 m_rItemPool( rItemPool
)
41 resetPropertySet( m_xPropertySet
);
44 ItemConverter::~ItemConverter()
46 stopAllComponentListening();
49 void ItemConverter::resetPropertySet(
50 const uno::Reference
< beans::XPropertySet
> & xPropSet
)
55 stopAllComponentListening();
56 m_xPropertySet
= xPropSet
;
57 m_xPropertySetInfo
= m_xPropertySet
->getPropertySetInfo();
59 uno::Reference
< lang::XComponent
> xComp( m_xPropertySet
, uno::UNO_QUERY
);
62 // method of base class ::utl::OEventListenerAdapter
63 startComponentListening( xComp
);
67 SfxItemSet
ItemConverter::CreateEmptyItemSet() const
69 return SfxItemSet( GetItemPool(), GetWhichPairs() );
72 void ItemConverter::_disposing( const lang::EventObject
& )
76 void ItemConverter::FillItemSet( SfxItemSet
& rOutItemSet
) const
78 const sal_uInt16
* pRanges
= rOutItemSet
.GetRanges();
79 tPropertyNameWithMemberId aProperty
;
80 SfxItemPool
& rPool
= GetItemPool();
82 assert(pRanges
!= nullptr);
83 OSL_ASSERT( m_xPropertySetInfo
.is());
84 OSL_ASSERT( m_xPropertySet
.is());
86 while( (*pRanges
) != 0)
88 sal_uInt16 nBeg
= *pRanges
;
90 sal_uInt16 nEnd
= *pRanges
;
93 OSL_ASSERT( nBeg
<= nEnd
);
94 for( sal_uInt16 nWhich
= nBeg
; nWhich
<= nEnd
; ++nWhich
)
96 if( GetItemProperty( nWhich
, aProperty
))
98 // put the Property into the itemset
99 std::unique_ptr
<SfxPoolItem
> pItem(rPool
.GetDefaultItem( nWhich
).Clone());
105 if( pItem
->PutValue( m_xPropertySet
->getPropertyValue( aProperty
.first
),
106 aProperty
.second
// nMemberId
109 pItem
->SetWhich(nWhich
);
110 rOutItemSet
.Put( std::move(pItem
) );
113 catch( const beans::UnknownPropertyException
& )
115 TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty
.first
);
117 catch( const uno::Exception
& )
119 DBG_UNHANDLED_EXCEPTION("chart2");
127 FillSpecialItem( nWhich
, rOutItemSet
);
129 catch( const uno::Exception
& )
131 DBG_UNHANDLED_EXCEPTION("chart2");
138 void ItemConverter::FillSpecialItem(
139 sal_uInt16
/*nWhichId*/, SfxItemSet
& /*rOutItemSet*/ ) const
141 OSL_FAIL( "ItemConverter: Unhandled special item found!" );
144 bool ItemConverter::ApplySpecialItem(
145 sal_uInt16
/*nWhichId*/, const SfxItemSet
& /*rItemSet*/ )
147 OSL_FAIL( "ItemConverter: Unhandled special item found!" );
151 bool ItemConverter::ApplyItemSet( const SfxItemSet
& rItemSet
)
153 OSL_ASSERT( m_xPropertySet
.is());
155 bool bItemsChanged
= false;
156 SfxItemIter
aIter( rItemSet
);
157 tPropertyNameWithMemberId aProperty
;
160 for (const SfxPoolItem
* pItem
= aIter
.GetCurItem(); pItem
; pItem
= aIter
.NextItem())
162 if( rItemSet
.GetItemState( pItem
->Which(), false ) == SfxItemState::SET
)
164 if( GetItemProperty( pItem
->Which(), aProperty
))
166 pItem
->QueryValue( aValue
, aProperty
.second
/* nMemberId */ );
170 if( aValue
!= m_xPropertySet
->getPropertyValue( aProperty
.first
))
172 m_xPropertySet
->setPropertyValue( aProperty
.first
, aValue
);
173 bItemsChanged
= true;
176 catch( const beans::UnknownPropertyException
& )
178 TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty
.first
);
180 catch( const uno::Exception
& )
182 TOOLS_WARN_EXCEPTION( "chart2", "" );
187 bItemsChanged
= ApplySpecialItem( pItem
->Which(), rItemSet
) || bItemsChanged
;
192 return bItemsChanged
;
195 void ItemConverter::InvalidateUnequalItems( SfxItemSet
&rDestSet
, const SfxItemSet
&rSourceSet
)
197 SfxWhichIter
aIter (rSourceSet
);
198 sal_uInt16 nWhich
= aIter
.FirstWhich ();
199 const SfxPoolItem
*pPoolItem
= nullptr;
203 if ((rSourceSet
.GetItemState(nWhich
, true, &pPoolItem
) == SfxItemState::SET
) &&
204 (rDestSet
.GetItemState(nWhich
, true, &pPoolItem
) == SfxItemState::SET
))
206 if (rSourceSet
.Get(nWhich
) != rDestSet
.Get(nWhich
))
208 if( nWhich
!= SID_CHAR_DLG_PREVIEW_STRING
)
210 rDestSet
.InvalidateItem(nWhich
);
214 else if( rSourceSet
.GetItemState(nWhich
, true, &pPoolItem
) == SfxItemState::DONTCARE
)
215 rDestSet
.InvalidateItem(nWhich
);
217 nWhich
= aIter
.NextWhich ();
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */