Give the tables default sorting

This commit is contained in:
eelke 2021-06-12 07:36:24 +02:00
parent 0da493f1e4
commit ddc163a0ee
10 changed files with 66 additions and 50 deletions

View file

@ -6,10 +6,10 @@
#include "PgLabTableView.h"
#include "ResultTableModelUtil.h"
#include "SqlCodePreview.h"
//#include "PgLabItemDelegate.h"
#include "SqlFormattingUtils.h"
#include "UserConfiguration.h"
#include "catalog/PgClass.h"
#include <QHeaderView>
#include <QSortFilterProxyModel>
#include <QStyledItemDelegate>
#include <QStringBuilder>
@ -28,6 +28,7 @@ ColumnPage::ColumnPage(QWidget *parent)
m_sortFilterProxy->setSourceModel(m_columnModel);
m_tableView->setItemDelegateForColumn(ColumnTableModel::TypeCol, new QStyledItemDelegate(this));
m_tableView->setModel(m_sortFilterProxy);
m_tableView->horizontalHeader()->setSortIndicator(ColumnTableModel::AttnumCol, Qt::AscendingOrder);
m_tableView->setSortingEnabled(true);
// m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_sortFilterProxy->sort(ColumnTableModel::AttnumCol, Qt::AscendingOrder);

View file

@ -63,60 +63,60 @@ void TablesTableModel::refresh()
m_tables.push_back(e);
}
doSort(1);
// doSort(1);
}
void TablesTableModel::setSortOrder(int so)
{
beginResetModel();
doSort(so);
endResetModel();
}
//void TablesTableModel::setSortOrder(int so)
//{
// beginResetModel();
// doSort(so);
// endResetModel();
//}
namespace {
//namespace {
inline bool compareByName(PgClass l, PgClass r)
{
return l.objectName() < r.objectName()
|| (l.objectName() == r.objectName() && l.nsName() < r.nsName());
}
// inline bool compareByName(PgClass l, PgClass r)
// {
// return l.objectName() < r.objectName()
// || (l.objectName() == r.objectName() && l.nsName() < r.nsName());
// }
inline bool compareBySchema(PgClass l, PgClass r)
{
return l.nsName() < r.nsName()
|| (l.nsName() == r.nsName() && l.objectName() < r.objectName());
}
// inline bool compareBySchema(PgClass l, PgClass r)
// {
// return l.nsName() < r.nsName()
// || (l.nsName() == r.nsName() && l.objectName() < r.objectName());
// }
}
//}
void TablesTableModel::sort(int column, Qt::SortOrder order)
{
if (column == NameCol) {
if (order == Qt::AscendingOrder)
std::sort(m_tables.begin(), m_tables.end(), compareByName);
else
std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareByName(l, r); });
}
else if (column == NamespaceCol) {
if (order == Qt::AscendingOrder)
std::sort(m_tables.begin(), m_tables.end(), compareBySchema);
else
std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareBySchema(l, r); });
}
//void TablesTableModel::sort(int column, Qt::SortOrder order)
//{
// if (column == NameCol) {
// if (order == Qt::AscendingOrder)
// std::sort(m_tables.begin(), m_tables.end(), compareByName);
// else
// std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareByName(l, r); });
// }
// else if (column == NamespaceCol) {
// if (order == Qt::AscendingOrder)
// std::sort(m_tables.begin(), m_tables.end(), compareBySchema);
// else
// std::sort(m_tables.begin(), m_tables.end(), [] (auto l, auto r) { return !compareBySchema(l, r); });
// }
emit layoutChanged();
}
// emit layoutChanged();
//}
void TablesTableModel::doSort(int )
{
// if (so == 1)
// std::sort(m_tables.begin(), m_tables.end(),
// [] (auto l, auto r) -> bool { return l.relnamespace_name < r.relnamespace_name
// || (l.relnamespace_name == r.relnamespace_name && l.name < r.name); });
// else
// std::sort(m_tables.begin(), m_tables.end(),
// [] (auto l, auto r) -> bool { return l.name < r.name; });
}
//void TablesTableModel::doSort(int )
//{
//// if (so == 1)
//// std::sort(m_tables.begin(), m_tables.end(),
//// [] (auto l, auto r) -> bool { return l.relnamespace_name < r.relnamespace_name
//// || (l.relnamespace_name == r.relnamespace_name && l.name < r.name); });
//// else
//// std::sort(m_tables.begin(), m_tables.end(),
//// [] (auto l, auto r) -> bool { return l.name < r.name; });
//}
QVariant TablesTableModel::headerData(int section, Qt::Orientation orientation, int role) const

View file

@ -35,8 +35,8 @@ public:
void setNamespaceFilter(NamespaceFilter nsf);
void setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat);
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
void setSortOrder(int so);
// virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
// void setSortOrder(int so);
// Header:
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
@ -61,7 +61,7 @@ private:
Oid getType(int column) const;
QVariant getData(const QModelIndex &index) const;
// QString formatTableName(const PgClass &cls) const;
void doSort(int so);
// void doSort(int so);
private slots:
void refresh();

View file

@ -8,6 +8,7 @@
#include "CustomDataRole.h"
#include "PgLabTableView.h"
#include "catalog/PgProcContainer.h"
#include <QHeaderView>
#include <QStringBuilder>
#include <unordered_set>

View file

@ -4,6 +4,7 @@
#include "IconColumnDelegate.h"
#include "PgLabTableView.h"
#include "SqlCodePreview.h"
#include <QHeaderView>
#include <QStringBuilder>
CatalogConstraintPage::CatalogConstraintPage(QWidget *parent)
@ -13,6 +14,8 @@ CatalogConstraintPage::CatalogConstraintPage(QWidget *parent)
m_sortFilterProxy->setSourceModel(m_constraintModel);
m_tableView->setItemDelegateForColumn(0, new IconColumnDelegate(this));
m_tableView->horizontalHeader()->setSortIndicator(ConstraintModel::NameCol, Qt::AscendingOrder);
m_tableView->setSortingEnabled(true);
connect(m_tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &CatalogConstraintPage::tableView_selectionChanged);

View file

@ -7,6 +7,7 @@
#include "SqlCodePreview.h"
#include <QApplication>
#include "PgLabTableView.h"
#include <QHeaderView>
#include <QVBoxLayout>
#include <QTabWidget>
@ -35,6 +36,7 @@ CatalogFunctionsPage::CatalogFunctionsPage(QWidget *parent)
m_sortFilterProxy = new CustomFilterSortModel(this);
m_sortFilterProxy->setSourceModel(m_model);
m_functionTable->setModel(m_sortFilterProxy);
m_functionTable->horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
m_functionTable->setSortingEnabled(true);
m_functionTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_functionTable->setSelectionMode(QAbstractItemView::SingleSelection);

View file

@ -3,6 +3,7 @@
#include "IndexModel.h"
#include "PgLabTableView.h"
#include "SqlCodePreview.h"
#include <QHeaderView>
#include <QStringBuilder>
#include "IconColumnDelegate.h"
@ -13,6 +14,8 @@ CatalogIndexPage::CatalogIndexPage(QWidget *parent)
m_indexModel = new IndexModel(this);
m_sortFilterProxy->setSourceModel(m_indexModel);
m_tableView->setItemDelegateForColumn(0, new IconColumnDelegate(this));
m_tableView->horizontalHeader()->setSortIndicator(IndexModel::NameCol, Qt::AscendingOrder);
m_tableView->setSortingEnabled(true);
connect(m_tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &CatalogIndexPage::tableView_selectionChanged);

View file

@ -6,6 +6,7 @@
#include "SequenceModel.h"
#include "SqlCodePreview.h"
#include "PgLabTableView.h"
#include <QHeaderView>
CatalogSequencesPage::CatalogSequencesPage(QWidget *parent)
: QSplitter(Qt::Horizontal, parent)
@ -22,6 +23,7 @@ CatalogSequencesPage::CatalogSequencesPage(QWidget *parent)
m_sortFilterProxy = new CustomFilterSortModel(this);
m_sortFilterProxy->setSourceModel(m_model);
m_sequenceTable->setModel(m_sortFilterProxy);
m_sequenceTable->horizontalHeader()->setSortIndicator(SequenceModel::NameCol, Qt::AscendingOrder);
m_sequenceTable->setSortingEnabled(true);
m_sequenceTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_sequenceTable->setSelectionMode(QAbstractItemView::SingleSelection);

View file

@ -18,6 +18,7 @@
#include "widgets/CatalogIndexPage.h"
#include <QApplication>
#include <QHeaderView>
#include <QStringBuilder>
#include <QSortFilterProxyModel>
#include <QTableWidget>
@ -38,7 +39,8 @@ CatalogTablesPage::CatalogTablesPage(QWidget *parent)
m_tablesModel = new TablesTableModel(this);
m_tablesSortFilter = new QSortFilterProxyModel(this);
m_tablesSortFilter->setSourceModel(m_tablesModel);
m_tableView->setModel(m_tablesSortFilter);
m_tableView->setModel(m_tablesSortFilter);
m_tableView->horizontalHeader()->setSortIndicator(TablesTableModel::NameCol, Qt::AscendingOrder);
m_tableView->setSortingEnabled(true);
// - Columns page

View file

@ -8,6 +8,7 @@
#include "SqlCodePreview.h"
#include "PgLabTableView.h"
#include <optional>
#include <QHeaderView>
CatalogTypesPage::CatalogTypesPage(QWidget *parent)
: QSplitter(Qt::Horizontal, parent)
@ -24,6 +25,7 @@ CatalogTypesPage::CatalogTypesPage(QWidget *parent)
m_sortFilterProxy = new CustomFilterSortModel(this);
m_sortFilterProxy->setSourceModel(m_model);
m_typeTable->setModel(m_sortFilterProxy);
m_typeTable->horizontalHeader()->setSortIndicator(TypeModel::NameCol, Qt::AscendingOrder);
m_typeTable->setSortingEnabled(true);
m_typeTable->setSelectionBehavior(QAbstractItemView::SelectRows);
m_typeTable->setSelectionMode(QAbstractItemView::SingleSelection);