Switched TablesTableModel to rely on the PgLabDelegate for formatting instead of the old model base class.

This commit is contained in:
eelke 2018-08-27 21:12:27 +02:00
parent a4054ed789
commit 7630723b69
3 changed files with 23 additions and 32 deletions

View file

@ -24,9 +24,13 @@ TablesPage::TablesPage(MainWindow *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
auto pglab_delegate = new PgLabItemDelegate(this);
auto icon_delegate = new IconColumnDelegate(this);
SetTableViewDefault(ui->tableListTable); SetTableViewDefault(ui->tableListTable);
m_tablesModel = new TablesTableModel(this); m_tablesModel = new TablesTableModel(this);
ui->tableListTable->setModel(m_tablesModel); ui->tableListTable->setModel(m_tablesModel);
ui->tableListTable->setItemDelegate(pglab_delegate);
ui->tableListTable->setSortingEnabled(true); ui->tableListTable->setSortingEnabled(true);
ui->tableListTable->sortByColumn(0, Qt::AscendingOrder); ui->tableListTable->sortByColumn(0, Qt::AscendingOrder);
@ -36,9 +40,8 @@ TablesPage::TablesPage(MainWindow *parent)
SetTableViewDefault(ui->constraintsTable); SetTableViewDefault(ui->constraintsTable);
m_constraintModel = new ConstraintModel(this); m_constraintModel = new ConstraintModel(this);
auto delegate = new IconColumnDelegate(this);
ui->constraintsTable->setModel(m_constraintModel); ui->constraintsTable->setModel(m_constraintModel);
ui->constraintsTable->setItemDelegateForColumn(0, delegate); ui->constraintsTable->setItemDelegateForColumn(0, icon_delegate);
QFont font; QFont font;
font.setFamily("Source Code Pro"); font.setFamily("Source Code Pro");
@ -50,13 +53,14 @@ TablesPage::TablesPage(MainWindow *parent)
SetTableViewDefault(ui->indexesTable); SetTableViewDefault(ui->indexesTable);
m_indexModel = new IndexModel(this); m_indexModel = new IndexModel(this);
ui->indexesTable->setModel(m_indexModel); ui->indexesTable->setModel(m_indexModel);
ui->indexesTable->setItemDelegate(new PgLabItemDelegate(ui->indexesTable)); ui->indexesTable->setItemDelegate(pglab_delegate);
ui->indexesTable->setItemDelegateForColumn(0, delegate); ui->indexesTable->setItemDelegateForColumn(0, icon_delegate);
PropertyProxyModel* property_model = new PropertyProxyModel(this); PropertyProxyModel* property_model = new PropertyProxyModel(this);
property_model->setSourceModel(m_tablesModel); property_model->setSourceModel(m_tablesModel);
SetTableViewDefault(ui->tablePropertiesTable); SetTableViewDefault(ui->tablePropertiesTable);
ui->tablePropertiesTable->setModel(property_model); ui->tablePropertiesTable->setModel(property_model);
ui->tablePropertiesTable->setItemDelegate(pglab_delegate);
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentChanged, connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentChanged,
[property_model](const QModelIndex &current, const QModelIndex &) { [property_model](const QModelIndex &current, const QModelIndex &) {

View file

@ -5,13 +5,12 @@
#include "PgNamespace.h" #include "PgNamespace.h"
#include "PgNamespaceContainer.h" #include "PgNamespaceContainer.h"
#include "Pgsql_declare.h" #include "Pgsql_declare.h"
#include "CustomDataRole.h"
#include <QBrush> #include <QBrush>
TablesTableModel::TablesTableModel(QObject *parent) TablesTableModel::TablesTableModel(QObject *parent)
: BaseTableModel(parent) : QAbstractTableModel(parent)
{ {}
}
void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat) void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
{ {
@ -166,11 +165,11 @@ QVariant TablesTableModel::getData(const QModelIndex &index) const
v = getTablespaceDisplayString(*m_catalog, t.tablespace); v = getTablespaceDisplayString(*m_catalog, t.tablespace);
break; break;
case OptionsCol: case OptionsCol:
v = t.options; //v = t.options;
break; break;
// case AclCol: // case AclCol:
// v = t.acl; // v = t.acl;
// break; // break;
} }
return v; return v;
@ -195,19 +194,9 @@ QString TablesTableModel::formatTableName(const PgClass &cls) const
QVariant TablesTableModel::data(const QModelIndex &index, int role) const QVariant TablesTableModel::data(const QModelIndex &index, int role) const
{ {
if (role == Qt::DisplayRole)
if (role == Qt::ForegroundRole) { return getData(index);
else if (role == CustomDataTypeRole)
const auto &t = m_tables[index.row()]; return getType(index.column());
auto ns = m_catalog->namespaces()->getByKey(t.relnamespace); return QVariant();
if (ns.isSystemCatalog()) {
switch (index.column()) {
case NameCol:
case NamespaceCol:
return QBrush(Qt::blue);
break;
}
}
}
return BaseTableModel::data(index, role);
} }

View file

@ -9,8 +9,7 @@
class PgClass; class PgClass;
class PgDatabaseCatalog; class PgDatabaseCatalog;
class TablesTableModel: public BaseTableModel class TablesTableModel: public QAbstractTableModel {
{
public: public:
enum e_Columns : int { enum e_Columns : int {
NameCol, ///< either table, ns.table or table (ns) depending on settings/filters NameCol, ///< either table, ns.table or table (ns) depending on settings/filters
@ -38,9 +37,6 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override; virtual QVariant data(const QModelIndex &index, int role) const override;
PgClass getTable(int row) const; PgClass getTable(int row) const;
Oid getTableOid(int row) const; Oid getTableOid(int row) const;
protected:
virtual Oid getType(int column) const override;
virtual QVariant getData(const QModelIndex &index) const override;
private: private:
using t_Tables = std::vector<PgClass>; using t_Tables = std::vector<PgClass>;
@ -48,6 +44,8 @@ private:
std::shared_ptr<const PgDatabaseCatalog> m_catalog; std::shared_ptr<const PgDatabaseCatalog> m_catalog;
t_Tables m_tables; t_Tables m_tables;
Oid getType(int column) const;
QVariant getData(const QModelIndex &index) const;
QString formatTableName(const PgClass &cls) const; QString formatTableName(const PgClass &cls) const;
void doSort(int so); void doSort(int so);
}; };