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

View file

@ -3,6 +3,9 @@
#include <QTableWidget>
#include <QSortFilterProxyModel>
#include "PgLabTableView.h"
#include <optional>
class PgDatabaseCatalog;
template <typename TableModel>
class PgLabTableViewHelper {
@ -33,6 +36,36 @@ public:
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:
PgLabTableView *m_tableView = nullptr;
TableModel *m_dataModel = nullptr;

View file

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

View file

@ -14,6 +14,7 @@ class PgDatabaseCatalog;
class TablesTableModel: public QAbstractTableModel {
public:
using RowItem = PgClass;
enum e_Columns : int {
NameCol, ///< either table, ns.table or table (ns) depending on settings/filters
NamespaceCol,
@ -47,6 +48,10 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const override;
PgClass getTable(int row) const;
RowItem rowItem(int row) const
{
return getTable(row);
}
Oid getTableOid(int row) const;
private:

View file

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

View file

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

View file

@ -30,10 +30,6 @@ void ServerInspector::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
m_catalog = cat;
m_databasesPage->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 )

View file

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

View file

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