diff --git a/pglab/crud/CrudModel.cpp b/pglab/crud/CrudModel.cpp index 7ef5cac..5439e50 100644 --- a/pglab/crud/CrudModel.cpp +++ b/pglab/crud/CrudModel.cpp @@ -49,16 +49,20 @@ void CrudModel::setConfig(std::shared_ptr db, const PgClass &table QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const { + if (section == 0) + return tr("#"); + + int column = section - PreColumnCount; QVariant r; if (role == Qt::DisplayRole) { if (orientation == Qt::Horizontal) { - QString s(m_roData->getColName(section)); + QString s(m_roData->getColName(column)); s += "\n"; - s += getTypeDisplayString(*m_database->catalog(), getType(section)); + s += getTypeDisplayString(*m_database->catalog(), getType(column)); r = s; } else { - r = QString::number(section + 1); + r = QString::number(column + 1); } } return r; @@ -72,7 +76,7 @@ int CrudModel::rowCount(const QModelIndex &/*parent*/) const int CrudModel::columnCount(const QModelIndex &/*parent*/) const { int col_count = m_roData ? m_roData->cols() : 0; - return col_count; + return PreColumnCount + col_count; } Oid CrudModel::getType(int column) const @@ -167,7 +171,17 @@ QVariant StringToVariant(const std::string &str, Oid oid) QVariant CrudModel::data(const QModelIndex &index, int role) const { - Oid typ = getType(index.column()); + if (index.column() < PreColumnCount) + { + if (role == Qt::DisplayRole || role == CustomSortRole) + return index.row(); + else if (role == CustomDataTypeRole) + return Pgsql::int4_oid; + + return {}; + } + + Oid typ = getType(index.column() - PreColumnCount); if (role == Qt::EditRole || role == Qt::DisplayRole) { auto value = getLatestData(index); @@ -192,9 +206,8 @@ QVariant CrudModel::data(const QModelIndex &index, int role) const else if (role == CustomSortRole) { auto value = getLatestData(index); - if (value) { + if (value) return StringToVariant(value->c_str(), typ); - } } return {}; @@ -258,9 +271,12 @@ void CrudModel::connectionStateChanged(ASyncDBConnection::State state) } } -Qt::ItemFlags CrudModel::flags(const QModelIndex &) const +Qt::ItemFlags CrudModel::flags(const QModelIndex &index) const { - Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; + Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; + if (index.column() < PreColumnCount) + return flags; + if (m_primaryKey) { flags |= Qt::ItemIsEditable; } @@ -269,9 +285,12 @@ Qt::ItemFlags CrudModel::flags(const QModelIndex &) const bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if (index.column() < PreColumnCount) + return false; + if (role == Qt::EditRole) { int grid_row = index.row(); - int col = index.column(); + int col = index.column() - PreColumnCount; auto& row_mapping = m_rowMapping[grid_row]; row_mapping.pending = true; diff --git a/pglab/crud/CrudModel.h b/pglab/crud/CrudModel.h index 92e2d58..cd6bafb 100644 --- a/pglab/crud/CrudModel.h +++ b/pglab/crud/CrudModel.h @@ -51,6 +51,8 @@ public slots: private: using PKeyValues = std::vector; + const int PreColumnCount = 1; + class ColumnSort { public: @@ -220,7 +222,7 @@ private: Value getLatestData(int column, int row) const; Value getLatestData(const QModelIndex &index) const { - return getLatestData(index.column(), index.row()); + return getLatestData(index.column() - PreColumnCount, index.row()); } Value getSavedData(int columnIndex, int rowIndex) const; Value getSavedData(const RowMapping &row_mapping, int columnIndex) const; diff --git a/pglab/crud/CrudTab.ui b/pglab/crud/CrudTab.ui index 26d29d4..8c58abd 100644 --- a/pglab/crud/CrudTab.ui +++ b/pglab/crud/CrudTab.ui @@ -15,7 +15,11 @@ - + + + false + +