use our own QAbstractTableModel and a QTableView instead of the QTableWidget v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 09 Oct 2018 23:02:11 +0200
branchv_0
changeset 23 ff4a1c07a481
parent 22 0f0344e1ba61
child 24 75f8fd148f06
use our own QAbstractTableModel and a QTableView instead of the QTableWidget
nbproject/configurations.xml
src/CMakeLists.txt
src/RelpipeChartMainWindow.cpp
src/RelpipeChartMainWindow.h
src/RelpipeTableModel.h
--- a/nbproject/configurations.xml	Mon Oct 08 14:08:21 2018 +0200
+++ b/nbproject/configurations.xml	Tue Oct 09 23:02:11 2018 +0200
@@ -52,6 +52,7 @@
         <in>RelpipeChartMainWindow.cpp</in>
         <in>RelpipeChartMainWindow.h</in>
         <in>RelpipeChartMainWindow.ui</in>
+        <in>RelpipeTableModel.h</in>
         <in>relpipe-out-chart.cpp</in>
       </df>
     </df>
@@ -62,6 +63,7 @@
       <logicalFolder name="EWIEGA46WW" displayName="EWIEGA46WW" projectFiles="true">
         <itemPath>build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_QtRelationalReaderStringHadler.cpp</itemPath>
         <itemPath>build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_RelpipeChartMainWindow.cpp</itemPath>
+        <itemPath>build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_RelpipeTableModel.cpp</itemPath>
       </logicalFolder>
       <logicalFolder name="include" displayName="include" projectFiles="true">
         <itemPath>build/Debug/src/relpipe-out-chart_autogen/include/ui_RelpipeChartMainWindow.h</itemPath>
@@ -143,6 +145,11 @@
             tool="3"
             flavor2="0">
       </item>
+      <item path="build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_RelpipeTableModel.cpp"
+            ex="true"
+            tool="3"
+            flavor2="0">
+      </item>
       <item path="build/Debug/src/relpipe-out-chart_autogen/mocs_compilation.cpp"
             ex="false"
             tool="1"
@@ -201,6 +208,8 @@
       </item>
       <item path="src/RelpipeChartMainWindow.ui" ex="false" tool="3" flavor2="0">
       </item>
+      <item path="src/RelpipeTableModel.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-out-chart.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
--- a/src/CMakeLists.txt	Mon Oct 08 14:08:21 2018 +0200
+++ b/src/CMakeLists.txt	Tue Oct 09 23:02:11 2018 +0200
@@ -22,6 +22,7 @@
 add_executable(
 	${EXECUTABLE_FILE}
 	QtRelationalReaderStringHadler.h # QObjects must be listed here (including them from other files is not enough)
+	RelpipeTableModel.h
 	RelpipeChartMainWindow.ui
 	RelpipeChartMainWindow.cpp
 	relpipe-out-chart.cpp
--- a/src/RelpipeChartMainWindow.cpp	Mon Oct 08 14:08:21 2018 +0200
+++ b/src/RelpipeChartMainWindow.cpp	Tue Oct 09 23:02:11 2018 +0200
@@ -1,10 +1,8 @@
-#include <QTabWidget>
+#include <vector>
+
 #include <QPushButton>
-#include <QLabel>
-#include <QStatusBar>
 #include <QSplitter>
-#include <QScrollArea>
-#include <QTableWidget>
+#include <QTableView>
 
 #include <QChart>
 #include <QChartView>
@@ -16,6 +14,7 @@
 
 using namespace relpipe::reader;
 using namespace relpipe::reader::handlers;
+
 QT_CHARTS_USE_NAMESPACE
 
 RelpipeChartMainWindow::RelpipeChartMainWindow() {
@@ -32,32 +31,23 @@
 
 void RelpipeChartMainWindow::startRelation(const string_t name, std::vector<AttributeMetadata> attributes) {
 	setStatusMessage(L"Reading relation: " + name);
-	attributeCounter = 0;
 	QSplitter* splitter = new QSplitter(Qt::Orientation::Vertical, tabs);
 
-	currentTable = new QTableWidget(0, attributes.size(), splitter);
-	QStringList headers;
-	for (AttributeMetadata a : attributes) headers << QString::fromWCharArray(a.getAttributeName().c_str());
-	// for (AttributeMetadata a : attributes) headers << (QString::fromWCharArray(a.getAttributeName().c_str()) + " (" + QString::fromWCharArray(a.getTypeName().c_str()) + ")" );
-	currentTable->setHorizontalHeaderLabels(headers);
-	currentTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
-
+	currentModel = new RelpipeTableModel(attributes, this);
+	QTableView* tableView = new QTableView(this);
+	tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
+	tableView->setModel(currentModel);
+	
 	// TODO: chart
 	splitter->addWidget(new QPushButton("here will be the chart", splitter));
-	splitter->addWidget(currentTable);
+	splitter->addWidget(tableView);
 	int index = tabs->addTab(splitter, QString::fromWCharArray(name.c_str()));
 	if (tabs->count() == 2) tabs->setCurrentIndex(index); // switch to the first relation (first tab is Options tab)
 	tabs->setTabIcon(index, QIcon::fromTheme("application-vnd.oasis.opendocument.spreadsheet"));
 }
 
 void RelpipeChartMainWindow::attribute(const string_t value) {
-	// TODO: draw chart
-	integer_t column = attributeCounter % currentTable->columnCount();
-	integer_t row = attributeCounter / currentTable->columnCount();
-	if (row >= currentTable->rowCount()) currentTable->insertRow(currentTable->rowCount());
-	QString name = QString::fromWCharArray(value.c_str());
-	currentTable->setItem(row, column, new QTableWidgetItem(name));
-	attributeCounter++;
+	currentModel->addAttribute(value);
 }
 
 void RelpipeChartMainWindow::setStatusMessage(string_t message) {
--- a/src/RelpipeChartMainWindow.h	Mon Oct 08 14:08:21 2018 +0200
+++ b/src/RelpipeChartMainWindow.h	Tue Oct 09 23:02:11 2018 +0200
@@ -10,6 +10,8 @@
 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
 #include <relpipe/reader/handlers/AttributeMetadata.h>
 
+#include "RelpipeTableModel.h"
+
 using namespace relpipe::reader;
 using namespace relpipe::reader::handlers;
 
@@ -26,7 +28,6 @@
 private:
 	Ui::RelpipeChartMainWindow widget;
 	QTabWidget* tabs = new QTabWidget(this);
-	QTableWidget* currentTable;
+	RelpipeTableModel* currentModel;
 	QLabel* status = new QLabel();
-	integer_t attributeCounter;
 };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/RelpipeTableModel.h	Tue Oct 09 23:02:11 2018 +0200
@@ -0,0 +1,76 @@
+#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;
+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 (role != Qt::ItemDataRole::DisplayRole) return QVariant();
+		else if (orientation == Qt::Orientation::Horizontal) return QString::fromStdWString(attributes[section].getAttributeName().c_str());
+		else if (orientation == Qt::Orientation::Vertical) return QString("%1").arg(section + 1);
+	}
+
+	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()));
+		setData(index(row, column), QString::fromWCharArray(value.c_str()));
+		if (column == 0) emit layoutChanged(); // FIXME: emit other signal ~ begin..., end..., rowsInserted(???  index(0,0), row, row);
+		attributeCounter++;
+	}
+
+	Qt::ItemFlags flags(const QModelIndex &index) const {
+		// TODO: not editable if not yet filled with data
+		return QAbstractItemModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
+	}
+
+};