Used slightly different approach. This tab is fully build in source code using subclasses to adjust behaviour of widgets for reuse in the other tabs. Uses custom proxy model for filtering triggers for correct table and supporting out of the box sorting by QTableView. SqlCodePreview: QPlainTextEditor which sql highlighter and in readonly mode but allows copy.
222 lines
7.1 KiB
C++
222 lines
7.1 KiB
C++
#include "TablesPage.h"
|
|
#include "ui_TablesPage.h"
|
|
|
|
#include "PgAttribute.h"
|
|
#include "PgDatabaseCatalog.h"
|
|
#include "ColumnTableModel.h"
|
|
#include "ConstraintModel.h"
|
|
#include "IconColumnDelegate.h"
|
|
#include "IndexModel.h"
|
|
#include "MainWindow.h"
|
|
#include "PgLabItemDelegate.h"
|
|
#include "PropertyProxyModel.h"
|
|
#include "ResultTableModelUtil.h"
|
|
#include "SqlFormattingUtils.h"
|
|
#include "SqlSyntaxHighlighter.h"
|
|
#include "TablesTableModel.h"
|
|
#include "TriggerPage.h"
|
|
#include "UserConfiguration.h"
|
|
#include <QStringBuilder>
|
|
#include <boost/container/flat_set.hpp>
|
|
|
|
TablesPage::TablesPage(MainWindow *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::TablesPage)
|
|
, m_window(parent)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
auto pglab_delegate = new PgLabItemDelegate(this);
|
|
auto icon_delegate = new IconColumnDelegate(this);
|
|
|
|
SetTableViewDefault(ui->tableListTable);
|
|
m_tablesModel = new TablesTableModel(this);
|
|
ui->tableListTable->setModel(m_tablesModel);
|
|
ui->tableListTable->setItemDelegate(pglab_delegate);
|
|
ui->tableListTable->setSortingEnabled(true);
|
|
ui->tableListTable->sortByColumn(0, Qt::AscendingOrder);
|
|
|
|
// Columns
|
|
SetTableViewDefault(ui->columnsTable);
|
|
m_columnsModel = new ColumnTableModel(this);
|
|
ui->columnsTable->setModel(m_columnsModel);
|
|
|
|
// Constraints
|
|
SetTableViewDefault(ui->constraintsTable);
|
|
m_constraintModel = new ConstraintModel(this);
|
|
ui->constraintsTable->setModel(m_constraintModel);
|
|
ui->constraintsTable->setItemDelegateForColumn(0, icon_delegate);
|
|
|
|
// Indexes
|
|
SetTableViewDefault(ui->indexesTable);
|
|
m_indexModel = new IndexModel(this);
|
|
ui->indexesTable->setModel(m_indexModel);
|
|
ui->indexesTable->setItemDelegate(pglab_delegate);
|
|
ui->indexesTable->setItemDelegateForColumn(0, icon_delegate);
|
|
|
|
// Properties
|
|
PropertyProxyModel* property_model = new PropertyProxyModel(this);
|
|
property_model->setSourceModel(m_tablesModel);
|
|
SetTableViewDefault(ui->tablePropertiesTable);
|
|
ui->tablePropertiesTable->setModel(property_model);
|
|
ui->tablePropertiesTable->setItemDelegate(pglab_delegate);
|
|
|
|
|
|
// Set code editor fonts
|
|
QFont code_font = UserConfiguration::instance()->codeFont();
|
|
ui->constraintSqlEdit->setFont(code_font);
|
|
ui->indexSqlEdit->setFont(code_font);
|
|
|
|
|
|
// Connect signals
|
|
// ---------------
|
|
// Table selection
|
|
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
|
property_model, &PropertyProxyModel::setActiveRow);
|
|
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
|
&TablesPage::tableListTable_currentRowChanged);
|
|
|
|
// connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
|
// &TablesPage::constraintsTable_currentRowChanged);
|
|
|
|
connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
|
|
&TablesPage::constraintsTable_selectionChanged);
|
|
connect(ui->constraintsTable->model(), &QAbstractItemModel::modelReset, this,
|
|
&TablesPage::constraintsTable_modelReset);
|
|
|
|
// React to changes in de selected indexes, does not trigger when model is reset
|
|
connect(ui->indexesTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
|
|
&TablesPage::indexesTable_selectionChanged);
|
|
// Capture model reset independently
|
|
connect(ui->indexesTable->model(), &QAbstractItemModel::modelReset, this,
|
|
&TablesPage::indexesTable_modelReset);
|
|
|
|
// Non designer based code
|
|
m_triggerPage = new TriggerPage(this);
|
|
m_triggerTab = addDetailTab(m_triggerPage, tr("Triggers"));
|
|
retranslateUi(false);
|
|
}
|
|
|
|
|
|
void TablesPage::retranslateUi(bool all)
|
|
{
|
|
if (all)
|
|
ui->retranslateUi(this);
|
|
|
|
ui->twDetails->setTabText(ui->twDetails->indexOf(m_triggerTab), QApplication::translate("TablesPage", "Triggers", nullptr));
|
|
}
|
|
|
|
|
|
QWidget* TablesPage::addDetailTab(QWidget *contents, QString caption)
|
|
{
|
|
auto tab = new QWidget();
|
|
auto verticalLayout = new QVBoxLayout(tab);
|
|
verticalLayout->addWidget(contents);
|
|
ui->twDetails->addTab(tab, caption);
|
|
return tab;
|
|
}
|
|
|
|
TablesPage::~TablesPage()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void TablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
|
|
{
|
|
m_catalog = cat;
|
|
m_tablesModel->setCatalog(cat);
|
|
ui->tableListTable->resizeColumnsToContents();
|
|
|
|
m_triggerPage->setCatalog(cat);
|
|
// m_namespaceFilterWidget->init(cat->namespaces());
|
|
|
|
auto highlighter = new SqlSyntaxHighlighter(ui->constraintSqlEdit->document());
|
|
highlighter->setTypes(*cat->types());
|
|
highlighter = new SqlSyntaxHighlighter(ui->indexSqlEdit->document());
|
|
highlighter->setTypes(*cat->types());
|
|
}
|
|
|
|
void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
{
|
|
if (current.row() != previous.row()) {
|
|
PgClass table = m_tablesModel->getTable(current.row());
|
|
m_columnsModel->setData(m_catalog, table);
|
|
ui->columnsTable->resizeColumnsToContents();
|
|
|
|
m_constraintModel->setData(m_catalog, table);
|
|
ui->constraintsTable->resizeColumnsToContents();
|
|
ui->constraintsTable->selectionModel()->reset();
|
|
|
|
m_indexModel->setData(m_catalog, table);
|
|
ui->indexesTable->resizeColumnsToContents();
|
|
|
|
m_triggerPage->setFilter(table);
|
|
}
|
|
}
|
|
|
|
//void TablesPage::constraintsTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
|
//{
|
|
// if (current.row() != previous.row()) {
|
|
//// QString drop_definition = m_constraintModel->dropDefinition(current.row());
|
|
//// QString create_definition = m_constraintModel->createDefinition(current.row());
|
|
// const PgConstraint& constraint = m_constraintModel->constraint(current.row());
|
|
// QString drop = getDropConstraintDefinition(*m_catalog, constraint);
|
|
// QString add = getConstraintDefinition(*m_catalog, constraint);
|
|
|
|
// ui->constraintSqlEdit->setPlainText(drop % QString::fromUtf16(u"\n") % add);
|
|
// }
|
|
//}
|
|
|
|
void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
|
|
{
|
|
const auto indexes = ui->constraintsTable->selectionModel()->selectedIndexes();
|
|
boost::container::flat_set<int> rijen;
|
|
for (const auto &e : indexes)
|
|
rijen.insert(e.row());
|
|
|
|
QString drops;
|
|
QString creates;
|
|
for (auto rij : rijen) {
|
|
const PgConstraint constraint = m_constraintModel->constraint(rij);
|
|
drops += getDropConstraintDefinition(*m_catalog, constraint) % "\n";
|
|
creates += getConstraintDefinition(*m_catalog, constraint) % "\n";
|
|
}
|
|
ui->constraintSqlEdit->setPlainText(drops % "\n" % creates);
|
|
}
|
|
|
|
void TablesPage::constraintsTable_modelReset()
|
|
{
|
|
ui->constraintSqlEdit->clear();
|
|
}
|
|
|
|
void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
|
|
{
|
|
const auto indexes = ui->indexesTable->selectionModel()->selectedIndexes();
|
|
boost::container::flat_set<int> rijen;
|
|
for (const auto &e : indexes)
|
|
rijen.insert(e.row());
|
|
|
|
QString drops;
|
|
QString creates;
|
|
for (auto rij : rijen) {
|
|
const PgIndex index = m_indexModel->getIndex(rij);
|
|
drops += getDropIndexDefinition(*m_catalog, index) % "\n";
|
|
creates += getIndexDefinition(*m_catalog, index) % "\n";
|
|
}
|
|
ui->indexSqlEdit->setPlainText(drops % "\n" % creates);
|
|
|
|
}
|
|
|
|
void TablesPage::indexesTable_modelReset()
|
|
{
|
|
ui->indexSqlEdit->clear();
|
|
}
|
|
|
|
void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
|
|
{
|
|
PgClass table = m_tablesModel->getTable(index.row());
|
|
if (table.oid != InvalidOid) {
|
|
m_window->newCrudPage(table);
|
|
}
|
|
|
|
}
|