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
kazu 5d002c3f2b added more cpp files for faster compile speeds
removed many obsolte elements
many improvements and fixes
2018-07-20 15:00:43 +02:00

199 lines
4.5 KiB
C++

#ifndef MAPLAYER_H
#define MAPLAYER_H
#include <string>
#include <vector>
#include <algorithm>
#include "MapLayerListener.h"
class MapModelElement;
enum class MapLayerType {
UNKNOWN,
ROOT,
REGISTRATION,
FLOORS,
FLOOR,
FLOOR_GROUND,
FLOOR_OBSTACLES,
FLOOR_BEACONS,
FLOOR_ACCESS_POINTS,
FLOOR_FINGERPRINTS,
FLOOR_ELEVATORS,
FLOOR_UNDERLAYS,
FLOOR_POIS,
FLOOR_STAIRS,
FLOOR_GROUND_TRUTH_POINTS
};
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 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) {
if (parent) {parent->addSublayer(this);} // attach myself as child of my parent
}
/** ctor */
MapLayer(MapLayer* parent, MapLayerType type) : parent(parent), type(type) {
if (parent) {parent->addSublayer(this);} // attach myself as child of my parent
}
/** 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;}
/** 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());
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;}
virtual bool isVisible() const = 0;
/** make this layer visible */
// virtual void setVisible(const bool visible) {
// this->visible = visible;
// onVisibilityChanged(visible);
// }
virtual void setVisible(const bool visible) = 0;
/** 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()) {
const std::vector<MapModelElement*> local = getElements();
el.insert(el.end(), local.begin(), local.end());
for (MapLayer* sub : getSubLayers()) {
sub->getVisibleElementsRecursive(el);
}
}
}
public:
/** 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);
}
void changed() {
for (MapLayerListener* listener : listeners) {
listener->onLayerChanged(this);
}
}
protected:
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:
MapLayerRoot(MapLayer* parent) : MapLayer(parent, MapLayerType::ROOT) {;}
std::string getLayerName() const override {return "root";}
bool isVisible() const override {
return true;
}
void setVisible(const bool visible) override {
(void) visible;
throw "error";
}
};
#endif // MAPLAYER_H