-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various improvements on ImagePack support
- Loading branch information
Shengjie Xu
committed
Nov 29, 2023
1 parent
c8a0639
commit b8ef67b
Showing
8 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |