summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.h
blob: 72a7b7fdd4db5ba877d181eeeb17bfd18e135c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#ifndef MAPPINGTREEMODEL_H
#define MAPPINGTREEMODEL_H

#include <QSqlDatabase>

#include "smtreemodel.h"

class QSqlQuery;
class SmTreeItem;
class MappingData;
class SmTreeItem;

class MappingTreeModel : public SmTreeModel {
    Q_OBJECT
    public:
        enum Roles { NameRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, MapParentIdRole = Qt::UserRole + 4 };
        enum Fields { Name = 0, Id = 1, Added = 2, MapParentId = 3 };
        enum { NumFields = 4 };
        MappingTreeModel(QStringList &headers, QObject *parent = 0);
        ~MappingTreeModel();

        //model type convenience functions
        int type() const { return mType; }
        QStringList mappingTypeNames() const;
        int mappingTypeIdFromName(const QVariant &name) const;
        QString mappingTypeNameFromId(int id) const;

        //static convenience funcs
        //the caller is responsible for deleting the result!
        SmTreeItem *treeFromPaths(const QStringList &paths);

        //data
        QVariant data(const QModelIndex &index, int role) const;
        QList<QVariant> childList(const QVariant &value, int column = 0) const;
        QList<QVariant> mappingsForFile(const QVariant &fileId) const;
        QStringList path(QModelIndex &idx) const;
        QModelIndex indexFromPath(const QString &path, int column = 0) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        void move(const QModelIndex &source, const QModelIndex &dest);
        bool addMappingType(const QString &type);
        bool deleteMappingType(int typeId);
        bool addChild(const QVariant &name, const QModelIndex &parent);
        bool deleteChild(const QModelIndex &idx);
        int childCount(const QModelIndex &idx) const;
        MappingData mappingDataFromIndex(QModelIndex &idx) const;
        QStringList paths() const;
        const QString &forbidden() const { return mForbidden; }

    public slots:
        void populate();
        void setType(int type);

    signals:
        void mappingTypesChanged();
        void needExpansion();

    private:
        struct mappingType {
            QVariant id;
            QVariant name;
        };
        void getMappings();
        int addChild(const QString &name, int type);
        void getMappingTypes();
        void getChildrenRecursive(SmTreeItem *item);
        QStringList getPathsRecursive(SmTreeItem *parent) const;
        QList<QVariant> getChildListRecursive(SmTreeItem *item, int column) const;
        QString basePath(SmTreeItem *item) const;
        int lowerBound(SmTreeItem *item, const QVariant &value, int column = 0) const;
        QList<MappingData> mappingData(SmTreeItem *item);
        QSqlDatabase mDb;
        QSqlQuery *mTypesQ;
        QString mSParentsQ;
        QSqlQuery *mUpdateTypeQ;
        QSqlQuery *mUpdateChildQ;
        QSqlQuery *mAddMappingTypeQ;
        QSqlQuery *mDeleteMappingTypeQ;
        QSqlQuery *mAddChildQ;
        QSqlQuery *mSelectChildQ;
        QSqlQuery *mAddParentQ;
        QSqlQuery *mDeleteChildQ;
        QSqlQuery *mUpdateParentQ;
        QSqlQuery *mDeleteMappingParentQ;
        QSqlQuery *mMappingsForFileIdQ;
        QSqlQuery *mMappingsQ;
        QList<mappingType> mMappingTypes;
        QMap<QString, int> mMappings;
        const QString mForbidden;
        int mType;
};

class MappingTreeResultModel : public SmTreeModel {
    Q_OBJECT
    public:
        enum Roles { NameRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 2 };
        enum Fields { Name = 0, Id = 1 };
        enum { NumFields = 2 };
        explicit MappingTreeResultModel(const QStringList &headers, QObject *parent = 0);

        //data + flags
        Qt::ItemFlags flags(const QModelIndex &index) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        QModelIndex insertChild(const QVariant &data, int id, SmTreeItem *parent);
        QList<int> mappingsIds() const;
        void clearData();

    public slots:
        void addItem(const MappingData &data);

    private:
        int hasChild(SmTreeItem *item, const QVariant &name, int column = 0) const;
        QList<int> mappingIdsRecursive(SmTreeItem *parent) const;
};

struct MappingData {
    int id;
    QString name;
    QList<QStringList> path;
};

Q_DECLARE_METATYPE(QList<QStringList>)

#endif // MAPPINGTREEMODEL_H