Skip to content

Commit

Permalink
GUI: Save the language preferences
Browse files Browse the repository at this point in the history
The language change only happens only upon restart.

Issue #210
  • Loading branch information
rakhimov committed Oct 17, 2017
1 parent 531150f commit 2b9d2c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions gui/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@
#include "preferencesdialog.h"
#include "ui_preferencesdialog.h"

#include <string>

#include <QMessageBox>

#include <boost/range/algorithm.hpp>

#include "guiassert.h"
#include "overload.h"

namespace scram {
namespace gui {

const char *const PreferencesDialog::m_languageToLocale[] = {"en", "ru_RU"};

PreferencesDialog::PreferencesDialog(QSettings *preferences,
QUndoStack *undoStack,
QTimer *autoSaveTimer,
Expand All @@ -31,6 +40,27 @@ PreferencesDialog::PreferencesDialog(QSettings *preferences,
{
ui->setupUi(this);

GUI_ASSERT(std::distance(std::begin(m_languageToLocale),
std::end(m_languageToLocale))
== ui->languageBox->count(), );
auto it = boost::find(m_languageToLocale,
preferences->value(QStringLiteral("language"))
.toString()
.toStdString());
auto it_end = std::end(m_languageToLocale);
if (it != it_end)
ui->languageBox->setCurrentIndex(std::distance(m_languageToLocale, it));
connect(ui->languageBox, OVERLOAD(QComboBox, currentIndexChanged, int),
preferences, [this, preferences](int index) {
QMessageBox::information(
this, tr("Restart Required"),
tr("The language change will take effect after an "
"application restart."));
preferences->setValue(
QStringLiteral("language"),
QString::fromLatin1(m_languageToLocale[index]));
});

if (undoStack->undoLimit()) {
ui->checkUndoLimit->setChecked(true);
ui->undoLimitBox->setValue(undoStack->undoLimit());
Expand Down
3 changes: 3 additions & 0 deletions gui/preferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class PreferencesDialog : public QDialog
~PreferencesDialog();

private:
/// Language to locale mapping in the same order as presented in the dialog.
static const char* const m_languageToLocale[];

std::unique_ptr<Ui::PreferencesDialog> ui;
};

Expand Down
5 changes: 5 additions & 0 deletions gui/preferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<string notr="true">English</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">Русский</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
Expand Down

0 comments on commit 2b9d2c1

Please sign in to comment.