Skip to content

Commit

Permalink
Accepting file drops on whole window
Browse files Browse the repository at this point in the history
  • Loading branch information
houmaster committed Aug 4, 2020
1 parent 23fa37e commit ccbaaa9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 16 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QCoreApplication>
#include <QDesktopWidget>
#include <QTimer>
#include <QMimeData>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
Expand All @@ -38,6 +39,8 @@ MainWindow::MainWindow(QWidget *parent)
mUi->setupUi(this);
setContentsMargins(1, 0, 1, 1);

setAcceptDrops(true);

auto icon = QIcon(":images/16x16/icon.png");
icon.addFile(":images/32x32/icon.png");
icon.addFile(":images/64x64/icon.png");
Expand Down Expand Up @@ -338,6 +341,19 @@ void MainWindow::readSettings()
handleDarkThemeChanging(settings.darkTheme());
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}

void MainWindow::dropEvent(QDropEvent *event)
{
for (const QUrl &url : event->mimeData()->urls())
openFile(url.toLocalFile());
}

void MainWindow::closeEvent(QCloseEvent *event)
{
if (closeSession())
Expand Down
2 changes: 2 additions & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public Q_SLOTS:

protected:
void closeEvent(QCloseEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;

private:
void writeSettings();
Expand Down
16 changes: 0 additions & 16 deletions src/editors/EditorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <QMessageBox>
#include <QAction>
#include <QApplication>
#include <QMimeData>
#include <QToolBar>

EditorManager::EditorManager(QWidget *parent)
Expand All @@ -19,7 +18,6 @@ EditorManager::EditorManager(QWidget *parent)
setDockOptions(AnimatedDocks | AllowNestedDocks | AllowTabbedDocks);
setDocumentMode(true);
setContentsMargins(0, 1, 0, 0);
setAcceptDrops(true);
}

EditorManager::~EditorManager() = default;
Expand Down Expand Up @@ -63,20 +61,6 @@ int EditorManager::openNotSavedDialog(const QString& fileName)
return dialog.exec();
}

void EditorManager::dragEnterEvent(QDragEnterEvent *e)
{
const auto urls = e->mimeData()->urls();
if (!urls.isEmpty() && !urls.front().toLocalFile().isEmpty())
e->accept();
}

void EditorManager::dropEvent(QDropEvent *e)
{
const auto urls = e->mimeData()->urls();
for (const QUrl &url : urls)
openEditor(url.toLocalFile());
}

int EditorManager::getFocusedEditorIndex() const
{
auto index = 0;
Expand Down
4 changes: 0 additions & 4 deletions src/editors/EditorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class EditorManager final : public DockWindow
void editorRenamed(const QString &prevFileName, const QString &fileName);
void sourceTypeChanged(SourceType sourceType);

protected:
void dragEnterEvent(QDragEnterEvent *e) override;
void dropEvent(QDropEvent *e) override;

private:
int getFocusedEditorIndex() const;
bool focusEditorByIndex(int index);
Expand Down

0 comments on commit ccbaaa9

Please sign in to comment.