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
{
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;