-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecordmodel.hpp
236 lines (146 loc) · 6.5 KB
/
recordmodel.hpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Firebird database editor *
* Copyright (C) 2016 Łukasz "Kuszki" Dróżdż lukasz.kuszki@gmail.com *
* *
* 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 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses/. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef RECORDMODEL_HPP
#define RECORDMODEL_HPP
#include <QFutureSynchronizer>
#include <QAbstractItemModel>
#include <QMutexLocker>
#include <QVariant>
#include <QMutex>
#include <QList>
#include <QHash>
#include <QtConcurrent>
class RecordModel : public QAbstractItemModel
{
Q_OBJECT
public: class RecordObject
{
protected:
QHash<int, QVariant> Attributes;
int Index;
public:
RecordObject(int Uid, const QHash<int, QVariant>& Fields);
virtual ~RecordObject(void);
QHash<int, QVariant> getFields(void) const;
void setFields(const QHash<int, QVariant>& Fields, bool Replace = true);
void setField(int Role, const QVariant& Value);
QVariant getField(int Role) const;
bool contain(const QHash<int, QVariant>& Fields) const;
int getUid(void) const;
void setUid(int Uid);
};
private: class SortObject
{
private:
const int Index;
const bool Mode;
public:
SortObject(int Column, bool Ascending = true);
bool operator () (RecordObject* First, RecordObject* Second) const;
};
private: class GroupObject : public RecordObject
{
protected:
QVector<RecordObject*> Childs;
GroupObject* Root;
const int Column;
public:
GroupObject(int Level = -1, const QHash<int, QVariant>& Fields = QHash<int, QVariant>());
virtual ~GroupObject(void) override;
void addChild(RecordObject* Object);
bool removeChild(const QHash<int, QVariant>& Fields);
bool removeChild(RecordObject* Object);
bool removeChild(int Index);
RecordObject* takeChild(const QHash<int, QVariant>& Fields);
RecordObject* takeChild(RecordObject* Object);
RecordObject* takeChild(int Index);
QVector<GroupObject*> allGroups(void);
QVector<RecordObject*> getChilds(void);
QSet<int> allUids(void);
GroupObject* getParent(void) const;
RecordObject* getChild(int Index);
QVariant getData(void) const;
int childrenCount(void) const;
bool hasChids(void) const;
int getIndex(RecordObject* Child) const;
int getColumn(void) const;
int getIndex(void) const;
int childIndex(RecordObject* Object) const;
void sortChilds(const SortObject& Functor, QFutureSynchronizer<void>& Synchronizer);
};
private:
QHash<RecordObject*, GroupObject*> Parents;
QVector<RecordObject*> Objects;
QSet<GroupObject*> Roots;
GroupObject* Root = nullptr;
QStringList Header;
QStringList Groups;
bool selectGroups = false;
mutable QMutex Locker;
public:
explicit RecordModel(const QStringList& Head, QObject* Parent = nullptr);
virtual ~RecordModel(void) override;
virtual QModelIndex index(int Row, int Col, const QModelIndex& Parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex& Index) const override;
virtual bool hasChildren(const QModelIndex& Parent) const override;
virtual int rowCount(const QModelIndex& Parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex& Parent = QModelIndex()) const override;
virtual QVariant headerData(int Section, Qt::Orientation Orientation, int Role) const override;
virtual QVariant data(const QModelIndex& Index, int Role = Qt::DisplayRole) const override;
virtual Qt::ItemFlags flags(const QModelIndex& Index) const override;
virtual bool setData(const QModelIndex& Index, const QVariant& Value, int Role = Qt::EditRole) override;
virtual void sort(int Column, Qt::SortOrder Order) override;
bool setData(int Index, const QHash<int, QVariant>& Data, bool Replace = true);
bool setData(const QModelIndex& Index, const QHash<int, QVariant>& Data, bool Replace = true);
QHash<int, QVariant> fullData(const QModelIndex& Index) const;
QVariant fieldData(const QModelIndex& Index, int Col) const;
QModelIndexList getIndexes(const QModelIndex& Parent = QModelIndex()) const;
QSet<int> getUids(const QModelIndexList& Selection) const;
QSet<int> getUids(void) const;
int getUid(const QModelIndex& Index) const;
bool updateUid(int Old, int New);
bool saveToFile(const QString& Path, const QList<int>& Columns,
const QModelIndexList& List, bool Names,
const QString& Separator = "\t") const;
bool removeItem(const QModelIndex& Index);
int totalCount(void) const;
bool isGrouped(void) const;
bool exists(int Index) const;
QModelIndex index(int Index) const;
QModelIndex find(int Index, QVariant Data) const;
void setGroupsSelectable(bool Selectable);
protected:
GroupObject* createGroups(QList<QPair<int, QList<QVariant>>>::ConstIterator From,
QList<QPair<int, QList<QVariant>>>::ConstIterator To,
GroupObject* Parent = nullptr);
GroupObject* appendItem(RecordObject* Object);
int getIndex(const QString& Field) const;
void removeEmpty(GroupObject* Parent = nullptr, bool Emit = true);
void groupItems(void);
public slots:
void groupByInt(const QList<int>& Levels);
void groupByStr(const QStringList& Groupby);
void addItem(int ID, const QHash<int, QVariant>& Attributes);
void addItems(const QHash<int, QHash<int, QVariant>>& Items);
bool removeItem(int Index);
signals:
void onGroupComplete(void);
};
#endif // RECORDMODEL_HPP