pgLab/pglab/TablesPage.cpp
eelke ec78dafa94 Removed on_ from the name of the slots to prevent warning about not being able to find the signals.
The old name followed the pattern used by Qt to automatically deduce what signal should
be connected to which slot. However as these are signals from a sub object it cannot find
them and I have made the connections manually. To prevent getting warnings at runtime
I changed the name.
2018-01-06 21:33:24 +01:00

123 lines
4.1 KiB
C++

#include "TablesPage.h"
#include "ui_TablesPage.h"
#include "PgAttribute.h"
#include "PgDatabaseCatalog.h"
#include "TablesTableModel.h"
#include "ResultTableModelUtil.h"
#include "ColumnTableModel.h"
#include "ConstraintModel.h"
#include "NamespaceFilterWidget.h"
#include "IconColumnDelegate.h"
#include "IndexModel.h"
#include "SqlFormattingUtils.h"
#include "SqlSyntaxHighlighter.h"
#include <QStringBuilder>
#include <boost/container/flat_set.hpp>
TablesPage::TablesPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::TablesPage)
{
ui->setupUi(this);
SetTableViewDefault(ui->tableListTable);
m_tablesModel = new TablesTableModel(this);
ui->tableListTable->setModel(m_tablesModel);
SetTableViewDefault(ui->columnsTable);
m_columnsModel = new ColumnTableModel(this);
ui->columnsTable->setModel(m_columnsModel);
SetTableViewDefault(ui->constraintsTable);
m_constraintModel = new ConstraintModel(this);
auto delegate = new IconColumnDelegate(this);
ui->constraintsTable->setModel(m_constraintModel);
ui->constraintsTable->setItemDelegateForColumn(0, delegate);
QFont font;
font.setFamily("Source Code Pro");
font.setFixedPitch(true);
font.setPointSize(10);
ui->constraintSqlEdit->setFont(font);
SetTableViewDefault(ui->indexesTable);
m_indexModel = new IndexModel(this);
ui->indexesTable->setModel(m_indexModel);
ui->indexesTable->setItemDelegateForColumn(0, delegate);
m_namespaceFilterWidget = new NamespaceFilterWidget(this);
ui->verticalLayoutTableView->addWidget(m_namespaceFilterWidget);
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);
}
TablesPage::~TablesPage()
{
delete ui;
}
void TablesPage::setCatalog(std::shared_ptr<PgDatabaseCatalog> cat)
{
m_catalog = cat;
m_tablesModel->setCatalog(cat);
ui->tableListTable->resizeColumnsToContents();
m_namespaceFilterWidget->init(cat->namespaces());
auto highlighter = new SqlSyntaxHighlighter(ui->constraintSqlEdit->document());
highlighter->setTypes(*cat->types());
}
void TablesPage::tableListTable_currentRowChanged(const QModelIndex &current, 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();
}
}
void TablesPage::constraintsTable_currentRowChanged(const QModelIndex &current, 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);
}