FreeCAD

Форк
0
/
GeneralSettingsWidget.cpp 
241 строка · 9.4 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/****************************************************************************
3
 *                                                                          *
4
 *   Copyright (c) 2024 The FreeCAD Project Association AISBL               *
5
 *                                                                          *
6
 *   This file is part of FreeCAD.                                          *
7
 *                                                                          *
8
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
9
 *   under the terms of the GNU Lesser General Public License as            *
10
 *   published by the Free Software Foundation, either version 2.1 of the   *
11
 *   License, or (at your option) any later version.                        *
12
 *                                                                          *
13
 *   FreeCAD is distributed in the hope that it will be useful, but         *
14
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       *
16
 *   Lesser General Public License for more details.                        *
17
 *                                                                          *
18
 *   You should have received a copy of the GNU Lesser General Public       *
19
 *   License along with FreeCAD. If not, see                                *
20
 *   <https://www.gnu.org/licenses/>.                                       *
21
 *                                                                          *
22
 ***************************************************************************/
23

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
#include <QApplication>
27
#include <QComboBox>
28
#include <QGridLayout>
29
#include <QLabel>
30
#include <QLayout>
31
#include <QToolButton>
32
#include <QVBoxLayout>
33
#include <QWidget>
34
#endif
35

36
#include "GeneralSettingsWidget.h"
37
#include <gsl/pointers>
38
#include <App/Application.h>
39
#include <Base/Parameter.h>
40
#include <Base/UnitsApi.h>
41
#include <Gui/Language/Translator.h>
42
#include <Gui/NavigationStyle.h>
43

44
using namespace StartGui;
45

46
GeneralSettingsWidget::GeneralSettingsWidget(QWidget* parent)
47
    : QWidget(parent)
48
    , _languageLabel {nullptr}
49
    , _unitSystemLabel {nullptr}
50
    , _navigationStyleLabel {nullptr}
51
    , _languageComboBox {nullptr}
52
    , _unitSystemComboBox {nullptr}
53
    , _navigationStyleComboBox {nullptr}
54
{
55
    setObjectName(QLatin1String("GeneralSettingsWidget"));
56
    setupUi();
57
    qApp->installEventFilter(this);
58
}
59

60
void GeneralSettingsWidget::setupUi()
61
{
62
    if (layout()) {
63
        qDeleteAll(findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly));
64
        delete layout();
65
    }
66
    _languageLabel = gsl::owner<QLabel*>(new QLabel);
67
    _navigationStyleLabel = gsl::owner<QLabel*>(new QLabel);
68
    _unitSystemLabel = gsl::owner<QLabel*>(new QLabel);
69
    createLanguageComboBox();
70
    createUnitSystemComboBox();
71
    createNavigationStyleComboBox();
72
    createHorizontalUi();
73
    retranslateUi();
74
}
75

76
void GeneralSettingsWidget::createHorizontalUi()
77
{
78
    auto mainLayout = gsl::owner<QHBoxLayout*>(new QHBoxLayout(this));
79
    const int extraSpace {36};
80
    mainLayout->addWidget(_languageLabel);
81
    mainLayout->addWidget(_languageComboBox);
82
    mainLayout->addSpacing(extraSpace);
83
    mainLayout->addWidget(_unitSystemLabel);
84
    mainLayout->addWidget(_unitSystemComboBox);
85
    mainLayout->addSpacing(extraSpace);
86
    mainLayout->addWidget(_navigationStyleLabel);
87
    mainLayout->addWidget(_navigationStyleComboBox);
88
}
89

90

91
QString GeneralSettingsWidget::createLabelText(const QString& translatedText) const
92
{
93
    static const auto h2Start = QLatin1String("<h2>");
94
    static const auto h2End = QLatin1String("</h2>");
95
    return h2Start + translatedText + h2End;
96
}
97

98
gsl::owner<QComboBox*> GeneralSettingsWidget::createLanguageComboBox()
99
{
100
    ParameterGrp::handle hGrp =
101
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
102
    auto langToStr = Gui::Translator::instance()->activeLanguage();
103
    QByteArray language = hGrp->GetASCII("Language", langToStr.c_str()).c_str();
104
    auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
105
    comboBox->addItem(QString::fromLatin1("English"), QByteArray("English"));
106
    Gui::TStringMap list = Gui::Translator::instance()->supportedLocales();
107
    int index {1};
108
    for (auto it = list.begin(); it != list.end(); ++it, ++index) {
109
        QByteArray lang = it->first.c_str();
110
        QString langname = QString::fromLatin1(lang.constData());
111

112
        if (it->second == "sr-CS") {
113
            // Qt does not treat sr-CS (Serbian, Latin) as a Latin-script variant by default: this
114
            // forces it to do so.
115
            it->second = "sr_Latn";
116
        }
117

118
        QLocale locale(QString::fromLatin1(it->second.c_str()));
119
        QString native = locale.nativeLanguageName();
120
        if (!native.isEmpty()) {
121
            if (native[0].isLetter()) {
122
                native[0] = native[0].toUpper();
123
            }
124
            langname = native;
125
        }
126

127
        comboBox->addItem(langname, lang);
128
        if (language == lang) {
129
            comboBox->setCurrentIndex(index);
130
        }
131
    }
132
    if (QAbstractItemModel* model = comboBox->model()) {
133
        model->sort(0);
134
    }
135
    _languageComboBox = comboBox;
136
    connect(_languageComboBox,
137
            qOverload<int>(&QComboBox::currentIndexChanged),
138
            this,
139
            &GeneralSettingsWidget::onLanguageChanged);
140
    return comboBox;
141
}
142

143
gsl::owner<QComboBox*> GeneralSettingsWidget::createUnitSystemComboBox()
144
{
145
    // Contents are created in retranslateUi()
146
    auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
147
    _unitSystemComboBox = comboBox;
148
    connect(_unitSystemComboBox,
149
            qOverload<int>(&QComboBox::currentIndexChanged),
150
            this,
151
            &GeneralSettingsWidget::onUnitSystemChanged);
152
    return comboBox;
153
}
154

155
gsl::owner<QComboBox*> GeneralSettingsWidget::createNavigationStyleComboBox()
156
{
157
    // Contents are created in retranslateUi()
158
    auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
159
    _navigationStyleComboBox = comboBox;
160
    connect(_navigationStyleComboBox,
161
            qOverload<int>(&QComboBox::currentIndexChanged),
162
            this,
163
            &GeneralSettingsWidget::onNavigationStyleChanged);
164
    return comboBox;
165
}
166

167
void GeneralSettingsWidget::onLanguageChanged(int index)
168
{
169
    if (index < 0) {
170
        return;  // happens when clearing the combo box in retranslateUi()
171
    }
172
    Gui::Translator::instance()->activateLanguage(
173
        _languageComboBox->itemData(index).toByteArray().data());
174
    ParameterGrp::handle hGrp =
175
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
176
    auto langToStr = Gui::Translator::instance()->activeLanguage();
177
    hGrp->SetASCII("Language", langToStr.c_str());
178
}
179

180
void GeneralSettingsWidget::onUnitSystemChanged(int index)
181
{
182
    if (index < 0) {
183
        return;  // happens when clearing the combo box in retranslateUi()
184
    }
185
    Base::UnitsApi::setSchema(static_cast<Base::UnitSystem>(index));
186
    ParameterGrp::handle hGrp =
187
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units");
188
    hGrp->SetInt("UserSchema", index);
189
}
190

191
void GeneralSettingsWidget::onNavigationStyleChanged(int index)
192
{
193
    if (index < 0) {
194
        return;  // happens when clearing the combo box in retranslateUi()
195
    }
196
    auto navStyleName = _navigationStyleComboBox->itemData(index).toByteArray();
197
    ParameterGrp::handle hGrp =
198
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
199
    hGrp->SetASCII("NavigationStyle", navStyleName.constData());
200
}
201

202
bool GeneralSettingsWidget::eventFilter(QObject* object, QEvent* event)
203
{
204
    if (object == this && event->type() == QEvent::LanguageChange) {
205
        this->retranslateUi();
206
    }
207
    return QWidget::eventFilter(object, event);
208
}
209

210
void GeneralSettingsWidget::retranslateUi()
211
{
212
    _languageLabel->setText(createLabelText(tr("Language")));
213
    _unitSystemLabel->setText(createLabelText(tr("Unit System")));
214

215
    _unitSystemComboBox->clear();
216
    ParameterGrp::handle hGrpUnits =
217
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Units");
218
    auto userSchema = hGrpUnits->GetInt("UserSchema", 0);
219
    int num = static_cast<int>(Base::UnitSystem::NumUnitSystemTypes);
220
    for (int i = 0; i < num; i++) {
221
        QString item = Base::UnitsApi::getDescription(static_cast<Base::UnitSystem>(i));
222
        _unitSystemComboBox->addItem(item, i);
223
    }
224
    _unitSystemComboBox->setCurrentIndex(userSchema);
225

226
    _navigationStyleLabel->setText(createLabelText(tr("Navigation Style")));
227
    _navigationStyleComboBox->clear();
228
    ParameterGrp::handle hGrpNav =
229
        App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
230
    auto navStyleName =
231
        hGrpNav->GetASCII("NavigationStyle", Gui::CADNavigationStyle::getClassTypeId().getName());
232
    std::map<Base::Type, std::string> styles = Gui::UserNavigationStyle::getUserFriendlyNames();
233
    for (const auto& style : styles) {
234
        QByteArray data(style.first.getName());
235
        QString name = QApplication::translate(style.first.getName(), style.second.c_str());
236
        _navigationStyleComboBox->addItem(name, data);
237
        if (navStyleName == style.first.getName()) {
238
            _navigationStyleComboBox->setCurrentIndex(_navigationStyleComboBox->count() - 1);
239
        }
240
    }
241
}
242

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.