-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMappingManager.h
161 lines (125 loc) · 4.66 KB
/
MappingManager.h
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
/*
* MappingManager.h
*
* (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/>.
*/
#ifndef MAPPINGMANAGER_H_
#define MAPPINGMANAGER_H_
#include <QVector>
#include <QMap>
#include "Paint.h"
#include "Mapping.h"
namespace mmp {
/**
* This is a container class for all the paints and mappings ie. the main model object that allows
* CRUD over paints and mappings.
*/
class MappingManager
{
public:
/// Constructor.
MappingManager();
private:
// Model elements.
/// Container for all paints.
QVector<Paint::ptr> paintVector;
/// Maps from uids to paints.
QMap<uid, Paint::ptr> paintMap;
/// Container for all mappings (ordered from bottom layer to top layer).
QVector<Mapping::ptr> mappingVector;
/// Maps from uids to mappings.
QMap<uid, Mapping::ptr> mappingMap;
public:
/// Returns the list of mappings associated with given paint.
QMap<uid, Mapping::ptr> getPaintMappings(const Paint::ptr paint) const;
/// Returns the list of mappings associated with given paint uid.
QMap<uid, Mapping::ptr> getPaintMappingsById(uid paintId) const;
/// Adds a paint and returns its uid.
uid addPaint(Paint::ptr paint);
/// Returns the uid of a paint.
uid getPaintId(Paint::ptr paint) const { return paintMap.key(paint); }
/// Removes a paint of given uid.
bool removePaint(uid paintId);
/// DEPRECATED.
bool replacePaintMappings(Paint::ptr oldpaint, Paint::ptr newpaint);
/// Returns the number of paints.
int nPaints() const { return paintVector.size(); }
/// Returns the i-th paint in the vector. Good for iterating over all paints.
Paint::ptr getPaint(int i) { return paintVector[i]; }
/// Returns paint with given uid.
Paint::ptr getPaintById(uid id) { return paintMap[id]; }
/// Returns mapping with given name (first match).
Paint::ptr getPaintByName(QString name);
/// Returns all mappings with given regexp.
QVector<Paint::ptr> getPaintsByNameRegExp(QString namePattern);
/// Adds a mapping and returns its uid.
uid addMapping(Mapping::ptr mapping);
/// Removes a mapping of given uid.
bool removeMapping(uid mappingId);
/// Returns the number of mappings.
int nMappings() const { return mappingVector.size(); }
/**
* Returns the i-th mapping in the vector. Good for iterating over all mappings. Vector is
* ordered from bottom to top layer.
*/
Mapping::ptr getMapping(int i) { return mappingVector[i]; }
/// Returns mapping with given uid.
Mapping::ptr getMappingById(uid id) { return mappingMap[id]; }
/// Returns mapping with given name (first match).
Mapping::ptr getMappingByName(QString name);
/// Returns all mappings with given regexp.
QVector<Mapping::ptr> getMappingsByNameRegExp(QString namePattern);
/// Reorders the mappings according to given list of uids. QVector needs to
void reorderMappings(QVector<uid> mappingIds);
/// Returns the ordered list of visible mappings, using both the "visible" and "solo" properties.
QVector<Mapping::ptr> getVisibleMappings() const;
/// Returns true iff the mapping is visible.
bool mappingIsVisible(Mapping::ptr mapping) const;
/// Returns the list of visible paints (ie. paints for which at least one mapping is visible).
QVector<Paint::ptr> getVisiblePaints() const;
void clearAll();
private:
template<class T>
QSharedPointer<T> _getElementByName(const QVector<QSharedPointer<T> >& vector, QString name)
{
for (QSharedPointer<T> it: vector)
{
if (it->getName() == name)
{
return it;
}
}
// Nothing found.
return QSharedPointer<T>();
}
template<class T>
QVector<QSharedPointer<T> > _getElementsByNameRegExp(const QVector<QSharedPointer<T> >& vector, QString namePattern)
{
QVector<QSharedPointer<T> > matchedElems;
QRegExp regExp(namePattern, Qt::CaseSensitive, QRegExp::Wildcard);
for (QSharedPointer<T> it: vector)
{
if (regExp.exactMatch(it->getName()))
{
matchedElems.push_back(it);
}
}
return matchedElems;
}
};
}
#endif /* MAPPINGMANAGER_H_ */