Gui: improve loading file by drag and drop

This commit is contained in:
wmayer
2023-03-19 19:13:47 +01:00
parent a9bff40914
commit 63320c752b
2 changed files with 17 additions and 1 deletions

View File

@@ -246,6 +246,18 @@ void ImageView::dragEnterEvent(QDragEnterEvent* event)
}
}
bool ImageView::isImageFormat(const QFileInfo& fileInfo)
{
QString ext = fileInfo.suffix().toLower();
QByteArray suffix = ext.toLatin1();
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
auto it = std::find_if(supportedFormats.begin(), supportedFormats.end(), [suffix](const QByteArray& image) {
return (image == suffix);
});
return (it != supportedFormats.end());
}
void ImageView::loadImageFromUrl(const QList<QUrl>& urls)
{
if (urls.isEmpty()) {
@@ -255,7 +267,9 @@ void ImageView::loadImageFromUrl(const QList<QUrl>& urls)
const QUrl& url = urls.first();
const QFileInfo info(url.toLocalFile());
if (info.exists() && info.isFile()) {
loadFile(info.absoluteFilePath());
if (isImageFormat(info)) {
loadFile(info.absoluteFilePath());
}
}
}

View File

@@ -26,6 +26,7 @@
#include <Gui/MDIView.h>
class QFileInfo;
class QLabel;
class QScrollArea;
class QScrollBar;
@@ -82,6 +83,7 @@ private:
void pasteImage();
bool canPasteImage() const;
static QImage imageFromClipboard();
static bool isImageFormat(const QFileInfo&);
void loadImageFromUrl(const QList<QUrl>&);
private: