added functionality to change app language via settings

This commit is contained in:
vladimir.kuznetsov 2023-06-30 10:40:43 +09:00
parent d0c9c1043c
commit 464d77dfb5
42 changed files with 3333 additions and 1677 deletions

View file

@ -2,9 +2,9 @@
#include <QStandardPaths>
#include "defines.h"
#include "logger.h"
#include "utilities.h"
#include "version.h"
SettingsController::SettingsController(const QSharedPointer<ServersModel> &serversModel,
const QSharedPointer<ContainersModel> &containersModel,

View file

@ -0,0 +1,62 @@
#include "languageModel.h"
LanguageModel::LanguageModel(std::shared_ptr<Settings> settings, QObject *parent)
: m_settings(settings), QAbstractListModel(parent)
{
QMetaEnum metaEnum = QMetaEnum::fromType<LanguageSettings::AvailableLanguageEnum>();
for (int i = 0; i < metaEnum.keyCount(); i++) {
m_availableLanguages.push_back(
LanguageModelData { metaEnum.valueToKey(i), static_cast<LanguageSettings::AvailableLanguageEnum>(i) });
}
}
int LanguageModel::rowCount(const QModelIndex &parent) const
{
return static_cast<int>(m_availableLanguages.size());
}
QVariant LanguageModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_availableLanguages.size())) {
return QVariant();
}
switch (role) {
case NameRole: {
return m_availableLanguages[index.row()].name;
break;
}
case IndexRole: {
return static_cast<int>(m_availableLanguages[index.row()].index);
break;
}
}
return QVariant();
}
QHash<int, QByteArray> LanguageModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "languageName";
roles[IndexRole] = "languageIndex";
return roles;
}
void LanguageModel::changeLanguage(const LanguageSettings::AvailableLanguageEnum language)
{
switch (language) {
case LanguageSettings::AvailableLanguageEnum::English: emit updateTranslations(QLocale::English); break;
case LanguageSettings::AvailableLanguageEnum::Russian: emit updateTranslations(QLocale::Russian); break;
default: emit updateTranslations(QLocale::English); break;
}
}
int LanguageModel::getCurrentLanguageIndex()
{
auto locale = m_settings->getAppLanguage();
switch (locale.language()) {
case QLocale::English: return static_cast<int>(LanguageSettings::AvailableLanguageEnum::English); break;
case QLocale::Russian: return static_cast<int>(LanguageSettings::AvailableLanguageEnum::Russian); break;
default: return static_cast<int>(LanguageSettings::AvailableLanguageEnum::English); break;
}
}

View file

@ -0,0 +1,62 @@
#ifndef LANGUAGEMODEL_H
#define LANGUAGEMODEL_H
#include <QAbstractListModel>
#include <QQmlEngine>
#include "settings.h"
namespace LanguageSettings
{
Q_NAMESPACE
enum class AvailableLanguageEnum {
English,
Russian
};
Q_ENUM_NS(AvailableLanguageEnum)
static void declareQmlAvailableLanguageEnum()
{
qmlRegisterUncreatableMetaObject(LanguageSettings::staticMetaObject, "AvailableLanguageEnum", 1, 0,
"AvailableLanguageEnum", QString());
}
}
struct LanguageModelData
{
QString name;
LanguageSettings::AvailableLanguageEnum index;
};
class LanguageModel : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
NameRole = Qt::UserRole + 1,
IndexRole
};
LanguageModel(std::shared_ptr<Settings> settings, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
public slots:
void changeLanguage(const LanguageSettings::AvailableLanguageEnum language);
int getCurrentLanguageIndex();
signals:
void updateTranslations(const QLocale &locale);
protected:
QHash<int, QByteArray> roleNames() const override;
private:
QVector<LanguageModelData> m_availableLanguages;
std::shared_ptr<Settings> m_settings;
};
#endif // LANGUAGEMODEL_H

View file

@ -18,12 +18,14 @@ DrawerType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
spacing: 0
Header2TextType {
Layout.fillWidth: true
Layout.topMargin: 24
Layout.rightMargin: 16
Layout.leftMargin: 16
Layout.bottomMargin: 32
Layout.alignment: Qt.AlignHCenter
text: "Данные для подключения"

View file

@ -0,0 +1,137 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "../Controls2"
import "../Controls2/TextTypes"
DrawerType {
id: root
width: parent.width
height: parent.height * 0.9
ColumnLayout {
id: backButton
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 16
BackButtonType {
backButtonImage: "qrc:/images/controls/arrow-left.svg"
backButtonFunction: function() {
root.close()
}
}
}
FlickableType {
anchors.top: backButton.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
contentHeight: content.implicitHeight
ColumnLayout {
id: content
anchors.fill: parent
Header2Type {
id: header
Layout.fillWidth: true
Layout.topMargin: 16
Layout.rightMargin: 16
Layout.leftMargin: 16
headerText: qsTr("Choose language")
}
ListView {
id: listView
Layout.fillWidth: true
height: listView.contentItem.height
clip: true
interactive: false
model: LanguageModel
currentIndex: LanguageModel.getCurrentLanguageIndex()
ButtonGroup {
id: buttonGroup
}
delegate: Item {
implicitWidth: root.width
implicitHeight: content.implicitHeight
ColumnLayout {
id: delegateContent
anchors.fill: parent
RadioButton {
id: radioButton
implicitWidth: parent.width
implicitHeight: radioButtonContent.implicitHeight
hoverEnabled: true
indicator: Rectangle {
anchors.fill: parent
color: radioButton.hovered ? "#2C2D30" : "#1C1D21"
Behavior on color {
PropertyAnimation { duration: 200 }
}
}
RowLayout {
id: radioButtonContent
anchors.fill: parent
anchors.rightMargin: 16
anchors.leftMargin: 16
spacing: 0
z: 1
ParagraphTextType {
Layout.fillWidth: true
Layout.topMargin: 20
Layout.bottomMargin: 20
text: languageName
}
Image {
source: "qrc:/images/controls/check.svg"
visible: radioButton.checked
width: 24
height: 24
Layout.rightMargin: 8
}
}
ButtonGroup.group: buttonGroup
checked: listView.currentIndex === index
onClicked: {
listView.currentIndex = index
LanguageModel.changeLanguage(languageIndex)
}
}
}
}
}
}
}
}

View file

@ -105,8 +105,6 @@ DrawerType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 16
backButtonFunction: function() {

View file

@ -1,10 +0,0 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "../Controls2"
import "../Controls2/TextTypes"
Item {
}

View file

@ -17,6 +17,7 @@ Item {
id: content
anchors.fill: parent
anchors.leftMargin: 8
ImageButtonType {
image: backButtonImage

View file

@ -1,6 +1,8 @@
import QtQuick
import QtQuick.Controls
import "TextTypes"
Button {
id: root
@ -46,12 +48,8 @@ Button {
cursorShape: Qt.PointingHandCursor
}
contentItem: Text {
contentItem: ButtonTextType {
anchors.fill: background
font.family: "PT Root UI VF"
font.styleName: "normal"
font.weight: 400
font.pixelSize: 16
color: textColor
text: root.text
horizontalAlignment: Text.AlignHCenter

View file

@ -3,6 +3,10 @@ import QtQuick.Layouts
Rectangle {
Layout.fillWidth: true
Layout.leftMargin: 16
Layout.rightMargin: 16
height: 1
color: "#2C2D30"
}

View file

@ -146,8 +146,6 @@ Item {
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 16
anchors.leftMargin: 16
anchors.rightMargin: 16
BackButtonType {
backButtonImage: root.headerBackButtonImage

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 24
lineHeight: 24
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 16
font.weight: Font.Medium
font.weight: 500
font.family: "PT Root UI VF"
wrapMode: Text.WordWrap

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 16
lineHeight: 16
lineHeightMode: Text.FixedHeight
color: "#0E0E11"
font.pixelSize: 13
font.weight: Font.Normal
font.weight: 400
font.family: "PT Root UI VF"
font.letterSpacing: 0.02

View file

@ -1,13 +1,14 @@
import QtQuick
Text {
height: 38
lineHeight: 38
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 36
font.weight: Font.Bold
font.weight: 700
font.family: "PT Root UI VF"
font.letterSpacing: -0.03
font.letterSpacing: -1.08
wrapMode: Text.WordWrap
}

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 30
lineHeight: 30
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 25
font.weight: Font.Bold
font.weight: 700
font.family: "PT Root UI VF"
wrapMode: Text.WordWrap

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 16
lineHeight: 16
lineHeightMode: Text.FixedHeight
color: "#878B91"
font.pixelSize: 13
font.weight: Font.Normal
font.weight: 400
font.family: "PT Root UI VF"
font.letterSpacing: 0.02

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 21.6
lineHeight: 21.6
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 18
font.weight: Font.Normal
font.weight: 400
font.family: "PT Root UI VF"
wrapMode: Text.WordWrap

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 24
lineHeight: 24
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 16
font.weight: Font.Normal
font.weight: 400
font.family: "PT Root UI VF"
wrapMode: Text.WordWrap

View file

@ -1,11 +1,12 @@
import QtQuick
Text {
height: 20
lineHeight: 20
lineHeightMode: Text.FixedHeight
color: "#D7D8DB"
font.pixelSize: 14
font.weight: Font.Normal
font.weight: 400
font.family: "PT Root UI VF"
wrapMode: Text.WordWrap

View file

@ -19,8 +19,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -8,6 +8,7 @@ import "./"
import "../Controls2"
import "../Config"
import "../Controls2/TextTypes"
import "../Components"
PageType {
id: root
@ -18,8 +19,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}
@ -52,10 +51,14 @@ PageType {
rightImageSource: "qrc:/images/controls/chevron-right.svg"
clickedFunction: function() {
selectLanguageDrawer.open()
}
}
DividerType {}
SelectLanguageDrawer {
id: selectLanguageDrawer
}
LabelWithButtonType {
Layout.fillWidth: true

View file

@ -18,8 +18,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -17,8 +17,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -18,8 +18,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -42,14 +42,14 @@ PageType {
id: content
Layout.topMargin: 20
Layout.leftMargin: 16
Layout.rightMargin: 16
BackButtonType {
}
HeaderType {
Layout.fillWidth: true
Layout.leftMargin: 16
Layout.rightMargin: 16
actionButtonImage: "qrc:/images/controls/edit-3.svg"

View file

@ -25,14 +25,14 @@ PageType {
anchors.right: parent.right
anchors.topMargin: 20
anchors.leftMargin: 16
anchors.rightMargin: 16
BackButtonType {
}
HeaderType {
Layout.fillWidth: true
Layout.leftMargin: 16
Layout.rightMargin: 16
actionButtonImage: "qrc:/images/controls/plus.svg"

View file

@ -26,19 +26,18 @@ PageType {
anchors.left: parent.left
anchors.right: parent.right
spacing: 0
BackButtonType {
Layout.topMargin: 20
Layout.rightMargin: 16
Layout.leftMargin: 16
}
HeaderType {
Layout.fillWidth: true
Layout.topMargin: 20
Layout.topMargin: 8
Layout.rightMargin: 16
Layout.leftMargin: 16
headerText: "Подключение к серверу"
descriptionText: "Не используйте код подключения из публичных источников. Его могли создать, чтобы перехватывать ваши данные.\n
Всё в порядке, если код передал друг."
@ -46,7 +45,7 @@ PageType {
Header2TextType {
Layout.fillWidth: true
Layout.topMargin: 32
Layout.topMargin: 48
Layout.rightMargin: 16
Layout.leftMargin: 16

View file

@ -18,8 +18,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -36,8 +36,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -63,6 +63,8 @@ PageType {
id: backButton
Layout.topMargin: 20
Layout.rightMargin: -16
Layout.leftMargin: -16
}
HeaderType {
@ -107,8 +109,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 16
backButtonFunction: function() {

View file

@ -42,8 +42,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
spacing: 16
@ -52,11 +50,23 @@ PageType {
width: parent.width
}
HeaderType {
Item {
width: parent.width
height: header.implicitHeight
headerText: "Протокол подключения"
descriptionText: "Выберите более приоритетный для вас. Позже можно будет установить остальные протоколы и доп сервисы, вроде DNS-прокси и SFTP."
HeaderType {
id: header
anchors.fill: parent
anchors.leftMargin: 16
anchors.rightMargin: 16
width: parent.width
headerText: "Протокол подключения"
descriptionText: "Выберите более приоритетный для вас. Позже можно будет установить остальные протоколы и доп сервисы, вроде DNS-прокси и SFTP."
}
}
ListView {
@ -78,8 +88,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: -16
anchors.leftMargin: -16
LabelWithButtonType {
id: container

View file

@ -34,6 +34,7 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
spacing: 0
Image {
id: image

View file

@ -24,8 +24,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
spacing: 16
@ -35,6 +33,8 @@ PageType {
HeaderType {
Layout.fillWidth: true
Layout.rightMargin: 16
Layout.leftMargin: 16
headerText: qsTr("Connection key")
descriptionText: qsTr("A line that starts with vpn://...")
@ -45,6 +45,8 @@ PageType {
Layout.fillWidth: true
Layout.topMargin: 32
Layout.rightMargin: 16
Layout.leftMargin: 16
headerText: qsTr("Key")
textFieldPlaceholderText: "vpn://"

View file

@ -42,8 +42,6 @@ PageType {
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.rightMargin: 16
anchors.leftMargin: 16
anchors.topMargin: 20
}

View file

@ -185,8 +185,6 @@ PageType {
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 16
anchors.leftMargin: 16
anchors.rightMargin: 16
BackButtonType {
backButtonImage: "qrc:/images/controls/arrow-left.svg"