diff -r 0f0344e1ba61 -r ff4a1c07a481 src/RelpipeTableModel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/RelpipeTableModel.h Tue Oct 09 23:02:11 2018 +0200 @@ -0,0 +1,76 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace relpipe::reader; +using namespace relpipe::reader::handlers; + +class RelpipeTableModel : public QAbstractTableModel { + Q_OBJECT +private: + std::vector attributes; + QList * > records; + int attributeCounter = 0; +public: + + RelpipeTableModel(std::vector attributes, QObject *parent = 0) : QAbstractTableModel(parent), attributes(attributes) { + } + + virtual ~RelpipeTableModel() { + qDeleteAll(records); + } + + int rowCount(const QModelIndex &parent = QModelIndex()) const { + return records.count(); + } + + int columnCount(const QModelIndex &parent = QModelIndex()) const { + return attributes.size(); + } + + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::ItemDataRole::DisplayRole) const { + if (role != Qt::ItemDataRole::DisplayRole) return QVariant(); + else if (orientation == Qt::Orientation::Horizontal) return QString::fromStdWString(attributes[section].getAttributeName().c_str()); + else if (orientation == Qt::Orientation::Vertical) return QString("%1").arg(section + 1); + } + + QVariant data(const QModelIndex &index, int role = Qt::ItemDataRole::DisplayRole) const { + if (role == Qt::ItemDataRole::DisplayRole || role == Qt::ItemDataRole::EditRole) return records[index.row()]->at(index.column()); + else return QVariant(); + } + + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::ItemDataRole::EditRole) { + if (index.isValid() && role == Qt::ItemDataRole::EditRole) { + records[index.row()]->replace(index.column(), value); + emit dataChanged(index, index); + return true; + } else { + return false; + } + } + + void addAttribute(const string_t &value) { + int column = attributeCounter % columnCount(); + int row = attributeCounter / columnCount(); + if (row >= records.size()) records.append(new QVector(columnCount())); + setData(index(row, column), QString::fromWCharArray(value.c_str())); + if (column == 0) emit layoutChanged(); // FIXME: emit other signal ~ begin..., end..., rowsInserted(??? index(0,0), row, row); + attributeCounter++; + } + + Qt::ItemFlags flags(const QModelIndex &index) const { + // TODO: not editable if not yet filled with data + return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable; + } + +};