2017-12-10 14:20:45 +01:00
|
|
|
|
#include "TablesPage.h"
|
2017-12-10 08:17:07 +01:00
|
|
|
|
#include "ui_TablesPage.h"
|
|
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
#include "PgAttribute.h"
|
2017-12-29 08:39:08 +01:00
|
|
|
|
#include "PgDatabaseCatalog.h"
|
2017-12-10 14:20:45 +01:00
|
|
|
|
#include "TablesTableModel.h"
|
2017-12-12 20:13:53 +01:00
|
|
|
|
#include "ResultTableModelUtil.h"
|
|
|
|
|
|
#include "ColumnTableModel.h"
|
2017-12-30 12:57:55 +01:00
|
|
|
|
#include "ConstraintModel.h"
|
2018-08-26 07:14:25 +02:00
|
|
|
|
#include "PropertyProxyModel.h"
|
2017-12-30 12:57:55 +01:00
|
|
|
|
#include "IconColumnDelegate.h"
|
2018-01-06 21:22:22 +01:00
|
|
|
|
#include "IndexModel.h"
|
|
|
|
|
|
#include "SqlFormattingUtils.h"
|
|
|
|
|
|
#include "SqlSyntaxHighlighter.h"
|
|
|
|
|
|
#include <QStringBuilder>
|
|
|
|
|
|
#include <boost/container/flat_set.hpp>
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include "MainWindow.h"
|
2018-01-15 13:31:37 +01:00
|
|
|
|
#include "PgLabItemDelegate.h"
|
2017-12-10 14:20:45 +01:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
TablesPage::TablesPage(MainWindow *parent)
|
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
|
, ui(new Ui::TablesPage)
|
|
|
|
|
|
, m_window(parent)
|
2017-12-10 08:17:07 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
2017-12-10 14:20:45 +01:00
|
|
|
|
|
2018-08-27 21:12:27 +02:00
|
|
|
|
auto pglab_delegate = new PgLabItemDelegate(this);
|
|
|
|
|
|
auto icon_delegate = new IconColumnDelegate(this);
|
|
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
SetTableViewDefault(ui->tableListTable);
|
2017-12-10 14:20:45 +01:00
|
|
|
|
m_tablesModel = new TablesTableModel(this);
|
|
|
|
|
|
ui->tableListTable->setModel(m_tablesModel);
|
2018-08-27 21:12:27 +02:00
|
|
|
|
ui->tableListTable->setItemDelegate(pglab_delegate);
|
2018-07-07 09:57:59 +02:00
|
|
|
|
ui->tableListTable->setSortingEnabled(true);
|
|
|
|
|
|
ui->tableListTable->sortByColumn(0, Qt::AscendingOrder);
|
2017-12-10 14:20:45 +01:00
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
SetTableViewDefault(ui->columnsTable);
|
|
|
|
|
|
m_columnsModel = new ColumnTableModel(this);
|
|
|
|
|
|
ui->columnsTable->setModel(m_columnsModel);
|
|
|
|
|
|
|
2017-12-30 12:57:55 +01:00
|
|
|
|
SetTableViewDefault(ui->constraintsTable);
|
|
|
|
|
|
m_constraintModel = new ConstraintModel(this);
|
|
|
|
|
|
ui->constraintsTable->setModel(m_constraintModel);
|
2018-08-27 21:12:27 +02:00
|
|
|
|
ui->constraintsTable->setItemDelegateForColumn(0, icon_delegate);
|
2017-12-30 12:57:55 +01:00
|
|
|
|
|
2018-01-06 21:22:22 +01:00
|
|
|
|
QFont font;
|
|
|
|
|
|
font.setFamily("Source Code Pro");
|
|
|
|
|
|
font.setFixedPitch(true);
|
|
|
|
|
|
font.setPointSize(10);
|
|
|
|
|
|
ui->constraintSqlEdit->setFont(font);
|
2018-04-08 09:04:38 +02:00
|
|
|
|
ui->indexSqlEdit->setFont(font);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
SetTableViewDefault(ui->indexesTable);
|
|
|
|
|
|
m_indexModel = new IndexModel(this);
|
|
|
|
|
|
ui->indexesTable->setModel(m_indexModel);
|
2018-08-27 21:12:27 +02:00
|
|
|
|
ui->indexesTable->setItemDelegate(pglab_delegate);
|
|
|
|
|
|
ui->indexesTable->setItemDelegateForColumn(0, icon_delegate);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-08-26 07:59:15 +02:00
|
|
|
|
PropertyProxyModel* property_model = new PropertyProxyModel(this);
|
|
|
|
|
|
property_model->setSourceModel(m_tablesModel);
|
2018-08-26 15:18:32 +02:00
|
|
|
|
SetTableViewDefault(ui->tablePropertiesTable);
|
2018-08-26 07:59:15 +02:00
|
|
|
|
ui->tablePropertiesTable->setModel(property_model);
|
2018-08-27 21:12:27 +02:00
|
|
|
|
ui->tablePropertiesTable->setItemDelegate(pglab_delegate);
|
2018-08-26 07:59:15 +02:00
|
|
|
|
|
2018-09-02 12:28:56 +02:00
|
|
|
|
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged,
|
|
|
|
|
|
property_model, &PropertyProxyModel::setActiveRow);
|
2018-08-26 07:59:15 +02:00
|
|
|
|
|
2018-08-26 07:11:46 +02:00
|
|
|
|
|
2018-04-08 09:04:38 +02:00
|
|
|
|
//m_namespaceFilterWidget = new NamespaceFilterWidget(this);
|
|
|
|
|
|
//ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget);
|
2017-12-29 08:39:08 +01:00
|
|
|
|
|
2018-08-05 11:57:27 +02:00
|
|
|
|
// Table selection
|
2017-12-12 20:13:53 +01:00
|
|
|
|
connect(ui->tableListTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
2018-01-06 21:33:24 +01:00
|
|
|
|
&TablesPage::tableListTable_currentRowChanged);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
// connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::currentRowChanged, this,
|
2018-01-06 21:33:24 +01:00
|
|
|
|
// &TablesPage::constraintsTable_currentRowChanged);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
connect(ui->constraintsTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
|
2018-01-06 21:33:24 +01:00
|
|
|
|
&TablesPage::constraintsTable_selectionChanged);
|
2018-08-05 11:57:27 +02:00
|
|
|
|
connect(ui->constraintsTable->model(), &QAbstractItemModel::modelReset, this,
|
|
|
|
|
|
&TablesPage::constraintsTable_modelReset);
|
2017-12-12 20:13:53 +01:00
|
|
|
|
|
2018-08-05 11:57:27 +02:00
|
|
|
|
// React to changes in de selected indexes, does not trigger when model is reset
|
2018-04-08 09:04:38 +02:00
|
|
|
|
connect(ui->indexesTable->selectionModel(), &QItemSelectionModel::selectionChanged, this,
|
|
|
|
|
|
&TablesPage::indexesTable_selectionChanged);
|
2018-08-05 11:57:27 +02:00
|
|
|
|
// Capture model reset independently
|
|
|
|
|
|
connect(ui->indexesTable->model(), &QAbstractItemModel::modelReset, this,
|
|
|
|
|
|
&TablesPage::indexesTable_modelReset);
|
2018-04-08 09:04:38 +02:00
|
|
|
|
|
2017-12-12 20:13:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TablesPage::~TablesPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
delete ui;
|
2017-12-10 14:20:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_catalog = cat;
|
|
|
|
|
|
m_tablesModel->setCatalog(cat);
|
2017-12-19 19:55:12 +01:00
|
|
|
|
ui->tableListTable->resizeColumnsToContents();
|
2018-04-08 09:04:38 +02:00
|
|
|
|
// m_namespaceFilterWidget->init(cat->namespaces());
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
|
|
|
|
|
auto highlighter = new SqlSyntaxHighlighter(ui->constraintSqlEdit->document());
|
|
|
|
|
|
highlighter->setTypes(*cat->types());
|
2018-04-08 09:04:38 +02:00
|
|
|
|
highlighter = new SqlSyntaxHighlighter(ui->indexSqlEdit->document());
|
|
|
|
|
|
highlighter->setTypes(*cat->types());
|
2017-12-10 08:17:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-06 21:33:24 +01:00
|
|
|
|
void TablesPage::tableListTable_currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
2017-12-10 08:17:07 +01:00
|
|
|
|
{
|
2017-12-12 20:13:53 +01:00
|
|
|
|
if (current.row() != previous.row()) {
|
2017-12-17 19:54:23 +01:00
|
|
|
|
PgClass table = m_tablesModel->getTable(current.row());
|
|
|
|
|
|
m_columnsModel->setData(m_catalog, table);
|
2017-12-17 20:05:09 +01:00
|
|
|
|
ui->columnsTable->resizeColumnsToContents();
|
2017-12-30 12:57:55 +01:00
|
|
|
|
|
|
|
|
|
|
m_constraintModel->setData(m_catalog, table);
|
|
|
|
|
|
ui->constraintsTable->resizeColumnsToContents();
|
2018-01-06 21:22:22 +01:00
|
|
|
|
ui->constraintsTable->selectionModel()->reset();
|
|
|
|
|
|
|
|
|
|
|
|
m_indexModel->setData(m_catalog, table);
|
|
|
|
|
|
ui->indexesTable->resizeColumnsToContents();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-08 09:04:38 +02:00
|
|
|
|
//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);
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-04-08 09:04:38 +02:00
|
|
|
|
// ui->constraintSqlEdit->setPlainText(drop % QString::fromUtf16(u"\n") % add);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
|
2018-01-15 13:31:37 +01:00
|
|
|
|
void TablesPage::constraintsTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
|
2018-01-06 21:22:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
const auto indexes = ui->constraintsTable->selectionModel()->selectedIndexes();
|
|
|
|
|
|
boost::container::flat_set<int> rijen;
|
2018-08-05 11:57:27 +02:00
|
|
|
|
for (const auto &e : indexes)
|
2018-04-08 09:04:38 +02:00
|
|
|
|
rijen.insert(e.row());
|
|
|
|
|
|
|
2018-01-06 21:22:22 +01:00
|
|
|
|
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";
|
2017-12-12 20:13:53 +01:00
|
|
|
|
}
|
2018-01-06 21:22:22 +01:00
|
|
|
|
ui->constraintSqlEdit->setPlainText(drops % "\n" % creates);
|
2017-12-10 08:17:07 +01:00
|
|
|
|
}
|
2018-01-09 20:39:43 +01:00
|
|
|
|
|
2018-08-05 11:57:27 +02:00
|
|
|
|
void TablesPage::constraintsTable_modelReset()
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->constraintSqlEdit->clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-08 09:04:38 +02:00
|
|
|
|
void TablesPage::indexesTable_selectionChanged(const QItemSelection &/*selected*/, const QItemSelection &/*deselected*/)
|
|
|
|
|
|
{
|
|
|
|
|
|
const auto indexes = ui->indexesTable->selectionModel()->selectedIndexes();
|
|
|
|
|
|
boost::container::flat_set<int> rijen;
|
2018-08-05 11:57:27 +02:00
|
|
|
|
for (const auto &e : indexes)
|
2018-04-08 09:04:38 +02:00
|
|
|
|
rijen.insert(e.row());
|
|
|
|
|
|
|
|
|
|
|
|
QString drops;
|
|
|
|
|
|
QString creates;
|
|
|
|
|
|
for (auto rij : rijen) {
|
|
|
|
|
|
const PgIndex index = m_indexModel->getIndex(rij);
|
2018-08-05 11:27:05 +02:00
|
|
|
|
drops += getDropIndexDefinition(*m_catalog, index) % "\n";
|
2018-04-08 09:04:38 +02:00
|
|
|
|
creates += getIndexDefinition(*m_catalog, index) % "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
ui->indexSqlEdit->setPlainText(drops % "\n" % creates);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-05 11:57:27 +02:00
|
|
|
|
void TablesPage::indexesTable_modelReset()
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->indexSqlEdit->clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
void TablesPage::on_tableListTable_doubleClicked(const QModelIndex &index)
|
|
|
|
|
|
{
|
|
|
|
|
|
PgClass table = m_tablesModel->getTable(index.row());
|
|
|
|
|
|
if (table.oid != InvalidOid) {
|
|
|
|
|
|
m_window->newCrudPage(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|