ADDED support for saving/loading settings to/from an INI file.
This commit is contained in:
spacy51 2008-02-22 18:59:57 +00:00
parent 5c8a761aa4
commit 450df58520
3 changed files with 63 additions and 7 deletions

View File

@ -23,9 +23,10 @@
#include "sidewidget_cheats.h" #include "sidewidget_cheats.h"
MainWnd::MainWnd( QWidget *parent, QTranslator **trans ) MainWnd::MainWnd( QTranslator **trans, QSettings *settings, QWidget *parent )
: QMainWindow( parent ), : QMainWindow( parent ),
translator( trans ), translator( trans ),
settings( settings ),
fileMenu( 0 ), fileMenu( 0 ),
settingsMenu( 0 ), settingsMenu( 0 ),
toolsMenu( 0 ), toolsMenu( 0 ),
@ -40,6 +41,8 @@ MainWnd::MainWnd( QWidget *parent, QTranslator **trans )
createDockWidgets(); createDockWidgets();
createActions(); createActions();
createMenus(); 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() void MainWnd::createActions()
{ {
bool enabled, checked; bool enabled, checked;
@ -131,6 +169,7 @@ void MainWnd::createDockWidgets()
// Cheat Widget // Cheat Widget
dockWidget_cheats = new QDockWidget( tr( "Cheats" ), this ); 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 ); SideWidget_Cheats *sw_cheats = new SideWidget_Cheats( dockWidget_cheats );
dockWidget_cheats->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea ); dockWidget_cheats->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
dockWidget_cheats->setWidget( sw_cheats ); dockWidget_cheats->setWidget( sw_cheats );
@ -153,6 +192,12 @@ bool MainWnd::createDisplay()
} }
void MainWnd::closeEvent( QCloseEvent * )
{
saveSettings();
}
bool MainWnd::selectLanguage() bool MainWnd::selectLanguage()
{ {
QString file = QFileDialog::getOpenFileName( QString file = QFileDialog::getOpenFileName(
@ -163,7 +208,9 @@ bool MainWnd::selectLanguage()
if( file.isNull() ) return false; if( file.isNull() ) return false;
bool ret = loadTranslation( file ); languageFile = file;
bool ret = loadTranslation();
ret &= enableTranslation( true ); ret &= enableTranslation( true );
if( ret == false ) { 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 // remove current translation
enableTranslation( false ); enableTranslation( false );

View File

@ -26,19 +26,24 @@ class MainWnd : public QMainWindow
Q_OBJECT Q_OBJECT
public: public:
MainWnd( QWidget *parent = 0, QTranslator **trans = 0 ); MainWnd( QTranslator **trans, QSettings *settings, QWidget *parent = 0 );
~MainWnd(); ~MainWnd();
public slots: public slots:
bool loadTranslation( QString file ); void closeEvent( QCloseEvent * );
private: private:
void loadSettings();
void saveSettings();
bool loadTranslation();
void createActions(); void createActions();
void createMenus(); void createMenus();
void createDockWidgets(); void createDockWidgets();
bool createDisplay(); bool createDisplay();
QTranslator **translator; QTranslator **translator;
QString languageFile;
QSettings *settings;
QMenu *fileMenu; QMenu *fileMenu;
QMenu *settingsMenu; QMenu *settingsMenu;
QAction *enableTranslationAct; QAction *enableTranslationAct;

View File

@ -23,9 +23,11 @@
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
QApplication theApp( argc, argv ); QApplication theApp( argc, argv );
// create/open ini file for settings
QSettings settings( "vba-m.ini", QSettings::IniFormat, &theApp );
QTranslator *translator = 0; QTranslator *translator = 0;
MainWnd *mainWnd = new MainWnd( 0, &translator ); MainWnd *mainWnd = new MainWnd( &translator, &settings, 0 );
mainWnd->show(); mainWnd->show();
return theApp.exec(); return theApp.exec();