diff options
Diffstat (limited to 'src/widget/wcoverartmenu.cpp')
-rw-r--r-- | src/widget/wcoverartmenu.cpp | 143 |
1 files changed, 52 insertions, 91 deletions
diff --git a/src/widget/wcoverartmenu.cpp b/src/widget/wcoverartmenu.cpp index da908e169e..5fe19713bd 100644 --- a/src/widget/wcoverartmenu.cpp +++ b/src/widget/wcoverartmenu.cpp @@ -1,14 +1,12 @@ #include <QFileDialog> -#include <QStringBuilder> +#include <QFileInfo> -#include "wcoverartmenu.h" -#include "library/coverartcache.h" -#include "library/dao/coverartdao.h" +#include "widget/wcoverartmenu.h" +#include "library/coverartutils.h" WCoverArtMenu::WCoverArtMenu(QWidget *parent) : QMenu(parent) { createActions(); - addActions(); } WCoverArtMenu::~WCoverArtMenu() { @@ -21,121 +19,84 @@ void WCoverArtMenu::createActions() { m_pChange = new QAction(tr("Choose new cover", "change cover art location"), this); connect(m_pChange, SIGNAL(triggered()), this, SLOT(slotChange())); + addAction(m_pChange); m_pUnset = new QAction(tr("Unset cover", - "unset cover art - load default"), this); + "clears the set cover art -- does not touch files on disk"), this); connect(m_pUnset, SIGNAL(triggered()), this, SLOT(slotUnset())); + addAction(m_pUnset); m_pReload = new QAction(tr("Reload from track/folder", - "reload just cover art, using the search algorithm"), this); + "reload cover art from track metadata or folder"), this); connect(m_pReload, SIGNAL(triggered()), this, SLOT(slotReload())); -} - -void WCoverArtMenu::addActions() { - addAction(m_pChange); - addAction(m_pUnset); addAction(m_pReload); } -void WCoverArtMenu::show(QPoint pos, CoverInfo info, TrackPointer pTrack) { - setTrack(pTrack, info); - popup(pos); -} - -void WCoverArtMenu::setTrack(TrackPointer pTrack, CoverInfo info) { - if (!pTrack) { - return; - } +void WCoverArtMenu::setCoverArt(TrackPointer pTrack, const CoverInfo& info) { m_pTrack = pTrack; - if (info.trackId == -1) { - info = CoverArtCache::instance()->getCoverInfo(m_pTrack); - } m_coverInfo = info; } void WCoverArtMenu::slotChange() { - if (m_coverInfo.trackId < 1 || !m_pTrack) { - return; - } - // get initial directory (trackdir or coverdir) QString initialDir; - QString trackPath = m_pTrack->getDirectory(); - if (m_coverInfo.coverLocation.isEmpty() || - m_coverInfo.coverLocation == CoverArtCache::instance() - ->getDefaultCoverLocation()) { - initialDir = trackPath; - } else { - initialDir = m_coverInfo.coverLocation; + + QFileInfo track; + if (m_pTrack) { + track = m_pTrack->getFileInfo(); + } else if (!m_coverInfo.trackLocation.isEmpty()) { + track = QFileInfo(m_coverInfo.trackLocation); + } + + // If the cover is from file metadata then use the directory the track is + // in. + if (m_coverInfo.type == CoverInfo::METADATA) { + initialDir = track.absolutePath(); + } else if (m_coverInfo.type == CoverInfo::FILE) { + QFileInfo file(track.dir(), m_coverInfo.coverLocation); + initialDir = file.absolutePath(); } + QStringList extensions = CoverArtUtils::supportedCoverArtExtensions(); + for (QStringList::iterator it = extensions.begin(); it != extensions.end(); ++it) { + it->prepend("*."); + } + QString supportedText = QString("%1 (%2)").arg(tr("Image Files")) + .arg(extensions.join(" ")); + // open file dialog QString selectedCover = QFileDialog::getOpenFileName( - this, tr("Change Cover Art"), initialDir, - tr("Image Files (*.png *.jpg *.jpeg *.bmp)")); - + this, tr("Change Cover Art"), initialDir, supportedText); if (selectedCover.isEmpty()) { return; } - // if the cover comes from an external dir, - // we copy it to the track directory. - QString newCover; - QFileInfo fileInfo(selectedCover); - QString coverPath = fileInfo.absolutePath(); - if (trackPath == coverPath) { - newCover = selectedCover; - } else { - QDir trackDir(trackPath); - QString ext = fileInfo.suffix(); - QString mixxxCoverFile = trackDir.filePath("mixxx-cover." % ext); - QStringList filepaths; - filepaths << trackDir.filePath("cover." % ext) - << trackDir.filePath("album." % ext) - << mixxxCoverFile; - - foreach (QString filepath, filepaths) { - if (QFile::copy(selectedCover, filepath)) { - newCover = filepath; - break; - } - } - - if (newCover.isEmpty()) { - // overwrites "mixxx-cover" - QFile::remove(mixxxCoverFile); - if (QFile::copy(selectedCover, mixxxCoverFile)) { - newCover = mixxxCoverFile; - } - } - } + // TODO(rryan): Ask if user wants to copy the file. - QPixmap px(newCover); - emit(coverLocationUpdated(newCover, m_coverInfo.coverLocation, px)); + CoverArt art; + art.image = QImage(selectedCover); + if (art.image.isNull()) { + // TODO(rryan): feedback + return; + } + art.info.type = CoverInfo::FILE; + art.info.source = CoverInfo::USER_SELECTED; + art.info.coverLocation = selectedCover; + art.info.hash = CoverArtUtils::calculateHash(art.image); + qDebug() << "WCoverArtMenu::slotChange emit" << art; + emit(coverArtSelected(art)); } void WCoverArtMenu::slotReload() { - if (!m_pTrack) { - return; - } - CoverArtDAO::CoverArtInfo info; - info.trackId = m_pTrack->getId(); - info.album = m_pTrack->getAlbum(); - info.trackDirectory = m_pTrack->getDirectory(); - info.trackLocation = m_pTrack->getLocation(); - info.trackBaseName = QFileInfo(m_pTrack->getFilename()).baseName(); - CoverArtCache::FutureResult res = - CoverArtCache::instance()->searchImage(info, QSize(0,0), false); - QPixmap px; - px.convertFromImage(res.img); - emit(coverLocationUpdated(res.coverLocation, m_coverInfo.coverLocation, px)); + CoverArt art = CoverArtUtils::guessCoverArt(m_pTrack); + qDebug() << "WCoverArtMenu::slotReload emit" << art; + emit(coverArtSelected(art)); } void WCoverArtMenu::slotUnset() { - if (m_coverInfo.trackId < 1) { - return; - } - QString newLoc = CoverArtCache::instance()->getDefaultCoverLocation(); - QPixmap px = CoverArtCache::instance()->getDefaultCoverArt(); - emit(coverLocationUpdated(newLoc, m_coverInfo.coverLocation, px)); + CoverArt art; + art.info.type = CoverInfo::NONE; + art.info.source = CoverInfo::USER_SELECTED; + qDebug() << "WCoverArtMenu::slotUnset emit" << art; + emit(coverArtSelected(art)); } |