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