Revert "Revert "abstracting setting class and more wokr on gamelist class""

This reverts commit 2809eb7c2c.
This commit is contained in:
georgemoralis 2023-03-10 10:43:44 +02:00
parent 2809eb7c2c
commit 29a4b249d8
10 changed files with 289 additions and 12 deletions

View File

@ -1,8 +1,9 @@
#include "game_list_frame.h" #include "game_list_frame.h"
#include "gui_settings.h" #include "gui_settings.h"
game_list_frame::game_list_frame(QWidget* parent) game_list_frame::game_list_frame(std::shared_ptr<gui_settings> gui_settings,QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_gui_settings(std::move(gui_settings))
{ {
m_game_list = new game_list_table(); m_game_list = new game_list_table();
m_game_list->setShowGrid(false); m_game_list->setShowGrid(false);
@ -34,6 +35,51 @@ game_list_frame::game_list_frame(QWidget* parent)
layout->addWidget(m_game_list); layout->addWidget(m_game_list);
setLayout(layout); setLayout(layout);
//endof temp code //endof temp code
// Actions regarding showing/hiding columns
auto add_column = [this](gui::game_list_columns col, const QString& header_text, const QString& action_text)
{
m_game_list->setHorizontalHeaderItem(col, new QTableWidgetItem(header_text));
m_columnActs.append(new QAction(action_text, this));
};
add_column(gui::column_icon, tr("Icon"), tr("Show Icons"));
add_column(gui::column_name, tr("Name"), tr("Show Names"));
add_column(gui::column_serial, tr("Serial"), tr("Show Serials"));
add_column(gui::column_firmware, tr("Firmware"), tr("Show Firmwares"));
add_column(gui::column_version, tr("Version"), tr("Show Versions"));
add_column(gui::column_category, tr("Category"), tr("Show Categories"));
add_column(gui::column_path, tr("Path"), tr("Show Paths"));
for (int col = 0; col < m_columnActs.count(); ++col)
{
m_columnActs[col]->setCheckable(true);
connect(m_columnActs[col], &QAction::triggered, this, [this, col](bool checked)
{
if (!checked) // be sure to have at least one column left so you can call the context menu at all time
{
int c = 0;
for (int i = 0; i < m_columnActs.count(); ++i)
{
if (m_gui_settings->GetGamelistColVisibility(i) && ++c > 1)
break;
}
if (c < 2)
{
m_columnActs[col]->setChecked(true); // re-enable the checkbox if we don't change the actual state
return;
}
}
m_game_list->setColumnHidden(col, !checked); // Negate because it's a set col hidden and we have menu say show.
m_gui_settings->SetGamelistColVisibility(col, checked);
if (checked) // handle hidden columns that have zero width after showing them (stuck between others)
{
FixNarrowColumns();
}
});
}
} }
game_list_frame::~game_list_frame(){ game_list_frame::~game_list_frame(){

View File

@ -11,7 +11,7 @@ class game_list_frame : public QWidget
{ {
Q_OBJECT Q_OBJECT
public : public :
explicit game_list_frame(QWidget* parent = nullptr); explicit game_list_frame(std::shared_ptr<gui_settings> gui_settings,QWidget* parent = nullptr);
~game_list_frame(); ~game_list_frame();
/** Fix columns with width smaller than the minimal section size */ /** Fix columns with width smaller than the minimal section size */
void FixNarrowColumns() const; void FixNarrowColumns() const;
@ -29,6 +29,7 @@ private:
int m_sort_column; int m_sort_column;
// data // data
std::shared_ptr<gui_settings> m_gui_settings;
QList<game_info> m_game_data; QList<game_info> m_game_data;
// Icons // Icons

30
shadPS4/gui/gui_save.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <QString>
#include <QVariant>
struct gui_save
{
QString key;
QString name;
QVariant def;
gui_save()
{
key = "";
name = "";
def = QVariant();
}
gui_save(const QString& k, const QString& n, const QVariant& d)
{
key = k;
name = n;
def = d;
}
bool operator==(const gui_save& rhs) const noexcept
{
return key == rhs.key && name == rhs.name && def == rhs.def;
}
};

View File

@ -3,6 +3,20 @@
gui_settings::gui_settings(QObject* parent) gui_settings::gui_settings(QObject* parent)
{ {
m_settings.reset(new QSettings("shadps4.ini", QSettings::Format::IniFormat, parent)); m_settings.reset(new QSettings("shadps4.ini", QSettings::Format::IniFormat, parent)); //TODO make the path configurable
} }
void gui_settings::SetGamelistColVisibility(int col, bool val) const
{
SetValue(GetGuiSaveForColumn(col), val);
}
bool gui_settings::GetGamelistColVisibility(int col) const
{
return GetValue(GetGuiSaveForColumn(col)).toBool();
}
gui_save gui_settings::GetGuiSaveForColumn(int col)
{
return gui_save{ gui::game_list, "visibility_" + gui::get_game_list_column_name(static_cast<gui::game_list_columns>(col)), true };
}

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <QSettings> #include "settings.h"
namespace gui namespace gui
{ {
@ -17,15 +17,47 @@ namespace gui
column_count column_count
}; };
inline QString get_game_list_column_name(game_list_columns col)
{
switch (col)
{
case column_icon:
return "column_icon";
case column_name:
return "column_name";
case column_serial:
return "column_serial";
case column_firmware:
return "column_firmware";
case column_version:
return "column_version";
case column_category:
return "column_category";
case column_path:
return "column_path";
case column_count:
return "";
}
throw std::exception("get_game_list_column_name: Invalid column");
}
const QString game_list = "GameList";
} }
class gui_settings class gui_settings : public settings
{ {
Q_OBJECT
public: public:
explicit gui_settings(QObject* parent = nullptr); explicit gui_settings(QObject* parent = nullptr);
private: bool GetGamelistColVisibility(int col) const;
std::unique_ptr<QSettings> m_settings;
public Q_SLOTS:
void SetGamelistColVisibility(int col, bool val) const;
static gui_save GetGuiSaveForColumn(int col);
}; };

94
shadPS4/gui/settings.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "settings.h"
settings::settings(QObject* parent) : QObject(parent),
m_settings_dir(ComputeSettingsDir())
{
}
settings::~settings()
{
if (m_settings)
{
m_settings->sync();
}
}
QString settings::GetSettingsDir() const
{
return m_settings_dir.absolutePath();
}
QString settings::ComputeSettingsDir()
{
return ""; //TODO currently we configure same dir , make it configurable
}
void settings::RemoveValue(const QString& key, const QString& name) const
{
if (m_settings)
{
m_settings->beginGroup(key);
m_settings->remove(name);
m_settings->endGroup();
}
}
void settings::RemoveValue(const gui_save& entry) const
{
RemoveValue(entry.key, entry.name);
}
QVariant settings::GetValue(const QString& key, const QString& name, const QVariant& def) const
{
return m_settings ? m_settings->value(key + "/" + name, def) : def;
}
QVariant settings::GetValue(const gui_save& entry) const
{
return GetValue(entry.key, entry.name, entry.def);
}
QVariant settings::List2Var(const q_pair_list& list)
{
QByteArray ba;
QDataStream stream(&ba, QIODevice::WriteOnly);
stream << list;
return QVariant(ba);
}
q_pair_list settings::Var2List(const QVariant& var)
{
q_pair_list list;
QByteArray ba = var.toByteArray();
QDataStream stream(&ba, QIODevice::ReadOnly);
stream >> list;
return list;
}
void settings::SetValue(const gui_save& entry, const QVariant& value) const
{
if (m_settings)
{
m_settings->beginGroup(entry.key);
m_settings->setValue(entry.name, value);
m_settings->endGroup();
}
}
void settings::SetValue(const QString& key, const QVariant& value) const
{
if (m_settings)
{
m_settings->setValue(key, value);
}
}
void settings::SetValue(const QString& key, const QString& name, const QVariant& value) const
{
if (m_settings)
{
m_settings->beginGroup(key);
m_settings->setValue(name, value);
m_settings->endGroup();
}
}

48
shadPS4/gui/settings.h Normal file
View File

@ -0,0 +1,48 @@
#pragma once
#include <QSettings>
#include <QDir>
#include <QVariant>
#include <QSize>
#include <memory>
#include "gui_save.h"
typedef QPair<QString, QString> q_string_pair;
typedef QPair<QString, QSize> q_size_pair;
typedef QList<q_string_pair> q_pair_list;
typedef QList<q_size_pair> q_size_list;
// Parent Class for GUI settings
class settings : public QObject
{
Q_OBJECT
public:
explicit settings(QObject* parent = nullptr);
~settings();
QString GetSettingsDir() const;
QVariant GetValue(const QString& key, const QString& name, const QVariant& def) const;
QVariant GetValue(const gui_save& entry) const;
static QVariant List2Var(const q_pair_list& list);
static q_pair_list Var2List(const QVariant& var);
public Q_SLOTS:
/** Remove entry */
void RemoveValue(const QString& key, const QString& name) const;
void RemoveValue(const gui_save& entry) const;
/** Write value to entry */
void SetValue(const gui_save& entry, const QVariant& value) const;
void SetValue(const QString& key, const QVariant& value) const;
void SetValue(const QString& key, const QString& name, const QVariant& value) const;
protected:
static QString ComputeSettingsDir();
std::unique_ptr<QSettings> m_settings;
QDir m_settings_dir;
};

View File

@ -17,7 +17,7 @@ shadps4gui::shadps4gui(std::shared_ptr<gui_settings> gui_settings, QWidget* pare
//ui.horizontalLayout->addWidget(game_list); //ui.horizontalLayout->addWidget(game_list);
//show(); //show();
//game_list->PopulateAsync(); //game_list->PopulateAsync();
game_list_frame* game_list2 = new game_list_frame(); game_list_frame* game_list2 = new game_list_frame(m_gui_settings);
ui.horizontalLayout->addWidget(game_list2); ui.horizontalLayout->addWidget(game_list2);
show(); show();
} }

View File

@ -22,6 +22,7 @@
<ClCompile Include="gui\game_list_grid_delegate.cpp" /> <ClCompile Include="gui\game_list_grid_delegate.cpp" />
<ClCompile Include="gui\game_list_table.cpp" /> <ClCompile Include="gui\game_list_table.cpp" />
<ClCompile Include="gui\gui_settings.cpp" /> <ClCompile Include="gui\gui_settings.cpp" />
<ClCompile Include="gui\settings.cpp" />
<ClCompile Include="gui\shadps4gui.cpp" /> <ClCompile Include="gui\shadps4gui.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
</ItemGroup> </ItemGroup>
@ -45,7 +46,9 @@
<QtMoc Include="gui\game_list_frame.h" /> <QtMoc Include="gui\game_list_frame.h" />
<ClInclude Include="gui\game_list_grid_delegate.h" /> <ClInclude Include="gui\game_list_grid_delegate.h" />
<ClInclude Include="gui\game_list_table.h" /> <ClInclude Include="gui\game_list_table.h" />
<ClInclude Include="gui\gui_settings.h" /> <ClInclude Include="gui\gui_save.h" />
<QtMoc Include="gui\gui_settings.h" />
<QtMoc Include="gui\settings.h" />
<ClInclude Include="Types.h" /> <ClInclude Include="Types.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -74,6 +74,9 @@
<ClCompile Include="gui\game_list_frame.cpp"> <ClCompile Include="gui\game_list_frame.cpp">
<Filter>gui</Filter> <Filter>gui</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="gui\settings.cpp">
<Filter>gui</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtUic Include="gui\shadps4gui.ui"> <QtUic Include="gui\shadps4gui.ui">
@ -93,6 +96,12 @@
<QtMoc Include="gui\game_list_frame.h"> <QtMoc Include="gui\game_list_frame.h">
<Filter>gui</Filter> <Filter>gui</Filter>
</QtMoc> </QtMoc>
<QtMoc Include="gui\settings.h">
<Filter>gui</Filter>
</QtMoc>
<QtMoc Include="gui\gui_settings.h">
<Filter>gui</Filter>
</QtMoc>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Types.h"> <ClInclude Include="Types.h">
@ -110,9 +119,6 @@
<ClInclude Include="emulator\fileFormat\PKG.h"> <ClInclude Include="emulator\fileFormat\PKG.h">
<Filter>emulator\fileFormat</Filter> <Filter>emulator\fileFormat</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="gui\gui_settings.h">
<Filter>gui</Filter>
</ClInclude>
<ClInclude Include="emulator\gameInfo.h"> <ClInclude Include="emulator\gameInfo.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
@ -125,6 +131,9 @@
<ClInclude Include="gui\game_list_grid_delegate.h"> <ClInclude Include="gui\game_list_grid_delegate.h">
<Filter>gui</Filter> <Filter>gui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="gui\gui_save.h">
<Filter>gui</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="cpp.hint" /> <None Include="cpp.hint" />