/src/libreoffice/chart2/source/controller/itemsetwrapper/ItemConverter.cxx
Line | Count | Source (jump to first uncovered line) |
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 <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 <comphelper/diagnose_ex.hxx> |
29 | | #include <sal/log.hxx> |
30 | | #include <utility> |
31 | | #include <chartview/ChartSfxItemIds.hxx> |
32 | | |
33 | | using namespace ::com::sun::star; |
34 | | |
35 | | namespace chart::wrapper { |
36 | | |
37 | | ItemConverter::ItemConverter( |
38 | | uno::Reference< beans::XPropertySet > xPropertySet, |
39 | | SfxItemPool& rItemPool ) : |
40 | 0 | m_xPropertySet(std::move( xPropertySet )), |
41 | 0 | m_rItemPool( rItemPool ) |
42 | 0 | { |
43 | 0 | resetPropertySet( m_xPropertySet ); |
44 | 0 | } |
45 | | |
46 | | ItemConverter::~ItemConverter() |
47 | 0 | { |
48 | 0 | stopAllComponentListening(); |
49 | 0 | } |
50 | | |
51 | | void ItemConverter::resetPropertySet( |
52 | | const uno::Reference< beans::XPropertySet > & xPropSet ) |
53 | 0 | { |
54 | 0 | if( !xPropSet.is()) |
55 | 0 | return; |
56 | | |
57 | 0 | stopAllComponentListening(); |
58 | 0 | m_xPropertySet = xPropSet; |
59 | 0 | m_xPropertySetInfo = m_xPropertySet->getPropertySetInfo(); |
60 | |
|
61 | 0 | uno::Reference< lang::XComponent > xComp( m_xPropertySet, uno::UNO_QUERY ); |
62 | 0 | if( xComp.is()) |
63 | 0 | { |
64 | | // method of base class ::utl::OEventListenerAdapter |
65 | 0 | startComponentListening( xComp ); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | SfxItemSet ItemConverter::CreateEmptyItemSet() const |
70 | 0 | { |
71 | 0 | return SfxItemSet( GetItemPool(), GetWhichPairs() ); |
72 | 0 | } |
73 | | |
74 | | void ItemConverter::_disposing( const lang::EventObject& ) |
75 | 0 | { |
76 | 0 | } |
77 | | |
78 | | void ItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const |
79 | 0 | { |
80 | 0 | const WhichRangesContainer& pRanges = rOutItemSet.GetRanges(); |
81 | 0 | tPropertyNameWithMemberId aProperty; |
82 | 0 | SfxItemPool & rPool = GetItemPool(); |
83 | |
|
84 | 0 | assert(!pRanges.empty()); |
85 | 0 | OSL_ASSERT( m_xPropertySetInfo.is()); |
86 | 0 | OSL_ASSERT( m_xPropertySet.is()); |
87 | |
|
88 | 0 | for(const auto& rPair : pRanges) |
89 | 0 | { |
90 | 0 | sal_uInt16 nBeg = rPair.first; |
91 | 0 | sal_uInt16 nEnd = rPair.second; |
92 | |
|
93 | 0 | OSL_ASSERT( nBeg <= nEnd ); |
94 | 0 | for( sal_uInt16 nWhich = nBeg; nWhich <= nEnd; ++nWhich ) |
95 | 0 | { |
96 | 0 | if( GetItemProperty( nWhich, aProperty )) |
97 | 0 | { |
98 | | // put the Property into the itemset |
99 | 0 | std::unique_ptr<SfxPoolItem> pItem(rPool.GetUserOrPoolDefaultItem( nWhich ).Clone()); |
100 | |
|
101 | 0 | if( pItem ) |
102 | 0 | { |
103 | 0 | try |
104 | 0 | { |
105 | 0 | if( pItem->PutValue( m_xPropertySet->getPropertyValue( aProperty.first ), |
106 | 0 | aProperty.second // nMemberId |
107 | 0 | )) |
108 | 0 | { |
109 | 0 | pItem->SetWhich(nWhich); |
110 | 0 | rOutItemSet.Put( std::move(pItem) ); |
111 | 0 | } |
112 | 0 | } |
113 | 0 | catch( const beans::UnknownPropertyException & ) |
114 | 0 | { |
115 | 0 | TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty.first); |
116 | 0 | } |
117 | 0 | catch( const uno::Exception & ) |
118 | 0 | { |
119 | 0 | DBG_UNHANDLED_EXCEPTION("chart2"); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | 0 | else |
124 | 0 | { |
125 | 0 | try |
126 | 0 | { |
127 | 0 | FillSpecialItem( nWhich, rOutItemSet ); |
128 | 0 | } |
129 | 0 | catch( const uno::Exception & ) |
130 | 0 | { |
131 | 0 | DBG_UNHANDLED_EXCEPTION("chart2"); |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | | void ItemConverter::FillSpecialItem( |
139 | | sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const |
140 | 0 | { |
141 | 0 | OSL_FAIL( "ItemConverter: Unhandled special item found!" ); |
142 | 0 | } |
143 | | |
144 | | bool ItemConverter::ApplySpecialItem( |
145 | | sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ ) |
146 | 0 | { |
147 | 0 | OSL_FAIL( "ItemConverter: Unhandled special item found!" ); |
148 | 0 | return false; |
149 | 0 | } |
150 | | |
151 | | bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet ) |
152 | 0 | { |
153 | 0 | OSL_ASSERT( m_xPropertySet.is()); |
154 | |
|
155 | 0 | bool bItemsChanged = false; |
156 | 0 | SfxItemIter aIter( rItemSet ); |
157 | 0 | tPropertyNameWithMemberId aProperty; |
158 | 0 | uno::Any aValue; |
159 | | |
160 | | // tdf#165491 the Item with WhichID SCHATTR_STAT_KIND_ERROR *has* to |
161 | | // be handled 1st since it sets additional information about the |
162 | | // ErrorBarStyle at uno::Reference< beans::XPropertySet > xErrorBarProp |
163 | | // that the processing of the *other* Items already need access to, |
164 | | // see 'case SCHATTR_STAT_KIND_ERROR' in |
165 | | // StatisticsItemConverter::ApplySpecialItem. This worked before the |
166 | | // change of SfxItemSet to use a std::unordered_set since the order |
167 | | // of Items was fix and - since SCHATTR_STAT_KIND_ERROR had the |
168 | | // lowest WhichID - was handled 1st. Not sure if that was by purpose |
169 | | // and it was known that this was necessary - there are no comments |
170 | | // hinting to that. In general it is bad style to rely on the 'order' |
171 | | // of Items being processed - there is no order defined in general. |
172 | 0 | { |
173 | 0 | const SfxPoolItem* pItem(nullptr); |
174 | 0 | if (SfxItemState::SET == rItemSet.GetItemState(SCHATTR_STAT_KIND_ERROR, false, &pItem)) |
175 | 0 | if(!GetItemProperty(pItem->Which(), aProperty)) |
176 | 0 | bItemsChanged = ApplySpecialItem(pItem->Which(), rItemSet); |
177 | 0 | } |
178 | |
|
179 | 0 | for (const SfxPoolItem* pItem = aIter.GetCurItem(); pItem; pItem = aIter.NextItem()) |
180 | 0 | { |
181 | 0 | if (SCHATTR_STAT_KIND_ERROR == pItem->Which()) |
182 | 0 | continue; |
183 | | |
184 | 0 | if( aIter.GetItemState( false ) == SfxItemState::SET ) |
185 | 0 | { |
186 | 0 | if( GetItemProperty( pItem->Which(), aProperty )) |
187 | 0 | { |
188 | 0 | pItem->QueryValue( aValue, aProperty.second /* nMemberId */ ); |
189 | |
|
190 | 0 | try |
191 | 0 | { |
192 | 0 | if( aValue != m_xPropertySet->getPropertyValue( aProperty.first )) |
193 | 0 | { |
194 | 0 | m_xPropertySet->setPropertyValue( aProperty.first, aValue ); |
195 | 0 | bItemsChanged = true; |
196 | 0 | } |
197 | 0 | } |
198 | 0 | catch( const beans::UnknownPropertyException & ) |
199 | 0 | { |
200 | 0 | TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty.first); |
201 | 0 | } |
202 | 0 | catch( const uno::Exception & ) |
203 | 0 | { |
204 | 0 | TOOLS_WARN_EXCEPTION( "chart2", "" ); |
205 | 0 | } |
206 | 0 | } |
207 | 0 | else |
208 | 0 | { |
209 | 0 | bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged; |
210 | 0 | } |
211 | 0 | } |
212 | 0 | } |
213 | | |
214 | 0 | return bItemsChanged; |
215 | 0 | } |
216 | | |
217 | | void ItemConverter::InvalidateUnequalItems( SfxItemSet &rDestSet, const SfxItemSet &rSourceSet ) |
218 | 0 | { |
219 | 0 | SfxWhichIter aIter (rSourceSet); |
220 | 0 | sal_uInt16 nWhich = aIter.FirstWhich (); |
221 | 0 | const SfxPoolItem *pPoolItem = nullptr; |
222 | |
|
223 | 0 | while (nWhich) |
224 | 0 | { |
225 | 0 | SfxItemState nSourceItemState = aIter.GetItemState(true, &pPoolItem); |
226 | 0 | if ((nSourceItemState == SfxItemState::SET) && |
227 | 0 | (rDestSet.GetItemState(nWhich, true, &pPoolItem) == SfxItemState::SET)) |
228 | 0 | { |
229 | 0 | if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich)) |
230 | 0 | { |
231 | 0 | if( nWhich != SID_CHAR_DLG_PREVIEW_STRING ) |
232 | 0 | { |
233 | 0 | rDestSet.InvalidateItem(nWhich); |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | 0 | else if( nSourceItemState == SfxItemState::INVALID ) |
238 | 0 | rDestSet.InvalidateItem(nWhich); |
239 | |
|
240 | 0 | nWhich = aIter.NextWhich (); |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | | } |
245 | | |
246 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |