From fcde7f2a14e9b90c76b12628fdb960074ed10eba Mon Sep 17 00:00:00 2001 From: spacy51 Date: Fri, 22 Feb 2008 18:59:57 +0000 Subject: [PATCH] Qt: ADDED support for saving/loading settings to/from an INI file. git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@382 a31d4220-a93d-0410-bf67-fe4944624d44 --- src/qt/MainWnd.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++---- src/qt/MainWnd.h | 9 ++++++-- src/qt/main.cpp | 4 +++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/qt/MainWnd.cpp b/src/qt/MainWnd.cpp index af403262..7b5d9186 100644 --- a/src/qt/MainWnd.cpp +++ b/src/qt/MainWnd.cpp @@ -23,9 +23,10 @@ #include "sidewidget_cheats.h" -MainWnd::MainWnd( QWidget *parent, QTranslator **trans ) +MainWnd::MainWnd( QTranslator **trans, QSettings *settings, QWidget *parent ) : QMainWindow( parent ), translator( trans ), + settings( settings ), fileMenu( 0 ), settingsMenu( 0 ), toolsMenu( 0 ), @@ -40,6 +41,8 @@ MainWnd::MainWnd( QWidget *parent, QTranslator **trans ) createDockWidgets(); createActions(); createMenus(); + + loadSettings(); } @@ -48,6 +51,41 @@ MainWnd::~MainWnd() } +void MainWnd::loadSettings() +{ + QVariant v; + + v = settings->value( "MainWnd/state" ); + if( v.isValid() ) { + restoreState( v.toByteArray() ); + } + + v = settings->value( "App/language_file" ); + if( v.isValid() ) { + languageFile = v.toString(); + if( loadTranslation() ) { + // only enable language if it loaded correctly + v = settings->value( "App/language_enable" ); + enableTranslation( v.toBool() ); + } + } +} + + +void MainWnd::saveSettings() +{ + QVariant v; + + // state of toolbars and dock widgets + // all memorizable widgets need an objectName! + v = saveState(); + settings->setValue( "MainWnd/state", v ); + + v = enableTranslationAct->isEnabled(); + settings->setValue( "App/language_enable", v ); +} + + void MainWnd::createActions() { bool enabled, checked; @@ -131,6 +169,7 @@ void MainWnd::createDockWidgets() // Cheat Widget dockWidget_cheats = new QDockWidget( tr( "Cheats" ), this ); + dockWidget_cheats->setObjectName( "dockWidget_cheats" ); // necessary for MainWnd::saveState SideWidget_Cheats *sw_cheats = new SideWidget_Cheats( dockWidget_cheats ); dockWidget_cheats->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea ); dockWidget_cheats->setWidget( sw_cheats ); @@ -153,6 +192,12 @@ bool MainWnd::createDisplay() } +void MainWnd::closeEvent( QCloseEvent * ) +{ + saveSettings(); +} + + bool MainWnd::selectLanguage() { QString file = QFileDialog::getOpenFileName( @@ -163,7 +208,9 @@ bool MainWnd::selectLanguage() if( file.isNull() ) return false; - bool ret = loadTranslation( file ); + languageFile = file; + + bool ret = loadTranslation(); ret &= enableTranslation( true ); if( ret == false ) { @@ -173,9 +220,11 @@ bool MainWnd::selectLanguage() } -bool MainWnd::loadTranslation( QString file ) +bool MainWnd::loadTranslation() { - if( !file.endsWith( tr( ".qm" ), Qt::CaseInsensitive ) ) return false; + settings->setValue( "App/language_file", languageFile ); + if( !languageFile.endsWith( tr( ".qm" ), Qt::CaseInsensitive ) ) return false; + QString file = languageFile; // remove current translation enableTranslation( false ); diff --git a/src/qt/MainWnd.h b/src/qt/MainWnd.h index 70200b8a..30d29168 100644 --- a/src/qt/MainWnd.h +++ b/src/qt/MainWnd.h @@ -26,19 +26,24 @@ class MainWnd : public QMainWindow Q_OBJECT public: - MainWnd( QWidget *parent = 0, QTranslator **trans = 0 ); + MainWnd( QTranslator **trans, QSettings *settings, QWidget *parent = 0 ); ~MainWnd(); public slots: - bool loadTranslation( QString file ); + void closeEvent( QCloseEvent * ); private: + void loadSettings(); + void saveSettings(); + bool loadTranslation(); void createActions(); void createMenus(); void createDockWidgets(); bool createDisplay(); QTranslator **translator; + QString languageFile; + QSettings *settings; QMenu *fileMenu; QMenu *settingsMenu; QAction *enableTranslationAct; diff --git a/src/qt/main.cpp b/src/qt/main.cpp index ae150cf5..cfa69356 100644 --- a/src/qt/main.cpp +++ b/src/qt/main.cpp @@ -23,9 +23,11 @@ int main( int argc, char *argv[] ) { QApplication theApp( argc, argv ); + // create/open ini file for settings + QSettings settings( "vba-m.ini", QSettings::IniFormat, &theApp ); QTranslator *translator = 0; - MainWnd *mainWnd = new MainWnd( 0, &translator ); + MainWnd *mainWnd = new MainWnd( &translator, &settings, 0 ); mainWnd->show(); return theApp.exec();