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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <vector>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <QtCore/QAbstractTableModel>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include <QtCore/QHash>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#include <QtCore/QRect>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
#include <QtCore/QVector>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#include <QtCore/QTime>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
#include <QtGui/QColor>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
#include <relpipe/reader/typedefs.h>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
#include <relpipe/reader/handlers/AttributeMetadata.h>
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
using namespace relpipe::reader;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
using namespace relpipe::reader::handlers;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
class RelpipeTableModel : public QAbstractTableModel {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
	Q_OBJECT
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
private:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
	std::vector<AttributeMetadata> attributes;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
	QList<QVector<QVariant> * > records;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
	int attributeCounter = 0;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
public:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
	RelpipeTableModel(std::vector<AttributeMetadata> attributes, QObject *parent = 0) : QAbstractTableModel(parent), attributes(attributes) {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	virtual ~RelpipeTableModel() {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
		qDeleteAll(records);
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	int rowCount(const QModelIndex &parent = QModelIndex()) const {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		return records.count();
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	int columnCount(const QModelIndex &parent = QModelIndex()) const {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		return attributes.size();
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::ItemDataRole::DisplayRole) const {
24
75f8fd148f06 table header tooltip: data type name
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    42
		if (orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole) return QString::fromStdWString(attributes[section].getAttributeName().c_str());
75f8fd148f06 table header tooltip: data type name
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    43
		if (orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::ToolTipRole) return QString::fromStdWString(attributes[section].getTypeName().c_str());
75f8fd148f06 table header tooltip: data type name
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    44
		if (orientation == Qt::Orientation::Vertical && role == Qt::ItemDataRole::DisplayRole) return QString("%1").arg(section + 1);
75f8fd148f06 table header tooltip: data type name
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    45
		return QVariant();
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	QVariant data(const QModelIndex &index, int role = Qt::ItemDataRole::DisplayRole) const {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		if (role == Qt::ItemDataRole::DisplayRole || role == Qt::ItemDataRole::EditRole) return records[index.row()]->at(index.column());
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		else return QVariant();
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::ItemDataRole::EditRole) {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		if (index.isValid() && role == Qt::ItemDataRole::EditRole) {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
			records[index.row()]->replace(index.column(), value);
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			emit dataChanged(index, index);
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			return true;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		} else {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			return false;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	void addAttribute(const string_t &value) {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		int column = attributeCounter % columnCount();
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		int row = attributeCounter / columnCount();
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		if (row >= records.size()) records.append(new QVector<QVariant>(columnCount()));
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		setData(index(row, column), QString::fromWCharArray(value.c_str()));
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		if (column == 0) emit layoutChanged(); // FIXME: emit other signal ~ begin..., end..., rowsInserted(???  index(0,0), row, row);
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		attributeCounter++;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	Qt::ItemFlags flags(const QModelIndex &index) const {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		// TODO: not editable if not yet filled with data
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
};