src/RelpipeTableModel.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 24 Oct 2020 00:08:18 +0200
branchv_0
changeset 51 19c6ad5cf39c
parent 42 707a6734f364
permissions -rw-r--r--
Added tag v0.17.1 for changeset 9008fd23fc17

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#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()));

		QString valueString = QString::fromWCharArray(value.c_str());
		QVariant valueVariant;
		boolean_t conversionOk = false;
		if (attributes[column].getTypeId() == TypeId::INTEGER) {
			valueVariant = valueString.toUInt(&conversionOk);
		}
		// TODO: boolean → checkboxes
		// else if (attributes[column].getTypeId() == TypeId::BOOLEAN) {
		//	valueVariant = L"true" == value;
		//	conversionOk = true;
		// }

		if (!conversionOk) valueVariant = valueString;

		setData(index(row, column), valueVariant);
		if (column == 0) emit layoutChanged(); // FIXME: emit other signal ~ begin..., end..., rowsInserted(???  index(0,0), row, row);
		attributeCounter++;
	}
	
	TypeId attributeType(int section) {
		return attributes[section].getTypeId();
	}

	Qt::ItemFlags flags(const QModelIndex &index) const {
		if (isFilled(index)) {
			return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
		} else {
			return QAbstractItemModel::flags(index);
		}
	}

};