This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmodel.h
274 lines (217 loc) · 6.24 KB
/
dbmodel.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @file dbmodel.h
* @brief Declarations for DbModel class
* @author Nicu Tofan <nicu.tofan@gmail.com>
* @copyright Copyright 2014 piles contributors. All rights reserved.
* This file is released under the
* [MIT License](http://opensource.org/licenses/mit-license.html)
*/
#ifndef GUARD_DBMODEL_H_INCLUDE
#define GUARD_DBMODEL_H_INCLUDE
#include <dbmodel/dbmodel-config.h>
#include <dbmodel/dbmodelcol.h>
#include <dbmodel/dbmodeltbl.h>
#include <QSqlRecord>
#include <QSortFilterProxyModel>
#include <QSqlDatabase>
#include <QVector>
#include <QModelIndex>
class DbModelPrivate;
QT_BEGIN_NAMESPACE
class QSqlQueryModel;
class QSqlTableModel;
class QAbstractItemView;
QT_END_NAMESPACE
//! A Qt model capable of representing sql tables.
class DBMODEL_EXPORT DbModel : public QSortFilterProxyModel {
Q_OBJECT
DbModelPrivate * impl; /**< the private implementation */
QString filter_; /**< current installed filter */
public:
//! Create a new model from a table or view definition and a database.
DbModel (
DbStruct * db,
DbTaew * meta,
QObject * parent = NULL);
//! Create a new model from a database and an index.
DbModel (
DbStruct * db,
int component,
QObject * parent = NULL);
Q_DISABLE_COPY(DbModel)
//! Destructor.
virtual ~DbModel();
//! Is this a valid model (with a table set) or not?
bool
isValid() const;
//! Set the table or view; old instance is deleted;
//! ownership of table is assumed.
void
setMeta (
DbTaew * meta);
//! Set the database and table or view; old instance is deleted;
//! ownership of table is assumed.
void
setMeta (
DbStruct * database,
DbTaew * meta);
//! Give away the pointer and remove it from internal storage.
DbTaew *
takeMeta ();
//! Get a pointer to meta object; ownership stays with this instance.
DbTaew *
metaTaew () const;
//! Get a pointer to internal main model object.
QSqlTableModel *
mainModel () const;
//! Select the model (retrieve information using options).
bool
selectMe ();
//! Number of rows.
virtual int
rowCount () const;
//! Number of columns.
virtual int
columnCount () const;
//! Number of rows.
virtual int
rowCount (
const QModelIndex & idx) const {
return QSortFilterProxyModel::rowCount (idx);
}
//! Number of columns.
virtual int
columnCount (
const QModelIndex & idx) const {
return QSortFilterProxyModel::columnCount (idx);
}
//! Custom sorting.
bool
lessThan (
const QModelIndex &left,
const QModelIndex &right) const;
//! Retrieve the database. Ownership of returned pointer stays
//! with this instance.
DbStruct *
database () const;
//! Set the database; also invalidates the table.
void
setDatabase (
DbStruct * value);
//! Give away the pointer and remove it from internal storage.
DbStruct *
takeDatabase ();
//! Get the model data regarding a column; index is a virtual index.
const DbModelCol &
columnData (
int index) const;
//! Get the model data regarding a table.
const DbModelTbl &
tableData (
int table_index) const;
//! Set a filter on one of the internal models identified by its index.
bool
setFilter (
const QString & filter,
int table_index = 0);
//! Set a filter on one of the internal models identified by its name.
bool
setFilter (
const QString & filter,
const QString & table);
//! Get the filter installed on main model.
const QString &
filter () const {
return filter_;
}
//! Set a filter on one of the internal models identified by its index.
bool
setOrder (
int column,
Qt::SortOrder order,
int table_index = 0);
//! Set a filter on one of the internal models identified by its name.
bool
setOrder (
int column,
Qt::SortOrder order,
const QString & table);
//! Find the index of a model identified by its name.
int
findTable (
const QString & table) const;
//! Get a record for a row
QSqlRecord
record (
int row) const;
//! Read the labels again (possibly in a different language).
void
reloadHeaders ();
//! Set the callback for a column in a table.
bool
setColumnCallback (
int table_index,
int column_index,
DbColKb value,
void * user_data = NULL);
//! Set the callback for a column in main table.
bool
setColumnCallback (
int column_index,
DbColKb value,
void * user_data = NULL) {
return setColumnCallback (0, column_index, value, user_data);
}
//! Get the callback for a cell.
DbColKb
columnCallback (
int table_index,
int column_index);
//! Get the callback for a cell.
void *
columnCallbackData ();
DbModelPrivate *
implementation () const {
return impl;
}
/* - - - - - - - - - - - - - - - - */
/** @name Marker
* A cell may be highlighted in a different color and with
* a special icon; the model only stores the coordinates
* of that cell.
*/
///@{
//! Set the indicated .
bool
setCurrentMarker (
int row,
int column);
//! Retrieve highlight row.
int
getMarkerRow () const;
//! Retrieve highlight column.
int
getMarkerCol () const;
//! Tell if we have a highlight cell set.
bool
hasMarkerCell () const;
///@}
/* - - - - - - - - - - - - - - - - */
static int
findById (
QAbstractItemModel * m,
long id,
int col_id = 0);
static int
findById (
QAbstractItemView *view,
long id,
int col_id = 0);
static int
selectById (
QAbstractItemView *view,
long id,
int col_id = 0);
public: virtual void anchorVtable() const;
};
#endif // GUARD_DBMODEL_H_INCLUDE