Skip to content

Commit

Permalink
Fix settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sguionni committed Jul 13, 2024
1 parent 9b22743 commit eb948a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
10 changes: 5 additions & 5 deletions lib/ui/qt/include/qt/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QPointer>
#include <QSettings>
#include <QTimer>
#include <app/filesystem.hpp>
#include <ui/base_application.hpp>

class QMenu;
Expand All @@ -15,6 +16,8 @@ class QSplashScreen;

namespace VTX::UI::QT
{
inline QSettings SETTINGS
= QSettings( QString::fromStdString( App::Filesystem::getConfigIniFile().string() ), QSettings::IniFormat );

class Application final : public UI::BaseApplication<MainWindow>, QApplication
{
Expand All @@ -32,17 +35,14 @@ namespace VTX::UI::QT
void _start() override;

private:
QTimer _timer;
QElapsedTimer _elapsedTimer;
QPointer<QSplashScreen> _qSplashScreen;

QSettings _settings;
QTimer _timer;
QElapsedTimer _elapsedTimer;

void _loadTheme();
void _saveSettings();
void _restoreSettings();
};

} // namespace VTX::UI::QT

#endif
9 changes: 4 additions & 5 deletions lib/ui/qt/include/qt/dialog/download.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __VTX_UI_QT_DIALOG_DOWNLOAD__
#define __VTX_UI_QT_DIALOG_DOWNLOAD__

#include "qt/application.hpp"
#include "qt/base_widget.hpp"
#include <QComboBox>
#include <QDialog>
Expand Down Expand Up @@ -130,15 +131,13 @@ namespace VTX::UI::QT::Dialog

void _loadHistory( const std::string & p_key, QComboBox * const p_comboBox )
{
QSettings settings;
QStringList history = settings.value( p_key.c_str() ).toStringList();
QStringList history = SETTINGS.value( p_key.c_str() ).toStringList();
p_comboBox->addItems( history );
}

void _saveHistory( const std::string & p_key, const std::string & p_value )
{
QSettings settings;
QStringList history = settings.value( p_key.c_str() ).toStringList();
QStringList history = SETTINGS.value( p_key.c_str() ).toStringList();
// Remove duplicates.
if ( history.contains( p_value.c_str() ) )
{
Expand All @@ -151,7 +150,7 @@ namespace VTX::UI::QT::Dialog
{
history.removeLast();
}
settings.setValue( p_key.c_str(), history );
SETTINGS.setValue( p_key.c_str(), history );
}
};

Expand Down
28 changes: 13 additions & 15 deletions lib/ui/qt/src/qt/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <QFile>
#include <QIcon>
#include <QSplashScreen>
#include <app/filesystem.hpp>
#include <app/infos.hpp>
#include <util/enum.hpp>

Expand All @@ -21,9 +20,8 @@ namespace VTX::UI::QT

// Create QApplication with zero argc and nullptr argv.
int zero = 0;
Application::Application() :
UI::BaseApplication<MainWindow>(), QApplication( zero, nullptr ),
_settings( QString::fromStdString( App::Filesystem::getConfigIniFile().string() ), QSettings::IniFormat )
Application::Application() : UI::BaseApplication<MainWindow>(), QApplication( zero, nullptr )

{
using namespace Resources;
using namespace VTX::App::Info;
Expand Down Expand Up @@ -176,27 +174,27 @@ namespace VTX::UI::QT

void Application::_saveSettings()
{
VTX_INFO( "Saving settings: {}", _settings.fileName().toStdString() );
_settings.setValue( "geometry", _mainWindow->saveGeometry() );
_settings.setValue( "windowState", _mainWindow->saveState() );
_settings.sync();
VTX_INFO( "Saving settings: {}", SETTINGS.fileName().toStdString() );
SETTINGS.setValue( "geometry", _mainWindow->saveGeometry() );
SETTINGS.setValue( "windowState", _mainWindow->saveState() );
SETTINGS.sync();

if ( _settings.status() != QSettings::NoError )
if ( SETTINGS.status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( _settings.status() ) ) );
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( SETTINGS.status() ) ) );
}
}

void Application::_restoreSettings()
{
if ( _settings.status() != QSettings::NoError )
if ( SETTINGS.status() != QSettings::NoError )
{
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( _settings.status() ) ) );
throw std::runtime_error( fmt::format( "{}", Util::Enum::enumName( SETTINGS.status() ) ) );
}

VTX_INFO( "Restoring settings: {}", _settings.fileName().toStdString() );
_mainWindow->restoreGeometry( _settings.value( "geometry" ).toByteArray() );
_mainWindow->restoreState( _settings.value( "windowState" ).toByteArray() );
VTX_INFO( "Restoring settings: {}", SETTINGS.fileName().toStdString() );
_mainWindow->restoreGeometry( SETTINGS.value( "geometry" ).toByteArray() );
_mainWindow->restoreState( SETTINGS.value( "windowState" ).toByteArray() );
}

} // namespace VTX::UI::QT

0 comments on commit eb948a3

Please sign in to comment.