From 1df8455af54d2f3fc895f84923ef31e1bde34acd Mon Sep 17 00:00:00 2001 From: eelke Date: Wed, 20 Nov 2019 19:09:22 +0100 Subject: [PATCH] Add tab with namespaces (schema's) --- pglab/CatalogInspector.cpp | 6 ++ pglab/CatalogInspector.h | 2 + pglab/NamespaceItemModel.cpp | 89 +++++++++++++++++++++----- pglab/NamespaceItemModel.h | 22 ++++++- pglab/pglab.pro | 2 + pglab/widgets/CatalogNamespacePage.cpp | 32 +++++++++ pglab/widgets/CatalogNamespacePage.h | 36 +++++++++++ 7 files changed, 171 insertions(+), 18 deletions(-) create mode 100644 pglab/widgets/CatalogNamespacePage.cpp create mode 100644 pglab/widgets/CatalogNamespacePage.h diff --git a/pglab/CatalogInspector.cpp b/pglab/CatalogInspector.cpp index 1cfaae2..06b2320 100644 --- a/pglab/CatalogInspector.cpp +++ b/pglab/CatalogInspector.cpp @@ -2,6 +2,7 @@ #include "OpenDatabase.h" #include "UserConfiguration.h" #include "widgets/CatalogFunctionsPage.h" +#include "widgets/CatalogNamespacePage.h" #include "widgets/CatalogSequencesPage.h" #include "widgets/CatalogTablesPage.h" @@ -15,6 +16,7 @@ CatalogInspector::CatalogInspector(std::shared_ptr open_database, : QWidget(parent) { m_tabWidget = new QTabWidget(this); + m_namespacePage = new CatalogNamespacePage(this); m_tablesPage = new CatalogTablesPage(this); m_functionsPage = new CatalogFunctionsPage(this); m_sequencesPage = new CatalogSequencesPage(this); @@ -22,6 +24,7 @@ CatalogInspector::CatalogInspector(std::shared_ptr open_database, auto layout = new QVBoxLayout(this); setLayout(layout); layout->addWidget(m_tabWidget); + m_tabWidget->addTab(m_namespacePage, ""); m_tabWidget->addTab(m_tablesPage, ""); m_tabWidget->addTab(m_functionsPage, ""); m_tabWidget->addTab(m_sequencesPage, ""); @@ -34,6 +37,8 @@ void CatalogInspector::retranslateUi(bool all) { m_tablesPage->retranslateUi(all); + m_tabWidget->setTabText(m_tabWidget->indexOf(m_namespacePage), + QApplication::translate("CatalogInspector", "Schemas", nullptr)); m_tabWidget->setTabText(m_tabWidget->indexOf(m_tablesPage), QApplication::translate("CatalogInspector", "Tables", nullptr)); m_tabWidget->setTabText(m_tabWidget->indexOf(m_functionsPage), @@ -49,6 +54,7 @@ CatalogInspector::~CatalogInspector() void CatalogInspector::setCatalog(std::shared_ptr cat) { m_catalog = cat; + m_namespacePage->setCatalog(cat); m_tablesPage->setCatalog(cat); m_functionsPage->setCatalog(cat); m_sequencesPage->setCatalog(cat); diff --git a/pglab/CatalogInspector.h b/pglab/CatalogInspector.h index 7b72cbd..499cf50 100644 --- a/pglab/CatalogInspector.h +++ b/pglab/CatalogInspector.h @@ -8,6 +8,7 @@ class CatalogFunctionsPage; class CatalogSequencesPage; class CatalogTablesPage; +class CatalogNamespacePage; class OpenDatabase; class PgDatabaseCatalog; class QTabWidget; @@ -24,6 +25,7 @@ public: CatalogTablesPage *tablesPage(); private: QTabWidget *m_tabWidget = nullptr; + CatalogNamespacePage *m_namespacePage = nullptr; CatalogTablesPage *m_tablesPage = nullptr; CatalogFunctionsPage *m_functionsPage = nullptr; CatalogSequencesPage *m_sequencesPage = nullptr; diff --git a/pglab/NamespaceItemModel.cpp b/pglab/NamespaceItemModel.cpp index 59e45df..e9d5962 100644 --- a/pglab/NamespaceItemModel.cpp +++ b/pglab/NamespaceItemModel.cpp @@ -13,6 +13,7 @@ namespace NamespaceItemModel_impl { virtual Qt::CheckState getCheckState() const = 0; virtual void setChecked(NamespaceItemModel *model, const QModelIndex &index, bool checked) = 0; virtual QVariant data(const QModelIndex &index, int role) const = 0; + virtual int columnCount() const = 0; }; class GroupNode; @@ -45,16 +46,29 @@ namespace NamespaceItemModel_impl { emit model->dataChanged(index.parent(), index.parent()); } - QVariant data(const QModelIndex &/*index*/, int role) const override + QVariant data(const QModelIndex &index, int role) const override { QVariant v; if (role == Qt::DisplayRole) { - v = ns.objectName(); + switch (index.column()) { + case NamespaceItemModel::ColNamespaceName: + return ns.objectName(); + case NamespaceItemModel::ColOwner: + return ns.ownerName(); + case NamespaceItemModel::ColAcl: + return ns.aclString(); + } } else if (role == Qt::CheckStateRole) { - v = getCheckState(); + if (index.column() == 0) + return getCheckState(); } - return v; + return {}; + } + + int columnCount() const override + { + return NamespaceItemModel::ColCount; } bool operator < (const LeafNode &rhs) const @@ -62,12 +76,11 @@ namespace NamespaceItemModel_impl { return ns.objectName() < rhs.ns.objectName(); } - }; class GroupNode: public Node { public: - using LeafVec = std::vector>; + using LeafVec = QVector>; QString name; LeafVec leaves; @@ -120,16 +133,25 @@ namespace NamespaceItemModel_impl { emit model->dataChanged(model->index(0, 0, index), model->index(leaves.size(), 0, index)); } - QVariant data(const QModelIndex &/*index*/, int role) const override + QVariant data(const QModelIndex &index, int role) const override { QVariant v; if (role == Qt::DisplayRole) { - v = name; + switch (index.column()) { + case NamespaceItemModel::ColNamespaceName: + return name; + } } else if(role == Qt::CheckStateRole) { - v = getCheckState(); + if (index.column() == 0) + return getCheckState(); } - return v; + return {}; + } + + int columnCount() const override + { + return 1; } }; @@ -164,6 +186,21 @@ void NamespaceItemModel::init(std::shared_ptr ns) e->checked = true; } +void NamespaceItemModel::setEnableCheckboxes(bool enable) +{ + if (m_enableCheckboxes != enable) { + beginResetModel(); + SCOPE_EXIT { endResetModel(); }; + m_enableCheckboxes = enable; + } + +} + +bool NamespaceItemModel::isEnableCheckboxes() const +{ + return m_enableCheckboxes; +} + QModelIndex NamespaceItemModel::index(int row, int column, const QModelIndex &parent) const { @@ -192,8 +229,9 @@ QModelIndex NamespaceItemModel::parent(const QModelIndex &index) const auto ln = dynamic_cast(n); if (ln) { // leafnode auto grp = ln->parent.lock(); // Get the parent group - auto fr = std::find(groups.begin(), groups.end(), grp); // find it in the list - int row = fr - groups.begin(); // calculate index ie row +// auto fr = std::find(groups.begin(), groups.end(), grp); // find it in the list +// int row = fr - groups.begin(); // calculate index ie row + int row = groups.indexOf(grp); result = createIndex(row, 0, grp.get()); // return index } @@ -216,7 +254,11 @@ int NamespaceItemModel::rowCount(const QModelIndex &parent) const int NamespaceItemModel::columnCount(const QModelIndex &/*index*/) const { - return 1; +// if (index.isValid()) { +// auto *n = static_cast(index.internalPointer()); +// return n->columnCount(); +// } + return ColCount; } QVariant NamespaceItemModel::data(const QModelIndex &index, int role) const @@ -234,7 +276,8 @@ bool NamespaceItemModel::setData(const QModelIndex &index, return false; auto *n = static_cast(index.internalPointer()); - n->setChecked(this, index, value == Qt::Checked); + if (index.column() == 0) + n->setChecked(this, index, value == Qt::Checked); return true; } @@ -242,7 +285,23 @@ bool NamespaceItemModel::setData(const QModelIndex &index, Qt::ItemFlags NamespaceItemModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractItemModel::flags(index); - flags.setFlag(Qt::ItemIsUserCheckable); + if (index.column() == 0 && m_enableCheckboxes) + flags.setFlag(Qt::ItemIsUserCheckable); return flags; } + + +QVariant NamespaceItemModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal) { + if (role == Qt::DisplayRole) { + switch (section) { + case ColNamespaceName: return tr("Name"); + case ColOwner: return tr("Owner"); + case ColAcl: return tr("ACL"); + } + } + } + return {}; +} diff --git a/pglab/NamespaceItemModel.h b/pglab/NamespaceItemModel.h index 38705d5..79817e1 100644 --- a/pglab/NamespaceItemModel.h +++ b/pglab/NamespaceItemModel.h @@ -2,9 +2,10 @@ #define NAMESPACEITEMMODEL_H #include +#include #include "catalog/PgNamespace.h" -#include -#include +//#include +//#include namespace NamespaceItemModel_impl { @@ -20,7 +21,17 @@ class NamespaceItemModel: public QAbstractItemModel { public: NamespaceItemModel(QObject *parent = 0); + enum Columns { + ColNamespaceName, + ColOwner, + ColAcl, + + ColCount + }; + void init(std::shared_ptr ns); + void setEnableCheckboxes(bool enable); + bool isEnableCheckboxes() const; virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; @@ -33,9 +44,14 @@ public: virtual Qt::ItemFlags flags(const QModelIndex &index) const override; private: - using GroupVec = std::vector>; + using GroupVec = QVector>; GroupVec groups; + bool m_enableCheckboxes = true; + + // QAbstractItemModel interface +public: + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; }; #endif // NAMESPACEITEMMODEL_H diff --git a/pglab/pglab.pro b/pglab/pglab.pro index f46a89e..40e4782 100644 --- a/pglab/pglab.pro +++ b/pglab/pglab.pro @@ -81,6 +81,7 @@ PropertyProxyModel.cpp \ QueryTool.cpp \ CatalogInspector.cpp \ widgets/CatalogIndexPage.cpp \ + widgets/CatalogNamespacePage.cpp \ widgets/CatalogPageBase.cpp \ widgets/CatalogConstraintPage.cpp \ widgets/CatalogTablesPage.cpp \ @@ -151,6 +152,7 @@ CustomDataRole.h \ QueryTool.h \ CatalogInspector.h \ widgets/CatalogIndexPage.h \ + widgets/CatalogNamespacePage.h \ widgets/CatalogPageBase.h \ widgets/CatalogConstraintPage.h \ widgets/CatalogTablesPage.h \ diff --git a/pglab/widgets/CatalogNamespacePage.cpp b/pglab/widgets/CatalogNamespacePage.cpp new file mode 100644 index 0000000..afef3f3 --- /dev/null +++ b/pglab/widgets/CatalogNamespacePage.cpp @@ -0,0 +1,32 @@ +#include "CatalogNamespacePage.h" +#include +#include "NamespaceItemModel.h" +#include "SqlCodePreview.h" +#include "catalog/PgDatabaseCatalog.h" + +CatalogNamespacePage::CatalogNamespacePage(QWidget *parent) + : QSplitter(Qt::Horizontal, parent) + , m_namespaceTree(new QTreeView(this)) + , m_model(new NamespaceItemModel(this)) +{ + m_namespaceTree->setModel(m_model); + m_definitionView = new SqlCodePreview(this); + + addWidget(m_namespaceTree); + addWidget(m_definitionView); + + retranslateUi(); +} + +void CatalogNamespacePage::setCatalog(std::shared_ptr cat) +{ + m_catalog = cat; + m_model->setEnableCheckboxes(false); + m_model->init(cat->namespaces()); +} + +void CatalogNamespacePage::retranslateUi() +{ + +} + diff --git a/pglab/widgets/CatalogNamespacePage.h b/pglab/widgets/CatalogNamespacePage.h new file mode 100644 index 0000000..4d41aef --- /dev/null +++ b/pglab/widgets/CatalogNamespacePage.h @@ -0,0 +1,36 @@ +#ifndef CATALOGNAMESPACEPAGE_H +#define CATALOGNAMESPACEPAGE_H + +#include "widgets/CatalogPageBase.h" + +class QTreeView; +class NamespaceItemModel; +class PgDatabaseCatalog; +class SqlCodePreview; + + +class CatalogNamespacePage : public QSplitter +{ + Q_OBJECT +public: + explicit CatalogNamespacePage(QWidget *parent = nullptr); + + void setCatalog(std::shared_ptr cat); +signals: + +public slots: + +protected: + +private: + QTreeView *m_namespaceTree = nullptr; + SqlCodePreview *m_definitionView = nullptr; + NamespaceItemModel *m_model = nullptr; + CustomFilterSortModel *m_sortFilterProxy = nullptr; + std::shared_ptr m_catalog; + + void retranslateUi(); + +}; + +#endif // CATALOGNAMESPACEPAGE_H