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
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;
25
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    24
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    25
	/**
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    26
	 * @param index
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    27
	 * @return whether data at this row/clumn are already loaded (last row might contain columns that are still waiting to be filled)
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    28
	 */
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    29
	boolean_t isFilled(const QModelIndex &index) const {
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    30
		return (index.row() * columnCount() + index.column()) <= attributeCounter;
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    31
	}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
public:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	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
    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
	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
    38
		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
    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
	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
    42
		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
    43
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	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
    46
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	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
    50
		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
    51
		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
    52
		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
    53
		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
    54
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	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
    57
		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
    58
		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
    59
	}
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
	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
    62
		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
    63
			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
    64
			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
    65
			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
    66
		} else {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			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
    68
		}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
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
	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
    72
		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
    73
		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
    74
		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
    75
		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
    76
		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
    77
		attributeCounter++;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	Qt::ItemFlags flags(const QModelIndex &index) const {
25
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    81
		if (isFilled(index)) {
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    82
			return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    83
		} else {
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    84
			return QAbstractItemModel::flags(index);
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    85
		}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
};