Add row number column in model instead of vertical header

THis is because the column can be sorted but the header not.
This commit is contained in:
eelke 2022-04-02 13:55:24 +02:00
parent 1ab119c29a
commit f492c8f9bc
3 changed files with 37 additions and 12 deletions

View file

@ -49,16 +49,20 @@ void CrudModel::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table
QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if (section == 0)
return tr("#");
int column = section - PreColumnCount;
QVariant r; QVariant r;
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) { if (orientation == Qt::Horizontal) {
QString s(m_roData->getColName(section)); QString s(m_roData->getColName(column));
s += "\n"; s += "\n";
s += getTypeDisplayString(*m_database->catalog(), getType(section)); s += getTypeDisplayString(*m_database->catalog(), getType(column));
r = s; r = s;
} }
else { else {
r = QString::number(section + 1); r = QString::number(column + 1);
} }
} }
return r; return r;
@ -72,7 +76,7 @@ int CrudModel::rowCount(const QModelIndex &/*parent*/) const
int CrudModel::columnCount(const QModelIndex &/*parent*/) const int CrudModel::columnCount(const QModelIndex &/*parent*/) const
{ {
int col_count = m_roData ? m_roData->cols() : 0; int col_count = m_roData ? m_roData->cols() : 0;
return col_count; return PreColumnCount + col_count;
} }
Oid CrudModel::getType(int column) const 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 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) if (role == Qt::EditRole || role == Qt::DisplayRole)
{ {
auto value = getLatestData(index); auto value = getLatestData(index);
@ -192,9 +206,8 @@ QVariant CrudModel::data(const QModelIndex &index, int role) const
else if (role == CustomSortRole) else if (role == CustomSortRole)
{ {
auto value = getLatestData(index); auto value = getLatestData(index);
if (value) { if (value)
return StringToVariant(value->c_str(), typ); return StringToVariant(value->c_str(), typ);
}
} }
return {}; 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) { if (m_primaryKey) {
flags |= Qt::ItemIsEditable; 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) bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
if (index.column() < PreColumnCount)
return false;
if (role == Qt::EditRole) { if (role == Qt::EditRole) {
int grid_row = index.row(); int grid_row = index.row();
int col = index.column(); int col = index.column() - PreColumnCount;
auto& row_mapping = m_rowMapping[grid_row]; auto& row_mapping = m_rowMapping[grid_row];
row_mapping.pending = true; row_mapping.pending = true;

View file

@ -51,6 +51,8 @@ public slots:
private: private:
using PKeyValues = std::vector<std::string>; using PKeyValues = std::vector<std::string>;
const int PreColumnCount = 1;
class ColumnSort { class ColumnSort {
public: public:
@ -220,7 +222,7 @@ private:
Value getLatestData(int column, int row) const; Value getLatestData(int column, int row) const;
Value getLatestData(const QModelIndex &index) 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(int columnIndex, int rowIndex) const;
Value getSavedData(const RowMapping &row_mapping, int columnIndex) const; Value getSavedData(const RowMapping &row_mapping, int columnIndex) const;

View file

@ -15,7 +15,11 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTableView" name="tableView"/> <widget class="QTableView" name="tableView">
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item> </item>
</layout> </layout>
<action name="actionRemove_rows"> <action name="actionRemove_rows">