mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 09:04:53 -07:00
Give people options from a dropdown.
Took 1 hour 6 minutes Took 3 seconds
This commit is contained in:
parent
64885f4c20
commit
37cb86f16a
7 changed files with 173 additions and 47 deletions
|
|
@ -189,9 +189,19 @@ void CardPictureLoader::saveCardImageToLocalStorage(const ExactCard &card, const
|
|||
}
|
||||
|
||||
const QString picsRoot = SettingsCache::instance().getPicsPath();
|
||||
const QString scheme = SettingsCache::instance().getLocalCardImageStorageNamingScheme();
|
||||
CardPictureLoaderLocalSchemes::NamingScheme scheme =
|
||||
SettingsCache::instance().getLocalCardImageStorageNamingScheme();
|
||||
|
||||
if (picsRoot.isEmpty() || scheme.isEmpty()) {
|
||||
QString pattern;
|
||||
|
||||
for (const auto &s : CardPictureLoaderLocalSchemes::exportSchemes()) {
|
||||
if (s.id == scheme) {
|
||||
pattern = s.pattern;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (picsRoot.isEmpty() || pattern.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -217,18 +227,26 @@ void CardPictureLoader::saveCardImageToLocalStorage(const ExactCard &card, const
|
|||
}
|
||||
|
||||
// Build path from scheme
|
||||
QString relativePath = scheme;
|
||||
QString relativePath =
|
||||
CardPictureLoaderLocalSchemes::expandPattern(pattern, cardName, setName, collectorNumber, uuid);
|
||||
|
||||
relativePath.replace("{name}", cardName);
|
||||
relativePath.replace("{set}", setName);
|
||||
relativePath.replace("{collector}", collectorNumber);
|
||||
relativePath.replace("{uuid}", uuid);
|
||||
relativePath.replace("{ext}", "png");
|
||||
if (relativePath.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// append extension
|
||||
relativePath += ".png";
|
||||
|
||||
// Normalize slashes
|
||||
relativePath = QDir::cleanPath(relativePath);
|
||||
|
||||
QFileInfo outInfo(baseDir.filePath(relativePath));
|
||||
|
||||
// Do not overwrite existing files
|
||||
if (outInfo.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QDir outDir = outInfo.dir();
|
||||
|
||||
// Ensure directory exists
|
||||
|
|
@ -239,11 +257,6 @@ void CardPictureLoader::saveCardImageToLocalStorage(const ExactCard &card, const
|
|||
}
|
||||
}
|
||||
|
||||
// Do not overwrite existing files
|
||||
if (outInfo.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save image
|
||||
QImage image = pixmap.toImage();
|
||||
if (!image.save(outInfo.absoluteFilePath(), "PNG")) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "card_picture_loader_local.h"
|
||||
|
||||
#include "../../client/settings/cache_settings.h"
|
||||
#include "card_picture_loader_local_schemes.h"
|
||||
#include "card_picture_to_load.h"
|
||||
|
||||
#include <QDirIterator>
|
||||
|
|
@ -77,26 +78,8 @@ QImage CardPictureLoaderLocal::tryLoadCardImageFromDisk(const QString &setName,
|
|||
imgReader.setDecideFormatFromContent(true);
|
||||
|
||||
// Most-to-least specific, these will fall through in order.
|
||||
QStringList nameVariants;
|
||||
|
||||
// cardName_providerId
|
||||
if (!providerId.isEmpty()) {
|
||||
nameVariants << QString("%1-%2").arg(correctedCardName, providerId)
|
||||
<< QString("%1_%2").arg(correctedCardName, providerId);
|
||||
}
|
||||
// cardName_setName_collectorNumber & setName-collectorNumber-cardName
|
||||
if (!setName.isEmpty() && !collectorNumber.isEmpty()) {
|
||||
nameVariants << QString("%1_%2_%3").arg(correctedCardName, setName, collectorNumber)
|
||||
<< QString("%1-%2-%3").arg(setName, collectorNumber, correctedCardName);
|
||||
}
|
||||
// cardName_setName
|
||||
if (!setName.isEmpty()) {
|
||||
nameVariants << QString("%1_%2").arg(correctedCardName, setName)
|
||||
<< QString("%1-%2").arg(setName, correctedCardName);
|
||||
}
|
||||
|
||||
// cardName
|
||||
nameVariants << correctedCardName;
|
||||
QStringList nameVariants =
|
||||
CardPictureLoaderLocalSchemes::generateImportVariants(correctedCardName, setName, collectorNumber, providerId);
|
||||
|
||||
for (const QString &nameVariant : nameVariants) {
|
||||
if (nameVariant.isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
#ifndef COCKATRICE_CARD_PICTURE_LOADER_LOCAL_SCHEMES_H
|
||||
#define COCKATRICE_CARD_PICTURE_LOADER_LOCAL_SCHEMES_H
|
||||
|
||||
#include <QList>
|
||||
#include <QRegularExpression>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
namespace CardPictureLoaderLocalSchemes
|
||||
{
|
||||
|
||||
enum class NamingScheme
|
||||
{
|
||||
NameOnly,
|
||||
Name_Set,
|
||||
Name_Set_Collector,
|
||||
Set_Collector_Name,
|
||||
Name_ProviderId,
|
||||
Set_Folder_Name_ProviderId,
|
||||
Set_Folder_Name_Set_Collector
|
||||
};
|
||||
|
||||
struct NamingSchemeInfo
|
||||
{
|
||||
NamingScheme id;
|
||||
QString displayName;
|
||||
QString pattern;
|
||||
};
|
||||
|
||||
inline const QList<NamingSchemeInfo> &importSchemes()
|
||||
{
|
||||
static QList<NamingSchemeInfo> list = {
|
||||
{NamingScheme::Name_ProviderId, "Card Name + Provider ID", "{name}_{providerId}"},
|
||||
{NamingScheme::Name_Set_Collector, "Card Name + Set + Collector", "{name}_{set}_{collector}"},
|
||||
{NamingScheme::Set_Collector_Name, "Set + Collector + Card Name", "{set}_{collector}_{name}"},
|
||||
{NamingScheme::Name_Set, "Card Name + Set", "{name}_{set}"},
|
||||
{NamingScheme::NameOnly, "Card Name", "{name}"},
|
||||
};
|
||||
return list;
|
||||
}
|
||||
|
||||
inline const QList<NamingSchemeInfo> &exportSchemes()
|
||||
{
|
||||
static QList<NamingSchemeInfo> list = {
|
||||
{NamingScheme::Set_Folder_Name_ProviderId, "Set Folder / Name + Provider ID", "{set}/{name}_{providerId}"},
|
||||
{NamingScheme::Set_Folder_Name_Set_Collector, "Set Folder / Name + Set Name + Collector",
|
||||
"{set}/{name}_{set}_{collector}"},
|
||||
{NamingScheme::Name_ProviderId, "Card Name + Provider ID", "{name}_{providerId}"},
|
||||
{NamingScheme::Name_Set_Collector, "Card Name + Set + Collector", "{name}_{set}_{collector}"},
|
||||
{NamingScheme::Set_Collector_Name, "Set + Collector + Card Name", "{set}_{collector}_{name}"},
|
||||
};
|
||||
return list;
|
||||
}
|
||||
|
||||
inline QString expandPattern(const QString &pattern,
|
||||
const QString &name,
|
||||
const QString &set,
|
||||
const QString &collector,
|
||||
const QString &providerId)
|
||||
{
|
||||
QString result = pattern;
|
||||
|
||||
auto replaceIfPresent = [&](const QString &token, const QString &value) -> bool {
|
||||
if (!result.contains(token))
|
||||
return true;
|
||||
|
||||
if (value.isEmpty())
|
||||
return false;
|
||||
|
||||
result.replace(token, value);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!replaceIfPresent("{name}", name))
|
||||
return {};
|
||||
if (!replaceIfPresent("{set}", set))
|
||||
return {};
|
||||
if (!replaceIfPresent("{collector}", collector))
|
||||
return {};
|
||||
if (!replaceIfPresent("{providerId}", providerId))
|
||||
return {};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
inline QStringList
|
||||
generateImportVariants(const QString &name, const QString &set, const QString &collector, const QString &providerId)
|
||||
{
|
||||
QStringList variants;
|
||||
const QStringList separators = {"_", "-"};
|
||||
|
||||
for (const auto &scheme : importSchemes()) {
|
||||
for (const QString &sep : separators) {
|
||||
|
||||
QString pattern = scheme.pattern;
|
||||
pattern.replace("_", sep);
|
||||
|
||||
QString v = expandPattern(pattern, name, set, collector, providerId);
|
||||
if (!v.isEmpty())
|
||||
variants << v;
|
||||
}
|
||||
}
|
||||
|
||||
return variants;
|
||||
}
|
||||
|
||||
} // namespace CardPictureLoaderLocalSchemes
|
||||
|
||||
#endif // COCKATRICE_CARD_PICTURE_LOADER_LOCAL_SCHEMES_H
|
||||
|
|
@ -1079,10 +1079,24 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
connect(&saveCardImagesToLocalStorageCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance(),
|
||||
&SettingsCache::setSaveCardImagesToLocalStorage);
|
||||
|
||||
localCardImageStorageNamingSchemeLineEdit =
|
||||
new QLineEdit(SettingsCache::instance().getLocalCardImageStorageNamingScheme());
|
||||
connect(localCardImageStorageNamingSchemeLineEdit, &QLineEdit::textChanged, &SettingsCache::instance(),
|
||||
&SettingsCache::setLocalCardImageStorageNamingScheme);
|
||||
localCardImageStorageNamingSchemeComboBox = new QComboBox;
|
||||
for (const auto &scheme : CardPictureLoaderLocalSchemes::exportSchemes()) {
|
||||
localCardImageStorageNamingSchemeComboBox->addItem(scheme.displayName, static_cast<int>(scheme.id));
|
||||
}
|
||||
|
||||
int current = static_cast<int>(SettingsCache::instance().getLocalCardImageStorageNamingScheme());
|
||||
|
||||
int index = localCardImageStorageNamingSchemeComboBox->findData(current);
|
||||
if (index >= 0) {
|
||||
localCardImageStorageNamingSchemeComboBox->setCurrentIndex(index);
|
||||
}
|
||||
|
||||
connect(localCardImageStorageNamingSchemeComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this,
|
||||
[this](int index) {
|
||||
auto scheme = static_cast<CardPictureLoaderLocalSchemes::NamingScheme>(
|
||||
localCardImageStorageNamingSchemeComboBox->itemData(index).toInt());
|
||||
SettingsCache::instance().setLocalCardImageStorageNamingScheme(scheme);
|
||||
});
|
||||
|
||||
auto networkCacheLayout = new QHBoxLayout;
|
||||
networkCacheLayout->addStretch();
|
||||
|
|
@ -1105,7 +1119,7 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
|
||||
auto localCardImageStorageNamingSchemeLayout = new QHBoxLayout;
|
||||
localCardImageStorageNamingSchemeLayout->addWidget(&localCardImageStorageNamingSchemeLabel);
|
||||
localCardImageStorageNamingSchemeLayout->addWidget(localCardImageStorageNamingSchemeLineEdit);
|
||||
localCardImageStorageNamingSchemeLayout->addWidget(localCardImageStorageNamingSchemeComboBox);
|
||||
|
||||
// Top Layout
|
||||
lpGeneralGrid->addWidget(&picDownloadCheckBox, 0, 0);
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ private:
|
|||
QCheckBox saveCardImagesToLocalStorageCheckBox;
|
||||
QLabel saveCardImagesToLocalStorageLabel;
|
||||
QLabel localCardImageStorageNamingSchemeLabel;
|
||||
QLineEdit *localCardImageStorageNamingSchemeLineEdit;
|
||||
QComboBox *localCardImageStorageNamingSchemeComboBox;
|
||||
};
|
||||
|
||||
class MessagesSettingsPage : public AbstractSettingsPage
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue