24
#include "PreCompiled.h"
26
#include <QApplication>
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>
44
using namespace StartGui;
46
GeneralSettingsWidget::GeneralSettingsWidget(QWidget* parent)
48
, _languageLabel {nullptr}
49
, _unitSystemLabel {nullptr}
50
, _navigationStyleLabel {nullptr}
51
, _languageComboBox {nullptr}
52
, _unitSystemComboBox {nullptr}
53
, _navigationStyleComboBox {nullptr}
55
setObjectName(QLatin1String("GeneralSettingsWidget"));
57
qApp->installEventFilter(this);
60
void GeneralSettingsWidget::setupUi()
63
qDeleteAll(findChildren<QWidget*>(QString(), Qt::FindDirectChildrenOnly));
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();
76
void GeneralSettingsWidget::createHorizontalUi()
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);
91
QString GeneralSettingsWidget::createLabelText(const QString& translatedText) const
93
static const auto h2Start = QLatin1String("<h2>");
94
static const auto h2End = QLatin1String("</h2>");
95
return h2Start + translatedText + h2End;
98
gsl::owner<QComboBox*> GeneralSettingsWidget::createLanguageComboBox()
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();
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());
112
if (it->second == "sr-CS") {
115
it->second = "sr_Latn";
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();
127
comboBox->addItem(langname, lang);
128
if (language == lang) {
129
comboBox->setCurrentIndex(index);
132
if (QAbstractItemModel* model = comboBox->model()) {
135
_languageComboBox = comboBox;
136
connect(_languageComboBox,
137
qOverload<int>(&QComboBox::currentIndexChanged),
139
&GeneralSettingsWidget::onLanguageChanged);
143
gsl::owner<QComboBox*> GeneralSettingsWidget::createUnitSystemComboBox()
146
auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
147
_unitSystemComboBox = comboBox;
148
connect(_unitSystemComboBox,
149
qOverload<int>(&QComboBox::currentIndexChanged),
151
&GeneralSettingsWidget::onUnitSystemChanged);
155
gsl::owner<QComboBox*> GeneralSettingsWidget::createNavigationStyleComboBox()
158
auto comboBox = gsl::owner<QComboBox*>(new QComboBox);
159
_navigationStyleComboBox = comboBox;
160
connect(_navigationStyleComboBox,
161
qOverload<int>(&QComboBox::currentIndexChanged),
163
&GeneralSettingsWidget::onNavigationStyleChanged);
167
void GeneralSettingsWidget::onLanguageChanged(int index)
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());
180
void GeneralSettingsWidget::onUnitSystemChanged(int index)
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);
191
void GeneralSettingsWidget::onNavigationStyleChanged(int index)
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());
202
bool GeneralSettingsWidget::eventFilter(QObject* object, QEvent* event)
204
if (object == this && event->type() == QEvent::LanguageChange) {
205
this->retranslateUi();
207
return QWidget::eventFilter(object, event);
210
void GeneralSettingsWidget::retranslateUi()
212
_languageLabel->setText(createLabelText(tr("Language")));
213
_unitSystemLabel->setText(createLabelText(tr("Unit System")));
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);
224
_unitSystemComboBox->setCurrentIndex(userSchema);
226
_navigationStyleLabel->setText(createLabelText(tr("Navigation Style")));
227
_navigationStyleComboBox->clear();
228
ParameterGrp::handle hGrpNav =
229
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
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);