1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include "translationsettingsdialog.h"
31#include "messagemodel.h"
32#include "phrase.h"
33
34#include <QtCore/QLocale>
35
36QT_BEGIN_NAMESPACE
37
38TranslationSettingsDialog::TranslationSettingsDialog(QWidget *parent)
39 : QDialog(parent)
40{
41 m_ui.setupUi(this);
42
43 for (int i = QLocale::C + 1; i < QLocale::LastLanguage; ++i) {
44 QString lang = QLocale::languageToString(language: QLocale::Language(i));
45 auto loc = QLocale(QLocale::Language(i));
46 if (loc.language() != QLocale::English) {
47 QString nln = loc.nativeLanguageName();
48 if (!nln.isEmpty()) {
49 //: <english> (<endonym>) (language and country names)
50 lang = tr(s: "%1 (%2)").arg(args&: lang, args&: nln);
51 }
52 }
53 m_ui.srcCbLanguageList->addItem(atext: lang, auserData: QVariant(i));
54 }
55 m_ui.srcCbLanguageList->model()->sort(column: 0, order: Qt::AscendingOrder);
56 m_ui.srcCbLanguageList->insertItem(aindex: 0, atext: QLatin1String("POSIX"), auserData: QVariant(QLocale::C));
57
58 m_ui.tgtCbLanguageList->setModel(m_ui.srcCbLanguageList->model());
59}
60
61void TranslationSettingsDialog::setDataModel(DataModel *dataModel)
62{
63 m_dataModel = dataModel;
64 m_phraseBook = 0;
65 QString fn = QFileInfo(dataModel->srcFileName()).baseName();
66 setWindowTitle(tr(s: "Settings for '%1' - Qt Linguist").arg(a: fn));
67}
68
69void TranslationSettingsDialog::setPhraseBook(PhraseBook *phraseBook)
70{
71 m_phraseBook = phraseBook;
72 m_dataModel = 0;
73 QString fn = QFileInfo(phraseBook->fileName()).baseName();
74 setWindowTitle(tr(s: "Settings for '%1' - Qt Linguist").arg(a: fn));
75}
76
77static void fillCountryCombo(const QVariant &lng, QComboBox *combo)
78{
79 combo->clear();
80 QLocale::Language lang = QLocale::Language(lng.toInt());
81 if (lang != QLocale::C) {
82 foreach (QLocale::Country cntr, QLocale::countriesForLanguage(lang)) {
83 QString country = QLocale::countryToString(country: cntr);
84 auto loc = QLocale(lang, cntr);
85 if (loc.language() != QLocale::English) {
86 QString ncn = loc.nativeCountryName();
87 if (!ncn.isEmpty())
88 country = TranslationSettingsDialog::tr(s: "%1 (%2)").arg(args&: country, args&: ncn);
89 }
90 combo->addItem(atext: country, auserData: QVariant(cntr));
91 }
92 combo->model()->sort(column: 0, order: Qt::AscendingOrder);
93 }
94 combo->insertItem(aindex: 0, atext: TranslationSettingsDialog::tr(s: "Any Country"), auserData: QVariant(QLocale::AnyCountry));
95 combo->setCurrentIndex(0);
96}
97
98void TranslationSettingsDialog::on_srcCbLanguageList_currentIndexChanged(int idx)
99{
100 fillCountryCombo(lng: m_ui.srcCbLanguageList->itemData(index: idx), combo: m_ui.srcCbCountryList);
101}
102
103void TranslationSettingsDialog::on_tgtCbLanguageList_currentIndexChanged(int idx)
104{
105 fillCountryCombo(lng: m_ui.tgtCbLanguageList->itemData(index: idx), combo: m_ui.tgtCbCountryList);
106}
107
108void TranslationSettingsDialog::on_buttonBox_accepted()
109{
110 int itemindex = m_ui.tgtCbLanguageList->currentIndex();
111 QVariant var = m_ui.tgtCbLanguageList->itemData(index: itemindex);
112 QLocale::Language lang = QLocale::Language(var.toInt());
113
114 itemindex = m_ui.tgtCbCountryList->currentIndex();
115 var = m_ui.tgtCbCountryList->itemData(index: itemindex);
116 QLocale::Country country = QLocale::Country(var.toInt());
117
118 itemindex = m_ui.srcCbLanguageList->currentIndex();
119 var = m_ui.srcCbLanguageList->itemData(index: itemindex);
120 QLocale::Language lang2 = QLocale::Language(var.toInt());
121
122 itemindex = m_ui.srcCbCountryList->currentIndex();
123 var = m_ui.srcCbCountryList->itemData(index: itemindex);
124 QLocale::Country country2 = QLocale::Country(var.toInt());
125
126 if (m_phraseBook) {
127 m_phraseBook->setLanguageAndCountry(lang, country);
128 m_phraseBook->setSourceLanguageAndCountry(lang: lang2, country: country2);
129 } else {
130 m_dataModel->setLanguageAndCountry(lang, country);
131 m_dataModel->setSourceLanguageAndCountry(lang: lang2, country: country2);
132 }
133
134 accept();
135}
136
137void TranslationSettingsDialog::showEvent(QShowEvent *)
138{
139 QLocale::Language lang, lang2;
140 QLocale::Country country, country2;
141
142 if (m_phraseBook) {
143 lang = m_phraseBook->language();
144 country = m_phraseBook->country();
145 lang2 = m_phraseBook->sourceLanguage();
146 country2 = m_phraseBook->sourceCountry();
147 } else {
148 lang = m_dataModel->language();
149 country = m_dataModel->country();
150 lang2 = m_dataModel->sourceLanguage();
151 country2 = m_dataModel->sourceCountry();
152 }
153
154 int itemindex = m_ui.tgtCbLanguageList->findData(data: QVariant(int(lang)));
155 m_ui.tgtCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
156
157 itemindex = m_ui.tgtCbCountryList->findData(data: QVariant(int(country)));
158 m_ui.tgtCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
159
160 itemindex = m_ui.srcCbLanguageList->findData(data: QVariant(int(lang2)));
161 m_ui.srcCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
162
163 itemindex = m_ui.srcCbCountryList->findData(data: QVariant(int(country2)));
164 m_ui.srcCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
165}
166
167QT_END_NAMESPACE
168

source code of qttools/src/linguist/linguist/translationsettingsdialog.cpp