1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
)
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(
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());
69 const MapUnit
aOldUnit(rProps
.GetSdrObject().getSdrModelFromSdrObject().GetScaleUnit());
70 const MapUnit
aNewUnit(rObj
.getSdrModelFromSdrObject().GetScaleUnit());
71 const bool bScaleUnitChanged(aNewUnit
!= aOldUnit
);
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
100 mxItemSet
.emplace(const_cast<DefaultProperties
*>(this)->CreateObjectSpecificItemSet(GetSdrObject().GetObjectItemPool()));
101 const_cast<DefaultProperties
*>(this)->ForceDefaultAttributes();
104 assert(mxItemSet
&& "Could not create an SfxItemSet(!)");
109 void DefaultProperties::SetObjectItem(const SfxPoolItem
& rItem
)
111 const sal_uInt16
nWhichID(rItem
.Which());
113 if(AllowItemChange(nWhichID
, &rItem
))
115 ItemChange(nWhichID
, &rItem
);
116 PostItemChange(nWhichID
);
118 SfxItemSet
aSet(GetSdrObject().GetObjectItemPool(), {{nWhichID
, nWhichID
}});
120 ItemSetChanged(aSet
);
124 void DefaultProperties::SetObjectItemDirect(const SfxPoolItem
& rItem
)
126 const sal_uInt16
nWhichID(rItem
.Which());
128 if(AllowItemChange(nWhichID
, &rItem
))
130 ItemChange(nWhichID
, &rItem
);
134 void DefaultProperties::ClearObjectItem(const sal_uInt16 nWhich
)
136 if(AllowItemChange(nWhich
))
139 PostItemChange(nWhich
);
143 SfxItemSet
aSet(GetSdrObject().GetObjectItemPool(), {{nWhich
, nWhich
}});
144 ItemSetChanged(aSet
);
149 void DefaultProperties::ClearObjectItemDirect(const sal_uInt16 nWhich
)
151 if(AllowItemChange(nWhich
))
157 void DefaultProperties::SetObjectItemSet(const SfxItemSet
& rSet
)
159 if (rSet
.HasItem(XATTR_FILLBITMAP
))
161 const XFillBitmapItem
* pItem
= rSet
.GetItem(XATTR_FILLBITMAP
);
162 const std::shared_ptr
<VectorGraphicData
>& pVectorData
163 = pItem
->GetGraphicObject().GetGraphic().getVectorGraphicData();
166 // Shape is filled by a vector graphic: tell it our size as a hint.
167 basegfx::B2DTuple aSizeHint
;
168 aSizeHint
.setX(GetSdrObject().GetSnapRect().getWidth());
169 aSizeHint
.setY(GetSdrObject().GetSnapRect().getHeight());
170 pVectorData
->setSizeHint(aSizeHint
);
174 SfxWhichIter
aWhichIter(rSet
);
175 sal_uInt16
nWhich(aWhichIter
.FirstWhich());
176 const SfxPoolItem
*pPoolItem
;
177 std::vector
< sal_uInt16
> aPostItemChangeList
;
178 bool bDidChange(false);
179 SfxItemSet
aSet(GetSdrObject().GetObjectItemPool(), svl::Items
<SDRATTR_START
, EE_ITEMS_END
>{});
181 // give a hint to STL_Vector
182 aPostItemChangeList
.reserve(rSet
.Count());
186 if(SfxItemState::SET
== rSet
.GetItemState(nWhich
, false, &pPoolItem
))
188 if(AllowItemChange(nWhich
, pPoolItem
))
191 ItemChange(nWhich
, pPoolItem
);
192 aPostItemChangeList
.push_back( nWhich
);
193 aSet
.Put(*pPoolItem
);
197 nWhich
= aWhichIter
.NextWhich();
202 for (const auto& rItem
: aPostItemChangeList
)
204 PostItemChange(rItem
);
207 ItemSetChanged(aSet
);
211 void DefaultProperties::ItemSetChanged(const SfxItemSet
& /*rSet*/)
215 bool DefaultProperties::AllowItemChange(const sal_uInt16
/*nWhich*/, const SfxPoolItem
* /*pNewItem*/) const
220 void DefaultProperties::ItemChange(const sal_uInt16
/*nWhich*/, const SfxPoolItem
* /*pNewItem*/)
224 void DefaultProperties::PostItemChange(const sal_uInt16 nWhich
)
226 if( (nWhich
== XATTR_FILLSTYLE
) && mxItemSet
)
227 CleanupFillProperties(*mxItemSet
);
230 void DefaultProperties::SetStyleSheet(SfxStyleSheet
* /*pNewStyleSheet*/, bool /*bDontRemoveHardAttr*/)
232 // no StyleSheet in DefaultProperties
235 SfxStyleSheet
* DefaultProperties::GetStyleSheet() const
237 // no StyleSheet in DefaultProperties
241 void DefaultProperties::ForceDefaultAttributes()
245 void DefaultProperties::dumpAsXml(xmlTextWriterPtr pWriter
) const
247 (void)xmlTextWriterStartElement(pWriter
, BAD_CAST("DefaultProperties"));
248 BaseProperties::dumpAsXml(pWriter
);
251 mxItemSet
->dumpAsXml(pWriter
);
253 (void)xmlTextWriterEndElement(pWriter
);
255 } // end of namespace
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */