File size: 9,619 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
 *                                                                          *
 *   Copyright (c) 2024 The FreeCAD Project Association AISBL               *
 *                                                                          *
 *   This file is part of FreeCAD.                                          *
 *                                                                          *
 *   FreeCAD is free software: you can redistribute it and/or modify it     *
 *   under the terms of the GNU Lesser General Public License as            *
 *   published by the Free Software Foundation, either version 2.1 of the   *
 *   License, or (at your option) any later version.                        *
 *                                                                          *
 *   FreeCAD is distributed in the hope that it will be useful, but         *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU Lesser General Public       *
 *   License along with FreeCAD. If not, see                                *
 *   <https://www.gnu.org/licenses/>.                                       *
 *                                                                          *
 ***************************************************************************/

#include <QApplication>
#include <QComboBox>
#include <QGridLayout>
#include <QLabel>
#include <QLayout>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWidget>


#include <algorithm>
#include "GeneralSettingsWidget.h"
#include <gsl/pointers>
#include <App/Application.h>
#include <Base/Parameter.h>
#include <Base/UnitsApi.h>
#include <Gui/Language/Translator.h>
#include <Gui/Navigation/NavigationStyle.h>

using namespace StartGui;

GeneralSettingsWidget::GeneralSettingsWidget(QWidget* parent)
    : QWidget(parent)
    , _languageLabel {nullptr}
    , _unitSystemLabel {nullptr}
    , _navigationStyleLabel {nullptr}
    , _languageComboBox {nullptr}
    , _unitSystemComboBox {nullptr}
    , _navigationStyleComboBox {nullptr}
{
    setObjectName(QLatin1String("GeneralSettingsWidget"));
    setupUi();
    qApp->installEventFilter(this);
}

void GeneralSettingsWidget::setupUi()
{
    if (layout()) {
        qDeleteAll(findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly));
        delete layout();
    }
    _languageLabel = gsl::owner<QLabel*>(new QLabel);
    _navigationStyleLabel = gsl::owner<QLabel*>(new QLabel);
    _unitSystemLabel = gsl::owner<QLabel*>(new QLabel);
    createLanguageComboBox();
    createUnitSystemComboBox();
    createNavigationStyleComboBox();
    createHorizontalUi();
    retranslateUi();
}

void GeneralSettingsWidget::createHorizontalUi()
{
    auto mainLayout = gsl::owner<QHBoxLayout*>(new QHBoxLayout(this));
    const int extraSpace {36};
    mainLayout->addWidget(_languageLabel);
    mainLayout->addWidget(_languageComboBox);
    mainLayout->addSpacing(extraSpace);
    mainLayout->addWidget(_unitSystemLabel);
    mainLayout->addWidget(_unitSystemComboBox);
    mainLayout->addSpacing(extraSpace);
    mainLayout->addWidget(_navigationStyleLabel);
    mainLayout->addWidget(_navigationStyleComboBox);
}


QString GeneralSettingsWidget::createLabelText(const QString& translatedText) const
{
    static const auto h2Start = QLatin1String("<h2>");
    static const auto h2End = QLatin1String("</h2>");
    return h2Start + translatedText + h2End;
}

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

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

        QLocale locale(QString::fromLatin1(it->second.c_str()));
        QString native = locale.nativeLanguageName();
        if (!native.isEmpty()) {
            if (native[0].isLetter()) {
                native[0] = native[0].toUpper();
            }
            langname = native;
        }

        comboBox->addItem(langname, lang);
        if (language == lang) {
            comboBox->setCurrentIndex(index);
        }
    }
    if (QAbstractItemModel* model = comboBox->model()) {
        model->sort(0);
    }
    _languageComboBox = comboBox;
    connect(
        _languageComboBox,
        qOverload<int>(&QComboBox::currentIndexChanged),
        this,
        &GeneralSettingsWidget::onLanguageChanged
    );
    return comboBox;
}

gsl::owner<QComboBox*> GeneralSettingsWidget::createUnitSystemComboBox()
{
    // Contents are created in retranslateUi()
    auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
    _unitSystemComboBox = comboBox;
    connect(
        _unitSystemComboBox,
        qOverload<int>(&QComboBox::currentIndexChanged),
        this,
        &GeneralSettingsWidget::onUnitSystemChanged
    );
    return comboBox;
}

gsl::owner<QComboBox*> GeneralSettingsWidget::createNavigationStyleComboBox()
{
    // Contents are created in retranslateUi()
    auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
    _navigationStyleComboBox = comboBox;
    connect(
        _navigationStyleComboBox,
        qOverload<int>(&QComboBox::currentIndexChanged),
        this,
        &GeneralSettingsWidget::onNavigationStyleChanged
    );
    return comboBox;
}

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

void GeneralSettingsWidget::onUnitSystemChanged(int index)
{
    if (index < 0) {
        return;  // happens when clearing the combo box in retranslateUi()
    }
    Base::UnitsApi::setSchema(index);
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Units"
    );
    hGrp->SetInt("UserSchema", index);
}

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

bool GeneralSettingsWidget::eventFilter(QObject* object, QEvent* event)
{
    if (object == this && event->type() == QEvent::LanguageChange) {
        this->retranslateUi();
    }
    return QWidget::eventFilter(object, event);
}

void GeneralSettingsWidget::retranslateUi()
{
    _languageLabel->setText(createLabelText(tr("Language")));
    _unitSystemLabel->setText(createLabelText(tr("Unit System")));

    _unitSystemComboBox->clear();

    const ParameterGrp::handle hGrpUnits = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/Units"
    );
    auto userSchema = hGrpUnits->GetInt("UserSchema", 0);

    auto addItem = [&, index {0}](const std::string& item) mutable {
        _unitSystemComboBox->addItem(QString::fromStdString(item), index++);
    };
    auto descriptions = Base::UnitsApi::getDescriptions();
    std::for_each(descriptions.begin(), descriptions.end(), addItem);

    _unitSystemComboBox->setCurrentIndex(userSchema);

    _navigationStyleLabel->setText(createLabelText(tr("Navigation Style")));
    _navigationStyleComboBox->clear();
    ParameterGrp::handle hGrpNav = App::GetApplication().GetParameterGroupByPath(
        "User parameter:BaseApp/Preferences/View"
    );
    auto navStyleName
        = hGrpNav->GetASCII("NavigationStyle", Gui::CADNavigationStyle::getClassTypeId().getName());
    std::map<Base::Type, std::string> styles = Gui::UserNavigationStyle::getUserFriendlyNames();
    for (const auto& style : styles) {
        QByteArray data(style.first.getName());
        QString name = QApplication::translate(style.first.getName(), style.second.c_str());
        _navigationStyleComboBox->addItem(name, data);
        if (navStyleName == style.first.getName()) {
            _navigationStyleComboBox->setCurrentIndex(_navigationStyleComboBox->count() - 1);
        }
    }
}