Qt: Delay settings save by one second

This commit is contained in:
Connor McLaughlin 2020-07-21 19:58:50 +10:00
parent 68d98af497
commit 2fb00faa67
2 changed files with 39 additions and 2 deletions

View file

@ -123,7 +123,8 @@ bool QtHostInterface::parseCommandLineParameters(int argc, char* argv[],
return CommonHostInterface::ParseCommandLineParameters(argc, argv, out_boot_params); return CommonHostInterface::ParseCommandLineParameters(argc, argv, out_boot_params);
} }
std::string QtHostInterface::GetStringSettingValue(const char* section, const char* key, const char* default_value /*= ""*/) std::string QtHostInterface::GetStringSettingValue(const char* section, const char* key,
const char* default_value /*= ""*/)
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
return m_settings_interface->GetStringValue(section, key, default_value); return m_settings_interface->GetStringValue(section, key, default_value);
@ -157,24 +158,28 @@ void QtHostInterface::SetBoolSettingValue(const char* section, const char* key,
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->SetBoolValue(section, key, value); m_settings_interface->SetBoolValue(section, key, value);
queueSettingsSave();
} }
void QtHostInterface::SetIntSettingValue(const char* section, const char* key, int value) void QtHostInterface::SetIntSettingValue(const char* section, const char* key, int value)
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->SetIntValue(section, key, value); m_settings_interface->SetIntValue(section, key, value);
queueSettingsSave();
} }
void QtHostInterface::SetFloatSettingValue(const char* section, const char* key, float value) void QtHostInterface::SetFloatSettingValue(const char* section, const char* key, float value)
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->SetFloatValue(section, key, value); m_settings_interface->SetFloatValue(section, key, value);
queueSettingsSave();
} }
void QtHostInterface::SetStringSettingValue(const char* section, const char* key, const char* value) void QtHostInterface::SetStringSettingValue(const char* section, const char* key, const char* value)
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->SetStringValue(section, key, value); m_settings_interface->SetStringValue(section, key, value);
queueSettingsSave();
} }
void QtHostInterface::SetStringListSettingValue(const char* section, const char* key, void QtHostInterface::SetStringListSettingValue(const char* section, const char* key,
@ -182,12 +187,37 @@ void QtHostInterface::SetStringListSettingValue(const char* section, const char*
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->SetStringList(section, key, values); m_settings_interface->SetStringList(section, key, values);
queueSettingsSave();
} }
void QtHostInterface::RemoveSettingValue(const char* section, const char* key) void QtHostInterface::RemoveSettingValue(const char* section, const char* key)
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->DeleteValue(section, key); m_settings_interface->DeleteValue(section, key);
queueSettingsSave();
}
void QtHostInterface::queueSettingsSave()
{
if (!m_settings_save_timer)
{
m_settings_save_timer = std::make_unique<QTimer>();
connect(m_settings_save_timer.get(), &QTimer::timeout, this, &QtHostInterface::doSaveSettings);
m_settings_save_timer->setSingleShot(true);
}
else
{
m_settings_save_timer->stop();
}
m_settings_save_timer->start(SETTINGS_SAVE_DELAY);
}
void QtHostInterface::doSaveSettings()
{
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
m_settings_interface->Save();
m_settings_save_timer.reset();
} }
void QtHostInterface::setDefaultSettings() void QtHostInterface::setDefaultSettings()
@ -202,6 +232,8 @@ void QtHostInterface::setDefaultSettings()
{ {
std::lock_guard<std::recursive_mutex> guard(m_settings_mutex); std::lock_guard<std::recursive_mutex> guard(m_settings_mutex);
SetDefaultSettings(*m_settings_interface.get()); SetDefaultSettings(*m_settings_interface.get());
m_settings_interface->Save();
CommonHostInterface::LoadSettings(*m_settings_interface.get()); CommonHostInterface::LoadSettings(*m_settings_interface.get());
} }

View file

@ -145,6 +145,7 @@ private Q_SLOTS:
void doStopThread(); void doStopThread();
void onHostDisplayWindowResized(int width, int height); void onHostDisplayWindowResized(int width, int height);
void doBackgroundControllerPoll(); void doBackgroundControllerPoll();
void doSaveSettings();
protected: protected:
bool AcquireHostDisplay() override; bool AcquireHostDisplay() override;
@ -171,7 +172,9 @@ private:
enum : u32 enum : u32
{ {
BACKGROUND_CONTROLLER_POLLING_INTERVAL = BACKGROUND_CONTROLLER_POLLING_INTERVAL =
100 /// Interval at which the controllers are polled when the system is not active. 100, /// Interval at which the controllers are polled when the system is not active.
SETTINGS_SAVE_DELAY = 1000
}; };
using InputButtonHandler = std::function<void(bool)>; using InputButtonHandler = std::function<void(bool)>;
@ -211,6 +214,7 @@ private:
void renderDisplay(); void renderDisplay();
void connectDisplaySignals(QtDisplayWidget* widget); void connectDisplaySignals(QtDisplayWidget* widget);
void updateDisplayState(); void updateDisplayState();
void queueSettingsSave();
void wakeThread(); void wakeThread();
std::unique_ptr<INISettingsInterface> m_settings_interface; std::unique_ptr<INISettingsInterface> m_settings_interface;
@ -224,6 +228,7 @@ private:
std::atomic_bool m_shutdown_flag{false}; std::atomic_bool m_shutdown_flag{false};
QTimer* m_background_controller_polling_timer = nullptr; QTimer* m_background_controller_polling_timer = nullptr;
std::unique_ptr<QTimer> m_settings_save_timer;
bool m_is_rendering_to_main = false; bool m_is_rendering_to_main = false;
bool m_is_fullscreen = false; bool m_is_fullscreen = false;