src/RelpipeTableModel.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 09 Oct 2018 23:10:00 +0200
branchv_0
changeset 24 75f8fd148f06
parent 23 ff4a1c07a481
child 25 da2e3739b197
permissions -rw-r--r--
table header tooltip: data type name

#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 (orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole) return QString::fromStdWString(attributes[section].getAttributeName().c_str());
		if (orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::ToolTipRole) return QString::fromStdWString(attributes[section].getTypeName().c_str());
		if (orientation == Qt::Orientation::Vertical && role == Qt::ItemDataRole::DisplayRole) return QString("%1").arg(section + 1);
		return QVariant();
	}

	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;
	}

};