refactor use PgLabTableViewHelper on CatalogTablesPage

This commit is contained in:
eelke 2021-12-30 18:54:26 +01:00
parent 87ab22919f
commit 90851ef950
9 changed files with 68 additions and 54 deletions

View file

@ -6,6 +6,7 @@
class PgDatabaseContainer; class PgDatabaseContainer;
class PgDatabaseCatalog; class PgDatabaseCatalog;
class PgDatabase;
/** Class for displaying the list of databases of a server in a QTableView /** Class for displaying the list of databases of a server in a QTableView
* *
@ -15,6 +16,8 @@ class DatabasesTableModel : public BaseTableModel
Q_OBJECT Q_OBJECT
public: public:
using RowItem = PgDatabase;
enum e_Columns : int { NameCol, DbaCol, EncodingCol, CollateCol, enum e_Columns : int { NameCol, DbaCol, EncodingCol, CollateCol,
CTypeCol, IsTemplateCol, AllowConnCol, ConnLimitCol, CTypeCol, IsTemplateCol, AllowConnCol, ConnLimitCol,
TablespaceCol, CommentCol, SizeCol, AclCol, COL_COUNT }; TablespaceCol, CommentCol, SizeCol, AclCol, COL_COUNT };

View file

@ -3,6 +3,9 @@
#include <QTableWidget> #include <QTableWidget>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "PgLabTableView.h" #include "PgLabTableView.h"
#include <optional>
class PgDatabaseCatalog;
template <typename TableModel> template <typename TableModel>
class PgLabTableViewHelper { class PgLabTableViewHelper {
@ -33,6 +36,36 @@ public:
return m_sortFilter; return m_sortFilter;
} }
void setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
{
m_dataModel->setCatalog(cat);
m_tableView->resizeColumnsToContents();
}
QModelIndex currentIndex() const
{
QModelIndex index = m_tableView->selectionModel()->currentIndex();
if (!index.isValid())
return index;
return m_sortFilter->mapToSource(index);
}
typename TableModel::RowItem rowItem(int row) const
{
return m_dataModel->rowItem(row);
}
std::optional<typename TableModel::RowItem> currentRowItem() const
{
auto index = currentIndex();
if (!index.isValid())
return {};
return rowItem(index.row());
}
private: private:
PgLabTableView *m_tableView = nullptr; PgLabTableView *m_tableView = nullptr;
TableModel *m_dataModel = nullptr; TableModel *m_dataModel = nullptr;

View file

@ -5,6 +5,7 @@
#include "BaseTableModel.h" #include "BaseTableModel.h"
#include <memory> #include <memory>
class PgAuthId;
class PgAuthIdContainer; class PgAuthIdContainer;
/** Class for displaying the list of roles of a server in a QTableView /** Class for displaying the list of roles of a server in a QTableView
@ -13,6 +14,7 @@ class PgAuthIdContainer;
class RolesTableModel : public BaseTableModel { class RolesTableModel : public BaseTableModel {
Q_OBJECT Q_OBJECT
public: public:
using RowItem = PgAuthId;
enum e_Columns : int { NameCol, SuperCol, InheritCol, CreateRoleCol, enum e_Columns : int { NameCol, SuperCol, InheritCol, CreateRoleCol,
CreateDBCol, CanLoginCol, ReplicationCol, CreateDBCol, CanLoginCol, ReplicationCol,
BypassRlsCol, ConnlimitCol, ValidUntilCol }; BypassRlsCol, ConnlimitCol, ValidUntilCol };

View file

@ -14,6 +14,7 @@ class PgDatabaseCatalog;
class TablesTableModel: public QAbstractTableModel { class TablesTableModel: public QAbstractTableModel {
public: public:
using RowItem = PgClass;
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
NamespaceCol, NamespaceCol,
@ -47,6 +48,10 @@ 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;
RowItem rowItem(int row) const
{
return getTable(row);
}
Oid getTableOid(int row) const; Oid getTableOid(int row) const;
private: private:

View file

@ -4,8 +4,8 @@
#include <QWidget> #include <QWidget>
#include "PgLabTableViewHelper.h" #include "PgLabTableViewHelper.h"
#include "DatabasesTableModel.h"
class DatabasesTableModel;
class PgDatabaseCatalog; class PgDatabaseCatalog;
class PgLabTableView; class PgLabTableView;
class QSortFilterProxyModel; class QSortFilterProxyModel;

View file

@ -4,8 +4,8 @@
#include <QWidget> #include <QWidget>
#include "PgLabTableViewHelper.h" #include "PgLabTableViewHelper.h"
#include "RolesTableModel.h"
class RolesTableModel;
class PgDatabaseCatalog; class PgDatabaseCatalog;
class PgLabTableView; class PgLabTableView;
class QSortFilterProxyModel; class QSortFilterProxyModel;

View file

@ -30,10 +30,6 @@ void ServerInspector::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
m_catalog = cat; m_catalog = cat;
m_databasesPage->setCatalog(cat); m_databasesPage->setCatalog(cat);
m_rolesPage->setCatalog(cat); m_rolesPage->setCatalog(cat);
// m_tablesPage->setCatalog(cat);
// m_functionsPage->setCatalog(cat);
// m_sequencesPage->setCatalog(cat);
// m_typesPage->setCatalog(cat);
} }
void ServerInspector::retranslateUi(bool ) void ServerInspector::retranslateUi(bool )

View file

@ -25,24 +25,16 @@
CatalogTablesPage::CatalogTablesPage(QWidget *parent) CatalogTablesPage::CatalogTablesPage(QWidget *parent)
: QSplitter(Qt::Horizontal, parent) : QSplitter(Qt::Horizontal, parent)
, m_tablesTableView(this)
{ {
m_tableView = new PgLabTableView(this); auto tv = m_tablesTableView.tableView();
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows); tv->setSelectionMode(QAbstractItemView::SingleSelection);
m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
m_detailsTabs = new QTabWidget(this); m_detailsTabs = new QTabWidget(this);
// Populate splitter // Populate splitter
addWidget(m_tableView); addWidget(tv);
addWidget(m_detailsTabs); addWidget(m_detailsTabs);
// Setup model(s)
m_tablesModel = new TablesTableModel(this);
m_tablesSortFilter = new QSortFilterProxyModel(this);
m_tablesSortFilter->setSourceModel(m_tablesModel);
m_tableView->setModel(m_tablesSortFilter);
m_tableView->horizontalHeader()->setSortIndicator(TablesTableModel::NameCol, Qt::AscendingOrder);
m_tableView->setSortingEnabled(true);
// - Columns page // - Columns page
m_columnsPage = new ColumnPage(this); m_columnsPage = new ColumnPage(this);
m_detailsTabs->addTab(m_columnsPage, ""); m_detailsTabs->addTab(m_columnsPage, "");
@ -56,9 +48,9 @@ CatalogTablesPage::CatalogTablesPage(QWidget *parent)
m_detailsTabs->addTab(m_indexPage, ""); m_detailsTabs->addTab(m_indexPage, "");
// - Properties page // - Properties page
m_propertiesPage = new PropertiesPage(this); // m_propertiesPage = new PropertiesPage(this);
m_propertiesPage->setSourceModel(m_tablesModel); // m_propertiesPage->setSourceModel(m_tablesTableView.dataModel());
m_detailsTabs->addTab(m_propertiesPage, ""); // m_detailsTabs->addTab(m_propertiesPage, "");
// - Trigger page // - Trigger page
m_triggerPage = new TriggerPage(this); m_triggerPage = new TriggerPage(this);
@ -75,19 +67,19 @@ CatalogTablesPage::CatalogTablesPage(QWidget *parent)
m_detailsTabs->setCurrentIndex(0); m_detailsTabs->setCurrentIndex(0);
// Signals // Signals
connect(m_tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, connect(tv->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
&CatalogTablesPage::tableListTable_currentRowChanged); &CatalogTablesPage::tableListTable_currentRowChanged);
connect(m_tablesModel, &TablesTableModel::modelReset, connect(m_tablesTableView.dataModel(), &TablesTableModel::modelReset,
[this] () [this] ()
{ {
selectedTableChanged({}); selectedTableChanged({});
m_propertiesPage->setActiveRow({}); m_propertiesPage->setActiveRow({});
}); });
connect(m_tablesModel, &QAbstractItemModel::layoutChanged, connect(m_tablesTableView.dataModel(), &QAbstractItemModel::layoutChanged,
this, &CatalogTablesPage::tableListTable_layoutChanged); this, &CatalogTablesPage::tableListTable_layoutChanged);
connect(m_tableView, &QTableView::doubleClicked, connect(tv, &QTableView::doubleClicked,
this, &CatalogTablesPage::on_tableListTable_doubleClicked); this, &CatalogTablesPage::on_tableListTable_doubleClicked);
} }
@ -109,8 +101,7 @@ void CatalogTablesPage::retranslateUi(bool /*all*/)
void CatalogTablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat) void CatalogTablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
{ {
m_catalog = cat; m_catalog = cat;
m_tablesModel->setCatalog(cat); m_tablesTableView.setCatalog(cat);
m_tableView->resizeColumnsToContents();
m_constraintPage->setCatalog(cat); m_constraintPage->setCatalog(cat);
m_indexPage->setCatalog(cat); m_indexPage->setCatalog(cat);
@ -120,44 +111,29 @@ void CatalogTablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
void CatalogTablesPage::setNamespaceFilter(NamespaceFilter filter) void CatalogTablesPage::setNamespaceFilter(NamespaceFilter filter)
{ {
m_tablesModel->setNamespaceFilter(filter); m_tablesTableView.dataModel()->setNamespaceFilter(filter);
m_tableView->resizeColumnsToContents(); m_tablesTableView.tableView()->resizeColumnsToContents();
} }
void CatalogTablesPage::tableListTable_currentRowChanged(const QModelIndex &current, const QModelIndex &previous) void CatalogTablesPage::tableListTable_currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{ {
if (current.row() != previous.row()) { if (current.row() != previous.row()) {
if (current.isValid()) { auto table = m_tablesTableView.rowItem(current.row());
auto sourceIndex = m_tablesSortFilter->mapToSource(current);
auto row = sourceIndex.row();
PgClass table = m_tablesModel->getTable(row);
selectedTableChanged(table); selectedTableChanged(table);
m_propertiesPage->setActiveRow(sourceIndex);
}
else {
selectedTableChanged({});
m_propertiesPage->setActiveRow({});
}
} }
} }
void CatalogTablesPage::tableListTable_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint ) void CatalogTablesPage::tableListTable_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint )
{ {
auto&& index = m_tableView->selectionModel()->currentIndex(); auto table = m_tablesTableView.currentRowItem();
if (index.isValid()) {
auto row = m_tablesSortFilter->mapToSource(index).row();
PgClass table = m_tablesModel->getTable(row);
selectedTableChanged(table); selectedTableChanged(table);
} }
else
selectedTableChanged({});
}
void CatalogTablesPage::on_tableListTable_doubleClicked(const QModelIndex &index) void CatalogTablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
{ {
auto row = m_tablesSortFilter->mapToSource(index).row(); auto row = m_tablesTableView.sortFilter()->mapToSource(index).row();
PgClass table = m_tablesModel->getTable(row); PgClass table = m_tablesTableView.dataModel()->getTable(row);
if (table.oid() != InvalidOid) { if (table.oid() != InvalidOid) {
tableSelected(table.oid()); tableSelected(table.oid());
} }

View file

@ -2,6 +2,8 @@
#define CATALOGTABLESPAGE_H #define CATALOGTABLESPAGE_H
#include "NamespaceFilter.h" #include "NamespaceFilter.h"
#include "TablesTableModel.h"
#include "PgLabTableViewHelper.h"
#include <QSplitter> #include <QSplitter>
#include <memory> #include <memory>
#include <optional> #include <optional>
@ -21,7 +23,6 @@ class PropertiesPage;
class QTabWidget; class QTabWidget;
class SqlCodePreview; class SqlCodePreview;
class QSortFilterProxyModel; class QSortFilterProxyModel;
class TablesTableModel;
class TriggerPage; class TriggerPage;
@ -38,9 +39,7 @@ public:
signals: signals:
void tableSelected(Oid tableoid); void tableSelected(Oid tableoid);
private: private:
PgLabTableView *m_tableView = nullptr; PgLabTableViewHelper<TablesTableModel> m_tablesTableView;
TablesTableModel* m_tablesModel = nullptr;
QSortFilterProxyModel *m_tablesSortFilter = nullptr;
// Details // Details
QTabWidget *m_detailsTabs = nullptr; QTabWidget *m_detailsTabs = nullptr;