2016-12-26 16:06:55 +01:00
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
|
2017-01-21 18:16:57 +01:00
|
|
|
|
//#include "QueryResultModel.h"
|
|
|
|
|
|
//#include "QueryExplainModel.h"
|
2017-01-09 07:39:09 +01:00
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
#include <QFileDialog>
|
2017-01-19 17:48:44 +01:00
|
|
|
|
#include <QMessageBox>
|
2016-12-29 13:48:35 +01:00
|
|
|
|
#include <QTextTable>
|
2016-12-30 10:14:26 +01:00
|
|
|
|
#include <QTimer>
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include <QElapsedTimer>
|
2016-12-27 15:41:11 +01:00
|
|
|
|
#include <windows.h>
|
2017-01-08 15:16:16 +01:00
|
|
|
|
#include <algorithm>
|
2017-01-19 17:48:44 +01:00
|
|
|
|
#include <QCloseEvent>
|
2017-01-21 08:09:12 +01:00
|
|
|
|
#include <querytab.h>
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include "util.h"
|
2017-02-01 18:01:02 +01:00
|
|
|
|
#include "MasterController.h"
|
2017-01-08 15:16:16 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
namespace pg = Pgsql;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-01 18:01:02 +01:00
|
|
|
|
MainWindow::MainWindow(MasterController *master, QWidget *parent)
|
2017-01-06 07:23:40 +01:00
|
|
|
|
: QMainWindow(parent)
|
|
|
|
|
|
, ui(new Ui::MainWindow)
|
2017-02-01 18:01:02 +01:00
|
|
|
|
, m_masterController(master)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
2017-01-22 08:50:41 +01:00
|
|
|
|
ui->tabWidget->setDocumentMode(true);
|
2017-01-25 06:54:21 +01:00
|
|
|
|
//ui->tabWidget->setTabsClosable(true);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
2017-01-06 07:23:40 +01:00
|
|
|
|
{
|
2017-01-09 07:39:09 +01:00
|
|
|
|
delete ui;
|
2017-01-06 07:23:40 +01:00
|
|
|
|
}
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
2017-01-22 08:50:41 +01:00
|
|
|
|
QueryTab* MainWindow::newSqlPage()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QueryTab *qt = new QueryTab(this);
|
|
|
|
|
|
qt->setConfig(m_config);
|
|
|
|
|
|
ui->tabWidget->addTab(qt, "Tab");
|
2017-01-22 08:50:41 +01:00
|
|
|
|
ui->tabWidget->setCurrentWidget(qt);
|
2017-01-25 06:54:21 +01:00
|
|
|
|
qt->newdoc();
|
2017-01-22 08:50:41 +01:00
|
|
|
|
return qt;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QueryTab *MainWindow::GetActiveQueryTab()
|
|
|
|
|
|
{
|
|
|
|
|
|
QWidget *widget = ui->tabWidget->currentWidget();
|
|
|
|
|
|
QueryTab *qt = dynamic_cast<QueryTab*>(widget);
|
|
|
|
|
|
return qt;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
void MainWindow::setConfig(const ConnectionConfig &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_config = config;
|
|
|
|
|
|
QString title = "pglab - ";
|
|
|
|
|
|
title += m_config.name().c_str();
|
|
|
|
|
|
setWindowTitle(title);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
newSqlPage();
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-03 07:22:36 +01:00
|
|
|
|
void MainWindow::QueueTask(TSQueue::t_Callable c)
|
2016-12-30 10:14:26 +01:00
|
|
|
|
{
|
2017-01-03 07:22:36 +01:00
|
|
|
|
m_taskQueue.add(c);
|
2016-12-30 10:14:26 +01:00
|
|
|
|
// Theoretically this needs to be only called if the queue was empty because otherwise it already would
|
|
|
|
|
|
// be busy emptying the queue. For now however I think it is safer to call it just to make sure.
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "processCallableQueue", Qt::QueuedConnection); // queues on main thread
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::processCallableQueue()
|
|
|
|
|
|
{
|
2017-01-03 07:22:36 +01:00
|
|
|
|
if (!m_taskQueue.empty()) {
|
|
|
|
|
|
auto c = m_taskQueue.pop();
|
|
|
|
|
|
c();
|
|
|
|
|
|
if (!m_taskQueue.empty()) {
|
|
|
|
|
|
QTimer::singleShot(0, this, SLOT(processCallableQueue()));
|
|
|
|
|
|
}
|
2016-12-30 10:14:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-09 07:39:09 +01:00
|
|
|
|
void MainWindow::on_actionLoad_SQL_triggered()
|
|
|
|
|
|
{
|
2017-01-22 08:50:41 +01:00
|
|
|
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
|
|
|
|
|
QString file_name = QFileDialog::getOpenFileName(this,
|
|
|
|
|
|
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
|
|
|
|
|
|
if ( ! file_name.isEmpty()) {
|
|
|
|
|
|
QueryTab* qt = newSqlPage();
|
|
|
|
|
|
qt->load(file_name);
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionSave_SQL_triggered()
|
2017-01-21 08:09:12 +01:00
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->save();
|
2017-01-21 08:09:12 +01:00
|
|
|
|
}
|
2017-01-21 08:19:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionSave_SQL_as_triggered()
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->saveAs();
|
2017-01-21 08:19:47 +01:00
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2017-01-21 08:19:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionSave_copy_of_SQL_as_triggered()
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->saveCopyAs();
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionExport_data_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
|
|
|
|
|
QString file_name = QFileDialog::getSaveFileName(this,
|
|
|
|
|
|
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionClose_triggered()
|
|
|
|
|
|
{
|
2017-01-25 06:54:21 +01:00
|
|
|
|
//close();
|
|
|
|
|
|
on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionAbout_triggered()
|
|
|
|
|
|
{
|
2017-02-01 18:01:02 +01:00
|
|
|
|
QMessageBox::about(this, "pgLab 0.1", tr(
|
|
|
|
|
|
"Copyrights 2016-2017, Eelke Klein, All Rights Reserved.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"The program is provided AS IS with NO WARRANTY OF ANY KIND,"
|
|
|
|
|
|
" INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS "
|
|
|
|
|
|
"FOR A PARTICULAR PURPOSE.\n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"This program is dynamically linked with Qt 5.7 Copyright (C) 2016 The Qt Company Ltd. https://www.qt.io/licensing/. \n"
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
"Icons by fatcow http://www.fatcow.com/free-icons provided under Creative Commons "
|
|
|
|
|
|
"attribution 3.0 license\n"
|
|
|
|
|
|
));
|
|
|
|
|
|
|
2017-01-09 07:39:09 +01:00
|
|
|
|
}
|
2017-01-13 19:09:58 +01:00
|
|
|
|
|
|
|
|
|
|
#if false
|
|
|
|
|
|
|
|
|
|
|
|
void Copy( )
|
|
|
|
|
|
{
|
|
|
|
|
|
QString selected_text;
|
|
|
|
|
|
// You need a pair of indexes to find the row changes
|
|
|
|
|
|
QModelIndex previous = indexes.first();
|
|
|
|
|
|
indexes.removeFirst();
|
|
|
|
|
|
foreach(current, indexes)
|
|
|
|
|
|
{
|
|
|
|
|
|
QVariant data = model->data(current);
|
|
|
|
|
|
QString text = data.toString();
|
|
|
|
|
|
// At this point `text` contains the text in one cell
|
|
|
|
|
|
selected_text.append(text);
|
|
|
|
|
|
// If you are at the start of the row the row number of the previous index
|
|
|
|
|
|
// isn't the same. Text is followed by a row separator, which is a newline.
|
|
|
|
|
|
if (current.row() != previous.row())
|
|
|
|
|
|
{
|
|
|
|
|
|
selected_text.append('\n');
|
|
|
|
|
|
}
|
|
|
|
|
|
// Otherwise it's the same row, so append a column separator, which is a tab.
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
selected_text.append('\t');
|
|
|
|
|
|
}
|
|
|
|
|
|
previous = current;
|
|
|
|
|
|
}
|
|
|
|
|
|
QApplication.clipboard().setText(selected_text);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
2017-01-15 21:01:40 +01:00
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionExecute_SQL_triggered()
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->execute();
|
|
|
|
|
|
}
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-25 06:54:21 +01:00
|
|
|
|
void MainWindow::on_actionExplain_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->explain(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
void MainWindow::on_actionExplain_Analyze_triggered()
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
2017-01-25 06:54:21 +01:00
|
|
|
|
tab->explain(true);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionCancel_triggered()
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QueryTab *tab = GetActiveQueryTab();
|
|
|
|
|
|
if (tab) {
|
|
|
|
|
|
tab->cancel();
|
|
|
|
|
|
}
|
2017-01-19 17:48:44 +01:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::closeEvent(QCloseEvent *event)
|
|
|
|
|
|
{
|
2017-01-21 18:16:57 +01:00
|
|
|
|
// TODO collect which files need saving
|
|
|
|
|
|
// if (!m_queryTextChanged || continueWithoutSaving()) {
|
|
|
|
|
|
// event->accept();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else {
|
|
|
|
|
|
// event->ignore();
|
|
|
|
|
|
// }
|
2017-01-19 17:48:44 +01:00
|
|
|
|
}
|
2017-01-21 08:09:12 +01:00
|
|
|
|
|
|
|
|
|
|
void MainWindow::showEvent(QShowEvent *event)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!event->spontaneous()) {
|
2017-01-21 18:16:57 +01:00
|
|
|
|
// m_queryTextChanged = false;
|
2017-01-21 08:09:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
event->accept();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-21 18:16:57 +01:00
|
|
|
|
void MainWindow::on_actionNew_SQL_triggered()
|
|
|
|
|
|
{
|
2017-01-22 08:50:41 +01:00
|
|
|
|
newSqlPage()->newdoc();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_tabWidget_tabCloseRequested(int index)
|
|
|
|
|
|
{
|
|
|
|
|
|
QWidget *widget = ui->tabWidget->widget(index);
|
|
|
|
|
|
QueryTab *qt = dynamic_cast<QueryTab*>(widget);
|
|
|
|
|
|
if (qt->canClose()) {
|
|
|
|
|
|
ui->tabWidget->removeTab(index);
|
|
|
|
|
|
}
|
2017-02-01 18:01:02 +01:00
|
|
|
|
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-25 06:54:21 +01:00
|
|
|
|
|
2017-02-01 18:01:02 +01:00
|
|
|
|
|
|
|
|
|
|
void MainWindow::on_actionShow_connection_manager_triggered()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_masterController->showConnectionManager();
|
|
|
|
|
|
}
|