mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-29 17:32:17 -07:00
Merge branch 'master' into tooomm-qt5
This commit is contained in:
commit
bbf2412157
219 changed files with 7310 additions and 1824 deletions
|
|
@ -19,19 +19,22 @@
|
|||
#include <QThread>
|
||||
#include <algorithm>
|
||||
#include <libcockatrice/settings/cache_storage_settings.h>
|
||||
#include <libcockatrice/settings/download_settings.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
#include <utility>
|
||||
|
||||
// never cache more than 300 cards at once for a single deck
|
||||
#define CACHED_CARD_PER_DECK_MAX 300
|
||||
|
||||
// wait at least this long before retrying a card whose picture failed to load
|
||||
static constexpr int RETRY_FAILED_CARDS_SECS = 300;
|
||||
|
||||
CardPictureLoader::CardPictureLoader() : QObject(nullptr)
|
||||
{
|
||||
worker = new CardPictureLoaderWorker;
|
||||
connect(&SettingsCache::instance().paths(), &PathsSettings::picsPathChanged, this,
|
||||
&CardPictureLoader::picsPathChanged);
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::picDownloadChanged, this,
|
||||
connect(&SettingsCache::instance().downloads(), &DownloadSettings::picDownloadChanged, this,
|
||||
&CardPictureLoader::picDownloadChanged);
|
||||
|
||||
qRegisterMetaType<ExactCard>();
|
||||
|
|
@ -135,7 +138,14 @@ void CardPictureLoader::getPixmap(QPixmap &pixmap, const ExactCard &card, QSize
|
|||
QPixmap bigPixmap;
|
||||
if (QPixmapCache::find(key, &bigPixmap)) {
|
||||
if (bigPixmap.isNull()) {
|
||||
qCDebug(CardPictureLoaderLog) << "Cached pixmap for key" << key << "is NULL!";
|
||||
getCardBackLoadingFailedPixmap(pixmap, size);
|
||||
QDateTime failedAtTime = getInstance().failedAt.value(key);
|
||||
if (!failedAtTime.isValid() ||
|
||||
failedAtTime.addSecs(RETRY_FAILED_CARDS_SECS) < QDateTime::currentDateTime()) {
|
||||
getInstance().failedAt.remove(key);
|
||||
QPixmapCache::remove(key);
|
||||
getInstance().worker->enqueueImageLoad(card);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -159,8 +169,10 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
|
|||
QPixmap finalPixmap;
|
||||
|
||||
if (image.isNull()) {
|
||||
getInstance().failedAt.insert(card.getPixmapCacheKey(), QDateTime::currentDateTime());
|
||||
qCDebug(CardPictureLoaderLog) << "Caching NULL pixmap for" << card.getName();
|
||||
} else {
|
||||
getInstance().failedAt.remove(card.getPixmapCacheKey());
|
||||
if (card.getInfo().getUiAttributes().upsideDownArt) {
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
|
||||
QImage mirrorImage = image.flipped(Qt::Horizontal | Qt::Vertical);
|
||||
|
|
@ -184,8 +196,10 @@ void CardPictureLoader::imageLoaded(const ExactCard &card, const QImage &image)
|
|||
// imageLoaded should only be reached if the exactCard isn't already in cache.
|
||||
// (plus there's a deduplication mechanism in CardPictureLoaderWorker)
|
||||
// It should be safe to connect the CardInfo here without worrying about redundant connections.
|
||||
connect(card.getCardPtr().data(), &QObject::destroyed, this,
|
||||
[cacheKey = card.getPixmapCacheKey()] { QPixmapCache::remove(cacheKey); });
|
||||
connect(card.getCardPtr().data(), &QObject::destroyed, this, [cacheKey = card.getPixmapCacheKey()] {
|
||||
QPixmapCache::remove(cacheKey);
|
||||
getInstance().failedAt.remove(cacheKey);
|
||||
});
|
||||
|
||||
card.emitPixmapUpdated();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
#include "card_picture_loader_status_bar.h"
|
||||
#include "card_picture_loader_worker.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QHash>
|
||||
#include <QLoggingCategory>
|
||||
|
||||
inline Q_LOGGING_CATEGORY(CardPictureLoaderLog, "card_picture_loader");
|
||||
|
|
@ -56,6 +58,7 @@ private:
|
|||
|
||||
CardPictureLoaderWorker *worker; ///< Worker thread for async image loading
|
||||
CardPictureLoaderStatusBar *statusBar; ///< Status bar widget showing load progress
|
||||
QHash<QString, QDateTime> failedAt; ///< Timestamp of the last failed load attempt per pixmap cache key
|
||||
|
||||
public:
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -11,15 +11,17 @@
|
|||
#include <QNetworkReply>
|
||||
#include <QThread>
|
||||
#include <libcockatrice/settings/cache_storage_settings.h>
|
||||
#include <libcockatrice/settings/download_settings.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
#include <utility>
|
||||
#include <version_string.h>
|
||||
|
||||
static constexpr int MAX_REQUESTS_PER_SEC = 10;
|
||||
static constexpr int MIN_HOST_QUOTA = 1; ///< Floor for the per-host request allowance
|
||||
static constexpr qint64 QUOTA_RECOVER_MS = 60000; ///< Idle time before a reduced quota starts recovering
|
||||
|
||||
CardPictureLoaderWorker::CardPictureLoaderWorker()
|
||||
: QObject(nullptr), picDownload(SettingsCache::instance().personal().getPicDownload()),
|
||||
: QObject(nullptr), picDownload(SettingsCache::instance().downloads().getPicDownload()),
|
||||
requestQuota(MAX_REQUESTS_PER_SEC)
|
||||
{
|
||||
networkManager = new QNetworkAccessManager(this);
|
||||
|
|
@ -124,6 +126,19 @@ QNetworkReply *CardPictureLoaderWorker::makeRequest(const QUrl &url, CardPicture
|
|||
void CardPictureLoaderWorker::resetRequestQuota()
|
||||
{
|
||||
requestQuota = MAX_REQUESTS_PER_SEC;
|
||||
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
for (auto it = hostRequestQuota.begin(); it != hostRequestQuota.end(); ++it) {
|
||||
if (!hostLast429.contains(it.key()) || now.msecsTo(hostLast429.value(it.key())) < -QUOTA_RECOVER_MS) {
|
||||
it.value() = qMin(MAX_REQUESTS_PER_SEC, it.value() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &request : requestLoadQueue) {
|
||||
const QString host = request.first.host();
|
||||
hostQuotaRemaining.insert(host, hostRequestQuota.value(host, MAX_REQUESTS_PER_SEC));
|
||||
}
|
||||
|
||||
processQueuedRequests();
|
||||
}
|
||||
|
||||
|
|
@ -136,14 +151,26 @@ void CardPictureLoaderWorker::processQueuedRequests()
|
|||
|
||||
bool CardPictureLoaderWorker::processSingleRequest()
|
||||
{
|
||||
if (!requestLoadQueue.isEmpty()) {
|
||||
auto request = requestLoadQueue.takeFirst();
|
||||
makeRequest(request.first, request.second);
|
||||
return true;
|
||||
for (int i = 0; i < requestLoadQueue.size(); ++i) {
|
||||
const auto &request = requestLoadQueue.at(i);
|
||||
QString host = request.first.host();
|
||||
int allowance = hostQuotaRemaining.value(host, MAX_REQUESTS_PER_SEC);
|
||||
if (allowance > 0) {
|
||||
hostQuotaRemaining.insert(host, allowance - 1);
|
||||
makeRequest(request.first, request.second);
|
||||
requestLoadQueue.removeAt(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CardPictureLoaderWorker::onHostRateLimited(const QString &host)
|
||||
{
|
||||
hostRequestQuota.insert(host, qMax(MIN_HOST_QUOTA, hostRequestQuota.value(host, MAX_REQUESTS_PER_SEC) / 2));
|
||||
hostLast429.insert(host, QDateTime::currentDateTime());
|
||||
}
|
||||
|
||||
void CardPictureLoaderWorker::enqueueImageLoad(const ExactCard &card)
|
||||
{
|
||||
// Send call through a connection to ensure the handling is run on the pictureLoader thread
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
#include "card_picture_loader_worker_work.h"
|
||||
#include "card_picture_to_load.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QHash>
|
||||
#include <QLoggingCategory>
|
||||
#include <QMutex>
|
||||
#include <QNetworkAccessManager>
|
||||
|
|
@ -66,6 +68,12 @@ public:
|
|||
*/
|
||||
void queueRequest(const QUrl &url, CardPictureLoaderWorkerWork *worker);
|
||||
|
||||
/**
|
||||
* @brief Handles a server returning HTTP 429 by reducing that host's request quota.
|
||||
* @param host The host that returned 429
|
||||
*/
|
||||
void onHostRateLimited(const QString &host);
|
||||
|
||||
/** @brief Clears the network cache and redirect cache. */
|
||||
void clearNetworkCache();
|
||||
|
||||
|
|
@ -110,8 +118,11 @@ private:
|
|||
bool picDownload; ///< Whether downloading images from network is enabled
|
||||
QQueue<QPair<QUrl, CardPictureLoaderWorkerWork *>> requestLoadQueue; ///< Queue of pending network requests
|
||||
|
||||
int requestQuota; ///< Remaining requests allowed per second
|
||||
QTimer requestTimer; ///< Timer to reset the request quota
|
||||
int requestQuota; ///< Remaining requests allowed per second
|
||||
QTimer requestTimer; ///< Timer to reset the request quota
|
||||
QHash<QString, int> hostRequestQuota; ///< Sustained per-host request allowance
|
||||
QHash<QString, int> hostQuotaRemaining; ///< Per-host allowance left in the current second
|
||||
QHash<QString, QDateTime> hostLast429; ///< When each host was last rate limited
|
||||
|
||||
CardPictureLoaderLocal *localLoader; ///< Loader for local images
|
||||
QSet<QString> currentlyLoading; ///< Deduplication: contains pixmapCacheKey currently being loaded
|
||||
|
|
|
|||
|
|
@ -8,9 +8,13 @@
|
|||
#include <QLoggingCategory>
|
||||
#include <QMovie>
|
||||
#include <QNetworkReply>
|
||||
#include <QRandomGenerator>
|
||||
#include <QThread>
|
||||
#include <QThreadPool>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
#include <QTimer>
|
||||
|
||||
ServerRateLimiter CardPictureLoaderWorkerWork::s_rateLimiter;
|
||||
#include <libcockatrice/settings/download_settings.h>
|
||||
|
||||
// Card back returned by gatherer when card is not found
|
||||
static const QStringList MD5_BLACKLIST = {
|
||||
|
|
@ -20,7 +24,7 @@ static const QStringList MD5_BLACKLIST = {
|
|||
|
||||
CardPictureLoaderWorkerWork::CardPictureLoaderWorkerWork(const CardPictureLoaderWorker *worker, const ExactCard &toLoad)
|
||||
: QObject(nullptr), cardToDownload(CardPictureToLoad(toLoad)),
|
||||
picDownload(SettingsCache::instance().personal().getPicDownload())
|
||||
picDownload(SettingsCache::instance().downloads().getPicDownload())
|
||||
{
|
||||
// Hook up signals to the orchestrator
|
||||
connect(this, &CardPictureLoaderWorkerWork::requestImageDownload, worker, &CardPictureLoaderWorker::queueRequest);
|
||||
|
|
@ -30,19 +34,48 @@ CardPictureLoaderWorkerWork::CardPictureLoaderWorkerWork(const CardPictureLoader
|
|||
connect(this, &CardPictureLoaderWorkerWork::imageLoaded, worker, &CardPictureLoaderWorker::handleImageLoaded);
|
||||
connect(this, &CardPictureLoaderWorkerWork::requestSucceeded, worker,
|
||||
&CardPictureLoaderWorker::imageRequestSucceeded);
|
||||
connect(this, &CardPictureLoaderWorkerWork::rateLimited, worker, &CardPictureLoaderWorker::onHostRateLimited);
|
||||
|
||||
// Hook up signals to settings
|
||||
connect(&SettingsCache::instance().personal(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
|
||||
connect(&SettingsCache::instance().downloads(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
|
||||
|
||||
startNextPicDownload();
|
||||
}
|
||||
|
||||
void CardPictureLoaderWorkerWork::startNextPicDownload()
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
while (!cardToDownload.getCurrentUrl().isEmpty() &&
|
||||
s_rateLimiter.isRateLimited(QUrl(cardToDownload.getCurrentUrl()).host(), now)) {
|
||||
QString host = QUrl(cardToDownload.getCurrentUrl()).host();
|
||||
if (s_rateLimiter.rounds(host) == 1) {
|
||||
// First 429 round for this server: wait out the backoff and give it
|
||||
// one more chance instead of immediately falling through to a worse
|
||||
// source. A second 429 makes us fall through instead.
|
||||
qCDebug(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getInfo().getCorrectedName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: Waiting out backoff for " << host << " to retry "
|
||||
<< cardToDownload.getCurrentUrl();
|
||||
scheduleDeferredRetry();
|
||||
return;
|
||||
}
|
||||
|
||||
// The server has already 429'd us at least twice, so further retries are
|
||||
// unlikely to succeed: move on to the other configured sources.
|
||||
qCDebug(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getInfo().getCorrectedName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: Skipping rate-limited URL "
|
||||
<< cardToDownload.getCurrentUrl() << " (server " << host << " still rate limiting)";
|
||||
if (!cardToDownload.nextUrl() && !cardToDownload.nextSet()) {
|
||||
scheduleDeferredRetry();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QString picUrl = cardToDownload.getCurrentUrl();
|
||||
|
||||
if (picUrl.isEmpty()) {
|
||||
picDownloadFailed();
|
||||
scheduleDeferredRetry();
|
||||
} else {
|
||||
QUrl url(picUrl);
|
||||
qCDebug(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
|
|
@ -108,7 +141,41 @@ static bool imageIsBlackListed(const QByteArray &picData)
|
|||
void CardPictureLoaderWorkerWork::handleFailedReply(const QNetworkReply *reply)
|
||||
{
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 429) {
|
||||
qCWarning(CardPictureLoaderWorkerWorkLog) << "Too many requests.";
|
||||
QString host = reply->url().host();
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
||||
qint64 retryAfterMs = 0;
|
||||
const QByteArray retryAfterHeader = reply->rawHeader("Retry-After");
|
||||
if (!retryAfterHeader.isEmpty()) {
|
||||
bool ok = false;
|
||||
int seconds = retryAfterHeader.toInt(&ok);
|
||||
if (ok && seconds > 0) {
|
||||
retryAfterMs = static_cast<qint64>(seconds) * 1000;
|
||||
} else {
|
||||
QDateTime retryAfterDate =
|
||||
QDateTime::fromString(QString::fromLatin1(retryAfterHeader), Qt::RFC2822Date);
|
||||
if (retryAfterDate.isValid()) {
|
||||
retryAfterMs = qMax<qint64>(0, now.msecsTo(retryAfterDate));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QDateTime backoffUntil = s_rateLimiter.on429(host, now, retryAfterMs);
|
||||
emit rateLimited(host);
|
||||
|
||||
if (s_rateLimiter.rounds(host) == 1) {
|
||||
qCWarning(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: Too many requests from " << host
|
||||
<< ", backing off until " << backoffUntil.toString(Qt::ISODate) << ", retrying the same url";
|
||||
scheduleDeferredRetry();
|
||||
} else {
|
||||
qCWarning(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: Too many requests from " << host
|
||||
<< ", retry already attempted, falling through to other sources";
|
||||
picDownloadFailed();
|
||||
}
|
||||
} else {
|
||||
bool isFromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
|
||||
|
||||
|
|
@ -149,6 +216,9 @@ void CardPictureLoaderWorkerWork::handleSuccessfulReply(QNetworkReply *reply)
|
|||
return;
|
||||
}
|
||||
|
||||
// A non-redirect successful response means the server is not rate limiting us anymore.
|
||||
s_rateLimiter.onSuccess(reply->url().host());
|
||||
|
||||
// peek is used to keep the data in the buffer for use by QImageReader
|
||||
const QByteArray &picData = reply->peek(reply->size());
|
||||
|
||||
|
|
@ -203,6 +273,42 @@ QImage CardPictureLoaderWorkerWork::tryLoadImageFromReply(QNetworkReply *reply)
|
|||
return imgReader.read();
|
||||
}
|
||||
|
||||
void CardPictureLoaderWorkerWork::scheduleDeferredRetry()
|
||||
{
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
|
||||
// Prefer waiting on the current URL's server so we retry the same source.
|
||||
QString currentHost = QUrl(cardToDownload.getCurrentUrl()).host();
|
||||
QDateTime backoffUntil = s_rateLimiter.deadline(currentHost);
|
||||
if (!s_rateLimiter.isRateLimited(currentHost, now)) {
|
||||
backoffUntil = s_rateLimiter.earliestDeadline(now);
|
||||
}
|
||||
|
||||
if (!backoffUntil.isValid()) {
|
||||
qCWarning(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getInfo().getCorrectedName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: All URLs exhausted, no servers in backoff: BAILING OUT";
|
||||
concludeImageLoad(QImage());
|
||||
return;
|
||||
}
|
||||
|
||||
qint64 waitMs = qMax<qint64>(0, now.msecsTo(backoffUntil));
|
||||
// Add some jitter to desynchronize concurrent retries and avoid a thundering herd.
|
||||
waitMs += QRandomGenerator::global()->bounded(5000);
|
||||
|
||||
qCDebug(CardPictureLoaderWorkerWorkLog).nospace()
|
||||
<< "PictureLoader: [card: " << cardToDownload.getCard().getInfo().getCorrectedName()
|
||||
<< " set: " << cardToDownload.getSetName() << "]: All URLs exhausted, scheduling deferred retry in " << waitMs
|
||||
<< "ms";
|
||||
|
||||
QTimer::singleShot(waitMs, this, [this] {
|
||||
s_rateLimiter.clearExpired(QDateTime::currentDateTime());
|
||||
|
||||
cardToDownload.resetIndices();
|
||||
startNextPicDownload();
|
||||
});
|
||||
}
|
||||
|
||||
void CardPictureLoaderWorkerWork::concludeImageLoad(const QImage &image)
|
||||
{
|
||||
emit imageLoaded(cardToDownload.getCard(), image);
|
||||
|
|
@ -211,5 +317,5 @@ void CardPictureLoaderWorkerWork::concludeImageLoad(const QImage &image)
|
|||
|
||||
void CardPictureLoaderWorkerWork::picDownloadChanged()
|
||||
{
|
||||
picDownload = SettingsCache::instance().personal().getPicDownload();
|
||||
picDownload = SettingsCache::instance().downloads().getPicDownload();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,15 @@
|
|||
#include "card_picture_loader_worker.h"
|
||||
#include "card_picture_to_load.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QLoggingCategory>
|
||||
#include <QMutex>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QObject>
|
||||
#include <QRandomGenerator>
|
||||
#include <QThread>
|
||||
#include <libcockatrice/card/database/card_database.h>
|
||||
#include <libcockatrice/utility/server_rate_limiter.h>
|
||||
|
||||
inline Q_LOGGING_CATEGORY(CardPictureLoaderWorkerWorkLog, "card_picture_loader.worker");
|
||||
|
||||
|
|
@ -50,6 +53,8 @@ public slots:
|
|||
private:
|
||||
bool picDownload; ///< Whether network downloading is enabled
|
||||
|
||||
static ServerRateLimiter s_rateLimiter; ///< Shared per-server 429 backoff state
|
||||
|
||||
/** @brief Starts downloading the next URL for this card. */
|
||||
void startNextPicDownload();
|
||||
|
||||
|
|
@ -77,6 +82,16 @@ private:
|
|||
*/
|
||||
void concludeImageLoad(const QImage &image);
|
||||
|
||||
/**
|
||||
* @brief Schedules a deferred retry after the relevant server backoff expires.
|
||||
*
|
||||
* Waits on the current URL's server when it is the reason we are blocked,
|
||||
* otherwise on the earliest active backoff. If no servers are in backoff,
|
||||
* concludes with failure. Otherwise resets the CardPictureToLoad indices and
|
||||
* retries after the backoff period.
|
||||
*/
|
||||
void scheduleDeferredRetry();
|
||||
|
||||
private slots:
|
||||
/** @brief Updates the picDownload setting when it changes. */
|
||||
void picDownloadChanged();
|
||||
|
|
@ -100,6 +115,9 @@ signals:
|
|||
/** @brief Emitted when a URL has been redirected. */
|
||||
void urlRedirected(const QUrl &originalUrl, const QUrl &redirectUrl);
|
||||
|
||||
/** @brief Emitted when a server returned HTTP 429. */
|
||||
void rateLimited(const QString &host);
|
||||
|
||||
/** @brief Emitted when a cached URL is invalid and must be removed. */
|
||||
void cachedUrlInvalidated(const QUrl &url);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ CardPictureToLoad::CardPictureToLoad(const ExactCard &_card)
|
|||
{
|
||||
if (card) {
|
||||
sortedSets = extractSetsSorted(card);
|
||||
// The first time called, nextSet will also populate the Urls for the first set.
|
||||
nextSet();
|
||||
currentSetIndex = 0;
|
||||
currentSet = sortedSets.first();
|
||||
populateSetUrls();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,15 +102,19 @@ void CardPictureToLoad::populateSetUrls()
|
|||
}
|
||||
}
|
||||
|
||||
/* Call nextUrl to make sure currentUrl is up-to-date
|
||||
but we don't need the result here. */
|
||||
(void)nextUrl();
|
||||
currentUrlIndex = 0;
|
||||
if (!currentSetUrls.isEmpty()) {
|
||||
currentUrl = currentSetUrls.first();
|
||||
} else {
|
||||
currentUrl = QString();
|
||||
}
|
||||
}
|
||||
|
||||
bool CardPictureToLoad::nextSet()
|
||||
{
|
||||
if (!sortedSets.isEmpty()) {
|
||||
currentSet = sortedSets.takeFirst();
|
||||
currentSetIndex++;
|
||||
if (currentSetIndex < sortedSets.size()) {
|
||||
currentSet = sortedSets.at(currentSetIndex);
|
||||
populateSetUrls();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -119,8 +124,9 @@ bool CardPictureToLoad::nextSet()
|
|||
|
||||
bool CardPictureToLoad::nextUrl()
|
||||
{
|
||||
if (!currentSetUrls.isEmpty()) {
|
||||
currentUrl = currentSetUrls.takeFirst();
|
||||
currentUrlIndex++;
|
||||
if (currentUrlIndex < currentSetUrls.size()) {
|
||||
currentUrl = currentSetUrls.at(currentUrlIndex);
|
||||
return true;
|
||||
}
|
||||
currentUrl = QString();
|
||||
|
|
@ -136,6 +142,28 @@ QString CardPictureToLoad::getSetName() const
|
|||
}
|
||||
}
|
||||
|
||||
QString CardPictureToLoad::peekNextUrl() const
|
||||
{
|
||||
int nextIndex = currentUrlIndex + 1;
|
||||
if (nextIndex < currentSetUrls.size()) {
|
||||
return currentSetUrls.at(nextIndex);
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
void CardPictureToLoad::resetIndices()
|
||||
{
|
||||
currentSetIndex = 0;
|
||||
if (!sortedSets.isEmpty()) {
|
||||
currentSet = sortedSets.first();
|
||||
populateSetUrls();
|
||||
} else {
|
||||
currentSet = {};
|
||||
currentSetUrls.clear();
|
||||
currentUrl = QString();
|
||||
}
|
||||
}
|
||||
|
||||
static int parse(const QString &urlTemplate,
|
||||
const QString &propType,
|
||||
const QString &cardName,
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ private:
|
|||
QList<QString> currentSetUrls; ///< URLs for the current set being attempted
|
||||
QString currentUrl; ///< Currently active URL to download
|
||||
CardSetPtr currentSet; ///< Currently active set
|
||||
int currentSetIndex = 0; ///< Current position in sortedSets
|
||||
int currentUrlIndex = 0; ///< Current position in currentSetUrls
|
||||
|
||||
public:
|
||||
/**
|
||||
|
|
@ -56,6 +58,9 @@ public:
|
|||
/** @return The short name of the current set, or empty string if no set. */
|
||||
[[nodiscard]] QString getSetName() const;
|
||||
|
||||
/** @return The next URL in the current set's list without advancing, or empty if at end. */
|
||||
[[nodiscard]] QString peekNextUrl() const;
|
||||
|
||||
/**
|
||||
* @brief Transforms a URL template into a concrete URL for this card/set.
|
||||
* @param urlTemplate The URL template to transform
|
||||
|
|
@ -88,6 +93,14 @@ public:
|
|||
*/
|
||||
void populateSetUrls();
|
||||
|
||||
/**
|
||||
* @brief Resets iteration indices to the beginning.
|
||||
*
|
||||
* Restarts URL/set iteration from the first set and first URL.
|
||||
* Used for deferred retry after server backoff expires.
|
||||
*/
|
||||
void resetIndices();
|
||||
|
||||
/**
|
||||
* @brief Extract all sets from the card and sort them by priority.
|
||||
* @param card The card to extract sets from
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef COCKATRICE_CONTEXT_CONNECT_TO_SERVER_H
|
||||
#define COCKATRICE_CONTEXT_CONNECT_TO_SERVER_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct ContextConnectToServer
|
||||
{
|
||||
QString hostname;
|
||||
QString port;
|
||||
QString username;
|
||||
QString password;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CONTEXT_CONNECT_TO_SERVER_H
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef COCKATRICE_CONTEXT_JOIN_GAME_H
|
||||
#define COCKATRICE_CONTEXT_JOIN_GAME_H
|
||||
#include "context_join_room.h"
|
||||
|
||||
struct ContextJoinGame
|
||||
{
|
||||
ContextJoinRoom roomContext;
|
||||
int gameId;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CONTEXT_JOIN_GAME_H
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef COCKATRICE_CONTEXT_JOIN_ROOM_H
|
||||
#define COCKATRICE_CONTEXT_JOIN_ROOM_H
|
||||
|
||||
#include "context_connect_to_server.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct ContextJoinRoom
|
||||
{
|
||||
ContextConnectToServer serverContext;
|
||||
int roomId;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CONTEXT_JOIN_ROOM_H
|
||||
48
cockatrice/src/interface/intents/intent.cpp
Normal file
48
cockatrice/src/interface/intents/intent.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include "intent.h"
|
||||
|
||||
Intent::Intent(QObject *parent) : QObject(parent)
|
||||
{
|
||||
// An intent is done as soon as it reports success or failure. Deleting it
|
||||
// also tears down its dependency chain and disconnects any signal wiring.
|
||||
connect(this, &Intent::finished, this, &QObject::deleteLater);
|
||||
connect(this, &Intent::failed, this, &QObject::deleteLater);
|
||||
}
|
||||
|
||||
Intent::~Intent() = default;
|
||||
|
||||
void Intent::execute()
|
||||
{
|
||||
if (checkPrecondition()) {
|
||||
onPreconditionSatisfied();
|
||||
} else {
|
||||
onPreconditionNotSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
void Intent::runDependency(Intent *dependency)
|
||||
{
|
||||
dependency->setParent(this);
|
||||
connect(dependency, &Intent::finished, this, [this]() {
|
||||
// Re-check after dependency finishes
|
||||
this->execute();
|
||||
});
|
||||
connect(dependency, &Intent::failed, this, &Intent::failed);
|
||||
|
||||
dependency->execute();
|
||||
}
|
||||
|
||||
void Intent::emitFinished()
|
||||
{
|
||||
if (!completed) {
|
||||
completed = true;
|
||||
emit finished();
|
||||
}
|
||||
}
|
||||
|
||||
void Intent::emitFailed(const QString &reason)
|
||||
{
|
||||
if (!completed) {
|
||||
completed = true;
|
||||
emit failed(reason);
|
||||
}
|
||||
}
|
||||
37
cockatrice/src/interface/intents/intent.h
Normal file
37
cockatrice/src/interface/intents/intent.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef COCKATRICE_INTENT_H
|
||||
#define COCKATRICE_INTENT_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class Intent : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Intent(QObject *parent = nullptr);
|
||||
~Intent() override;
|
||||
|
||||
void execute();
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
void failed(QString reason);
|
||||
|
||||
protected:
|
||||
// --- Subclasses must implement these ---
|
||||
virtual bool checkPrecondition() const = 0;
|
||||
virtual void onPreconditionSatisfied() = 0;
|
||||
virtual void onPreconditionNotSatisfied() = 0;
|
||||
|
||||
// Helper to chain another intent
|
||||
void runDependency(Intent *dependency);
|
||||
|
||||
// Emit the outcome exactly once; ignore late signals after the intent is done.
|
||||
void emitFinished();
|
||||
void emitFailed(const QString &reason);
|
||||
|
||||
private:
|
||||
bool completed = false;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_H
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
#include "intent_connect_to_server.h"
|
||||
|
||||
#include "intent_disconnect_from_server.h"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
IntentConnectToServer::IntentConnectToServer(RemoteClient *_remoteClient, ContextConnectToServer *_context)
|
||||
: Intent(), remoteClient(_remoteClient), context(_context)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentConnectToServer::checkPrecondition() const
|
||||
{
|
||||
return remoteClient->getStatus() == ClientStatus::StatusDisconnected;
|
||||
}
|
||||
|
||||
void IntentConnectToServer::onPreconditionSatisfied()
|
||||
{
|
||||
remoteClient->connectToServer(context->hostname, context->port.toUInt(), context->username, context->password);
|
||||
connect(remoteClient, &RemoteClient::statusChanged, this, &IntentConnectToServer::onStatusChanged);
|
||||
connect(remoteClient, &RemoteClient::socketError, this, &IntentConnectToServer::onSocketError);
|
||||
connect(
|
||||
remoteClient, &RemoteClient::loginError, this,
|
||||
[this](Response::ResponseCode, const QString &reason, quint32, const QList<QString> &) { emitFailed(reason); });
|
||||
|
||||
QTimer::singleShot(15000, this, [this]() {
|
||||
emitFailed(tr("Timed out while connecting to %1:%2").arg(context->hostname, context->port));
|
||||
});
|
||||
}
|
||||
|
||||
void IntentConnectToServer::onPreconditionNotSatisfied()
|
||||
{
|
||||
runDependency(new IntentDisconnectFromServer(remoteClient));
|
||||
}
|
||||
|
||||
void IntentConnectToServer::onStatusChanged(ClientStatus status)
|
||||
{
|
||||
if (status == ClientStatus::StatusLoggedIn) {
|
||||
emitFinished();
|
||||
}
|
||||
}
|
||||
|
||||
void IntentConnectToServer::onSocketError(const QString &errorString)
|
||||
{
|
||||
emitFailed(tr("Failed to connect to %1:%2: %3").arg(context->hostname, context->port, errorString));
|
||||
}
|
||||
29
cockatrice/src/interface/intents/intent_connect_to_server.h
Normal file
29
cockatrice/src/interface/intents/intent_connect_to_server.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef COCKATRICE_INTENT_CONNECT_TO_SERVER_H
|
||||
#define COCKATRICE_INTENT_CONNECT_TO_SERVER_H
|
||||
|
||||
#include "contexts/context_connect_to_server.h"
|
||||
#include "intent.h"
|
||||
#include "remote_client.h"
|
||||
|
||||
class IntentConnectToServer : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentConnectToServer(RemoteClient *_remoteClient, ContextConnectToServer *_context);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
RemoteClient *remoteClient;
|
||||
ContextConnectToServer *context;
|
||||
|
||||
private slots:
|
||||
void onStatusChanged(ClientStatus status);
|
||||
void onSocketError(const QString &errorString);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_CONNECT_TO_SERVER_H
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include "intent_disconnect_from_server.h"
|
||||
|
||||
IntentDisconnectFromServer::IntentDisconnectFromServer(RemoteClient *_remoteClient)
|
||||
: Intent(), remoteClient(_remoteClient)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentDisconnectFromServer::checkPrecondition() const
|
||||
{
|
||||
return remoteClient->getStatus() == ClientStatus::StatusDisconnected;
|
||||
}
|
||||
|
||||
void IntentDisconnectFromServer::onPreconditionSatisfied()
|
||||
{
|
||||
emitFinished();
|
||||
}
|
||||
|
||||
void IntentDisconnectFromServer::onPreconditionNotSatisfied()
|
||||
{
|
||||
connect(remoteClient, &RemoteClient::statusChanged, this, &IntentDisconnectFromServer::onStatusChanged);
|
||||
remoteClient->disconnectFromServer();
|
||||
}
|
||||
|
||||
void IntentDisconnectFromServer::onStatusChanged(ClientStatus status)
|
||||
{
|
||||
if (status == ClientStatus::StatusDisconnected) {
|
||||
emitFinished();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef COCKATRICE_INTENT_DISCONNECT_FROM_SERVER_H
|
||||
#define COCKATRICE_INTENT_DISCONNECT_FROM_SERVER_H
|
||||
|
||||
#include "intent.h"
|
||||
#include "remote_client.h"
|
||||
|
||||
class IntentDisconnectFromServer : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentDisconnectFromServer(RemoteClient *_remoteClient);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
RemoteClient *remoteClient;
|
||||
|
||||
private slots:
|
||||
void onStatusChanged(ClientStatus status);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_DISCONNECT_FROM_SERVER_H
|
||||
76
cockatrice/src/interface/intents/intent_join_server_game.cpp
Normal file
76
cockatrice/src/interface/intents/intent_join_server_game.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "intent_join_server_game.h"
|
||||
|
||||
#include "../widgets/server/game_selector.h"
|
||||
#include "../widgets/tabs/tab_room.h"
|
||||
#include "../widgets/tabs/tab_supervisor.h"
|
||||
#include "intent_join_server_room.h"
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
IntentJoinServerGame::IntentJoinServerGame(TabSupervisor *_tabSupervisor,
|
||||
RemoteClient *_remoteClient,
|
||||
std::unique_ptr<ContextJoinGame> _context)
|
||||
: Intent(), tabSupervisor(_tabSupervisor), remoteClient(_remoteClient), context(_context.release())
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentJoinServerGame::checkPrecondition() const
|
||||
{
|
||||
if (remoteClient->getStatus() != ClientStatus::StatusLoggedIn) {
|
||||
return false;
|
||||
}
|
||||
// peerPort() reflects the actual TCP peer, which may differ from the
|
||||
// configured server port (e.g. when connecting through a proxy), so only
|
||||
// the hostname is compared here.
|
||||
if (remoteClient->peerName() != context->roomContext.serverContext.hostname) {
|
||||
return false;
|
||||
}
|
||||
if (QString::number(remoteClient->peerPort()) != context->roomContext.serverContext.port) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!tabSupervisor->getRoomTabs().contains(context->roomContext.roomId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IntentJoinServerGame::onPreconditionSatisfied()
|
||||
{
|
||||
TabRoom *room = tabSupervisor->getRoomTabs().value(context->roomContext.roomId);
|
||||
if (!tryJoinGame(room)) {
|
||||
waitForGame(room);
|
||||
}
|
||||
}
|
||||
|
||||
void IntentJoinServerGame::onPreconditionNotSatisfied()
|
||||
{
|
||||
runDependency(new IntentJoinServerRoom(tabSupervisor, remoteClient, &context->roomContext));
|
||||
}
|
||||
|
||||
bool IntentJoinServerGame::tryJoinGame(TabRoom *room)
|
||||
{
|
||||
if (!room) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (room->getGameSelector()->joinGameById(context->gameId)) {
|
||||
emitFinished();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void IntentJoinServerGame::waitForGame(TabRoom *room)
|
||||
{
|
||||
connect(room, &TabRoom::gameListUpdated, this, [this]() {
|
||||
TabRoom *updatedRoom = tabSupervisor->getRoomTabs().value(context->roomContext.roomId);
|
||||
if (updatedRoom) {
|
||||
tryJoinGame(updatedRoom);
|
||||
}
|
||||
});
|
||||
|
||||
QTimer::singleShot(15000, this, [this]() { emitFailed(tr("Game %1 not found in the room").arg(context->gameId)); });
|
||||
}
|
||||
37
cockatrice/src/interface/intents/intent_join_server_game.h
Normal file
37
cockatrice/src/interface/intents/intent_join_server_game.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef COCKATRICE_INTENT_JOIN_SERVER_GAME_H
|
||||
#define COCKATRICE_INTENT_JOIN_SERVER_GAME_H
|
||||
|
||||
#include "contexts/context_join_game.h"
|
||||
#include "intent.h"
|
||||
#include "remote_client.h"
|
||||
|
||||
#include <QScopedPointer>
|
||||
#include <memory>
|
||||
|
||||
class TabRoom;
|
||||
class TabSupervisor;
|
||||
|
||||
class IntentJoinServerGame : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentJoinServerGame(TabSupervisor *_tabSupervisor,
|
||||
RemoteClient *_remoteClient,
|
||||
std::unique_ptr<ContextJoinGame> _context);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
bool tryJoinGame(TabRoom *room);
|
||||
void waitForGame(TabRoom *room);
|
||||
|
||||
TabSupervisor *tabSupervisor;
|
||||
RemoteClient *remoteClient;
|
||||
QScopedPointer<ContextJoinGame> context;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_JOIN_SERVER_GAME_H
|
||||
74
cockatrice/src/interface/intents/intent_join_server_room.cpp
Normal file
74
cockatrice/src/interface/intents/intent_join_server_room.cpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include "intent_join_server_room.h"
|
||||
|
||||
#include "../widgets/tabs/tab_room.h"
|
||||
#include "../widgets/tabs/tab_server.h"
|
||||
#include "../widgets/tabs/tab_supervisor.h"
|
||||
#include "intent_connect_to_server.h"
|
||||
|
||||
#include <QTimer>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_room.pb.h>
|
||||
|
||||
IntentJoinServerRoom::IntentJoinServerRoom(TabSupervisor *_tabSupervisor,
|
||||
RemoteClient *_remoteClient,
|
||||
ContextJoinRoom *_context)
|
||||
: Intent(), tabSupervisor(_tabSupervisor), remoteClient(_remoteClient), context(_context)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentJoinServerRoom::checkPrecondition() const
|
||||
{
|
||||
if (remoteClient->getStatus() != ClientStatus::StatusLoggedIn) {
|
||||
return false;
|
||||
}
|
||||
// peerPort() reflects the actual TCP peer, which may differ from the
|
||||
// configured server port (e.g. when connecting through a proxy), so only
|
||||
// the hostname is compared here.
|
||||
if (remoteClient->peerName() != context->serverContext.hostname) {
|
||||
return false;
|
||||
}
|
||||
if (QString::number(remoteClient->peerPort()) != context->serverContext.port) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IntentJoinServerRoom::onPreconditionSatisfied()
|
||||
{
|
||||
if (tabSupervisor->getRoomTabs().contains(context->roomId)) {
|
||||
tabSupervisor->setCurrentWidget(tabSupervisor->getRoomTabs().value(context->roomId));
|
||||
emitFinished();
|
||||
return;
|
||||
}
|
||||
|
||||
TabServer *tabServer = tabSupervisor->getTabServer();
|
||||
if (!tabServer) {
|
||||
tabSupervisor->openTabServer();
|
||||
tabServer = tabSupervisor->getTabServer();
|
||||
}
|
||||
if (!tabServer) {
|
||||
emitFailed(tr("No server tab available"));
|
||||
return;
|
||||
}
|
||||
|
||||
const int roomId = context->roomId;
|
||||
tabServer->joinRoom(roomId, true);
|
||||
connect(tabServer, &TabServer::roomJoined, this, [this, roomId](const ServerInfo_Room &info, bool) {
|
||||
if (info.room_id() == roomId) {
|
||||
emitFinished();
|
||||
}
|
||||
});
|
||||
connect(tabServer, &TabServer::roomJoinFailed, this, [this, roomId](int failedRoomId) {
|
||||
if (failedRoomId == roomId) {
|
||||
emitFailed(tr("Failed to join the server room %1").arg(roomId));
|
||||
}
|
||||
});
|
||||
|
||||
QTimer::singleShot(15000, this,
|
||||
[this, roomId]() { emitFailed(tr("Timed out while joining the server room %1").arg(roomId)); });
|
||||
}
|
||||
|
||||
void IntentJoinServerRoom::onPreconditionNotSatisfied()
|
||||
{
|
||||
runDependency(new IntentConnectToServer(remoteClient, &context->serverContext));
|
||||
}
|
||||
28
cockatrice/src/interface/intents/intent_join_server_room.h
Normal file
28
cockatrice/src/interface/intents/intent_join_server_room.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef COCKATRICE_INTENT_JOIN_SERVER_ROOM_H
|
||||
#define COCKATRICE_INTENT_JOIN_SERVER_ROOM_H
|
||||
|
||||
#include "contexts/context_join_room.h"
|
||||
#include "intent.h"
|
||||
#include "remote_client.h"
|
||||
|
||||
class TabSupervisor;
|
||||
|
||||
class IntentJoinServerRoom : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentJoinServerRoom(TabSupervisor *_tabSupervisor, RemoteClient *_remoteClient, ContextJoinRoom *_context);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
TabSupervisor *tabSupervisor;
|
||||
RemoteClient *remoteClient;
|
||||
ContextJoinRoom *context;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_JOIN_SERVER_ROOM_H
|
||||
33
cockatrice/src/interface/intents/intent_login.cpp
Normal file
33
cockatrice/src/interface/intents/intent_login.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "intent_login.h"
|
||||
|
||||
#include "../../client/settings/cache_settings.h"
|
||||
#include "libcockatrice/settings/servers_settings.h"
|
||||
|
||||
IntentGetLoginCredentials::IntentGetLoginCredentials(ContextConnectToServer *_context) : Intent(), context(_context)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentGetLoginCredentials::checkPrecondition() const
|
||||
{
|
||||
ServersSettings &servers = SettingsCache::instance().servers();
|
||||
return servers.hasLoginData(context->hostname, context->port);
|
||||
}
|
||||
|
||||
void IntentGetLoginCredentials::onPreconditionSatisfied()
|
||||
{
|
||||
ServersSettings &servers = SettingsCache::instance().servers();
|
||||
const int index = servers.findServerIndex(context->hostname, context->port);
|
||||
|
||||
if (index >= 0) {
|
||||
context->username = servers.getValue(QString("username%1").arg(index), "server", "server_details").toString();
|
||||
context->password = servers.getValue(QString("password%1").arg(index), "server", "server_details").toString();
|
||||
emitFinished();
|
||||
} else {
|
||||
emitFailed(tr("No saved credentials for this server"));
|
||||
}
|
||||
}
|
||||
|
||||
void IntentGetLoginCredentials::onPreconditionNotSatisfied()
|
||||
{
|
||||
emitFailed(tr("No saved credentials for this server"));
|
||||
}
|
||||
23
cockatrice/src/interface/intents/intent_login.h
Normal file
23
cockatrice/src/interface/intents/intent_login.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef COCKATRICE_INTENT_LOGIN_H
|
||||
#define COCKATRICE_INTENT_LOGIN_H
|
||||
|
||||
#include "contexts/context_connect_to_server.h"
|
||||
#include "intent.h"
|
||||
|
||||
class IntentGetLoginCredentials : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentGetLoginCredentials(ContextConnectToServer *_context);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
ContextConnectToServer *context;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_LOGIN_H
|
||||
34
cockatrice/src/interface/intents/intent_open_local_deck.cpp
Normal file
34
cockatrice/src/interface/intents/intent_open_local_deck.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include "intent_open_local_deck.h"
|
||||
|
||||
#include "../deck_loader/deck_file_format.h"
|
||||
#include "../deck_loader/deck_loader.h"
|
||||
#include "../widgets/tabs/tab_supervisor.h"
|
||||
#include "intent_wait_for_database_load.h"
|
||||
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
|
||||
IntentOpenLocalDeck::IntentOpenLocalDeck(TabSupervisor *_tabSupervisor, const QString &_file)
|
||||
: Intent(), tabSupervisor(_tabSupervisor), file(_file)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntentOpenLocalDeck::checkPrecondition() const
|
||||
{
|
||||
return CardDatabaseManager::getInstance()->getLoadStatus() == LoadStatus::Ok;
|
||||
}
|
||||
|
||||
void IntentOpenLocalDeck::onPreconditionSatisfied()
|
||||
{
|
||||
std::optional<LoadedDeck> deckOpt = DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), true);
|
||||
if (deckOpt) {
|
||||
tabSupervisor->openDeckInNewTab(deckOpt.value());
|
||||
emitFinished();
|
||||
} else {
|
||||
emitFailed(tr("Unable to load deck file %1").arg(file));
|
||||
}
|
||||
}
|
||||
|
||||
void IntentOpenLocalDeck::onPreconditionNotSatisfied()
|
||||
{
|
||||
runDependency(new IntentWaitForDatabaseLoad);
|
||||
}
|
||||
27
cockatrice/src/interface/intents/intent_open_local_deck.h
Normal file
27
cockatrice/src/interface/intents/intent_open_local_deck.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef COCKATRICE_INTENT_OPEN_LOCAL_DECK_H
|
||||
#define COCKATRICE_INTENT_OPEN_LOCAL_DECK_H
|
||||
|
||||
#include "intent.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
class TabSupervisor;
|
||||
|
||||
class IntentOpenLocalDeck : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentOpenLocalDeck(TabSupervisor *_tabSupervisor, const QString &_file);
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
|
||||
private:
|
||||
TabSupervisor *tabSupervisor;
|
||||
QString file;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_OPEN_LOCAL_DECK_H
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include "intent_wait_for_database_load.h"
|
||||
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
|
||||
bool IntentWaitForDatabaseLoad::checkPrecondition() const
|
||||
{
|
||||
return CardDatabaseManager::getInstance()->getLoadStatus() == LoadStatus::Ok;
|
||||
}
|
||||
|
||||
void IntentWaitForDatabaseLoad::onPreconditionSatisfied()
|
||||
{
|
||||
emitFinished();
|
||||
}
|
||||
|
||||
void IntentWaitForDatabaseLoad::onPreconditionNotSatisfied()
|
||||
{
|
||||
connect(CardDatabaseManager::getInstance(), &CardDatabase::cardDatabaseLoadingFinished, this,
|
||||
[this]() { emitFinished(); });
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H
|
||||
#define COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H
|
||||
|
||||
#include "intent.h"
|
||||
|
||||
class IntentWaitForDatabaseLoad : public Intent
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
bool checkPrecondition() const override;
|
||||
void onPreconditionSatisfied() override;
|
||||
void onPreconditionNotSatisfied() override;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H
|
||||
89
cockatrice/src/interface/intents/url_parser.cpp
Normal file
89
cockatrice/src/interface/intents/url_parser.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#include "url_parser.h"
|
||||
|
||||
#include "../window_main.h"
|
||||
#include "contexts/context_join_game.h"
|
||||
#include "intent_join_server_game.h"
|
||||
#include "intent_login.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <memory>
|
||||
|
||||
IntentUrlParser::IntentUrlParser(QObject *parent, MainWindow *_mainWindow) : QObject(parent), mainWindow(_mainWindow)
|
||||
{
|
||||
}
|
||||
|
||||
void IntentUrlParser::handle(const QString &urlStr)
|
||||
{
|
||||
QUrl url(urlStr);
|
||||
|
||||
if (url.scheme() != "cockatrice") {
|
||||
return;
|
||||
}
|
||||
|
||||
const QString action = url.host();
|
||||
QUrlQuery query(url);
|
||||
|
||||
if (action == "joingame") {
|
||||
handleJoinGame(query);
|
||||
} else if (action == "opendeck") {
|
||||
// handleOpenDeck(query);
|
||||
} else {
|
||||
qWarning() << "Unknown intent:" << action;
|
||||
}
|
||||
}
|
||||
|
||||
void IntentUrlParser::handleJoinGame(const QUrlQuery &query)
|
||||
{
|
||||
auto showError = [this](const QString &message) { QMessageBox::warning(mainWindow, tr("Open game"), message); };
|
||||
|
||||
auto ctx = std::make_unique<ContextJoinGame>();
|
||||
|
||||
ctx->roomContext.serverContext.hostname = query.queryItemValue("hostname");
|
||||
ctx->roomContext.serverContext.port = query.queryItemValue("port");
|
||||
|
||||
if (ctx->roomContext.serverContext.hostname.isEmpty()) {
|
||||
showError(tr("Missing or empty hostname in the game link"));
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
ctx->roomContext.serverContext.port.toUShort(&ok);
|
||||
if (!ok) {
|
||||
showError(tr("Invalid or missing port in the game link"));
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->roomContext.roomId = query.queryItemValue("roomid").toInt(&ok);
|
||||
|
||||
if (!ok) {
|
||||
showError(tr("Invalid or missing room id in the game link"));
|
||||
return;
|
||||
}
|
||||
|
||||
ok = false;
|
||||
ctx->gameId = query.queryItemValue("gameid").toInt(&ok);
|
||||
|
||||
if (!ok) {
|
||||
showError(tr("Invalid or missing game id in the game link"));
|
||||
return;
|
||||
}
|
||||
|
||||
// The join game intent owns the context and the credential lookup; once the
|
||||
// chain finishes (or fails) it deletes the whole tree.
|
||||
ContextConnectToServer *serverContext = &ctx->roomContext.serverContext;
|
||||
auto joinGameIntent =
|
||||
new IntentJoinServerGame(mainWindow->getTabSupervisor(), mainWindow->getRemoteClient(), std::move(ctx));
|
||||
joinGameIntent->setParent(this);
|
||||
|
||||
auto getLoginCredentialsIntent = new IntentGetLoginCredentials(serverContext);
|
||||
getLoginCredentialsIntent->setParent(joinGameIntent);
|
||||
|
||||
connect(getLoginCredentialsIntent, &Intent::finished, joinGameIntent, &Intent::execute);
|
||||
connect(getLoginCredentialsIntent, &Intent::failed, joinGameIntent, &Intent::failed);
|
||||
connect(joinGameIntent, &Intent::failed, this, [showError](const QString &reason) { showError(reason); });
|
||||
|
||||
getLoginCredentialsIntent->execute();
|
||||
}
|
||||
20
cockatrice/src/interface/intents/url_parser.h
Normal file
20
cockatrice/src/interface/intents/url_parser.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef COCKATRICE_URL_PARSER_H
|
||||
#define COCKATRICE_URL_PARSER_H
|
||||
#include <QObject>
|
||||
#include <QUrlQuery>
|
||||
|
||||
class MainWindow;
|
||||
class IntentUrlParser : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IntentUrlParser(QObject *parent, MainWindow *mainWindow);
|
||||
void handle(const QString &urlStr);
|
||||
void handleJoinGame(const QUrlQuery &query);
|
||||
|
||||
private:
|
||||
MainWindow *mainWindow;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_URL_PARSER_H
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include "../../../client/settings/cache_settings.h"
|
||||
#include "../../../client/settings/shortcuts_settings.h"
|
||||
#include "../settings_page/user_interface_settings_page.h"
|
||||
#include "../tabs/api/commander_spellbook/commander_bracket_widget.h"
|
||||
#include "deck_list_style_proxy.h"
|
||||
#include "deck_state_manager.h"
|
||||
|
||||
|
|
@ -12,7 +14,7 @@
|
|||
#include <QSplitter>
|
||||
#include <QTextEdit>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <libcockatrice/settings/deck_editor_settings.h>
|
||||
#include <libcockatrice/utility/macros.h>
|
||||
#include <libcockatrice/utility/string_limits.h>
|
||||
|
||||
|
|
@ -111,20 +113,18 @@ void DeckEditorDeckDockWidget::createDeckDock()
|
|||
|
||||
showBannerCardCheckBox = new QCheckBox();
|
||||
showBannerCardCheckBox->setObjectName("showBannerCardCheckBox");
|
||||
showBannerCardCheckBox->setChecked(
|
||||
SettingsCache::instance().cardsDisplay().getDeckEditorBannerCardComboBoxVisible());
|
||||
connect(showBannerCardCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setDeckEditorBannerCardComboBoxVisible);
|
||||
connect(&SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::deckEditorBannerCardComboBoxVisibleChanged, this,
|
||||
showBannerCardCheckBox->setChecked(SettingsCache::instance().deckEditor().getBannerCardComboBoxVisible());
|
||||
connect(showBannerCardCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().deckEditor(),
|
||||
&DeckEditorSettings::setBannerCardComboBoxVisible);
|
||||
connect(&SettingsCache::instance().deckEditor(), &DeckEditorSettings::bannerCardComboBoxVisibleChanged, this,
|
||||
&DeckEditorDeckDockWidget::updateShowBannerCardComboBox);
|
||||
|
||||
showTagsWidgetCheckBox = new QCheckBox();
|
||||
showTagsWidgetCheckBox->setObjectName("showTagsWidgetCheckBox");
|
||||
showTagsWidgetCheckBox->setChecked(SettingsCache::instance().cardsDisplay().getDeckEditorTagsWidgetVisible());
|
||||
connect(showTagsWidgetCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setDeckEditorTagsWidgetVisible);
|
||||
connect(&SettingsCache::instance().cardsDisplay(), &CardsDisplaySettings::deckEditorTagsWidgetVisibleChanged, this,
|
||||
showTagsWidgetCheckBox->setChecked(SettingsCache::instance().deckEditor().getTagsWidgetVisible());
|
||||
connect(showTagsWidgetCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().deckEditor(),
|
||||
&DeckEditorSettings::setTagsWidgetVisible);
|
||||
connect(&SettingsCache::instance().deckEditor(), &DeckEditorSettings::tagsWidgetVisibleChanged, this,
|
||||
&DeckEditorDeckDockWidget::updateShowTagsWidget);
|
||||
|
||||
quickSettingsWidget->addSettingsWidget(showBannerCardCheckBox);
|
||||
|
|
@ -136,6 +136,8 @@ void DeckEditorDeckDockWidget::createDeckDock()
|
|||
formatComboBox->addItem(tr("Loading Database..."));
|
||||
formatComboBox->setEnabled(false); // Disable until loaded
|
||||
|
||||
commanderBracketWidget = new CommanderBracketWidget(this);
|
||||
|
||||
commentsLabel = new QLabel();
|
||||
commentsLabel->setObjectName("commentsLabel");
|
||||
commentsEdit = new QTextEdit;
|
||||
|
|
@ -156,7 +158,7 @@ void DeckEditorDeckDockWidget::createDeckDock()
|
|||
bannerCardLabel = new QLabel();
|
||||
bannerCardLabel->setObjectName("bannerCardLabel");
|
||||
bannerCardLabel->setText(tr("Banner Card"));
|
||||
bannerCardLabel->setHidden(!SettingsCache::instance().cardsDisplay().getDeckEditorBannerCardComboBoxVisible());
|
||||
bannerCardLabel->setHidden(!SettingsCache::instance().deckEditor().getBannerCardComboBoxVisible());
|
||||
bannerCardComboBox = new QComboBox(this);
|
||||
connect(getModel(), &DeckListModel::cardNodesChanged, this, [this]() {
|
||||
// Delay the update to avoid race conditions
|
||||
|
|
@ -167,10 +169,10 @@ void DeckEditorDeckDockWidget::createDeckDock()
|
|||
|
||||
connect(bannerCardComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
||||
&DeckEditorDeckDockWidget::writeBannerCard);
|
||||
bannerCardComboBox->setHidden(!SettingsCache::instance().cardsDisplay().getDeckEditorBannerCardComboBoxVisible());
|
||||
bannerCardComboBox->setHidden(!SettingsCache::instance().deckEditor().getBannerCardComboBoxVisible());
|
||||
|
||||
deckTagsDisplayWidget = new DeckPreviewDeckTagsDisplayWidget(this, {});
|
||||
deckTagsDisplayWidget->setHidden(!SettingsCache::instance().cardsDisplay().getDeckEditorTagsWidgetVisible());
|
||||
deckTagsDisplayWidget->setHidden(!SettingsCache::instance().deckEditor().getTagsWidgetVisible());
|
||||
connect(deckTagsDisplayWidget, &DeckPreviewDeckTagsDisplayWidget::tagsChanged, deckStateManager,
|
||||
&DeckStateManager::setTags);
|
||||
|
||||
|
|
@ -221,13 +223,15 @@ void DeckEditorDeckDockWidget::createDeckDock()
|
|||
upperLayout->addWidget(formatLabel, 2, 0);
|
||||
upperLayout->addWidget(formatComboBox, 2, 1);
|
||||
|
||||
upperLayout->addWidget(bannerCardLabel, 3, 0);
|
||||
upperLayout->addWidget(bannerCardComboBox, 3, 1);
|
||||
upperLayout->addWidget(commanderBracketWidget, 3, 0, 1, 2);
|
||||
|
||||
upperLayout->addWidget(deckTagsDisplayWidget, 4, 1);
|
||||
upperLayout->addWidget(bannerCardLabel, 4, 0);
|
||||
upperLayout->addWidget(bannerCardComboBox, 4, 1);
|
||||
|
||||
upperLayout->addWidget(activeGroupCriteriaLabel, 5, 0);
|
||||
upperLayout->addWidget(activeGroupCriteriaComboBox, 5, 1);
|
||||
upperLayout->addWidget(deckTagsDisplayWidget, 5, 1);
|
||||
|
||||
upperLayout->addWidget(activeGroupCriteriaLabel, 6, 0);
|
||||
upperLayout->addWidget(activeGroupCriteriaComboBox, 6, 1);
|
||||
|
||||
hashLabel1 = new QLabel();
|
||||
hashLabel1->setObjectName("hashLabel1");
|
||||
|
|
@ -305,15 +309,19 @@ void DeckEditorDeckDockWidget::initializeFormats()
|
|||
// Ensure no selection is visible initially
|
||||
formatComboBox->setCurrentIndex(-1);
|
||||
}
|
||||
|
||||
connect(formatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
|
||||
QString formatKey;
|
||||
if (index >= 0) {
|
||||
QString formatKey = formatComboBox->itemData(index).toString();
|
||||
formatKey = formatComboBox->itemData(index).toString();
|
||||
deckStateManager->setFormat(formatKey);
|
||||
} else {
|
||||
deckStateManager->setFormat(""); // clear format if deselected
|
||||
}
|
||||
|
||||
commanderBracketWidget->setDeck(deckStateManager->getDeckListShared());
|
||||
});
|
||||
|
||||
commanderBracketWidget->setDeck(deckStateManager->getDeckListShared());
|
||||
}
|
||||
|
||||
ExactCard DeckEditorDeckDockWidget::getCurrentCard()
|
||||
|
|
@ -495,6 +503,8 @@ void DeckEditorDeckDockWidget::syncDisplayWidgetsToModel()
|
|||
formatComboBox->setCurrentIndex(formatComboBox->findData(deckStateManager->getMetadata().gameFormat));
|
||||
formatComboBox->blockSignals(false);
|
||||
|
||||
commanderBracketWidget->setDeck(deckStateManager->getDeckListShared());
|
||||
|
||||
deckTagsDisplayWidget->blockSignals(true);
|
||||
deckTagsDisplayWidget->setTags(deckStateManager->getMetadata().tags);
|
||||
deckTagsDisplayWidget->blockSignals(false);
|
||||
|
|
@ -748,6 +758,7 @@ void DeckEditorDeckDockWidget::retranslateUi()
|
|||
commentsLabel->setText(tr("&Comments:"));
|
||||
activeGroupCriteriaLabel->setText(tr("Group by:"));
|
||||
formatLabel->setText(tr("Format:"));
|
||||
commanderBracketWidget->retranslateUi();
|
||||
|
||||
hashLabel1->setText(tr("Hash:"));
|
||||
|
||||
|
|
@ -755,4 +766,4 @@ void DeckEditorDeckDockWidget::retranslateUi()
|
|||
aDecrement->setText(tr("&Decrement number"));
|
||||
aRemoveCard->setText(tr("&Remove row"));
|
||||
aSwapCard->setText(tr("Swap card to/from sideboard"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
#include <QTreeView>
|
||||
#include <libcockatrice/card/card_info.h>
|
||||
|
||||
class CommanderBracketWidget;
|
||||
class DeckListModel;
|
||||
class AbstractTabDeckEditor;
|
||||
class DeckEditorDeckDockWidget : public QDockWidget
|
||||
|
|
@ -89,6 +90,8 @@ private:
|
|||
|
||||
QAction *aRemoveCard, *aIncrement, *aDecrement, *aSwapCard;
|
||||
|
||||
CommanderBracketWidget *commanderBracketWidget;
|
||||
|
||||
DeckListModel *getModel() const;
|
||||
[[nodiscard]] QModelIndexList getSelectedCardNodeSourceIndices() const;
|
||||
void offsetCountAtIndex(const QModelIndex &idx, bool isIncrement);
|
||||
|
|
|
|||
|
|
@ -57,6 +57,14 @@ public:
|
|||
*/
|
||||
const DeckList &getDeckList() const;
|
||||
|
||||
/**
|
||||
* @brief Gets the underlying DeckList.
|
||||
*/
|
||||
QSharedPointer<DeckList> getDeckListShared() const
|
||||
{
|
||||
return deckList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a LoadedDeck containing the contents of the current deck and the current LoadInfo.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -271,6 +271,7 @@ void DlgConnect::updateDisplayInfo(const QString &saveName)
|
|||
hostEdit->setText(_data.at(1));
|
||||
portEdit->setText(_data.at(2));
|
||||
playernameEdit->setText(_data.at(3));
|
||||
playernameEdit->setFocus();
|
||||
savePasswordCheckBox->setChecked(savePasswordStatus);
|
||||
|
||||
if (savePasswordStatus) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
#include "../../../client/settings/shortcuts_settings.h"
|
||||
#include "../../deck_loader/card_node_function.h"
|
||||
#include "../../deck_loader/deck_loader.h"
|
||||
#include "dlg_settings.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCheckBox>
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
/**
|
||||
* @file dlg_settings.cpp
|
||||
* @brief Implementation of the main settings dialog
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
#include "dlg_settings.h"
|
||||
|
||||
#include "../../../client/settings/cache_settings.h"
|
||||
|
|
@ -6,6 +11,8 @@
|
|||
#include "../settings_page/deck_editor_settings_page.h"
|
||||
#include "../settings_page/general_settings_page.h"
|
||||
#include "../settings_page/messages_settings_page.h"
|
||||
#include "../settings_page/settings_search_delegate.h"
|
||||
#include "../settings_page/settings_search_model.h"
|
||||
#include "../settings_page/shortcut_settings_page.h"
|
||||
#include "../settings_page/sound_settings_page.h"
|
||||
#include "../settings_page/storage_settings_page.h"
|
||||
|
|
@ -13,20 +20,36 @@
|
|||
#include "libcockatrice/card/database/card_database_loader.h"
|
||||
#include "libcockatrice/card/database/card_database_manager.h"
|
||||
|
||||
#include <QAbstractItemView>
|
||||
#include <QCloseEvent>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QDir>
|
||||
#include <QFrame>
|
||||
#include <QGraphicsOpacityEffect>
|
||||
#include <QGuiApplication>
|
||||
#include <QListWidget>
|
||||
#include <QHBoxLayout>
|
||||
#include <QKeyEvent>
|
||||
#include <QLineEdit>
|
||||
#include <QListView>
|
||||
#include <QMessageBox>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QPushButton>
|
||||
#include <QScreen>
|
||||
#include <QScrollArea>
|
||||
#include <QScrollBar>
|
||||
#include <QSequentialAnimationGroup>
|
||||
#include <QShortcut>
|
||||
#include <QStackedLayout>
|
||||
#include <QStackedWidget>
|
||||
#include <QToolButton>
|
||||
#include <QVBoxLayout>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
|
||||
/**
|
||||
* @brief Wraps a widget in a scroll area for long settings pages
|
||||
* @param widget The widget to wrap
|
||||
* @return The scroll area containing the widget
|
||||
*/
|
||||
static QScrollArea *makeScrollable(QWidget *widget)
|
||||
{
|
||||
widget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
|
||||
|
|
@ -40,112 +63,355 @@ static QScrollArea *makeScrollable(QWidget *widget)
|
|||
return scrollArea;
|
||||
}
|
||||
|
||||
DlgSettings::DlgSettings(QWidget *parent) : QDialog(parent)
|
||||
/**
|
||||
* @brief Returns the theme icon resources for each settings page, indexed by SettingsPage order
|
||||
*/
|
||||
static QStringList pageIconResources()
|
||||
{
|
||||
return {QStringLiteral("theme:config/general"), QStringLiteral("theme:config/appearance"),
|
||||
QStringLiteral("theme:config/interface"), QStringLiteral("theme:config/deckeditor"),
|
||||
QStringLiteral("theme:config/storage"), QStringLiteral("theme:config/messages"),
|
||||
QStringLiteral("theme:config/sound"), QStringLiteral("theme:config/shorcuts")};
|
||||
}
|
||||
|
||||
DlgSettings::DlgSettings(QWidget *parent) : QDialog(parent), currentTabIndex(0), searchActive(false)
|
||||
{
|
||||
auto rec = QGuiApplication::primaryScreen()->availableGeometry();
|
||||
this->setMinimumSize(qMin(700, rec.width()), qMin(700, rec.height()));
|
||||
setMinimumSize(qMin(750, rec.width()), qMin(700, rec.height()));
|
||||
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::langChanged, this, &DlgSettings::updateLanguage);
|
||||
|
||||
contentsWidget = new QListWidget;
|
||||
contentsWidget->setViewMode(QListView::IconMode);
|
||||
contentsWidget->setIconSize(QSize(58, 50));
|
||||
contentsWidget->setMovement(QListView::Static);
|
||||
contentsWidget->setMinimumHeight(85);
|
||||
contentsWidget->setMaximumHeight(85);
|
||||
contentsWidget->setSpacing(5);
|
||||
|
||||
pagesWidget = new QStackedWidget;
|
||||
pagesWidget->addWidget(makeScrollable(new GeneralSettingsPage));
|
||||
pagesWidget->addWidget(makeScrollable(new AppearanceSettingsPage));
|
||||
pagesWidget->addWidget(makeScrollable(new UserInterfaceSettingsPage));
|
||||
pagesWidget->addWidget(new DeckEditorSettingsPage);
|
||||
pagesWidget->addWidget(makeScrollable(new StorageSettingsPage));
|
||||
pagesWidget->addWidget(new MessagesSettingsPage);
|
||||
pagesWidget->addWidget(new SoundSettingsPage);
|
||||
pagesWidget->addWidget(new ShortcutSettingsPage);
|
||||
|
||||
createIcons();
|
||||
contentsWidget->setCurrentRow(0);
|
||||
|
||||
auto *vboxLayout = new QVBoxLayout;
|
||||
vboxLayout->addWidget(contentsWidget);
|
||||
vboxLayout->addWidget(pagesWidget);
|
||||
|
||||
auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
||||
connect(buttonBox, &QDialogButtonBox::accepted, this, &DlgSettings::close);
|
||||
|
||||
auto *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(vboxLayout);
|
||||
mainLayout->addSpacing(2);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
setLayout(mainLayout);
|
||||
setupUi();
|
||||
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::langChanged, this, &DlgSettings::retranslateUi);
|
||||
retranslateUi();
|
||||
|
||||
searchEdit->setFocus();
|
||||
|
||||
adjustSize();
|
||||
}
|
||||
|
||||
void DlgSettings::createIcons()
|
||||
void DlgSettings::setupUi()
|
||||
{
|
||||
generalButton = new QListWidgetItem(contentsWidget);
|
||||
generalButton->setTextAlignment(Qt::AlignHCenter);
|
||||
generalButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
generalButton->setIcon(QPixmap("theme:config/general"));
|
||||
// Search bar
|
||||
searchEdit = new QLineEdit;
|
||||
searchEdit->setClearButtonEnabled(true);
|
||||
searchEdit->addAction(QPixmap("theme:icons/search"), QLineEdit::LeadingPosition);
|
||||
searchEdit->installEventFilter(this);
|
||||
connect(searchEdit, &QLineEdit::textChanged, this, &DlgSettings::onSearchTextChanged);
|
||||
|
||||
appearanceButton = new QListWidgetItem(contentsWidget);
|
||||
appearanceButton->setTextAlignment(Qt::AlignHCenter);
|
||||
appearanceButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
appearanceButton->setIcon(QPixmap("theme:config/appearance"));
|
||||
auto *searchLayout = new QHBoxLayout;
|
||||
searchLayout->addWidget(searchEdit);
|
||||
|
||||
userInterfaceButton = new QListWidgetItem(contentsWidget);
|
||||
userInterfaceButton->setTextAlignment(Qt::AlignHCenter);
|
||||
userInterfaceButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
userInterfaceButton->setIcon(QPixmap("theme:config/interface"));
|
||||
// Tab bar (built in setupTabBar)
|
||||
setupTabBar();
|
||||
|
||||
deckEditorButton = new QListWidgetItem(contentsWidget);
|
||||
deckEditorButton->setTextAlignment(Qt::AlignHCenter);
|
||||
deckEditorButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
deckEditorButton->setIcon(QPixmap("theme:config/deckeditor"));
|
||||
// Pages stacked widget
|
||||
pagesWidget = new QStackedWidget;
|
||||
|
||||
storageButton = new QListWidgetItem(contentsWidget);
|
||||
storageButton->setTextAlignment(Qt::AlignHCenter);
|
||||
storageButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
storageButton->setIcon(QPixmap("theme:config/storage"));
|
||||
auto *generalPage = new GeneralSettingsPage;
|
||||
auto *appearancePage = new AppearanceSettingsPage;
|
||||
auto *userInterfacePage = new UserInterfaceSettingsPage;
|
||||
auto *deckEditorPage = new DeckEditorSettingsPage;
|
||||
auto *storagePage = new StorageSettingsPage;
|
||||
auto *messagesPage = new MessagesSettingsPage;
|
||||
auto *soundPage = new SoundSettingsPage;
|
||||
auto *shortcutsPage = new ShortcutSettingsPage;
|
||||
|
||||
messagesButton = new QListWidgetItem(contentsWidget);
|
||||
messagesButton->setTextAlignment(Qt::AlignHCenter);
|
||||
messagesButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
messagesButton->setIcon(QPixmap("theme:config/messages"));
|
||||
pages.append(generalPage);
|
||||
pages.append(appearancePage);
|
||||
pages.append(userInterfacePage);
|
||||
pages.append(deckEditorPage);
|
||||
pages.append(storagePage);
|
||||
pages.append(messagesPage);
|
||||
pages.append(soundPage);
|
||||
pages.append(shortcutsPage);
|
||||
|
||||
soundButton = new QListWidgetItem(contentsWidget);
|
||||
soundButton->setTextAlignment(Qt::AlignHCenter);
|
||||
soundButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
soundButton->setIcon(QPixmap("theme:config/sound"));
|
||||
pagesWidget->addWidget(makeScrollable(generalPage));
|
||||
pagesWidget->addWidget(makeScrollable(appearancePage));
|
||||
pagesWidget->addWidget(makeScrollable(userInterfacePage));
|
||||
pagesWidget->addWidget(makeScrollable(deckEditorPage));
|
||||
pagesWidget->addWidget(makeScrollable(storagePage));
|
||||
pagesWidget->addWidget(messagesPage);
|
||||
pagesWidget->addWidget(soundPage);
|
||||
pagesWidget->addWidget(shortcutsPage);
|
||||
|
||||
shortcutsButton = new QListWidgetItem(contentsWidget);
|
||||
shortcutsButton->setTextAlignment(Qt::AlignHCenter);
|
||||
shortcutsButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
shortcutsButton->setIcon(QPixmap("theme:config/shorcuts"));
|
||||
Q_ASSERT(pages.size() == NumPages);
|
||||
|
||||
connect(contentsWidget, &QListWidget::currentItemChanged, this, &DlgSettings::changePage);
|
||||
// Search results view (hidden by default)
|
||||
searchResultsView = new QListView;
|
||||
searchResultsView->setUniformItemSizes(false);
|
||||
searchResultsView->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
searchResultsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
searchResultsView->setVisible(false);
|
||||
searchResultsView->setStyleSheet(
|
||||
"QListView::item:selected { background: palette(highlight); color: palette(highlighted-text); }");
|
||||
|
||||
searchModel = new SettingsSearchModel(this);
|
||||
searchDelegate = new SettingsSearchDelegate(this);
|
||||
searchResultsView->setModel(searchModel);
|
||||
searchResultsView->setItemDelegate(searchDelegate);
|
||||
connect(searchResultsView, &QListView::clicked, this, &DlgSettings::onSearchResultClicked);
|
||||
|
||||
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this, [this] {
|
||||
const QStringList icons = pageIconResources();
|
||||
for (int i = 0; i < tabButtons.size() && i < icons.size(); ++i) {
|
||||
tabButtons[i]->setIcon(QPixmap(icons[i]));
|
||||
}
|
||||
searchDelegate->setPageIcons(icons);
|
||||
searchResultsView->viewport()->update();
|
||||
});
|
||||
|
||||
// Build search index after pages are created
|
||||
buildSearchIndex();
|
||||
|
||||
// Pages container (stacked widget + search results overlay)
|
||||
pagesContainer = new QWidget;
|
||||
auto *containerLayout = new QStackedLayout;
|
||||
containerLayout->setStackingMode(QStackedLayout::StackAll);
|
||||
containerLayout->addWidget(pagesWidget);
|
||||
containerLayout->addWidget(searchResultsView);
|
||||
pagesContainer->setLayout(containerLayout);
|
||||
|
||||
// Bottom buttons
|
||||
auto *buttonBox = new QHBoxLayout;
|
||||
buttonBox->addStretch();
|
||||
okButton = new QPushButton;
|
||||
okButton->setDefault(true);
|
||||
connect(okButton, &QPushButton::clicked, this, &DlgSettings::close);
|
||||
buttonBox->addWidget(okButton);
|
||||
|
||||
// Main layout
|
||||
auto *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(searchLayout);
|
||||
mainLayout->addWidget(tabBarWidget);
|
||||
auto *separator = new QFrame;
|
||||
separator->setFrameShape(QFrame::HLine);
|
||||
separator->setFrameShadow(QFrame::Sunken);
|
||||
mainLayout->addWidget(separator);
|
||||
mainLayout->addWidget(pagesContainer);
|
||||
mainLayout->addSpacing(4);
|
||||
mainLayout->addLayout(buttonBox);
|
||||
setLayout(mainLayout);
|
||||
|
||||
// Keyboard shortcuts
|
||||
auto *searchShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F), this);
|
||||
connect(searchShortcut, &QShortcut::activated, searchEdit, qOverload<>(&QLineEdit::setFocus));
|
||||
|
||||
auto *nextTabShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Tab), this);
|
||||
connect(nextTabShortcut, &QShortcut::activated, this, [this] {
|
||||
int next = (currentTabIndex + 1) % tabButtons.size();
|
||||
setActiveTab(next);
|
||||
});
|
||||
|
||||
auto *prevTabShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab), this);
|
||||
connect(prevTabShortcut, &QShortcut::activated, this, [this] {
|
||||
int prev = (currentTabIndex - 1 + tabButtons.size()) % tabButtons.size();
|
||||
setActiveTab(prev);
|
||||
});
|
||||
|
||||
// Initialize to first tab
|
||||
setActiveTab(0);
|
||||
}
|
||||
|
||||
void DlgSettings::changePage(QListWidgetItem *current, QListWidgetItem *previous)
|
||||
void DlgSettings::setupTabBar()
|
||||
{
|
||||
if (!current) {
|
||||
current = previous;
|
||||
tabBarWidget = new QWidget;
|
||||
auto *tabLayout = new QHBoxLayout;
|
||||
tabLayout->setContentsMargins(0, 0, 0, 0);
|
||||
tabLayout->setSpacing(2);
|
||||
|
||||
const QStringList iconResources = pageIconResources();
|
||||
|
||||
for (int i = 0; i < iconResources.size(); ++i) {
|
||||
auto *tabButton = new QToolButton;
|
||||
tabButton->setCheckable(true);
|
||||
tabButton->setIcon(QPixmap(iconResources[i]));
|
||||
tabButton->setIconSize(QSize(48, 48));
|
||||
tabButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
||||
tabButton->setAutoExclusive(true);
|
||||
tabButton->setMinimumHeight(85);
|
||||
tabButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
|
||||
connect(tabButton, &QToolButton::clicked, this, [this, idx = i] { onTabClicked(idx); });
|
||||
|
||||
tabButtons.append(tabButton);
|
||||
tabLayout->addWidget(tabButton);
|
||||
}
|
||||
|
||||
pagesWidget->setCurrentIndex(contentsWidget->row(current));
|
||||
tabBarWidget->setLayout(tabLayout);
|
||||
}
|
||||
|
||||
void DlgSettings::buildSearchIndex()
|
||||
{
|
||||
QList<SettingsSearchEntry> allEntries;
|
||||
|
||||
const QStringList pageNames = translatedPageNames();
|
||||
searchDelegate->setPageNames(pageNames);
|
||||
searchDelegate->setPageIcons(pageIconResources());
|
||||
|
||||
for (int i = 0; i < pages.size(); ++i) {
|
||||
QList<SettingsSearchEntry> pageEntries = pages[i]->getSearchEntries();
|
||||
for (auto &entry : pageEntries) {
|
||||
if (entry.pageIndex == -1) {
|
||||
entry.pageIndex = i;
|
||||
}
|
||||
}
|
||||
allEntries.append(pageEntries);
|
||||
}
|
||||
|
||||
searchModel->setSourceEntries(allEntries);
|
||||
}
|
||||
|
||||
void DlgSettings::onTabClicked(int index)
|
||||
{
|
||||
if (searchActive) {
|
||||
switchToTabMode();
|
||||
}
|
||||
setActiveTab(index);
|
||||
}
|
||||
|
||||
void DlgSettings::setActiveTab(int index)
|
||||
{
|
||||
if (index < 0 || index >= tabButtons.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentTabIndex = index;
|
||||
pagesWidget->setCurrentIndex(index);
|
||||
|
||||
for (int i = 0; i < tabButtons.size(); ++i) {
|
||||
tabButtons[i]->setChecked(i == index);
|
||||
}
|
||||
|
||||
// Style active tab with a thick accent border + subtle background tint
|
||||
for (int i = 0; i < tabButtons.size(); ++i) {
|
||||
if (i == index) {
|
||||
tabButtons[i]->setStyleSheet("QToolButton { border: none; border-bottom: 3px solid palette(highlight); "
|
||||
"border-top-left-radius: 4px; border-top-right-radius: 4px; "
|
||||
"background: palette(window); padding-bottom: 1px; }");
|
||||
} else {
|
||||
tabButtons[i]->setStyleSheet("QToolButton { border: none; border-bottom: 1px solid transparent; "
|
||||
"border-top-left-radius: 4px; border-top-right-radius: 4px; "
|
||||
"background: transparent; }");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DlgSettings::flashWidget(QWidget *widget)
|
||||
{
|
||||
auto *overlay = new QWidget(widget);
|
||||
overlay->setGeometry(widget->rect());
|
||||
overlay->setAttribute(Qt::WA_TransparentForMouseEvents, true);
|
||||
|
||||
QPalette pal = overlay->palette();
|
||||
QColor flashColor = pal.color(QPalette::Highlight);
|
||||
flashColor.setAlpha(100);
|
||||
pal.setBrush(QPalette::Window, flashColor);
|
||||
overlay->setPalette(pal);
|
||||
overlay->setAutoFillBackground(true);
|
||||
|
||||
auto *effect = new QGraphicsOpacityEffect(overlay);
|
||||
effect->setOpacity(0.0);
|
||||
overlay->setGraphicsEffect(effect);
|
||||
overlay->show();
|
||||
overlay->raise();
|
||||
|
||||
auto *flashIn = new QPropertyAnimation(effect, "opacity");
|
||||
flashIn->setDuration(120);
|
||||
flashIn->setStartValue(0.0);
|
||||
flashIn->setEndValue(0.6);
|
||||
flashIn->setEasingCurve(QEasingCurve::OutCubic);
|
||||
|
||||
auto *fadeOut = new QPropertyAnimation(effect, "opacity");
|
||||
fadeOut->setDuration(900);
|
||||
fadeOut->setStartValue(0.6);
|
||||
fadeOut->setEndValue(0.0);
|
||||
fadeOut->setEasingCurve(QEasingCurve::InCubic);
|
||||
|
||||
auto *group = new QSequentialAnimationGroup(overlay);
|
||||
group->addAnimation(flashIn);
|
||||
group->addAnimation(fadeOut);
|
||||
|
||||
connect(group, &QSequentialAnimationGroup::finished, overlay, &QWidget::deleteLater);
|
||||
|
||||
group->start(QAbstractAnimation::DeleteWhenStopped);
|
||||
}
|
||||
|
||||
void DlgSettings::onSearchTextChanged(const QString &text)
|
||||
{
|
||||
searchModel->setFilterString(text);
|
||||
|
||||
if (searchModel->isFilterActive() && !text.trimmed().isEmpty()) {
|
||||
if (!searchActive) {
|
||||
switchToSearchMode();
|
||||
}
|
||||
if (searchModel->rowCount(QModelIndex()) > 0) {
|
||||
searchResultsView->setCurrentIndex(searchModel->index(0));
|
||||
}
|
||||
} else if (searchActive) {
|
||||
switchToTabMode();
|
||||
}
|
||||
}
|
||||
|
||||
void DlgSettings::switchToSearchMode()
|
||||
{
|
||||
searchActive = true;
|
||||
tabBarWidget->setVisible(false);
|
||||
pagesWidget->setVisible(false);
|
||||
searchResultsView->setVisible(true);
|
||||
if (searchModel->rowCount(QModelIndex()) > 0) {
|
||||
searchResultsView->setCurrentIndex(searchModel->index(0));
|
||||
}
|
||||
}
|
||||
|
||||
void DlgSettings::switchToTabMode()
|
||||
{
|
||||
searchActive = false;
|
||||
tabBarWidget->setVisible(true);
|
||||
pagesWidget->setVisible(true);
|
||||
searchResultsView->setVisible(false);
|
||||
searchEdit->blockSignals(true);
|
||||
searchEdit->clear();
|
||||
searchEdit->blockSignals(false);
|
||||
setActiveTab(currentTabIndex);
|
||||
}
|
||||
|
||||
void DlgSettings::onSearchResultClicked(const QModelIndex &index)
|
||||
{
|
||||
navigateToSearchResult(index);
|
||||
}
|
||||
|
||||
void DlgSettings::navigateToSearchResult(const QModelIndex &index)
|
||||
{
|
||||
SettingsSearchEntry entry = searchModel->entryForIndex(index);
|
||||
if (entry.pageIndex < 0 || entry.pageIndex >= pages.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Switch to the page
|
||||
switchToTabMode();
|
||||
setActiveTab(entry.pageIndex);
|
||||
|
||||
// Scroll to the widget, focus it, and flash to highlight it
|
||||
if (entry.widget) {
|
||||
QWidget *widget = entry.widget;
|
||||
while (widget) {
|
||||
if (auto *scrollArea = qobject_cast<QScrollArea *>(widget)) {
|
||||
scrollArea->ensureWidgetVisible(entry.widget);
|
||||
break;
|
||||
}
|
||||
widget = widget->parentWidget();
|
||||
}
|
||||
entry.widget->setFocus();
|
||||
flashWidget(entry.widget);
|
||||
}
|
||||
}
|
||||
|
||||
void DlgSettings::setTab(int index)
|
||||
{
|
||||
if (index <= contentsWidget->count() - 1 && index >= 0) {
|
||||
changePage(contentsWidget->item(index), contentsWidget->currentItem());
|
||||
contentsWidget->setCurrentRow(index);
|
||||
if (index >= 0 && index < tabButtons.size()) {
|
||||
setActiveTab(index);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -155,6 +421,49 @@ void DlgSettings::updateLanguage()
|
|||
installNewTranslator();
|
||||
}
|
||||
|
||||
bool DlgSettings::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if (watched == searchEdit && event->type() == QEvent::KeyPress) {
|
||||
auto *keyEvent = static_cast<QKeyEvent *>(event);
|
||||
if (keyEvent->key() == Qt::Key_Escape) {
|
||||
if (searchActive) {
|
||||
switchToTabMode();
|
||||
return true;
|
||||
}
|
||||
} else if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
|
||||
if (searchActive) {
|
||||
if (searchResultsView->currentIndex().isValid()) {
|
||||
navigateToSearchResult(searchResultsView->currentIndex());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else if (keyEvent->key() == Qt::Key_Down) {
|
||||
if (searchActive) {
|
||||
int nextRow = searchResultsView->currentIndex().row() + 1;
|
||||
if (nextRow >= searchModel->rowCount()) {
|
||||
nextRow = 0;
|
||||
}
|
||||
searchResultsView->setCurrentIndex(searchModel->index(nextRow));
|
||||
searchResultsView->scrollTo(searchModel->index(nextRow));
|
||||
return true;
|
||||
}
|
||||
} else if (keyEvent->key() == Qt::Key_Up) {
|
||||
if (searchActive) {
|
||||
int prevRow = searchResultsView->currentIndex().row() - 1;
|
||||
if (prevRow < 0) {
|
||||
prevRow = searchModel->rowCount() - 1;
|
||||
}
|
||||
if (prevRow >= 0) {
|
||||
searchResultsView->setCurrentIndex(searchModel->index(prevRow));
|
||||
searchResultsView->scrollTo(searchModel->index(prevRow));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return QDialog::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void DlgSettings::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
bool showLoadError = true;
|
||||
|
|
@ -209,7 +518,6 @@ void DlgSettings::closeEvent(QCloseEvent *event)
|
|||
|
||||
if (!QDir(SettingsCache::instance().paths().getDeckPath()).exists() ||
|
||||
SettingsCache::instance().paths().getDeckPath().isEmpty()) {
|
||||
//! \todo Prompt to create the deck directory.
|
||||
if (QMessageBox::critical(
|
||||
this, tr("Error"),
|
||||
tr("The path to your deck directory is invalid. Would you like to go back and set the correct path?"),
|
||||
|
|
@ -221,7 +529,6 @@ void DlgSettings::closeEvent(QCloseEvent *event)
|
|||
|
||||
if (!QDir(SettingsCache::instance().paths().getPicsPath()).exists() ||
|
||||
SettingsCache::instance().paths().getPicsPath().isEmpty()) {
|
||||
//! \todo Prompt to create the pictures directory.
|
||||
if (QMessageBox::critical(this, tr("Error"),
|
||||
tr("The path to your card pictures directory is invalid. Would you like to go back "
|
||||
"and set the correct path?"),
|
||||
|
|
@ -236,15 +543,26 @@ void DlgSettings::closeEvent(QCloseEvent *event)
|
|||
void DlgSettings::retranslateUi()
|
||||
{
|
||||
setWindowTitle(tr("Settings"));
|
||||
retranslateTabNames();
|
||||
|
||||
generalButton->setText(tr("General"));
|
||||
appearanceButton->setText(tr("Appearance"));
|
||||
userInterfaceButton->setText(tr("User Interface"));
|
||||
storageButton->setText(tr("Storage"));
|
||||
deckEditorButton->setText(tr("Card Sources"));
|
||||
messagesButton->setText(tr("Chat"));
|
||||
soundButton->setText(tr("Sound"));
|
||||
shortcutsButton->setText(tr("Shortcuts"));
|
||||
searchEdit->setPlaceholderText(tr("Search settings..."));
|
||||
okButton->setText(tr("OK"));
|
||||
|
||||
contentsWidget->reset();
|
||||
// Rebuild search index for translated text
|
||||
buildSearchIndex();
|
||||
}
|
||||
|
||||
QStringList DlgSettings::translatedPageNames()
|
||||
{
|
||||
return {tr("General"), tr("Appearance"), tr("User Interface"), tr("Card Sources"),
|
||||
tr("Storage"), tr("Chat"), tr("Sound"), tr("Shortcuts")};
|
||||
}
|
||||
|
||||
void DlgSettings::retranslateTabNames()
|
||||
{
|
||||
const QStringList tabLabels = translatedPageNames();
|
||||
|
||||
for (int i = 0; i < tabButtons.size() && i < tabLabels.size(); ++i) {
|
||||
tabButtons[i]->setText(tabLabels[i]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +1,99 @@
|
|||
/**
|
||||
* @file dlg_settings.h
|
||||
* @brief Main settings dialog for the Cockatrice client
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
//! \todo Document this file.
|
||||
|
||||
#ifndef DLG_SETTINGS_H
|
||||
#define DLG_SETTINGS_H
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDialog>
|
||||
#include <QLoggingCategory>
|
||||
|
||||
class QPushButton;
|
||||
|
||||
inline Q_LOGGING_CATEGORY(DlgSettingsLog, "dlg_settings");
|
||||
|
||||
class QListWidget;
|
||||
class QStackedWidget;
|
||||
class QListWidgetItem;
|
||||
class QToolButton;
|
||||
class QListView;
|
||||
class QLineEdit;
|
||||
|
||||
class AbstractSettingsPage;
|
||||
class SettingsSearchModel;
|
||||
class SettingsSearchDelegate;
|
||||
|
||||
/**
|
||||
* @brief Main application settings dialog with tabbed navigation and search
|
||||
*
|
||||
* Provides a modern settings interface organized into tabbed pages. Users can
|
||||
* either navigate by clicking tabs or search for specific settings using the
|
||||
* built-in search bar. Search results are filtered and ranked by relevance.
|
||||
*/
|
||||
class DlgSettings : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
* @brief Page order in the tab bar, matching the order pages are added in setupUi()
|
||||
*
|
||||
* Use these values instead of raw indices so reordering pages never silently
|
||||
* breaks external callers like tab_room.cpp.
|
||||
*/
|
||||
enum SettingsPage
|
||||
{
|
||||
GeneralPage = 0,
|
||||
AppearancePage,
|
||||
UserInterfacePage,
|
||||
DeckEditorPage,
|
||||
StoragePage,
|
||||
MessagesPage,
|
||||
SoundPage,
|
||||
ShortcutsPage,
|
||||
NumPages
|
||||
};
|
||||
|
||||
explicit DlgSettings(QWidget *parent = nullptr);
|
||||
void setTab(int index);
|
||||
|
||||
private slots:
|
||||
void changePage(QListWidgetItem *current, QListWidgetItem *previous);
|
||||
void onTabClicked(int index);
|
||||
void onSearchTextChanged(const QString &text);
|
||||
void onSearchResultClicked(const QModelIndex &index);
|
||||
void updateLanguage();
|
||||
|
||||
private:
|
||||
QListWidget *contentsWidget;
|
||||
QStackedWidget *pagesWidget;
|
||||
QListWidgetItem *generalButton, *appearanceButton, *userInterfaceButton, *deckEditorButton, *storageButton,
|
||||
*messagesButton, *soundButton, *shortcutsButton;
|
||||
void createIcons();
|
||||
// UI elements
|
||||
QLineEdit *searchEdit; ///< Search bar for filtering settings
|
||||
QWidget *tabBarWidget; ///< Container widget for the tab buttons
|
||||
QList<QToolButton *> tabButtons; ///< Navigation tab buttons
|
||||
QStackedWidget *pagesWidget; ///< Stacked widget containing settings pages
|
||||
QListView *searchResultsView; ///< Search results list view
|
||||
QWidget *pagesContainer; ///< Container stacking pages and search results
|
||||
QPushButton *okButton; ///< Button to close the dialog
|
||||
|
||||
// Data
|
||||
QList<AbstractSettingsPage *> pages; ///< All settings page instances
|
||||
SettingsSearchModel *searchModel; ///< Model for search results
|
||||
SettingsSearchDelegate *searchDelegate; ///< Delegate for search result rendering
|
||||
int currentTabIndex; ///< Currently active tab index
|
||||
bool searchActive; ///< Whether search mode is active
|
||||
|
||||
void setupUi();
|
||||
void setupTabBar();
|
||||
void buildSearchIndex();
|
||||
void switchToTabMode();
|
||||
void switchToSearchMode();
|
||||
void navigateToSearchResult(const QModelIndex &index);
|
||||
void setActiveTab(int index);
|
||||
static void flashWidget(QWidget *widget);
|
||||
static QStringList translatedPageNames();
|
||||
|
||||
void retranslateUi();
|
||||
void retranslateTabNames();
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *event) override;
|
||||
bool eventFilter(QObject *watched, QEvent *event) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // DLG_SETTINGS_H
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ void DlgUpdate::finishedUpdateCheck(bool needToUpdate, bool isCompatible, Releas
|
|||
// If there's no need to update, tell them that. However we still allow them to run the
|
||||
// downloader themselves if there's a compatible build
|
||||
QMessageBox::information(
|
||||
this, tr("No Update Available"),
|
||||
window(), tr("No Update Available"),
|
||||
tr("Cockatrice is up to date!") + "<br><br>" +
|
||||
tr("You are already running the latest version available in the chosen release channel.") + "<br>" +
|
||||
"<b>" + tr("Current version") + QString(":</b> %1<br>").arg(VERSION_STRING) + "<b>" +
|
||||
|
|
@ -147,7 +147,7 @@ void DlgUpdate::finishedUpdateCheck(bool needToUpdate, bool isCompatible, Releas
|
|||
if (isCompatible) {
|
||||
int reply;
|
||||
reply = QMessageBox::question(
|
||||
this, tr("Update Available"),
|
||||
window(), tr("Update Available"),
|
||||
tr("A new version of Cockatrice is available!") + "<br><br>" + "<b>" + tr("New version") +
|
||||
QString(":</b> %1<br>").arg(release->getName()) + "<b>" + tr("Released") +
|
||||
QString(":</b> %1 (<a href=\"%2\">").arg(publishDate, release->getDescriptionUrl()) + tr("Changelog") +
|
||||
|
|
@ -156,10 +156,12 @@ void DlgUpdate::finishedUpdateCheck(bool needToUpdate, bool isCompatible, Releas
|
|||
|
||||
if (reply == QMessageBox::Yes) {
|
||||
downloadUpdate(release->getName());
|
||||
} else {
|
||||
closeDialog();
|
||||
}
|
||||
} else {
|
||||
QMessageBox::information(
|
||||
this, tr("Update Available"),
|
||||
window(), tr("Update Available"),
|
||||
tr("A new version of Cockatrice is available!") + "<br><br>" + "<b>" + tr("New version") +
|
||||
QString(":</b> %1<br>").arg(release->getName()) + "<b>" + tr("Released") +
|
||||
QString(":</b> %1 (<a href=\"%2\">").arg(publishDate, release->getDescriptionUrl()) + tr("Changelog") +
|
||||
|
|
|
|||
|
|
@ -82,9 +82,7 @@ void ColorPie::paintEvent(QPaintEvent *)
|
|||
QString label = QString("%1%").arg(int(ratio * 100 + 0.5));
|
||||
|
||||
QFontMetrics fm(p.font());
|
||||
|
||||
int labelWidth = fm.horizontalAdvance(label);
|
||||
|
||||
QRectF textRect(labelPos.x() - labelWidth / 2.0, labelPos.y() - fm.height() / 2.0, labelWidth, fm.height());
|
||||
|
||||
p.setPen(Qt::black);
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@
|
|||
#include <QVBoxLayout>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/network/client/remote/remote_client.h>
|
||||
#include <libcockatrice/settings/appearance_settings.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
|
||||
HomeWidget::HomeWidget(QWidget *parent, TabSupervisor *_tabSupervisor)
|
||||
: QWidget(parent), tabSupervisor(_tabSupervisor), background("theme:backgrounds/home"), overlay("theme:cockatrice")
|
||||
|
|
@ -43,12 +43,12 @@ HomeWidget::HomeWidget(QWidget *parent, TabSupervisor *_tabSupervisor)
|
|||
updateConnectButton(tabSupervisor->getClient()->getStatus());
|
||||
|
||||
connect(tabSupervisor->getClient(), &RemoteClient::statusChanged, this, &HomeWidget::updateConnectButton);
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::homeTabBackgroundSourceChanged, this,
|
||||
connect(&SettingsCache::instance().appearance(), &AppearanceSettings::homeTabBackgroundSourceChanged, this,
|
||||
&HomeWidget::initializeBackgroundFromSource);
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::homeTabBackgroundShuffleFrequencyChanged, this,
|
||||
&HomeWidget::onBackgroundShuffleFrequencyChanged);
|
||||
connect(&SettingsCache::instance().appearance(), &AppearanceSettings::homeTabBackgroundShuffleFrequencyChanged,
|
||||
this, &HomeWidget::onBackgroundShuffleFrequencyChanged);
|
||||
// Lambda is cleaner to read than overloading this
|
||||
connect(&SettingsCache::instance().personal(), &PersonalSettings::homeTabDisplayCardNameChanged, this,
|
||||
connect(&SettingsCache::instance().appearance(), &AppearanceSettings::homeTabDisplayCardNameChanged, this,
|
||||
[this] { repaint(); });
|
||||
connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this,
|
||||
&HomeWidget::initializeBackgroundFromSource);
|
||||
|
|
@ -65,7 +65,7 @@ void HomeWidget::initializeBackgroundFromSource()
|
|||
}
|
||||
|
||||
auto backgroundSourceType =
|
||||
BackgroundSources::fromId(SettingsCache::instance().personal().getHomeTabBackgroundSource());
|
||||
BackgroundSources::fromId(SettingsCache::instance().appearance().getHomeTabBackgroundSource());
|
||||
|
||||
switch (backgroundSourceType) {
|
||||
case BackgroundSources::Theme:
|
||||
|
|
@ -113,7 +113,7 @@ void HomeWidget::setRandomCard(ExactCard &newCard)
|
|||
void HomeWidget::updateRandomCard()
|
||||
{
|
||||
auto backgroundSourceType =
|
||||
BackgroundSources::fromId(SettingsCache::instance().personal().getHomeTabBackgroundSource());
|
||||
BackgroundSources::fromId(SettingsCache::instance().appearance().getHomeTabBackgroundSource());
|
||||
|
||||
ExactCard newCard;
|
||||
|
||||
|
|
@ -156,8 +156,8 @@ void HomeWidget::updateRandomCard()
|
|||
void HomeWidget::onBackgroundShuffleFrequencyChanged()
|
||||
{
|
||||
cardChangeTimer->stop();
|
||||
if (SettingsCache::instance().personal().getHomeTabBackgroundShuffleFrequency() > 0) {
|
||||
cardChangeTimer->start(SettingsCache::instance().personal().getHomeTabBackgroundShuffleFrequency() * 1000);
|
||||
if (SettingsCache::instance().appearance().getHomeTabBackgroundShuffleFrequency() > 0) {
|
||||
cardChangeTimer->start(SettingsCache::instance().appearance().getHomeTabBackgroundShuffleFrequency() * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -265,7 +265,7 @@ void HomeWidget::updateConnectButton(const ClientStatus status)
|
|||
|
||||
QPair<QColor, QColor> HomeWidget::extractDominantColors(const QPixmap &pixmap)
|
||||
{
|
||||
if (themeManager->isBuiltInTheme() && SettingsCache::instance().personal().getHomeTabBackgroundSource() ==
|
||||
if (themeManager->isBuiltInTheme() && SettingsCache::instance().appearance().getHomeTabBackgroundSource() ==
|
||||
BackgroundSources::toId(BackgroundSources::Theme)) {
|
||||
return QPair<QColor, QColor>(QColor::fromRgb(20, 140, 60), QColor::fromRgb(120, 200, 80));
|
||||
}
|
||||
|
|
@ -352,7 +352,7 @@ void HomeWidget::paintEvent(QPaintEvent *event)
|
|||
}
|
||||
}
|
||||
|
||||
if (!cardName.isEmpty() && SettingsCache::instance().personal().getHomeTabDisplayCardName()) {
|
||||
if (!cardName.isEmpty() && SettingsCache::instance().appearance().getHomeTabDisplayCardName()) {
|
||||
QFont font = painter.font();
|
||||
font.setPointSize(14);
|
||||
font.setBold(true);
|
||||
|
|
|
|||
|
|
@ -16,16 +16,16 @@ class TearOffMenu : public QMenu
|
|||
public:
|
||||
explicit TearOffMenu(const QString &title, QWidget *parent = nullptr) : QMenu(title, parent)
|
||||
{
|
||||
connect(&SettingsCache::instance().interface(), &InterfaceSettings::useTearOffMenusChanged, this,
|
||||
connect(&SettingsCache::instance().userInterface(), &InterfaceSettings::useTearOffMenusChanged, this,
|
||||
[this](const bool state) { setTearOffEnabled(state); });
|
||||
setTearOffEnabled(SettingsCache::instance().interface().getUseTearOffMenus());
|
||||
setTearOffEnabled(SettingsCache::instance().userInterface().getUseTearOffMenus());
|
||||
}
|
||||
|
||||
explicit TearOffMenu(QWidget *parent = nullptr) : QMenu(parent)
|
||||
{
|
||||
connect(&SettingsCache::instance().interface(), &InterfaceSettings::useTearOffMenusChanged, this,
|
||||
connect(&SettingsCache::instance().userInterface(), &InterfaceSettings::useTearOffMenusChanged, this,
|
||||
[this](const bool state) { setTearOffEnabled(state); });
|
||||
setTearOffEnabled(SettingsCache::instance().interface().getUseTearOffMenus());
|
||||
setTearOffEnabled(SettingsCache::instance().userInterface().getUseTearOffMenus());
|
||||
}
|
||||
|
||||
TearOffMenu *addTearOffMenu(const QString &title)
|
||||
|
|
|
|||
234
cockatrice/src/interface/widgets/replay/replay_manager.cpp
Normal file
234
cockatrice/src/interface/widgets/replay/replay_manager.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
#include "replay_manager.h"
|
||||
|
||||
#include "../../../client/settings/cache_settings.h"
|
||||
|
||||
#include <QTimer>
|
||||
#include <libcockatrice/protocol/get_pb_extension.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
|
||||
static constexpr int TIMER_INTERVAL_MS = 200;
|
||||
static constexpr int EMPTY_SECTION_MARGIN_MS = 500;
|
||||
|
||||
static QList<int> createReplayTimeline(const GameReplay *replay)
|
||||
{
|
||||
// Create list: event number -> time [ms]
|
||||
unsigned int lastEventTimestamp = 0;
|
||||
const int eventCount = replay->event_list_size();
|
||||
|
||||
QList<int> replayTimeline;
|
||||
for (int i = 0; i < eventCount; ++i) {
|
||||
int nextSecondIndex = i + 1;
|
||||
while (nextSecondIndex < eventCount &&
|
||||
replay->event_list(nextSecondIndex).seconds_elapsed() == lastEventTimestamp) {
|
||||
++nextSecondIndex;
|
||||
}
|
||||
|
||||
// Distribute simultaneous events evenly across 1 second.
|
||||
const int numberEventsThisSecond = nextSecondIndex - i;
|
||||
for (int k = 0; k < numberEventsThisSecond; ++k) {
|
||||
int eventMs = replay->event_list(i + k).seconds_elapsed() * 1000;
|
||||
int distributionMs = static_cast<int>(static_cast<qreal>(k) / numberEventsThisSecond * 1000);
|
||||
replayTimeline.append(eventMs + distributionMs);
|
||||
}
|
||||
|
||||
if (nextSecondIndex < eventCount) {
|
||||
lastEventTimestamp = replay->event_list(nextSecondIndex).seconds_elapsed();
|
||||
}
|
||||
i += numberEventsThisSecond - 1;
|
||||
}
|
||||
|
||||
return replayTimeline;
|
||||
}
|
||||
|
||||
ReplayManager::ReplayManager(QObject *parent, GameReplay *replay)
|
||||
: QObject(parent), replay(replay), replayTimeline(createReplayTimeline(replay))
|
||||
{
|
||||
maxTime = replayTimeline.isEmpty() ? 0 : replayTimeline.last();
|
||||
|
||||
replayTimer = new QTimer(this);
|
||||
replayTimer->setInterval(TIMER_INTERVAL_MS);
|
||||
connect(replayTimer, &QTimer::timeout, this, &ReplayManager::replayTimerTimeout);
|
||||
|
||||
rewindBufferingTimer = new QTimer(this);
|
||||
rewindBufferingTimer->setSingleShot(true);
|
||||
connect(rewindBufferingTimer, &QTimer::timeout, this, &ReplayManager::processRewind);
|
||||
}
|
||||
|
||||
ReplayManager::~ReplayManager()
|
||||
{
|
||||
delete replay;
|
||||
}
|
||||
|
||||
void ReplayManager::skipToTime(int newTime, bool doRewindBuffering)
|
||||
{
|
||||
// check boundary conditions
|
||||
if (newTime < 0) {
|
||||
newTime = 0;
|
||||
}
|
||||
if (newTime > maxTime) {
|
||||
newTime = maxTime;
|
||||
}
|
||||
|
||||
newTime -= newTime % TIMER_INTERVAL_MS; // Time should always be a multiple of the interval
|
||||
|
||||
const bool isBackwardsSkip = newTime < currentProcessedTime;
|
||||
currentVisualTime = newTime;
|
||||
|
||||
if (isBackwardsSkip) {
|
||||
handleBackwardsSkip(doRewindBuffering);
|
||||
} else {
|
||||
processNewEvents(FORWARD_SKIP);
|
||||
}
|
||||
|
||||
timeChanged(currentVisualTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handles a backwards skip in the replay timeline.
|
||||
*
|
||||
* @param doRewindBuffering When true, if multiple backward skips are made in quick succession, only a single rewind
|
||||
* is processed at the end. When false, the backwards skip will always cause an immediate rewind.
|
||||
*/
|
||||
void ReplayManager::handleBackwardsSkip(bool doRewindBuffering)
|
||||
{
|
||||
if (doRewindBuffering) {
|
||||
// We use a one-shot timer to implement the rewind buffering.
|
||||
// The rewind only happens once the timer runs out.
|
||||
// If another backwards skip happens, the timer will just get reset instead of rewinding.
|
||||
rewindBufferingTimer->stop();
|
||||
rewindBufferingTimer->start(SettingsCache::instance().userInterface().getRewindBufferingMs());
|
||||
} else {
|
||||
// otherwise, process the rewind immediately
|
||||
processRewind();
|
||||
}
|
||||
}
|
||||
|
||||
void ReplayManager::processRewind()
|
||||
{
|
||||
// stop any queued-up rewinds
|
||||
rewindBufferingTimer->stop();
|
||||
|
||||
// process the rewind
|
||||
currentEvent = 0;
|
||||
emit rewound();
|
||||
processNewEvents(BACKWARD_SKIP);
|
||||
}
|
||||
|
||||
void ReplayManager::replayTimerTimeout()
|
||||
{
|
||||
currentVisualTime += TIMER_INTERVAL_MS;
|
||||
|
||||
processNewEvents(NORMAL_PLAYBACK);
|
||||
|
||||
timeChanged(currentVisualTime);
|
||||
|
||||
if (skipEmptySections) {
|
||||
handleSkipEmptySection();
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Processes all unprocessed events up to the current time. */
|
||||
void ReplayManager::processNewEvents(PlaybackMode playbackMode)
|
||||
{
|
||||
currentProcessedTime = currentVisualTime;
|
||||
|
||||
while (currentEvent < replayTimeline.size() && replayTimeline[currentEvent] < currentProcessedTime) {
|
||||
EventProcessingOptions options;
|
||||
|
||||
// backwards skip => always skip reveal windows
|
||||
// forwards skip => skip reveal windows that don't happen within a big skip of the target
|
||||
if (playbackMode == BACKWARD_SKIP || currentProcessedTime - replayTimeline[currentEvent] > BIG_SKIP_MS) {
|
||||
options |= SKIP_REVEAL_WINDOW;
|
||||
}
|
||||
|
||||
// backwards skip => always skip tap animation
|
||||
if (playbackMode == BACKWARD_SKIP) {
|
||||
options |= SKIP_TAP_ANIMATION;
|
||||
}
|
||||
|
||||
emit eventReplayed(replay->event_list(currentEvent), options);
|
||||
++currentEvent;
|
||||
}
|
||||
if (currentEvent == replayTimeline.size()) {
|
||||
emit replayFinished();
|
||||
replayTimer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
static bool hasMeaningfulEvent(const GameEventContainer &cont)
|
||||
{
|
||||
const int eventListSize = cont.event_list_size();
|
||||
for (int i = 0; i < eventListSize; ++i) {
|
||||
const GameEvent &event = cont.event_list(i);
|
||||
const auto eventType = static_cast<GameEvent::GameEventType>(getPbExtension(event));
|
||||
|
||||
if (eventType != GameEvent::PLAYER_PROPERTIES_CHANGED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReplayManager::handleSkipEmptySection()
|
||||
{
|
||||
if (currentEvent == replayTimeline.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// find most recent meaningful event
|
||||
int prevEvent = std::max(0, currentEvent - 1);
|
||||
for (; prevEvent > 0 && !hasMeaningfulEvent(replay->event_list(prevEvent)); --prevEvent) {
|
||||
}
|
||||
|
||||
int prevEventTime = replayTimeline.value(prevEvent);
|
||||
if (currentVisualTime - prevEventTime <= EMPTY_SECTION_MARGIN_MS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// find next earliest meaningful event
|
||||
int nextEvent = currentEvent;
|
||||
for (; nextEvent < replayTimeline.size() - 1 && !hasMeaningfulEvent(replay->event_list(nextEvent)); ++nextEvent) {
|
||||
}
|
||||
|
||||
int nextEventTime = replayTimeline.value(nextEvent);
|
||||
if (nextEventTime - currentVisualTime <= EMPTY_SECTION_MARGIN_MS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// skip forward if we're not within margin of either event
|
||||
skipToTime(nextEventTime - EMPTY_SECTION_MARGIN_MS, false);
|
||||
}
|
||||
|
||||
void ReplayManager::setTimeScaleFactor(qreal _timeScaleFactor)
|
||||
{
|
||||
timeScaleFactor = _timeScaleFactor;
|
||||
int interval = std::max(1, qRound(TIMER_INTERVAL_MS / timeScaleFactor));
|
||||
replayTimer->setInterval(interval);
|
||||
}
|
||||
|
||||
void ReplayManager::setSkipEmptySections(bool value)
|
||||
{
|
||||
skipEmptySections = value;
|
||||
}
|
||||
|
||||
void ReplayManager::startReplay()
|
||||
{
|
||||
replayTimer->start();
|
||||
}
|
||||
|
||||
void ReplayManager::stopReplay()
|
||||
{
|
||||
replayTimer->stop();
|
||||
}
|
||||
|
||||
void ReplayManager::setTime(int time)
|
||||
{
|
||||
// don't buffer rewinds from clicks, since clicks usually don't happen fast enough to require buffering
|
||||
skipToTime(time, false);
|
||||
}
|
||||
|
||||
void ReplayManager::skipByAmount(int amount)
|
||||
{
|
||||
skipToTime(currentVisualTime + amount, amount < 0);
|
||||
}
|
||||
83
cockatrice/src/interface/widgets/replay/replay_manager.h
Normal file
83
cockatrice/src/interface/widgets/replay/replay_manager.h
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#ifndef COCKATRICE_REPLAY_MANAGER_H
|
||||
#define COCKATRICE_REPLAY_MANAGER_H
|
||||
|
||||
#include "../../../game/player/event_processing_options.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <libcockatrice/protocol/pb/game_replay.pb.h>
|
||||
|
||||
class GameReplay;
|
||||
class QTimer;
|
||||
|
||||
/**
|
||||
* @brief This class handles all logic to do with playing back replays
|
||||
*/
|
||||
class ReplayManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
enum PlaybackMode
|
||||
{
|
||||
NORMAL_PLAYBACK,
|
||||
FORWARD_SKIP,
|
||||
BACKWARD_SKIP
|
||||
};
|
||||
|
||||
GameReplay *replay;
|
||||
QList<int> replayTimeline; ///< timestamp of each event, with the indexes corresponding
|
||||
int maxTime;
|
||||
|
||||
QTimer *replayTimer;
|
||||
QTimer *rewindBufferingTimer;
|
||||
|
||||
qreal timeScaleFactor = 1.0;
|
||||
bool skipEmptySections = false;
|
||||
|
||||
int currentVisualTime = 0; ///< time currently displayed by the timeline
|
||||
int currentProcessedTime = 0; ///< time that events are currently processed up to. Could differ from visual time due
|
||||
///< to rewind buffering
|
||||
int currentEvent = 0; ///< current event's index
|
||||
|
||||
void skipToTime(int newTime, bool doRewindBuffering);
|
||||
void handleBackwardsSkip(bool doRewindBuffering);
|
||||
void processRewind();
|
||||
void processNewEvents(PlaybackMode playbackMode);
|
||||
void handleSkipEmptySection();
|
||||
|
||||
private slots:
|
||||
void replayTimerTimeout();
|
||||
|
||||
public:
|
||||
static constexpr int SMALL_SKIP_MS = 1000;
|
||||
static constexpr int BIG_SKIP_MS = 10000;
|
||||
|
||||
/**
|
||||
* @param parent The parent QObject
|
||||
* @param replay Cannot be null. Takes ownership of the object.
|
||||
*/
|
||||
explicit ReplayManager(QObject *parent, GameReplay *replay);
|
||||
|
||||
~ReplayManager() override;
|
||||
|
||||
const QList<int> &getReplayTimeline() const
|
||||
{
|
||||
return replayTimeline;
|
||||
}
|
||||
|
||||
void setTimeScaleFactor(qreal _timeScaleFactor);
|
||||
void setSkipEmptySections(bool value);
|
||||
|
||||
public slots:
|
||||
void startReplay();
|
||||
void stopReplay();
|
||||
void setTime(int time);
|
||||
void skipByAmount(int amount); // use a negative amount to skip backwards
|
||||
|
||||
signals:
|
||||
void timeChanged(int time);
|
||||
void eventReplayed(const GameEventContainer &cont, EventProcessingOptions options);
|
||||
void replayFinished();
|
||||
void rewound();
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_REPLAY_MANAGER_H
|
||||
|
|
@ -14,16 +14,21 @@ ReplayQuickSettingsWidget::ReplayQuickSettingsWidget(QWidget *parent) : Settings
|
|||
fastForwardSpeedBox.setMinimum(1);
|
||||
fastForwardSpeedBox.setMaximum(99.9);
|
||||
fastForwardSpeedBox.setDecimals(1);
|
||||
fastForwardSpeedBox.setValue(SettingsCache::instance().interface().getFastForwardSpeed());
|
||||
fastForwardSpeedBox.setValue(SettingsCache::instance().userInterface().getFastForwardSpeed());
|
||||
connect(&fastForwardSpeedBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this,
|
||||
&ReplayQuickSettingsWidget::actUpdateFastForwardSpeed);
|
||||
|
||||
skipEmptyCheckBox.setChecked(SettingsCache::instance().userInterface().getSkipEmptySections());
|
||||
connect(&skipEmptyCheckBox, &QCheckBox::QT_STATE_CHANGED, this,
|
||||
&ReplayQuickSettingsWidget::actUpdateSkipEmptySections);
|
||||
|
||||
// putting it all together
|
||||
auto *widget = new QWidget;
|
||||
auto *grid = new QGridLayout(widget);
|
||||
grid->setContentsMargins(0, 0, 0, 0);
|
||||
grid->addWidget(&fastForwardSpeedLabel, 0, 0, 1, 1);
|
||||
grid->addWidget(&fastForwardSpeedBox, 0, 1, 1, 1);
|
||||
grid->addWidget(&skipEmptyCheckBox, 1, 0, 1, 2);
|
||||
|
||||
this->addSettingsWidget(widget);
|
||||
|
||||
|
|
@ -36,10 +41,18 @@ void ReplayQuickSettingsWidget::retranslateUi()
|
|||
{
|
||||
fastForwardSpeedLabel.setText(tr("Fast forward speed:"));
|
||||
fastForwardSpeedBox.setSuffix("x");
|
||||
|
||||
skipEmptyCheckBox.setText(tr("Skip empty sections"));
|
||||
}
|
||||
|
||||
void ReplayQuickSettingsWidget::actUpdateFastForwardSpeed(qreal value)
|
||||
{
|
||||
SettingsCache::instance().interface().setFastForwardSpeed(value);
|
||||
SettingsCache::instance().userInterface().setFastForwardSpeed(value);
|
||||
emit fastForwardSpeedChanged(value);
|
||||
}
|
||||
|
||||
void ReplayQuickSettingsWidget::actUpdateSkipEmptySections(QT_STATE_CHANGED_T value)
|
||||
{
|
||||
SettingsCache::instance().userInterface().setSkipEmptySections(value);
|
||||
emit skipEmptySectionsChanged(value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
|
||||
#include "../../interface/widgets/quick_settings/settings_button_widget.h"
|
||||
|
||||
#include <QCheckBox>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <libcockatrice/utility/macros.h>
|
||||
|
||||
class ReplayQuickSettingsWidget : public SettingsButtonWidget
|
||||
{
|
||||
|
|
@ -16,13 +18,17 @@ public:
|
|||
|
||||
signals:
|
||||
void fastForwardSpeedChanged(qreal speed);
|
||||
void skipEmptySectionsChanged(bool skip);
|
||||
|
||||
private:
|
||||
QLabel fastForwardSpeedLabel;
|
||||
QDoubleSpinBox fastForwardSpeedBox;
|
||||
|
||||
QCheckBox skipEmptyCheckBox;
|
||||
|
||||
private slots:
|
||||
void actUpdateFastForwardSpeed(qreal value);
|
||||
void actUpdateSkipEmptySections(QT_STATE_CHANGED_T value);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_REPLAY_QUICK_SETTINGS_WIDGET_H
|
||||
|
|
|
|||
|
|
@ -4,26 +4,19 @@
|
|||
|
||||
#include <QPainter>
|
||||
#include <QPainterPath>
|
||||
#include <QTimer>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
|
||||
ReplayTimelineWidget::ReplayTimelineWidget(QWidget *parent)
|
||||
: QWidget(parent), maxBinValue(1), maxTime(1), timeScaleFactor(1.0), currentVisualTime(0), currentProcessedTime(0),
|
||||
currentEvent(0)
|
||||
static constexpr int BIN_LENGTH = 5000;
|
||||
static constexpr int MIN_RESOLUTION_MS = 1000;
|
||||
|
||||
ReplayTimelineWidget::ReplayTimelineWidget(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
replayTimer = new QTimer(this);
|
||||
replayTimer->setInterval(TIMER_INTERVAL_MS);
|
||||
connect(replayTimer, &QTimer::timeout, this, &ReplayTimelineWidget::replayTimerTimeout);
|
||||
|
||||
rewindBufferingTimer = new QTimer(this);
|
||||
rewindBufferingTimer->setSingleShot(true);
|
||||
connect(rewindBufferingTimer, &QTimer::timeout, this, &ReplayTimelineWidget::processRewind);
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::setTimeline(const QList<int> &_replayTimeline)
|
||||
void ReplayTimelineWidget::setTimeline(const QList<int> &replayTimeline)
|
||||
{
|
||||
replayTimeline = _replayTimeline;
|
||||
histogram.clear();
|
||||
currentTime = 0;
|
||||
|
||||
int binEndTime = BIN_LENGTH - 1;
|
||||
int binValue = 0;
|
||||
for (int i : replayTimeline) {
|
||||
|
|
@ -66,70 +59,31 @@ void ReplayTimelineWidget::paintEvent(QPaintEvent * /* event */)
|
|||
painter.fillPath(path, Qt::black);
|
||||
|
||||
const QColor barColor = QColor::fromHsv(120, 255, 255, 100);
|
||||
quint64 w = (quint64)(width() - 1) * (quint64)currentVisualTime / maxTime;
|
||||
quint64 w = (quint64)(width() - 1) * (quint64)currentTime / maxTime;
|
||||
painter.fillRect(0, 0, static_cast<int>(w), height() - 1, barColor);
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
int newTime = static_cast<int>((qint64)maxTime * (qint64)event->position().x() / width());
|
||||
// don't buffer rewinds from clicks, since clicks usually don't happen fast enough to require buffering
|
||||
skipToTime(newTime, false);
|
||||
emit timeClicked(newTime);
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::skipToTime(int newTime, bool doRewindBuffering)
|
||||
void ReplayTimelineWidget::setCurrentTime(int time)
|
||||
{
|
||||
// check boundary conditions
|
||||
if (newTime < 0) {
|
||||
newTime = 0;
|
||||
}
|
||||
if (newTime > maxTime) {
|
||||
newTime = maxTime;
|
||||
int newTime = qBound(0, time, maxTime);
|
||||
|
||||
if (currentTime == newTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
newTime -= newTime % TIMER_INTERVAL_MS; // Time should always be a multiple of the interval
|
||||
bool doUpdate = currentTime / MIN_RESOLUTION_MS != newTime / MIN_RESOLUTION_MS;
|
||||
|
||||
const bool isBackwardsSkip = newTime < currentProcessedTime;
|
||||
currentVisualTime = newTime;
|
||||
currentTime = newTime;
|
||||
|
||||
if (isBackwardsSkip) {
|
||||
handleBackwardsSkip(doRewindBuffering);
|
||||
} else {
|
||||
processNewEvents(FORWARD_SKIP);
|
||||
if (doUpdate) {
|
||||
update();
|
||||
}
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handles a backwards skip in the replay timeline.
|
||||
*
|
||||
* @param doRewindBuffering When true, if multiple backward skips are made in quick succession, only a single rewind
|
||||
* is processed at the end. When false, the backwards skip will always cause an immediate rewind.
|
||||
*/
|
||||
void ReplayTimelineWidget::handleBackwardsSkip(bool doRewindBuffering)
|
||||
{
|
||||
if (doRewindBuffering) {
|
||||
// We use a one-shot timer to implement the rewind buffering.
|
||||
// The rewind only happens once the timer runs out.
|
||||
// If another backwards skip happens, the timer will just get reset instead of rewinding.
|
||||
rewindBufferingTimer->stop();
|
||||
rewindBufferingTimer->start(SettingsCache::instance().interface().getRewindBufferingMs());
|
||||
} else {
|
||||
// otherwise, process the rewind immediately
|
||||
processRewind();
|
||||
}
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::processRewind()
|
||||
{
|
||||
// stop any queued-up rewinds
|
||||
rewindBufferingTimer->stop();
|
||||
|
||||
// process the rewind
|
||||
currentEvent = 0;
|
||||
emit rewound();
|
||||
processNewEvents(BACKWARD_SKIP);
|
||||
}
|
||||
|
||||
QSize ReplayTimelineWidget::sizeHint() const
|
||||
|
|
@ -141,64 +95,3 @@ QSize ReplayTimelineWidget::minimumSizeHint() const
|
|||
{
|
||||
return {400, 50};
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::replayTimerTimeout()
|
||||
{
|
||||
currentVisualTime += TIMER_INTERVAL_MS;
|
||||
|
||||
processNewEvents(NORMAL_PLAYBACK);
|
||||
|
||||
if (!(currentVisualTime % 1000)) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Processes all unprocessed events up to the current time. */
|
||||
void ReplayTimelineWidget::processNewEvents(PlaybackMode playbackMode)
|
||||
{
|
||||
currentProcessedTime = currentVisualTime;
|
||||
|
||||
while ((currentEvent < replayTimeline.size()) && (replayTimeline[currentEvent] < currentProcessedTime)) {
|
||||
EventProcessingOptions options;
|
||||
|
||||
// backwards skip => always skip reveal windows
|
||||
// forwards skip => skip reveal windows that don't happen within a big skip of the target
|
||||
if (playbackMode == BACKWARD_SKIP || currentProcessedTime - replayTimeline[currentEvent] > BIG_SKIP_MS) {
|
||||
options |= SKIP_REVEAL_WINDOW;
|
||||
}
|
||||
|
||||
// backwards skip => always skip tap animation
|
||||
if (playbackMode == BACKWARD_SKIP) {
|
||||
options |= SKIP_TAP_ANIMATION;
|
||||
}
|
||||
|
||||
emit processNextEvent(options);
|
||||
++currentEvent;
|
||||
}
|
||||
if (currentEvent == replayTimeline.size()) {
|
||||
emit replayFinished();
|
||||
replayTimer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::setTimeScaleFactor(qreal _timeScaleFactor)
|
||||
{
|
||||
timeScaleFactor = _timeScaleFactor;
|
||||
int interval = std::max(1, qRound(TIMER_INTERVAL_MS / timeScaleFactor));
|
||||
replayTimer->setInterval(interval);
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::startReplay()
|
||||
{
|
||||
replayTimer->start();
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::stopReplay()
|
||||
{
|
||||
replayTimer->stop();
|
||||
}
|
||||
|
||||
void ReplayTimelineWidget::skipByAmount(int amount)
|
||||
{
|
||||
skipToTime(currentVisualTime + amount, amount < 0);
|
||||
}
|
||||
|
|
@ -18,57 +18,25 @@ class QTimer;
|
|||
class ReplayTimelineWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void processNextEvent(EventProcessingOptions options);
|
||||
void replayFinished();
|
||||
void rewound();
|
||||
void timeClicked(int newTime);
|
||||
|
||||
private:
|
||||
enum PlaybackMode
|
||||
{
|
||||
NORMAL_PLAYBACK,
|
||||
FORWARD_SKIP,
|
||||
BACKWARD_SKIP
|
||||
};
|
||||
|
||||
static constexpr int TIMER_INTERVAL_MS = 200;
|
||||
static constexpr int BIN_LENGTH = 5000;
|
||||
|
||||
QTimer *replayTimer;
|
||||
QTimer *rewindBufferingTimer;
|
||||
QList<int> replayTimeline;
|
||||
QList<int> histogram;
|
||||
int maxBinValue, maxTime;
|
||||
qreal timeScaleFactor;
|
||||
int currentVisualTime; // time currently displayed by the timeline
|
||||
int currentProcessedTime; // time that events are currently processed up to. Could differ from visual time due to
|
||||
// rewind buffering
|
||||
int currentEvent;
|
||||
int maxBinValue = 1;
|
||||
int maxTime = 1;
|
||||
|
||||
void skipToTime(int newTime, bool doRewindBuffering);
|
||||
void handleBackwardsSkip(bool doRewindBuffering);
|
||||
void processRewind();
|
||||
void processNewEvents(PlaybackMode playbackMode);
|
||||
private slots:
|
||||
void replayTimerTimeout();
|
||||
int currentTime = 0;
|
||||
|
||||
public:
|
||||
static constexpr int SMALL_SKIP_MS = 1000;
|
||||
static constexpr int BIG_SKIP_MS = 10000;
|
||||
|
||||
explicit ReplayTimelineWidget(QWidget *parent = nullptr);
|
||||
void setTimeline(const QList<int> &_replayTimeline);
|
||||
void setTimeline(const QList<int> &replayTimeline);
|
||||
[[nodiscard]] QSize sizeHint() const override;
|
||||
[[nodiscard]] QSize minimumSizeHint() const override;
|
||||
void setTimeScaleFactor(qreal _timeScaleFactor);
|
||||
[[nodiscard]] int getCurrentEvent() const
|
||||
{
|
||||
return currentEvent;
|
||||
}
|
||||
|
||||
public slots:
|
||||
void startReplay();
|
||||
void stopReplay();
|
||||
void skipByAmount(int amount); // use a negative amount to skip backwards
|
||||
void setCurrentTime(int time);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
|
|
|
|||
|
|
@ -1,70 +1,50 @@
|
|||
#include "replay_widget.h"
|
||||
|
||||
#include "../../../client/settings/cache_settings.h"
|
||||
#include "../../../client/settings/shortcuts_settings.h"
|
||||
#include "../interface/widgets/tabs/tab_game.h"
|
||||
#include "replay_manager.h"
|
||||
#include "replay_quick_settings_widget.h"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QToolButton>
|
||||
|
||||
ReplayWidget::ReplayWidget(TabGame *parent, GameReplay *_replay)
|
||||
: QWidget(parent), game(parent), replay(_replay), replayPlayButton(nullptr), replayFastForwardButton(nullptr),
|
||||
aReplaySkipForward(nullptr), aReplaySkipBackward(nullptr), aReplaySkipForwardBig(nullptr),
|
||||
aReplaySkipBackwardBig(nullptr)
|
||||
ReplayWidget::ReplayWidget(QWidget *parent, GameReplay *replay)
|
||||
: QWidget(parent), replayPlayButton(nullptr), replayFastForwardButton(nullptr), aReplaySkipForward(nullptr),
|
||||
aReplaySkipBackward(nullptr), aReplaySkipForwardBig(nullptr), aReplaySkipBackwardBig(nullptr)
|
||||
{
|
||||
if (replay) {
|
||||
game->getGame()->loadReplay(replay);
|
||||
|
||||
// Create list: event number -> time [ms]
|
||||
// Distribute simultaneous events evenly across 1 second.
|
||||
unsigned int lastEventTimestamp = 0;
|
||||
const int eventCount = replay->event_list_size();
|
||||
for (int i = 0; i < eventCount; ++i) {
|
||||
int j = i + 1;
|
||||
while ((j < eventCount) && (replay->event_list(j).seconds_elapsed() == lastEventTimestamp)) {
|
||||
++j;
|
||||
}
|
||||
|
||||
const int numberEventsThisSecond = j - i;
|
||||
for (int k = 0; k < numberEventsThisSecond; ++k) {
|
||||
replayTimeline.append(replay->event_list(i + k).seconds_elapsed() * 1000 +
|
||||
(int)((qreal)k / (qreal)numberEventsThisSecond * 1000));
|
||||
}
|
||||
|
||||
if (j < eventCount) {
|
||||
lastEventTimestamp = replay->event_list(j).seconds_elapsed();
|
||||
}
|
||||
i += numberEventsThisSecond - 1;
|
||||
}
|
||||
}
|
||||
// replay manager
|
||||
replayManager = new ReplayManager(this, replay);
|
||||
connect(replayManager, &ReplayManager::eventReplayed, this, &ReplayWidget::eventReplayed);
|
||||
connect(replayManager, &ReplayManager::replayFinished, this, &ReplayWidget::replayFinished);
|
||||
connect(replayManager, &ReplayManager::rewound, this, &ReplayWidget::rewound);
|
||||
|
||||
// timeline widget
|
||||
timelineWidget = new ReplayTimelineWidget;
|
||||
timelineWidget->setTimeline(replayTimeline);
|
||||
connect(timelineWidget, &ReplayTimelineWidget::processNextEvent, this, &ReplayWidget::replayNextEvent);
|
||||
connect(timelineWidget, &ReplayTimelineWidget::replayFinished, this, &ReplayWidget::replayFinished);
|
||||
connect(timelineWidget, &ReplayTimelineWidget::rewound, this, &ReplayWidget::replayRewind);
|
||||
timelineWidget->setTimeline(replayManager->getReplayTimeline());
|
||||
connect(replayManager, &ReplayManager::timeChanged, timelineWidget, &ReplayTimelineWidget::setCurrentTime);
|
||||
connect(timelineWidget, &ReplayTimelineWidget::timeClicked, replayManager, &ReplayManager::setTime);
|
||||
|
||||
// timeline skip shortcuts
|
||||
aReplaySkipForward = new QAction(timelineWidget);
|
||||
timelineWidget->addAction(aReplaySkipForward);
|
||||
connect(aReplaySkipForward, &QAction::triggered, this,
|
||||
[this] { timelineWidget->skipByAmount(ReplayTimelineWidget::SMALL_SKIP_MS); });
|
||||
[this] { replayManager->skipByAmount(ReplayManager::SMALL_SKIP_MS); });
|
||||
|
||||
aReplaySkipBackward = new QAction(timelineWidget);
|
||||
timelineWidget->addAction(aReplaySkipBackward);
|
||||
connect(aReplaySkipBackward, &QAction::triggered, this,
|
||||
[this] { timelineWidget->skipByAmount(-ReplayTimelineWidget::SMALL_SKIP_MS); });
|
||||
[this] { replayManager->skipByAmount(-ReplayManager::SMALL_SKIP_MS); });
|
||||
|
||||
aReplaySkipForwardBig = new QAction(timelineWidget);
|
||||
timelineWidget->addAction(aReplaySkipForwardBig);
|
||||
connect(aReplaySkipForwardBig, &QAction::triggered, this,
|
||||
[this] { timelineWidget->skipByAmount(ReplayTimelineWidget::BIG_SKIP_MS); });
|
||||
[this] { replayManager->skipByAmount(ReplayManager::BIG_SKIP_MS); });
|
||||
|
||||
aReplaySkipBackwardBig = new QAction(timelineWidget);
|
||||
timelineWidget->addAction(aReplaySkipBackwardBig);
|
||||
connect(aReplaySkipBackwardBig, &QAction::triggered, this,
|
||||
[this] { timelineWidget->skipByAmount(-ReplayTimelineWidget::BIG_SKIP_MS); });
|
||||
[this] { replayManager->skipByAmount(-ReplayManager::BIG_SKIP_MS); });
|
||||
|
||||
// buttons
|
||||
replayPlayButton = new QToolButton;
|
||||
|
|
@ -86,6 +66,10 @@ ReplayWidget::ReplayWidget(TabGame *parent, GameReplay *_replay)
|
|||
settingsWidget->setFixedSize(QSize(32, 32));
|
||||
connect(settingsWidget, &ReplayQuickSettingsWidget::fastForwardSpeedChanged, this,
|
||||
[this] { updateTimeScaleFactor(replayFastForwardButton->isChecked()); });
|
||||
connect(settingsWidget, &ReplayQuickSettingsWidget::skipEmptySectionsChanged, replayManager,
|
||||
&ReplayManager::setSkipEmptySections);
|
||||
|
||||
replayManager->setSkipEmptySections(SettingsCache::instance().userInterface().getSkipEmptySections());
|
||||
|
||||
// putting everything together
|
||||
auto replayControlLayout = new QHBoxLayout;
|
||||
|
|
@ -97,18 +81,11 @@ ReplayWidget::ReplayWidget(TabGame *parent, GameReplay *_replay)
|
|||
setObjectName("replayControlWidget");
|
||||
setLayout(replayControlLayout);
|
||||
|
||||
connect(this, &ReplayWidget::requestChatAndPhaseReset, game, &TabGame::resetChatAndPhase);
|
||||
|
||||
connect(&SettingsCache::instance().shortcuts(), &ShortcutsSettings::shortCutChanged, this,
|
||||
&ReplayWidget::refreshShortcuts);
|
||||
refreshShortcuts();
|
||||
}
|
||||
|
||||
void ReplayWidget::replayNextEvent(EventProcessingOptions options)
|
||||
{
|
||||
emit eventReplayed(replay->event_list(timelineWidget->getCurrentEvent()), options);
|
||||
}
|
||||
|
||||
void ReplayWidget::replayFinished()
|
||||
{
|
||||
replayPlayButton->setChecked(false);
|
||||
|
|
@ -117,24 +94,16 @@ void ReplayWidget::replayFinished()
|
|||
void ReplayWidget::replayPlayButtonToggled(bool checked)
|
||||
{
|
||||
if (checked) { // start replay
|
||||
timelineWidget->startReplay();
|
||||
replayManager->startReplay();
|
||||
} else { // pause replay
|
||||
timelineWidget->stopReplay();
|
||||
replayManager->stopReplay();
|
||||
}
|
||||
}
|
||||
|
||||
void ReplayWidget::updateTimeScaleFactor(bool isFastForward)
|
||||
{
|
||||
qreal factor = isFastForward ? SettingsCache::instance().interface().getFastForwardSpeed() : 1.0;
|
||||
timelineWidget->setTimeScaleFactor(factor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Handles everything that needs to be reset when doing a replay rewind.
|
||||
*/
|
||||
void ReplayWidget::replayRewind()
|
||||
{
|
||||
emit requestChatAndPhaseReset();
|
||||
qreal factor = isFastForward ? SettingsCache::instance().userInterface().getFastForwardSpeed() : 1.0;
|
||||
replayManager->setTimeScaleFactor(factor);
|
||||
}
|
||||
|
||||
void ReplayWidget::refreshShortcuts()
|
||||
|
|
|
|||
|
|
@ -14,11 +14,12 @@
|
|||
#include <QWidget>
|
||||
#include <libcockatrice/protocol/pb/game_replay.pb.h>
|
||||
|
||||
class ReplayManager;
|
||||
class ReplayQuickSettingsWidget;
|
||||
class TabGame;
|
||||
|
||||
/**
|
||||
* @brief The top-level that is put in the replay dock widget.
|
||||
* @brief The top-level widget that is put in the replay dock widget.
|
||||
* Contains the replay timeline as well as the buttons.
|
||||
*/
|
||||
class ReplayWidget : public QWidget
|
||||
|
|
@ -26,29 +27,28 @@ class ReplayWidget : public QWidget
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ReplayWidget(TabGame *parent, GameReplay *replay);
|
||||
TabGame *game;
|
||||
GameReplay *replay;
|
||||
/**
|
||||
* @param parent The parent widget
|
||||
* @param replay Cannot be null. Takes ownership of the replay.
|
||||
*/
|
||||
ReplayWidget(QWidget *parent, GameReplay *replay);
|
||||
|
||||
signals:
|
||||
void requestChatAndPhaseReset();
|
||||
void rewound();
|
||||
void eventReplayed(const GameEventContainer &cont, EventProcessingOptions options);
|
||||
|
||||
private:
|
||||
// Replay related members
|
||||
int currentReplayStep = 0;
|
||||
QList<int> replayTimeline;
|
||||
ReplayManager *replayManager;
|
||||
|
||||
ReplayTimelineWidget *timelineWidget;
|
||||
QToolButton *replayPlayButton, *replayFastForwardButton;
|
||||
ReplayQuickSettingsWidget *settingsWidget;
|
||||
QAction *aReplaySkipForward, *aReplaySkipBackward, *aReplaySkipForwardBig, *aReplaySkipBackwardBig;
|
||||
|
||||
private slots:
|
||||
void replayNextEvent(EventProcessingOptions options);
|
||||
void replayFinished();
|
||||
void replayPlayButtonToggled(bool checked);
|
||||
void updateTimeScaleFactor(bool checked);
|
||||
void replayRewind();
|
||||
void refreshShortcuts();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#include <QDesktopServices>
|
||||
#include <QMouseEvent>
|
||||
#include <QScrollBar>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/network/server/remote/user_level.h>
|
||||
#include <libcockatrice/settings/chat_settings.h>
|
||||
|
||||
|
|
@ -62,12 +63,14 @@ void ChatView::adjustColorsToPalette()
|
|||
serverMessageColor = QColor(0xFF, 0x73, 0x83);
|
||||
otherUserColor = otherUserColor.lighter(150);
|
||||
linkColor = QColor(71, 158, 252);
|
||||
unresolvedCardTagColor = QColor(0xFF, 0xA5, 0x00);
|
||||
} else {
|
||||
document()->setDefaultStyleSheet(R"(
|
||||
a { text-decoration: none; color: blue; }
|
||||
.blue { color: blue }
|
||||
)");
|
||||
linkColor = palette().link().color();
|
||||
unresolvedCardTagColor = QColor(0xA0, 0x52, 0x2D);
|
||||
}
|
||||
|
||||
QTimer::singleShot(0, this, &ChatView::refreshBlockColors);
|
||||
|
|
@ -173,13 +176,22 @@ void ChatView::appendHtmlServerMessage(const QString &html, bool optionalIsBold,
|
|||
void ChatView::appendCardTag(QTextCursor &cursor, const QString &cardName)
|
||||
{
|
||||
QTextCharFormat oldFormat = cursor.charFormat();
|
||||
QTextCharFormat anchorFormat = oldFormat;
|
||||
anchorFormat.setForeground(linkColor);
|
||||
anchorFormat.setAnchor(true);
|
||||
anchorFormat.setAnchorHref("card://" + cardName);
|
||||
anchorFormat.setFontItalic(true);
|
||||
QTextCharFormat cardFormat = oldFormat;
|
||||
cardFormat.setFontItalic(true);
|
||||
|
||||
cursor.setCharFormat(anchorFormat);
|
||||
if (!CardDatabaseManager::query()->lookupCardByName(cardName)) {
|
||||
cardFormat.setForeground(unresolvedCardTagColor);
|
||||
cursor.setCharFormat(cardFormat);
|
||||
cursor.insertText(cardName);
|
||||
cursor.setCharFormat(oldFormat);
|
||||
return;
|
||||
}
|
||||
|
||||
cardFormat.setForeground(linkColor);
|
||||
cardFormat.setAnchor(true);
|
||||
cardFormat.setAnchorHref("card://" + cardName);
|
||||
|
||||
cursor.setCharFormat(cardFormat);
|
||||
cursor.insertText(cardName);
|
||||
cursor.setCharFormat(oldFormat);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,6 +81,7 @@ private:
|
|||
QColor otherUserColor = QColor(0, 65, 255); // dark blue
|
||||
QColor serverMessageColor = QColor(0x85, 0x15, 0x15);
|
||||
QColor linkColor;
|
||||
QColor unresolvedCardTagColor;
|
||||
|
||||
private slots:
|
||||
void openLink(const QUrl &link);
|
||||
|
|
|
|||
|
|
@ -10,18 +10,23 @@
|
|||
#include "games_model.h"
|
||||
#include "user/user_list_manager.h"
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QDebug>
|
||||
#include <QGuiApplication>
|
||||
#include <QHBoxLayout>
|
||||
#include <QHeaderView>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QTreeView>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <libcockatrice/network/client/abstract/abstract_client.h>
|
||||
#include <libcockatrice/protocol/pb/response.pb.h>
|
||||
#include <libcockatrice/protocol/pb/room_commands.pb.h>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
|
||||
GameSelector::GameSelector(AbstractClient *_client,
|
||||
TabSupervisor *_tabSupervisor,
|
||||
|
|
@ -79,12 +84,12 @@ GameSelector::GameSelector(AbstractClient *_client,
|
|||
if (showFilters && restoresettings) {
|
||||
quickFilterToolBar = new GameSelectorQuickFilterToolBar(this, tabSupervisor, gameListProxyModel, gameTypeMap);
|
||||
quickFilterToolBar->setVisible(showFilters && restoresettings &&
|
||||
SettingsCache::instance().cardsDisplay().getShowGameSelectorFilterToolbar());
|
||||
SettingsCache::instance().userInterface().getShowGameSelectorFilterToolbar());
|
||||
|
||||
connect(&SettingsCache::instance().cardsDisplay(), &CardsDisplaySettings::showGameSelectorFilterToolbarChanged,
|
||||
connect(&SettingsCache::instance().userInterface(), &InterfaceSettings::showGameSelectorFilterToolbarChanged,
|
||||
this, [this] {
|
||||
quickFilterToolBar->setVisible(
|
||||
SettingsCache::instance().cardsDisplay().getShowGameSelectorFilterToolbar());
|
||||
SettingsCache::instance().userInterface().getShowGameSelectorFilterToolbar());
|
||||
});
|
||||
} else {
|
||||
quickFilterToolBar = nullptr;
|
||||
|
|
@ -315,6 +320,21 @@ void GameSelector::customContextMenu(const QPoint &point)
|
|||
dlg.exec();
|
||||
});
|
||||
|
||||
QAction copyLink(tr("Copy Game Link"));
|
||||
connect(©Link, &QAction::triggered, this, [=, this]() {
|
||||
const ServerInfo_Game &gameInfo = gameListModel->getGame(index.data(Qt::UserRole).toInt());
|
||||
QUrl url;
|
||||
url.setScheme("cockatrice");
|
||||
url.setHost("joingame");
|
||||
QUrlQuery query;
|
||||
query.addQueryItem("hostname", client->serverName());
|
||||
query.addQueryItem("port", QString::number(client->serverPort()));
|
||||
query.addQueryItem("roomid", QString::number(gameInfo.room_id()));
|
||||
query.addQueryItem("gameid", QString::number(gameInfo.game_id()));
|
||||
url.setQuery(query);
|
||||
QGuiApplication::clipboard()->setText(url.toString(QUrl::FullyEncoded));
|
||||
});
|
||||
|
||||
QMenu menu;
|
||||
menu.addAction(&joinGame);
|
||||
|
||||
|
|
@ -332,6 +352,11 @@ void GameSelector::customContextMenu(const QPoint &point)
|
|||
|
||||
menu.addAction(&spectateGame);
|
||||
menu.addAction(&getGameInfo);
|
||||
|
||||
if (!client->serverName().isEmpty()) {
|
||||
menu.addAction(©Link);
|
||||
}
|
||||
|
||||
menu.exec(gameListView->mapToGlobal(point));
|
||||
}
|
||||
|
||||
|
|
@ -379,6 +404,24 @@ void GameSelector::joinGame(const bool asSpectator, const bool asJudge)
|
|||
disableButtons();
|
||||
}
|
||||
|
||||
bool GameSelector::joinGameById(int gameId)
|
||||
{
|
||||
auto *model = gameListView->model();
|
||||
|
||||
for (int row = 0; row < model->rowCount(); ++row) {
|
||||
QModelIndex idx = model->index(row, 0);
|
||||
const ServerInfo_Game &game = gameListModel->getGame(idx.data(Qt::UserRole).toInt());
|
||||
if (game.game_id() == gameId) {
|
||||
gameListView->setCurrentIndex(idx);
|
||||
joinGame();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
qWarning() << "Game" << gameId << "not found";
|
||||
return false;
|
||||
}
|
||||
|
||||
void GameSelector::disableButtons()
|
||||
{
|
||||
if (createButton) {
|
||||
|
|
|
|||
|
|
@ -202,6 +202,7 @@ public:
|
|||
* @param info The ServerInfo_Game object containing information about the game to update.
|
||||
*/
|
||||
void processGameInfo(const ServerInfo_Game &info);
|
||||
bool joinGameById(int gameId);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
#include <libcockatrice/protocol/pb/response_get_games_of_user.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_get_user_info.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/settings/appearance_settings.h>
|
||||
#include <libcockatrice/utility/string_limits.h>
|
||||
|
||||
BanDialog::BanDialog(const ServerInfo_User &info, QWidget *parent) : QDialog(parent)
|
||||
|
|
@ -349,7 +349,7 @@ bool UserListItemDelegate::editorEvent(QEvent *event,
|
|||
|
||||
QSize UserListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
if (!SettingsCache::instance().interface().getStyleUserList()) {
|
||||
if (!SettingsCache::instance().appearance().getStyleUserList()) {
|
||||
return QStyledItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
return UserListPainter::sizeHint();
|
||||
|
|
@ -357,7 +357,7 @@ QSize UserListItemDelegate::sizeHint(const QStyleOptionViewItem &option, const Q
|
|||
|
||||
void UserListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
if (!SettingsCache::instance().interface().getStyleUserList()) {
|
||||
if (!SettingsCache::instance().appearance().getStyleUserList()) {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
return;
|
||||
}
|
||||
|
|
@ -521,7 +521,7 @@ UserListWidget::UserListWidget(TabSupervisor *_tabSupervisor,
|
|||
|
||||
// Pin on item click
|
||||
connect(userTree, &QTreeWidget::itemClicked, this, [this](QTreeWidgetItem *item, int) {
|
||||
if (!SettingsCache::instance().interface().getStyleUserList()) {
|
||||
if (!SettingsCache::instance().appearance().getStyleUserList()) {
|
||||
return;
|
||||
}
|
||||
const QString name = static_cast<UserListTWI *>(item)->getUserInfo().name().c_str();
|
||||
|
|
@ -553,7 +553,7 @@ UserListWidget::UserListWidget(TabSupervisor *_tabSupervisor,
|
|||
connect(cardArtProvider, &UserCardArtProvider::cardArtUpdated, this,
|
||||
[this](const QString &) { userTree->viewport()->update(); });
|
||||
|
||||
connect(&SettingsCache::instance().interface(), &InterfaceSettings::styleUserListChanged, this,
|
||||
connect(&SettingsCache::instance().appearance(), &AppearanceSettings::styleUserListChanged, this,
|
||||
&UserListWidget::applyDisplayMode);
|
||||
applyDisplayMode();
|
||||
|
||||
|
|
@ -659,7 +659,7 @@ void UserListWidget::hideEvent(QHideEvent *e)
|
|||
|
||||
void UserListWidget::applyDisplayMode()
|
||||
{
|
||||
const bool styled = SettingsCache::instance().interface().getStyleUserList();
|
||||
const bool styled = SettingsCache::instance().appearance().getStyleUserList();
|
||||
|
||||
if (styled) {
|
||||
userTree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
|
||||
|
|
@ -718,7 +718,7 @@ bool UserListWidget::eventFilter(QObject *obj, QEvent *event)
|
|||
{
|
||||
if (obj == userTree->viewport()) {
|
||||
if (event->type() == QEvent::MouseMove) {
|
||||
if (!SettingsCache::instance().interface().getStyleUserList()) {
|
||||
if (!SettingsCache::instance().appearance().getStyleUserList()) {
|
||||
return QGroupBox::eventFilter(obj, event);
|
||||
}
|
||||
auto *me = static_cast<QMouseEvent *>(event);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,196 @@
|
|||
#include "abstract_settings_page.h"
|
||||
|
||||
#include "settings_search_model.h"
|
||||
|
||||
#include <QCheckBox>
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QPair>
|
||||
#include <QSpinBox>
|
||||
|
||||
/**
|
||||
* @brief Recursively collects all widgets within a layout
|
||||
* @param layout The layout to walk
|
||||
* @param widgets Output list of (widget, containing layout) pairs
|
||||
*/
|
||||
static void collectWidgets(QLayout *layout, QList<QPair<QWidget *, QLayout *>> &widgets)
|
||||
{
|
||||
for (int i = 0; i < layout->count(); ++i) {
|
||||
QLayoutItem *item = layout->itemAt(i);
|
||||
if (!item) {
|
||||
continue;
|
||||
}
|
||||
if (QWidget *widget = item->widget()) {
|
||||
widgets.append({widget, layout});
|
||||
} else if (QLayout *subLayout = item->layout()) {
|
||||
collectWidgets(subLayout, widgets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rejects QLabels that are not setting names
|
||||
*
|
||||
* HTML link labels, path values, and excessively long labels are filtered out.
|
||||
*/
|
||||
static bool isValidSettingLabel(const QLabel *label)
|
||||
{
|
||||
const QString &text = label->text();
|
||||
if (Qt::mightBeRichText(text)) {
|
||||
return false;
|
||||
}
|
||||
if (text.contains(QLatin1Char('/')) || text.contains(QLatin1Char('\\'))) {
|
||||
return false;
|
||||
}
|
||||
if (text.size() > 60) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Finds the control associated with a setting label
|
||||
*
|
||||
* Uses the explicit buddy if set, otherwise the widget in the cell (or slot)
|
||||
* immediately following the label within the same layout. Returns nullptr when
|
||||
* no obvious control is found.
|
||||
*/
|
||||
static QWidget *controlForLabel(QLabel *label, QLayout *containingLayout)
|
||||
{
|
||||
if (QWidget *buddy = label->buddy()) {
|
||||
return buddy;
|
||||
}
|
||||
|
||||
if (auto *grid = qobject_cast<QGridLayout *>(containingLayout)) {
|
||||
int index = grid->indexOf(label);
|
||||
if (index != -1) {
|
||||
int row = 0;
|
||||
int column = 0;
|
||||
int rowSpan = 1;
|
||||
int columnSpan = 1;
|
||||
grid->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
|
||||
if (QLayoutItem *next = grid->itemAtPosition(row, column + columnSpan)) {
|
||||
if (QWidget *nextWidget = next->widget()) {
|
||||
return nextWidget;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int index = containingLayout->indexOf(label);
|
||||
if (index != -1) {
|
||||
for (int i = index + 1; i < containingLayout->count(); ++i) {
|
||||
if (QLayoutItem *next = containingLayout->itemAt(i)) {
|
||||
if (QWidget *nextWidget = next->widget()) {
|
||||
return nextWidget;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Builds the extended search text for an entry
|
||||
*
|
||||
* Combines the group title, label, and any extra searchable text derived from
|
||||
* the associated control (placeholder, prefix/suffix, tooltip). Combo values
|
||||
* and numeric tooltips are excluded since they change at runtime and would
|
||||
* make the search index stale.
|
||||
*/
|
||||
static QString buildFullSearchText(const QString &groupTitle, const QString &cleanLabel, QWidget *control)
|
||||
{
|
||||
QStringList parts = {groupTitle, cleanLabel};
|
||||
if (control) {
|
||||
if (auto *lineEdit = qobject_cast<QLineEdit *>(control)) {
|
||||
parts.append(lineEdit->placeholderText());
|
||||
} else if (auto *spinBox = qobject_cast<QSpinBox *>(control)) {
|
||||
parts.append(spinBox->prefix());
|
||||
parts.append(spinBox->suffix());
|
||||
}
|
||||
if (!control->toolTip().isEmpty()) {
|
||||
bool isNumeric = false;
|
||||
control->toolTip().toInt(&isNumeric);
|
||||
if (!isNumeric) {
|
||||
parts.append(control->toolTip());
|
||||
}
|
||||
}
|
||||
}
|
||||
parts.removeAll(QString());
|
||||
return parts.join(QLatin1Char(' '));
|
||||
}
|
||||
|
||||
QList<SettingsSearchEntry> AbstractSettingsPage::getSearchEntries()
|
||||
{
|
||||
return autoDetectSearchEntries(this, -1);
|
||||
}
|
||||
|
||||
QList<SettingsSearchEntry> AbstractSettingsPage::autoDetectSearchEntries(QWidget *page, int pageIndex)
|
||||
{
|
||||
QList<SettingsSearchEntry> entries;
|
||||
|
||||
const auto children = page->children();
|
||||
for (QObject *child : children) {
|
||||
auto *groupBox = qobject_cast<QGroupBox *>(child);
|
||||
if (!groupBox) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString groupTitle = groupBox->title();
|
||||
if (groupTitle.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QLayout *groupLayout = groupBox->layout();
|
||||
if (!groupLayout) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QList<QPair<QWidget *, QLayout *>> widgets;
|
||||
collectWidgets(groupLayout, widgets);
|
||||
|
||||
for (const auto &pair : widgets) {
|
||||
QWidget *widget = pair.first;
|
||||
QString label;
|
||||
|
||||
auto *checkBox = qobject_cast<QCheckBox *>(widget);
|
||||
if (checkBox) {
|
||||
label = checkBox->text();
|
||||
} else {
|
||||
auto *labelWidget = qobject_cast<QLabel *>(widget);
|
||||
if (!labelWidget || labelWidget->text().isEmpty() || !isValidSettingLabel(labelWidget)) {
|
||||
continue;
|
||||
}
|
||||
label = labelWidget->text();
|
||||
}
|
||||
|
||||
if (label.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Strip accelerator markers (&) for search
|
||||
QString cleanLabel = label;
|
||||
cleanLabel.remove(QLatin1Char('&'));
|
||||
|
||||
QWidget *control = widget;
|
||||
if (auto *labelWidget = qobject_cast<QLabel *>(widget)) {
|
||||
control = controlForLabel(labelWidget, pair.second);
|
||||
if (!control) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
entries.append(SettingsSearchEntry{.pageIndex = pageIndex,
|
||||
.groupTitle = groupTitle,
|
||||
.widgetLabel = cleanLabel,
|
||||
.fullSearchText = buildFullSearchText(groupTitle, cleanLabel, control),
|
||||
.widget = control});
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
|
@ -1,16 +1,24 @@
|
|||
#ifndef COCKATRICE_ABSTRACT_SETTINGS_PAGE_H
|
||||
#define COCKATRICE_ABSTRACT_SETTINGS_PAGE_H
|
||||
|
||||
#include <QList>
|
||||
#include <QWidget>
|
||||
|
||||
#define WIKI_CUSTOM_PIC_URL "https://github.com/Cockatrice/Cockatrice/wiki/Custom-Picture-Download-URLs"
|
||||
#define WIKI_CUSTOM_SHORTCUTS "https://github.com/Cockatrice/Cockatrice/wiki/Custom-Keyboard-Shortcuts"
|
||||
#define WIKI_TRANSLATION_FAQ "https://github.com/Cockatrice/Cockatrice/wiki/Translation-FAQ"
|
||||
|
||||
struct SettingsSearchEntry;
|
||||
|
||||
class AbstractSettingsPage : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
virtual void retranslateUi() = 0;
|
||||
virtual QList<SettingsSearchEntry> getSearchEntries();
|
||||
|
||||
protected:
|
||||
static QList<SettingsSearchEntry> autoDetectSearchEntries(QWidget *page, int pageIndex);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_ABSTRACT_SETTINGS_PAGE_H
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
#include <QMessageBox>
|
||||
#include <QStyleFactory>
|
||||
#include <QTimer>
|
||||
#include <libcockatrice/settings/appearance_settings.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
|
|
@ -104,7 +105,7 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
homeTabBackgroundSourceBox.addItem(QObject::tr(entry.trKey), QVariant::fromValue(entry.type));
|
||||
}
|
||||
|
||||
QString homeTabBackgroundSource = SettingsCache::instance().personal().getHomeTabBackgroundSource();
|
||||
QString homeTabBackgroundSource = settings.appearance().getHomeTabBackgroundSource();
|
||||
int homeTabBackgroundSourceId =
|
||||
homeTabBackgroundSourceBox.findData(BackgroundSources::fromId(homeTabBackgroundSource));
|
||||
if (homeTabBackgroundSourceId != -1) {
|
||||
|
|
@ -113,20 +114,19 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
|
||||
connect(&homeTabBackgroundSourceBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this]() {
|
||||
auto type = homeTabBackgroundSourceBox.currentData().value<BackgroundSources::Type>();
|
||||
SettingsCache::instance().personal().setHomeTabBackgroundSource(BackgroundSources::toId(type));
|
||||
SettingsCache::instance().appearance().setHomeTabBackgroundSource(BackgroundSources::toId(type));
|
||||
updateHomeTabSettingsVisibility();
|
||||
});
|
||||
|
||||
homeTabBackgroundShuffleFrequencySpinBox.setRange(0, 3600);
|
||||
homeTabBackgroundShuffleFrequencySpinBox.setSuffix(tr(" seconds"));
|
||||
homeTabBackgroundShuffleFrequencySpinBox.setValue(
|
||||
SettingsCache::instance().personal().getHomeTabBackgroundShuffleFrequency());
|
||||
connect(&homeTabBackgroundShuffleFrequencySpinBox, qOverload<int>(&QSpinBox::valueChanged), &settings.personal(),
|
||||
&PersonalSettings::setHomeTabBackgroundShuffleFrequency);
|
||||
homeTabBackgroundShuffleFrequencySpinBox.setValue(settings.appearance().getHomeTabBackgroundShuffleFrequency());
|
||||
connect(&homeTabBackgroundShuffleFrequencySpinBox, qOverload<int>(&QSpinBox::valueChanged), &settings.appearance(),
|
||||
&AppearanceSettings::setHomeTabBackgroundShuffleFrequency);
|
||||
|
||||
homeTabDisplayCardNameCheckBox.setChecked(settings.personal().getHomeTabDisplayCardName());
|
||||
connect(&homeTabDisplayCardNameCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.personal(),
|
||||
&PersonalSettings::setHomeTabDisplayCardName);
|
||||
homeTabDisplayCardNameCheckBox.setChecked(settings.appearance().getHomeTabDisplayCardName());
|
||||
connect(&homeTabDisplayCardNameCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.appearance(),
|
||||
&AppearanceSettings::setHomeTabDisplayCardName);
|
||||
|
||||
updateHomeTabSettingsVisibility();
|
||||
|
||||
|
|
@ -140,9 +140,9 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
homeTabGroupBox = new QGroupBox;
|
||||
homeTabGroupBox->setLayout(homeTabGrid);
|
||||
|
||||
styleUserListCheckBox.setChecked(settings.interface().getStyleUserList());
|
||||
connect(&styleUserListCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.interface(),
|
||||
&InterfaceSettings::setStyleUserList);
|
||||
styleUserListCheckBox.setChecked(settings.appearance().getStyleUserList());
|
||||
connect(&styleUserListCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.appearance(),
|
||||
&AppearanceSettings::setStyleUserList);
|
||||
|
||||
auto stylingTabGrid = new QGridLayout;
|
||||
stylingTabGrid->addWidget(&styleUserListCheckBox, 0, 0, 1, 2);
|
||||
|
|
@ -151,12 +151,12 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
stylingGroupBox->setLayout(stylingTabGrid);
|
||||
|
||||
// Menu settings
|
||||
showShortcutsCheckBox.setChecked(settings.cardsDisplay().getShowShortcuts());
|
||||
showShortcutsCheckBox.setChecked(settings.userInterface().getShowShortcuts());
|
||||
connect(&showShortcutsCheckBox, &QCheckBox::QT_STATE_CHANGED, this, &AppearanceSettingsPage::showShortcutsChanged);
|
||||
|
||||
showGameSelectorFilterToolbarCheckBox.setChecked(settings.cardsDisplay().getShowGameSelectorFilterToolbar());
|
||||
connect(&showGameSelectorFilterToolbarCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.cardsDisplay(),
|
||||
&CardsDisplaySettings::setShowGameSelectorFilterToolbar);
|
||||
showGameSelectorFilterToolbarCheckBox.setChecked(settings.userInterface().getShowGameSelectorFilterToolbar());
|
||||
connect(&showGameSelectorFilterToolbarCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.userInterface(),
|
||||
&InterfaceSettings::setShowGameSelectorFilterToolbar);
|
||||
|
||||
auto *menuGrid = new QGridLayout;
|
||||
menuGrid->addWidget(&showShortcutsCheckBox, 0, 0);
|
||||
|
|
@ -199,9 +199,9 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
connect(&roundCardCornersCheckBox, &QAbstractButton::toggled, &settings.cardsDisplay(),
|
||||
&CardsDisplaySettings::setRoundCardCorners);
|
||||
|
||||
connect(&maxFontSizeForCardsEdit, qOverload<int>(&QSpinBox::valueChanged), &settings.personal(),
|
||||
&PersonalSettings::setMaxFontSize);
|
||||
maxFontSizeForCardsEdit.setValue(settings.personal().getMaxFontSize());
|
||||
connect(&maxFontSizeForCardsEdit, qOverload<int>(&QSpinBox::valueChanged), &settings.appearance(),
|
||||
&AppearanceSettings::setMaxFontSize);
|
||||
maxFontSizeForCardsEdit.setValue(settings.appearance().getMaxFontSize());
|
||||
maxFontSizeForCardsLabel.setBuddy(&maxFontSizeForCardsEdit);
|
||||
maxFontSizeForCardsEdit.setMinimum(9);
|
||||
maxFontSizeForCardsEdit.setMaximum(100);
|
||||
|
|
@ -224,12 +224,12 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
&CardsDisplaySettings::setStackCardOverlapPercent);
|
||||
|
||||
cardViewInitialRowsMaxBox.setRange(1, 999);
|
||||
cardViewInitialRowsMaxBox.setValue(SettingsCache::instance().interface().getCardViewInitialRowsMax());
|
||||
cardViewInitialRowsMaxBox.setValue(SettingsCache::instance().userInterface().getCardViewInitialRowsMax());
|
||||
connect(&cardViewInitialRowsMaxBox, qOverload<int>(&QSpinBox::valueChanged), this,
|
||||
&AppearanceSettingsPage::cardViewInitialRowsMaxChanged);
|
||||
|
||||
cardViewExpandedRowsMaxBox.setRange(1, 999);
|
||||
cardViewExpandedRowsMaxBox.setValue(SettingsCache::instance().interface().getCardViewExpandedRowsMax());
|
||||
cardViewExpandedRowsMaxBox.setValue(SettingsCache::instance().userInterface().getCardViewExpandedRowsMax());
|
||||
connect(&cardViewExpandedRowsMaxBox, qOverload<int>(&QSpinBox::valueChanged), this,
|
||||
&AppearanceSettingsPage::cardViewExpandedRowsMaxChanged);
|
||||
|
||||
|
|
@ -291,12 +291,12 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
cardCountersGroupBox->setLayout(cardCountersLayout);
|
||||
|
||||
// Hand layout
|
||||
horizontalHandCheckBox.setChecked(settings.interface().getHorizontalHand());
|
||||
connect(&horizontalHandCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.interface(),
|
||||
horizontalHandCheckBox.setChecked(settings.userInterface().getHorizontalHand());
|
||||
connect(&horizontalHandCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.userInterface(),
|
||||
&InterfaceSettings::setHorizontalHand);
|
||||
|
||||
leftJustifiedHandCheckBox.setChecked(settings.interface().getLeftJustified());
|
||||
connect(&leftJustifiedHandCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.interface(),
|
||||
leftJustifiedHandCheckBox.setChecked(settings.userInterface().getLeftJustified());
|
||||
connect(&leftJustifiedHandCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.userInterface(),
|
||||
&InterfaceSettings::setLeftJustified);
|
||||
|
||||
auto *handGrid = new QGridLayout;
|
||||
|
|
@ -307,13 +307,13 @@ AppearanceSettingsPage::AppearanceSettingsPage()
|
|||
handGroupBox->setLayout(handGrid);
|
||||
|
||||
// table grid layout
|
||||
invertVerticalCoordinateCheckBox.setChecked(settings.interface().getInvertVerticalCoordinate());
|
||||
connect(&invertVerticalCoordinateCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.interface(),
|
||||
invertVerticalCoordinateCheckBox.setChecked(settings.userInterface().getInvertVerticalCoordinate());
|
||||
connect(&invertVerticalCoordinateCheckBox, &QCheckBox::QT_STATE_CHANGED, &settings.userInterface(),
|
||||
&InterfaceSettings::setInvertVerticalCoordinate);
|
||||
|
||||
minPlayersForMultiColumnLayoutEdit.setMinimum(2);
|
||||
minPlayersForMultiColumnLayoutEdit.setValue(settings.interface().getMinPlayersForMultiColumnLayout());
|
||||
connect(&minPlayersForMultiColumnLayoutEdit, qOverload<int>(&QSpinBox::valueChanged), &settings.interface(),
|
||||
minPlayersForMultiColumnLayoutEdit.setValue(settings.userInterface().getMinPlayersForMultiColumnLayout());
|
||||
connect(&minPlayersForMultiColumnLayoutEdit, qOverload<int>(&QSpinBox::valueChanged), &settings.userInterface(),
|
||||
&InterfaceSettings::setMinPlayersForMultiColumnLayout);
|
||||
minPlayersForMultiColumnLayoutLabel.setBuddy(&minPlayersForMultiColumnLayoutEdit);
|
||||
|
||||
|
|
@ -375,7 +375,7 @@ void AppearanceSettingsPage::editPalette()
|
|||
|
||||
void AppearanceSettingsPage::updateHomeTabSettingsVisibility()
|
||||
{
|
||||
bool visible = SettingsCache::instance().personal().getHomeTabBackgroundSource() !=
|
||||
bool visible = SettingsCache::instance().appearance().getHomeTabBackgroundSource() !=
|
||||
BackgroundSources::toId(BackgroundSources::Theme);
|
||||
|
||||
homeTabBackgroundShuffleFrequencyLabel.setVisible(visible);
|
||||
|
|
@ -385,7 +385,7 @@ void AppearanceSettingsPage::updateHomeTabSettingsVisibility()
|
|||
|
||||
void AppearanceSettingsPage::showShortcutsChanged(QT_STATE_CHANGED_T value)
|
||||
{
|
||||
SettingsCache::instance().cardsDisplay().setShowShortcuts(value);
|
||||
SettingsCache::instance().userInterface().setShowShortcuts(value);
|
||||
qApp->setAttribute(Qt::AA_DontShowShortcutsInContextMenus, value == 0); // 0 = unchecked
|
||||
}
|
||||
|
||||
|
|
@ -412,7 +412,7 @@ void AppearanceSettingsPage::overrideAllCardArtWithPersonalPreferenceToggled(QT_
|
|||
*/
|
||||
void AppearanceSettingsPage::cardViewInitialRowsMaxChanged(int value)
|
||||
{
|
||||
SettingsCache::instance().interface().setCardViewInitialRowsMax(value);
|
||||
SettingsCache::instance().userInterface().setCardViewInitialRowsMax(value);
|
||||
if (cardViewExpandedRowsMaxBox.value() < value) {
|
||||
cardViewExpandedRowsMaxBox.setValue(value);
|
||||
}
|
||||
|
|
@ -425,7 +425,7 @@ void AppearanceSettingsPage::cardViewInitialRowsMaxChanged(int value)
|
|||
*/
|
||||
void AppearanceSettingsPage::cardViewExpandedRowsMaxChanged(int value)
|
||||
{
|
||||
SettingsCache::instance().interface().setCardViewExpandedRowsMax(value);
|
||||
SettingsCache::instance().userInterface().setCardViewExpandedRowsMax(value);
|
||||
if (cardViewInitialRowsMaxBox.value() > value) {
|
||||
cardViewInitialRowsMaxBox.setValue(value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,10 +40,10 @@ private:
|
|||
QSpinBox homeTabBackgroundShuffleFrequencySpinBox;
|
||||
QCheckBox homeTabDisplayCardNameCheckBox;
|
||||
QCheckBox styleUserListCheckBox;
|
||||
QLabel minPlayersForMultiColumnLayoutLabel;
|
||||
QLabel maxFontSizeForCardsLabel;
|
||||
QCheckBox showShortcutsCheckBox;
|
||||
QCheckBox showGameSelectorFilterToolbarCheckBox;
|
||||
QLabel minPlayersForMultiColumnLayoutLabel;
|
||||
QLabel maxFontSizeForCardsLabel;
|
||||
QCheckBox overrideAllCardArtWithPersonalPreferenceCheckBox;
|
||||
QCheckBox bumpSetsWithCardsInDeckToTopCheckBox;
|
||||
QCheckBox displayCardNamesCheckBox;
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@
|
|||
|
||||
DeckEditorSettingsPage::DeckEditorSettingsPage()
|
||||
{
|
||||
picDownloadCheckBox.setChecked(SettingsCache::instance().personal().getPicDownload());
|
||||
connect(&picDownloadCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().personal(),
|
||||
&PersonalSettings::setPicDownload);
|
||||
picDownloadCheckBox.setChecked(SettingsCache::instance().downloads().getPicDownload());
|
||||
connect(&picDownloadCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().downloads(),
|
||||
&DownloadSettings::setPicDownload);
|
||||
|
||||
urlLinkLabel.setTextInteractionFlags(Qt::LinksAccessibleByMouse);
|
||||
urlLinkLabel.setOpenExternalLinks(true);
|
||||
|
|
@ -29,7 +29,7 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
auto *lpGeneralGrid = new QGridLayout;
|
||||
auto *lpSpoilerGrid = new QGridLayout;
|
||||
|
||||
mcDownloadSpoilersCheckBox.setChecked(SettingsCache::instance().personal().getDownloadSpoilersStatus());
|
||||
mcDownloadSpoilersCheckBox.setChecked(SettingsCache::instance().downloads().getDownloadSpoilersStatus());
|
||||
|
||||
mpSpoilerSavePathLineEdit = new QLineEdit(SettingsCache::instance().getSpoilerCardDatabasePath());
|
||||
mpSpoilerSavePathLineEdit->setReadOnly(true);
|
||||
|
|
@ -91,8 +91,8 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
lpSpoilerGrid->addWidget(&infoOnSpoilersLabel, 3, 0, 1, 3, Qt::AlignTop);
|
||||
|
||||
// On a change to the checkbox, hide/un-hide the other fields
|
||||
connect(&mcDownloadSpoilersCheckBox, &QCheckBox::toggled, &SettingsCache::instance().personal(),
|
||||
&PersonalSettings::setDownloadSpoilerStatus);
|
||||
connect(&mcDownloadSpoilersCheckBox, &QCheckBox::toggled, &SettingsCache::instance().downloads(),
|
||||
&DownloadSettings::setDownloadSpoilerStatus);
|
||||
connect(&mcDownloadSpoilersCheckBox, &QCheckBox::toggled, this, &DeckEditorSettingsPage::setSpoilersEnabled);
|
||||
|
||||
mpGeneralGroupBox = new QGroupBox;
|
||||
|
|
|
|||
|
|
@ -90,6 +90,7 @@ MessagesSettingsPage::MessagesSettingsPage()
|
|||
highlightNotice->addWidget(&hexHighlightLabel, 1, 2);
|
||||
highlightNotice->addWidget(customAlertString, 0, 0);
|
||||
highlightNotice->addWidget(&customAlertStringLabel, 1, 0);
|
||||
customAlertStringLabel.setBuddy(customAlertString);
|
||||
highlightGroupBox = new QGroupBox;
|
||||
highlightGroupBox->setLayout(highlightNotice);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,125 @@
|
|||
/**
|
||||
* @file settings_search_delegate.cpp
|
||||
* @brief Implementation of the custom settings search result delegate
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
#include "settings_search_delegate.h"
|
||||
|
||||
#include "settings_search_model.h"
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
SettingsSearchDelegate::SettingsSearchDelegate(QObject *parent) : QStyledItemDelegate(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void SettingsSearchDelegate::setPageNames(const QStringList &names)
|
||||
{
|
||||
pageNames = names;
|
||||
}
|
||||
|
||||
void SettingsSearchDelegate::setPageIcons(const QStringList &iconResources)
|
||||
{
|
||||
pageIcons.clear();
|
||||
for (const QString &resource : iconResources) {
|
||||
pageIcons.append(QPixmap(resource));
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsSearchDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
painter->save();
|
||||
|
||||
SettingsSearchEntry entry = index.data(SettingsSearchModel::EntryRole).value<SettingsSearchEntry>();
|
||||
|
||||
bool isSelected = option.state & QStyle::State_Selected;
|
||||
bool isHovered = option.state & QStyle::State_MouseOver;
|
||||
|
||||
// Background
|
||||
QColor bgColor = isSelected ? option.palette.color(QPalette::Highlight)
|
||||
: isHovered ? option.palette.color(QPalette::Midlight)
|
||||
: option.palette.color(QPalette::Base);
|
||||
painter->fillRect(option.rect, bgColor);
|
||||
|
||||
if (isSelected) {
|
||||
// Accent bar on the left to make the selection unmistakable
|
||||
painter->fillRect(QRect(option.rect.left(), option.rect.top(), 4, option.rect.height()),
|
||||
option.palette.color(QPalette::Highlight).darker(150));
|
||||
}
|
||||
|
||||
int leftMargin = 12;
|
||||
int topMargin = 8;
|
||||
int rightMargin = 12;
|
||||
int bottomMargin = 4;
|
||||
|
||||
QRect contentRect = option.rect.adjusted(leftMargin, topMargin, -rightMargin, -bottomMargin);
|
||||
int yPos = contentRect.top();
|
||||
|
||||
// Icon of the related settings page
|
||||
const int iconSize = 24;
|
||||
QPixmap pageIcon =
|
||||
(entry.pageIndex >= 0 && entry.pageIndex < pageIcons.size()) ? pageIcons.at(entry.pageIndex) : QPixmap();
|
||||
int iconOffset = pageIcon.isNull() ? 0 : iconSize + 8;
|
||||
if (!pageIcon.isNull()) {
|
||||
QRect iconRect(contentRect.left(), contentRect.top() + (contentRect.height() - iconSize) / 2, iconSize,
|
||||
iconSize);
|
||||
painter->drawPixmap(iconRect, pageIcon);
|
||||
}
|
||||
|
||||
QRect textRect = contentRect.adjusted(iconOffset, 0, 0, 0);
|
||||
|
||||
// Breadcrumb: "Page > Group"
|
||||
QFont breadcrumbFont = option.font;
|
||||
breadcrumbFont.setPointSize(breadcrumbFont.pointSize() - 1);
|
||||
breadcrumbFont.setBold(true);
|
||||
|
||||
QColor breadcrumbColor =
|
||||
isSelected ? option.palette.color(QPalette::HighlightedText) : option.palette.color(QPalette::Text);
|
||||
if (!isSelected) {
|
||||
breadcrumbColor.setAlpha(180);
|
||||
}
|
||||
|
||||
QString pageName;
|
||||
if (entry.pageIndex >= 0 && entry.pageIndex < pageNames.size()) {
|
||||
pageName = pageNames[entry.pageIndex];
|
||||
} else {
|
||||
pageName = QString::number(entry.pageIndex);
|
||||
}
|
||||
|
||||
QString breadcrumbText = QStringLiteral("%1 > %2").arg(pageName, entry.groupTitle);
|
||||
painter->setFont(breadcrumbFont);
|
||||
painter->setPen(breadcrumbColor);
|
||||
painter->drawText(QRect(textRect.left(), yPos, textRect.width(), 20), Qt::AlignLeft | Qt::AlignVCenter,
|
||||
breadcrumbText);
|
||||
yPos += 20;
|
||||
|
||||
// Setting label
|
||||
QFont labelFont = option.font;
|
||||
labelFont.setPointSize(labelFont.pointSize() + 1);
|
||||
labelFont.setBold(isSelected);
|
||||
|
||||
QColor labelColor =
|
||||
isSelected ? option.palette.color(QPalette::HighlightedText) : option.palette.color(QPalette::Text);
|
||||
|
||||
painter->setFont(labelFont);
|
||||
painter->setPen(labelColor);
|
||||
painter->drawText(QRect(textRect.left(), yPos, textRect.width(), 24), Qt::AlignLeft | Qt::AlignVCenter,
|
||||
entry.widgetLabel);
|
||||
yPos += 24;
|
||||
|
||||
// Bottom separator
|
||||
QPen separatorPen(option.palette.color(QPalette::Mid), 1);
|
||||
painter->setPen(separatorPen);
|
||||
painter->drawLine(option.rect.left() + leftMargin, option.rect.bottom(), option.rect.right() - rightMargin,
|
||||
option.rect.bottom());
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
QSize SettingsSearchDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
Q_UNUSED(index);
|
||||
return QSize(option.rect.width(), 56);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
* @file settings_search_delegate.h
|
||||
* @brief Custom delegate for rendering settings search results
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
#ifndef COCKATRICE_SETTINGS_SEARCH_DELEGATE_H
|
||||
#define COCKATRICE_SETTINGS_SEARCH_DELEGATE_H
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
/**
|
||||
* @brief Custom paint delegate for settings search result items
|
||||
*
|
||||
* Renders each search result with a breadcrumb line ("Page > Group"),
|
||||
* the setting label, and a subtle separator. Supports selected/hovered states.
|
||||
*/
|
||||
class SettingsSearchDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SettingsSearchDelegate(QObject *parent = nullptr);
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
|
||||
/** @brief Sets the translated page names for breadcrumb display */
|
||||
void setPageNames(const QStringList &names);
|
||||
|
||||
/** @brief Sets the icons shown in front of results, indexed by page position */
|
||||
void setPageIcons(const QStringList &iconResources);
|
||||
|
||||
private:
|
||||
QStringList pageNames; ///< Translated page names indexed by page position
|
||||
QList<QPixmap> pageIcons; ///< Icons of the related settings pages
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_SETTINGS_SEARCH_DELEGATE_H
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
/**
|
||||
* @file settings_search_model.cpp
|
||||
* @brief Implementation of the settings search list model
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
#include "settings_search_model.h"
|
||||
|
||||
#include <QPair>
|
||||
#include <algorithm>
|
||||
|
||||
SettingsSearchModel::SettingsSearchModel(QObject *parent) : QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void SettingsSearchModel::setSourceEntries(const QList<SettingsSearchEntry> &entries)
|
||||
{
|
||||
beginResetModel();
|
||||
sourceEntries = entries;
|
||||
endResetModel();
|
||||
rebuildFilter();
|
||||
}
|
||||
|
||||
void SettingsSearchModel::setFilterString(const QString &text)
|
||||
{
|
||||
filterActive = !text.trimmed().isEmpty();
|
||||
if (filterActive) {
|
||||
filterQuery = text.trimmed();
|
||||
filterRegex =
|
||||
QRegularExpression(QRegularExpression::escape(filterQuery), QRegularExpression::CaseInsensitiveOption);
|
||||
}
|
||||
rebuildFilter();
|
||||
}
|
||||
|
||||
bool SettingsSearchModel::isFilterActive() const
|
||||
{
|
||||
return filterActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculates a relevance score for a single entry against the query
|
||||
*
|
||||
* Scoring priorities (highest to lowest):
|
||||
* 1. Label starts with query -> 100
|
||||
* 2. Label contains query -> 80
|
||||
* 3. Group title starts with query -> 60
|
||||
* 4. Group title contains query -> 40
|
||||
* 5. Full text regex match -> 20
|
||||
* 6. No match -> 0 (excluded from results)
|
||||
*/
|
||||
static int relevanceScore(const SettingsSearchEntry &entry, const QString &query, const QRegularExpression ®ex)
|
||||
{
|
||||
QString lowerQuery = query.toLower();
|
||||
|
||||
// Label matches are most relevant
|
||||
QString label = entry.widgetLabel.toLower();
|
||||
if (label.startsWith(lowerQuery)) {
|
||||
return 100;
|
||||
}
|
||||
if (label.contains(lowerQuery)) {
|
||||
return 80;
|
||||
}
|
||||
|
||||
// Group title matches are next
|
||||
QString group = entry.groupTitle.toLower();
|
||||
if (group.startsWith(lowerQuery)) {
|
||||
return 60;
|
||||
}
|
||||
if (group.contains(lowerQuery)) {
|
||||
return 40;
|
||||
}
|
||||
|
||||
// Full text match is least relevant
|
||||
if (entry.fullSearchText.contains(regex)) {
|
||||
return 20;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SettingsSearchModel::rebuildFilter()
|
||||
{
|
||||
beginResetModel();
|
||||
filteredIndices.clear();
|
||||
|
||||
if (!filterActive) {
|
||||
for (int i = 0; i < sourceEntries.size(); ++i) {
|
||||
filteredIndices.append(i);
|
||||
}
|
||||
} else {
|
||||
QList<QPair<int, int>> scored; // <score, index>
|
||||
for (int i = 0; i < sourceEntries.size(); ++i) {
|
||||
const SettingsSearchEntry &entry = sourceEntries[i];
|
||||
// Skip conditional settings that are currently disabled or hidden
|
||||
if (entry.widget && (!entry.widget->isEnabled() || entry.widget->isHidden())) {
|
||||
continue;
|
||||
}
|
||||
int score = relevanceScore(entry, filterQuery, filterRegex);
|
||||
if (score > 0) {
|
||||
scored.append({-score, i}); // negative for descending sort
|
||||
}
|
||||
}
|
||||
std::sort(scored.begin(), scored.end());
|
||||
for (const auto &pair : scored) {
|
||||
filteredIndices.append(pair.second);
|
||||
}
|
||||
}
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
int SettingsSearchModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return filteredIndices.size();
|
||||
}
|
||||
|
||||
QVariant SettingsSearchModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() >= filteredIndices.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const SettingsSearchEntry &entry = sourceEntries[filteredIndices[index.row()]];
|
||||
|
||||
switch (role) {
|
||||
case EntryRole:
|
||||
return QVariant::fromValue(entry);
|
||||
case Qt::DisplayRole:
|
||||
return entry.widgetLabel;
|
||||
case Qt::ToolTipRole:
|
||||
return QStringLiteral("%1 > %2 > %3")
|
||||
.arg(QString::number(entry.pageIndex), entry.groupTitle, entry.widgetLabel);
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
SettingsSearchEntry SettingsSearchModel::entryForIndex(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid() || index.row() >= filteredIndices.size()) {
|
||||
return {};
|
||||
}
|
||||
return sourceEntries[filteredIndices[index.row()]];
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* @file settings_search_model.h
|
||||
* @brief Data model for the settings search feature
|
||||
* @ingroup Dialogs
|
||||
*/
|
||||
#ifndef COCKATRICE_SETTINGS_SEARCH_MODEL_H
|
||||
#define COCKATRICE_SETTINGS_SEARCH_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QList>
|
||||
#include <QRegularExpression>
|
||||
#include <QWidget>
|
||||
|
||||
/**
|
||||
* @brief Represents a single searchable setting entry
|
||||
*
|
||||
* Each settings page provides a list of these entries via getSearchEntries().
|
||||
* The model uses them for filtering, relevance scoring, and display.
|
||||
*/
|
||||
struct SettingsSearchEntry
|
||||
{
|
||||
int pageIndex; ///< Index of the settings page this entry belongs to
|
||||
QString groupTitle; ///< Title of the group/section within the page
|
||||
QString widgetLabel; ///< Display label for the setting widget
|
||||
QString fullSearchText; ///< Extended search text (label, control text, tooltip) for full-text matching
|
||||
QWidget *widget; ///< Pointer to the setting widget for focus/scrolling
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief List model providing filtered, ranked search results
|
||||
*
|
||||
* Manages a list of SettingsSearchEntry items. When a filter string is set,
|
||||
* entries are scored by relevance and sorted so the best matches appear first.
|
||||
* Supports custom roles for accessing entry fields from views and delegates.
|
||||
*/
|
||||
class SettingsSearchModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
* @brief Custom data role for accessing the full entry
|
||||
*/
|
||||
enum Roles
|
||||
{
|
||||
EntryRole = Qt::UserRole + 1, ///< Full SettingsSearchEntry object
|
||||
};
|
||||
|
||||
explicit SettingsSearchModel(QObject *parent = nullptr);
|
||||
|
||||
/** @brief Replaces the source entries and rebuilds the filter */
|
||||
void setSourceEntries(const QList<SettingsSearchEntry> &entries);
|
||||
/** @brief Sets the filter string and recalculates the results */
|
||||
void setFilterString(const QString &text);
|
||||
/** @brief Whether a non-empty filter is currently active */
|
||||
bool isFilterActive() const;
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
/** @brief Returns the full entry for a given model index */
|
||||
SettingsSearchEntry entryForIndex(const QModelIndex &index) const;
|
||||
|
||||
private:
|
||||
QList<SettingsSearchEntry> sourceEntries; ///< Complete unfiltered entry list
|
||||
QList<int> filteredIndices; ///< Indices into sourceEntries matching the filter
|
||||
QRegularExpression filterRegex; ///< Compiled regex for the current filter
|
||||
QString filterQuery; ///< Current filter query string
|
||||
bool filterActive = false; ///< Whether filtering is active
|
||||
|
||||
/** @brief Recalculates the filtered index list and ranking */
|
||||
void rebuildFilter();
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(SettingsSearchEntry)
|
||||
|
||||
#endif // COCKATRICE_SETTINGS_SEARCH_MODEL_H
|
||||
|
|
@ -123,6 +123,7 @@ void ShortcutSettingsPage::retranslateUi()
|
|||
currentActionGroupLabel->setText(tr("Section:"));
|
||||
currentActionLabel->setText(tr("Action:"));
|
||||
currentShortcutLabel->setText(tr("Shortcut:"));
|
||||
editShortcutGroupBox->setTitle(tr("Shortcut editor"));
|
||||
editTextBox->retranslateUi();
|
||||
faqLabel->setText(QString("<a href='%1'>%2</a>").arg(WIKI_CUSTOM_SHORTCUTS).arg(tr("How to set custom shortcuts")));
|
||||
btnResetAll->setText(tr("Restore all default shortcuts"));
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
#include "../../../client/settings/cache_settings.h"
|
||||
#include "../interface/widgets/tabs/tab_supervisor.h"
|
||||
#include "../tabs/api/commander_spellbook/commander_spellbook_bracket_explainer.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <libcockatrice/settings/deck_editor_settings.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/settings/personal_settings.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
|
|
@ -19,70 +21,70 @@ enum visualDeckStoragePromptForConversionIndex
|
|||
UserInterfaceSettingsPage::UserInterfaceSettingsPage()
|
||||
{
|
||||
// general settings and notification settings
|
||||
notificationsEnabledCheckBox.setChecked(SettingsCache::instance().interface().getNotificationsEnabled());
|
||||
connect(¬ificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
notificationsEnabledCheckBox.setChecked(SettingsCache::instance().userInterface().getNotificationsEnabled());
|
||||
connect(¬ificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setNotificationsEnabled);
|
||||
connect(¬ificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED, this,
|
||||
&UserInterfaceSettingsPage::setNotificationEnabled);
|
||||
|
||||
specNotificationsEnabledCheckBox.setChecked(
|
||||
SettingsCache::instance().interface().getSpectatorNotificationsEnabled());
|
||||
specNotificationsEnabledCheckBox.setEnabled(SettingsCache::instance().interface().getNotificationsEnabled());
|
||||
connect(&specNotificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
SettingsCache::instance().userInterface().getSpectatorNotificationsEnabled());
|
||||
specNotificationsEnabledCheckBox.setEnabled(SettingsCache::instance().userInterface().getNotificationsEnabled());
|
||||
connect(&specNotificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setSpectatorNotificationsEnabled);
|
||||
|
||||
buddyConnectNotificationsEnabledCheckBox.setChecked(
|
||||
SettingsCache::instance().interface().getBuddyConnectNotificationsEnabled());
|
||||
SettingsCache::instance().userInterface().getBuddyConnectNotificationsEnabled());
|
||||
buddyConnectNotificationsEnabledCheckBox.setEnabled(
|
||||
SettingsCache::instance().interface().getNotificationsEnabled());
|
||||
SettingsCache::instance().userInterface().getNotificationsEnabled());
|
||||
connect(&buddyConnectNotificationsEnabledCheckBox, &QCheckBox::QT_STATE_CHANGED,
|
||||
&SettingsCache::instance().interface(), &InterfaceSettings::setBuddyConnectNotificationsEnabled);
|
||||
&SettingsCache::instance().userInterface(), &InterfaceSettings::setBuddyConnectNotificationsEnabled);
|
||||
|
||||
doubleClickToPlayCheckBox.setChecked(SettingsCache::instance().interface().getDoubleClickToPlay());
|
||||
connect(&doubleClickToPlayCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
doubleClickToPlayCheckBox.setChecked(SettingsCache::instance().userInterface().getDoubleClickToPlay());
|
||||
connect(&doubleClickToPlayCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setDoubleClickToPlay);
|
||||
|
||||
clickPlaysAllSelectedCheckBox.setChecked(SettingsCache::instance().interface().getClickPlaysAllSelected());
|
||||
connect(&clickPlaysAllSelectedCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
clickPlaysAllSelectedCheckBox.setChecked(SettingsCache::instance().userInterface().getClickPlaysAllSelected());
|
||||
connect(&clickPlaysAllSelectedCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setClickPlaysAllSelected);
|
||||
|
||||
playToStackCheckBox.setChecked(SettingsCache::instance().interface().getPlayToStack());
|
||||
connect(&playToStackCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
playToStackCheckBox.setChecked(SettingsCache::instance().userInterface().getPlayToStack());
|
||||
connect(&playToStackCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setPlayToStack);
|
||||
|
||||
doNotDeleteArrowsInSubPhasesCheckBox.setChecked(
|
||||
SettingsCache::instance().interface().getDoNotDeleteArrowsInSubPhases());
|
||||
connect(&doNotDeleteArrowsInSubPhasesCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
&InterfaceSettings::setDoNotDeleteArrowsInSubPhases);
|
||||
SettingsCache::instance().userInterface().getDoNotDeleteArrowsInSubPhases());
|
||||
connect(&doNotDeleteArrowsInSubPhasesCheckBox, &QCheckBox::QT_STATE_CHANGED,
|
||||
&SettingsCache::instance().userInterface(), &InterfaceSettings::setDoNotDeleteArrowsInSubPhases);
|
||||
|
||||
closeEmptyCardViewCheckBox.setChecked(SettingsCache::instance().interface().getCloseEmptyCardView());
|
||||
connect(&closeEmptyCardViewCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
closeEmptyCardViewCheckBox.setChecked(SettingsCache::instance().userInterface().getCloseEmptyCardView());
|
||||
connect(&closeEmptyCardViewCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setCloseEmptyCardView);
|
||||
|
||||
focusCardViewSearchBarCheckBox.setChecked(SettingsCache::instance().interface().getFocusCardViewSearchBar());
|
||||
connect(&focusCardViewSearchBarCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
focusCardViewSearchBarCheckBox.setChecked(SettingsCache::instance().userInterface().getFocusCardViewSearchBar());
|
||||
connect(&focusCardViewSearchBarCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setFocusCardViewSearchBar);
|
||||
|
||||
annotateTokensCheckBox.setChecked(SettingsCache::instance().interface().getAnnotateTokens());
|
||||
connect(&annotateTokensCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
annotateTokensCheckBox.setChecked(SettingsCache::instance().userInterface().getAnnotateTokens());
|
||||
connect(&annotateTokensCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setAnnotateTokens);
|
||||
|
||||
showDragSelectionCountCheckBox.setChecked(SettingsCache::instance().interface().getShowDragSelectionCount());
|
||||
connect(&showDragSelectionCountCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
showDragSelectionCountCheckBox.setChecked(SettingsCache::instance().userInterface().getShowDragSelectionCount());
|
||||
connect(&showDragSelectionCountCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setShowDragSelectionCount);
|
||||
|
||||
showTotalSelectionCountCheckBox.setChecked(SettingsCache::instance().interface().getShowTotalSelectionCount());
|
||||
connect(&showTotalSelectionCountCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
showTotalSelectionCountCheckBox.setChecked(SettingsCache::instance().userInterface().getShowTotalSelectionCount());
|
||||
connect(&showTotalSelectionCountCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setShowTotalSelectionCount);
|
||||
|
||||
useTearOffMenusCheckBox.setChecked(SettingsCache::instance().interface().getUseTearOffMenus());
|
||||
connect(&useTearOffMenusCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
useTearOffMenusCheckBox.setChecked(SettingsCache::instance().userInterface().getUseTearOffMenus());
|
||||
connect(&useTearOffMenusCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
[](const QT_STATE_CHANGED_T state) {
|
||||
SettingsCache::instance().interface().setUseTearOffMenus(state == Qt::Checked);
|
||||
SettingsCache::instance().userInterface().setUseTearOffMenus(state == Qt::Checked);
|
||||
});
|
||||
|
||||
keepGameChatFocusCheckBox.setChecked(SettingsCache::instance().interface().getKeepGameChatFocus());
|
||||
connect(&keepGameChatFocusCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
keepGameChatFocusCheckBox.setChecked(SettingsCache::instance().userInterface().getKeepGameChatFocus());
|
||||
connect(&keepGameChatFocusCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setKeepGameChatFocus);
|
||||
|
||||
auto *generalGrid = new QGridLayout;
|
||||
|
|
@ -121,9 +123,9 @@ UserInterfaceSettingsPage::UserInterfaceSettingsPage()
|
|||
animationGroupBox->setLayout(animationGrid);
|
||||
|
||||
// deck editor settings
|
||||
openDeckInNewTabCheckBox.setChecked(SettingsCache::instance().interface().getOpenDeckInNewTab());
|
||||
connect(&openDeckInNewTabCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().interface(),
|
||||
&InterfaceSettings::setOpenDeckInNewTab);
|
||||
openDeckInNewTabCheckBox.setChecked(SettingsCache::instance().deckEditor().getOpenDeckInNewTab());
|
||||
connect(&openDeckInNewTabCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance().deckEditor(),
|
||||
&DeckEditorSettings::setOpenDeckInNewTab);
|
||||
|
||||
visualDeckStorageInGameCheckBox.setChecked(
|
||||
SettingsCache::instance().visualDeckStorage().getVisualDeckStorageInGame());
|
||||
|
|
@ -156,10 +158,60 @@ UserInterfaceSettingsPage::UserInterfaceSettingsPage()
|
|||
|
||||
defaultDeckEditorTypeSelector.addItem(""); // these will be set in retranslateUI
|
||||
defaultDeckEditorTypeSelector.addItem("");
|
||||
defaultDeckEditorTypeSelector.setCurrentIndex(
|
||||
SettingsCache::instance().visualDeckStorage().getDefaultDeckEditorType());
|
||||
defaultDeckEditorTypeSelector.setCurrentIndex(SettingsCache::instance().deckEditor().getDefaultDeckEditorType());
|
||||
connect(&defaultDeckEditorTypeSelector, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
&SettingsCache::instance().visualDeckStorage(), &VisualDeckStorageSettings::setDefaultDeckEditorType);
|
||||
&SettingsCache::instance().deckEditor(), &DeckEditorSettings::setDefaultDeckEditorType);
|
||||
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setText("?");
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setAutoRaise(true);
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setEnabled(false);
|
||||
|
||||
// Add items with userData = internal enum
|
||||
commanderSpellbookIntegrationEnabledSelector.addItem(tr("Disabled"),
|
||||
commanderSpellbookIntegrationEnabledIndexDisabled);
|
||||
commanderSpellbookIntegrationEnabledSelector.addItem(tr("Enabled"),
|
||||
commanderSpellbookIntegrationEnabledIndexEnabled);
|
||||
commanderSpellbookIntegrationEnabledSelector.addItem(tr("Automatic"),
|
||||
commanderSpellbookIntegrationEnabledIndexAutomatic);
|
||||
|
||||
int storedMode = SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationEnabled();
|
||||
for (int i = 0; i < commanderSpellbookIntegrationEnabledSelector.count(); ++i) {
|
||||
if (commanderSpellbookIntegrationEnabledSelector.itemData(i).toInt() == storedMode) {
|
||||
commanderSpellbookIntegrationEnabledSelector.setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
connect(&commanderSpellbookIntegrationEnabledSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
|
||||
[this](int index) {
|
||||
int mode = commanderSpellbookIntegrationEnabledSelector.itemData(index).toInt();
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationEnabled(mode);
|
||||
updateCommanderSpellbookUiState();
|
||||
});
|
||||
|
||||
commanderSpellbookIntegrationBracketNamingSelector.addItem(
|
||||
tr("CommanderSpellbook bracket names")); // index 0 = false
|
||||
commanderSpellbookIntegrationBracketNamingSelector.addItem(
|
||||
tr("Official Commander bracket names (approximate)")); // index 1 = true
|
||||
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setCurrentIndex(
|
||||
SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationUseOfficialBracketNames() ? 1 : 0);
|
||||
|
||||
connect(&commanderSpellbookIntegrationBracketNamingSelector, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
&SettingsCache::instance(), [](int index) {
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationUseOfficialBracketNames(index ==
|
||||
1);
|
||||
});
|
||||
|
||||
updateCommanderSpellbookUiState();
|
||||
|
||||
auto *labelLayout = new QHBoxLayout;
|
||||
labelLayout->setContentsMargins(0, 0, 0, 0);
|
||||
labelLayout->addWidget(&commanderSpellbookIntegrationUseOfficialBracketNamesLabel);
|
||||
labelLayout->addWidget(&commanderSpellbookIntegrationUseOfficialBracketNamesExplainer);
|
||||
|
||||
auto *labelWidget = new QWidget;
|
||||
labelWidget->setLayout(labelLayout);
|
||||
|
||||
auto *deckEditorGrid = new QGridLayout;
|
||||
deckEditorGrid->addWidget(&openDeckInNewTabCheckBox, 0, 0);
|
||||
|
|
@ -169,14 +221,18 @@ UserInterfaceSettingsPage::UserInterfaceSettingsPage()
|
|||
deckEditorGrid->addWidget(&visualDeckStoragePromptForConversionSelector, 3, 1);
|
||||
deckEditorGrid->addWidget(&defaultDeckEditorTypeLabel, 4, 0);
|
||||
deckEditorGrid->addWidget(&defaultDeckEditorTypeSelector, 4, 1);
|
||||
deckEditorGrid->addWidget(&commanderSpellbookIntegrationEnabledLabel, 5, 0);
|
||||
deckEditorGrid->addWidget(&commanderSpellbookIntegrationEnabledSelector, 5, 1);
|
||||
deckEditorGrid->addWidget(labelWidget, 6, 0);
|
||||
deckEditorGrid->addWidget(&commanderSpellbookIntegrationBracketNamingSelector, 6, 1);
|
||||
|
||||
deckEditorGroupBox = new QGroupBox;
|
||||
deckEditorGroupBox->setLayout(deckEditorGrid);
|
||||
|
||||
// replay settings
|
||||
rewindBufferingMsBox.setRange(0, 9999);
|
||||
rewindBufferingMsBox.setValue(SettingsCache::instance().interface().getRewindBufferingMs());
|
||||
connect(&rewindBufferingMsBox, qOverload<int>(&QSpinBox::valueChanged), &SettingsCache::instance().interface(),
|
||||
rewindBufferingMsBox.setValue(SettingsCache::instance().userInterface().getRewindBufferingMs());
|
||||
connect(&rewindBufferingMsBox, qOverload<int>(&QSpinBox::valueChanged), &SettingsCache::instance().userInterface(),
|
||||
&InterfaceSettings::setRewindBufferingMs);
|
||||
|
||||
auto *replayGrid = new QGridLayout;
|
||||
|
|
@ -212,6 +268,27 @@ void UserInterfaceSettingsPage::setNotificationEnabled(QT_STATE_CHANGED_T i)
|
|||
}
|
||||
}
|
||||
|
||||
void UserInterfaceSettingsPage::updateCommanderSpellbookUiState()
|
||||
{
|
||||
const int mode = SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationEnabled();
|
||||
|
||||
const bool enabled = mode != commanderSpellbookIntegrationEnabledIndexDisabled &&
|
||||
mode != commanderSpellbookIntegrationEnabledIndexUnprompted;
|
||||
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setEnabled(enabled);
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setEnabled(enabled);
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesLabel.setVisible(enabled);
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setVisible(enabled);
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setVisible(enabled);
|
||||
|
||||
if (enabled) {
|
||||
// Sync selector with the current stored bool
|
||||
const bool useOfficial =
|
||||
SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationUseOfficialBracketNames();
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setCurrentIndex(useOfficial ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
void UserInterfaceSettingsPage::retranslateUi()
|
||||
{
|
||||
generalGroupBox->setTitle(tr("General interface settings"));
|
||||
|
|
@ -241,15 +318,31 @@ void UserInterfaceSettingsPage::retranslateUi()
|
|||
visualDeckStoragePromptForConversionLabel.setText(
|
||||
tr("When adding a tag in the visual deck storage to a .txt deck:"));
|
||||
visualDeckStoragePromptForConversionSelector.setItemText(visualDeckStoragePromptForConversionIndexNone,
|
||||
tr("do nothing"));
|
||||
tr("Do nothing"));
|
||||
visualDeckStoragePromptForConversionSelector.setItemText(visualDeckStoragePromptForConversionIndexPrompt,
|
||||
tr("ask to convert to .cod"));
|
||||
tr("Ask to convert to .cod"));
|
||||
visualDeckStoragePromptForConversionSelector.setItemText(visualDeckStoragePromptForConversionIndexAlways,
|
||||
tr("always convert to .cod"));
|
||||
tr("Always convert to .cod"));
|
||||
defaultDeckEditorTypeLabel.setText(tr("Default deck editor type"));
|
||||
defaultDeckEditorTypeSelector.setItemText(TabSupervisor::ClassicDeckEditor, tr("Classic Deck Editor"));
|
||||
defaultDeckEditorTypeSelector.setItemText(TabSupervisor::VisualDeckEditor, tr("Visual Deck Editor"));
|
||||
|
||||
commanderSpellbookIntegrationEnabledLabel.setText(
|
||||
tr("CommanderSpellbook integration to estimate commander bracket"));
|
||||
commanderSpellbookIntegrationEnabledSelector.setItemText(commanderSpellbookIntegrationEnabledIndexDisabled,
|
||||
tr("Disabled"));
|
||||
commanderSpellbookIntegrationEnabledSelector.setItemText(commanderSpellbookIntegrationEnabledIndexEnabled,
|
||||
tr("Enabled"));
|
||||
commanderSpellbookIntegrationEnabledSelector.setItemText(commanderSpellbookIntegrationEnabledIndexAutomatic,
|
||||
tr("Automatic"));
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesLabel.setText(tr("Bracket naming"));
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setItemText(
|
||||
0, CommanderBracketNames::CommanderSpellbookBracketNames);
|
||||
commanderSpellbookIntegrationBracketNamingSelector.setItemText(
|
||||
1, CommanderBracketNames::OfficialCommanderBracketNames);
|
||||
|
||||
commanderSpellbookIntegrationUseOfficialBracketNamesExplainer.setToolTip(CommanderBracketNames::Explainer);
|
||||
replayGroupBox->setTitle(tr("Replay settings"));
|
||||
rewindBufferingMsLabel.setText(tr("Buffer time for backwards skip via shortcut:"));
|
||||
rewindBufferingMsBox.setSuffix(" ms");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
#include <QSpinBox>
|
||||
#include <QToolButton>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <libcockatrice/utility/macros.h>
|
||||
|
||||
class UserInterfaceSettingsPage : public AbstractSettingsPage
|
||||
|
|
@ -15,6 +17,7 @@ class UserInterfaceSettingsPage : public AbstractSettingsPage
|
|||
Q_OBJECT
|
||||
private slots:
|
||||
void setNotificationEnabled(QT_STATE_CHANGED_T);
|
||||
void updateCommanderSpellbookUiState();
|
||||
|
||||
private:
|
||||
QCheckBox notificationsEnabledCheckBox;
|
||||
|
|
@ -39,6 +42,11 @@ private:
|
|||
QCheckBox visualDeckStorageSelectionAnimationCheckBox;
|
||||
QLabel defaultDeckEditorTypeLabel;
|
||||
QComboBox defaultDeckEditorTypeSelector;
|
||||
QLabel commanderSpellbookIntegrationEnabledLabel;
|
||||
QComboBox commanderSpellbookIntegrationEnabledSelector;
|
||||
QLabel commanderSpellbookIntegrationUseOfficialBracketNamesLabel;
|
||||
QToolButton commanderSpellbookIntegrationUseOfficialBracketNamesExplainer;
|
||||
QComboBox commanderSpellbookIntegrationBracketNamingSelector;
|
||||
QLabel rewindBufferingMsLabel;
|
||||
QSpinBox rewindBufferingMsBox;
|
||||
QGroupBox *generalGroupBox;
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@
|
|||
#include <libcockatrice/protocol/pb/command_deck_upload.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/settings/deck_editor_settings.h>
|
||||
#include <libcockatrice/settings/paths_settings.h>
|
||||
#include <libcockatrice/settings/recents_settings.h>
|
||||
#include <libcockatrice/utility/string_limits.h>
|
||||
|
|
@ -203,7 +203,7 @@ void AbstractTabDeckEditor::cleanDeckAndResetModified()
|
|||
*/
|
||||
AbstractTabDeckEditor::DeckOpenLocation AbstractTabDeckEditor::confirmOpen(const bool openInSameTabIfBlank)
|
||||
{
|
||||
if (SettingsCache::instance().interface().getOpenDeckInNewTab()) {
|
||||
if (SettingsCache::instance().deckEditor().getOpenDeckInNewTab()) {
|
||||
if (openInSameTabIfBlank && deckStateManager->isBlankNewDeck()) {
|
||||
return SAME_TAB;
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ public slots:
|
|||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void enterEvent(QEnterEvent *event) override;
|
||||
void enterEvent(QEnterEvent *event) override; ///< Hover enter
|
||||
void leaveEvent(QEvent *event) override;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/models/database/card/card_completer_proxy_model.h>
|
||||
#include <libcockatrice/models/database/card/card_search_model.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <version_string.h>
|
||||
|
||||
TabArchidekt::TabArchidekt(TabSupervisor *_tabSupervisor)
|
||||
|
|
@ -132,8 +132,8 @@ void TabArchidekt::initializeUi()
|
|||
|
||||
// Settings
|
||||
settingsButton = new SettingsButtonWidget(primaryToolbar);
|
||||
cardSizeSlider = new CardSizeWidget(primaryToolbar, nullptr,
|
||||
SettingsCache::instance().visualDeckStorage().getArchidektPreviewSize());
|
||||
cardSizeSlider =
|
||||
new CardSizeWidget(primaryToolbar, nullptr, SettingsCache::instance().cardsDisplay().getArchidektPreviewSize());
|
||||
settingsButton->addSettingsWidget(cardSizeSlider);
|
||||
|
||||
// Assemble primary toolbar
|
||||
|
|
@ -339,8 +339,8 @@ void TabArchidekt::connectSignals()
|
|||
doSearch();
|
||||
});
|
||||
|
||||
connect(cardSizeSlider, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().visualDeckStorage(),
|
||||
&VisualDeckStorageSettings::setArchidektPreviewCardSize);
|
||||
connect(cardSizeSlider, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setArchidektPreviewCardSize);
|
||||
|
||||
// Search button triggers immediate search
|
||||
connect(searchButton, &QPushButton::clicked, this, &TabArchidekt::doSearchImmediate);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
#include "card_in_deck_request.h"
|
||||
|
||||
CardInDeckRequest CardInDeckRequest::fromJson(const QJsonObject &json)
|
||||
{
|
||||
CardInDeckRequest request;
|
||||
request.card = json.value("card").toString();
|
||||
request.quantity = json.value("quantity").toInt();
|
||||
return request;
|
||||
}
|
||||
|
||||
QJsonObject CardInDeckRequest::toJson() const
|
||||
{
|
||||
QJsonObject json;
|
||||
json.insert("card", card);
|
||||
json.insert("quantity", quantity);
|
||||
return json;
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef COCKATRICE_CARD_IN_DECK_REQUEST_H
|
||||
#define COCKATRICE_CARD_IN_DECK_REQUEST_H
|
||||
#include <QJsonObject>
|
||||
|
||||
struct CardInDeckRequest
|
||||
{
|
||||
static CardInDeckRequest fromJson(const QJsonObject &json);
|
||||
QJsonObject toJson() const;
|
||||
|
||||
QString card;
|
||||
int quantity;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CARD_IN_DECK_REQUEST_H
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include "commander_spellbook_card_result.h"
|
||||
|
||||
CommanderSpellbookCardResult CommanderSpellbookCardResult::fromJson(const QJsonObject &json)
|
||||
{
|
||||
CommanderSpellbookCardResult result;
|
||||
|
||||
result.id = json.value("id").toString();
|
||||
result.name = json.value("name").toString();
|
||||
result.oracleId = json.value("oracleId").toString();
|
||||
result.spoiler = json.value("spoiler").toBool();
|
||||
result.typeLine = json.value("typeLine").toString();
|
||||
|
||||
result.imageUriFrontPng = json.value("imageUriFrontPng").toString();
|
||||
result.imageUriFrontLarge = json.value("imageUriFrontLarge").toString();
|
||||
result.imageUriFrontNormal = json.value("imageUriFrontNormal").toString();
|
||||
result.imageUriFrontSmall = json.value("imageUriFrontSmall").toString();
|
||||
result.imageUriFrontArtCrop = json.value("imageUriFrontArtCrop").toString();
|
||||
|
||||
result.imageUriBackPng = json.value("imageUriBackPng").toString();
|
||||
result.imageUriBackLarge = json.value("imageUriBackLarge").toString();
|
||||
result.imageUriBackNormal = json.value("imageUriBackNormal").toString();
|
||||
result.imageUriBackSmall = json.value("imageUriBackSmall").toString();
|
||||
result.imageUriBackArtCrop = json.value("imageUriBackArtCrop").toString();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_CARD_RESULT_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_CARD_RESULT_H
|
||||
#include <QJsonObject>
|
||||
#include <QString>
|
||||
|
||||
struct CommanderSpellbookCardResult
|
||||
{
|
||||
static CommanderSpellbookCardResult fromJson(const QJsonObject &json);
|
||||
|
||||
QString id;
|
||||
QString name;
|
||||
QString oracleId;
|
||||
bool spoiler = false;
|
||||
QString typeLine;
|
||||
|
||||
QString imageUriFrontPng;
|
||||
QString imageUriFrontLarge;
|
||||
QString imageUriFrontNormal;
|
||||
QString imageUriFrontSmall;
|
||||
QString imageUriFrontArtCrop;
|
||||
|
||||
QString imageUriBackPng;
|
||||
QString imageUriBackLarge;
|
||||
QString imageUriBackNormal;
|
||||
QString imageUriBackSmall;
|
||||
QString imageUriBackArtCrop;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_CARD_RESULT_H
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
#include "commander_spellbook_deck_request.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <libcockatrice/deck_list/tree/deck_list_card_node.h>
|
||||
|
||||
CommanderSpellbookDeckRequest CommanderSpellbookDeckRequest::fromJson(const QJsonObject &json)
|
||||
{
|
||||
CommanderSpellbookDeckRequest request;
|
||||
|
||||
// Main deck
|
||||
const QJsonArray mainArray = json.value("main").toArray();
|
||||
for (const QJsonValue &value : mainArray) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
request.mainDeck.append(CardInDeckRequest::fromJson(value.toObject()));
|
||||
|
||||
// Max size allowed by commanderspellbook
|
||||
if (request.mainDeck.size() >= 600) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Commanders
|
||||
const QJsonArray commanderArray = json.value("commanders").toArray();
|
||||
for (const QJsonValue &value : commanderArray) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
request.commanderDeck.append(CardInDeckRequest::fromJson(value.toObject()));
|
||||
|
||||
// Max size allowed by commanderspellbook
|
||||
if (request.commanderDeck.size() >= 12) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
QJsonObject CommanderSpellbookDeckRequest::toJson() const
|
||||
{
|
||||
QJsonObject json;
|
||||
|
||||
QJsonArray mainArray;
|
||||
for (const CardInDeckRequest &card : mainDeck) {
|
||||
mainArray.append(card.toJson());
|
||||
}
|
||||
|
||||
QJsonArray commanderArray;
|
||||
for (const CardInDeckRequest &card : commanderDeck) {
|
||||
commanderArray.append(card.toJson());
|
||||
}
|
||||
|
||||
json.insert("main", mainArray);
|
||||
json.insert("commanders", commanderArray);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
CommanderSpellbookDeckRequest CommanderSpellbookDeckRequest::fromDeckList(const DeckList &deck)
|
||||
{
|
||||
CommanderSpellbookDeckRequest request;
|
||||
|
||||
// --- Mainboard ---
|
||||
const auto mainCards = deck.getCardNodes({DECK_ZONE_MAIN});
|
||||
for (const DecklistCardNode *node : mainCards) {
|
||||
if (!node) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QJsonObject json;
|
||||
json.insert("card", node->getName());
|
||||
json.insert("quantity", node->getNumber());
|
||||
request.mainDeck.append(CardInDeckRequest::fromJson(json));
|
||||
|
||||
// Max size allowed by commanderspellbook
|
||||
if (request.mainDeck.size() >= 600) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Commander (bannerCard) ---
|
||||
const auto &metadata = deck.getMetadata();
|
||||
if (!metadata.bannerCard.name.isEmpty()) {
|
||||
QJsonObject json;
|
||||
json.insert("card", metadata.bannerCard.name);
|
||||
json.insert("quantity", 1);
|
||||
request.commanderDeck.append(CardInDeckRequest::fromJson(json));
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_DECK_REQUEST_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_DECK_REQUEST_H
|
||||
#include "card_in_deck_request.h"
|
||||
#include "libcockatrice/deck_list/deck_list.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QList>
|
||||
|
||||
struct CommanderSpellbookDeckRequest
|
||||
{
|
||||
static CommanderSpellbookDeckRequest fromJson(const QJsonObject &json);
|
||||
static CommanderSpellbookDeckRequest fromDeckList(const DeckList &deck);
|
||||
QJsonObject toJson() const;
|
||||
|
||||
QList<CardInDeckRequest> mainDeck; // maxItems: 600
|
||||
QList<CardInDeckRequest> commanderDeck; // maxItems: 12
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_DECK_REQUEST_H
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
#include "commander_spellbook_estimate_bracket_result.h"
|
||||
|
||||
EstimateBracketResult EstimateBracketResult::fromJson(const QJsonObject &json)
|
||||
{
|
||||
EstimateBracketResult result;
|
||||
|
||||
result.bracketTag = json.value("bracketTag").toString();
|
||||
|
||||
//
|
||||
// Cards
|
||||
//
|
||||
for (const auto &value : json.value("cards").toArray()) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QJsonObject obj = value.toObject();
|
||||
|
||||
CommanderSpellbookCardResult card = CommanderSpellbookCardResult::fromJson(obj.value("card").toObject());
|
||||
|
||||
if (obj.value("gameChanger").toBool()) {
|
||||
result.gameChangerCards.append(card);
|
||||
}
|
||||
|
||||
if (obj.value("massLandDenial").toBool()) {
|
||||
result.massLandDenialCards.append(card);
|
||||
}
|
||||
|
||||
if (obj.value("extraTurn").toBool()) {
|
||||
result.extraTurnCards.append(card);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Templates
|
||||
//
|
||||
for (const auto &value : json.value("templates").toArray()) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QJsonObject obj = value.toObject();
|
||||
|
||||
CommanderSpellbookVariantResult variant = CommanderSpellbookVariantResult::fromJson(obj);
|
||||
|
||||
if (obj.value("massLandDenial").toBool()) {
|
||||
result.massLandDenialTemplates.append(variant);
|
||||
}
|
||||
|
||||
if (obj.value("extraTurn").toBool()) {
|
||||
result.extraTurnTemplates.append(variant);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Combos
|
||||
//
|
||||
for (const auto &value : json.value("combos").toArray()) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QJsonObject obj = value.toObject();
|
||||
|
||||
CommanderSpellbookVariantResult combo = CommanderSpellbookVariantResult::fromJson(obj);
|
||||
|
||||
if (obj.value("massLandDenial").toBool()) {
|
||||
result.massLandDenialCombos.append(combo);
|
||||
}
|
||||
|
||||
if (obj.value("extraTurn").toBool()) {
|
||||
result.extraTurnCombos.append(combo);
|
||||
}
|
||||
|
||||
if (obj.value("lock").toBool()) {
|
||||
result.lockCombos.append(combo);
|
||||
}
|
||||
|
||||
if (obj.value("skipTurns").toBool()) {
|
||||
result.skipTurnsCombos.append(combo);
|
||||
}
|
||||
|
||||
if (obj.value("definitelyTwoCard").toBool()) {
|
||||
result.definitelyTwoCardCombos.append(combo);
|
||||
}
|
||||
|
||||
if (obj.value("arguablyTwoCard").toBool()) {
|
||||
result.arguablyTwoCardCombos.append(combo);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_ESTIMATE_BRACKET_RESULT_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_ESTIMATE_BRACKET_RESULT_H
|
||||
|
||||
#include "commander_spellbook_card_result.h"
|
||||
#include "commander_spellbook_variant_result.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QList>
|
||||
|
||||
struct EstimateBracketResult
|
||||
{
|
||||
static EstimateBracketResult fromJson(const QJsonObject &json);
|
||||
|
||||
QString bracketTag;
|
||||
|
||||
QList<CommanderSpellbookCardResult> gameChangerCards;
|
||||
QList<CommanderSpellbookCardResult> massLandDenialCards;
|
||||
QList<CommanderSpellbookCardResult> extraTurnCards;
|
||||
|
||||
QList<CommanderSpellbookVariantResult> massLandDenialTemplates;
|
||||
QList<CommanderSpellbookVariantResult> extraTurnTemplates;
|
||||
|
||||
QList<CommanderSpellbookVariantResult> massLandDenialCombos;
|
||||
QList<CommanderSpellbookVariantResult> extraTurnCombos;
|
||||
QList<CommanderSpellbookVariantResult> lockCombos;
|
||||
QList<CommanderSpellbookVariantResult> skipTurnsCombos;
|
||||
|
||||
QList<CommanderSpellbookVariantResult> definitelyTwoCardCombos;
|
||||
QList<CommanderSpellbookVariantResult> arguablyTwoCardCombos;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_ESTIMATE_BRACKET_RESULT_H
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
#include "commander_spellbook_variant_result.h"
|
||||
|
||||
CommanderSpellbookVariantResult CommanderSpellbookVariantResult::fromJson(const QJsonObject &json)
|
||||
{
|
||||
CommanderSpellbookVariantResult result;
|
||||
|
||||
result.id = json.value("id").toString();
|
||||
result.status = json.value("status").toString();
|
||||
|
||||
result.uses = json.value("uses").toArray();
|
||||
result.cardRequires = json.value("requires").toArray();
|
||||
result.produces = json.value("produces").toArray();
|
||||
result.of = json.value("of").toArray();
|
||||
result.includes = json.value("includes").toArray();
|
||||
|
||||
result.manaNeeded = json.value("manaNeeded").toArray();
|
||||
result.manaValueNeeded = json.value("manaValueNeeded").toArray();
|
||||
|
||||
result.easyPrerequisites = json.value("easyPrerequisites").toArray();
|
||||
result.notablePrerequisites = json.value("notablePrerequisites").toArray();
|
||||
|
||||
result.description = json.value("description").toString();
|
||||
result.notes = json.value("notes").toString();
|
||||
result.popularity = json.value("popularity").toDouble();
|
||||
|
||||
result.spoiler = json.value("spoiler").toBool();
|
||||
result.bracketTag = json.value("bracketTag").toString();
|
||||
|
||||
result.legalities = json.value("legalities").toObject();
|
||||
result.prices = json.value("prices").toObject();
|
||||
|
||||
result.variantCount = json.value("variantCount").toInt();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_VARIANT_RESULT_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_VARIANT_RESULT_H
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
struct CommanderSpellbookVariantResult
|
||||
{
|
||||
static CommanderSpellbookVariantResult fromJson(const QJsonObject &json);
|
||||
|
||||
QString id;
|
||||
QString status;
|
||||
|
||||
QJsonArray uses;
|
||||
QJsonArray cardRequires;
|
||||
QJsonArray produces;
|
||||
QJsonArray of;
|
||||
QJsonArray includes;
|
||||
|
||||
QJsonArray manaNeeded;
|
||||
QJsonArray manaValueNeeded;
|
||||
|
||||
QJsonArray easyPrerequisites;
|
||||
QJsonArray notablePrerequisites;
|
||||
|
||||
QString description;
|
||||
QString notes;
|
||||
double popularity = 0.0;
|
||||
|
||||
bool spoiler = false;
|
||||
QString bracketTag;
|
||||
|
||||
QJsonObject legalities;
|
||||
QJsonObject prices;
|
||||
|
||||
int variantCount = 0;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_VARIANT_RESULT_H
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
#include "commander_bracket_service.h"
|
||||
|
||||
#include "../../../../../client/settings/cache_settings.h"
|
||||
|
||||
#include <libcockatrice/settings/commander_bracket_settings.h>
|
||||
|
||||
CommanderBracketService &CommanderBracketService::instance()
|
||||
{
|
||||
static CommanderBracketService service;
|
||||
return service;
|
||||
}
|
||||
|
||||
CommanderBracketService::CommanderBracketService(QObject *parent) : QObject(parent)
|
||||
{
|
||||
connect(&CommanderSpellbookApiAccessor::instance(), &CommanderSpellbookApiAccessor::estimateBracketFinished, this,
|
||||
&CommanderBracketService::onEstimateBracketFinished);
|
||||
|
||||
connect(&CommanderSpellbookApiAccessor::instance(), &CommanderSpellbookApiAccessor::estimateBracketError, this,
|
||||
&CommanderBracketService::onEstimateBracketError);
|
||||
}
|
||||
|
||||
quint64 CommanderBracketService::estimateBracket(const DeckList &deck, QObject *requester)
|
||||
{
|
||||
return CommanderSpellbookApiAccessor::instance().estimateBracket(deck, requester);
|
||||
}
|
||||
|
||||
void CommanderBracketService::onEstimateBracketFinished(CommanderSpellbookApiAccessor::RequestId id,
|
||||
QObject *requester,
|
||||
const EstimateBracketResult &result)
|
||||
{
|
||||
CommanderBracketEstimate estimate;
|
||||
|
||||
estimate.bracketTag = result.bracketTag;
|
||||
|
||||
estimate.rawResult = result;
|
||||
|
||||
auto &brackets = SettingsCache::instance().commanderBrackets();
|
||||
|
||||
estimate.officialName = brackets.officialName(result.bracketTag);
|
||||
|
||||
estimate.displayName = brackets.displayName(result.bracketTag);
|
||||
|
||||
estimate.explanation = brackets.explanation(result.bracketTag);
|
||||
|
||||
emit estimateFinished(id, requester, estimate);
|
||||
}
|
||||
|
||||
void CommanderBracketService::onEstimateBracketError(CommanderSpellbookApiAccessor::RequestId id,
|
||||
QObject *requester,
|
||||
const QString &error)
|
||||
{
|
||||
emit estimateError(id, requester, error);
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef COCKATRICE_COMMANDER_BRACKET_SERVICE_H
|
||||
#define COCKATRICE_COMMANDER_BRACKET_SERVICE_H
|
||||
|
||||
#include "commander_spellbook_api_accessor.h"
|
||||
#include "libcockatrice/deck_list/deck_list.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
struct CommanderBracketEstimate
|
||||
{
|
||||
QString bracketTag;
|
||||
|
||||
QString officialName;
|
||||
QString displayName;
|
||||
QString explanation;
|
||||
|
||||
EstimateBracketResult rawResult;
|
||||
};
|
||||
|
||||
class CommanderBracketService : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static CommanderBracketService &instance();
|
||||
|
||||
quint64 estimateBracket(const DeckList &deck, QObject *requester);
|
||||
|
||||
signals:
|
||||
void estimateFinished(quint64 requestId, QObject *requester, const CommanderBracketEstimate &estimate);
|
||||
|
||||
void estimateError(quint64 requestId, QObject *requester, const QString &error);
|
||||
|
||||
private slots:
|
||||
void onEstimateBracketFinished(CommanderSpellbookApiAccessor::RequestId id,
|
||||
QObject *requester,
|
||||
const EstimateBracketResult &result);
|
||||
|
||||
void onEstimateBracketError(CommanderSpellbookApiAccessor::RequestId id, QObject *requester, const QString &error);
|
||||
|
||||
private:
|
||||
explicit CommanderBracketService(QObject *parent = nullptr);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,269 @@
|
|||
#include "commander_bracket_widget.h"
|
||||
|
||||
#include "../../../../../client/settings/cache_settings.h"
|
||||
#include "commander_bracket_service.h"
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDialog>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFormLayout>
|
||||
#include <QGridLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QTimer>
|
||||
#include <QToolButton>
|
||||
#include <QVBoxLayout>
|
||||
#include <libcockatrice/settings/deck_editor_settings.h>
|
||||
|
||||
CommanderBracketWidget::CommanderBracketWidget(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
bracketLabel = new QLabel(tr("Bracket:"), this);
|
||||
|
||||
bracketValueLabel = new QLabel(this);
|
||||
bracketValueLabel->setText("-");
|
||||
bracketValueLabel->setObjectName("bracketValueLabel");
|
||||
|
||||
bracketInfoButton = new QToolButton(this);
|
||||
bracketInfoButton->setText("?");
|
||||
bracketInfoButton->setAutoRaise(true);
|
||||
bracketInfoButton->setEnabled(false);
|
||||
|
||||
bracketRefreshButton = new QToolButton(this);
|
||||
bracketRefreshButton->setIcon(QPixmap("theme:icons/reload"));
|
||||
bracketRefreshButton->setAutoRaise(true);
|
||||
|
||||
connect(bracketRefreshButton, &QToolButton::clicked, this, &CommanderBracketWidget::requestBracketEstimate);
|
||||
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
layout->addWidget(bracketLabel, 0, 0);
|
||||
|
||||
auto *bracketRow = new QHBoxLayout;
|
||||
bracketRow->addWidget(bracketValueLabel);
|
||||
bracketRow->addWidget(bracketInfoButton);
|
||||
bracketRow->addWidget(bracketRefreshButton);
|
||||
bracketRow->addStretch();
|
||||
|
||||
layout->addLayout(bracketRow, 0, 1);
|
||||
|
||||
connect(&CommanderBracketService::instance(), &CommanderBracketService::estimateFinished, this,
|
||||
&CommanderBracketWidget::onEstimateBracketFinished);
|
||||
connect(&CommanderBracketService::instance(), &CommanderBracketService::estimateError, this,
|
||||
&CommanderBracketWidget::onEstimateBracketError);
|
||||
|
||||
connect(&SettingsCache::instance().deckEditor(), &DeckEditorSettings::commanderSpellbookIntegrationEnabledChanged,
|
||||
this, &CommanderBracketWidget::maybeAutoEstimateBracket);
|
||||
connect(&SettingsCache::instance().deckEditor(),
|
||||
&DeckEditorSettings::commanderSpellbookIntegrationUseOfficialBracketNamesChanged, this,
|
||||
&CommanderBracketWidget::maybeAutoEstimateBracket);
|
||||
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::setDeck(const QSharedPointer<DeckList> &_deck)
|
||||
{
|
||||
deck = _deck;
|
||||
requestId = 0; // invalidate any in-flight estimate for the previous deck
|
||||
|
||||
// Reset the displayed bracket
|
||||
bracketValueLabel->setText("-");
|
||||
bracketInfoButton->setToolTip({});
|
||||
bracketInfoButton->setEnabled(false);
|
||||
bracketRefreshButton->setEnabled(true);
|
||||
|
||||
maybeAutoEstimateBracket();
|
||||
}
|
||||
|
||||
bool CommanderBracketWidget::promptCommanderSpellbookIntegration()
|
||||
{
|
||||
QDialog dialog(this);
|
||||
dialog.setWindowTitle(tr("CommanderSpellbook integration"));
|
||||
|
||||
auto *mainLayout = new QVBoxLayout(&dialog);
|
||||
|
||||
// Main text
|
||||
auto *label = new QLabel(tr("CommanderSpellbook can analyze your deck and estimate its Commander bracket.\n\n"
|
||||
"This sends your deck list to an external service.\n\n"
|
||||
"CommanderSpellbook uses its own bracket naming system based on their own algorithm. "
|
||||
"These names can be mapped to the official Commander brackets, but the mapping "
|
||||
"is only an approximation."));
|
||||
label->setWordWrap(true);
|
||||
mainLayout->addWidget(label);
|
||||
|
||||
// Naming selector
|
||||
auto *formLayout = new QFormLayout;
|
||||
auto *namingCombo = new QComboBox(&dialog);
|
||||
namingCombo->addItem(tr("CommanderSpellbook bracket names"));
|
||||
namingCombo->addItem(tr("Official Commander bracket names (approximate)"));
|
||||
namingCombo->setCurrentIndex(
|
||||
SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationUseOfficialBracketNames() ? 1 : 0);
|
||||
|
||||
// Create label + explainer button
|
||||
auto *labelWidget = new QWidget(&dialog);
|
||||
auto *labelLayout = new QHBoxLayout(labelWidget);
|
||||
labelLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
auto *namingLabel = new QLabel(tr("Bracket naming:"), labelWidget);
|
||||
auto *explainerButton = new QToolButton(labelWidget);
|
||||
explainerButton->setText("?");
|
||||
explainerButton->setAutoRaise(true);
|
||||
explainerButton->setEnabled(false);
|
||||
explainerButton->setToolTip(CommanderBracketNames::Explainer);
|
||||
|
||||
labelLayout->addWidget(namingLabel);
|
||||
labelLayout->addWidget(explainerButton);
|
||||
labelLayout->addStretch(); // push the button next to label, combo stays aligned
|
||||
|
||||
// Add row with the custom label widget
|
||||
formLayout->addRow(labelWidget, namingCombo);
|
||||
mainLayout->addLayout(formLayout);
|
||||
|
||||
// Buttons
|
||||
auto *buttonBox = new QDialogButtonBox(&dialog);
|
||||
auto *enableBtn = buttonBox->addButton(tr("Enable"), QDialogButtonBox::AcceptRole);
|
||||
auto *automaticBtn = buttonBox->addButton(tr("Automatic"), QDialogButtonBox::ApplyRole);
|
||||
auto *disableBtn = buttonBox->addButton(tr("Disable"), QDialogButtonBox::RejectRole);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
|
||||
// Track which button was clicked
|
||||
QAbstractButton *clickedButton = nullptr;
|
||||
QObject::connect(buttonBox, &QDialogButtonBox::clicked, &dialog, [&](QAbstractButton *btn) {
|
||||
clickedButton = btn;
|
||||
dialog.accept();
|
||||
});
|
||||
|
||||
dialog.exec();
|
||||
|
||||
// Persist naming choice (if not disabled)
|
||||
if (clickedButton != disableBtn) {
|
||||
bool useOfficial = namingCombo->currentIndex() == 1;
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationUseOfficialBracketNames(useOfficial);
|
||||
}
|
||||
|
||||
// Persist integration mode
|
||||
if (clickedButton == disableBtn) {
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationEnabled(
|
||||
commanderSpellbookIntegrationEnabledIndexDisabled);
|
||||
return false;
|
||||
}
|
||||
if (clickedButton == enableBtn) {
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationEnabled(
|
||||
commanderSpellbookIntegrationEnabledIndexEnabled);
|
||||
return true;
|
||||
}
|
||||
if (clickedButton == automaticBtn) {
|
||||
SettingsCache::instance().deckEditor().setCommanderSpellbookIntegrationEnabled(
|
||||
commanderSpellbookIntegrationEnabledIndexAutomatic);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::updateBracketVisibility(bool visible)
|
||||
{
|
||||
setVisible(visible);
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::requestBracketEstimate()
|
||||
{
|
||||
bracketRefreshButton->setEnabled(false);
|
||||
bracketInfoButton->setEnabled(false);
|
||||
bracketValueLabel->setText(tr("Calculating…"));
|
||||
|
||||
requestId = CommanderBracketService::instance().estimateBracket(*deck, this);
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::onEstimateBracketFinished(quint64 id,
|
||||
QObject *requester,
|
||||
const CommanderBracketEstimate &result)
|
||||
{
|
||||
if (requester != this || id != requestId) {
|
||||
return;
|
||||
}
|
||||
|
||||
BracketExplainer explainer;
|
||||
lastBracketExplanation = explainer.explain(result.rawResult);
|
||||
|
||||
// Display bracket
|
||||
bracketValueLabel->setText(
|
||||
SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationUseOfficialBracketNames()
|
||||
? result.officialName
|
||||
: result.displayName);
|
||||
bracketRefreshButton->setEnabled(true);
|
||||
|
||||
// Build tooltip
|
||||
QString tooltip;
|
||||
for (const auto §ion : lastBracketExplanation.sections) {
|
||||
tooltip += "<b>" + section.title + "</b><br>";
|
||||
for (const auto &line : section.bulletPoints) {
|
||||
tooltip += "• " + line + "<br>";
|
||||
}
|
||||
tooltip += "<br>";
|
||||
}
|
||||
|
||||
bracketInfoButton->setToolTip(tooltip);
|
||||
bracketInfoButton->setEnabled(!tooltip.isEmpty());
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::onEstimateBracketError(quint64 id, QObject *requester, const QString & /*error*/)
|
||||
{
|
||||
if (requester != this || id != requestId) {
|
||||
return;
|
||||
}
|
||||
|
||||
bracketValueLabel->setText("-");
|
||||
bracketRefreshButton->setEnabled(true);
|
||||
bracketInfoButton->setToolTip({});
|
||||
bracketInfoButton->setEnabled(false);
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::maybeAutoEstimateBracket()
|
||||
{
|
||||
const QString formatKey = deck->getGameFormat();
|
||||
|
||||
const bool isCommander = (formatKey.compare("commander", Qt::CaseInsensitive) == 0);
|
||||
|
||||
int mode = SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationEnabled();
|
||||
|
||||
if (!isCommander || mode == commanderSpellbookIntegrationEnabledIndexDisabled) {
|
||||
updateBracketVisibility(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == commanderSpellbookIntegrationEnabledIndexUnprompted) {
|
||||
if (prompting) {
|
||||
return;
|
||||
}
|
||||
prompting = true;
|
||||
const bool accepted = promptCommanderSpellbookIntegration();
|
||||
prompting = false;
|
||||
if (!accepted) {
|
||||
updateBracketVisibility(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
updateBracketVisibility(true);
|
||||
|
||||
mode = SettingsCache::instance().deckEditor().getCommanderSpellbookIntegrationEnabled();
|
||||
if (mode != commanderSpellbookIntegrationEnabledIndexAutomatic) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Avoid firing if we already have a result or a request in flight
|
||||
if (!bracketRefreshButton->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Defer to avoid races during init / model rebuild
|
||||
QTimer::singleShot(0, this, &CommanderBracketWidget::requestBracketEstimate);
|
||||
}
|
||||
|
||||
void CommanderBracketWidget::retranslateUi()
|
||||
{
|
||||
bracketLabel->setText(tr("Bracket:"));
|
||||
bracketInfoButton->setToolTip(tr("Why this bracket?"));
|
||||
bracketRefreshButton->setToolTip(tr("Recalculate bracket"));
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
#ifndef COCKATRICE_COMMANDER_BRACKET_WIDGET_H
|
||||
#define COCKATRICE_COMMANDER_BRACKET_WIDGET_H
|
||||
|
||||
#include "commander_spellbook_bracket_explainer.h"
|
||||
|
||||
#include <QSharedPointer>
|
||||
#include <QWidget>
|
||||
#include <libcockatrice/deck_list/deck_list.h>
|
||||
|
||||
class QLabel;
|
||||
class QToolButton;
|
||||
struct CommanderBracketEstimate;
|
||||
|
||||
class CommanderBracketWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CommanderBracketWidget(QWidget *parent = nullptr);
|
||||
|
||||
void setDeck(const QSharedPointer<DeckList> &_deck);
|
||||
void retranslateUi();
|
||||
|
||||
private slots:
|
||||
void requestBracketEstimate();
|
||||
void onEstimateBracketFinished(quint64 id, QObject *requester, const CommanderBracketEstimate &result);
|
||||
void onEstimateBracketError(quint64 id, QObject *requester, const QString &error);
|
||||
void maybeAutoEstimateBracket();
|
||||
|
||||
private:
|
||||
bool promptCommanderSpellbookIntegration();
|
||||
void updateBracketVisibility(bool visible);
|
||||
|
||||
QSharedPointer<DeckList> deck;
|
||||
bool prompting = false;
|
||||
quint64 requestId = 0;
|
||||
|
||||
QLabel *bracketLabel;
|
||||
QLabel *bracketValueLabel;
|
||||
QToolButton *bracketInfoButton;
|
||||
QToolButton *bracketRefreshButton;
|
||||
|
||||
BracketExplanation lastBracketExplanation;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_BRACKET_WIDGET_H
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
#include "commander_spellbook_api_accessor.h"
|
||||
|
||||
#include "api_response/commander_spellbook_deck_request.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkRequest>
|
||||
#include <QUrl>
|
||||
#include <version_string.h>
|
||||
|
||||
static const QUrl ESTIMATE_BRACKET_URL(QStringLiteral("https://backend.commanderspellbook.com/estimate-bracket"));
|
||||
|
||||
CommanderSpellbookApiAccessor &CommanderSpellbookApiAccessor::instance()
|
||||
{
|
||||
static CommanderSpellbookApiAccessor instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
CommanderSpellbookApiAccessor::CommanderSpellbookApiAccessor(QObject *parent) : QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
CommanderSpellbookApiAccessor::RequestId CommanderSpellbookApiAccessor::estimateBracket(const DeckList &deck,
|
||||
QObject *requester)
|
||||
{
|
||||
CommanderSpellbookDeckRequest deckRequest = CommanderSpellbookDeckRequest::fromDeckList(deck);
|
||||
|
||||
QJsonDocument doc(deckRequest.toJson());
|
||||
QByteArray body = doc.toJson(QJsonDocument::Compact);
|
||||
|
||||
QNetworkRequest req(ESTIMATE_BRACKET_URL);
|
||||
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
req.setHeader(QNetworkRequest::UserAgentHeader, QString("Cockatrice %1").arg(VERSION_STRING));
|
||||
|
||||
QNetworkReply *reply = network.post(req, body);
|
||||
|
||||
const RequestId id = nextRequestId++;
|
||||
|
||||
reply->setProperty("requestId", QVariant::fromValue(id));
|
||||
reply->setProperty("requester", QVariant::fromValue(requester));
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() { onEstimateReplyFinished(reply); });
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void CommanderSpellbookApiAccessor::onEstimateReplyFinished(QNetworkReply *reply)
|
||||
{
|
||||
reply->deleteLater();
|
||||
|
||||
const RequestId id = reply->property("requestId").toULongLong();
|
||||
QObject *requester = reply->property("requester").value<QObject *>();
|
||||
|
||||
if (!requester) {
|
||||
// Requester died — silently drop
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit estimateBracketError(id, requester, reply->errorString());
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError err;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll(), &err);
|
||||
|
||||
if (err.error != QJsonParseError::NoError || !doc.isObject()) {
|
||||
emit estimateBracketError(id, requester, QStringLiteral("Invalid JSON response"));
|
||||
return;
|
||||
}
|
||||
|
||||
EstimateBracketResult result = EstimateBracketResult::fromJson(doc.object());
|
||||
|
||||
emit estimateBracketFinished(id, requester, result);
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_API_ACCESSOR_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_API_ACCESSOR_H
|
||||
|
||||
#include "api_response/commander_spellbook_estimate_bracket_result.h"
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
#include <libcockatrice/deck_list/deck_list.h>
|
||||
|
||||
class CommanderSpellbookApiAccessor final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static CommanderSpellbookApiAccessor &instance();
|
||||
|
||||
using RequestId = quint64;
|
||||
|
||||
RequestId estimateBracket(const DeckList &deck, QObject *requester);
|
||||
|
||||
signals:
|
||||
void estimateBracketFinished(RequestId id, QObject *requester, const EstimateBracketResult &result);
|
||||
|
||||
void estimateBracketError(RequestId id, QObject *requester, const QString &errorMessage);
|
||||
|
||||
private:
|
||||
explicit CommanderSpellbookApiAccessor(QObject *parent = nullptr);
|
||||
Q_DISABLE_COPY_MOVE(CommanderSpellbookApiAccessor)
|
||||
|
||||
void onEstimateReplyFinished(QNetworkReply *reply);
|
||||
|
||||
QNetworkAccessManager network;
|
||||
RequestId nextRequestId = 1;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_API_ACCESSOR_H
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
#include "commander_spellbook_bracket_explainer.h"
|
||||
|
||||
static QString cardList(const QList<CommanderSpellbookCardResult> &cards, int max = 5)
|
||||
{
|
||||
QStringList names;
|
||||
for (int i = 0; i < cards.size() && i < max; ++i) {
|
||||
names << cards[i].name;
|
||||
}
|
||||
|
||||
if (cards.size() > max) {
|
||||
names << QString("and %1 more").arg(cards.size() - max);
|
||||
}
|
||||
|
||||
return names.join(", ");
|
||||
}
|
||||
|
||||
static QString comboCount(const QList<CommanderSpellbookVariantResult> &variants)
|
||||
{
|
||||
return QString::number(variants.size());
|
||||
}
|
||||
|
||||
BracketExplanation BracketExplainer::explain(const EstimateBracketResult &r)
|
||||
{
|
||||
BracketExplanation out;
|
||||
out.bracket = r.bracketTag;
|
||||
|
||||
if (!r.gameChangerCards.isEmpty()) {
|
||||
BracketExplanationSection s;
|
||||
s.title = "Game-changing cards";
|
||||
|
||||
s.bulletPoints << QString("Your deck contains %1 game-changing cards, such as %2.")
|
||||
.arg(r.gameChangerCards.size())
|
||||
.arg(cardList(r.gameChangerCards));
|
||||
|
||||
out.sections << s;
|
||||
}
|
||||
|
||||
if (!r.extraTurnCards.isEmpty() || !r.extraTurnTemplates.isEmpty() || !r.extraTurnCombos.isEmpty()) {
|
||||
|
||||
BracketExplanationSection s;
|
||||
s.title = "Extra turns";
|
||||
|
||||
if (!r.extraTurnCards.isEmpty()) {
|
||||
s.bulletPoints << QString("The deck contains %1 extra-turn cards (%2).")
|
||||
.arg(r.extraTurnCards.size())
|
||||
.arg(cardList(r.extraTurnCards));
|
||||
}
|
||||
|
||||
if (!r.extraTurnTemplates.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 extra-turn templates were identified.").arg(comboCount(r.extraTurnTemplates));
|
||||
}
|
||||
|
||||
if (!r.extraTurnCombos.isEmpty()) {
|
||||
s.bulletPoints
|
||||
<< QString("%1 extra-turn combo variants were identified.").arg(comboCount(r.extraTurnCombos));
|
||||
}
|
||||
|
||||
out.sections << s;
|
||||
}
|
||||
|
||||
if (!r.massLandDenialCards.isEmpty() || !r.massLandDenialTemplates.isEmpty() || !r.massLandDenialCombos.isEmpty()) {
|
||||
|
||||
BracketExplanationSection s;
|
||||
s.title = "Mass land denial";
|
||||
|
||||
if (!r.massLandDenialCards.isEmpty()) {
|
||||
s.bulletPoints << QString("The deck contains %1 mass land denial cards (%2).")
|
||||
.arg(r.massLandDenialCards.size())
|
||||
.arg(cardList(r.massLandDenialCards));
|
||||
}
|
||||
|
||||
if (!r.massLandDenialTemplates.isEmpty()) {
|
||||
s.bulletPoints
|
||||
<< QString("%1 mass land denial templates were identified.").arg(comboCount(r.massLandDenialTemplates));
|
||||
}
|
||||
|
||||
if (!r.massLandDenialCombos.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 mass land denial combo variants were identified.")
|
||||
.arg(comboCount(r.massLandDenialCombos));
|
||||
}
|
||||
|
||||
out.sections << s;
|
||||
}
|
||||
|
||||
if (!r.lockCombos.isEmpty() || !r.skipTurnsCombos.isEmpty()) {
|
||||
|
||||
BracketExplanationSection s;
|
||||
s.title = "Lock pieces";
|
||||
|
||||
if (!r.lockCombos.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 lock combo variants were detected.").arg(comboCount(r.lockCombos));
|
||||
}
|
||||
|
||||
if (!r.skipTurnsCombos.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 skip-turn combo variants were detected.").arg(comboCount(r.skipTurnsCombos));
|
||||
}
|
||||
|
||||
out.sections << s;
|
||||
}
|
||||
|
||||
if (!r.definitelyTwoCardCombos.isEmpty() || !r.arguablyTwoCardCombos.isEmpty()) {
|
||||
|
||||
BracketExplanationSection s;
|
||||
s.title = "Two-card combos";
|
||||
|
||||
if (!r.definitelyTwoCardCombos.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 definite two-card combo variants were identified.")
|
||||
.arg(comboCount(r.definitelyTwoCardCombos));
|
||||
}
|
||||
|
||||
if (!r.arguablyTwoCardCombos.isEmpty()) {
|
||||
s.bulletPoints << QString("%1 arguable two-card combo variants were identified.")
|
||||
.arg(comboCount(r.arguablyTwoCardCombos));
|
||||
}
|
||||
|
||||
out.sections << s;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef COCKATRICE_COMMANDER_SPELLBOOK_BRACKET_EXPLAINER_H
|
||||
#define COCKATRICE_COMMANDER_SPELLBOOK_BRACKET_EXPLAINER_H
|
||||
#include "api_response/commander_spellbook_estimate_bracket_result.h"
|
||||
|
||||
namespace CommanderBracketNames
|
||||
{
|
||||
inline const char *CommanderSpellbookBracketNames = QT_TR_NOOP("CommanderSpellbook");
|
||||
inline const char *OfficialCommanderBracketNames = QT_TR_NOOP("Official (approximate)");
|
||||
inline const char *Explainer = QT_TR_NOOP(
|
||||
"The bracket system combines both objective data, as well as subjective play experience to estimate a "
|
||||
"bracket for a deck.\nCommanderSpellbook's estimation is algorithmical, which means that it can only operate "
|
||||
"on the objective data, not the subjective intent. \nThey have chosen to represent this by defining their "
|
||||
"own bracket system which matches their algorithm.\n"
|
||||
"This custom bracket system maps loosely to the standard system. \nYou may choose to use these mapped "
|
||||
"standardized names if these are more familiar to you, however, you should keep in mind that these are just "
|
||||
"rough estimations.\n\nAlways consider the subjective factors of the bracket system when determing a deck's "
|
||||
"final bracket!");
|
||||
} // namespace CommanderBracketNames
|
||||
|
||||
struct BracketExplanationSection
|
||||
{
|
||||
QString title;
|
||||
QStringList bulletPoints;
|
||||
};
|
||||
|
||||
struct BracketExplanation
|
||||
{
|
||||
QString bracket;
|
||||
QList<BracketExplanationSection> sections;
|
||||
|
||||
bool isEmpty() const
|
||||
{
|
||||
return sections.isEmpty();
|
||||
}
|
||||
};
|
||||
|
||||
class BracketExplainer
|
||||
{
|
||||
public:
|
||||
static BracketExplanation explain(const EstimateBracketResult &result);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COMMANDER_SPELLBOOK_BRACKET_EXPLAINER_H
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#include "handle_commander_brackets.h"
|
||||
|
||||
#include "../../../../../client/settings/cache_settings.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QUrl>
|
||||
#include <libcockatrice/settings/commander_bracket_settings.h>
|
||||
|
||||
static const QUrl COMMANDER_BRACKET_JSON_URL(QStringLiteral("https://cockatrice.github.io/commander-brackets.json"));
|
||||
|
||||
HandleCommanderBrackets::HandleCommanderBrackets(QObject *parent)
|
||||
: QObject(parent), nam(new QNetworkAccessManager(this)), reply(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void HandleCommanderBrackets::downloadBracketDefinitions()
|
||||
{
|
||||
if (reply) {
|
||||
return;
|
||||
}
|
||||
|
||||
reply = nam->get(QNetworkRequest(COMMANDER_BRACKET_JSON_URL));
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, &HandleCommanderBrackets::actFinishParsingDownloadedData);
|
||||
}
|
||||
|
||||
void HandleCommanderBrackets::actFinishParsingDownloadedData()
|
||||
{
|
||||
reply = qobject_cast<QNetworkReply *>(sender());
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit sigBracketDefinitionsDownloadFailed(reply->error());
|
||||
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError parseError;
|
||||
|
||||
auto document = QJsonDocument::fromJson(reply->readAll(), &parseError);
|
||||
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
emit sigBracketDefinitionsDownloadFailed(QNetworkReply::UnknownContentError);
|
||||
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
updateBracketDefinitions(document.toVariant().toMap());
|
||||
|
||||
emit sigBracketDefinitionsDownloaded();
|
||||
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void HandleCommanderBrackets::updateBracketDefinitions(const QVariantMap &jsonMap)
|
||||
{
|
||||
const auto bracketList = jsonMap.value("brackets").toList();
|
||||
SettingsCache::instance().commanderBrackets().saveDefinitions(bracketList);
|
||||
SettingsCache::instance().commanderBrackets().reloadDefinitions(bracketList);
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef COCKATRICE_HANDLE_COMMANDER_BRACKETS_H
|
||||
#define COCKATRICE_HANDLE_COMMANDER_BRACKETS_H
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
|
||||
class HandleCommanderBrackets : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HandleCommanderBrackets(QObject *parent = nullptr);
|
||||
|
||||
void downloadBracketDefinitions();
|
||||
|
||||
signals:
|
||||
void sigBracketDefinitionsDownloaded();
|
||||
void sigBracketDefinitionsDownloadFailed(QNetworkReply::NetworkError error);
|
||||
|
||||
private slots:
|
||||
void actFinishParsingDownloadedData();
|
||||
|
||||
private:
|
||||
void updateBracketDefinitions(const QVariantMap &jsonMap);
|
||||
|
||||
QNetworkAccessManager *nam;
|
||||
QNetworkReply *reply;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_HANDLE_COMMANDER_BRACKETS_H
|
||||
|
|
@ -38,8 +38,7 @@ private:
|
|||
|
||||
protected slots:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
void enterEvent(QEnterEvent *event) override;
|
||||
void enterEvent(QEnterEvent *event) override; ///< Hover enter
|
||||
void leaveEvent(QEvent *event) override;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/models/database/card/card_completer_proxy_model.h>
|
||||
#include <libcockatrice/models/database/card/card_search_model.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <version_string.h>
|
||||
|
||||
static bool canBeCommander(const CardInfoPtr &cardInfo)
|
||||
|
|
@ -95,10 +95,9 @@ TabEdhRecMain::TabEdhRecMain(TabSupervisor *_tabSupervisor) : Tab(_tabSupervisor
|
|||
|
||||
settingsButton = new SettingsButtonWidget(this);
|
||||
|
||||
cardSizeSlider =
|
||||
new CardSizeWidget(this, nullptr, SettingsCache::instance().visualDeckStorage().getEDHRecCardSize());
|
||||
connect(cardSizeSlider, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().visualDeckStorage(),
|
||||
&VisualDeckStorageSettings::setEDHRecCardSize);
|
||||
cardSizeSlider = new CardSizeWidget(this, nullptr, SettingsCache::instance().cardsDisplay().getEDHRecCardSize());
|
||||
connect(cardSizeSlider, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setEDHRecCardSize);
|
||||
|
||||
settingsButton->addSettingsWidget(cardSizeSlider);
|
||||
|
||||
|
|
|
|||
|
|
@ -258,16 +258,13 @@ void TabGame::resetChatAndPhase()
|
|||
void TabGame::emitUserEvent()
|
||||
{
|
||||
bool globalEvent = !game->getPlayerManager()->isSpectator() ||
|
||||
SettingsCache::instance().interface().getSpectatorNotificationsEnabled();
|
||||
SettingsCache::instance().userInterface().getSpectatorNotificationsEnabled();
|
||||
emit userEvent(globalEvent);
|
||||
updatePlayerListDockTitle();
|
||||
}
|
||||
|
||||
TabGame::~TabGame()
|
||||
{
|
||||
if (replayWidget) {
|
||||
delete replayWidget->replay;
|
||||
}
|
||||
for (auto &player : game->getPlayerManager()->getPlayers()) {
|
||||
player->clear();
|
||||
}
|
||||
|
|
@ -1183,6 +1180,7 @@ void TabGame::createReplayDock(GameReplay *replay)
|
|||
replayDock->setWidget(replayWidget);
|
||||
replayDock->setFloating(false);
|
||||
|
||||
connect(replayWidget, &ReplayWidget::rewound, this, &TabGame::resetChatAndPhase);
|
||||
connect(replayWidget, &ReplayWidget::eventReplayed, game->getGameEventHandler(),
|
||||
[this](const auto &event, auto options) {
|
||||
game->getGameEventHandler()->processGameEventContainer(event, nullptr, options);
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ void TabRoom::actClearChat()
|
|||
void TabRoom::actOpenChatSettings()
|
||||
{
|
||||
DlgSettings settings(this);
|
||||
settings.setTab(4);
|
||||
settings.setTab(DlgSettings::MessagesPage);
|
||||
settings.exec();
|
||||
}
|
||||
|
||||
|
|
@ -280,6 +280,7 @@ void TabRoom::processListGamesEvent(const Event_ListGames &event)
|
|||
for (int i = 0; i < gameListSize; ++i) {
|
||||
gameSelector->processGameInfo(event.game_list(i));
|
||||
}
|
||||
emit gameListUpdated();
|
||||
}
|
||||
|
||||
void TabRoom::processJoinRoomEvent(const Event_JoinRoom &event)
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ signals:
|
|||
void openMessageDialog(const QString &userName, bool focus);
|
||||
void maximizeClient();
|
||||
void notIdle();
|
||||
void gameListUpdated();
|
||||
private slots:
|
||||
void sendMessage();
|
||||
void sayFinished(const Response &response);
|
||||
|
|
@ -127,6 +128,10 @@ public:
|
|||
{
|
||||
return ownUser;
|
||||
}
|
||||
[[nodiscard]] GameSelector *getGameSelector() const
|
||||
{
|
||||
return gameSelector;
|
||||
}
|
||||
|
||||
PendingCommand *prepareRoomCommand(const ::google::protobuf::Message &cmd);
|
||||
void sendRoomCommand(PendingCommand *pend);
|
||||
|
|
|
|||
|
|
@ -191,7 +191,10 @@ void TabServer::joinRoom(int id, bool setCurrent)
|
|||
|
||||
PendingCommand *pend = client->prepareSessionCommand(cmd);
|
||||
pend->setExtraData(setCurrent);
|
||||
connect(pend, &PendingCommand::finished, this, &TabServer::joinRoomFinished);
|
||||
connect(pend, &PendingCommand::finished, this,
|
||||
[this, id](const Response &r, const CommandContainer &c, const QVariant &v) {
|
||||
joinRoomFinished(r, c, v, id);
|
||||
});
|
||||
|
||||
client->sendCommand(pend);
|
||||
|
||||
|
|
@ -205,7 +208,8 @@ void TabServer::joinRoom(int id, bool setCurrent)
|
|||
|
||||
void TabServer::joinRoomFinished(const Response &r,
|
||||
const CommandContainer & /*commandContainer*/,
|
||||
const QVariant &extraData)
|
||||
const QVariant &extraData,
|
||||
int roomId)
|
||||
{
|
||||
switch (r.response_code()) {
|
||||
case Response::RespOk:
|
||||
|
|
@ -213,21 +217,25 @@ void TabServer::joinRoomFinished(const Response &r,
|
|||
case Response::RespNameNotFound:
|
||||
QMessageBox::critical(this, tr("Error"),
|
||||
tr("Failed to join the server room: it doesn't exist on the server."));
|
||||
emit roomJoinFailed(roomId);
|
||||
return;
|
||||
case Response::RespContextError:
|
||||
QMessageBox::critical(
|
||||
this, tr("Error"),
|
||||
tr("The server thinks you are in the server room but your client is unable to display it. "
|
||||
"Try restarting your client."));
|
||||
emit roomJoinFailed(roomId);
|
||||
return;
|
||||
case Response::RespUserLevelTooLow:
|
||||
QMessageBox::critical(this, tr("Error"),
|
||||
tr("You do not have the required permission to join this server room."));
|
||||
emit roomJoinFailed(roomId);
|
||||
return;
|
||||
default:
|
||||
QMessageBox::critical(
|
||||
this, tr("Error"),
|
||||
tr("Failed to join the server room due to an unknown error: %1.").arg(r.response_code()));
|
||||
emit roomJoinFailed(roomId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -49,10 +49,13 @@ class TabServer : public Tab
|
|||
Q_OBJECT
|
||||
signals:
|
||||
void roomJoined(const ServerInfo_Room &info, bool setCurrent);
|
||||
void roomJoinFailed(int roomId);
|
||||
private slots:
|
||||
void processServerMessageEvent(const Event_ServerMessage &event);
|
||||
void joinRoom(int id, bool setCurrent);
|
||||
void joinRoomFinished(const Response &resp, const CommandContainer &commandContainer, const QVariant &extraData);
|
||||
void joinRoomFinished(const Response &resp,
|
||||
const CommandContainer &commandContainer,
|
||||
const QVariant &extraData,
|
||||
int roomId);
|
||||
|
||||
private:
|
||||
AbstractClient *client;
|
||||
|
|
@ -62,6 +65,7 @@ private:
|
|||
|
||||
public:
|
||||
TabServer(TabSupervisor *_tabSupervisor, AbstractClient *_client);
|
||||
void joinRoom(int id, bool setCurrent);
|
||||
void retranslateUi() override;
|
||||
[[nodiscard]] QString getTabText() const override
|
||||
{
|
||||
|
|
|
|||
|
|
@ -39,9 +39,21 @@
|
|||
#include <libcockatrice/protocol/pb/serverinfo_room.pb.h>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_user.pb.h>
|
||||
#include <libcockatrice/settings/chat_settings.h>
|
||||
#include <libcockatrice/settings/deck_editor_settings.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/settings/tabs_settings.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
|
||||
QRect MacOSTabFixStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
|
||||
{
|
||||
if (element != SE_TabBarTabText) {
|
||||
return QProxyStyle::subElementRect(element, option, widget);
|
||||
}
|
||||
|
||||
// Skip over QProxyStyle handling subElementRect,
|
||||
// This fixes an issue on OSX where the labels for tabs with a button and an icon
|
||||
// get cut-off too early
|
||||
return QCommonStyle::subElementRect(element, option, widget);
|
||||
}
|
||||
|
||||
CloseButton::CloseButton(QWidget *parent) : QAbstractButton(parent)
|
||||
{
|
||||
|
|
@ -106,6 +118,12 @@ TabSupervisor::TabSupervisor(AbstractClient *_client, QMenu *tabsMenu, QWidget *
|
|||
setMovable(true);
|
||||
setIconSize(QSize(15, 15));
|
||||
|
||||
#if defined(Q_OS_MAC)
|
||||
// This is necessary to fix an issue on macOS,
|
||||
// where tabs with icons and buttons get drawn incorrectly
|
||||
tabBar()->setStyle(new MacOSTabFixStyle);
|
||||
#endif
|
||||
|
||||
userListManager = new UserListManager(client, this);
|
||||
|
||||
// connect tab changes
|
||||
|
|
@ -565,6 +583,11 @@ void TabSupervisor::actTabServer(bool checked)
|
|||
|
||||
void TabSupervisor::openTabServer()
|
||||
{
|
||||
SettingsCache::instance().tabs().setTabServerOpen(true);
|
||||
if (tabServer) {
|
||||
return;
|
||||
}
|
||||
|
||||
tabServer = new TabServer(this, client);
|
||||
connect(tabServer, &TabServer::roomJoined, this, &TabSupervisor::addRoomTab);
|
||||
myAddTab(tabServer, aTabServer);
|
||||
|
|
@ -888,7 +911,7 @@ void TabSupervisor::talkLeft(TabMessage *tab)
|
|||
*/
|
||||
void TabSupervisor::openDeckInNewTab(const LoadedDeck &deckToOpen)
|
||||
{
|
||||
int type = SettingsCache::instance().visualDeckStorage().getDefaultDeckEditorType();
|
||||
int type = SettingsCache::instance().deckEditor().getDefaultDeckEditorType();
|
||||
switch (type) {
|
||||
case ClassicDeckEditor:
|
||||
addDeckEditorTab(deckToOpen);
|
||||
|
|
@ -987,7 +1010,7 @@ void TabSupervisor::tabUserEvent(bool globalEvent)
|
|||
tab->setContentsChanged(true);
|
||||
setTabIcon(indexOf(tab), QPixmap("theme:icons/tab_changed"));
|
||||
}
|
||||
if (globalEvent && SettingsCache::instance().interface().getNotificationsEnabled()) {
|
||||
if (globalEvent && SettingsCache::instance().userInterface().getNotificationsEnabled()) {
|
||||
QApplication::alert(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -1082,7 +1105,7 @@ void TabSupervisor::processUserJoined(const ServerInfo_User &userInfoJoined)
|
|||
}
|
||||
}
|
||||
|
||||
if (SettingsCache::instance().interface().getBuddyConnectNotificationsEnabled()) {
|
||||
if (SettingsCache::instance().userInterface().getBuddyConnectNotificationsEnabled()) {
|
||||
QApplication::alert(this);
|
||||
this->actShowPopup(tr("Your buddy %1 has signed on!").arg(userName));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,10 @@ public:
|
|||
{
|
||||
return userListManager;
|
||||
}
|
||||
[[nodiscard]] TabServer *getTabServer() const
|
||||
{
|
||||
return tabServer;
|
||||
}
|
||||
[[nodiscard]] const QMap<int, TabRoom *> &getRoomTabs() const
|
||||
{
|
||||
return roomTabs;
|
||||
|
|
@ -171,6 +175,7 @@ public slots:
|
|||
void maximizeMainWindow();
|
||||
void actTabVisualDeckStorage(bool checked);
|
||||
void actTabReplays(bool checked);
|
||||
void openTabServer();
|
||||
private slots:
|
||||
void refreshShortcuts();
|
||||
|
||||
|
|
@ -183,7 +188,6 @@ private slots:
|
|||
|
||||
void openTabVisualDeckStorage();
|
||||
void openTabHome();
|
||||
void openTabServer();
|
||||
void openTabAccount();
|
||||
void openTabDeckStorage();
|
||||
void openTabReplays();
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@
|
|||
#include <libcockatrice/card/card_info_comparator.h>
|
||||
#include <libcockatrice/card/database/card_database.h>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <utility>
|
||||
|
||||
VisualDatabaseDisplayWidget::VisualDatabaseDisplayWidget(QWidget *parent,
|
||||
|
|
@ -52,10 +52,10 @@ VisualDatabaseDisplayWidget::VisualDatabaseDisplayWidget(QWidget *parent,
|
|||
mainLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
flowWidget = new FlowWidget(this, Qt::Horizontal, Qt::ScrollBarAlwaysOff, Qt::ScrollBarPolicy::ScrollBarAsNeeded);
|
||||
cardSizeWidget = new CardSizeWidget(
|
||||
this, flowWidget, SettingsCache::instance().visualDeckStorage().getVisualDatabaseDisplayCardSize());
|
||||
connect(cardSizeWidget, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().visualDeckStorage(),
|
||||
&VisualDeckStorageSettings::setVisualDatabaseDisplayCardSize);
|
||||
cardSizeWidget = new CardSizeWidget(this, flowWidget,
|
||||
SettingsCache::instance().cardsDisplay().getVisualDatabaseDisplayCardSize());
|
||||
connect(cardSizeWidget, &CardSizeWidget::cardSizeSettingUpdated, &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setVisualDatabaseDisplayCardSize);
|
||||
|
||||
searchContainer = new FlowWidget(this, Qt::Horizontal, Qt::ScrollBarAlwaysOff, Qt::ScrollBarAlwaysOff);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include <QSplitter>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||
#include <libcockatrice/settings/cards_display_settings.h>
|
||||
#include <random>
|
||||
|
||||
VisualDeckEditorSampleHandWidget::VisualDeckEditorSampleHandWidget(QWidget *parent,
|
||||
|
|
@ -34,10 +34,10 @@ VisualDeckEditorSampleHandWidget::VisualDeckEditorSampleHandWidget(QWidget *pare
|
|||
resetAndHandSizeLayout->addWidget(resetButton);
|
||||
|
||||
handSizeSpinBox = new QSpinBox(this);
|
||||
handSizeSpinBox->setValue(SettingsCache::instance().visualDeckStorage().getVisualDeckEditorSampleHandSize());
|
||||
handSizeSpinBox->setValue(SettingsCache::instance().cardsDisplay().getSampleHandSize());
|
||||
handSizeSpinBox->setMinimum(1);
|
||||
connect(handSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), &SettingsCache::instance().visualDeckStorage(),
|
||||
&VisualDeckStorageSettings::setVisualDeckEditorSampleHandSize);
|
||||
connect(handSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), &SettingsCache::instance().cardsDisplay(),
|
||||
&CardsDisplaySettings::setSampleHandSize);
|
||||
connect(handSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), this,
|
||||
&VisualDeckEditorSampleHandWidget::updateDisplay);
|
||||
resetAndHandSizeLayout->addWidget(handSizeSpinBox);
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue