This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
IndoorMap/mapview/model/MapLayer.h
2016-06-06 22:08:53 +02:00

129 lines
2.8 KiB
C++

#ifndef MAPLAYER_H
#define MAPLAYER_H
#include <string>
#include <vector>
#include <algorithm>
class MapModelElement;
enum class MapLayerType {
UNKNOWN,
ROOT,
FLOORS,
FLOOR,
FLOOR_GROUND,
FLOOR_OBSTACLES,
FLOOR_BEACONS,
FLOOR_ACCESS_POINTS,
FLOOR_UNDERLAYS,
FLOOR_POIS,
};
class MapLayer {
protected:
/** this layer's parent */
MapLayer* parent;
/** this layer's elements */
std::vector<MapModelElement*> elements;
/** this layer's sublayers */
std::vector<MapLayer*> sublayers;
/** this layer's type */
MapLayerType type;
/** whether this layer is visible */
bool visible;
public:
/** ctor */
MapLayer(MapLayer* parent) : parent(parent), type(MapLayerType::UNKNOWN), visible(true) {
if (parent) {parent->addSublayer(this);} // attach myself as child of my parent
}
/** ctor */
MapLayer(MapLayer* parent, MapLayerType type) : parent(parent), type(type), visible(true) {
if (parent) {parent->addSublayer(this);} // attach myself as child of my parent
}
/** dtor */
virtual ~MapLayer() {;}
/** get the layer's parent */
MapLayer* getParent() const {return parent;}
/** get the layer's name */
virtual std::string getLayerName() const = 0;
/** get the layer's type */
MapLayerType getLayerType() const {return type;}
/** get all elements within this layer */
const std::vector<MapModelElement*>& getElements() const {return elements;}
/** get all elements within this layer */
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()); }
/** is this layer currently visible? */
bool isVisible() const {return visible;}
/** make this layer visible */
void setVisible(const bool visible) {this->visible = visible;}
/** get all sub-layers within this layer */
const std::vector<MapLayer*>& getSubLayers() const {return sublayers;}
/** 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();
el.insert(el.end(), local.begin(), local.end());
for (MapLayer* sub : getSubLayers()) {
sub->getVisibleElementsRecursive(el);
}
}
}
protected:
/** add a new sublayer to this layer */
void addSublayer(MapLayer* ml) {
// sanity check before adding
if (std::find(sublayers.begin(), sublayers.end(), ml) != sublayers.end()) {throw "layer already present!";}
sublayers.push_back(ml);
}
};
class MapLayerRoot : public MapLayer {
public:
MapLayerRoot(MapLayer* parent) : MapLayer(parent, MapLayerType::ROOT) {;}
std::string getLayerName() const override {return "root";}
};
#endif // MAPLAYER_H