| 1 | #ifndef PROPERTYITEMMODEL_HXX |
| 2 | #define PROPERTYITEMMODEL_HXX |
| 3 | |
| 4 | #include <QAbstractListModel> |
| 5 | #include <QQmlListProperty> |
| 6 | |
| 7 | #include <simgear/props/props.hxx> |
| 8 | |
| 9 | class PropertyItemModelRoleMapping; |
| 10 | class FGQmlPropertyNode; |
| 11 | |
| 12 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 13 | #define QML_LIST_INDEX_TYPE qsizetype |
| 14 | #else |
| 15 | #define QML_LIST_INDEX_TYPE int |
| 16 | #endif |
| 17 | |
| 18 | class PropertyItemModel : public QAbstractListModel, |
| 19 | public SGPropertyChangeListener |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | |
| 23 | Q_PROPERTY(QString rootPath READ rootPath WRITE setRootPath NOTIFY rootChanged) |
| 24 | |
| 25 | Q_PROPERTY(FGQmlPropertyNode* root READ root WRITE setRoot NOTIFY rootChanged) |
| 26 | |
| 27 | Q_PROPERTY(QString childNameFilter READ childNameFilter WRITE setChildNameFilter NOTIFY childNameFilterChanged) |
| 28 | Q_PROPERTY(QQmlListProperty<PropertyItemModelRoleMapping> mappings READ mappings NOTIFY mappingsChanged) |
| 29 | // list property storing child/role mapping |
| 30 | |
| 31 | QQmlListProperty<PropertyItemModelRoleMapping> mappings(); |
| 32 | public: |
| 33 | PropertyItemModel(); |
| 34 | |
| 35 | int rowCount(const QModelIndex &parent) const override; |
| 36 | |
| 37 | QVariant data(const QModelIndex &index, int role) const override; |
| 38 | |
| 39 | QHash<int, QByteArray> roleNames() const override; |
| 40 | |
| 41 | QString rootPath() const; |
| 42 | |
| 43 | QString childNameFilter() const; |
| 44 | |
| 45 | FGQmlPropertyNode* root() const; |
| 46 | |
| 47 | public slots: |
| 48 | void setRootPath(QString rootPath); |
| 49 | |
| 50 | void setChildNameFilter(QString childNameFilter); |
| 51 | |
| 52 | void setRoot(FGQmlPropertyNode* root); |
| 53 | |
| 54 | signals: |
| 55 | void rootChanged(); |
| 56 | |
| 57 | void childNameFilterChanged(QString childNameFilter); |
| 58 | |
| 59 | void mappingsChanged(); |
| 60 | protected: |
| 61 | // implement property-change listener to watch for changes to the |
| 62 | // underlying nodes |
| 63 | void valueChanged(SGPropertyNode *node) override; |
| 64 | void childAdded(SGPropertyNode *parent, SGPropertyNode *child) override; |
| 65 | void childRemoved(SGPropertyNode *parent, SGPropertyNode *child) override; |
| 66 | |
| 67 | private: |
| 68 | void rebuild(); |
| 69 | |
| 70 | void innerSetRoot(SGPropertyNode_ptr root); |
| 71 | |
| 72 | static void append_mapping(QQmlListProperty<PropertyItemModelRoleMapping> *list, |
| 73 | PropertyItemModelRoleMapping *mapping); |
| 74 | static PropertyItemModelRoleMapping* at_mapping(QQmlListProperty<PropertyItemModelRoleMapping>* list, QML_LIST_INDEX_TYPE index); |
| 75 | static QML_LIST_INDEX_TYPE count_mapping(QQmlListProperty<PropertyItemModelRoleMapping>* list); |
| 76 | static void clear_mapping(QQmlListProperty<PropertyItemModelRoleMapping> *list); |
| 77 | |
| 78 | SGPropertyNode_ptr _modelRoot; |
| 79 | QVector<PropertyItemModelRoleMapping*> _roleMappings; |
| 80 | QString _childNameFilter; |
| 81 | |
| 82 | mutable std::vector<SGPropertyNode_ptr> _nodes; |
| 83 | }; |
| 84 | |
| 85 | #endif // PROPERTYITEMMODEL_HXX |