Merge pull request #256 from pliu037/handle-PNGs-2

Handle PNGs (and other image formats)
This commit is contained in:
Gavin Bisesi 2014-08-06 10:42:15 -04:00
commit ef1fbc0db9

View file

@ -13,6 +13,7 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QDebug> #include <QDebug>
#include <QImageReader>
const int CardDatabase::versionNeeded = 3; const int CardDatabase::versionNeeded = 3;
@ -124,29 +125,38 @@ void PictureLoader::processLoadQueue()
} }
PictureToLoad ptl = loadQueue.takeFirst(); PictureToLoad ptl = loadQueue.takeFirst();
mutex.unlock(); mutex.unlock();
QString correctedName = ptl.getCard()->getCorrectedName();
QString picsPath = _picsPath; //The list of paths to the folders in which to search for images
QString setName = ptl.getSetName(); QList<QString> picsPaths = QList<QString>() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full"
<< _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"
<< _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full";
QImage image; QImage image;
if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) { QImageReader imgReader;
if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName))) imgReader.setDecideFormatFromContent(true);
//if (!image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(setName).arg(correctedName).arg(1))) bool found = false;
if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) {
if (picDownload) { //Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension
cardsToDownload.append(ptl); for (int i = 0; i < picsPaths.length() && !found; i ++) {
if (!downloadRunning) imgReader.setFileName(picsPaths.at(i));
startNextPicDownload(); if (imgReader.read(&image)) {
} else { emit imageLoaded(ptl.getCard(), image);
if (ptl.nextSet()) found = true;
loadQueue.prepend(ptl); }
else
emit imageLoaded(ptl.getCard(), QImage());
}
}
} }
emit imageLoaded(ptl.getCard(), image); if (!found) {
if (picDownload) {
cardsToDownload.append(ptl);
if (!downloadRunning)
startNextPicDownload();
} else {
if (ptl.nextSet())
loadQueue.prepend(ptl);
else
emit imageLoaded(ptl.getCard(), QImage());
}
}
} }
} }
@ -208,25 +218,27 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply)
qDebug() << "Download failed:" << reply->errorString(); qDebug() << "Download failed:" << reply->errorString();
} }
const QByteArray &picData = reply->readAll(); const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader
QImage testImage; QImage testImage;
if (testImage.loadFromData(picData)) {
if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) { QImageReader imgReader;
QDir dir(picsPath); imgReader.setDecideFormatFromContent(true);
if (!dir.exists()) imgReader.setDevice(reply);
return; QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer
dir.mkdir("downloadedPics"); if (extension == ".jpeg")
} extension = ".jpg";
if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) {
QDir dir(QString(picsPath + "/downloadedPics")); if (imgReader.read(&testImage)) {
dir.mkdir(cardBeingDownloaded.getSetName()); if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) {
qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created.";
return;
} }
QString suffix; QString suffix;
if (!cardBeingDownloaded.getStripped()) if (!cardBeingDownloaded.getStripped())
suffix = ".full"; suffix = ".full";
QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg"); QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension);
if (!newPic.open(QIODevice::WriteOnly)) if (!newPic.open(QIODevice::WriteOnly))
return; return;
newPic.write(picData); newPic.write(picData);