Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
translationsettingsdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4
6#include "messagemodel.h"
7#include "phrase.h"
8
9#include <QtCore/QLocale>
10
12
15{
16 m_ui.setupUi(this);
17
18 for (int i = QLocale::C + 1; i < QLocale::LastLanguage; ++i) {
19 const auto language = QLocale::Language(i);
20 QString lang = QLocale::languageToString(language);
21 const auto loc = QLocale(language);
22 // Languages for which we have no data get mapped to the default locale;
23 // its endonym is unrelated to the language requested. For English, the
24 // endonym is the name we already have; don't repeat it.
25 if (loc.language() == language && language != QLocale::English) {
26 const QString native = loc.nativeLanguageName();
27 if (!native.isEmpty()) //: <english> (<endonym>) (language names)
28 lang = tr("%1 (%2)").arg(lang, native);
29 }
30 m_ui.srcCbLanguageList->addItem(lang, QVariant(i));
31 }
32 m_ui.srcCbLanguageList->model()->sort(0, Qt::AscendingOrder);
33 m_ui.srcCbLanguageList->insertItem(0, QLatin1String("POSIX"), QVariant(QLocale::C));
34
35 m_ui.tgtCbLanguageList->setModel(m_ui.srcCbLanguageList->model());
36}
37
39{
40 m_dataModel = dataModel;
41 m_phraseBook = 0;
42 QString fn = QFileInfo(dataModel->srcFileName()).baseName();
43 setWindowTitle(tr("Settings for '%1' - Qt Linguist").arg(fn));
44}
45
47{
48 m_phraseBook = phraseBook;
49 m_dataModel = 0;
50 QString fn = QFileInfo(phraseBook->fileName()).baseName();
51 setWindowTitle(tr("Settings for '%1' - Qt Linguist").arg(fn));
52}
53
54static void fillTerritoryCombo(const QVariant &lng, QComboBox *combo)
55{
56 combo->clear();
57 QLocale::Language lang = QLocale::Language(lng.toInt());
58 if (lang != QLocale::C) {
59 const auto matches = QLocale::matchingLocales(lang, QLocale::AnyScript,
60 QLocale::AnyTerritory);
61 for (const auto &loc : matches) {
62 QString name = QLocale::territoryToString(loc.territory());
63 if (loc.language() != QLocale::English) {
64 QString endonym = loc.nativeTerritoryName();
65 if (!endonym.isEmpty())
66 name = TranslationSettingsDialog::tr("%1 (%2)").arg(name, endonym);
67 }
68 combo->addItem(name, QVariant(loc.territory()));
69 }
70 combo->model()->sort(0, Qt::AscendingOrder);
71 }
72 combo->insertItem(0, TranslationSettingsDialog::tr("Any Territory"),
73 QVariant(QLocale::AnyTerritory));
74 combo->setCurrentIndex(0);
75}
76
77void TranslationSettingsDialog::on_srcCbLanguageList_currentIndexChanged(int idx)
78{
79 fillTerritoryCombo(m_ui.srcCbLanguageList->itemData(idx), m_ui.srcCbCountryList);
80}
81
82void TranslationSettingsDialog::on_tgtCbLanguageList_currentIndexChanged(int idx)
83{
84 fillTerritoryCombo(m_ui.tgtCbLanguageList->itemData(idx), m_ui.tgtCbCountryList);
85}
86
87void TranslationSettingsDialog::on_buttonBox_accepted()
88{
89 int itemindex = m_ui.tgtCbLanguageList->currentIndex();
90 QVariant var = m_ui.tgtCbLanguageList->itemData(itemindex);
91 QLocale::Language lang = QLocale::Language(var.toInt());
92
93 itemindex = m_ui.tgtCbCountryList->currentIndex();
94 var = m_ui.tgtCbCountryList->itemData(itemindex);
95 QLocale::Territory territory = QLocale::Territory(var.toInt());
96
97 itemindex = m_ui.srcCbLanguageList->currentIndex();
98 var = m_ui.srcCbLanguageList->itemData(itemindex);
99 QLocale::Language lang2 = QLocale::Language(var.toInt());
100
101 itemindex = m_ui.srcCbCountryList->currentIndex();
102 var = m_ui.srcCbCountryList->itemData(itemindex);
103 QLocale::Territory territory2 = QLocale::Territory(var.toInt());
104
105 if (m_phraseBook) {
106 m_phraseBook->setLanguageAndTerritory(lang, territory);
107 m_phraseBook->setSourceLanguageAndTerritory(lang2, territory2);
108 } else {
109 m_dataModel->setLanguageAndTerritory(lang, territory);
110 m_dataModel->setSourceLanguageAndTerritory(lang2, territory2);
111 }
112
113 accept();
114}
115
117{
118 QLocale::Language lang, lang2;
119 QLocale::Territory territory, territory2;
120
121 if (m_phraseBook) {
122 lang = m_phraseBook->language();
123 territory = m_phraseBook->territory();
124 lang2 = m_phraseBook->sourceLanguage();
125 territory2 = m_phraseBook->sourceTerritory();
126 } else {
127 lang = m_dataModel->language();
128 territory = m_dataModel->territory();
129 lang2 = m_dataModel->sourceLanguage();
130 territory2 = m_dataModel->sourceTerritory();
131 }
132
133 int itemindex = m_ui.tgtCbLanguageList->findData(QVariant(int(lang)));
134 m_ui.tgtCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
135
136 itemindex = m_ui.tgtCbCountryList->findData(QVariant(int(territory)));
137 m_ui.tgtCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
138
139 itemindex = m_ui.srcCbLanguageList->findData(QVariant(int(lang2)));
140 m_ui.srcCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
141
142 itemindex = m_ui.srcCbCountryList->findData(QVariant(int(territory2)));
143 m_ui.srcCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
144}
145
146QT_END_NAMESPACE
QObject * parent
Definition qobject.h:73
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void showEvent(QShowEvent *e) override
\reimp
void setPhraseBook(PhraseBook *phraseBook)
Combined button and popup list for selecting options.
static void fillTerritoryCombo(const QVariant &lng, QComboBox *combo)