add "open recent" menu option to deck editor tab (#5319)
Some checks are pending
Build Desktop / Configure (push) Waiting to run
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Debian, DEB, 12) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Debian, DEB, skip, 11) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Fedora, RPM, 41) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Fedora, RPM, skip, 40) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, 24.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, skip, 20.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (Ubuntu, DEB, skip, 22.04) (push) Blocked by required conditions
Build Desktop / ${{matrix.distro}} ${{matrix.version}} (yes, Arch, skip) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (3, 1, macos-14, Apple, 14, Release, 15.4) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (3, macos-14, Apple, 14, Debug, 15.4) (push) Blocked by required conditions
Build Desktop / macOS ${{matrix.target}}${{ matrix.soc == 'Intel' && ' Intel' || '' }}${{ matrix.type == 'Debug' && ' Debug' || '' }} (4, 1, macos-13, Intel, 13, Release, 14.3.1) (push) Blocked by required conditions
Build Desktop / Windows ${{matrix.target}} (msvc2019_64, 5.15.*, 7) (push) Blocked by required conditions
Build Desktop / Windows ${{matrix.target}} (msvc2019_64, qtimageformats qtmultimedia qtwebsockets, 6.6.*, 10) (push) Blocked by required conditions

* add "open recent" menu option to deck editor tab

* change texts

* also get it to work with loading from deck storage tab

* add error message when fail to open

* only update recents on successful open

* only update recents on successful open

* reword to "Clear"
This commit is contained in:
RickyRister 2024-12-24 16:55:04 -08:00 committed by GitHub
parent e7585271fb
commit 4ca1fc083d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 130 additions and 5 deletions

View File

@ -122,6 +122,7 @@ set(cockatrice_SOURCES
src/settings/game_filters_settings.cpp
src/settings/layouts_settings.cpp
src/settings/message_settings.cpp
src/settings/recents_settings.cpp
src/settings/servers_settings.cpp
src/settings/settings_manager.cpp
src/settings/cache_settings.cpp

View File

@ -292,6 +292,15 @@ void TabDeckEditor::createMenus()
aLoadDeck = new QAction(QString(), this);
connect(aLoadDeck, SIGNAL(triggered()), this, SLOT(actLoadDeck()));
loadRecentDeckMenu = new QMenu(this);
connect(&SettingsCache::instance().recents(), &RecentsSettings::recentlyOpenedDeckPathsChanged, this,
&TabDeckEditor::updateRecentlyOpened);
aClearRecents = new QAction(QString(), this);
connect(aClearRecents, &QAction::triggered, this, &TabDeckEditor::actClearRecents);
updateRecentlyOpened();
aSaveDeck = new QAction(QString(), this);
connect(aSaveDeck, SIGNAL(triggered()), this, SLOT(actSaveDeck()));
@ -342,6 +351,7 @@ void TabDeckEditor::createMenus()
deckMenu = new QMenu(this);
deckMenu->addAction(aNewDeck);
deckMenu->addAction(aLoadDeck);
deckMenu->addMenu(loadRecentDeckMenu);
deckMenu->addAction(aSaveDeck);
deckMenu->addAction(aSaveDeckAs);
deckMenu->addSeparator();
@ -705,6 +715,8 @@ void TabDeckEditor::retranslateUi()
aNewDeck->setText(tr("&New deck"));
aLoadDeck->setText(tr("&Load deck..."));
loadRecentDeckMenu->setTitle(tr("Load recent deck..."));
aClearRecents->setText(tr("Clear"));
aSaveDeck->setText(tr("&Save deck"));
aSaveDeckAs->setText(tr("Save deck &as..."));
aLoadDeckFromClipboard->setText(tr("Load deck from cl&ipboard..."));
@ -852,6 +864,20 @@ void TabDeckEditor::updateHash()
hashLabel->setText(deckModel->getDeckList()->getDeckHash());
}
void TabDeckEditor::updateRecentlyOpened()
{
loadRecentDeckMenu->clear();
for (const auto &deckPath : SettingsCache::instance().recents().getRecentlyOpenedDeckPaths()) {
QAction *aRecentlyOpenedDeck = new QAction(deckPath, this);
loadRecentDeckMenu->addAction(aRecentlyOpenedDeck);
connect(aRecentlyOpenedDeck, &QAction::triggered, this,
[=, this] { actOpenRecent(aRecentlyOpenedDeck->text()); });
}
loadRecentDeckMenu->addSeparator();
loadRecentDeckMenu->addAction(aClearRecents);
aClearRecents->setEnabled(SettingsCache::instance().recents().getRecentlyOpenedDeckPaths().length() > 0);
}
bool TabDeckEditor::confirmClose()
{
if (modified) {
@ -907,21 +933,50 @@ void TabDeckEditor::actLoadDeck()
return;
QString fileName = dialog.selectedFiles().at(0);
openDeckFromFile(fileName, deckOpenLocation);
}
void TabDeckEditor::actOpenRecent(const QString &fileName)
{
auto deckOpenLocation = confirmOpen();
if (deckOpenLocation == CANCELLED) {
return;
}
openDeckFromFile(fileName, deckOpenLocation);
}
/**
* Actually opens the deck from file
* @param fileName The path of the deck to open
* @param deckOpenLocation Which tab to open the deck
*/
void TabDeckEditor::openDeckFromFile(const QString &fileName, DeckOpenLocation deckOpenLocation)
{
DeckLoader::FileFormat fmt = DeckLoader::getFormatFromName(fileName);
auto *l = new DeckLoader;
if (l->loadFromFile(fileName, fmt)) {
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(fileName);
if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(l);
} else {
setSaveStatus(false);
setDeck(l);
}
} else
} else {
delete l;
QMessageBox::critical(this, tr("Error"), tr("Could not open deck at %1").arg(fileName));
}
setSaveStatus(true);
}
void TabDeckEditor::actClearRecents()
{
SettingsCache::instance().recents().clearRecentlyOpenedDeckPaths();
}
void TabDeckEditor::saveDeckRemoteFinished(const Response &response)
{
if (response.response_code() != Response::RespOk)

View File

@ -43,9 +43,12 @@ private slots:
void updateSearch(const QString &search);
void databaseCustomMenu(QPoint point);
void decklistCustomMenu(QPoint point);
void updateRecentlyOpened();
void actNewDeck();
void actLoadDeck();
void actOpenRecent(const QString &fileName);
void actClearRecents();
bool actSaveDeck();
bool actSaveDeckAs();
void actLoadDeckFromClipboard();
@ -106,6 +109,7 @@ private:
void offsetCountAtIndex(const QModelIndex &idx, int offset);
void decrementCardHelper(QString zoneName);
void recursiveExpand(const QModelIndex &index);
void openDeckFromFile(const QString &fileName, DeckOpenLocation deckOpenLocation);
CardDatabaseModel *databaseModel;
CardDatabaseDisplayModel *databaseDisplayModel;
@ -131,10 +135,10 @@ private:
QWidget *filterBox;
QMenu *deckMenu, *viewMenu, *cardInfoDockMenu, *deckDockMenu, *filterDockMenu, *printingSelectorDockMenu,
*analyzeDeckMenu, *saveDeckToClipboardMenu;
QAction *aNewDeck, *aLoadDeck, *aSaveDeck, *aSaveDeckAs, *aLoadDeckFromClipboard, *aSaveDeckToClipboard,
*aSaveDeckToClipboardRaw, *aPrintDeck, *aExportDeckDecklist, *aAnalyzeDeckDeckstats, *aAnalyzeDeckTappedout,
*aClose;
*analyzeDeckMenu, *saveDeckToClipboardMenu, *loadRecentDeckMenu;
QAction *aNewDeck, *aLoadDeck, *aClearRecents, *aSaveDeck, *aSaveDeckAs, *aLoadDeckFromClipboard,
*aSaveDeckToClipboard, *aSaveDeckToClipboardRaw, *aPrintDeck, *aExportDeckDecklist, *aAnalyzeDeckDeckstats,
*aAnalyzeDeckTappedout, *aClose;
QAction *aClearFilterAll, *aClearFilterOne;
QAction *aAddCard, *aAddCardToSideboard, *aRemoveCard, *aIncrement, *aDecrement;
QAction *aResetLayout;

View File

@ -166,6 +166,8 @@ void TabDeckStorage::actOpenLocalDeck()
if (!deckLoader.loadFromFile(filePath, DeckLoader::CockatriceFormat))
return;
SettingsCache::instance().recents().updateRecentlyOpenedDeckPaths(filePath);
emit openDeckEditor(&deckLoader);
}
}

View File

@ -178,6 +178,7 @@ SettingsCache::SettingsCache()
gameFiltersSettings = new GameFiltersSettings(settingsPath, this);
layoutsSettings = new LayoutsSettings(settingsPath, this);
downloadSettings = new DownloadSettings(settingsPath, this);
recentsSettings = new RecentsSettings(settingsPath, this);
if (!QFile(settingsPath + "global.ini").exists())
translateLegacySettings();

View File

@ -7,6 +7,7 @@
#include "game_filters_settings.h"
#include "layouts_settings.h"
#include "message_settings.h"
#include "recents_settings.h"
#include "servers_settings.h"
#include "shortcuts_settings.h"
@ -82,6 +83,7 @@ private:
GameFiltersSettings *gameFiltersSettings;
LayoutsSettings *layoutsSettings;
DownloadSettings *downloadSettings;
RecentsSettings *recentsSettings;
QByteArray mainWindowGeometry;
QByteArray tokenDialogGeometry;
@ -598,6 +600,10 @@ public:
{
return *downloadSettings;
}
RecentsSettings &recents() const
{
return *recentsSettings;
}
bool getIsPortableBuild() const
{
return isPortableBuild;

View File

@ -0,0 +1,32 @@
#include "recents_settings.h"
#define MAX_RECENT_DECK_COUNT 10
RecentsSettings::RecentsSettings(QString settingPath, QObject *parent)
: SettingsManager(settingPath + "recents.ini", parent)
{
}
QStringList RecentsSettings::getRecentlyOpenedDeckPaths()
{
return getValue("deckpaths", "deckbuilder").toStringList();
}
void RecentsSettings::clearRecentlyOpenedDeckPaths()
{
deleteValue("deckpaths", "deckbuilder");
emit recentlyOpenedDeckPathsChanged();
}
void RecentsSettings::updateRecentlyOpenedDeckPaths(const QString &deckPath)
{
auto deckPaths = getValue("deckpaths", "deckbuilder").toStringList();
deckPaths.removeAll(deckPath);
deckPaths.prepend(deckPath);
while (deckPaths.size() > MAX_RECENT_DECK_COUNT) {
deckPaths.removeLast();
}
setValue(deckPaths, "deckpaths", "deckbuilder");
emit recentlyOpenedDeckPathsChanged();
}

View File

@ -0,0 +1,23 @@
#ifndef RECENTS_SETTINGS_H
#define RECENTS_SETTINGS_H
#include "settings_manager.h"
class RecentsSettings : public SettingsManager
{
Q_OBJECT
friend class SettingsCache;
explicit RecentsSettings(QString settingPath, QObject *parent = nullptr);
RecentsSettings(const RecentsSettings & /*other*/);
public:
QStringList getRecentlyOpenedDeckPaths();
void clearRecentlyOpenedDeckPaths();
void updateRecentlyOpenedDeckPaths(const QString &deckPath);
signals:
void recentlyOpenedDeckPathsChanged();
};
#endif // RECENTS_SETTINGS_H

View File

@ -28,6 +28,7 @@ set(oracle_SOURCES
../cockatrice/src/settings/servers_settings.cpp
../cockatrice/src/settings/settings_manager.cpp
../cockatrice/src/settings/message_settings.cpp
../cockatrice/src/settings/recents_settings.cpp
../cockatrice/src/settings/game_filters_settings.cpp
../cockatrice/src/settings/layouts_settings.cpp
../cockatrice/src/settings/download_settings.cpp