diff --git a/pglab/TablesTableModel.cpp b/pglab/TablesTableModel.cpp index 0c4f767..bb7de12 100644 --- a/pglab/TablesTableModel.cpp +++ b/pglab/TablesTableModel.cpp @@ -1,4 +1,5 @@ #include "TablesTableModel.h" +#include "ScopeGuard.h" #include "catalog/PgDatabaseCatalog.h" #include "catalog/PgClass.h" #include "catalog/PgClassContainer.h" @@ -15,23 +16,27 @@ TablesTableModel::TablesTableModel(QObject *parent) void TablesTableModel::setNamespaceFilter(NamespaceFilter nsf) { m_namespaceFilter = nsf; - reloadData(); + refresh(); } void TablesTableModel::setCatalog(std::shared_ptr cat) { - m_catalog = cat; - reloadData(); - + if (cat != m_catalog) { + m_catalog = cat; + refreshConnection = connect(m_catalog.get(), &PgDatabaseCatalog::refreshed, + this, &TablesTableModel::refresh); + } + refresh(); } -void TablesTableModel::reloadData() +void TablesTableModel::refresh() { + beginResetModel(); + SCOPE_EXIT { endResetModel(); }; + if (!m_catalog) return; - beginResetModel(); - // Later afscheiden naar filter functie auto classes = m_catalog->classes(); @@ -59,7 +64,6 @@ void TablesTableModel::reloadData() } doSort(1); - endResetModel(); } void TablesTableModel::setSortOrder(int so) diff --git a/pglab/TablesTableModel.h b/pglab/TablesTableModel.h index 4aa1ba7..52e8814 100644 --- a/pglab/TablesTableModel.h +++ b/pglab/TablesTableModel.h @@ -49,13 +49,16 @@ private: std::shared_ptr m_catalog; NamespaceFilter m_namespaceFilter = NamespaceFilter::User; t_Tables m_tables; + QMetaObject::Connection refreshConnection; - void reloadData(); Oid getType(int column) const; QVariant getData(const QModelIndex &index) const; // QString formatTableName(const PgClass &cls) const; void doSort(int so); +private slots: + void refresh(); + }; #endif // TABLESTABLEMODEL_H diff --git a/pglab/widgets/CatalogTablesPage.cpp b/pglab/widgets/CatalogTablesPage.cpp index 1bc7460..07aebda 100644 --- a/pglab/widgets/CatalogTablesPage.cpp +++ b/pglab/widgets/CatalogTablesPage.cpp @@ -72,6 +72,12 @@ CatalogTablesPage::CatalogTablesPage(QWidget *parent) // Signals connect(m_tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &CatalogTablesPage::tableListTable_currentRowChanged); + connect(m_tablesModel, &TablesTableModel::modelReset, + [this] () + { + selectedTableChanged({}); + m_propertiesPage->setActiveRow({}); + }); connect(m_tablesModel, &QAbstractItemModel::layoutChanged, this, &CatalogTablesPage::tableListTable_layoutChanged);