feat(editor): drag-and-drop open file
This commit is contained in:
parent
14b0667fc9
commit
c8880e0edc
4 changed files with 57 additions and 0 deletions
|
@ -438,6 +438,39 @@ void MainWindow::connectActionHandlers() {
|
|||
});
|
||||
}
|
||||
|
||||
void MainWindow::openFile(std::string path) {
|
||||
// Don't ask for confirmation if running a debug build (makes development easier)
|
||||
#ifdef NDEBUG
|
||||
// Ask if the user wants to save their changes
|
||||
// https://stackoverflow.com/a/33890731
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText("Save changes before creating new document?");
|
||||
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
||||
msgBox.setDefaultButton(QMessageBox::Save);
|
||||
int result = msgBox.exec();
|
||||
|
||||
if (result == QMessageBox::Cancel) return;
|
||||
if (result == QMessageBox::Save) {
|
||||
std::optional<std::string> path;
|
||||
if (!gDataModel->HasFile())
|
||||
path = openFileDialog("Openblocks Level (*.obl)", ".obl", QFileDialog::AcceptSave, QString::fromStdString("Save " + gDataModel->name));
|
||||
if (!path || path == "") return;
|
||||
|
||||
gDataModel->SaveToFile(path);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::shared_ptr<DataModel> newModel = DataModel::LoadFromFile(path);
|
||||
editModeDataModel = newModel;
|
||||
gDataModel = newModel;
|
||||
newModel->Init();
|
||||
ui->explorerView->updateRoot(newModel);
|
||||
|
||||
// Reset running state
|
||||
placeDocument->setRunState(RUN_STOPPED);
|
||||
updateToolbars();
|
||||
}
|
||||
|
||||
void MainWindow::updateToolbars() {
|
||||
ui->actionToolSelect->setChecked(selectedTool == TOOL_SELECT);
|
||||
ui->actionToolMove->setChecked(selectedTool == TOOL_MOVE);
|
||||
|
|
|
@ -57,6 +57,8 @@ public:
|
|||
void openScriptDocument(std::shared_ptr<Script>);
|
||||
void closeScriptDocument(std::shared_ptr<Script>);
|
||||
|
||||
void openFile(std::string path);
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
private:
|
||||
PlaceDocument* placeDocument;
|
||||
|
|
|
@ -9,14 +9,17 @@
|
|||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <qboxlayout.h>
|
||||
#include <qdebug.h>
|
||||
#include <qevent.h>
|
||||
#include <qmargins.h>
|
||||
#include <qmdisubwindow.h>
|
||||
#include <qlayout.h>
|
||||
#include <qmimedata.h>
|
||||
|
||||
PlaceDocument::PlaceDocument(QWidget* parent):
|
||||
QMdiSubWindow(parent) {
|
||||
placeWidget = new MainGLWidget;
|
||||
setAcceptDrops(true);
|
||||
setWidget(placeWidget);
|
||||
setWindowTitle("Place");
|
||||
|
||||
|
@ -136,3 +139,18 @@ void PlaceDocument::init() {
|
|||
MainWindow* mainWnd = dynamic_cast<MainWindow*>(window());
|
||||
// mainWnd->openScriptDocument(script);
|
||||
}
|
||||
|
||||
void PlaceDocument::dragEnterEvent(QDragEnterEvent* evt) {
|
||||
// https://stackoverflow.com/a/14895393/16255372
|
||||
if (evt->mimeData()->hasUrls()) {
|
||||
evt->acceptProposedAction();
|
||||
}
|
||||
}
|
||||
|
||||
void PlaceDocument::dropEvent(QDropEvent* evt) {
|
||||
auto urls = evt->mimeData()->urls();
|
||||
if (urls.size() == 0) return;
|
||||
QString fileName = urls[0].toLocalFile();
|
||||
MainWindow* mainWnd = dynamic_cast<MainWindow*>(window());
|
||||
mainWnd->openFile(fileName.toStdString());
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "mainglwidget.h"
|
||||
#include <qevent.h>
|
||||
#include <qmdisubwindow.h>
|
||||
#include <QBasicTimer>
|
||||
|
||||
|
@ -25,4 +26,7 @@ public:
|
|||
|
||||
void closeEvent(QCloseEvent *closeEvent) override;
|
||||
void init();
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent*) override;
|
||||
void dropEvent(QDropEvent*) override;
|
||||
};
|
Loading…
Add table
Reference in a new issue