src/RelpipeTableModel.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 31 Jan 2020 23:34:52 +0100
branchv_0
changeset 44 334a114407cd
parent 42 707a6734f364
permissions -rw-r--r--
Added tag v0.15 for changeset f3867ada29b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     1
/**
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     2
 * Relational pipes
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     4
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
42
707a6734f364 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
29
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     8
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    12
 * GNU General Public License for more details.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    13
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    14
 * You should have received a copy of the GNU General Public License
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    16
 */
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#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
    18
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#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
    20
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#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
    22
#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
    23
#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
    24
#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
    25
#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
    26
#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
    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
#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
    29
#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
    30
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
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
    32
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
    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
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
    35
	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
    36
private:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	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
    38
	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
    39
	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
    40
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    41
	/**
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    42
	 * @param index
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    43
	 * @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
    44
	 */
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    45
	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
    46
		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
    47
	}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	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
    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
	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
    54
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	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
    58
		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
    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
	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
    62
		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
    63
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	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
    66
		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
    67
		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
    68
		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
    69
		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
    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
	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
    73
		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
    74
		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
    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
	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
    78
		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
    79
			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
    80
			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
    81
			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
    82
		} else {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			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
    84
		}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
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
	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
    88
		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
    89
		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
    90
		if (row >= records.size()) records.append(new QVector<QVariant>(columnCount()));
26
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    91
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    92
		QString valueString = QString::fromWCharArray(value.c_str());
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    93
		QVariant valueVariant;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    94
		boolean_t conversionOk = false;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    95
		if (attributes[column].getTypeId() == TypeId::INTEGER) {
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    96
			valueVariant = valueString.toUInt(&conversionOk);
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    97
		}
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    98
		// TODO: boolean → checkboxes
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    99
		// else if (attributes[column].getTypeId() == TypeId::BOOLEAN) {
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   100
		//	valueVariant = L"true" == value;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   101
		//	conversionOk = true;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   102
		// }
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   103
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   104
		if (!conversionOk) valueVariant = valueString;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   105
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   106
		setData(index(row, column), valueVariant);
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		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
   108
		attributeCounter++;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	}
28
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   110
	
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   111
	TypeId attributeType(int section) {
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   112
		return attributes[section].getTypeId();
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   113
	}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	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
   116
		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
   117
			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
   118
		} else {
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   119
			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
   120
		}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	}
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
};