This commit is contained in:
toni
2017-03-10 14:41:04 +01:00
66 changed files with 1551 additions and 312 deletions

View File

@@ -5,6 +5,8 @@
#include <vector>
#include <algorithm>
#include "MapLayerListener.h"
class MapModelElement;
@@ -28,14 +30,17 @@ enum class MapLayerType {
class MapLayer {
/** this layer's elements */
std::vector<MapModelElement*> elements;
/** attached listeners (if any) */
std::vector<MapLayerListener*> listeners;
protected:
/** this layer's parent */
MapLayer* parent;
/** this layer's elements */
std::vector<MapModelElement*> elements;
/** this layer's sublayers */
std::vector<MapLayer*> sublayers;
@@ -60,6 +65,11 @@ public:
/** dtor */
virtual ~MapLayer() {;}
/** attach a listener to this layer [usually only added to the root layer] */
void addListener(MapLayerListener* listener) {
listeners.push_back(listener);
}
/** get the layer's parent */
MapLayer* getParent() const {return parent;}
@@ -77,14 +87,28 @@ public:
size_t getNumElements() const {return elements.size();}
/** remove the given element from the elements list */
void removeElement(const MapModelElement* elem) { elements.erase(std::remove(elements.begin(), elements.end(), elem), elements.end()); }
void removeElement(const MapModelElement* elem) {
elements.erase(std::remove(elements.begin(), elements.end(), elem), elements.end());
onElemRemoved(elem);
}
/** add a new element to this layer */
void addElement(MapModelElement* el) {
elements.push_back(el);
onElemAdded(el);
}
/** remove the given sublayer from this layer */
void removeSublayer(const MapLayer* layer) { sublayers.erase(std::remove(sublayers.begin(), sublayers.end(), layer), sublayers.end()); }
/** is this layer currently visible? */
bool isVisible() const {return visible;}
/** make this layer visible */
void setVisible(const bool visible) {this->visible = visible;}
void setVisible(const bool visible) {
this->visible = visible;
onVisibilityChanged(visible);
}
/** get all sub-layers within this layer */
@@ -94,7 +118,7 @@ public:
/** helper method to get all elements and those of all sub-layers */
void getVisibleElementsRecursive(std::vector<MapModelElement*>& el) {
if (isVisible()) {
std::vector<MapModelElement*> local = getElements();
const std::vector<MapModelElement*> local = getElements();
el.insert(el.end(), local.begin(), local.end());
for (MapLayer* sub : getSubLayers()) {
sub->getVisibleElementsRecursive(el);
@@ -102,9 +126,7 @@ public:
}
}
protected:
public:
/** add a new sublayer to this layer */
void addSublayer(MapLayer* ml) {
@@ -115,8 +137,35 @@ protected:
}
private:
void onElemAdded(MapModelElement* e) {
if (parent) {parent->onElemAdded(e);}
for (MapLayerListener* listener : listeners) {
listener->onLayerChanged(this);
listener->onLayerElementAdded(this, e);
}
}
void onElemRemoved(const MapModelElement* e) {
if (parent) {parent->onElemRemoved(e);}
for (MapLayerListener* listener : listeners) {
listener->onLayerChanged(this);
listener->onLayerElementRemoved(this, e);
}
}
void onVisibilityChanged(const bool visibile) {
if (parent) {parent->onVisibilityChanged(visibile);}
for (MapLayerListener* listener : listeners) {
listener->onLayerChanged(this);
listener->onLayerVisibilityChanged(this, visibile);
}
}
};
class MapLayerRoot : public MapLayer {
public: