Commit 621a47de authored by Mawu3n4's avatar Mawu3n4
Browse files

add: find token cards by prefix

parent ef1fbc0d
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QDebug> #include <QDebug>
#include <QImageReader>
const int CardDatabase::versionNeeded = 3; const int CardDatabase::versionNeeded = 3;
...@@ -125,38 +124,29 @@ void PictureLoader::processLoadQueue() ...@@ -125,38 +124,29 @@ void PictureLoader::processLoadQueue()
} }
PictureToLoad ptl = loadQueue.takeFirst(); PictureToLoad ptl = loadQueue.takeFirst();
mutex.unlock(); mutex.unlock();
QString correctedName = ptl.getCard()->getCorrectedName();
//The list of paths to the folders in which to search for images QString picsPath = _picsPath;
QList<QString> picsPaths = QList<QString>() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full" QString setName = ptl.getSetName();
<< _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"
<< _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full";
QImage image; QImage image;
QImageReader imgReader; if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) {
imgReader.setDecideFormatFromContent(true); if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName)))
bool found = false; //if (!image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(setName).arg(correctedName).arg(1)))
if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) {
//Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension if (picDownload) {
for (int i = 0; i < picsPaths.length() && !found; i ++) { cardsToDownload.append(ptl);
imgReader.setFileName(picsPaths.at(i)); if (!downloadRunning)
if (imgReader.read(&image)) { startNextPicDownload();
emit imageLoaded(ptl.getCard(), image); } else {
found = true; if (ptl.nextSet())
} loadQueue.prepend(ptl);
else
emit imageLoaded(ptl.getCard(), QImage());
}
}
} }
if (!found) { emit imageLoaded(ptl.getCard(), image);
if (picDownload) {
cardsToDownload.append(ptl);
if (!downloadRunning)
startNextPicDownload();
} else {
if (ptl.nextSet())
loadQueue.prepend(ptl);
else
emit imageLoaded(ptl.getCard(), QImage());
}
}
} }
} }
...@@ -218,27 +208,25 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) ...@@ -218,27 +208,25 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply)
qDebug() << "Download failed:" << reply->errorString(); qDebug() << "Download failed:" << reply->errorString();
} }
const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader const QByteArray &picData = reply->readAll();
QImage testImage; QImage testImage;
if (testImage.loadFromData(picData)) {
QImageReader imgReader; if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) {
imgReader.setDecideFormatFromContent(true); QDir dir(picsPath);
imgReader.setDevice(reply); if (!dir.exists())
QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer return;
if (extension == ".jpeg") dir.mkdir("downloadedPics");
extension = ".jpg"; }
if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) {
if (imgReader.read(&testImage)) { QDir dir(QString(picsPath + "/downloadedPics"));
if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) { dir.mkdir(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 + extension); QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg");
if (!newPic.open(QIODevice::WriteOnly)) if (!newPic.open(QIODevice::WriteOnly))
return; return;
newPic.write(picData); newPic.write(picData);
...@@ -450,7 +438,7 @@ void CardInfo::updatePixmapCache() ...@@ -450,7 +438,7 @@ void CardInfo::updatePixmapCache()
qDebug() << "Updating pixmap cache for" << name; qDebug() << "Updating pixmap cache for" << name;
clearPixmapCache(); clearPixmapCache();
loadPixmap(); loadPixmap();
emit pixmapUpdated(); emit pixmapUpdated();
} }
...@@ -555,7 +543,7 @@ CardDatabase::~CardDatabase() ...@@ -555,7 +543,7 @@ CardDatabase::~CardDatabase()
{ {
clear(); clear();
delete noCard; delete noCard;
pictureLoader->deleteLater(); pictureLoader->deleteLater();
pictureLoaderThread->wait(); pictureLoaderThread->wait();
delete pictureLoaderThread; delete pictureLoaderThread;
...@@ -569,7 +557,7 @@ void CardDatabase::clear() ...@@ -569,7 +557,7 @@ void CardDatabase::clear()
delete setIt.value(); delete setIt.value();
} }
sets.clear(); sets.clear();
QHashIterator<QString, CardInfo *> i(cards); QHashIterator<QString, CardInfo *> i(cards);
while (i.hasNext()) { while (i.hasNext()) {
i.next(); i.next();
...@@ -717,11 +705,22 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) ...@@ -717,11 +705,22 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
} }
} }
CardInfo *CardNameMap::findByPrefix(const std::string &prefix) {
for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) {
auto std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin())
if (auto.first == prefix.end())
return it.value();
}
return NULL;
}
CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound) { CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound) {
CardInfo *foundCard;
if (cardName.isEmpty()) if (cardName.isEmpty())
return noCard; return noCard;
else if (cardMap.contains(cardName)) else if ((foundCard = cardMap.findByPrefix(cardName.toStdString())))
return cardMap.value(cardName); return foundCard;
else if (createIfNotFound) { else if (createIfNotFound) {
CardInfo *newCard = new CardInfo(this, cardName, true); CardInfo *newCard = new CardInfo(this, cardName, true);
newCard->addToSet(getSet("TK")); newCard->addToSet(getSet("TK"));
......
...@@ -182,7 +182,13 @@ signals: ...@@ -182,7 +182,13 @@ signals:
enum LoadStatus { Ok, VersionTooOld, Invalid, NotLoaded, FileError, NoCards }; enum LoadStatus { Ok, VersionTooOld, Invalid, NotLoaded, FileError, NoCards };
typedef QHash<QString, CardInfo *> CardNameMap;
class CardNameMap: public QHash<QString, CardInfo *>
{
public:
CardInfo *findByPrefix(const std::string &prefix);
};
typedef QHash<QString, CardSet *> SetNameMap; typedef QHash<QString, CardSet *> SetNameMap;
class CardDatabase : public QObject { class CardDatabase : public QObject {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment