Merge pull request #187 from samir-aguiar/master

Add language combobox to the Preferences menu
This commit is contained in:
Martin Kleusberg
2015-01-25 20:38:37 +01:00
7 changed files with 203 additions and 93 deletions
+1 -1
View File
@@ -109,7 +109,7 @@ set(SQLB_MISC
# Translation files
set(SQLB_TSS
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_cn.ts"
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_zh.ts"
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_de.ts"
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_fr.ts"
"${CMAKE_SOURCE_DIR}/src/translations/sqlb_ru.ts"
+12 -2
View File
@@ -8,6 +8,7 @@
#include "Application.h"
#include "MainWindow.h"
#include "PreferencesDialog.h"
Application::Application(int& argc, char** argv) :
QApplication(argc, argv)
@@ -25,12 +26,15 @@ Application::Application(int& argc, char** argv) :
// Load translations
bool ok;
QString name = QLocale::system().name();
QString name = PreferencesDialog::getSettingsValue("General", "language").toString();
// First of all try to load the application translation file.
m_translatorApp = new QTranslator(this);
ok = m_translatorApp->load("sqlb_" + name, "translations");
ok = m_translatorApp->load("sqlb_" + name,
QCoreApplication::applicationDirPath() + "/translations");
if (ok == true) {
PreferencesDialog::setSettingsValue("General", "language", name);
installTranslator(m_translatorApp);
// The application translation file has been found and loaded.
@@ -47,6 +51,12 @@ Application::Application(int& argc, char** argv) :
if (ok == true)
installTranslator(m_translatorQt);
}
else {
// Set the correct locale so that the program won't erroneously detect
// a language change when the user toggles settings for the first time.
// (it also prevents the program from always looking for a translation on launch)
PreferencesDialog::setSettingsValue("General", "language", "en_US");
}
// Parse command line
QString fileToOpen;
+60
View File
@@ -6,6 +6,8 @@
#include <QSettings>
#include <QFileDialog>
#include <QColorDialog>
#include <QMessageBox>
#include <QSortFilterProxyModel>
QHash<QString, QVariant> PreferencesDialog::m_hCache;
@@ -16,6 +18,15 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
ui->setupUi(this);
ui->treeSyntaxHighlighting->setColumnHidden(0, true);
// Model to sort the languages in the language combo box
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(ui->languageComboBox);
proxy->setSourceModel(ui->languageComboBox->model());
// Prevent setModel() from deleting the current model (now source of the proxy model)
ui->languageComboBox->model()->setParent(proxy);
ui->languageComboBox->setModel(proxy);
loadSettings();
}
@@ -63,6 +74,7 @@ void PreferencesDialog::loadSettings()
ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt());
ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList());
fillLanguageBox();
}
void PreferencesDialog::saveSettings()
@@ -89,6 +101,14 @@ void PreferencesDialog::saveSettings()
extList.append(item->text());
setSettingsValue("extensions", "list", extList);
// Warn about restarting to change language
QVariant newLanguage = ui->languageComboBox->itemData(ui->languageComboBox->currentIndex());
if (newLanguage != getSettingsValue("General", "language"))
QMessageBox::information(this, QApplication::applicationName(),
tr("The language will change after you restart the application."));
setSettingsValue("General", "language", newLanguage);
accept();
}
@@ -160,6 +180,10 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
if(group == "General" && name == "recentFileList")
return QStringList();
// General/language?
if(group == "General" && name == "language")
return QLocale::system().name();
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
@@ -238,3 +262,39 @@ void PreferencesDialog::removeExtension()
if(ui->listExtensions->currentIndex().isValid())
ui->listExtensions->takeItem(ui->listExtensions->currentIndex().row());
}
void PreferencesDialog::fillLanguageBox()
{
// Use the path relative to the main executable
QDir translationsDir(QCoreApplication::applicationDirPath() + "/translations",
"sqlb_*.qm");
// Add default language
ui->languageComboBox->addItem("English (United States)", "en_US");
foreach(const QFileInfo &file, translationsDir.entryInfoList())
{
QLocale locale(file.baseName().remove("sqlb_"));
// Prevent invalid locales from being added to the box
if(locale.name() == "C")
continue;
QString language = QLocale::languageToString(locale.language()) + " (" +
QLocale::countryToString(locale.country()) + ")";
ui->languageComboBox->addItem(language, locale.name());
}
ui->languageComboBox->model()->sort(0);
// Try to select the language for the stored locale
int index = ui->languageComboBox->findData(getSettingsValue("General", "language"),
Qt::UserRole, Qt::MatchExactly);
// If there's no translation for the current locale, default to English
if(index < 0)
index = ui->languageComboBox->findData("en_US", Qt::UserRole, Qt::MatchExactly);
ui->languageComboBox->setCurrentIndex(index);
}
+2
View File
@@ -38,6 +38,8 @@ private:
// This works similar to getSettingsValue but returns the default value instead of the value set by the user
static QVariant getSettingsDefaultValue(const QString& group, const QString& name);
void fillLanguageBox();
// Cache for storing the settings to avoid repeatedly reading the settings file all the time
static QHash<QString, QVariant> m_hCache;
};
+127 -89
View File
@@ -22,13 +22,89 @@
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>General</string>
</attribute>
<layout class="QFormLayout" name="formLayout_3">
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Default &amp;location</string>
</property>
<property name="buddy">
<cstring>locationEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Language</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="languageComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToMinimumContentsLength</enum>
</property>
<property name="minimumContentsLength">
<number>20</number>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>&amp;Database</string>
</attribute>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
<enum>QFormLayout::FieldsStayAtSizeHint</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="formAlignment">
<set>Qt::AlignHCenter|Qt::AlignTop</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
@@ -75,54 +151,6 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Default &amp;location</string>
</property>
<property name="buddy">
<cstring>locationEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Prefetch block size</string>
</property>
<property name="buddy">
<cstring>spinPrefetchSize</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="spinPrefetchSize">
<property name="minimum">
<number>255</number>
</property>
<property name="maximum">
<number>1000000</number>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Remove line breaks in schema view</string>
@@ -132,9 +160,42 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QCheckBox" name="checkHideSchemaLinebreaks"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Prefetch block size</string>
</property>
<property name="buddy">
<cstring>spinPrefetchSize</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="spinPrefetchSize">
<property name="minimum">
<number>255</number>
</property>
<property name="maximum">
<number>1000000</number>
</property>
</widget>
</item>
<item row="4" column="1">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>180</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
@@ -420,13 +481,6 @@
</layout>
</widget>
<tabstops>
<tabstop>tabWidget</tabstop>
<tabstop>encodingComboBox</tabstop>
<tabstop>foreignKeysCheckBox</tabstop>
<tabstop>locationEdit</tabstop>
<tabstop>setLocationButton</tabstop>
<tabstop>checkHideSchemaLinebreaks</tabstop>
<tabstop>spinPrefetchSize</tabstop>
<tabstop>treeSyntaxHighlighting</tabstop>
<tabstop>spinEditorFontSize</tabstop>
<tabstop>spinLogFontSize</tabstop>
@@ -470,38 +524,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>setLocationButton</sender>
<signal>clicked()</signal>
<receiver>PreferencesDialog</receiver>
<slot>chooseLocation()</slot>
<hints>
<hint type="sourcelabel">
<x>568</x>
<y>119</y>
</hint>
<hint type="destinationlabel">
<x>459</x>
<y>62</y>
</hint>
</hints>
</connection>
<connection>
<sender>treeSyntaxHighlighting</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>103</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>-1</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonAddExtension</sender>
<signal>clicked()</signal>
@@ -534,6 +556,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>treeSyntaxHighlighting</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>103</x>
<y>53</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>-1</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>saveSettings()</slot>
+1 -1
View File
@@ -87,7 +87,7 @@ FORMS += \
CipherDialog.ui
TRANSLATIONS += \
translations/sqlb_cn.ts \
translations/sqlb_zh.ts \
translations/sqlb_de.ts \
translations/sqlb_fr.ts \
translations/sqlb_ru.ts