Qt: Fix game list settings headings not being translatable
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include "common/assert.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/game_list.h"
|
||||
#include "gamelistsearchdirectoriesmodel.h"
|
||||
#include "qthostinterface.h"
|
||||
#include "qtutils.h"
|
||||
#include <QtCore/QAbstractTableModel>
|
||||
@ -21,192 +22,6 @@
|
||||
|
||||
static constexpr char REDUMP_DOWNLOAD_URL[] = "http://redump.org/datfile/psx/serial,version,description";
|
||||
|
||||
class GameListSearchDirectoriesModel : public QAbstractTableModel
|
||||
{
|
||||
public:
|
||||
GameListSearchDirectoriesModel(QtHostInterface* host_interface) : m_host_interface(host_interface)
|
||||
{
|
||||
loadFromSettings();
|
||||
}
|
||||
|
||||
~GameListSearchDirectoriesModel() = default;
|
||||
|
||||
int columnCount(const QModelIndex& parent) const override
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
|
||||
{
|
||||
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
|
||||
return {};
|
||||
|
||||
if (section == 0)
|
||||
return tr("Path");
|
||||
else
|
||||
return tr("Recursive");
|
||||
}
|
||||
|
||||
int rowCount(const QModelIndex& parent) const override
|
||||
{
|
||||
if (parent.isValid())
|
||||
return 0;
|
||||
|
||||
return static_cast<int>(m_entries.size());
|
||||
}
|
||||
|
||||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
|
||||
{
|
||||
if (!index.isValid())
|
||||
return {};
|
||||
|
||||
const int row = index.row();
|
||||
const int column = index.column();
|
||||
if (row < 0 || row >= static_cast<int>(m_entries.size()))
|
||||
return {};
|
||||
|
||||
const Entry& entry = m_entries[row];
|
||||
if (role == Qt::CheckStateRole)
|
||||
{
|
||||
if (column == 1)
|
||||
return entry.recursive ? Qt::Checked : Qt::Unchecked;
|
||||
}
|
||||
else if (role == Qt::DisplayRole)
|
||||
{
|
||||
if (column == 0)
|
||||
return entry.path;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool setData(const QModelIndex& index, const QVariant& value, int role) override
|
||||
{
|
||||
if (!index.isValid())
|
||||
return false;
|
||||
|
||||
const int row = index.row();
|
||||
const int column = index.column();
|
||||
if (row < 0 || row >= static_cast<int>(m_entries.size()))
|
||||
return false;
|
||||
|
||||
if (column != 1 || role == Qt::CheckStateRole)
|
||||
return false;
|
||||
|
||||
Entry& entry = m_entries[row];
|
||||
entry.recursive = value == Qt::Checked;
|
||||
saveToSettings();
|
||||
m_host_interface->refreshGameList(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
void addEntry(const QString& path, bool recursive)
|
||||
{
|
||||
auto existing = std::find_if(m_entries.begin(), m_entries.end(), [path](const Entry& e) { return e.path == path; });
|
||||
if (existing != m_entries.end())
|
||||
{
|
||||
const int row = static_cast<int>(existing - m_entries.begin());
|
||||
existing->recursive = recursive;
|
||||
dataChanged(index(row, 1), index(row, 1), QVector<int>{Qt::CheckStateRole});
|
||||
}
|
||||
else
|
||||
{
|
||||
beginInsertRows(QModelIndex(), static_cast<int>(m_entries.size()), static_cast<int>(m_entries.size()));
|
||||
m_entries.push_back({path, recursive});
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
saveToSettings();
|
||||
m_host_interface->refreshGameList(false);
|
||||
}
|
||||
|
||||
void removeEntry(int row)
|
||||
{
|
||||
if (row < 0 || row >= static_cast<int>(m_entries.size()))
|
||||
return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), row, row);
|
||||
m_entries.erase(m_entries.begin() + row);
|
||||
endRemoveRows();
|
||||
|
||||
saveToSettings();
|
||||
m_host_interface->refreshGameList(false);
|
||||
}
|
||||
|
||||
bool isEntryRecursive(int row) const
|
||||
{
|
||||
return (row < 0 || row >= static_cast<int>(m_entries.size())) ? false : m_entries[row].recursive;
|
||||
}
|
||||
|
||||
void setEntryRecursive(int row, bool recursive)
|
||||
{
|
||||
if (row < 0 || row >= static_cast<int>(m_entries.size()))
|
||||
return;
|
||||
|
||||
m_entries[row].recursive = recursive;
|
||||
emit dataChanged(index(row, 1), index(row, 1), {Qt::CheckStateRole});
|
||||
|
||||
saveToSettings();
|
||||
m_host_interface->refreshGameList(false);
|
||||
}
|
||||
|
||||
void openEntryInExplorer(QWidget* parent, int row) const
|
||||
{
|
||||
if (row < 0 || row >= static_cast<int>(m_entries.size()))
|
||||
return;
|
||||
|
||||
QtUtils::OpenURL(parent, QUrl::fromLocalFile(m_entries[row].path));
|
||||
}
|
||||
|
||||
void loadFromSettings()
|
||||
{
|
||||
std::vector<std::string> path_list = m_host_interface->GetSettingStringList("GameList", "Paths");
|
||||
for (std::string& entry : path_list)
|
||||
m_entries.push_back({QString::fromStdString(entry), false});
|
||||
|
||||
path_list = m_host_interface->GetSettingStringList("GameList", "RecursivePaths");
|
||||
for (std::string& entry : path_list)
|
||||
m_entries.push_back({QString::fromStdString(entry), true});
|
||||
}
|
||||
|
||||
void saveToSettings()
|
||||
{
|
||||
std::vector<std::string> paths;
|
||||
std::vector<std::string> recursive_paths;
|
||||
|
||||
for (const Entry& entry : m_entries)
|
||||
{
|
||||
if (entry.recursive)
|
||||
recursive_paths.push_back(entry.path.toStdString());
|
||||
else
|
||||
paths.push_back(entry.path.toStdString());
|
||||
}
|
||||
|
||||
if (paths.empty())
|
||||
m_host_interface->RemoveSettingValue("GameList", "Paths");
|
||||
else
|
||||
m_host_interface->SetStringListSettingValue("GameList", "Paths", paths);
|
||||
|
||||
if (recursive_paths.empty())
|
||||
m_host_interface->RemoveSettingValue("GameList", "RecursivePaths");
|
||||
else
|
||||
m_host_interface->SetStringListSettingValue("GameList", "RecursivePaths", recursive_paths);
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry
|
||||
{
|
||||
QString path;
|
||||
bool recursive;
|
||||
};
|
||||
|
||||
QtHostInterface* m_host_interface;
|
||||
std::vector<Entry> m_entries;
|
||||
};
|
||||
|
||||
GameListSettingsWidget::GameListSettingsWidget(QtHostInterface* host_interface, QWidget* parent /* = nullptr */)
|
||||
: QWidget(parent), m_host_interface(host_interface)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user