-
Notifications
You must be signed in to change notification settings - Fork 105
/
map_part.h
292 lines (234 loc) · 7.47 KB
/
map_part.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
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
/*
* Copyright 2012, 2013 Thomas Schöps
* Copyright 2012-2017 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering 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.
*
* OpenOrienteering 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 OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPENORIENTEERING_MAP_PART_H
#define OPENORIENTEERING_MAP_PART_H
#include <cstddef>
#include <functional>
#include <memory>
#include <vector>
#include <utility>
#include <QHash>
#include <QRectF>
#include <QString>
class QIODevice;
class QTransform;
class QXmlStreamReader;
class QXmlStreamWriter;
// IWYU pragma: no_forward_declare QRectF
namespace OpenOrienteering {
class Map;
class MapCoordF;
class Object;
class Symbol;
using SymbolDictionary = QHash<QString, Symbol*>; // from symbol.h
class UndoStep;
using SelectionInfoVector = std::vector<std::pair<int, Object*>> ;
/**
* Represents a part of a map by owning a list of map objects.
*
* Dividing maps in parts is e.g. useful to have multiple mappers work on a map:
* every mapper can do the work in his/her part without getting into conflict
* with other parts. For display, the objects from all parts are merged.
*
* Another application is in course setting, where it is useful to have
* a map part for event-specific map objects and parts for course-specific
* map objects. Then a course can be printed by merging the event-specific part
* with the part for the course.
*
* Currently, only one map part can be used per map.
*/
class MapPart
{
friend class OCAD8FileImport;
public:
/**
* Creates a new map part with the given name for a map.
*/
MapPart(const QString& name, Map* map);
MapPart(const MapPart&) = delete;
/**
* Destroys the map part.
*/
~MapPart();
MapPart& operator=(const MapPart&) = delete;
/**
* Saves the map part in xml format to the given stream.
*/
void save(QXmlStreamWriter& xml) const;
/**
* Loads the map part in xml format from the given stream.
*
* Needs a dictionary to map symbol ids to symbol pointers.
*/
static MapPart* load(QXmlStreamReader& xml, Map& map, SymbolDictionary& symbol_dict);
/**
* Returns the part's name.
*/
const QString& getName() const;
/**
* Sets the part's name.
*/
void setName(const QString& new_name);
/**
* Returns the number of objects in the part.
*/
int getNumObjects() const;
/**
* Returns the i-th object from the part.
*/
const Object* getObject(int i) const;
/**
* Returns the i-th object from the part.
*/
Object* getObject(int i);
/**
* Returns the index of the object.
*
* Loops over all objects in the part and looks for the given pointer.
* The object must be contained in this part,
* otherwise an assert is triggered (in debug builds),
* or -1 is returned (release builds).
*/
int findObjectIndex(const Object* object) const;
/**
* Replaces the object at the given index with another.
*
* If delete_old is set, calls "delete old_object".
*/
void setObject(Object* object, int pos, bool delete_old);
/**
* Adds the object as new object at the end.
*/
void addObject(Object* object);
/**
* Adds the object as new object at the given index.
*/
void addObject(Object* object, int pos);
/**
* Deleted the object from the given index.
*
* If remove_only is set, does not call "delete object".
*
* @todo Make a separate method "removeObject()", this is misleading!
*/
void deleteObject(int pos, bool remove_only);
/**
* Deleted the object from the given index.
*
* If remove_only is set, does not call "delete object".
* Returns if the object was found in this part.
*
* @todo Make a separate method "removeObject()", this is misleading!
*/
bool deleteObject(Object* object, bool remove_only);
/**
* Imports the contents another part into this part.
*
* The other part can be from another map.
* Uses symbol_map to replace all symbols contained there.
* No replacement is done for symbols which are not in the symbol_map.
*/
std::unique_ptr<UndoStep> importPart(const MapPart* other, const QHash<const Symbol*, Symbol*>& symbol_map,
const QTransform& transform, bool select_new_objects);
/**
* @see Map::findObjectsAt().
*/
void findObjectsAt(const MapCoordF& coord, float tolerance, bool treat_areas_as_paths,
bool extended_selection, bool include_hidden_objects,
bool include_protected_objects, SelectionInfoVector& out) const;
/**
* @see Map::findObjectsAtBox().
*/
void findObjectsAtBox(const MapCoordF& corner1, const MapCoordF& corner2,
bool include_hidden_objects, bool include_protected_objects,
std::vector<Object*>& out) const;
/**
* @see Map::countObjectsInRect().
*/
int countObjectsInRect(const QRectF& map_coord_rect, bool include_hidden_objects) const;
/**
* Calculates and returns the bounding box of all objects in this map part.
*/
QRectF calculateExtent(bool include_helper_symbols) const;
/**
* Applies a condition on all objects (until the first match is found).
*
* @return True if there is an object matching the condition, false otherwise.
*/
bool existsObject(const std::function<bool (const Object*)>& condition) const;
/**
* @copybrief Map::applyOnMatchingObjects()
* @copydetails Map::applyOnMatchingObjects()
*/
void applyOnMatchingObjects(const std::function<void (Object*)>& operation, const std::function<bool (const Object*)>& condition);
/**
* @copybrief Map::applyOnMatchingObjects()
* @copydetails Map::applyOnMatchingObjects()
*/
void applyOnMatchingObjects(const std::function<void (const Object*)>& operation, const std::function<bool (const Object*)>& condition) const;
/**
* @copybrief Map::applyOnMatchingObjects()
* @copydetails Map::applyOnMatchingObjects()
*/
void applyOnMatchingObjects(const std::function<void (Object*, MapPart*, int)>& operation, const std::function<bool (const Object*)>& condition);
/**
* @copybrief Map::applyOnAllObjects()
* @copydetails Map::applyOnAllObjects()
*/
void applyOnAllObjects(const std::function<void (Object*)>& operation);
/**
* @copybrief Map::applyOnAllObjects()
* @copydetails Map::applyOnAllObjects()
*/
void applyOnAllObjects(const std::function<void (const Object*)>& operation) const;
/**
* @copybrief Map::applyOnAllObjects()
* @copydetails Map::applyOnAllObjects()
*/
void applyOnAllObjects(const std::function<void (Object*, MapPart*, int)>& operation);
private:
typedef std::vector<Object*> ObjectList;
QString name;
ObjectList objects; ///< @todo This could be a spatial representation optimized for quick access
Map* const map;
};
// ## MapPart inline and template code ###
inline
const QString& MapPart::getName() const
{
return name;
}
inline
int MapPart::getNumObjects() const
{
return int(objects.size());
}
inline
Object* MapPart::getObject(int i)
{
return objects[std::size_t(i)];
}
inline
const Object* MapPart::getObject(int i) const
{
return objects[std::size_t(i)];
}
} // namespace OpenOrienteering
#endif