sw/inc/format.hxx | 16 ++++++++++ sw/source/core/attr/format.cxx | 7 +--- sw/source/core/attr/swatrset.cxx | 61 +++++++++++++++++++-------------------- sw/source/core/crsr/crstrvl.cxx | 18 +++++------ 4 files changed, 57 insertions(+), 45 deletions(-)
New commits: commit aef976e9ba29ed8af96d2b94a38e7ee8cf623fc8 Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Wed Mar 2 10:14:23 2022 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Wed Mar 2 12:06:20 2022 +0100 use SfxItemSet::GetItemIfSet in sw/source/core/attr Change-Id: Icec7a62a7b2987ffe9c0f74e2b49993bd2b9bfbd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130821 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/sw/inc/format.hxx b/sw/inc/format.hxx index a42d04f276cf..d2eae51ee1d1 100644 --- a/sw/inc/format.hxx +++ b/sw/inc/format.hxx @@ -98,8 +98,24 @@ public: { return static_cast<const T&>(GetFormatAttr(sal_uInt16(nWhich), bInParents)); } + SfxItemState GetItemState( sal_uInt16 nWhich, bool bSrchInParent = true, const SfxPoolItem **ppItem = nullptr ) const; + template<class T> + SfxItemState GetItemState( TypedWhichId<T> nWhich, bool bSrchInParent = true, + const T **ppItem = nullptr ) const + { return GetItemState(sal_uInt16(nWhich), bSrchInParent, reinterpret_cast<const SfxPoolItem **>(ppItem)); } + + /// Templatized version of GetItemState() to directly return the correct type. + template<class T> + const T * GetItemIfSet( TypedWhichId<T> nWhich, + bool bSrchInParent = true ) const + { + const SfxPoolItem * pItem = nullptr; + if( SfxItemState::SET == GetItemState(sal_uInt16(nWhich), bSrchInParent, &pItem) ) + return static_cast<const T*>(pItem); + return nullptr; + } SfxItemState GetBackgroundState(std::unique_ptr<SvxBrushItem>& rItem) const; virtual bool SetFormatAttr( const SfxPoolItem& rAttr ); virtual bool SetFormatAttr( const SfxItemSet& rSet ); diff --git a/sw/source/core/attr/format.cxx b/sw/source/core/attr/format.cxx index a878f3bf8f36..2ce4c9aa7dc6 100644 --- a/sw/source/core/attr/format.cxx +++ b/sw/source/core/attr/format.cxx @@ -550,9 +550,7 @@ bool SwFormat::SetFormatAttr( const SfxItemSet& rSet ) if (supportsFullDrawingLayerFillAttributeSet()) { - const SfxPoolItem* pSource = nullptr; - - if(SfxItemState::SET == aTempSet.GetItemState(RES_BACKGROUND, false, &pSource)) + if(const SvxBrushItem* pSource = aTempSet.GetItemIfSet(RES_BACKGROUND, false)) { // FALLBACKBREAKHERE should not be used; instead use [XATTR_FILL_FIRST .. XATTR_FILL_LAST] SAL_INFO("sw.core", "Do no longer use SvxBrushItem, instead use [XATTR_FILL_FIRST .. XATTR_FILL_LAST] FillAttributes (simple fallback is in place and used)"); @@ -560,8 +558,7 @@ bool SwFormat::SetFormatAttr( const SfxItemSet& rSet ) // copy all items to be set anyways to a local ItemSet with is also prepared for the new // fill attribute ranges [XATTR_FILL_FIRST .. XATTR_FILL_LAST]. Add the attributes // corresponding as good as possible to the new fill properties and set the whole ItemSet - const SvxBrushItem& rSource(pSource->StaticWhichCast(RES_BACKGROUND)); - setSvxBrushItemAsFillAttributesToTargetSet(rSource, aTempSet); + setSvxBrushItemAsFillAttributesToTargetSet(*pSource, aTempSet); if(IsModifyLocked()) { diff --git a/sw/source/core/attr/swatrset.cxx b/sw/source/core/attr/swatrset.cxx index dea4ccfef5aa..606ae209fc53 100644 --- a/sw/source/core/attr/swatrset.cxx +++ b/sw/source/core/attr/swatrset.cxx @@ -215,34 +215,33 @@ bool SwAttrSet::SetModifyAtAttr( const sw::BroadcastingModify* pModify ) { bool bSet = false; - const SfxPoolItem* pItem; - if( SfxItemState::SET == GetItemState( RES_PAGEDESC, false, &pItem ) && - pItem->StaticWhichCast(RES_PAGEDESC).GetDefinedIn() != pModify ) + const SwFormatPageDesc* pPageDescItem = GetItemIfSet( RES_PAGEDESC, false ); + if( pPageDescItem && + pPageDescItem->GetDefinedIn() != pModify ) { - const_cast<SwFormatPageDesc&>(pItem->StaticWhichCast(RES_PAGEDESC)).ChgDefinedIn( pModify ); + const_cast<SwFormatPageDesc&>(*pPageDescItem).ChgDefinedIn( pModify ); bSet = true; } - if(SfxItemState::SET == GetItemState( RES_PARATR_DROP, false, &pItem )) + if(SwFormatDrop* pFormatDrop = const_cast<SwFormatDrop*>(GetItemIfSet( RES_PARATR_DROP, false ))) { auto pDropDefiner = dynamic_cast<const sw::FormatDropDefiner*>(pModify); - SwFormatDrop& rFormatDrop = const_cast<SwFormatDrop&>(pItem->StaticWhichCast(RES_PARATR_DROP)); // If CharFormat is set and it is set in different attribute pools then // the CharFormat has to be copied. - SwCharFormat* pCharFormat = rFormatDrop.GetCharFormat(); + SwCharFormat* pCharFormat = pFormatDrop->GetCharFormat(); if(pCharFormat && GetPool() != pCharFormat->GetAttrSet().GetPool()) { pCharFormat = GetDoc()->CopyCharFormat(*pCharFormat); - rFormatDrop.SetCharFormat(pCharFormat); + pFormatDrop->SetCharFormat(pCharFormat); } - rFormatDrop.ChgDefinedIn(pDropDefiner); + pFormatDrop->ChgDefinedIn(pDropDefiner); bSet = true; } - if( SfxItemState::SET == GetItemState( RES_BOXATR_FORMULA, false, &pItem ) && - pItem->StaticWhichCast(RES_BOXATR_FORMULA).GetDefinedIn() != pModify ) + const SwTableBoxFormula* pBoxFormula = GetItemIfSet( RES_BOXATR_FORMULA, false ); + if( pBoxFormula && pBoxFormula->GetDefinedIn() != pModify ) { - const_cast<SwTableBoxFormula&>(pItem->StaticWhichCast(RES_BOXATR_FORMULA)).ChgDefinedIn( pModify ); + const_cast<SwTableBoxFormula&>(*pBoxFormula).ChgDefinedIn( pModify ); bSet = true; } @@ -262,15 +261,15 @@ void SwAttrSet::CopyToModify( sw::BroadcastingModify& rMod ) const // #i92811# std::unique_ptr<SfxStringItem> pNewListIdItem; - const SfxPoolItem* pItem; const SwDoc *pSrcDoc = GetDoc(); SwDoc *pDstDoc = pCNd ? &pCNd->GetDoc() : pFormat->GetDoc(); // Does the NumRule has to be copied? + const SwNumRuleItem* pNumRuleItem; if( pSrcDoc != pDstDoc && - SfxItemState::SET == GetItemState( RES_PARATR_NUMRULE, false, &pItem ) ) + (pNumRuleItem = GetItemIfSet( RES_PARATR_NUMRULE, false )) ) { - const OUString& rNm = pItem->StaticWhichCast(RES_PARATR_NUMRULE).GetValue(); + const OUString& rNm = pNumRuleItem->GetValue(); if( !rNm.isEmpty() ) { SwNumRule* pDestRule = pDstDoc->FindNumRulePtr( rNm ); @@ -283,12 +282,11 @@ void SwAttrSet::CopyToModify( sw::BroadcastingModify& rMod ) const // copy list and if needed also the corresponding list style // for text nodes + const SfxStringItem* pStrItem; if ( pSrcDoc != pDstDoc && pCNd && pCNd->IsTextNode() && - GetItemState( RES_PARATR_LIST_ID, false, &pItem ) == SfxItemState::SET ) + (pStrItem = GetItemIfSet( RES_PARATR_LIST_ID, false )) ) { - auto pStrItem = dynamic_cast<const SfxStringItem*>(pItem); - assert(pStrItem); const OUString& sListId = pStrItem->GetValue(); if ( !sListId.isEmpty() && !pDstDoc->getIDocumentListsAccess().getListByName( sListId ) ) @@ -332,11 +330,11 @@ void SwAttrSet::CopyToModify( sw::BroadcastingModify& rMod ) const } std::optional< SfxItemSet > tmpSet; - - if( pSrcDoc != pDstDoc && SfxItemState::SET == GetItemState( - RES_PAGEDESC, false, &pItem )) + const SwFormatPageDesc* pPageDescItem; + if( pSrcDoc != pDstDoc && (pPageDescItem = GetItemIfSet( + RES_PAGEDESC, false ))) { - const SwPageDesc* pPgDesc = pItem->StaticWhichCast(RES_PAGEDESC).GetPageDesc(); + const SwPageDesc* pPgDesc = pPageDescItem->GetPageDesc(); if( pPgDesc ) { tmpSet.emplace(*this); @@ -348,13 +346,14 @@ void SwAttrSet::CopyToModify( sw::BroadcastingModify& rMod ) const pDstDoc->CopyPageDesc( *pPgDesc, *pDstPgDesc ); } SwFormatPageDesc aDesc( pDstPgDesc ); - aDesc.SetNumOffset( pItem->StaticWhichCast(RES_PAGEDESC).GetNumOffset() ); + aDesc.SetNumOffset( pPageDescItem->GetNumOffset() ); tmpSet->Put( aDesc ); } } - if( pSrcDoc != pDstDoc && SfxItemState::SET == GetItemState( RES_ANCHOR, false, &pItem ) - && pItem->StaticWhichCast(RES_ANCHOR).GetContentAnchor() != nullptr ) + const SwFormatAnchor* pAnchorItem; + if( pSrcDoc != pDstDoc && (pAnchorItem = GetItemIfSet( RES_ANCHOR, false )) + && pAnchorItem->GetContentAnchor() != nullptr ) { if( !tmpSet ) tmpSet.emplace( *this ); @@ -363,19 +362,19 @@ void SwAttrSet::CopyToModify( sw::BroadcastingModify& rMod ) const tmpSet->ClearItem( RES_ANCHOR ); } + const SwFormatAutoFormat* pAutoFormatItem; if (pSrcDoc != pDstDoc && - SfxItemState::SET == GetItemState(RES_PARATR_LIST_AUTOFMT, false, &pItem)) + (pAutoFormatItem = GetItemIfSet(RES_PARATR_LIST_AUTOFMT, false))) { - SfxItemSet const& rAutoStyle(*pItem->StaticWhichCast(RES_PARATR_LIST_AUTOFMT).GetStyleHandle()); + SfxItemSet const& rAutoStyle(*pAutoFormatItem->GetStyleHandle()); std::shared_ptr<SfxItemSet> const pNewSet( rAutoStyle.SfxItemSet::Clone(true, &pDstDoc->GetAttrPool())); // fix up character style, it contains pointers to pSrcDoc - if (SfxItemState::SET == pNewSet->GetItemState(RES_TXTATR_CHARFMT, false, &pItem)) + if (const SwFormatCharFormat* pCharFormatItem = pNewSet->GetItemIfSet(RES_TXTATR_CHARFMT, false)) { - const auto& rChar(pItem->StaticWhichCast(RES_TXTATR_CHARFMT)); - SwCharFormat *const pCopy(pDstDoc->CopyCharFormat(*rChar.GetCharFormat())); - const_cast<SwFormatCharFormat&>(rChar).SetCharFormat(pCopy); + SwCharFormat *const pCopy(pDstDoc->CopyCharFormat(*pCharFormatItem->GetCharFormat())); + const_cast<SwFormatCharFormat&>(*pCharFormatItem).SetCharFormat(pCopy); } SwFormatAutoFormat item(RES_PARATR_LIST_AUTOFMT); diff --git a/sw/source/core/crsr/crstrvl.cxx b/sw/source/core/crsr/crstrvl.cxx index e548b748753c..7fa05b1920e3 100644 --- a/sw/source/core/crsr/crstrvl.cxx +++ b/sw/source/core/crsr/crstrvl.cxx @@ -1742,18 +1742,18 @@ bool SwCursorShell::GetContentAtPos( const Point& rPt, const SwTableNode* pTableNd; const SwTableBox* pBox; const SwStartNode* pSttNd = pTextNd->FindTableBoxStartNode(); - const SfxPoolItem* pItem; + const SwTableBoxFormula* pItem; +#ifdef DBG_UTIL + const SwTableBoxValue* pItem2 = nullptr; +#endif if( pSttNd && nullptr != ( pTableNd = pTextNd->FindTableNode()) && nullptr != ( pBox = pTableNd->GetTable().GetTableBox( pSttNd->GetIndex() )) && #ifdef DBG_UTIL - ( SfxItemState::SET == pBox->GetFrameFormat()->GetItemState( - RES_BOXATR_FORMULA, false, &pItem ) || - SfxItemState::SET == pBox->GetFrameFormat()->GetItemState( - RES_BOXATR_VALUE, false, &pItem )) + ( (pItem = pBox->GetFrameFormat()->GetItemIfSet( RES_BOXATR_FORMULA, false )) || + (pItem2 = pBox->GetFrameFormat()->GetItemIfSet( RES_BOXATR_VALUE, false )) ) #else - SfxItemState::SET == pBox->GetFrameFormat()->GetItemState( - RES_BOXATR_FORMULA, false, &pItem ) + (pItem = pBox->GetFrameFormat()->GetItemIfSet( RES_BOXATR_FORMULA, false )) #endif ) { @@ -1781,11 +1781,11 @@ bool SwCursorShell::GetContentAtPos( const Point& rPt, // (for UI) formula rContentAtPos.eContentAtPos = IsAttrAtPos::TableBoxFml; #ifdef DBG_UTIL - if( RES_BOXATR_VALUE == pItem->Which() ) + if( pItem2 ) rContentAtPos.eContentAtPos = IsAttrAtPos::TableBoxValue; else #endif - const_cast<SwTableBoxFormula&>(pItem->StaticWhichCast(RES_BOXATR_FORMULA)).PtrToBoxNm( &pTableNd->GetTable() ); + const_cast<SwTableBoxFormula&>(*pItem).PtrToBoxNm( &pTableNd->GetTable() ); bRet = true; if( bSetCursor )