add _fill_with_ template notation to picture loader (#4287)

This commit is contained in:
ebbit1q 2021-04-02 05:37:11 +02:00 committed by GitHub
parent 1c48656623
commit 7d1f082b27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,6 +17,7 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#include <QRegularExpression>
#include <QSet> #include <QSet>
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QThread> #include <QThread>
@ -241,6 +242,10 @@ QString PictureToLoad::transformUrl(const QString &urlTemplate) const
for downloading images. If information is requested by the template that is for downloading images. If information is requested by the template that is
not populated for this specific card/set combination, an empty string is returned.*/ not populated for this specific card/set combination, an empty string is returned.*/
static const QRegularExpression rxCardProp("!prop:([^!]+)!");
static const QRegularExpression rxFillWith("^(.+)_fill_with_(.+)$");
static const QRegularExpression rxSetProp("!set:([^!]+)!");
QString transformedUrl = urlTemplate; QString transformedUrl = urlTemplate;
CardSetPtr set = getCurrentSet(); CardSetPtr set = getCurrentSet();
@ -252,18 +257,39 @@ QString PictureToLoad::transformUrl(const QString &urlTemplate) const
transformMap["!corrected_name_lower!"] = card->getCorrectedName().toLower(); transformMap["!corrected_name_lower!"] = card->getCorrectedName().toLower();
// card properties // card properties
QRegExp rxCardProp("!prop:([^!]+)!"); auto matches = rxCardProp.globalMatch(transformedUrl);
int pos = 0; while (matches.hasNext()) {
while ((pos = rxCardProp.indexIn(transformedUrl, pos)) != -1) { auto match = matches.next();
QString propertyName = rxCardProp.cap(1); QString propertyName = match.captured(1);
pos += rxCardProp.matchedLength(); auto fillMatch = rxFillWith.match(propertyName);
QString propertyValue = card->getProperty(propertyName); QString fillPropertyName;
QString fillWith;
if (fillMatch.hasMatch()) {
fillPropertyName = fillMatch.captured(1);
fillWith = fillMatch.captured(2);
} else {
fillPropertyName = propertyName;
fillWith = QString();
}
QString propertyValue = card->getProperty(fillPropertyName);
if (propertyValue.isEmpty()) { if (propertyValue.isEmpty()) {
qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName() qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName()
<< "]: Requested property (" << propertyName << ") for Url template (" << urlTemplate << "]: Requested property (" << fillPropertyName << ") for Url template (" << urlTemplate
<< ") is not available"; << ") is not available";
return QString(); return QString();
} else { } else {
if (!fillWith.isEmpty()) {
int fillLength = fillWith.length();
int propLength = propertyValue.length();
if (fillLength < propLength) {
qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName()
<< "]: Requested property (" << fillPropertyName << ") for Url template (" << urlTemplate
<< ") is longer than fill specification (" << fillWith << ")";
return QString();
} else {
propertyValue = fillWith.left(fillLength - propLength) + propertyValue;
}
}
transformMap["!prop:" + propertyName + "!"] = propertyValue; transformMap["!prop:" + propertyName + "!"] = propertyValue;
} }
} }
@ -274,18 +300,39 @@ QString PictureToLoad::transformUrl(const QString &urlTemplate) const
transformMap["!setname!"] = set->getLongName(); transformMap["!setname!"] = set->getLongName();
transformMap["!setname_lower!"] = set->getLongName().toLower(); transformMap["!setname_lower!"] = set->getLongName().toLower();
QRegExp rxSetProp("!set:([^!]+)!"); auto matches = rxSetProp.globalMatch(transformedUrl);
pos = 0; // Defined above while (matches.hasNext()) {
while ((pos = rxSetProp.indexIn(transformedUrl, pos)) != -1) { auto match = matches.next();
QString propertyName = rxSetProp.cap(1); QString propertyName = match.captured(1);
pos += rxSetProp.matchedLength(); auto fillMatch = rxFillWith.match(propertyName);
QString propertyValue = card->getSetProperty(set->getShortName(), propertyName); QString fillPropertyName;
QString fillWith;
if (fillMatch.hasMatch()) {
fillPropertyName = fillMatch.captured(1);
fillWith = fillMatch.captured(2);
} else {
fillPropertyName = propertyName;
fillWith = QString();
}
QString propertyValue = card->getSetProperty(set->getShortName(), fillPropertyName);
if (propertyValue.isEmpty()) { if (propertyValue.isEmpty()) {
qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName() qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName()
<< "]: Requested set property (" << propertyName << ") for Url template (" << urlTemplate << "]: Requested set property (" << fillPropertyName << ") for Url template (" << urlTemplate
<< ") is not available"; << ") is not available";
return QString(); return QString();
} else { } else {
if (!fillWith.isEmpty()) {
int fillLength = fillWith.length();
int propLength = propertyValue.length();
if (fillLength < propLength) {
qDebug() << "PictureLoader: [card: " << card->getName() << " set: " << getSetName()
<< "]: Requested set property (" << fillPropertyName << ") for Url template ("
<< urlTemplate << ") is longer than fill specification (" << fillWith << ")";
return QString();
} else {
propertyValue = fillWith.left(fillLength - propLength) + propertyValue;
}
}
transformMap["!set:" + propertyName + "!"] = propertyValue; transformMap["!set:" + propertyName + "!"] = propertyValue;
} }
} }
@ -405,10 +452,9 @@ void PictureLoaderWorker::picDownloadFinished(QNetworkReply *reply)
QImageReader imgReader; QImageReader imgReader;
imgReader.setDecideFormatFromContent(true); imgReader.setDecideFormatFromContent(true);
imgReader.setDevice(reply); imgReader.setDevice(reply);
QString extension = "." + imgReader.format(); // the format is determined // the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the
// prior to reading the // QImageReader buffer
// QImageReader data QString extension = "." + imgReader.format();
// into a QImage object, as that wipes the QImageReader buffer
if (extension == ".jpeg") { if (extension == ".jpeg") {
extension = ".jpg"; extension = ".jpg";
} }