-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayermanagerimpl.cpp
More file actions
151 lines (131 loc) · 5.45 KB
/
layermanagerimpl.cpp
File metadata and controls
151 lines (131 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-FileCopyrightText: 2017 Christian Sailer
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "layermanagerimpl.hpp"
#include "genlib/stringutils.hpp"
LayerManagerImpl::LayerManagerImpl() : m_visibleLayers(1), m_layers(), m_layerLookup() {
m_layers.push_back("Everything");
m_layerLookup["Everything"] = 0;
}
size_t LayerManagerImpl::addLayer(const std::string &layerName) {
size_t newLayerIndex = m_layers.size();
if (newLayerIndex > 63) {
throw OutOfLayersException();
}
auto result = m_layerLookup.insert(std::make_pair(layerName, newLayerIndex));
if (!result.second) {
throw DuplicateKeyException(std::string("Trying to insert duplicate key: ") + layerName);
}
m_layers.push_back(layerName);
return newLayerIndex;
}
const std::string &LayerManagerImpl::getLayerName(size_t index) const {
checkIndex(index);
return m_layers[index];
}
size_t LayerManagerImpl::getLayerIndex(const std::string &layerName) const {
auto iter = m_layerLookup.find(layerName);
if (iter == m_layerLookup.end()) {
throw std::out_of_range("Unknown layer name");
}
return iter->second;
}
void LayerManagerImpl::setLayerVisible(size_t layerIndex, bool visible) {
checkIndex(layerIndex);
if (layerIndex == 0) {
// this it the everything layer - if switching on just show everything, else
// switch everything off
m_visibleLayers = visible ? 1 : 0;
return;
}
int64_t layerValue = (static_cast<KeyType>(1)) << layerIndex;
// if visible, switch on this layer and switch everything layer off, else just
// switch off this layer
if (visible) {
m_visibleLayers = (m_visibleLayers | layerValue) & (~0x1);
} else {
m_visibleLayers &= (~layerValue);
}
}
bool LayerManagerImpl::isLayerVisible(size_t layerIndex) const {
checkIndex(layerIndex);
return isVisible(getKey(layerIndex));
}
bool LayerManagerImpl::isVisible(const KeyType &key) const { return (m_visibleLayers & key) != 0; }
void LayerManagerImpl::read(std::istream &stream) {
m_layerLookup.clear();
m_layers.clear();
KeyType dummy;
stream.read(reinterpret_cast<char *>(&dummy), sizeof(dummy));
stream.read(reinterpret_cast<char *>(&m_visibleLayers), sizeof(m_visibleLayers));
int count;
stream.read(reinterpret_cast<char *>(&count), sizeof(int));
for (size_t i = 0; i < static_cast<size_t>(count); ++i) {
stream.read(reinterpret_cast<char *>(&dummy), sizeof(dummy));
m_layers.push_back(dXstring::readString(stream));
m_layerLookup[m_layers.back()] = i;
}
}
void LayerManagerImpl::write(std::ostream &stream) const {
// KeyType availableLayers = 0;
// for (size_t i = m_layers.size(); i < 64; ++i)
// {
// availableLayers |= ((KeyType)1) << i;
// }
// TODO: (PK) While the above seems to me like the sane solution and
// potentially what the intention was in the original implementation, the one
// in the old attributes table seems to be messed up because of its starting
// value. Therefore, for temporary binary compatibility at least until the new
// attributes table is in place the original solution is used here as found
// in AttributeTable::selectionToLayer():
int64_t availableLayers = 0xffffffff << (32 + 0xfffffffe);
// should have been:
// int64_t availableLayers = (int64_t(0xffffffff) << 32) + 0xfffffffe;
for (size_t i = 1; (i < 64) & (i < m_layers.size()); ++i) {
int loc = 1;
while (loc < 64 && ((availableLayers >> loc) & 0x1) == 0) {
loc++;
}
if (loc == 64) {
// too many layers -- maximum 64
throw OutOfLayersException();
}
int64_t newlayer = static_cast<int64_t>(0x1) << static_cast<int64_t>(loc);
// now layer has been found, eliminate from available layers
// and add a lookup for the name
availableLayers = (availableLayers & (~newlayer));
}
stream.write(reinterpret_cast<const char *>(&availableLayers), sizeof(KeyType));
stream.write(reinterpret_cast<const char *>(&m_visibleLayers), sizeof(KeyType));
auto sizeAsInt = static_cast<int>(m_layers.size());
stream.write(reinterpret_cast<const char *>(&sizeAsInt), sizeof(int));
availableLayers = 0xffffffff << (32 + 0xfffffffe);
int64_t newlayer = 0x1;
stream.write(reinterpret_cast<const char *>(&newlayer), sizeof(KeyType));
dXstring::writeString(stream, m_layers[0]);
for (size_t i = 1; i < m_layers.size(); ++i) {
// again keeping binary comatibility
// KeyType key = ((KeyType)1) << i;
// stream.write((const char *)&key, sizeof(KeyType));
// dXstring::writeString(stream,m_layers[i]);
int loc = 1;
while (loc < 64 && ((availableLayers >> loc) & 0x1) == 0) {
loc++;
}
if (loc == 64) {
// too many layers -- maximum 64
throw OutOfLayersException();
}
newlayer = static_cast<int64_t>(0x1) << static_cast<int64_t>(loc);
stream.write(reinterpret_cast<const char *>(&newlayer), sizeof(KeyType));
dXstring::writeString(stream, m_layers[i]);
}
}
LayerManager::KeyType LayerManagerImpl::getKey(size_t layerIndex) const {
return (static_cast<int64_t>(1)) << layerIndex;
}
void LayerManagerImpl::checkIndex(size_t index) const {
if (index >= m_layers.size()) {
throw std::out_of_range("Invalid layer index");
}
}