Skip to content

Commit

Permalink
Various improvements on ImagePack support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengjie Xu committed Nov 29, 2023
1 parent c8a0639 commit b8ef67b
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tooldialog/imagepacktooldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QStyle>
#include "ExecutionData.h"
#include "executewindow.h"
#include "util/filepathvalidate.h"

QString ImagePackToolDialog::STATE_lastImagePath;
QColor ImagePackToolDialog::STATE_lastColor(Qt::white);
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions tooldialog/imagepacktooldialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QDialog>
#include <QListWidgetItem>
#include "ExecutionData.h"
#include "util/filedropaccepter.h"

namespace Ui {
class ImagePackToolDialog;
Expand Down Expand Up @@ -49,6 +50,7 @@ private slots:
static bool STATE_lastExportOptionEnable;
private:
Ui::ImagePackToolDialog *ui;
FileDropAccepter* forkParamDropHandler = nullptr;
ExecutionInfo initialInfo;
};

Expand Down
2 changes: 1 addition & 1 deletion tooldialog/imagepacktooldialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
Expand Down
42 changes: 42 additions & 0 deletions util/filedropaccepter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "filedropaccepter.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>

FileDropAccepter::FileDropAccepter(QObject *parent)
: QObject{parent}
{

}

bool FileDropAccepter::eventFilter(QObject *watched, QEvent *event)
{
// 因为 QDragEnterEvent 继承自 QDragMoveEvent, 该事件又继承自 QDropEvent
// 所以这里 if-else 的顺序不能调换
if (QDragEnterEvent* e = dynamic_cast<QDragEnterEvent*>(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<QDragMoveEvent*>(event)) {
if (e->mimeData()->hasUrls()) {
return true;
}
} else if (QDropEvent* e = dynamic_cast<QDropEvent*>(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);
}
26 changes: 26 additions & 0 deletions util/filedropaccepter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef FILEDROPACCEPTER_H
#define FILEDROPACCEPTER_H

#include <QObject>

class FileDropAccepter : public QObject
{
Q_OBJECT
public:
explicit FileDropAccepter(QObject *parent = nullptr);

virtual bool eventFilter(QObject *watched, QEvent *event) override;

void setVerifyCallBack(std::function<bool(const QString&)> cb) {
verifyCB = cb;
}

signals:
void fileDropped(const QString& path, QObject* droppedTo);

private:
std::function<bool(const QString&)> verifyCB;

};

#endif // FILEDROPACCEPTER_H
48 changes: 48 additions & 0 deletions util/filepathvalidate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "filepathvalidate.h"

#include <QSet>
#include <QFileInfo>
namespace {

const QSet<QString> 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()));
}
12 changes: 12 additions & 0 deletions util/filepathvalidate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef FILEPATHVALIDATE_H
#define FILEPATHVALIDATE_H

#include <QString>

namespace FilePathValidate
{
bool isReadableImageFile(const QString& path);

} // end namespace FilePathValidate

#endif // FILEPATHVALIDATE_H

0 comments on commit b8ef67b

Please sign in to comment.