diff --git a/pglab/BaseTableModel.cpp b/pglab/BaseTableModel.cpp index 023cd30..536673f 100644 --- a/pglab/BaseTableModel.cpp +++ b/pglab/BaseTableModel.cpp @@ -1,4 +1,5 @@ #include "BaseTableModel.h" +#include "CustomDataRole.h" #include "ResultTableModelUtil.h" #include #include "Pgsql_oids.h" @@ -11,23 +12,17 @@ QVariant BaseTableModel::data(const QModelIndex &index, int role) const Oid oid = getType(index.column()); if (role == Qt::DisplayRole) { v = getData(index); - if (oid == bool_oid) { - v = FormatBoolForDisplay(v.toBool()); - } } - else if (role == Qt::TextAlignmentRole) { - v = (int)GetDefaultAlignmentForType(oid); - } - else if (role == Qt::ForegroundRole) { - if (oid == bool_oid) { - QVariant d = getData(index); - if (d.type() == QVariant::Bool) { - v = QBrush(GetDefaultBoolColor(d.toBool())); - } - } - else { - v = QBrush(GetDefaultColorForType(oid)); - } - } - return v; + else if (role == CustomDataTypeRole) { + v = oid; + } + else if (role == CustomDataMeaningRole) { + v = getDataMeaning(index); + } + return v; +} + +QVariant BaseTableModel::getDataMeaning(const QModelIndex &index) const +{ + return static_cast(DataMeaningNormal); } diff --git a/pglab/BaseTableModel.h b/pglab/BaseTableModel.h index cd18330..285ed35 100644 --- a/pglab/BaseTableModel.h +++ b/pglab/BaseTableModel.h @@ -16,6 +16,7 @@ public: protected: virtual Oid getType(int column) const = 0; virtual QVariant getData(const QModelIndex &index) const = 0; + virtual QVariant getDataMeaning(const QModelIndex &index) const; }; diff --git a/pglab/DatabasesTableModel.cpp b/pglab/DatabasesTableModel.cpp index dc48ae4..1b932ff 100644 --- a/pglab/DatabasesTableModel.cpp +++ b/pglab/DatabasesTableModel.cpp @@ -1,4 +1,6 @@ #include "DatabasesTableModel.h" +#include "CustomDataRole.h" +#include "ScopeGuard.h" #include "catalog/PgDatabaseCatalog.h" #include "catalog/PgDatabaseContainer.h" #include "catalog/PgAuthIdContainer.h" @@ -14,9 +16,10 @@ DatabasesTableModel::DatabasesTableModel(QObject *parent) void DatabasesTableModel::setDatabaseList(std::shared_ptr cat) { beginResetModel(); + SCOPE_EXIT { endResetModel(); }; + m_catalog = cat; m_databases = cat->databases(); - endResetModel(); } QVariant DatabasesTableModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -90,13 +93,13 @@ Oid DatabasesTableModel::getType(int column) const switch (column) { case AllowConnCol: case IsTemplateCol: - oid = bool_oid; + oid = Pgsql::bool_oid; break; case ConnLimitCol: - oid = int4_oid; + oid = Pgsql::int4_oid; break; case SizeCol: - oid = int8_oid; + oid = Pgsql::int8_oid; break; case AclCol: case CollateCol: @@ -106,10 +109,10 @@ Oid DatabasesTableModel::getType(int column) const case NameCol: case TablespaceCol: case CommentCol: - oid = varchar_oid; + oid = Pgsql::varchar_oid; break; default: - oid = InvalidOid; + oid = InvalidOid; } return oid; } @@ -163,3 +166,11 @@ QVariant DatabasesTableModel::getData(const QModelIndex &index) const } return v; } + +QVariant DatabasesTableModel::getDataMeaning(const QModelIndex &index) const +{ + if (index.column() == SizeCol) + return static_cast(DataMeaningBytes); + + return BaseTableModel::getDataMeaning(index); +} diff --git a/pglab/DatabasesTableModel.h b/pglab/DatabasesTableModel.h index 5f44c66..7e179fb 100644 --- a/pglab/DatabasesTableModel.h +++ b/pglab/DatabasesTableModel.h @@ -35,6 +35,8 @@ public: virtual Oid getType(int column) const override; virtual QVariant getData(const QModelIndex &index) const override; +protected: + virtual QVariant getDataMeaning(const QModelIndex &index) const override; private: std::shared_ptr m_catalog; std::shared_ptr m_databases; diff --git a/pglab/PgLabItemDelegate.cpp b/pglab/PgLabItemDelegate.cpp index 39c0de2..a14b9d5 100644 --- a/pglab/PgLabItemDelegate.cpp +++ b/pglab/PgLabItemDelegate.cpp @@ -103,9 +103,13 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option, : GetDefaultColorForType(oid); option->features |= QStyleOptionViewItem::HasDisplay; - if (oid == Pgsql::bool_oid) - option->text = FormatBoolForDisplay(value.toBool()); + if (oid == Pgsql::bool_oid) { + bool b = value.toBool(); + forground_color = GetDefaultBoolColor(b); + option->text = FormatBoolForDisplay(b); + } else { + forground_color = GetDefaultColorForType(oid); if (meaning == DataMeaningBytes) { QString suffix; auto s = value.toLongLong(); @@ -140,9 +144,9 @@ void PgLabItemDelegate::initStyleOption(QStyleOptionViewItem *option, // option->text = ((f > 0) ? s.left(f) : s).toString(); option->text = s; } - option->palette.setBrush(QPalette::Text, QBrush(forground_color)); } - } + option->palette.setBrush(QPalette::Text, QBrush(forground_color)); + } else { option->palette.setBrush(QPalette::Text, QBrush(GetDefaultNullColor())); diff --git a/pglab/ResultTableModelUtil.cpp b/pglab/ResultTableModelUtil.cpp index cedb21c..998101c 100644 --- a/pglab/ResultTableModelUtil.cpp +++ b/pglab/ResultTableModelUtil.cpp @@ -54,7 +54,10 @@ QColor GetDefaultColorForType(Oid o) QString FormatBoolForDisplay(bool v) { - return v ? "TRUE" : "FALSE"; + static QString t(QChar(0x2713)); + static QString f(QChar(0x2717)); + + return v ? t : f; } diff --git a/pglab/ServerWindow.cpp b/pglab/ServerWindow.cpp index aa4767f..49ba67c 100644 --- a/pglab/ServerWindow.cpp +++ b/pglab/ServerWindow.cpp @@ -15,15 +15,16 @@ ServerWindow::ServerWindow(MasterController *master, QWidget *parent) ui->setupUi(this); m_databasesModel = new DatabasesTableModel(this); - auto databasesSortFilter = new QSortFilterProxyModel(this); databasesSortFilter->setSourceModel(m_databasesModel); - ui->databasesTableView->setModel(databasesSortFilter); ui->databasesTableView->setSortingEnabled(true); m_rolesModel = new RolesTableModel(this); - ui->rolesTableView->setModel(m_rolesModel); + auto rolesSortFilter = new QSortFilterProxyModel(this); + rolesSortFilter->setSourceModel(m_rolesModel); + ui->rolesTableView->setModel(rolesSortFilter); + ui->rolesTableView->setSortingEnabled(true); } ServerWindow::~ServerWindow() @@ -39,7 +40,10 @@ void ServerWindow::setConfig(const ConnectionConfig &config) auto cat = m_database->catalog(); if (cat) { m_databasesModel->setDatabaseList(cat); + ui->databasesTableView->resizeColumnsToContents(); + m_rolesModel->setRoleList(cat->authIds()); + ui->rolesTableView->resizeColumnsToContents(); } } catch (const OpenDatabaseException &ex) { diff --git a/pglab/ServerWindow.ui b/pglab/ServerWindow.ui index 42988d9..6b3d1db 100644 --- a/pglab/ServerWindow.ui +++ b/pglab/ServerWindow.ui @@ -50,16 +50,16 @@ 0 - + true - - 20 - 16 + + 20 + @@ -86,12 +86,12 @@ true - - 20 - 16 + + 20 + @@ -114,16 +114,16 @@ 0 - + true - - 20 - 16 + + 20 + @@ -138,12 +138,19 @@ 0 0 800 - 23 + 30 + + + PgLabTableView + QTableView +
PgLabTableView.h
+
+