DatabaseWindow now provides some functionality to its child components through the IDatabaseWindow interface.

This way children do not need to include the full header to get access to some utility functions for changing
the titles and icons of tabpages (and in fact do not need to know that there are tabs, could be something else)
This commit is contained in:
eelke 2019-08-16 08:29:27 +02:00
parent 6d4df99100
commit 1a2ec6a224
6 changed files with 84 additions and 135 deletions

View file

@ -10,6 +10,7 @@
#include <QMessageBox>
#include <QMetaMethod>
#include <QStandardPaths>
#include <QStatusBar>
#include <QTableView>
#include "EditTableWidget.h"
@ -434,7 +435,7 @@ void DatabaseWindow::on_actionInspectUserSchemas_triggered()
void DatabaseWindow::on_actionNewSql_triggered()
{
auto *ct = new QueryTool(m_database, this);
auto *ct = new QueryTool(this, this);
addPage(ct, "query");
ct->newdoc();
}
@ -445,7 +446,7 @@ void DatabaseWindow::on_actionOpenSql_triggered()
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
if ( ! file_name.isEmpty()) {
auto *ct = new QueryTool(m_database, this);
auto *ct = new QueryTool(this, this);
if (ct->load(file_name)) {
addPage(ct, "");
}
@ -484,3 +485,33 @@ void DatabaseWindow::on_actionShowConnectionManager_triggered()
m_masterController->showConnectionManager();
}
void DatabaseWindow::setTitleForWidget(QWidget *widget, QString title, QString hint)
{
int i = m_tabWidget->indexOf(widget);
if (i >= 0) {
m_tabWidget->setTabText(i, title);
m_tabWidget->setTabToolTip(i, hint);
}
}
void DatabaseWindow::setIconForWidget(QWidget *widget, QIcon icon)
{
int i = m_tabWidget->indexOf(widget);
if (i >= 0) {
m_tabWidget->setTabIcon(i, icon);
}
}
std::shared_ptr<OpenDatabase> DatabaseWindow::openDatabase()
{
return m_database;
}
void DatabaseWindow::showStatusBarMessage(QString message)
{
statusBar()->showMessage(message);
}

View file

@ -5,6 +5,7 @@
#include "OpenDatabase.h"
#include "Pgsql_Connection.h"
#include "ControllableTask.h"
#include "IDatabaseWindow.h"
#include <QFutureWatcher>
#include <QMainWindow>
#include <memory>
@ -24,11 +25,10 @@ class QMenu;
class QTabWidget;
class QToolBar;
/** This is the class for windows that handle tasks for a specific database/catalog
*
*/
class DatabaseWindow : public QMainWindow {
class DatabaseWindow : public QMainWindow, public IDatabaseWindow {
Q_OBJECT
public:
DatabaseWindow(MasterController *master, QWidget *parent);
@ -136,6 +136,13 @@ private slots:
void on_actionSaveSqlAs_triggered();
void on_actionSaveCopyOfSqlAs_triggered();
void on_actionShowConnectionManager_triggered();
// IDatabaseWindow interface
public:
virtual void setTitleForWidget(QWidget *widget, QString title, QString hint) override;
virtual void setIconForWidget(QWidget *widget, QIcon icon) override;
virtual std::shared_ptr<OpenDatabase> openDatabase() override;
virtual void showStatusBarMessage(QString message) override;
};
#endif // MAINWINDOW_H

20
pglab/IDatabaseWindow.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef IDATABASEWINDOW_H
#define IDATABASEWINDOW_H
#include <QIcon>
#include <QString>
class OpenDatabase;
class QWidget;
/** Abstract class definition to make some functions
* available to other classes without having to know everything about the window.
*/
class IDatabaseWindow {
public:
virtual void setTitleForWidget(QWidget* widget, QString title, QString hint) = 0;
virtual void setIconForWidget(QWidget* widget, QIcon icon) = 0;
virtual std::shared_ptr<OpenDatabase> openDatabase() = 0;
virtual void showStatusBarMessage(QString message) = 0;
};
#endif // IDATABASEWINDOW_H

View file

@ -6,6 +6,7 @@
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QStatusBar>
#include <QTabWidget>
#include <QTextCodec>
#include <QTextDocumentFragment>
@ -19,17 +20,20 @@
#include "util.h"
#include "GlobalIoService.h"
#include "UserConfiguration.h"
#include "IDatabaseWindow.h"
QueryTool::QueryTool(std::shared_ptr<OpenDatabase> open_database, QWidget *parent)
QueryTool::QueryTool(IDatabaseWindow *context, QWidget *parent)
: QWidget(parent)
, m_context(context)
, ui(new Ui::QueryTab)
, m_dbConnection(*getGlobalAsioIoService())
{
ui->setupUi(this);
m_config = open_database->config();
m_catalog = open_database->catalog();
auto db = context->openDatabase();
m_config = db->config();
m_catalog = db->catalog();
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTool::connectionStateChanged);
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTool::receiveNotice);
@ -39,11 +43,9 @@ QueryTool::QueryTool(std::shared_ptr<OpenDatabase> open_database, QWidget *paren
highlighter = new SqlSyntaxHighlighter(ui->queryEdit->document());
highlighter->setTypes(*m_catalog->types());
initActions();
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTool::queryTextChanged);
m_queryParamListController = new QueryParamListController(ui->paramTableView, open_database, this);
m_queryParamListController = new QueryParamListController(ui->paramTableView, m_context->openDatabase(), this);
connect(ui->addButton, &QPushButton::clicked, m_queryParamListController,
&QueryParamListController::on_addParam);
connect(ui->removeButton, &QPushButton::clicked, m_queryParamListController,
@ -226,7 +228,7 @@ void QueryTool::setFileName(const QString &filename)
m_fileName = filename;
QFileInfo fileInfo(filename);
QString fn(fileInfo.fileName());
// context()->setCaption(this, fn, m_fileName);
m_context->setTitleForWidget(this, fn, m_fileName);
}
bool QueryTool::continueWithoutSavingWarning()
@ -282,29 +284,27 @@ void QueryTool::queryTextChanged()
m_queryTextChanged = true;
}
void QueryTool::connectionStateChanged(ASyncDBConnection::State state)
{
QString iconname;
switch (state) {
case ASyncDBConnection::State::NotConnected:
case ASyncDBConnection::State::Connecting:
iconname = "document_red.png";
iconname = "red.png";
break;
case ASyncDBConnection::State::Connected:
iconname = "document_green.png";
iconname = "green.png";
break;
case ASyncDBConnection::State::QuerySend:
case ASyncDBConnection::State::CancelSend:
iconname = "document_yellow.png";
iconname = "yellow.png";
break;
case ASyncDBConnection::State::Terminating:
break;
}
// context()->setIcon(this, iconname);
m_context->setIconForWidget(this, QIcon(":/icons/16x16/document_" + iconname));
}
void QueryTool::addLog(QString s)
{
QTextCursor text_cursor = QTextCursor(ui->edtLog->document());
@ -390,12 +390,13 @@ void QueryTool::explain_ready(ExplainRoot::SPtr explain)
ui->explainTreeView->setColumnWidth(6, 600);
ui->tabWidget->setCurrentWidget(ui->explainTab);
// context()->showStatusMessage(tr("Explain ready."));
m_context->showStatusBarMessage(tr("Explain ready."));
}
else {
addLog("Explain no result");
addLog(tr("Explain no result"));
ui->tabWidget->setCurrentWidget(ui->messageTab);
// statusBar()->showMessage(tr("Explain failed."));
m_context->showStatusBarMessage(tr("Explain no result"));
}
}
@ -417,27 +418,6 @@ std::string QueryTool::getCommandUtf8() const
return getCommand().toUtf8().data();
}
//QTabWidget *QueryTab::getTabWidget()
//{
// QWidget * w = parentWidget();
// QWidget * p = w->parentWidget();
// QTabWidget *tw = dynamic_cast<QTabWidget*>(p);
// return tw;
//}
//void QueryTab::setTabCaption(const QString &caption, const QString &tooltip)
//{
// QTabWidget *tabwidget = getTabWidget();
// if (tabwidget) {
// int i = tabwidget->indexOf(this);
// if (i >= 0) {
// tabwidget->setTabText(i, caption);
// tabwidget->setTabToolTip(i, tooltip);
// }
// }
//}
void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms)
{
if (dbres) {
@ -564,14 +544,6 @@ void QueryTool::clearResult()
void QueryTool::copyQueryAsCString()
{
// QString command;
// QTextCursor cursor = ui->queryEdit->textCursor();
// if (cursor.hasSelection()) {
// command = cursor.selection().toPlainText();
// }
// else {
// command = ui->queryEdit->toPlainText();
// }
QString command = getCommand();
QString cs = ConvertToMultiLineCString(command);
QApplication::clipboard()->setText(cs);
@ -583,7 +555,6 @@ void QueryTool::copyQueryAsCString()
void QueryTool::copyQueryAsRawCppString()
{
QString command = getCommand();
//auto sql = getAllOrSelectedSql();
QString cs = ConvertToMultiLineRawCppString(command);
QApplication::clipboard()->setText(cs);
}
@ -623,69 +594,3 @@ void QueryTool::exportData()
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
exportDataToFilename(file_name);
}
void QueryTool::initActions()
{
{
auto ac = new QAction(QIcon(":/icons/script_save.png"), tr("Save SQL"), this);
ac->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
connect(ac, &QAction::triggered, this, &QueryTool::save);
m_saveSqlAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/script_save.png"), tr("Save SQL as"), this);
connect(ac, &QAction::triggered, this, &QueryTool::saveAs);
m_saveSqlAsAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/script_save.png"), tr("Save copy of SQL as"), this);
connect(ac, &QAction::triggered, this, &QueryTool::saveCopyAs);
m_saveCopyOfSqlAsAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/table_save.png"), tr("&Export data"), this);
connect(ac, &QAction::triggered, this, &QueryTool::exportData);
m_exportDataAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/token_shortland_character.png"), tr("Copy as C string"), this);
connect(ac, &QAction::triggered, this, &QueryTool::copyQueryAsCString);
m_copyQueryAsCStringAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/token_shortland_character.png"), tr("Copy as raw C++ string"), this);
connect(ac, &QAction::triggered, this, &QueryTool::copyQueryAsRawCppString);
m_copyQueryAsRawCppStringAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/script_go.png"), tr("Execute"), this);
connect(ac, &QAction::triggered, this, &QueryTool::execute);
ac->setShortcut(QKeySequence(Qt::Key_F5));
m_executeAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/lightbulb_off.png"), tr("Explain"), this);
connect(ac, &QAction::triggered, [this] { explain(false); });
ac->setShortcut(QKeySequence(Qt::Key_F7));
m_explainAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/lightbulb.png"), tr("Analyze"), this);
connect(ac, &QAction::triggered, [this] { explain(true); });
ac->setShortcut(QKeySequence(Qt::SHIFT + Qt::Key_F7));
m_analyzeAction = ac;
}
{
auto ac = new QAction(QIcon(":/icons/script_delete.png"), tr("Cancel"), this);
connect(ac, &QAction::triggered, this, &QueryTool::cancel);
m_analyzeAction = ac;
}
}
QList<QAction *> QueryTool::actions()
{
return { m_saveSqlAction, m_saveSqlAsAction, m_saveCopyOfSqlAsAction,
m_exportDataAction,
m_copyQueryAsCStringAction, m_copyQueryAsRawCppStringAction,
m_executeAction, m_explainAction, m_analyzeAction };
}

View file

@ -10,6 +10,8 @@
#include <QWidget>
#include <memory>
class IDatabaseWindow;
namespace Ui {
class QueryTab;
}
@ -33,7 +35,7 @@ class PgDatabaseCatalog;
class QueryTool : public QWidget {
Q_OBJECT
public:
QueryTool(std::shared_ptr<OpenDatabase> open_database, QWidget *parent = nullptr);
QueryTool(IDatabaseWindow *context, QWidget *parent = nullptr);
~QueryTool() override;
void newdoc();
@ -51,8 +53,6 @@ public:
bool isNew() const { return m_new; }
void focusEditor();
QList<QAction *> actions();
public slots:
void execute();
/// Save the document under its current name, a file save dialog will be shown if this is a new document
@ -69,22 +69,12 @@ public slots:
private:
using ResultTabContainer = std::vector<TuplesResultWidget*>;
IDatabaseWindow *m_context;
Ui::QueryTab *ui;
SqlSyntaxHighlighter* highlighter;
ConnectionConfig m_config;
StopWatch m_stopwatch;
QAction *m_saveSqlAction = nullptr;
QAction *m_saveSqlAsAction = nullptr;
QAction *m_saveCopyOfSqlAsAction = nullptr;
QAction *m_exportDataAction = nullptr;
QAction *m_copyQueryAsCStringAction = nullptr;
QAction *m_copyQueryAsRawCppStringAction = nullptr;
QAction *m_executeAction = nullptr;
QAction *m_explainAction = nullptr;
QAction *m_analyzeAction = nullptr;
QueryParamListController *m_queryParamListController = nullptr;
bool m_new = true;
@ -102,18 +92,13 @@ private:
bool saveSqlTo(const QString &filename);
QString promptUserForSaveSqlFilename();
void addLog(QString s);
QString getCommand() const;
std::string getCommandUtf8() const;
//QTabWidget *getTabWidget();
//void setTabCaption(const QString &caption, const QString &tooltip);
void clearResult();
void markError(const Pgsql::ErrorDetails &details);
void initActions();
private slots:

View file

@ -85,6 +85,7 @@ PropertyProxyModel.cpp \
widgets/CatalogSequencesPage.cpp
HEADERS += \
IDatabaseWindow.h \
NotificationListWidget.h \
NotificationModel.h \
NotificationService.h \