Kaydet (Commit) 93067f37 authored tarafından Michael Stahl's avatar Michael Stahl

tdf#91383: sw: prevent style preview from actually creating styles

The dialog/sidebar should not actually create styles that don't exist yet,
because it messes up Undo and the (unused) styles are then unnecessarily
exported to documents.

Due to Writer's ... unusual SwDocStyleSheet class this is a bit tricky.

Add a new function GetItemSetForPreview() and use it from the style preview
code.

The implementation does not use FillPhysical so will temporarily create and
then delete any non-existing styles.

Skip page and numbering styles for now since they don't have a useful preview.

(regression from ca953076)

Change-Id: Id6ee30ea467fc24c991547a4c23a9ce14fdd86c7
üst 110dc43d
......@@ -139,6 +139,11 @@ public:
virtual void SetHelpId( const OUString& r, sal_uLong nId );
virtual SfxItemSet& GetItemSet();
/// Due to writer's usual lack of sanity this is a separate function for
/// preview only; it shall not create the style in case it does not exist.
/// If the style has parents, it is _not_ required that the returned item
/// set has parents (i.e. use it for display purposes only).
virtual std::unique_ptr<SfxItemSet> GetItemSetForPreview();
};
/* Class to iterate and search on a SfxStyleSheetBasePool */
......
......@@ -284,6 +284,11 @@ SfxItemSet& SfxStyleSheetBase::GetItemSet()
return *pSet;
}
std::unique_ptr<SfxItemSet> SfxStyleSheetBase::GetItemSetForPreview()
{
return std::unique_ptr<SfxItemSet>(new SfxItemSet(GetItemSet()));
}
/**
* Set help file and ID and return it
*/
......
......@@ -60,65 +60,67 @@ CommonStylePreviewRenderer::~CommonStylePreviewRenderer()
bool CommonStylePreviewRenderer::recalculate()
{
const SfxItemSet& aItemSet = mpStyle->GetItemSet();
maFont = SvxFont();
std::unique_ptr<SfxItemSet> pItemSet(mpStyle->GetItemSetForPreview());
if (!pItemSet) return false;
const SfxPoolItem* pItem;
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_WEIGHT)) != nullptr)
{
maFont.SetWeight(static_cast<const SvxWeightItem*>(pItem)->GetWeight());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_POSTURE)) != nullptr)
{
maFont.SetItalic(static_cast<const SvxPostureItem*>(pItem)->GetPosture());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CONTOUR)) != nullptr)
{
maFont.SetOutline(static_cast< const SvxContourItem*>(pItem)->GetValue());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_SHADOWED)) != nullptr)
{
maFont.SetShadow(static_cast<const SvxShadowedItem*>(pItem)->GetValue());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_RELIEF)) != nullptr)
{
maFont.SetRelief(static_cast<FontRelief>(static_cast<const SvxCharReliefItem*>(pItem)->GetValue()));
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_UNDERLINE)) != nullptr)
{
maFont.SetUnderline(static_cast< const SvxUnderlineItem*>(pItem)->GetLineStyle());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_OVERLINE)) != nullptr)
{
maFont.SetOverline(static_cast<FontUnderline>(static_cast<const SvxOverlineItem*>(pItem)->GetValue()));
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_STRIKEOUT)) != nullptr)
{
maFont.SetStrikeout(static_cast<const SvxCrossedOutItem*>(pItem)->GetStrikeout());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_CASEMAP)) != nullptr)
{
maFont.SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_EMPHASISMARK)) != nullptr)
{
maFont.SetEmphasisMark(static_cast<const SvxEmphasisMarkItem*>(pItem)->GetEmphasisMark());
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_COLOR)) != nullptr)
{
maFontColor = Color(static_cast<const SvxColorItem*>(pItem)->GetValue());
}
if (mpStyle->GetFamily() == SFX_STYLE_FAMILY_PARA)
{
if ((pItem = aItemSet.GetItem(XATTR_FILLSTYLE)) != nullptr)
if ((pItem = pItemSet->GetItem(XATTR_FILLSTYLE)) != nullptr)
{
sal_uInt16 aFillStyle = static_cast<const XFillStyleItem*>(pItem)->GetValue();
if (aFillStyle == drawing::FillStyle_SOLID)
{
if ((pItem = aItemSet.GetItem(XATTR_FILLCOLOR)) != nullptr)
if ((pItem = pItemSet->GetItem(XATTR_FILLCOLOR)) != nullptr)
{
maBackgroundColor = Color(static_cast<const XFillColorItem*>(pItem)->GetColorValue());
}
......@@ -126,7 +128,7 @@ bool CommonStylePreviewRenderer::recalculate()
}
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONT)) != nullptr)
{
const SvxFontItem* pFontItem = static_cast<const SvxFontItem*>(pItem);
maFont.SetName(pFontItem->GetFamilyName());
......@@ -137,7 +139,7 @@ bool CommonStylePreviewRenderer::recalculate()
return false;
}
if ((pItem = aItemSet.GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
if ((pItem = pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT)) != nullptr)
{
const SvxFontHeightItem* pFontHeightItem = static_cast<const SvxFontHeightItem*>(pItem);
Size aFontSize(0, pFontHeightItem->GetHeight());
......
......@@ -638,10 +638,13 @@ void SvxStyleBox_Impl::SetupEntry(vcl::RenderContext& rRenderContext, vcl::Windo
if (pStyle )
{
const SfxItemSet& aItemSet = pStyle->GetItemSet();
std::unique_ptr<const SfxItemSet> const pItemSet(pStyle->GetItemSetForPreview());
if (!pItemSet) return;
const SvxFontItem *pFontItem = static_cast< const SvxFontItem* >( aItemSet.GetItem( SID_ATTR_CHAR_FONT ) );
const SvxFontHeightItem *pFontHeightItem = static_cast< const SvxFontHeightItem* >( aItemSet.GetItem( SID_ATTR_CHAR_FONTHEIGHT ) );
const SvxFontItem * const pFontItem =
static_cast<const SvxFontItem*>(pItemSet->GetItem(SID_ATTR_CHAR_FONT));
const SvxFontHeightItem * const pFontHeightItem =
static_cast<const SvxFontHeightItem*>(pItemSet->GetItem(SID_ATTR_CHAR_FONTHEIGHT));
if ( pFontItem && pFontHeightItem )
{
......@@ -654,43 +657,43 @@ void SvxStyleBox_Impl::SetupEntry(vcl::RenderContext& rRenderContext, vcl::Windo
aFont.SetStyleName(pFontItem->GetStyleName());
aFont.SetSize(aPixelSize);
const SfxPoolItem *pItem = aItemSet.GetItem( SID_ATTR_CHAR_WEIGHT );
const SfxPoolItem *pItem = pItemSet->GetItem( SID_ATTR_CHAR_WEIGHT );
if ( pItem )
aFont.SetWeight( static_cast< const SvxWeightItem* >( pItem )->GetWeight() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_POSTURE );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_POSTURE );
if ( pItem )
aFont.SetItalic( static_cast< const SvxPostureItem* >( pItem )->GetPosture() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_CONTOUR );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_CONTOUR );
if ( pItem )
aFont.SetOutline( static_cast< const SvxContourItem* >( pItem )->GetValue() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_SHADOWED );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_SHADOWED );
if ( pItem )
aFont.SetShadow( static_cast< const SvxShadowedItem* >( pItem )->GetValue() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_RELIEF );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_RELIEF );
if ( pItem )
aFont.SetRelief( static_cast< FontRelief >( static_cast< const SvxCharReliefItem* >( pItem )->GetValue() ) );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_UNDERLINE );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_UNDERLINE );
if ( pItem )
aFont.SetUnderline( static_cast< const SvxUnderlineItem* >( pItem )->GetLineStyle() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_OVERLINE );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_OVERLINE );
if ( pItem )
aFont.SetOverline( static_cast< FontUnderline >( static_cast< const SvxOverlineItem* >( pItem )->GetValue() ) );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_STRIKEOUT );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_STRIKEOUT );
if ( pItem )
aFont.SetStrikeout( static_cast< const SvxCrossedOutItem* >( pItem )->GetStrikeout() );
pItem = aItemSet.GetItem( SID_ATTR_CHAR_CASEMAP );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_CASEMAP );
if ( pItem )
aFont.SetCaseMap(static_cast<const SvxCaseMapItem*>(pItem)->GetCaseMap());
pItem = aItemSet.GetItem( SID_ATTR_CHAR_EMPHASISMARK );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_EMPHASISMARK );
if ( pItem )
aFont.SetEmphasisMark( static_cast< const SvxEmphasisMarkItem* >( pItem )->GetEmphasisMark() );
......@@ -701,14 +704,14 @@ void SvxStyleBox_Impl::SetupEntry(vcl::RenderContext& rRenderContext, vcl::Windo
rRenderContext.SetFont(aFont);
pItem = aItemSet.GetItem( SID_ATTR_CHAR_COLOR );
pItem = pItemSet->GetItem( SID_ATTR_CHAR_COLOR );
// text color, when nothing is selected
if ( (NULL != pItem) && bIsNotSelected)
aFontCol = Color( static_cast< const SvxColorItem* >( pItem )->GetValue() );
sal_uInt16 style = drawing::FillStyle_NONE;
// which kind of Fill style is selected
pItem = aItemSet.GetItem( XATTR_FILLSTYLE );
pItem = pItemSet->GetItem( XATTR_FILLSTYLE );
// only when ok and not selected
if ( (NULL != pItem) && bIsNotSelected)
style = static_cast< const XFillStyleItem* >( pItem )->GetValue();
......@@ -718,7 +721,7 @@ void SvxStyleBox_Impl::SetupEntry(vcl::RenderContext& rRenderContext, vcl::Windo
case drawing::FillStyle_SOLID:
{
// set background color
pItem = aItemSet.GetItem( XATTR_FILLCOLOR );
pItem = pItemSet->GetItem( XATTR_FILLCOLOR );
if ( NULL != pItem )
aBackCol = Color( static_cast< const XFillColorItem* >( pItem )->GetColorValue() );
......
......@@ -61,10 +61,12 @@ class SW_DLLPUBLIC SwDocStyleSheet : public SfxStyleSheetBase
enum FillStyleType {
FillOnlyName,
FillAllInfo,
FillPhysical
FillPhysical,
FillPreview,
};
SAL_DLLPRIVATE bool FillStyleSheet( FillStyleType eFType );
SAL_DLLPRIVATE bool FillStyleSheet(FillStyleType eFType,
std::unique_ptr<SfxItemSet> * o_ppFlatSet = nullptr);
protected:
virtual ~SwDocStyleSheet();
......@@ -99,6 +101,7 @@ public:
const bool bResetIndentAttrsAtParagraphStyle = false );
virtual SfxItemSet& GetItemSet() SAL_OVERRIDE;
virtual std::unique_ptr<SfxItemSet> GetItemSetForPreview() override;
/** new method for paragraph styles to merge indent attributes of applied list
style into the given item set, if the list style indent attributes are applicable. */
void MergeIndentAttrsOfListStyle( SfxItemSet& rSet );
......
......@@ -1163,6 +1163,61 @@ bool SwDocStyleSheet::SetFollow( const OUString& rStr)
return true;
}
static
std::unique_ptr<SfxItemSet> lcl_SwFormatToFlatItemSet(SwFormat *const pFormat)
{
// note: we don't add the odd items that GetItemSet() would add
// because they don't seem relevant for preview
std::vector<SfxItemSet const*> sets;
sets.push_back(&pFormat->GetAttrSet());
while (SfxItemSet const*const pParent = sets.back()->GetParent())
{
sets.push_back(pParent);
}
// start by copying top-level parent set
std::unique_ptr<SfxItemSet> pRet(new SfxItemSet(*sets.back()));
sets.pop_back();
for (auto iter = sets.rbegin(); iter != sets.rend(); ++iter)
{ // in reverse so child overrides parent
pRet->Put(**iter);
}
return pRet;
}
std::unique_ptr<SfxItemSet> SwDocStyleSheet::GetItemSetForPreview()
{
if (SFX_STYLE_FAMILY_PAGE == nFamily || SFX_STYLE_FAMILY_PSEUDO == nFamily)
{
SAL_WARN("sw.ui", "GetItemSetForPreview not implemented for page or number style");
return std::unique_ptr<SfxItemSet>();
}
if (!bPhysical)
{
// because not only this style, but also any number of its parents
// (or follow style) may not actually exist in the document at this
// time, return one "flattened" item set that contains all items from
// all parents.
std::unique_ptr<SfxItemSet> pRet;
FillStyleSheet(FillPreview, &pRet);
assert(pRet);
return pRet;
}
else
{
switch (nFamily)
{
case SFX_STYLE_FAMILY_CHAR:
return lcl_SwFormatToFlatItemSet(pCharFormat);
case SFX_STYLE_FAMILY_PARA:
return lcl_SwFormatToFlatItemSet(pColl);
case SFX_STYLE_FAMILY_FRAME:
return lcl_SwFormatToFlatItemSet(pFrameFormat);
default:
assert(false);
}
}
}
// extract ItemSet to Name and Family, Mask
SfxItemSet& SwDocStyleSheet::GetItemSet()
......@@ -1703,7 +1758,8 @@ static void lcl_DeleteInfoStyles( sal_uInt16 nFamily, std::vector<void*>& rArr,
}
// determine the format
bool SwDocStyleSheet::FillStyleSheet( FillStyleType eFType )
bool SwDocStyleSheet::FillStyleSheet(
FillStyleType const eFType, std::unique_ptr<SfxItemSet> *const o_ppFlatSet)
{
bool bRet = false;
sal_uInt16 nPoolId = USHRT_MAX;
......@@ -1711,7 +1767,7 @@ bool SwDocStyleSheet::FillStyleSheet( FillStyleType eFType )
bool bCreate = FillPhysical == eFType;
bool bDeleteInfo = false;
bool bFillOnlyInfo = FillAllInfo == eFType;
bool bFillOnlyInfo = FillAllInfo == eFType || FillPreview == eFType;
std::vector<void*> aDelArr;
switch(nFamily)
......@@ -1879,6 +1935,12 @@ bool SwDocStyleSheet::FillStyleSheet( FillStyleType eFType )
if( RES_CONDTXTFMTCOLL == pFormat->Which() )
_nMask |= SWSTYLEBIT_CONDCOLL;
if (FillPreview == eFType)
{
assert(o_ppFlatSet);
*o_ppFlatSet = lcl_SwFormatToFlatItemSet(pFormat);
}
}
SetMask( _nMask );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment