pgLab/src/MainWindow.cpp

255 lines
5.3 KiB
C++
Raw Normal View History

2017-02-06 21:41:45 +01:00
#include "MainWindow.h"
#include "ui_mainwindow.h"
#include <QStandardPaths>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextTable>
#include <QElapsedTimer>
#include <windows.h>
#include <algorithm>
#include <QCloseEvent>
2017-02-05 08:23:06 +01:00
#include <QMetaObject>
#include <QMetaMethod>
#include <querytab.h>
#include "util.h"
#include "MasterController.h"
#include "OpenDatabase.h"
namespace pg = Pgsql;
MainWindow::MainWindow(MasterController *master, QWidget *parent)
: ASyncWindow(parent)
, ui(new Ui::MainWindow)
, m_masterController(master)
{
ui->setupUi(this);
ui->tabWidget->setDocumentMode(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
QueryTab* MainWindow::newSqlPage()
{
QueryTab *qt = new QueryTab(this);
qt->setConfig(m_config);
ui->tabWidget->addTab(qt, "Tab");
ui->tabWidget->setCurrentWidget(qt);
qt->newdoc();
return qt;
}
QueryTab *MainWindow::GetActiveQueryTab()
{
QWidget *widget = ui->tabWidget->currentWidget();
QueryTab *qt = dynamic_cast<QueryTab*>(widget);
return qt;
}
void MainWindow::setConfig(const ConnectionConfig &config)
{
m_config = config;
auto res = OpenDatabase::createOpenDatabase(config);
if (res.valid()) {
m_database = res.get();
}
QString title = "pglab - ";
title += m_config.name().c_str();
setWindowTitle(title);
newSqlPage();
}
void MainWindow::on_actionLoad_SQL_triggered()
{
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);
}
}
void MainWindow::on_actionSave_SQL_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->save();
}
}
void MainWindow::on_actionSave_SQL_as_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->saveAs();
}
}
void MainWindow::on_actionSave_copy_of_SQL_as_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->saveCopyAs();
}
}
void MainWindow::on_actionExport_data_triggered()
{
2017-02-05 13:35:41 +01:00
QueryTab *tab = GetActiveQueryTab();
if (tab) {
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
QString file_name = QFileDialog::getSaveFileName(this,
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
2017-02-05 13:35:41 +01:00
tab->exportData(file_name);
}
}
void MainWindow::on_actionClose_triggered()
{
//close();
on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex());
}
void MainWindow::on_actionAbout_triggered()
{
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.8 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"
));
}
void MainWindow::on_actionExecute_SQL_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->execute();
}
}
void MainWindow::on_actionExplain_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->explain(false);
}
}
void MainWindow::on_actionExplain_Analyze_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->explain(true);
}
}
void MainWindow::on_actionCancel_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->cancel();
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// TODO collect which files need saving
std::vector<QString> files_to_save;
int n = ui->tabWidget->count();
for (int i = 0; i < n; ++i) {
QWidget *w = ui->tabWidget->widget(i);
QueryTab *qt = dynamic_cast<QueryTab*>(w);
if (qt) {
if (qt->isChanged()) {
files_to_save.push_back(qt->fileName());
}
}
}
// if (!m_queryTextChanged || continueWithoutSaving()) {
// event->accept();
// }
// else {
// event->ignore();
// }
}
void MainWindow::showEvent(QShowEvent *event)
{
if (!event->spontaneous()) {
// m_queryTextChanged = false;
}
event->accept();
}
void MainWindow::on_actionNew_SQL_triggered()
{
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);
}
}
void MainWindow::on_actionShow_connection_manager_triggered()
{
m_masterController->showConnectionManager();
}
void MainWindow::on_actionCopy_triggered()
{
// What should be copied?
QWidget *w = QApplication::focusWidget();
QTableView *tv = dynamic_cast<QTableView*>(w);
if (tv) {
copySelectionToClipboard(tv);
}
2017-02-05 08:23:06 +01:00
else {
const QMetaObject *meta = w->metaObject();
int i = meta->indexOfSlot("copy");
if (i != -1) {
QMetaMethod method = meta->method(i);
method.invoke(w, Qt::AutoConnection);
}
}
//this->ui->
}
2017-02-05 08:23:06 +01:00
void MainWindow::on_actionCopy_as_C_string_triggered()
{
// Find which edit is active, copy the selected text or all text if no selection present
// Put quote's around each line and add escapes.
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->copyQueryAsCString();
}
}