LibreOffice Module svx (master)  1
svdlayer.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 <com/sun/star/uno/Sequence.hxx>
21 
22 #include <svx/svdlayer.hxx>
23 #include <svx/svdmodel.hxx>
24 #include <svx/dialmgr.hxx>
25 #include <svx/strings.hrc>
26 
28 {
29  for(sal_uInt8 i : aData)
30  {
31  if(i != 0)
32  return false;
33  }
34 
35  return true;
36 }
37 
39 {
40  for(sal_uInt16 i(0); i < 32; i++)
41  {
42  aData[i] &= r2ndSet.aData[i];
43  }
44 }
45 
48 void SdrLayerIDSet::PutValue( const css::uno::Any & rAny )
49 {
50  css::uno::Sequence< sal_Int8 > aSeq;
51  if( rAny >>= aSeq )
52  {
53  sal_Int16 nCount = static_cast<sal_Int16>(aSeq.getLength());
54  if( nCount > 32 )
55  nCount = 32;
56 
57  sal_Int16 nIndex;
58  for( nIndex = 0; nIndex < nCount; nIndex++ )
59  {
60  aData[nIndex] = static_cast<sal_uInt8>(aSeq[nIndex]);
61  }
62 
63  for( ; nIndex < 32; nIndex++ )
64  {
65  aData[nIndex] = 0;
66  }
67  }
68 }
69 
70 SdrLayer::SdrLayer(SdrLayerID nNewID, const OUString& rNewName) :
71  maName(rNewName), pModel(nullptr), nID(nNewID)
72 {
73  // ODF default values
74  mbVisibleODF = true;
75  mbPrintableODF = true;
76  mbLockedODF = false;
77 }
78 
79 void SdrLayer::SetName(const OUString& rNewName)
80 {
81  if (rNewName == maName)
82  return;
83 
84  maName = rNewName;
85 
86  if (pModel)
87  {
89  pModel->Broadcast(aHint);
90  pModel->SetChanged();
91  }
92 }
93 
94 bool SdrLayer::operator==(const SdrLayer& rCmpLayer) const
95 {
96  return (nID == rCmpLayer.nID
97  && maName == rCmpLayer.maName);
98 }
99 
101  pParent(pNewParent),
102  pModel(nullptr),
103  maControlLayerName("controls")
104 {
105 }
106 
108  pParent(nullptr),
109  pModel(nullptr),
110  maControlLayerName("controls")
111 {
112  *this = rSrcLayerAdmin;
113 }
114 
116 {
117 }
118 
120 {
121  maLayers.clear();
122 }
123 
125 {
126  if (this != &rSrcLayerAdmin)
127  {
128  maLayers.clear();
129  pParent=rSrcLayerAdmin.pParent;
130  sal_uInt16 i;
131  sal_uInt16 nCount=rSrcLayerAdmin.GetLayerCount();
132  for (i=0; i<nCount; i++) {
133  maLayers.emplace_back(new SdrLayer(*rSrcLayerAdmin.GetLayer(i)));
134  }
135  }
136  return *this;
137 }
138 
140 {
141  if (pNewModelel!=pModel) {
142  pModel=pNewModelel;
143  sal_uInt16 nCount=GetLayerCount();
144  sal_uInt16 i;
145  for (i=0; i<nCount; i++) {
146  GetLayer(i)->SetModel(pNewModelel);
147  }
148  }
149 }
150 
152 {
153  if (pModel!=nullptr) {
155  pModel->Broadcast(aHint);
156  pModel->SetChanged();
157  }
158 }
159 
160 void SdrLayerAdmin::InsertLayer(std::unique_ptr<SdrLayer> pLayer, sal_uInt16 nPos)
161 {
162  pLayer->SetModel(pModel);
163  if(nPos==0xFFFF)
164  maLayers.push_back(std::move(pLayer));
165  else
166  maLayers.insert(maLayers.begin() + nPos, std::move(pLayer));
167  Broadcast();
168 }
169 
170 std::unique_ptr<SdrLayer> SdrLayerAdmin::RemoveLayer(sal_uInt16 nPos)
171 {
172  std::unique_ptr<SdrLayer> pRetLayer = std::move(maLayers[nPos]);
173  maLayers.erase(maLayers.begin()+nPos);
174  Broadcast();
175  return pRetLayer;
176 }
177 
178 SdrLayer* SdrLayerAdmin::NewLayer(const OUString& rName, sal_uInt16 nPos)
179 {
181  SdrLayer* pLay=new SdrLayer(nID,rName);
182  pLay->SetModel(pModel);
183  if(nPos==0xFFFF)
184  maLayers.push_back(std::unique_ptr<SdrLayer>(pLay));
185  else
186  maLayers.insert(maLayers.begin() + nPos, std::unique_ptr<SdrLayer>(pLay));
187  Broadcast();
188  return pLay;
189 }
190 
191 sal_uInt16 SdrLayerAdmin::GetLayerPos(const SdrLayer* pLayer) const
192 {
193  sal_uInt16 nRet=SDRLAYERPOS_NOTFOUND;
194  if (pLayer!=nullptr) {
195  auto it = std::find_if(maLayers.begin(), maLayers.end(),
196  [&](const std::unique_ptr<SdrLayer> & p) { return p.get() == pLayer; });
197  if (it!=maLayers.end()) {
198  nRet=it - maLayers.begin();
199  }
200  }
201  return nRet;
202 }
203 
204 SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName)
205 {
206  return const_cast<SdrLayer*>(const_cast<const SdrLayerAdmin*>(this)->GetLayer(rName));
207 }
208 
209 const SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName) const
210 {
211  sal_uInt16 i(0);
212  const SdrLayer* pLay = nullptr;
213 
214  while(i < GetLayerCount() && !pLay)
215  {
216  if (rName == GetLayer(i)->GetName())
217  pLay = GetLayer(i);
218  else
219  i++;
220  }
221 
222  if(!pLay && pParent)
223  {
224  pLay = pParent->GetLayer(rName);
225  }
226 
227  return pLay;
228 }
229 
230 SdrLayerID SdrLayerAdmin::GetLayerID(const OUString& rName) const
231 {
233  const SdrLayer* pLay=GetLayer(rName);
234  if (pLay!=nullptr) nRet=pLay->GetID();
235  return nRet;
236 }
237 
239 {
240  for (auto const & pLayer : maLayers)
241  if (pLayer->GetID() == nID)
242  return pLayer.get();
243  return nullptr;
244 }
245 
246 // Global LayerIDs begin at 0 and increase,
247 // local LayerIDs begin at 254 and decrease;
248 // 255 is reserved for SDRLAYER_NOTFOUND.
249 
251 {
252  SdrLayerIDSet aSet;
253  for (sal_uInt16 j=0; j<GetLayerCount(); j++)
254  {
255  aSet.Set(GetLayer(j)->GetID());
256  }
257  sal_uInt8 i;
258  if (pParent != nullptr)
259  {
260  i = 254;
261  while (i && aSet.IsSet(SdrLayerID(i)))
262  --i;
263  assert(i != 0);
264  if (i == 0)
265  i = 254;
266  }
267  else
268  {
269  i = 0;
270  while (i<=254 && aSet.IsSet(SdrLayerID(i)))
271  i++;
272  assert(i <= 254);
273  if (i>254)
274  i = 0;
275  }
276  return SdrLayerID(i);
277 }
278 
279 void SdrLayerAdmin::SetControlLayerName(const OUString& rNewName)
280 {
281  maControlLayerName = rNewName;
282 }
283 
285 {
286  rOutSet.ClearAll();
287  for( auto & pCurrentLayer : maLayers )
288  {
289  if ( pCurrentLayer->IsVisibleODF() )
290  rOutSet.Set( pCurrentLayer->GetID() );
291  }
292 }
293 
295 {
296  rOutSet.ClearAll();
297  for( auto & pCurrentLayer : maLayers )
298  {
299  if ( pCurrentLayer->IsPrintableODF() )
300  rOutSet.Set( pCurrentLayer->GetID() );
301  }
302 }
303 
305 {
306  rOutSet.ClearAll();
307  for( auto& pCurrentLayer : maLayers )
308  {
309  if ( pCurrentLayer->IsLockedODF() )
310  rOutSet.Set( pCurrentLayer->GetID() );
311  }
312 }
313 
314  // Generates a bitfield for settings.xml from the SdrLayerIDSet.
315  // Output is a UNO sequence of BYTE (which is 'short' in API).
316 void SdrLayerAdmin::QueryValue(const SdrLayerIDSet& rViewLayerSet, css::uno::Any& rAny)
317 {
318  // tdf#119392 The SdrLayerIDSet in a view is ordered according LayerID, but in file
319  // the bitfield is interpreted in order of layers in <draw:layer-set>.
320  // First generate a new bitfield based on rViewLayerSet in the needed order.
321  sal_uInt8 aTmp[32]; // 256 bits in settings.xml makes byte 0 to 31
322  for (auto nIndex = 0; nIndex <32; nIndex++)
323  {
324  aTmp[nIndex] = 0;
325  }
326  sal_uInt8 nByteIndex = 0;
327  sal_uInt8 nBitpos = 0;
328  sal_uInt16 nLayerPos = 0; // Position of the layer in member aLayer and in <draw:layer-set> in file
329  sal_uInt16 nLayerIndex = 0;
330  for( const auto& pCurrentLayer : maLayers )
331  {
332  SdrLayerID nCurrentID = pCurrentLayer->GetID();
333  if ( rViewLayerSet.IsSet(nCurrentID) )
334  {
335  nLayerPos = nLayerIndex;
336  nByteIndex = nLayerPos / 8;
337  if (nByteIndex > 31)
338  continue; // skip position, if too large for bitfield
339  nBitpos = nLayerPos % 8;
340  aTmp[nByteIndex] |= (1 << nBitpos);
341  }
342  ++nLayerIndex;
343  }
344 
345  // Second transform the bitfield to byte sequence, same as in previous version of QueryValue
346  sal_uInt8 nNumBytesSet = 0;
347  for( auto nIndex = 31; nIndex >= 0; nIndex--)
348  {
349  if( 0 != aTmp[nIndex] )
350  {
351  nNumBytesSet = nIndex + 1;
352  break;
353  }
354  }
355  css::uno::Sequence< sal_Int8 > aSeq( nNumBytesSet );
356  for( auto nIndex = 0; nIndex < nNumBytesSet; nIndex++ )
357  {
358  aSeq[nIndex] = static_cast<sal_Int8>(aTmp[nIndex]);
359  }
360  rAny <<= aSeq;
361 }
362 
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void operator&=(const SdrLayerIDSet &r2ndSet)
Definition: svdlayer.cxx:38
void PutValue(const css::uno::Any &rAny)
initialize this set with a UNO sequence of sal_Int8 (e.g.
Definition: svdlayer.cxx:48
sal_Int32 nIndex
void Set(SdrLayerID a)
Definition: svdsob.hxx:48
void getVisibleLayersODF(SdrLayerIDSet &rOutSet) const
Definition: svdlayer.cxx:284
signed char sal_Int8
SdrModel * pModel
Definition: svdlayer.hxx:68
SdrLayerAdmin(SdrLayerAdmin *pNewParent=nullptr)
Definition: svdlayer.cxx:100
bool IsEmpty() const
Definition: svdlayer.cxx:27
#define SDRLAYERPOS_NOTFOUND
Definition: svdlayer.hxx:99
bool mbPrintableODF
Definition: svdlayer.hxx:66
void Broadcast() const
Definition: svdlayer.cxx:151
SdrModel * pModel
Definition: svdlayer.hxx:110
sal_uInt16 GetLayerPos(const SdrLayer *pLayer) const
Definition: svdlayer.cxx:191
std::unique_ptr< SdrLayer > RemoveLayer(sal_uInt16 nPos)
Definition: svdlayer.cxx:170
OUString maName
Definition: svdlayer.hxx:62
SdrLayerID GetID() const
Definition: svdlayer.hxx:94
void SetModel(SdrModel *pNewModel)
Definition: svdlayer.hxx:95
void getLockedLayersODF(SdrLayerIDSet &rOutSet) const
Definition: svdlayer.cxx:304
SdrLayerAdmin & operator=(const SdrLayerAdmin &rSrcLayerAdmin)
Definition: svdlayer.cxx:124
int nCount
SdrLayerID GetUniqueLayerID() const
Definition: svdlayer.cxx:250
void SetName(const OUString &rNewName)
Definition: svdlayer.cxx:79
constexpr SdrLayerID SDRLAYER_NOTFOUND(0xff)
virtual void SetChanged(bool bFlg=true)
Definition: svdmodel.cxx:1274
o3tl::strong_int< sal_uInt8, struct SdrLayerIDTag > SdrLayerID
Definition: svdtypes.hxx:59
SdrLayer * GetLayer(sal_uInt16 i)
Definition: svdlayer.hxx:139
sal_uInt16 GetLayerCount() const
Definition: svdlayer.hxx:137
bool operator==(const SdrLayer &rCmpLayer) const
Definition: svdlayer.cxx:94
sal_uInt8 aData[32]
Definition: svdsob.hxx:35
int i
SdrLayer * NewLayer(const OUString &rName, sal_uInt16 nPos=0xFFFF)
Definition: svdlayer.cxx:178
SdrLayer * GetLayerPerID(SdrLayerID nID)
Definition: svdlayer.hxx:147
SdrLayerAdmin * pParent
Definition: svdlayer.hxx:109
void ClearLayers()
Definition: svdlayer.cxx:119
bool IsSet(SdrLayerID a) const
Definition: svdsob.hxx:66
void QueryValue(const SdrLayerIDSet &rViewLayerSet, css::uno::Any &rAny)
Definition: svdlayer.cxx:316
void InsertLayer(std::unique_ptr< SdrLayer > pLayer, sal_uInt16 nPos)
Definition: svdlayer.cxx:160
void getPrintableLayersODF(SdrLayerIDSet &rOutSet) const
Definition: svdlayer.cxx:294
void Broadcast(const SfxHint &rHint)
bool mbLockedODF
Definition: svdlayer.hxx:67
bool mbVisibleODF
Definition: svdlayer.hxx:65
unsigned char sal_uInt8
OUString maName
OUString maControlLayerName
Definition: svdlayer.hxx:111
std::vector< std::unique_ptr< SdrLayer > > maLayers
Definition: svdlayer.hxx:108
void * p
Sequence< sal_Int8 > aSeq
SdrLayerID const nID
Definition: svdlayer.hxx:69
void SetControlLayerName(const OUString &rNewName)
Definition: svdlayer.cxx:279
void ClearAll()
Definition: svdsob.hxx:76
void SetModel(SdrModel *pNewModel)
Definition: svdlayer.cxx:139
SdrLayer(SdrLayerID nNewID, const OUString &rNewName)
Definition: svdlayer.cxx:70
SdrLayerID GetLayerID(const OUString &rName) const
Definition: svdlayer.cxx:230