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/tree/MapTreeModel.h
2018-03-27 14:04:31 +02:00

130 lines
2.9 KiB
C++

#ifndef MAPTREE_H
#define MAPTREE_H
#include "../fixC11.h"
#include <QAbstractItemModel>
#include "../mapview/model/MapModel.h"
// http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
class MapTreeModel : public QAbstractItemModel {
Q_OBJECT
private:
MapModel* mdl;
public:
/** ctor */
MapTreeModel(MapModel* mdl) : mdl(mdl) {
connect(mdl, SIGNAL(reset()), this, SLOT(onMapModelReset()));
}
virtual ~MapTreeModel() {;}
MapModel* getMapModel() {
return mdl;
}
virtual QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override {
MapLayer* _parent = nullptr;
if (!parent.isValid()) {
_parent = mdl->getRootLayer();
} else {
_parent = static_cast<MapLayer*>(parent.internalPointer());
}
if (_parent->getSubLayers().size() > 0) {
return createIndex(row, column, _parent->getSubLayers().at(row));
} else {
return createIndex(0, 0, nullptr);
}
}
virtual QModelIndex parent(const QModelIndex& child) const override {
MapLayer* _child = static_cast<MapLayer*>(child.internalPointer());
if (!_child) {return QModelIndex();}
return createIndex(0,0,_child->getParent());
}
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override {
// if (parent.column() > 0) {
// return 0;
// }
MapLayer* _parent = nullptr;
if (!parent.isValid()) { // root
_parent = mdl->getRootLayer();
} else { // deeper layer
_parent = static_cast<MapLayer*>(parent.internalPointer());
}
return _parent->getSubLayers().size();
}
virtual int columnCount(const QModelIndex& parent) const override {
if (!parent.isValid()) {
return 1; // root
} else {
return 1; // TODO
}
}
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
MapLayer* _elem = static_cast<MapLayer*>(index.internalPointer());
if (index.column() == 0) {
if (role == Qt::DisplayRole) {return _elem->getLayerName().c_str();}
if (role == Qt::CheckStateRole) {return _elem->isVisible() ? Qt::Checked : Qt::Unchecked;}
}
return QVariant();
}
Qt::ItemFlags flags(const QModelIndex &index) const override {
(void) index;
// each item is selectable, enable, and has an editable checkbox
int flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
return (Qt::ItemFlags) flags;
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override {
// checking / unchecking a layer (visibility)
if (role == Qt::CheckStateRole) {
MapLayer* _elem = static_cast<MapLayer*>(index.internalPointer());
_elem->setVisible( value != 0 );
return true;
}
return false;
}
public slots:
/** the underlying map-model has changed */
void onMapModelReset() {
//emit dataChanged(QModelIndex(), QModelIndex(-1));
//emit modelReset(QPrivateSignal());
endResetModel();
}
};
#endif // MAPTREE_H