src/RelpipeTableModel.h
branchv_0
changeset 23 ff4a1c07a481
child 24 75f8fd148f06
--- /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 <vector>
+
+#include <QtCore/QAbstractTableModel>
+#include <QtCore/QHash>
+#include <QtCore/QRect>
+#include <QtCore/QVector>
+#include <QtCore/QTime>
+#include <QtGui/QColor>
+
+#include <relpipe/reader/typedefs.h>
+#include <relpipe/reader/handlers/AttributeMetadata.h>
+
+using namespace relpipe::reader;
+using namespace relpipe::reader::handlers;
+
+class RelpipeTableModel : public QAbstractTableModel {
+	Q_OBJECT
+private:
+	std::vector<AttributeMetadata> attributes;
+	QList<QVector<QVariant> * > records;
+	int attributeCounter = 0;
+public:
+
+	RelpipeTableModel(std::vector<AttributeMetadata> 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<QVariant>(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;
+	}
+
+};