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