diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 4398541..0877411 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,8 @@ DatabaseWindow::DatabaseWindow(MasterController *master, QWidget *parent) initMenus(); QMetaObject::connectSlotsByName(this); + + setAcceptDrops(true); } DatabaseWindow::~DatabaseWindow() = default; @@ -397,20 +400,35 @@ 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 { - m_database = loadWatcher.future().result(); + try { + m_database = loadWatcher.future().result(); -// for (auto f : { "user", "pg_catalog", "information_schema" }) { -// // TODO open inspector windows -// } -// newCreateTablePage(); - on_actionNewSql_triggered(); - } catch (const OpenDatabaseException &ex) { - QMessageBox::critical(this, "Error reading database", ex.text()); - close(); - } + // for (auto f : { "user", "pg_catalog", "information_schema" }) { + // // TODO open inspector windows + // } + // newCreateTablePage(); + on_actionNewSql_triggered(); + } catch (const OpenDatabaseException &ex) { + QMessageBox::critical(this, "Error reading database", ex.text()); + close(); + } } void DatabaseWindow::tableSelected(Oid tableoid) @@ -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() @@ -662,5 +671,21 @@ std::shared_ptr DatabaseWindow::openDatabase() void DatabaseWindow::showStatusBarMessage(QString message) { - statusBar()->showMessage(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); + } } diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index 424f7f9..846f85b 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -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() 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 diff --git a/pglab/QueryTool.cpp b/pglab/QueryTool.cpp index 0bd1cd7..ebc1fbe 100644 --- a/pglab/QueryTool.cpp +++ b/pglab/QueryTool.cpp @@ -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,