cid#1607171 Data race condition
[LibreOffice.git] / svx / source / sdr / properties / defaultproperties.cxx
blob75bef5ff697cd9941bd3eec0eec2cd081cf286e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/config.h>
22 #include <svx/sdr/properties/defaultproperties.hxx>
23 #include <sdr/properties/itemsettools.hxx>
24 #include <svl/itemset.hxx>
25 #include <svl/whiter.hxx>
26 #include <vector>
27 #include <svx/svdobj.hxx>
28 #include <libxml/xmlwriter.h>
29 #include <svx/svdmodel.hxx>
30 #include <svx/svdtrans.hxx>
31 #include <svx/xbtmpit.hxx>
33 namespace sdr::properties
35 SfxItemSet DefaultProperties::CreateObjectSpecificItemSet(SfxItemPool& rPool)
37 // Basic implementation; Basic object has NO attributes
38 return SfxItemSet(rPool);
41 DefaultProperties::DefaultProperties(SdrObject& rObj)
42 : BaseProperties(rObj)
46 DefaultProperties::DefaultProperties(const DefaultProperties& rProps, SdrObject& rObj)
47 : BaseProperties(rObj)
49 if(!rProps.moItemSet)
50 return;
52 // Clone may be to another model and thus another ItemPool.
53 // SfxItemSet supports that thus we are able to Clone all
54 // SfxItemState::SET items to the target pool.
55 moItemSet.emplace(rProps.moItemSet->CloneAsValue(
56 true,
57 &rObj.getSdrModelFromSdrObject().GetItemPool()));
59 // React on ModelChange: If metric has changed, scale items.
60 // As seen above, clone is supported, but scale is not included,
61 // thus: TTTT maybe add scale to SfxItemSet::Clone() (?)
62 // tdf#117707 correct ModelChange detection
63 const bool bModelChange(&rObj.getSdrModelFromSdrObject() != &rProps.GetSdrObject().getSdrModelFromSdrObject());
65 if(bModelChange)
67 const MapUnit aOldUnit(rProps.GetSdrObject().getSdrModelFromSdrObject().GetScaleUnit());
68 const MapUnit aNewUnit(rObj.getSdrModelFromSdrObject().GetScaleUnit());
69 const bool bScaleUnitChanged(aNewUnit != aOldUnit);
71 if(bScaleUnitChanged)
73 const Fraction aMetricFactor(GetMapFactor(aOldUnit, aNewUnit).X());
75 ScaleItemSet(*moItemSet, aMetricFactor);
79 // do not keep parent info, this may be changed by later constructors.
80 // This class just copies the ItemSet, ignore parent.
81 if(moItemSet && moItemSet->GetParent())
83 moItemSet->SetParent(nullptr);
87 std::unique_ptr<BaseProperties> DefaultProperties::Clone(SdrObject& rObj) const
89 return std::unique_ptr<BaseProperties>(new DefaultProperties(*this, rObj));
92 DefaultProperties::~DefaultProperties() {}
94 const SfxItemSet& DefaultProperties::GetObjectItemSet() const
96 if(!moItemSet)
98 moItemSet.emplace(const_cast<DefaultProperties*>(this)->CreateObjectSpecificItemSet(GetSdrObject().GetObjectItemPool()));
99 const_cast<DefaultProperties*>(this)->ForceDefaultAttributes();
102 assert(moItemSet && "Could not create an SfxItemSet(!)");
104 return *moItemSet;
107 void DefaultProperties::SetObjectItem(const SfxPoolItem& rItem)
109 const sal_uInt16 nWhichID(rItem.Which());
111 if(!AllowItemChange(nWhichID, &rItem))
112 return;
114 ItemChange(nWhichID, &rItem);
115 PostItemChange(nWhichID);
117 const SfxPoolItem* pItem = &rItem;
118 ItemSetChanged( {&pItem, 1}, 0);
121 void DefaultProperties::SetObjectItemDirect(const SfxPoolItem& rItem)
123 const sal_uInt16 nWhichID(rItem.Which());
125 if(AllowItemChange(nWhichID, &rItem))
127 ItemChange(nWhichID, &rItem);
131 void DefaultProperties::ClearObjectItem(const sal_uInt16 nWhich)
133 if(!AllowItemChange(nWhich))
134 return;
136 ItemChange(nWhich);
137 PostItemChange(nWhich);
139 if(nWhich)
141 ItemSetChanged({}, nWhich);
145 void DefaultProperties::ClearObjectItemDirect(const sal_uInt16 nWhich)
147 if(AllowItemChange(nWhich))
149 ItemChange(nWhich);
153 void DefaultProperties::SetObjectItemSet(const SfxItemSet& rSet, bool bAdjustTextFrameWidthAndHeight)
155 if (rSet.HasItem(XATTR_FILLBITMAP))
157 const XFillBitmapItem* pItem = rSet.GetItem(XATTR_FILLBITMAP);
158 const std::shared_ptr<VectorGraphicData>& pVectorData
159 = pItem->GetGraphicObject().GetGraphic().getVectorGraphicData();
160 if (pVectorData)
162 // Shape is filled by a vector graphic: tell it our size as a hint.
163 basegfx::B2DTuple aSizeHint;
164 aSizeHint.setX(GetSdrObject().GetSnapRect().getOpenWidth());
165 aSizeHint.setY(GetSdrObject().GetSnapRect().getOpenHeight());
166 pVectorData->setSizeHint(aSizeHint);
170 SfxWhichIter aWhichIter(rSet);
171 sal_uInt16 nWhich(aWhichIter.FirstWhich());
172 std::vector< const SfxPoolItem * > aPostItemChangeList;
173 // give a hint to STL_Vector
174 aPostItemChangeList.reserve(rSet.Count());
176 while(nWhich)
178 const SfxPoolItem* pPoolItem;
179 if(SfxItemState::SET == aWhichIter.GetItemState(false, &pPoolItem))
181 if(AllowItemChange(nWhich, pPoolItem))
183 ItemChange(nWhich, pPoolItem);
184 aPostItemChangeList.emplace_back( pPoolItem );
188 nWhich = aWhichIter.NextWhich();
191 if(!aPostItemChangeList.empty())
193 for (const auto& rItem : aPostItemChangeList)
195 PostItemChange(rItem->Which());
198 ItemSetChanged(aPostItemChangeList, 0, bAdjustTextFrameWidthAndHeight);
202 void DefaultProperties::ItemSetChanged(std::span< const SfxPoolItem* const > /*aChangedItems*/, sal_uInt16 /*nDeletedWhich*/, bool /*bAdjustTextFrameWidthAndHeight*/)
206 bool DefaultProperties::AllowItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/) const
208 return true;
211 void DefaultProperties::ItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/)
215 void DefaultProperties::PostItemChange(const sal_uInt16 nWhich )
217 if( (nWhich == XATTR_FILLSTYLE) && moItemSet )
218 CleanupFillProperties(*moItemSet);
221 void DefaultProperties::SetStyleSheet(SfxStyleSheet* /*pNewStyleSheet*/, bool /*bDontRemoveHardAttr*/,
222 bool /*bBroadcast*/, bool /*bAdjustTextFrameWidthAndHeight*/)
224 // no StyleSheet in DefaultProperties
227 SfxStyleSheet* DefaultProperties::GetStyleSheet() const
229 // no StyleSheet in DefaultProperties
230 return nullptr;
233 void DefaultProperties::ForceDefaultAttributes()
237 void DefaultProperties::dumpAsXml(xmlTextWriterPtr pWriter) const
239 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("DefaultProperties"));
240 BaseProperties::dumpAsXml(pWriter);
241 if (moItemSet)
243 moItemSet->dumpAsXml(pWriter);
245 (void)xmlTextWriterEndElement(pWriter);
247 } // end of namespace
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */