|
1 #pragma once |
|
2 |
|
3 #include <vector> |
|
4 |
|
5 #include <QtCore/QAbstractTableModel> |
|
6 #include <QtCore/QHash> |
|
7 #include <QtCore/QRect> |
|
8 #include <QtCore/QVector> |
|
9 #include <QtCore/QTime> |
|
10 #include <QtGui/QColor> |
|
11 |
|
12 #include <relpipe/reader/typedefs.h> |
|
13 #include <relpipe/reader/handlers/AttributeMetadata.h> |
|
14 |
|
15 using namespace relpipe::reader; |
|
16 using namespace relpipe::reader::handlers; |
|
17 |
|
18 class RelpipeTableModel : public QAbstractTableModel { |
|
19 Q_OBJECT |
|
20 private: |
|
21 std::vector<AttributeMetadata> attributes; |
|
22 QList<QVector<QVariant> * > records; |
|
23 int attributeCounter = 0; |
|
24 public: |
|
25 |
|
26 RelpipeTableModel(std::vector<AttributeMetadata> attributes, QObject *parent = 0) : QAbstractTableModel(parent), attributes(attributes) { |
|
27 } |
|
28 |
|
29 virtual ~RelpipeTableModel() { |
|
30 qDeleteAll(records); |
|
31 } |
|
32 |
|
33 int rowCount(const QModelIndex &parent = QModelIndex()) const { |
|
34 return records.count(); |
|
35 } |
|
36 |
|
37 int columnCount(const QModelIndex &parent = QModelIndex()) const { |
|
38 return attributes.size(); |
|
39 } |
|
40 |
|
41 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::ItemDataRole::DisplayRole) const { |
|
42 if (role != Qt::ItemDataRole::DisplayRole) return QVariant(); |
|
43 else if (orientation == Qt::Orientation::Horizontal) return QString::fromStdWString(attributes[section].getAttributeName().c_str()); |
|
44 else if (orientation == Qt::Orientation::Vertical) return QString("%1").arg(section + 1); |
|
45 } |
|
46 |
|
47 QVariant data(const QModelIndex &index, int role = Qt::ItemDataRole::DisplayRole) const { |
|
48 if (role == Qt::ItemDataRole::DisplayRole || role == Qt::ItemDataRole::EditRole) return records[index.row()]->at(index.column()); |
|
49 else return QVariant(); |
|
50 } |
|
51 |
|
52 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::ItemDataRole::EditRole) { |
|
53 if (index.isValid() && role == Qt::ItemDataRole::EditRole) { |
|
54 records[index.row()]->replace(index.column(), value); |
|
55 emit dataChanged(index, index); |
|
56 return true; |
|
57 } else { |
|
58 return false; |
|
59 } |
|
60 } |
|
61 |
|
62 void addAttribute(const string_t &value) { |
|
63 int column = attributeCounter % columnCount(); |
|
64 int row = attributeCounter / columnCount(); |
|
65 if (row >= records.size()) records.append(new QVector<QVariant>(columnCount())); |
|
66 setData(index(row, column), QString::fromWCharArray(value.c_str())); |
|
67 if (column == 0) emit layoutChanged(); // FIXME: emit other signal ~ begin..., end..., rowsInserted(??? index(0,0), row, row); |
|
68 attributeCounter++; |
|
69 } |
|
70 |
|
71 Qt::ItemFlags flags(const QModelIndex &index) const { |
|
72 // TODO: not editable if not yet filled with data |
|
73 return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable; |
|
74 } |
|
75 |
|
76 }; |