Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / sdr / properties / defaultproperties.cxx
blob2db2e018ed7752a1ca8e0c97707c580dfc690a9b
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 <svx/svddef.hxx>
29 #include <editeng/eeitem.hxx>
30 #include <libxml/xmlwriter.h>
31 #include <svx/svdmodel.hxx>
32 #include <svx/svdtrans.hxx>
33 #include <svx/xbtmpit.hxx>
35 namespace sdr::properties
37 SfxItemSet DefaultProperties::CreateObjectSpecificItemSet(SfxItemPool& rPool)
39 // Basic implementation; Basic object has NO attributes
40 return SfxItemSet(rPool);
43 DefaultProperties::DefaultProperties(SdrObject& rObj)
44 : BaseProperties(rObj)
48 DefaultProperties::DefaultProperties(const DefaultProperties& rProps, SdrObject& rObj)
49 : BaseProperties(rObj)
51 if(!rProps.mxItemSet)
52 return;
54 // Clone may be to another model and thus another ItemPool.
55 // SfxItemSet supports that thus we are able to Clone all
56 // SfxItemState::SET items to the target pool.
57 mxItemSet.emplace(rProps.mxItemSet->CloneAsValue(
58 true,
59 &rObj.getSdrModelFromSdrObject().GetItemPool()));
61 // React on ModelChange: If metric has changed, scale items.
62 // As seen above, clone is supported, but scale is not included,
63 // thus: TTTT maybe add scale to SfxItemSet::Clone() (?)
64 // tdf#117707 correct ModelChange detection
65 const bool bModelChange(&rObj.getSdrModelFromSdrObject() != &rProps.GetSdrObject().getSdrModelFromSdrObject());
67 if(bModelChange)
69 const MapUnit aOldUnit(rProps.GetSdrObject().getSdrModelFromSdrObject().GetScaleUnit());
70 const MapUnit aNewUnit(rObj.getSdrModelFromSdrObject().GetScaleUnit());
71 const bool bScaleUnitChanged(aNewUnit != aOldUnit);
73 if(bScaleUnitChanged)
75 const Fraction aMetricFactor(GetMapFactor(aOldUnit, aNewUnit).X());
77 ScaleItemSet(*mxItemSet, aMetricFactor);
81 // do not keep parent info, this may be changed by later constructors.
82 // This class just copies the ItemSet, ignore parent.
83 if(mxItemSet && mxItemSet->GetParent())
85 mxItemSet->SetParent(nullptr);
89 std::unique_ptr<BaseProperties> DefaultProperties::Clone(SdrObject& rObj) const
91 return std::unique_ptr<BaseProperties>(new DefaultProperties(*this, rObj));
94 DefaultProperties::~DefaultProperties() {}
96 const SfxItemSet& DefaultProperties::GetObjectItemSet() const
98 if(!mxItemSet)
100 mxItemSet.emplace(const_cast<DefaultProperties*>(this)->CreateObjectSpecificItemSet(GetSdrObject().GetObjectItemPool()));
101 const_cast<DefaultProperties*>(this)->ForceDefaultAttributes();
104 assert(mxItemSet && "Could not create an SfxItemSet(!)");
106 return *mxItemSet;
109 void DefaultProperties::SetObjectItem(const SfxPoolItem& rItem)
111 const sal_uInt16 nWhichID(rItem.Which());
113 if(!AllowItemChange(nWhichID, &rItem))
114 return;
116 ItemChange(nWhichID, &rItem);
117 PostItemChange(nWhichID);
119 const SfxPoolItem* pItem = &rItem;
120 ItemSetChanged( {&pItem, 1}, 0);
123 void DefaultProperties::SetObjectItemDirect(const SfxPoolItem& rItem)
125 const sal_uInt16 nWhichID(rItem.Which());
127 if(AllowItemChange(nWhichID, &rItem))
129 ItemChange(nWhichID, &rItem);
133 void DefaultProperties::ClearObjectItem(const sal_uInt16 nWhich)
135 if(!AllowItemChange(nWhich))
136 return;
138 ItemChange(nWhich);
139 PostItemChange(nWhich);
141 if(nWhich)
143 ItemSetChanged({}, nWhich);
147 void DefaultProperties::ClearObjectItemDirect(const sal_uInt16 nWhich)
149 if(AllowItemChange(nWhich))
151 ItemChange(nWhich);
155 void DefaultProperties::SetObjectItemSet(const SfxItemSet& rSet)
157 if (rSet.HasItem(XATTR_FILLBITMAP))
159 const XFillBitmapItem* pItem = rSet.GetItem(XATTR_FILLBITMAP);
160 const std::shared_ptr<VectorGraphicData>& pVectorData
161 = pItem->GetGraphicObject().GetGraphic().getVectorGraphicData();
162 if (pVectorData)
164 // Shape is filled by a vector graphic: tell it our size as a hint.
165 basegfx::B2DTuple aSizeHint;
166 aSizeHint.setX(GetSdrObject().GetSnapRect().getOpenWidth());
167 aSizeHint.setY(GetSdrObject().GetSnapRect().getOpenHeight());
168 pVectorData->setSizeHint(aSizeHint);
172 SfxWhichIter aWhichIter(rSet);
173 sal_uInt16 nWhich(aWhichIter.FirstWhich());
174 std::vector< const SfxPoolItem * > aPostItemChangeList;
175 // give a hint to STL_Vector
176 aPostItemChangeList.reserve(rSet.Count());
178 while(nWhich)
180 const SfxPoolItem* pPoolItem;
181 if(SfxItemState::SET == aWhichIter.GetItemState(false, &pPoolItem))
183 if(AllowItemChange(nWhich, pPoolItem))
185 ItemChange(nWhich, pPoolItem);
186 aPostItemChangeList.emplace_back( pPoolItem );
190 nWhich = aWhichIter.NextWhich();
193 if(!aPostItemChangeList.empty())
195 for (const auto& rItem : aPostItemChangeList)
197 PostItemChange(rItem->Which());
200 ItemSetChanged(aPostItemChangeList, 0);
204 void DefaultProperties::ItemSetChanged(o3tl::span< const SfxPoolItem* const > /*aChangedItems*/, sal_uInt16 /*nDeletedWhich*/)
208 bool DefaultProperties::AllowItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/) const
210 return true;
213 void DefaultProperties::ItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/)
217 void DefaultProperties::PostItemChange(const sal_uInt16 nWhich )
219 if( (nWhich == XATTR_FILLSTYLE) && mxItemSet )
220 CleanupFillProperties(*mxItemSet);
223 void DefaultProperties::SetStyleSheet(SfxStyleSheet* /*pNewStyleSheet*/, bool /*bDontRemoveHardAttr*/,
224 bool /*bBroadcast*/)
226 // no StyleSheet in DefaultProperties
229 SfxStyleSheet* DefaultProperties::GetStyleSheet() const
231 // no StyleSheet in DefaultProperties
232 return nullptr;
235 void DefaultProperties::ForceDefaultAttributes()
239 void DefaultProperties::dumpAsXml(xmlTextWriterPtr pWriter) const
241 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("DefaultProperties"));
242 BaseProperties::dumpAsXml(pWriter);
243 if (mxItemSet)
245 mxItemSet->dumpAsXml(pWriter);
247 (void)xmlTextWriterEndElement(pWriter);
249 } // end of namespace
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */