From 8eb9f5bf39eb41f3f602a77964f29092c6269e0d Mon Sep 17 00:00:00 2001 From: blocksmith314 Date: Sat, 7 Nov 2026 10:15:20 +0800 Subject: [PATCH] feat(ui): open files dropped onto the window Accept drops of local files on MainWindow, accept the drag when at least one local file URL is present, and load the first one through LoadFile, so drops share the Open dialog path (recent files included). Adds a GUI test that sends drag-enter and drop events to the window. --- mainwindow.cpp | 28 ++++++++++++++++++++++++++++ mainwindow.h | 2 ++ test/test_gui.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index 4e97268..d107374 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -17,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +29,7 @@ #include #include #include +#include #include #include @@ -43,6 +47,7 @@ MainWindow::~MainWindow() = default; void MainWindow::SetupUi() { setWindowTitle("Digital Image Analyzer - H.264/H.265 Bitstream Analyzer"); resize(1400, 800); + setAcceptDrops(true); QMenu* fileMenu = menuBar()->addMenu("File"); QAction* openAct = fileMenu->addAction("Open Bitstream..."); @@ -288,6 +293,29 @@ void MainWindow::OpenFile() { } } +void MainWindow::dragEnterEvent(QDragEnterEvent* event) { + const QList urls = event->mimeData()->urls(); + for (const QUrl& url : urls) { + if (url.isLocalFile()) { + event->acceptProposedAction(); + return; + } + } + event->ignore(); +} + +void MainWindow::dropEvent(QDropEvent* event) { + const QList urls = event->mimeData()->urls(); + for (const QUrl& url : urls) { + if (url.isLocalFile()) { + LoadFile(url.toLocalFile()); + event->acceptProposedAction(); + return; + } + } + event->ignore(); +} + bool MainWindow::LoadFile(const QString& path) { std::string sp = path.toUtf8().toStdString(); diff --git a/mainwindow.h b/mainwindow.h index 6b566db..88dd4e6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -37,6 +37,8 @@ class MainWindow : public QMainWindow { protected: void closeEvent(QCloseEvent* event) override; + void dragEnterEvent(QDragEnterEvent* event) override; + void dropEvent(QDropEvent* event) override; private slots: void OpenFile(); diff --git a/test/test_gui.cpp b/test/test_gui.cpp index 2db2276..63ffa91 100644 --- a/test/test_gui.cpp +++ b/test/test_gui.cpp @@ -1,7 +1,10 @@ #include #include +#include +#include #include +#include #include #include #include @@ -10,6 +13,7 @@ #include #include #include +#include #include #include @@ -177,6 +181,31 @@ TEST(MainWindowTest, LoadsFileAndUpdatesViews) { EXPECT_TRUE(status_has_nal_count); } +TEST(MainWindowTest, OpensFileByDrop) { + if (!exists(data_path("output.h264"))) { + GTEST_SKIP() << "H.264 fixture missing"; + } + + MainWindow window; + window.show(); + + QMimeData mime; + mime.setUrls( + {QUrl::fromLocalFile(QString::fromStdString(data_path("output.h264")))}); + + QDragEnterEvent enter(QPointF(10, 10), Qt::CopyAction, &mime, Qt::LeftButton, + Qt::NoModifier); + QApplication::sendEvent(&window, &enter); + EXPECT_TRUE(enter.isAccepted()); + + QDropEvent drop(QPointF(10, 10), Qt::CopyAction, &mime, Qt::LeftButton, + Qt::NoModifier); + QApplication::sendEvent(&window, &drop); + QApplication::processEvents(); + + EXPECT_TRUE(window.windowTitle().contains("output.h264")); +} + TEST(MainWindowTest, StepForwardUsesSpinBoxValue) { if (!exists(data_path("sample_720p.mp4"))) { GTEST_SKIP() << "MP4 fixture missing";