| 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 |
m_xPropertySet(std::move( xPropertySet )), |
| 41 |
m_rItemPool( rItemPool ) |
| 42 |
{ |
| 43 |
resetPropertySet( m_xPropertySet ); |
| 44 |
} |
| 45 |
|
| 46 |
ItemConverter::~ItemConverter() |
| 47 |
{ |
| 48 |
stopAllComponentListening(); |
| 49 |
} |
| 50 |
|
| 51 |
void ItemConverter::resetPropertySet( |
| 52 |
const uno::Reference< beans::XPropertySet > & xPropSet ) |
| 53 |
{ |
| 54 |
if( !xPropSet.is()) |
| 55 |
return; |
| 56 |
|
| 57 |
stopAllComponentListening(); |
| 58 |
m_xPropertySet = xPropSet; |
| 59 |
m_xPropertySetInfo = m_xPropertySet->getPropertySetInfo(); |
| 60 |
|
| 61 |
uno::Reference< lang::XComponent > xComp( m_xPropertySet, uno::UNO_QUERY ); |
| 62 |
if( xComp.is()) |
| 63 |
{ |
| 64 |
// method of base class ::utl::OEventListenerAdapter |
| 65 |
startComponentListening( xComp ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
SfxItemSet ItemConverter::CreateEmptyItemSet() const |
| 70 |
{ |
| 71 |
return SfxItemSet( GetItemPool(), GetWhichPairs() ); |
| 72 |
} |
| 73 |
|
| 74 |
void ItemConverter::_disposing( const lang::EventObject& ) |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
void ItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const |
| 79 |
{ |
| 80 |
const WhichRangesContainer& pRanges = rOutItemSet.GetRanges(); |
| 81 |
tPropertyNameWithMemberId aProperty; |
| 82 |
SfxItemPool & rPool = GetItemPool(); |
| 83 |
|
| 84 |
assert(!pRanges.empty()); |
| 85 |
OSL_ASSERT( m_xPropertySetInfo.is()); |
| 86 |
OSL_ASSERT( m_xPropertySet.is()); |
| 87 |
|
| 88 |
for(const auto& rPair : pRanges) |
| 89 |
{ |
| 90 |
sal_uInt16 nBeg = rPair.first; |
| 91 |
sal_uInt16 nEnd = rPair.second; |
| 92 |
|
| 93 |
OSL_ASSERT( nBeg <= nEnd ); |
| 94 |
for( sal_uInt16 nWhich = nBeg; nWhich <= nEnd; ++nWhich ) |
| 95 |
{ |
| 96 |
if( GetItemProperty( nWhich, aProperty )) |
| 97 |
{ |
| 98 |
// put the Property into the itemset |
| 99 |
std::unique_ptr<SfxPoolItem> pItem(rPool.GetUserOrPoolDefaultItem( nWhich ).Clone()); |
| 100 |
|
| 101 |
if( pItem ) |
| 102 |
{ |
| 103 |
try |
| 104 |
{ |
| 105 |
if( pItem->PutValue( m_xPropertySet->getPropertyValue( aProperty.first ), |
| 106 |
aProperty.second // nMemberId |
| 107 |
)) |
| 108 |
{ |
| 109 |
pItem->SetWhich(nWhich); |
| 110 |
rOutItemSet.Put( std::move(pItem) ); |
| 111 |
} |
| 112 |
} |
| 113 |
catch( const beans::UnknownPropertyException & ) |
| 114 |
{ |
| 115 |
TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty.first); |
| 116 |
} |
| 117 |
catch( const uno::Exception & ) |
| 118 |
{ |
| 119 |
DBG_UNHANDLED_EXCEPTION("chart2"); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
try |
| 126 |
{ |
| 127 |
FillSpecialItem( nWhich, rOutItemSet ); |
| 128 |
} |
| 129 |
catch( const uno::Exception & ) |
| 130 |
{ |
| 131 |
DBG_UNHANDLED_EXCEPTION("chart2"); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void ItemConverter::FillSpecialItem( |
| 139 |
sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const |
| 140 |
{ |
| 141 |
OSL_FAIL( "ItemConverter: Unhandled special item found!" ); |
| 142 |
} |
| 143 |
|
| 144 |
bool ItemConverter::ApplySpecialItem( |
| 145 |
sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ ) |
| 146 |
{ |
| 147 |
OSL_FAIL( "ItemConverter: Unhandled special item found!" ); |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet ) |
| 152 |
{ |
| 153 |
OSL_ASSERT( m_xPropertySet.is()); |
| 154 |
|
| 155 |
bool bItemsChanged = false; |
| 156 |
SfxItemIter aIter( rItemSet ); |
| 157 |
tPropertyNameWithMemberId aProperty; |
| 158 |
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 |
{ |
| 173 |
const SfxPoolItem* pItem(nullptr); |
| 174 |
if (SfxItemState::SET == rItemSet.GetItemState(SCHATTR_STAT_KIND_ERROR, false, &pItem)) |
| 175 |
if(!GetItemProperty(pItem->Which(), aProperty)) |
| 176 |
bItemsChanged = ApplySpecialItem(pItem->Which(), rItemSet); |
| 177 |
} |
| 178 |
|
| 179 |
for (const SfxPoolItem* pItem = aIter.GetCurItem(); pItem; pItem = aIter.NextItem()) |
| 180 |
{ |
| 181 |
if (SCHATTR_STAT_KIND_ERROR == pItem->Which()) |
| 182 |
continue; |
| 183 |
|
| 184 |
if( aIter.GetItemState( false ) == SfxItemState::SET ) |
| 185 |
{ |
| 186 |
if( GetItemProperty( pItem->Which(), aProperty )) |
| 187 |
{ |
| 188 |
pItem->QueryValue( aValue, aProperty.second /* nMemberId */ ); |
| 189 |
|
| 190 |
try |
| 191 |
{ |
| 192 |
if( aValue != m_xPropertySet->getPropertyValue( aProperty.first )) |
| 193 |
{ |
| 194 |
m_xPropertySet->setPropertyValue( aProperty.first, aValue ); |
| 195 |
bItemsChanged = true; |
| 196 |
} |
| 197 |
} |
| 198 |
catch( const beans::UnknownPropertyException & ) |
| 199 |
{ |
| 200 |
TOOLS_WARN_EXCEPTION( "chart2", "unknown Property: " << aProperty.first); |
| 201 |
} |
| 202 |
catch( const uno::Exception & ) |
| 203 |
{ |
| 204 |
TOOLS_WARN_EXCEPTION( "chart2", "" ); |
| 205 |
} |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return bItemsChanged; |
| 215 |
} |
| 216 |
|
| 217 |
void ItemConverter::InvalidateUnequalItems( SfxItemSet &rDestSet, const SfxItemSet &rSourceSet ) |
| 218 |
{ |
| 219 |
SfxWhichIter aIter (rSourceSet); |
| 220 |
sal_uInt16 nWhich = aIter.FirstWhich (); |
| 221 |
const SfxPoolItem *pPoolItem = nullptr; |
| 222 |
|
| 223 |
while (nWhich) |
| 224 |
{ |
| 225 |
SfxItemState nSourceItemState = aIter.GetItemState(true, &pPoolItem); |
| 226 |
if ((nSourceItemState == SfxItemState::SET) && |
| 227 |
(rDestSet.GetItemState(nWhich, true, &pPoolItem) == SfxItemState::SET)) |
| 228 |
{ |
| 229 |
if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich)) |
| 230 |
{ |
| 231 |
if( nWhich != SID_CHAR_DLG_PREVIEW_STRING ) |
| 232 |
{ |
| 233 |
rDestSet.InvalidateItem(nWhich); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
else if( nSourceItemState == SfxItemState::INVALID ) |
| 238 |
rDestSet.InvalidateItem(nWhich); |
| 239 |
|
| 240 |
nWhich = aIter.NextWhich (); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |