When files are dropped on a database window open them as sql files.

This commit is contained in:
eelke 2021-06-16 19:20:03 +02:00
parent b04b947633
commit 6d08b40309
3 changed files with 55 additions and 22 deletions

View file

@ -14,6 +14,7 @@
#include <QMenuBar>
#include <QMessageBox>
#include <QMetaMethod>
#include <QMimeData>
#include <QStandardPaths>
#include <QStatusBar>
#include <QTableView>
@ -44,6 +45,8 @@ DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent)
initMenus();
QMetaObject::connectSlotsByName(this);
setAcceptDrops(true);
}
DatabaseWindow::~DatabaseWindow() = default;
@ -397,6 +400,21 @@ void DatabaseWindow::closeTab(int index)
}
}
void DatabaseWindow::openSqlFile(QString file_name)
{
if ( ! file_name.isEmpty()) {
auto *ct = new QueryTool(this, this);
if (ct->load(file_name)) {
ct->addAction(actionExecuteQuery);
addPage(ct, ct->title());
}
else {
delete ct;
}
}
}
void DatabaseWindow::catalogLoaded()
{
try {
@ -557,16 +575,7 @@ void DatabaseWindow::on_actionOpenSql_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()) {
auto *ct = new QueryTool(this, this);
if (ct->load(file_name)) {
ct->addAction(actionExecuteQuery);
addPage(ct, ct->title());
}
else {
delete ct;
}
}
openSqlFile(file_name);
}
void DatabaseWindow::on_actionPasteLangString_triggered()
@ -664,3 +673,19 @@ void DatabaseWindow::showStatusBarMessage(QString message)
{
statusBar()->showMessage(message);
}
void DatabaseWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void DatabaseWindow::dropEvent(QDropEvent *event)
{
foreach (const QUrl &url, event->mimeData()->urls()) {
QString file_name = url.toLocalFile();
qDebug() << "Dropped file:" << file_name;
openSqlFile(file_name);
}
}

View file

@ -128,6 +128,8 @@ private:
void newCatalogInspectorPage(QString caption, NamespaceFilter filter);
void newServerInspectorPage();
void closeTab(int index);
void openSqlFile(QString file_name);
private slots:
void catalogLoaded();
void tableSelected(Oid tableoid);
@ -167,6 +169,11 @@ public:
virtual void setIconForWidget(QWidget *widget, QIcon icon) override;
virtual std::shared_ptr<OpenDatabase> openDatabase() override;
virtual void showStatusBarMessage(QString message) override;
// QWidget interface
protected:
virtual void dragEnterEvent(QDragEnterEvent *event) override;
virtual void dropEvent(QDropEvent *event) override;
};
#endif // MAINWINDOW_H

View file

@ -46,6 +46,7 @@ QueryTool::QueryTool(IDatabaseWindow *context, QWidget *parent)
}
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTool::queryTextChanged);
ui->queryEdit->setAcceptDrops(false);
m_queryParamListController = new QueryParamListController(ui->paramTableView, m_context->openDatabase(), this);
connect(ui->addButton, &QPushButton::clicked, m_queryParamListController,