changeset 2426:58abdd7d008b draft

Make it possible to set user interface language from options dialog
author Wladimir J. van der Laan <laanwj@gmail.com>
date Tue, 08 May 2012 23:03:41 +0200
parents aa46239b5b09
children 02acb28fdf41
files src/qt/optionsdialog.cpp src/qt/optionsmodel.cpp src/qt/optionsmodel.h src/qt/qvaluecombobox.cpp src/qt/qvaluecombobox.h
diffstat 5 files changed, 58 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/qt/optionsdialog.cpp
+++ b/src/qt/optionsdialog.cpp
@@ -19,6 +19,8 @@
 #include <QDoubleValidator>
 #include <QRegExpValidator>
 #include <QDialogButtonBox>
+#include <QDir>
+#include <QMessageBox>
 
 class OptionsPage: public QWidget
 {
@@ -66,8 +68,12 @@
 
     virtual void setMapper(MonitoredDataMapper *mapper);
 private:
+    QValueComboBox *lang;
     QValueComboBox *unit;
     QCheckBox *display_addresses;
+    bool restart_warning_displayed;
+private slots:
+    void showRestartWarning();
 };
 
 class NetworkOptionsPage: public OptionsPage
@@ -230,12 +236,33 @@
 
 /* Display options */
 DisplayOptionsPage::DisplayOptionsPage(QWidget *parent):
-        OptionsPage(parent)
+    OptionsPage(parent), restart_warning_displayed(false)
 {
     setWindowTitle(tr("Display"));
 
     QVBoxLayout *layout = new QVBoxLayout();
 
+    QHBoxLayout *lang_hbox = new QHBoxLayout();
+    lang_hbox->addSpacing(18);
+    QLabel *lang_label = new QLabel(tr("User Interface &Language: "));
+    lang_hbox->addWidget(lang_label);
+    lang = new QValueComboBox(this);
+    // Make list of languages
+    QDir translations(":translations");
+    lang->addItem("(default)", QVariant(""));
+    foreach(const QString &langStr, translations.entryList())
+    {
+        lang->addItem(langStr, QVariant(langStr));
+    }
+
+    lang->setToolTip(tr("The user interface language can be set here. This setting will only take effect after restarting Bitcoin."));
+    connect(lang, SIGNAL(activated(int)), this, SLOT(showRestartWarning()));
+
+    lang_label->setBuddy(lang);
+    lang_hbox->addWidget(lang);
+
+    layout->addLayout(lang_hbox);
+
     QHBoxLayout *unit_hbox = new QHBoxLayout();
     unit_hbox->addSpacing(18);
     QLabel *unit_label = new QLabel(tr("&Unit to show amounts in: "));
@@ -259,10 +286,20 @@
 
 void DisplayOptionsPage::setMapper(MonitoredDataMapper *mapper)
 {
+    mapper->addMapping(lang, OptionsModel::Language);
     mapper->addMapping(unit, OptionsModel::DisplayUnit);
     mapper->addMapping(display_addresses, OptionsModel::DisplayAddresses);
 }
 
+void DisplayOptionsPage::showRestartWarning()
+{
+    if(!restart_warning_displayed)
+    {
+        QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Bitcoin."), QMessageBox::Ok);
+        restart_warning_displayed = true;
+    }
+}
+
 /* Window options */
 WindowOptionsPage::WindowOptionsPage(QWidget *parent):
         OptionsPage(parent)
--- a/src/qt/optionsmodel.cpp
+++ b/src/qt/optionsmodel.cpp
@@ -21,6 +21,7 @@
     fMinimizeToTray = settings.value("fMinimizeToTray", false).toBool();
     fMinimizeOnClose = settings.value("fMinimizeOnClose", false).toBool();
     nTransactionFee = settings.value("nTransactionFee").toLongLong();
+    language = settings.value("language", "").toString();
 
     // These are shared with core bitcoin; we want
     // command-line options to override the GUI settings:
@@ -30,6 +31,8 @@
         SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString());
     if (settings.contains("detachDB"))
         SoftSetBoolArg("-detachdb", settings.value("detachDB").toBool());
+    if (!language.isEmpty())
+        SoftSetArg("-lang", language.toStdString());
 }
 
 bool OptionsModel::Upgrade()
@@ -125,6 +128,8 @@
             return QVariant(bDisplayAddresses);
         case DetachDatabases:
             return QVariant(fDetachDB);
+        case Language:
+            return settings.value("language", "");
         default:
             return QVariant();
         }
@@ -213,6 +218,10 @@
             settings.setValue("detachDB", fDetachDB);
             }
             break;
+        case Language: {
+            settings.setValue("language", value);
+            }
+            break;
         default:
             break;
         }
--- a/src/qt/optionsmodel.h
+++ b/src/qt/optionsmodel.h
@@ -27,6 +27,7 @@
         DisplayUnit, // BitcoinUnits::Unit
         DisplayAddresses, // bool
         DetachDatabases, // bool
+        Language, // QString
         OptionIDRowCount,
     };
 
@@ -45,11 +46,13 @@
     bool getMinimizeOnClose();
     int getDisplayUnit();
     bool getDisplayAddresses();
+    QString getLanguage() { return language; }
 private:
     int nDisplayUnit;
     bool bDisplayAddresses;
     bool fMinimizeToTray;
     bool fMinimizeOnClose;
+    QString language;
 signals:
     void displayUnitChanged(int unit);
 
--- a/src/qt/qvaluecombobox.cpp
+++ b/src/qt/qvaluecombobox.cpp
@@ -6,12 +6,12 @@
     connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(handleSelectionChanged(int)));
 }
 
-int QValueComboBox::value() const
+QVariant QValueComboBox::value() const
 {
-    return itemData(currentIndex(), role).toInt();
+    return itemData(currentIndex(), role);
 }
 
-void QValueComboBox::setValue(int value)
+void QValueComboBox::setValue(const QVariant &value)
 {
     setCurrentIndex(findData(value, role));
 }
--- a/src/qt/qvaluecombobox.h
+++ b/src/qt/qvaluecombobox.h
@@ -2,19 +2,20 @@
 #define QVALUECOMBOBOX_H
 
 #include <QComboBox>
+#include <QVariant>
 
 /* QComboBox that can be used with QDataWidgetMapper to select ordinal values from a model. */
 class QValueComboBox : public QComboBox
 {
     Q_OBJECT
-    Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged USER true)
+    Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged USER true)
 public:
     explicit QValueComboBox(QWidget *parent = 0);
 
-    int value() const;
-    void setValue(int value);
+    QVariant value() const;
+    void setValue(const QVariant &value);
 
-    /** Specify model role to use as ordinal value */
+    /** Specify model role to use as ordinal value (defaults to Qt::UserRole) */
     void setRole(int role);
 
 signals: