From b8ef67b519e3f824ec7c1cfff186a31d69a24173 Mon Sep 17 00:00:00 2001 From: Shengjie Xu Date: Wed, 29 Nov 2023 17:16:19 -0500 Subject: [PATCH] Various improvements on ImagePack support --- CMakeLists.txt | 5 ++++ tooldialog/imagepacktooldialog.cpp | 9 ++++++ tooldialog/imagepacktooldialog.h | 2 ++ tooldialog/imagepacktooldialog.ui | 2 +- util/filedropaccepter.cpp | 42 ++++++++++++++++++++++++++ util/filedropaccepter.h | 26 ++++++++++++++++ util/filepathvalidate.cpp | 48 ++++++++++++++++++++++++++++++ util/filepathvalidate.h | 12 ++++++++ 8 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 util/filedropaccepter.cpp create mode 100644 util/filedropaccepter.h create mode 100644 util/filepathvalidate.cpp create mode 100644 util/filepathvalidate.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 93452b4..8a15ce1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,11 @@ set(PROJECT_SOURCES ExecutionData.h resource.qrc + util/filepathvalidate.h + util/filepathvalidate.cpp + util/filedropaccepter.h + util/filedropaccepter.cpp + tooldialog/imagepacktooldialog.h tooldialog/imagepacktooldialog.cpp tooldialog/imagepacktooldialog.ui diff --git a/tooldialog/imagepacktooldialog.cpp b/tooldialog/imagepacktooldialog.cpp index f52b82f..4982ba5 100644 --- a/tooldialog/imagepacktooldialog.cpp +++ b/tooldialog/imagepacktooldialog.cpp @@ -7,6 +7,7 @@ #include #include "ExecutionData.h" #include "executewindow.h" +#include "util/filepathvalidate.h" QString ImagePackToolDialog::STATE_lastImagePath; QColor ImagePackToolDialog::STATE_lastColor(Qt::white); @@ -100,6 +101,14 @@ ImagePackToolDialog::ImagePackToolDialog(QWidget *parent) : STATE_lastExportOptionEnable = checked; }); + forkParamDropHandler = new FileDropAccepter(this); + forkParamDropHandler->setVerifyCallBack(FilePathValidate::isReadableImageFile); + connect(forkParamDropHandler, &FileDropAccepter::fileDropped, this, [this](const QString& path, QObject* droppedTo) { + if (droppedTo == ui->forkParamListWidget) { + addImageForkParamImpl(path); + } + }); + ui->forkParamListWidget->installEventFilter(forkParamDropHandler); } ImagePackToolDialog::~ImagePackToolDialog() diff --git a/tooldialog/imagepacktooldialog.h b/tooldialog/imagepacktooldialog.h index 1fa8579..f2f3a99 100644 --- a/tooldialog/imagepacktooldialog.h +++ b/tooldialog/imagepacktooldialog.h @@ -4,6 +4,7 @@ #include #include #include "ExecutionData.h" +#include "util/filedropaccepter.h" namespace Ui { class ImagePackToolDialog; @@ -49,6 +50,7 @@ private slots: static bool STATE_lastExportOptionEnable; private: Ui::ImagePackToolDialog *ui; + FileDropAccepter* forkParamDropHandler = nullptr; ExecutionInfo initialInfo; }; diff --git a/tooldialog/imagepacktooldialog.ui b/tooldialog/imagepacktooldialog.ui index f8ca22b..a3cf052 100644 --- a/tooldialog/imagepacktooldialog.ui +++ b/tooldialog/imagepacktooldialog.ui @@ -152,7 +152,7 @@ true - QAbstractItemView::InternalMove + QAbstractItemView::DragDrop Qt::MoveAction diff --git a/util/filedropaccepter.cpp b/util/filedropaccepter.cpp new file mode 100644 index 0000000..19b2c6f --- /dev/null +++ b/util/filedropaccepter.cpp @@ -0,0 +1,42 @@ +#include "filedropaccepter.h" +#include +#include +#include + +FileDropAccepter::FileDropAccepter(QObject *parent) + : QObject{parent} +{ + +} + +bool FileDropAccepter::eventFilter(QObject *watched, QEvent *event) +{ + // 因为 QDragEnterEvent 继承自 QDragMoveEvent, 该事件又继承自 QDropEvent + // 所以这里 if-else 的顺序不能调换 + if (QDragEnterEvent* e = dynamic_cast(event)) { + if (e->mimeData()->hasUrls()) { + QString path = e->mimeData()->urls().first().toLocalFile(); + if (!verifyCB || verifyCB(path)) { + e->acceptProposedAction(); + return true; + } + } + } else if (QDragMoveEvent* e = dynamic_cast(event)) { + if (e->mimeData()->hasUrls()) { + return true; + } + } else if (QDropEvent* e = dynamic_cast(event)) { + bool isHandled = false; + for (const QUrl &url : e->mimeData()->urls()) { + QString path = url.toLocalFile(); + isHandled = true; + if (!verifyCB || verifyCB(path)) { + emit fileDropped(path, watched); + } + } + if (isHandled) { + return true; + } + } + return QObject::eventFilter(watched, event); +} diff --git a/util/filedropaccepter.h b/util/filedropaccepter.h new file mode 100644 index 0000000..fd01857 --- /dev/null +++ b/util/filedropaccepter.h @@ -0,0 +1,26 @@ +#ifndef FILEDROPACCEPTER_H +#define FILEDROPACCEPTER_H + +#include + +class FileDropAccepter : public QObject +{ + Q_OBJECT +public: + explicit FileDropAccepter(QObject *parent = nullptr); + + virtual bool eventFilter(QObject *watched, QEvent *event) override; + + void setVerifyCallBack(std::function cb) { + verifyCB = cb; + } + +signals: + void fileDropped(const QString& path, QObject* droppedTo); + +private: + std::function verifyCB; + +}; + +#endif // FILEDROPACCEPTER_H diff --git a/util/filepathvalidate.cpp b/util/filepathvalidate.cpp new file mode 100644 index 0000000..3416fa9 --- /dev/null +++ b/util/filepathvalidate.cpp @@ -0,0 +1,48 @@ +#include "filepathvalidate.h" + +#include +#include +namespace { + +const QSet SupportedImageExtensions { + QStringLiteral("blp"), + QStringLiteral("bmp"), + QStringLiteral("dds"), + QStringLiteral("dib"), + QStringLiteral("eps"), + QStringLiteral("gif"), + QStringLiteral("icns"), + QStringLiteral("ico"), + QStringLiteral("im"), + QStringLiteral("jfif"), + QStringLiteral("jpg"), + QStringLiteral("jpeg"), + QStringLiteral("j2k"), + QStringLiteral("j2p"), + QStringLiteral("j2x"), + QStringLiteral("msp"), + QStringLiteral("pcx"), + QStringLiteral("png"), + QStringLiteral("apng"), + QStringLiteral("pbm"), + QStringLiteral("pgm"), + QStringLiteral("ppm"), + QStringLiteral("pnm"), + QStringLiteral("sgi"), + QStringLiteral("spi"), + QStringLiteral("tga"), + QStringLiteral("tif"), + QStringLiteral("tiff"), + QStringLiteral("webp"), + QStringLiteral("xbm"), +}; + +} // end anonymous namespace + +bool FilePathValidate::isReadableImageFile(const QString& path) +{ + const QFileInfo info(path); + if (!info.exists() || !info.isFile() || !info.isReadable()) + return false; + return (SupportedImageExtensions.contains(info.suffix().toLower())); +} diff --git a/util/filepathvalidate.h b/util/filepathvalidate.h new file mode 100644 index 0000000..ad6c8ed --- /dev/null +++ b/util/filepathvalidate.h @@ -0,0 +1,12 @@ +#ifndef FILEPATHVALIDATE_H +#define FILEPATHVALIDATE_H + +#include + +namespace FilePathValidate +{ +bool isReadableImageFile(const QString& path); + +} // end namespace FilePathValidate + +#endif // FILEPATHVALIDATE_H