Qt:
ADDED support for saving/loading settings to/from an INI file.
This commit is contained in:
parent
5c8a761aa4
commit
450df58520
|
@ -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 );
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue