src/RelpipeTableModel.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 09 Oct 2018 23:22:05 +0200
branchv_0
changeset 25 da2e3739b197
parent 24 75f8fd148f06
child 26 97847181b724
permissions -rw-r--r--
table cells are not editable if not yet loaded

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

	/**
	 * @param index
	 * @return whether data at this row/clumn are already loaded (last row might contain columns that are still waiting to be filled)
	 */
	boolean_t isFilled(const QModelIndex &index) const {
		return (index.row() * columnCount() + index.column()) <= attributeCounter;
	}
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 {
		if (isFilled(index)) {
			return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
		} else {
			return QAbstractItemModel::flags(index);
		}
	}

};