EHM DAL 0.2.3
Data abstraction layer for Eastside Hockey Manager
Loading...
Searching...
No Matches
abstract_table_item.h
1#pragma once
2
3// Application headers
4namespace ehm_dal::column_data {
5class ColumnData;
6}
7namespace ehm_dal::database {
8class DatabaseVersion;
9}
10namespace ehm_dal::tables {
11enum class TableIndex : char;
12}
13
14// Qt headers
15class QDataStream;
16class QModelIndex;
17#include <QString>
18#include <QVariant>
19#include <type_traits>
20
21namespace ehm_dal::schema {
22
23template<typename T>
24concept IntegralNumber = std::is_integral<T>::value;
25
26// -- Table item/record --- //
31{
32public:
41
42 // Child data
49 virtual void addChildItem(std::shared_ptr<AbstractTableItem> item,
50 const ehm_dal::tables::TableIndex child_table);
59 virtual QVariant childData(const QModelIndex &index,
60 qint32 role,
62 const ehm_dal::tables::TableIndex child_table) const;
67 virtual qint32 childItemId(const ehm_dal::tables::TableIndex child_table) const;
72 virtual qint32 childRowCount(const ehm_dal::tables::TableIndex child_table) const;
83 virtual bool setChildData(const QModelIndex &index,
84 const QVariant &value,
85 const qint32 role,
86 std::vector<qint32> &modified_columns,
88 const ehm_dal::tables::TableIndex child_table);
89
90 // File i/o
97 virtual bool read(QDataStream &in, ehm_dal::database::DatabaseVersion &version);
104 virtual bool write(QDataStream &out, ehm_dal::database::DatabaseVersion &version);
105
106 // Get data - item attributes
111 inline qint32 id() const { return id_; }
118 QVariant id(const qint32 role);
123 virtual QString text() const = 0;
124 Q_DECL_DEPRECATED QString displayText() const { return text(); } // TODO to be deprecated
129 virtual qint32 uid() const;
130
131 // Get data
138 QVariant data(const qint32 column,
139 qint32 role,
140 ehm_dal::column_data::ColumnData *column_data) const;
147 virtual QVariant data(const QModelIndex &index,
148 qint32 role,
149 ehm_dal::column_data::ColumnData *column_data) const;
150
151 // Identifier
155 virtual QString identifier() const;
160 virtual QStringList identifierList() const;
161 // TODO implement
162 /*QString personIdentifier(const QString &first_name,
163 const QString &second_name,
164 const Date &date_of_birth) const;
165 QStringList personIdentifierList(const QString &first_name,
166 const QString &second_name,
167 const Date &date_of_birth,
168 const Year &year_of_birth) const;*/
169
170 // Initialisation
175 inline virtual bool init() = 0;
182 bool alloc(const qint32 new_id);
183
184 // Parent data
188 virtual qint32 parentItemId() const;
189
190 // Set data
200 bool setData(const qint32 column,
201 const QVariant &value,
202 qint32 role,
203 std::vector<qint32> &modified_columns,
214 virtual bool setData(const QModelIndex &index,
215 const QVariant &value,
216 qint32 role,
217 std::vector<qint32> &modified_columns,
219
220 // Validation
224 virtual void fixErrors() { return; }
225
226protected:
227 // Item attributes
228 qint32 id_; // Record id
229
230 // Get data
231 inline virtual bool deletable() const { return false; }
232 template<IntegralNumber T>
233 QVariant fromHexVector(const std::vector<T> &raw_data, const qint32 role) const;
240 QVariant fromValue(const QVariant &value, const qint32 role) const;
241
242 // Operator overloading (Data stream)
243 // This is solely for the purposes of reading/writing the record id for child items
244 friend QDataStream &operator>>(QDataStream &in, AbstractTableItem &data);
245 friend QDataStream &operator<<(QDataStream &out, const AbstractTableItem &data);
246
247private:
248 // Get data
249 virtual QVariant internalData(const qint32 column, const qint32 role) const = 0;
250
251 // Set data
252 virtual bool setInternalData(const qint32 column,
253 const QVariant &value,
254 const qint32 role,
255 std::vector<qint32> &modified_columns);
256};
257
258// File i/o
259QDataStream &operator>>(QDataStream &in, ehm_dal::schema::AbstractTableItem &data);
260QDataStream &operator<<(QDataStream &out, const ehm_dal::schema::AbstractTableItem &data);
261
262// --- Get vector of numbers as a string --- //
263template<IntegralNumber T>
264QVariant AbstractTableItem::fromHexVector(const std::vector<T> &raw_data, const qint32 role) const
265{
266 if (role != Qt::DisplayRole)
267 return QVariant();
268
269 QStringList buffer;
270 for (const auto &itr : raw_data)
271 buffer << QString("%1").arg(static_cast<quint8>(itr), 2, 16, QLatin1Char('0'));
272
273 return buffer.join(QStringLiteral(" "));
274}
275
276} // namespace ehm_dal::schema
277
278
The ColumnData class represents a container of ehm_dal::column_data::Column and attributes relating t...
Definition: column_data.h:18
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
The AbstractTableItem class represents a row in a Table with an id field.
Definition: abstract_table_item.h:31
QVariant data(const qint32 column, qint32 role, ehm_dal::column_data::ColumnData *column_data) const
Returns the row's data stored under the given role for the column.
virtual qint32 parentItemId() const
Returns the item id of the items parent item. Applies to tree structures only.
bool alloc(const qint32 new_id)
Initialises the table item and allocates and item id to new_id and a uid (if applicable) and then cal...
bool setData(const qint32 column, const QVariant &value, qint32 role, std::vector< qint32 > &modified_columns, ehm_dal::column_data::ColumnData *column_data)
Sets the role data for the item at column to value.
QVariant id(const qint32 role)
Returns the row id in a manner compatible with QAbstractTableModel item roles. This is generally unec...
virtual ~AbstractTableItem()
Default destructor.
virtual QVariant data(const QModelIndex &index, qint32 role, ehm_dal::column_data::ColumnData *column_data) const
Returns the row's data stored under the given role for the column referred to by the index.
virtual bool init()=0
Initialises the table item members. This only needs to be called once per item.
virtual QStringList identifierList() const
Returns a list of all of the item's identifiers for matching against. This includes the main identifi...
QVariant fromValue(const QVariant &value, const qint32 role) const
Returns a value as a QVariant if the role is Qt::DisplayRole or Qt::EditRole. A blank QVariant is ret...
virtual bool setChildData(const QModelIndex &index, const QVariant &value, const qint32 role, std::vector< qint32 > &modified_columns, ehm_dal::column_data::ColumnData *column_data, const ehm_dal::tables::TableIndex child_table)
Sets the role data for the child item at index to value.
virtual QString text() const =0
Returns the row's display text. This provides a convenient way to obtain a simple description of the ...
virtual bool read(QDataStream &in, ehm_dal::database::DatabaseVersion &version)
Reads a table row from the QDataStream.
virtual bool write(QDataStream &out, ehm_dal::database::DatabaseVersion &version)
Writes a table row to the QDataStream.
virtual QVariant childData(const QModelIndex &index, qint32 role, ehm_dal::column_data::ColumnData *column_data, const ehm_dal::tables::TableIndex child_table) const
Returns child data stored under the given role for the selected index.
virtual void addChildItem(std::shared_ptr< AbstractTableItem > item, const ehm_dal::tables::TableIndex child_table)
Adds the child item item from the child_table to the present tree table. This represents a tree relat...
virtual qint32 childItemId(const ehm_dal::tables::TableIndex child_table) const
Returns the item id number for the child data under the related child_table.
AbstractTableItem()
Default constructor.
qint32 id() const
Returns the row id. The first row of a table is 0, the second row is 1, etc.
Definition: abstract_table_item.h:111
virtual qint32 uid() const
Returns the row's unique database id. Not all tables use a uid.
virtual void fixErrors()
Fixes any known errors with the item. E.g. errors introduced with a prior version of EHM DAL or the E...
Definition: abstract_table_item.h:224
virtual bool setData(const QModelIndex &index, const QVariant &value, qint32 role, std::vector< qint32 > &modified_columns, ehm_dal::column_data::ColumnData *column_data)
Sets the role data for the item at index to value.
virtual QString identifier() const
Returns the item's text identifier for matching against.
virtual qint32 childRowCount(const ehm_dal::tables::TableIndex child_table) const
Returns the number of related child items in respect of child_table.
Definition: abstract_table_item.h:24
namespace ehm_dal::tables
Definition: column.h:4
TableIndex
The TableIndex enum represents the id number of each table within the database.
Definition: table_index.h:14