# HG changeset patch # User František Kučera # Date 1539118931 -7200 # Node ID ff4a1c07a4815f12479756acece6de5e5b005ce5 # Parent 0f0344e1ba619bd8c4832a6620def86afe76443f use our own QAbstractTableModel and a QTableView instead of the QTableWidget diff -r 0f0344e1ba61 -r ff4a1c07a481 nbproject/configurations.xml --- 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 @@ RelpipeChartMainWindow.cpp RelpipeChartMainWindow.h RelpipeChartMainWindow.ui + RelpipeTableModel.h relpipe-out-chart.cpp @@ -62,6 +63,7 @@ build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_QtRelationalReaderStringHadler.cpp build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_RelpipeChartMainWindow.cpp + build/Debug/src/relpipe-out-chart_autogen/EWIEGA46WW/moc_RelpipeTableModel.cpp build/Debug/src/relpipe-out-chart_autogen/include/ui_RelpipeChartMainWindow.h @@ -143,6 +145,11 @@ tool="3" flavor2="0"> + + + + diff -r 0f0344e1ba61 -r ff4a1c07a481 src/CMakeLists.txt --- 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 diff -r 0f0344e1ba61 -r ff4a1c07a481 src/RelpipeChartMainWindow.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 +#include + #include -#include -#include #include -#include -#include +#include #include #include @@ -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 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) { diff -r 0f0344e1ba61 -r ff4a1c07a481 src/RelpipeChartMainWindow.h --- 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 #include +#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; }; diff -r 0f0344e1ba61 -r ff4a1c07a481 src/RelpipeTableModel.h --- /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 + +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace relpipe::reader; +using namespace relpipe::reader::handlers; + +class RelpipeTableModel : public QAbstractTableModel { + Q_OBJECT +private: + std::vector attributes; + QList * > records; + int attributeCounter = 0; +public: + + RelpipeTableModel(std::vector 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(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; + } + +};