From a543ccb021fba068c9349eb3a2dc0b326a486496 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 15 Jan 2018 13:30:30 +0100 Subject: [PATCH] CrudTab uses new PgLabItemDelegate, no need anymore for CrudModel to inherit BaseTableModel. --- pglab/CrudModel.cpp | 15 +++++-------- pglab/CrudModel.h | 54 ++++++++++++++++++++++++++++++++++++++++----- pglab/CrudTab.cpp | 5 +++++ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/pglab/CrudModel.cpp b/pglab/CrudModel.cpp index 997ee1e..6605d22 100644 --- a/pglab/CrudModel.cpp +++ b/pglab/CrudModel.cpp @@ -108,17 +108,14 @@ QVariant CrudModel::getData(const QModelIndex &index) const QVariant CrudModel::data(const QModelIndex &index, int role) const { - if (role == Qt::ForegroundRole) { - int rij = index.row(); - int col = index.column(); - if (m_roData->null(col, rij)) { - return QBrush(Qt::gray); - } + QVariant v; + if (role == Qt::EditRole || role == Qt::DisplayRole) { + v = getData(index); } - else if (role == Qt::EditRole) { - return getData(index); + else if (role == Qt::UserRole) { + v = getType(index.column()); } - return BaseTableModel::data(index, role); + return v; } diff --git a/pglab/CrudModel.h b/pglab/CrudModel.h index f04c705..53d45ae 100644 --- a/pglab/CrudModel.h +++ b/pglab/CrudModel.h @@ -1,11 +1,12 @@ #ifndef CRUDMODEL_H #define CRUDMODEL_H -#include "BaseTableModel.h" +#include #include "ASyncDBConnection.h" #include "PgClass.h" #include "PgConstraint.h" #include "Pgsql_Connection.h" +#include #include #include #include @@ -32,7 +33,7 @@ class ASyncWindow; * std::string has short string optimization so this probably means * that a two dimensional array of std::string objects could actually be quite efficient! */ -class CrudModel: public BaseTableModel { +class CrudModel: public QAbstractTableModel { Q_OBJECT public: explicit CrudModel(ASyncWindow *async_win); @@ -50,9 +51,6 @@ public: virtual QVariant data(const QModelIndex &index, int role) const override; virtual Qt::ItemFlags flags(const QModelIndex &) const override; -protected: - virtual Oid getType(int column) const override; - virtual QVariant getData(const QModelIndex &index) const override; private: ASyncWindow * m_asyncWindow; @@ -77,6 +75,52 @@ private: // std::shared_ptr resultToRowList(std::shared_ptr result); + using Value = boost::optional; + + /** Remembers the values that have been applies to a row after the original result was retrieved. + */ + class ModifiedRow { + public: + + private: + std::vector m_values; + }; + using ModifiedRowList = std::map; + + /** Manages the changes for the current row + * + */ + class PendingChanges { + public: + + private: + int m_currentRow; + std::vector m_currentPKeyValues; ///< The values to use for updating the row, for new rows this list is empty + std::map m_values; ///< values that need to be applied + }; + + using DeletedList = std::vector; // These are the original indexes before there were any modifications + + PendingChanges m_pendingChanges; + /** Maintains a list of all modified rows. + * + * The key values are the indexes of the row before any rows were deleted. + */ + ModifiedRowList m_modifiedRowList; + + + // Alternative, vector of rows + class Row { + public: + int resultRow; ///< row index in original result, -1 for new rows + std::vector currentValues; /// + + }; + + + QVariant getData(const QModelIndex &index) const; + Oid getType(int column) const; + private slots: void connectionStateChanged(ASyncDBConnection::State state); diff --git a/pglab/CrudTab.cpp b/pglab/CrudTab.cpp index 21eb054..46a8ba9 100644 --- a/pglab/CrudTab.cpp +++ b/pglab/CrudTab.cpp @@ -3,6 +3,7 @@ #include "CrudModel.h" #include "MainWindow.h" #include "ResultTableModelUtil.h" +#include "PgLabItemDelegate.h" CrudTab::CrudTab(MainWindow *parent) @@ -13,6 +14,10 @@ CrudTab::CrudTab(MainWindow *parent) ui->setupUi(this); SetTableViewDefault(ui->tableView); + + auto delegate = new PgLabItemDelegate(ui->tableView); + ui->tableView->setItemDelegate(delegate); + m_crudModel = new CrudModel(parent); ui->tableView->setModel(m_crudModel);