#include "mainwindow.h" #include "ui_mainwindow.h" //#include "QueryResultModel.h" //#include "QueryExplainModel.h" #include #include #include #include #include #include #include #include #include #include #include "util.h" namespace pg = Pgsql; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->tabWidget->setDocumentMode(true); //ui->tabWidget->setTabsClosable(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(widget); return qt; } void MainWindow::setConfig(const ConnectionConfig &config) { m_config = config; QString title = "pglab - "; title += m_config.name().c_str(); setWindowTitle(title); newSqlPage(); } void MainWindow::QueueTask(TSQueue::t_Callable c) { m_taskQueue.add(c); // 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() { if (!m_taskQueue.empty()) { auto c = m_taskQueue.pop(); c(); if (!m_taskQueue.empty()) { QTimer::singleShot(0, this, SLOT(processCallableQueue())); } } } 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() { 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() { //close(); on_tabWidget_tabCloseRequested(ui->tabWidget->currentIndex()); } void MainWindow::on_actionAbout_triggered() { // } #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 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 // 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(widget); if (qt->canClose()) { ui->tabWidget->removeTab(index); } }