allow cover image to be added via Drag & Drop

This commit is contained in:
Martchus 2015-08-25 19:10:16 +02:00
parent 95a5fe093e
commit 019740d815
3 changed files with 81 additions and 47 deletions

View File

@ -230,24 +230,16 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event)
case QEvent::DragEnter: case QEvent::DragEnter:
case QEvent::Drop: case QEvent::Drop:
if(QDropEvent *dropEvent = static_cast<QDropEvent *>(event)) { if(QDropEvent *dropEvent = static_cast<QDropEvent *>(event)) {
QString data; for(const auto &url : dropEvent->mimeData()->urls()) {
const QMimeData *mimeData = dropEvent->mimeData();
if(mimeData->hasUrls()) {
const QUrl url = mimeData->urls().front();
if(url.scheme() == QLatin1String("file")) { if(url.scheme() == QLatin1String("file")) {
data = url.path();
}
} else if(mimeData->hasText()) {
data = mimeData->text();
}
if(!data.isEmpty()) {
event->accept(); event->accept();
if(event->type() == QEvent::Drop) { if(event->type() == QEvent::Drop) {
startParsing(data, true); startParsing(url.path(), true);
}
} }
return true; return true;
} }
}
}
default: default:
; ;
} }

View File

@ -22,6 +22,9 @@
#include <QFileDialog> #include <QFileDialog>
#include <QInputDialog> #include <QInputDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <stdexcept> #include <stdexcept>
#include <functional> #include <functional>
@ -51,12 +54,13 @@ PicturePreviewSelection::PicturePreviewSelection(Tag *tag, KnownField field, QWi
m_currentTypeIndex(0) m_currentTypeIndex(0)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
connect(m_ui->addButton, &QPushButton::clicked, this, &PicturePreviewSelection::addOfSelectedType); connect(m_ui->addButton, &QPushButton::clicked, this, static_cast<void (PicturePreviewSelection::*)(void)>(&PicturePreviewSelection::addOfSelectedType));
connect(m_ui->removeButton, &QPushButton::clicked, this, &PicturePreviewSelection::removeSelected); connect(m_ui->removeButton, &QPushButton::clicked, this, &PicturePreviewSelection::removeSelected);
connect(m_ui->extractButton, &QPushButton::clicked, this, &PicturePreviewSelection::extractSelected); connect(m_ui->extractButton, &QPushButton::clicked, this, &PicturePreviewSelection::extractSelected);
connect(m_ui->displayButton, &QPushButton::clicked, this, &PicturePreviewSelection::displaySelected); connect(m_ui->displayButton, &QPushButton::clicked, this, &PicturePreviewSelection::displaySelected);
connect(m_ui->restoreButton, &QPushButton::clicked, std::bind(&PicturePreviewSelection::setup, this, PreviousValueHandling::Clear)); connect(m_ui->restoreButton, &QPushButton::clicked, std::bind(&PicturePreviewSelection::setup, this, PreviousValueHandling::Clear));
setup(); setup();
setAcceptDrops(true);
} }
/*! /*!
@ -279,6 +283,18 @@ void PicturePreviewSelection::addOfSelectedType()
if(m_currentTypeIndex < static_cast<unsigned int>(m_values.count())) { if(m_currentTypeIndex < static_cast<unsigned int>(m_values.count())) {
QString path = QFileDialog::getOpenFileName(this, tr("Select a picture to add as cover")); QString path = QFileDialog::getOpenFileName(this, tr("Select a picture to add as cover"));
if(!path.isEmpty()) { if(!path.isEmpty()) {
addOfSelectedType(path);
}
} else {
throw logic_error("Invalid type selected (no corresponding value assigned).");
}
}
/*!
* \brief Adds the picture from the specified \a path of the selected type.
*/
void PicturePreviewSelection::addOfSelectedType(const QString &path)
{
TagValue &selectedCover = m_values[m_currentTypeIndex]; TagValue &selectedCover = m_values[m_currentTypeIndex];
try { try {
MediaFileInfo fileInfo(path.toLocal8Bit().constData()); MediaFileInfo fileInfo(path.toLocal8Bit().constData());
@ -304,10 +320,6 @@ void PicturePreviewSelection::addOfSelectedType()
QMessageBox::critical(this, QApplication::applicationName(), tr("Unable to parse specified cover file.")); QMessageBox::critical(this, QApplication::applicationName(), tr("Unable to parse specified cover file."));
} }
updatePreview(m_currentTypeIndex); updatePreview(m_currentTypeIndex);
}
} else {
throw logic_error("Invalid type selected (no corresponding value assigned).");
}
} }
/*! /*!
@ -423,6 +435,35 @@ void PicturePreviewSelection::resizeEvent(QResizeEvent *)
} }
} }
void PicturePreviewSelection::dragEnterEvent(QDragEnterEvent *event)
{
const auto *mimeData = event->mimeData();
if(mimeData->hasUrls()) {
for(const auto &url : mimeData->urls()) {
if(url.scheme() == QLatin1String("file")) {
event->accept();
return;
}
}
}
event->ignore();
}
void PicturePreviewSelection::dropEvent(QDropEvent *event)
{
const auto *mimeData = event->mimeData();
if(mimeData->hasUrls()) {
for(const auto &url : mimeData->urls()) {
if(url.scheme() == QLatin1String("file")) {
addOfSelectedType(url.path());
event->accept();
return;
}
}
}
event->ignore();
}
/*! /*!
* \brief Handles selected type being switched. * \brief Handles selected type being switched.
*/ */

View File

@ -9,13 +9,11 @@
#include <memory> #include <memory>
QT_BEGIN_NAMESPACE QT_FORWARD_DECLARE_CLASS(QEvent)
class QEvent; QT_FORWARD_DECLARE_CLASS(QGraphicsScene)
class QGraphicsScene; QT_FORWARD_DECLARE_CLASS(QGraphicsTextItem)
class QGraphicsTextItem; QT_FORWARD_DECLARE_CLASS(QGraphicsPixmapItem)
class QGraphicsPixmapItem; QT_FORWARD_DECLARE_CLASS(QGraphicsRectItem)
class QGraphicsRectItem;
QT_END_NAMESPACE
namespace Media { namespace Media {
class Tag; class Tag;
@ -48,6 +46,7 @@ public slots:
void apply(); void apply();
void clear(); void clear();
void addOfSelectedType(); void addOfSelectedType();
void addOfSelectedType(const QString &path);
void removeSelected(); void removeSelected();
void extractSelected(); void extractSelected();
void displaySelected(); void displaySelected();
@ -58,6 +57,8 @@ signals:
protected: protected:
void changeEvent(QEvent *event); void changeEvent(QEvent *event);
void resizeEvent(QResizeEvent *event); void resizeEvent(QResizeEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private slots: private slots:
void typeSwitched(int index); void typeSwitched(int index);