Support drag&drop for setting a custom cover in the properties dialog

This commit is contained in:
Luis Ángel San Martín 2025-05-09 19:58:52 +02:00
parent 28952180ab
commit 8b159c9da6
7 changed files with 115 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#include "yacreader_field_edit.h"
#include "yacreader_field_plain_text_edit.h"
#include "db_helper.h"
#include "yacreader_cover_label.h"
#include <QHBoxLayout>
#include <QApplication>
@ -39,7 +40,8 @@ PropertiesDialog::PropertiesDialog(QWidget *parent)
createTabBar();
auto rootLayout = new QGridLayout;
cover = new QLabel();
cover = new YACReader::CoverLabel();
connect(cover, &YACReader::CoverLabel::imageDropped, this, &PropertiesDialog::loadCustomCoverImageFromPath);
mainLayout = new QGridLayout;
mainLayout->addWidget(tabBar, 0, 0);
@ -1165,7 +1167,11 @@ void PropertiesDialog::resetCover()
void PropertiesDialog::loadCustomCoverImage()
{
auto path = YACReader::imageFileLoader(this);
loadCustomCoverImageFromPath(path);
}
void PropertiesDialog::loadCustomCoverImageFromPath(const QString &path)
{
if (path.isEmpty()) {
return;
}

View File

@ -19,6 +19,10 @@ class QComboBox;
// class YACReaderBusyWidget;
class QToolButton;
namespace YACReader {
class CoverLabel;
}
#include "comic_db.h"
class PropertiesDialog : public QDialog
@ -33,7 +37,7 @@ private:
QTabWidget *tabBar;
QWidget *coverBox;
QLabel *cover;
YACReader::CoverLabel *cover;
QScrollArea *sa;
QWidget *generalInfoBox;
@ -175,6 +179,7 @@ public slots:
void loadPreviousCover();
void resetCover();
void loadCustomCoverImage();
void loadCustomCoverImageFromPath(const QString &path);
void setCoverPage(int pageNumber);
bool close();

View File

@ -111,3 +111,40 @@ QString YACReader::imageFileLoader(QWidget *parent)
return QFileDialog::getOpenFileName(parent, QObject::tr("Select custom cover"), QDir::homePath(), QObject::tr("Images (%1)").arg(supportedImageFormatsString));
}
QString YACReader::imagePathFromMimeData(const QMimeData *mimeData)
{
QString filePath;
// Check for URLs (drag from browser or file explorer)
if (mimeData->hasUrls()) {
QList<QUrl> urlList = mimeData->urls();
// We only handle the first URL for simplicity
if (!urlList.isEmpty()) {
QUrl url = urlList.first();
if (url.isLocalFile()) {
filePath = url.toLocalFile();
// Verify it's an image file using the extension
QFileInfo fileInfo(filePath);
QString extension = fileInfo.suffix().toLower();
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
bool isSupported = false;
for (const QByteArray &format : supportedFormats) {
if (extension == QString(format).toLower()) {
isSupported = true;
break;
}
}
if (!isSupported) {
filePath.clear(); // Not a supported image format
}
}
}
}
return filePath;
}

View File

@ -113,5 +113,6 @@ QString addExtensionToIconPathInToolbar(const QString &path);
QAction *actionWithCustomIcon(const QIcon &icon, QAction *action);
QPixmap hdpiPixmap(const QString &file, QSize size);
QString imageFileLoader(QWidget *parent);
QString imagePathFromMimeData(const QMimeData *mimeData);
}
#endif

View File

@ -21,7 +21,8 @@ HEADERS += \
$$PWD/yacreader_library_list_widget.h \
$$PWD/yacreader_library_item_widget.h \
$$PWD/yacreader_treeview.h \
$$PWD/yacreader_busy_widget.h
$$PWD/yacreader_busy_widget.h \
$$PWD/yacreader_cover_label.h
!CONFIG(no_opengl){
HEADERS += $$PWD/yacreader_gl_flow_config_widget.h
}
@ -50,8 +51,8 @@ SOURCES += \
$$PWD/yacreader_library_list_widget.cpp \
$$PWD/yacreader_library_item_widget.cpp \
$$PWD/yacreader_treeview.cpp \
$$PWD/yacreader_busy_widget.cpp
$$PWD/yacreader_busy_widget.cpp \
$$PWD/yacreader_cover_label.cpp
!CONFIG(no_opengl){
SOURCES += $$PWD/yacreader_gl_flow_config_widget.cpp
}

View File

@ -0,0 +1,31 @@
#include "yacreader_cover_label.h"
#include "yacreader_global_gui.h"
YACReader::CoverLabel::CoverLabel(QWidget *parent)
: QLabel(parent)
{
setAcceptDrops(true);
}
void YACReader::CoverLabel::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls() && !YACReader::imagePathFromMimeData(event->mimeData()).isEmpty()) {
event->acceptProposedAction();
}
}
void YACReader::CoverLabel::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void YACReader::CoverLabel::dropEvent(QDropEvent *event)
{
QString path = YACReader::imagePathFromMimeData(event->mimeData());
if (!path.isEmpty()) {
emit imageDropped(path);
event->acceptProposedAction();
}
}

View File

@ -0,0 +1,29 @@
#ifndef DROP_LABEL_H
#define DROP_LABEL_H
#include <QLabel>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
namespace YACReader {
class CoverLabel : public QLabel
{
Q_OBJECT
public:
explicit CoverLabel(QWidget *parent = nullptr);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;
signals:
void imageDropped(const QString &path);
};
}
#endif // DROP_LABEL_H