-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathMappingManager.cpp
More file actions
342 lines (301 loc) · 8.97 KB
/
Copy pathMappingManager.cpp
File metadata and controls
342 lines (301 loc) · 8.97 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* MappingManager.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MappingManager.h"
#include <iostream>
namespace mmp {
MappingManager::MappingManager()
{
// TODO Auto-generated constructor stub
}
QMap<uid, Layer::ptr> MappingManager::getSourceLayers(const Source::ptr source) const
{
QMap<uid, Layer::ptr> sourceLayers;
for (QVector<Layer::ptr>::const_iterator it = layerVector.begin(); it != layerVector.end(); ++it)
{
if ((*it)->getSource() == source)
{
sourceLayers[(*it)->getId()] = *it;
}
}
return sourceLayers;
}
Source::ptr MappingManager::getSourceByName(QString name)
{
return _getElementByName(sourceVector, name);
}
QString MappingManager::generateUniqueSourceName(const QString& baseName) const
{
auto isTaken = [this](const QString& candidate) {
for (const Source::ptr& source : sourceVector)
if (source->getName() == candidate)
return true;
return false;
};
if (!isTaken(baseName))
return baseName;
for (int n = 2; ; ++n)
{
const QString candidate = QString("%1 %2").arg(baseName).arg(n);
if (!isTaken(candidate))
return candidate;
}
}
QVector<Source::ptr> MappingManager::getSourcesByNameRegExp(QString namePattern)
{
return _getElementsByNameRegExp(sourceVector, namePattern);
}
QVector<Source::ptr> MappingManager::getSourcesCompatibleWith(Layer::ptr layer)
{
QVector<Source::ptr> paints;
for (QVector<Source::ptr>::const_iterator it = sourceVector.constBegin();
it != sourceVector.constEnd(); ++it)
if (layer->sourceIsCompatible(*it))
paints.append(*it);
return paints;
}
Layer::ptr MappingManager::getLayerByName(QString name)
{
return _getElementByName(layerVector, name);
}
QVector<Layer::ptr> MappingManager::getLayersByNameRegExp(QString namePattern)
{
return _getElementsByNameRegExp(layerVector, namePattern);
}
QMap<uid, Layer::ptr> MappingManager::getSourceLayersById(uid sourceId) const
{
return getSourceLayers(sourceMap[sourceId]);
}
uid MappingManager::addSource(Source::ptr source)
{
sourceVector.push_back(source);
sourceMap[source->getId()] = source;
return source->getId();
}
bool MappingManager::removeSource(uid sourceId)
{
// Make sure the source to which this source refers to exists in the manager.
Source::ptr source = getSourceById(sourceId);
if (source)
{
// Remove all mappings associated with source.
QMap<uid, Layer::ptr> sourceLayers = getSourceLayers(source);
for (QMap<uid, Layer::ptr>::const_iterator it = sourceLayers.constBegin();
it != sourceLayers.constEnd(); ++it)
{
removeLayer(it.key());
}
// Remove source. Its lifetime is managed by QSharedPointer: dropping the
// manager's references here is enough. The source may still be held by the
// undo stack (so it can be restored), and is freed once the last shared
// pointer is released.
//
// NOTE: do NOT call source->~Source() explicitly. Because ~Source() is
// virtual, that runs the full destructor chain on an object the shared
// pointers still own, so the object is destroyed a second time when the
// last reference is released — a double-free that crashes on quit/undo
// (notably with Syphon sources, which carry extra state).
int idx = sourceVector.lastIndexOf(source);
Q_ASSERT(idx != -1);
sourceVector.remove(idx);
sourceMap.remove(sourceId);
// Release the source's heavy external resources (e.g. a camera device)
// immediately, even though the object itself may live on in the undo stack.
// reacquireResources() restores them if the removal is undone.
source->releaseResources();
return true;
}
else
{
return false;
}
}
bool MappingManager::replaceSourceLayers(Source::ptr oldSource,
Source::ptr newSource)
{
// Make sure the source to which this source refers to exists in the manager.
if (oldSource && newSource)
{
QMap<uid, Layer::ptr> sourceLayers = getSourceLayers(oldSource);
for (QMap<uid, Layer::ptr>::const_iterator it = sourceLayers.constBegin();
it != sourceLayers.constEnd(); ++it)
{
Layer::ptr layer = it.value();
layer->setSource(newSource);
}
return true;
}
else
{
return false;
}
}
uid MappingManager::addLayer(Layer::ptr layer)
{
// Make sure the source to which this mapping refers to exists in the manager.
Q_ASSERT ( sourceVector.contains(layer->getSource()) );
layerVector.insert(0, layer);
layerMap[layer->getId()] = layer;
return layer->getId();
}
bool MappingManager::removeLayer(uid mappingId)
{
// Make sure the source to which this mapping refers to exists in the manager.
Layer::ptr layer = getLayerById(mappingId);
if (layer)
{
int idx = layerVector.lastIndexOf(layer);
Q_ASSERT( idx != -1 ); // Q_ASSERT(layerVector.contains(layer));
layerVector.remove(idx);
layerMap.remove(mappingId);
updateLayerDepths();
return true;
}
else
{
return false;
}
}
/// Moves a mapping of given uid by a certain number of steps up or down.
bool MappingManager::moveLayer(uid mappingId, int toIndex)
{
// Make sure the source to which this mapping refers to exists in the manager.
int idx = getLayerIndex(mappingId);
if (idx >= 0)
{
layerVector.move(idx, toIndex);
updateLayerDepths();
return true;
}
else
{
return false;
}
}
QVector<Layer::ptr> MappingManager::getVisibleLayers() const
{
QVector<Layer::ptr> visible;
// First pass: check if one of the mappings is in solo mode.
bool hasSolo = false;
for (QVector<Layer::ptr>::const_iterator it = layerVector.begin();
it != layerVector.end(); ++it)
{
if ((*it)->isSolo())
{
hasSolo = true;
break;
}
}
// Second pass: fill the visible vector.
for (QVector<Layer::ptr>::const_iterator it = layerVector.begin();
it != layerVector.end(); ++it)
{
// Solo has priority over invisible (mute)
if ( (hasSolo && (*it)->isSolo()) ||
(! hasSolo && (*it)->isVisible()) )
{
visible.push_back(*it);
}
}
return visible;
}
/// Returns true iff the mapping is visible.
bool MappingManager::layerIsVisible(Layer::ptr layer) const
{
// Solo mappings are always visible.
if (layer->isSolo())
{
return true;
}
// Non-solo invisible mappings are always invisible.
else if (! layer->isVisible())
{
return false;
}
// Mapping is non-solo yet visible: check if another mapping is solo
// (which would thus make it invisible).
else
{
for (QVector<Layer::ptr>::const_iterator it = layerVector.begin();
it != layerVector.end(); ++it)
{
if ((*it)->isSolo())
{
return false;
}
}
// Mapping is non-solo yet visible and there are no solo mappings.
return true;
}
}
/// Returns the list of visible paints (ie. paints for which at least one mapping is visible).
QVector<Source::ptr> MappingManager::getVisibleSources() const
{
QVector<Source::ptr> visibleSources;
QVector<Layer::ptr> visibleLayers = getVisibleLayers();
for (QVector<Layer::ptr>::const_iterator it = visibleLayers.begin();
it != visibleLayers.end(); ++it)
{
Source::ptr source((*it)->getSource());
if (!visibleSources.contains(source))
visibleSources.push_back(source);
}
return visibleSources;
}
void MappingManager::reorderLayers(QVector<uid> mappingIds)
{
// Both vector needs to have the same size.
Q_ASSERT( mappingIds.size() == layerVector.size() );
layerVector.clear();
int depth = 0;
for (QVector<uid>::iterator it = mappingIds.begin();
it != mappingIds.end(); ++it)
{
// Uid should be a key of the layerMap.
Q_ASSERT( layerMap.contains(*it) );
// Makes sure the uids are not repeated.
Q_ASSERT( ! layerVector.contains(layerMap[*it]) );
// Adds the mapping at the right place in the vector.
Layer::ptr layer = layerMap[*it];
layer->setDepth(depth);
layerVector.push_back( layer );
depth++;
}
}
void MappingManager::updateLayerDepths()
{
int depth = 0;
for (QVector<Layer::ptr>::iterator it = layerVector.begin();
it != layerVector.end(); ++it)
{
(*it)->setDepth(depth);
depth++;
}
}
//bool MappingManager::removeLayer(Layer::ptr layer)
//{
//}
void MappingManager::clearAll()
{
sourceVector.clear();
layerVector.clear();
sourceMap.clear();
layerMap.clear();
}
}