LibreOffice Module svx (master)  1
defaultproperties.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
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 
34 namespace sdr::properties
35 {
36  std::unique_ptr<SfxItemSet> DefaultProperties::CreateObjectSpecificItemSet(SfxItemPool& rPool)
37  {
38  // Basic implementation; Basic object has NO attributes
39  return std::make_unique<SfxItemSet>(rPool);
40  }
41 
43  : BaseProperties(rObj)
44  {
45  }
46 
48  : BaseProperties(rObj)
49  {
50  if(rProps.mpItemSet)
51  {
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  mpItemSet = rProps.mpItemSet->Clone(
56  true,
58 
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());
64 
65  if(bModelChange)
66  {
67  const MapUnit aOldUnit(rProps.GetSdrObject().getSdrModelFromSdrObject().GetScaleUnit());
68  const MapUnit aNewUnit(rObj.getSdrModelFromSdrObject().GetScaleUnit());
69  const bool bScaleUnitChanged(aNewUnit != aOldUnit);
70 
71  if(bScaleUnitChanged)
72  {
73  const Fraction aMetricFactor(GetMapFactor(aOldUnit, aNewUnit).X());
74 
75  ScaleItemSet(*mpItemSet, aMetricFactor);
76  }
77  }
78 
79  // do not keep parent info, this may be changed by later constructors.
80  // This class just copies the ItemSet, ignore parent.
81  if(mpItemSet && mpItemSet->GetParent())
82  {
83  mpItemSet->SetParent(nullptr);
84  }
85  }
86  }
87 
88  std::unique_ptr<BaseProperties> DefaultProperties::Clone(SdrObject& rObj) const
89  {
90  return std::unique_ptr<BaseProperties>(new DefaultProperties(*this, rObj));
91  }
92 
94 
96  {
97  if(!mpItemSet)
98  {
99  const_cast<DefaultProperties*>(this)->mpItemSet = const_cast<DefaultProperties*>(this)->CreateObjectSpecificItemSet(GetSdrObject().GetObjectItemPool());
100  const_cast<DefaultProperties*>(this)->ForceDefaultAttributes();
101  }
102 
103  assert(mpItemSet && "Could not create an SfxItemSet(!)");
104 
105  return *mpItemSet;
106  }
107 
109  {
110  const sal_uInt16 nWhichID(rItem.Which());
111 
112  if(AllowItemChange(nWhichID, &rItem))
113  {
114  ItemChange(nWhichID, &rItem);
115  PostItemChange(nWhichID);
116 
117  SfxItemSet aSet(GetSdrObject().GetObjectItemPool(), {{nWhichID, nWhichID}});
118  aSet.Put(rItem);
119  ItemSetChanged(aSet);
120  }
121  }
122 
124  {
125  const sal_uInt16 nWhichID(rItem.Which());
126 
127  if(AllowItemChange(nWhichID, &rItem))
128  {
129  ItemChange(nWhichID, &rItem);
130  }
131  }
132 
133  void DefaultProperties::ClearObjectItem(const sal_uInt16 nWhich)
134  {
135  if(AllowItemChange(nWhich))
136  {
137  ItemChange(nWhich);
138  PostItemChange(nWhich);
139 
140  if(nWhich)
141  {
142  SfxItemSet aSet(GetSdrObject().GetObjectItemPool(), {{nWhich, nWhich}});
143  ItemSetChanged(aSet);
144  }
145  }
146  }
147 
148  void DefaultProperties::ClearObjectItemDirect(const sal_uInt16 nWhich)
149  {
150  if(AllowItemChange(nWhich))
151  {
152  ItemChange(nWhich);
153  }
154  }
155 
157  {
158  SfxWhichIter aWhichIter(rSet);
159  sal_uInt16 nWhich(aWhichIter.FirstWhich());
160  const SfxPoolItem *pPoolItem;
161  std::vector< sal_uInt16 > aPostItemChangeList;
162  bool bDidChange(false);
163  SfxItemSet aSet(GetSdrObject().GetObjectItemPool(), svl::Items<SDRATTR_START, EE_ITEMS_END>{});
164 
165  // give a hint to STL_Vector
166  aPostItemChangeList.reserve(rSet.Count());
167 
168  while(nWhich)
169  {
170  if(SfxItemState::SET == rSet.GetItemState(nWhich, false, &pPoolItem))
171  {
172  if(AllowItemChange(nWhich, pPoolItem))
173  {
174  bDidChange = true;
175  ItemChange(nWhich, pPoolItem);
176  aPostItemChangeList.push_back( nWhich );
177  aSet.Put(*pPoolItem);
178  }
179  }
180 
181  nWhich = aWhichIter.NextWhich();
182  }
183 
184  if(bDidChange)
185  {
186  for (const auto& rItem : aPostItemChangeList)
187  {
188  PostItemChange(rItem);
189  }
190 
191  ItemSetChanged(aSet);
192  }
193  }
194 
196  {
197  }
198 
199  bool DefaultProperties::AllowItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/) const
200  {
201  return true;
202  }
203 
204  void DefaultProperties::ItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/)
205  {
206  }
207 
208  void DefaultProperties::PostItemChange(const sal_uInt16 nWhich )
209  {
210  if( (nWhich == XATTR_FILLSTYLE) && (mpItemSet != nullptr) )
212  }
213 
214  void DefaultProperties::SetStyleSheet(SfxStyleSheet* /*pNewStyleSheet*/, bool /*bDontRemoveHardAttr*/)
215  {
216  // no StyleSheet in DefaultProperties
217  }
218 
220  {
221  // no StyleSheet in DefaultProperties
222  return nullptr;
223  }
224 
226  {
227  }
228 
230  {
231  xmlTextWriterStartElement(pWriter, BAD_CAST("DefaultProperties"));
232  BaseProperties::dumpAsXml(pWriter);
233  mpItemSet->dumpAsXml(pWriter);
234  xmlTextWriterEndElement(pWriter);
235  }
236 } // end of namespace
237 
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _xmlTextWriter * xmlTextWriterPtr
virtual void SetObjectItemSet(const SfxItemSet &rSet) override
virtual void SetStyleSheet(SfxStyleSheet *pNewStyleSheet, bool bDontRemoveHardAttr) override
virtual const SfxItemSet & GetObjectItemSet() const override
virtual void PostItemChange(const sal_uInt16 nWhich) override
sal_uInt16 FirstWhich()
virtual void SetObjectItemDirect(const SfxPoolItem &rItem) override
std::unique_ptr< SfxItemSet > mpItemSet
sal_uInt16 NextWhich()
virtual SfxStyleSheet * GetStyleSheet() const override
virtual void SetObjectItem(const SfxPoolItem &rItem) override
MapUnit GetScaleUnit() const
Definition: svdmodel.hxx:367
virtual void ItemSetChanged(const SfxItemSet &rSet) override
#define X
virtual void ItemChange(const sal_uInt16 nWhich, const SfxPoolItem *pNewItem=nullptr) override
void dumpAsXml(xmlTextWriterPtr pWriter) const override
void ScaleItemSet(SfxItemSet &rSet, const Fraction &rScale)
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
void CleanupFillProperties(SfxItemSet &rItemSet)
Definition: properties.cxx:162
sal_uInt16 Count() const
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: properties.cxx:156
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:272
#define XATTR_FILLSTYLE
Definition: xdef.hxx:107
Abstract DrawObject.
Definition: svdobj.hxx:312
virtual bool AllowItemChange(const sal_uInt16 nWhich, const SfxPoolItem *pNewItem=nullptr) const override
FrPair GetMapFactor(MapUnit eS, MapUnit eD)
Definition: svdtrans.cxx:632
virtual std::unique_ptr< SfxItemSet > CreateObjectSpecificItemSet(SfxItemPool &rPool) override
virtual void ClearObjectItemDirect(const sal_uInt16 nWhich) override
MapUnit
const SfxItemPool & GetItemPool() const
Definition: svdmodel.hxx:314
const SdrObject & GetSdrObject() const
Definition: properties.cxx:59
virtual std::unique_ptr< BaseProperties > Clone(SdrObject &rObj) const override
sal_uInt16 Which() const
virtual void ClearObjectItem(const sal_uInt16 nWhich=0) override