src/RelpipeTableModel.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 26 May 2019 22:22:59 +0200
branchv_0
changeset 40 338f27352aaa
parent 29 0f9f7d6564cd
child 42 707a6734f364
permissions -rw-r--r--
Added tag v0.12 for changeset 4ad44f170a58
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
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     8
 * (at your option) any later version.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     9
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * GNU General Public License for more details.
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    14
 *
0f9f7d6564cd license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    15
 * 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
    16
 * 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
    17
 */
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#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
    19
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#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
    21
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/QAbstractTableModel>
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/QHash>
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/QRect>
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/QVector>
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 <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
    27
#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
    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
#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
    30
#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
    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
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
    33
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
    34
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
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
    36
	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
    37
private:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	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
    39
	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
    40
	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
    41
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    42
	/**
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    43
	 * @param index
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    44
	 * @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
    45
	 */
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    46
	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
    47
		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
    48
	}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	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
    55
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	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
    59
		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
    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
	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
    63
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	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
    67
		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
    68
		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
    69
		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
    70
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	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
    74
		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
    75
		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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	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
    79
		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
    80
			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
    81
			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
    82
			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
    83
		} else {
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			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
    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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	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
    89
		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
    90
		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
    91
		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
    92
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    93
		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
    94
		QVariant valueVariant;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    95
		boolean_t conversionOk = false;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    96
		if (attributes[column].getTypeId() == TypeId::INTEGER) {
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    97
			valueVariant = valueString.toUInt(&conversionOk);
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    98
		}
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    99
		// TODO: boolean → checkboxes
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   100
		// 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
   101
		//	valueVariant = L"true" == value;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   102
		//	conversionOk = true;
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
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   105
		if (!conversionOk) valueVariant = valueString;
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   106
97847181b724 table: integer data type support
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   107
		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
   108
		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
   109
		attributeCounter++;
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
	}
28
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   111
	
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   112
	TypeId attributeType(int section) {
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   113
		return attributes[section].getTypeId();
04f1ac8a931b Qt Charts: first working version
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   114
	}
23
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	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
   117
		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
   118
			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
   119
		} else {
da2e3739b197 table cells are not editable if not yet loaded
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   120
			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
   121
		}
23
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
ff4a1c07a481 use our own QAbstractTableModel and a QTableView instead of the QTableWidget
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
};