mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-28 08:52:19 -07:00
[Refactor] Extract shared deck conversion prompt helper
Move the deck-to-.cod conversion prompt logic (format check, saved preference handling, overwrite confirmation, dialog) out of DeckPreviewWidget into dlg_convert_deck_to_cod_format so the deck editor can reuse it without duplicating it. Took 4 minutes
This commit is contained in:
parent
09d2270d45
commit
9f00ab6a11
3 changed files with 89 additions and 55 deletions
|
|
@ -1,9 +1,17 @@
|
||||||
#include "dlg_convert_deck_to_cod_format.h"
|
#include "dlg_convert_deck_to_cod_format.h"
|
||||||
|
|
||||||
|
#include "../../../client/settings/cache_settings.h"
|
||||||
|
#include "../../deck_loader/deck_loader.h"
|
||||||
|
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
#include <QDialogButtonBox>
|
#include <QDialogButtonBox>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QMessageBox>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
|
#include <libcockatrice/settings/visual_deck_storage_settings.h>
|
||||||
|
|
||||||
DialogConvertDeckToCodFormat::DialogConvertDeckToCodFormat(QWidget *parent) : QDialog(parent)
|
DialogConvertDeckToCodFormat::DialogConvertDeckToCodFormat(QWidget *parent) : QDialog(parent)
|
||||||
{
|
{
|
||||||
|
|
@ -38,3 +46,64 @@ bool DialogConvertDeckToCodFormat::dontAskAgain() const
|
||||||
{
|
{
|
||||||
return dontAskAgainCheckbox->isChecked();
|
return dontAskAgainCheckbox->isChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool confirmOverwriteIfExists(QWidget *parent, const QString &filePath)
|
||||||
|
{
|
||||||
|
QFileInfo fileInfo(filePath);
|
||||||
|
QString newFileName = QDir::toNativeSeparators(fileInfo.path() + "/" + fileInfo.completeBaseName() + ".cod");
|
||||||
|
|
||||||
|
if (QFile::exists(newFileName)) {
|
||||||
|
QMessageBox::StandardButton reply =
|
||||||
|
QMessageBox::question(parent, QObject::tr("Overwrite Existing File?"),
|
||||||
|
QObject::tr("A .cod version of this deck already exists. Overwrite it?"),
|
||||||
|
QMessageBox::Yes | QMessageBox::No);
|
||||||
|
return reply == QMessageBox::Yes;
|
||||||
|
}
|
||||||
|
return true; // Safe to proceed
|
||||||
|
}
|
||||||
|
|
||||||
|
bool promptFileConversionIfRequired(QWidget *parent, const QString &filePath, const std::function<bool()> &convert)
|
||||||
|
{
|
||||||
|
if (DeckFileFormat::getFormatFromName(filePath) == DeckFileFormat::Cockatrice) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve saved preference if the prompt is disabled
|
||||||
|
if (!SettingsCache::instance().visualDeckStorage().getVisualDeckStoragePromptForConversion()) {
|
||||||
|
if (!SettingsCache::instance().visualDeckStorage().getVisualDeckStorageAlwaysConvert()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!confirmOverwriteIfExists(parent, filePath)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return convert();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the dialog to the user
|
||||||
|
DialogConvertDeckToCodFormat conversionDialog(parent);
|
||||||
|
if (conversionDialog.exec() != QDialog::Accepted) {
|
||||||
|
SettingsCache::instance().visualDeckStorage().setVisualDeckStoragePromptForConversion(
|
||||||
|
!conversionDialog.dontAskAgain());
|
||||||
|
SettingsCache::instance().visualDeckStorage().setVisualDeckStorageAlwaysConvert(false);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to convert file
|
||||||
|
if (!confirmOverwriteIfExists(parent, filePath)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!convert()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conversionDialog.dontAskAgain()) {
|
||||||
|
SettingsCache::instance().visualDeckStorage().setVisualDeckStoragePromptForConversion(false);
|
||||||
|
SettingsCache::instance().visualDeckStorage().setVisualDeckStorageAlwaysConvert(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,9 @@
|
||||||
#include <QDialogButtonBox>
|
#include <QDialogButtonBox>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
class QWidget;
|
||||||
|
|
||||||
class DialogConvertDeckToCodFormat : public QDialog
|
class DialogConvertDeckToCodFormat : public QDialog
|
||||||
{
|
{
|
||||||
|
|
@ -33,4 +36,19 @@ private:
|
||||||
Q_DISABLE_COPY(DialogConvertDeckToCodFormat)
|
Q_DISABLE_COPY(DialogConvertDeckToCodFormat)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks whether the deck file at \a filePath can store tags.
|
||||||
|
*
|
||||||
|
* If the file is not a .cod deck, prompts the user for conversion to the
|
||||||
|
* Cockatrice format, honoring the saved "always convert / don't ask again"
|
||||||
|
* preference. On acceptance \a convert is called to perform the conversion.
|
||||||
|
*
|
||||||
|
* @param parent The widget to parent the prompt to.
|
||||||
|
* @param filePath The path of the deck file to check.
|
||||||
|
* @param convert Called to convert the deck once the user agrees.
|
||||||
|
* @return true if tags can be stored (no conversion needed, or the conversion
|
||||||
|
* was performed), false if the user declined to convert.
|
||||||
|
*/
|
||||||
|
bool promptFileConversionIfRequired(QWidget *parent, const QString &filePath, const std::function<bool()> &convert);
|
||||||
|
|
||||||
#endif // DIALOG_CONVERT_DECK_TO_COD_FORMAT_H
|
#endif // DIALOG_CONVERT_DECK_TO_COD_FORMAT_H
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@
|
||||||
#include "deck_preview_deck_tags_display_widget.h"
|
#include "deck_preview_deck_tags_display_widget.h"
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
|
@ -479,21 +478,6 @@ void DeckPreviewWidget::actDeleteFile()
|
||||||
// The folder widget removes this preview once the row is gone.
|
// The folder widget removes this preview once the row is gone.
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool confirmOverwriteIfExists(QWidget *parent, const QString &filePath)
|
|
||||||
{
|
|
||||||
QFileInfo fileInfo(filePath);
|
|
||||||
QString newFileName = QDir::toNativeSeparators(fileInfo.path() + "/" + fileInfo.completeBaseName() + ".cod");
|
|
||||||
|
|
||||||
if (QFile::exists(newFileName)) {
|
|
||||||
QMessageBox::StandardButton reply =
|
|
||||||
QMessageBox::question(parent, QObject::tr("Overwrite Existing File?"),
|
|
||||||
QObject::tr("A .cod version of this deck already exists. Overwrite it?"),
|
|
||||||
QMessageBox::Yes | QMessageBox::No);
|
|
||||||
return reply == QMessageBox::Yes;
|
|
||||||
}
|
|
||||||
return true; // Safe to proceed
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the deck's file format supports tags.
|
* Checks if the deck's file format supports tags.
|
||||||
* If not, then prompt the user for file conversion.
|
* If not, then prompt the user for file conversion.
|
||||||
|
|
@ -501,45 +485,8 @@ static bool confirmOverwriteIfExists(QWidget *parent, const QString &filePath)
|
||||||
*/
|
*/
|
||||||
bool DeckPreviewWidget::promptFileConversionIfRequired()
|
bool DeckPreviewWidget::promptFileConversionIfRequired()
|
||||||
{
|
{
|
||||||
if (DeckFileFormat::getFormatFromName(filePath) == DeckFileFormat::Cockatrice) {
|
return ::promptFileConversionIfRequired(this, filePath, [this] {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve saved preference if the prompt is disabled
|
|
||||||
if (!SettingsCache::instance().visualDeckStorage().getVisualDeckStoragePromptForConversion()) {
|
|
||||||
if (!SettingsCache::instance().visualDeckStorage().getVisualDeckStorageAlwaysConvert()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!confirmOverwriteIfExists(this, filePath)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
model_->convertToCockatriceFormat(row());
|
model_->convertToCockatriceFormat(row());
|
||||||
return true;
|
return true;
|
||||||
}
|
});
|
||||||
|
|
||||||
// Show the dialog to the user
|
|
||||||
DialogConvertDeckToCodFormat conversionDialog(this);
|
|
||||||
if (conversionDialog.exec() != QDialog::Accepted) {
|
|
||||||
SettingsCache::instance().visualDeckStorage().setVisualDeckStoragePromptForConversion(
|
|
||||||
!conversionDialog.dontAskAgain());
|
|
||||||
SettingsCache::instance().visualDeckStorage().setVisualDeckStorageAlwaysConvert(false);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to convert file
|
|
||||||
if (!confirmOverwriteIfExists(this, filePath)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
model_->convertToCockatriceFormat(row());
|
|
||||||
|
|
||||||
if (conversionDialog.dontAskAgain()) {
|
|
||||||
SettingsCache::instance().visualDeckStorage().setVisualDeckStoragePromptForConversion(false);
|
|
||||||
SettingsCache::instance().visualDeckStorage().setVisualDeckStorageAlwaysConvert(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue