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 
36 QT_BEGIN_NAMESPACE
37 
TranslationSettingsDialog(QWidget * parent)38 TranslationSettingsDialog::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(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("%1 (%2)").arg(lang, nln);
51             }
52         }
53         m_ui.srcCbLanguageList->addItem(lang, QVariant(i));
54     }
55     m_ui.srcCbLanguageList->model()->sort(0, Qt::AscendingOrder);
56     m_ui.srcCbLanguageList->insertItem(0, QLatin1String("POSIX"), QVariant(QLocale::C));
57 
58     m_ui.tgtCbLanguageList->setModel(m_ui.srcCbLanguageList->model());
59 }
60 
setDataModel(DataModel * dataModel)61 void TranslationSettingsDialog::setDataModel(DataModel *dataModel)
62 {
63     m_dataModel = dataModel;
64     m_phraseBook = 0;
65     QString fn = QFileInfo(dataModel->srcFileName()).baseName();
66     setWindowTitle(tr("Settings for '%1' - Qt Linguist").arg(fn));
67 }
68 
setPhraseBook(PhraseBook * phraseBook)69 void TranslationSettingsDialog::setPhraseBook(PhraseBook *phraseBook)
70 {
71     m_phraseBook = phraseBook;
72     m_dataModel = 0;
73     QString fn = QFileInfo(phraseBook->fileName()).baseName();
74     setWindowTitle(tr("Settings for '%1' - Qt Linguist").arg(fn));
75 }
76 
fillCountryCombo(const QVariant & lng,QComboBox * combo)77 static 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(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("%1 (%2)").arg(country, ncn);
89             }
90             combo->addItem(country, QVariant(cntr));
91         }
92         combo->model()->sort(0, Qt::AscendingOrder);
93     }
94     combo->insertItem(0, TranslationSettingsDialog::tr("Any Country"), QVariant(QLocale::AnyCountry));
95     combo->setCurrentIndex(0);
96 }
97 
on_srcCbLanguageList_currentIndexChanged(int idx)98 void TranslationSettingsDialog::on_srcCbLanguageList_currentIndexChanged(int idx)
99 {
100     fillCountryCombo(m_ui.srcCbLanguageList->itemData(idx), m_ui.srcCbCountryList);
101 }
102 
on_tgtCbLanguageList_currentIndexChanged(int idx)103 void TranslationSettingsDialog::on_tgtCbLanguageList_currentIndexChanged(int idx)
104 {
105     fillCountryCombo(m_ui.tgtCbLanguageList->itemData(idx), m_ui.tgtCbCountryList);
106 }
107 
on_buttonBox_accepted()108 void TranslationSettingsDialog::on_buttonBox_accepted()
109 {
110     int itemindex = m_ui.tgtCbLanguageList->currentIndex();
111     QVariant var = m_ui.tgtCbLanguageList->itemData(itemindex);
112     QLocale::Language lang = QLocale::Language(var.toInt());
113 
114     itemindex = m_ui.tgtCbCountryList->currentIndex();
115     var = m_ui.tgtCbCountryList->itemData(itemindex);
116     QLocale::Country country = QLocale::Country(var.toInt());
117 
118     itemindex = m_ui.srcCbLanguageList->currentIndex();
119     var = m_ui.srcCbLanguageList->itemData(itemindex);
120     QLocale::Language lang2 = QLocale::Language(var.toInt());
121 
122     itemindex = m_ui.srcCbCountryList->currentIndex();
123     var = m_ui.srcCbCountryList->itemData(itemindex);
124     QLocale::Country country2 = QLocale::Country(var.toInt());
125 
126     if (m_phraseBook) {
127         m_phraseBook->setLanguageAndCountry(lang, country);
128         m_phraseBook->setSourceLanguageAndCountry(lang2, country2);
129     } else {
130         m_dataModel->setLanguageAndCountry(lang, country);
131         m_dataModel->setSourceLanguageAndCountry(lang2, country2);
132     }
133 
134     accept();
135 }
136 
showEvent(QShowEvent *)137 void 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(QVariant(int(lang)));
155     m_ui.tgtCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
156 
157     itemindex = m_ui.tgtCbCountryList->findData(QVariant(int(country)));
158     m_ui.tgtCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
159 
160     itemindex = m_ui.srcCbLanguageList->findData(QVariant(int(lang2)));
161     m_ui.srcCbLanguageList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
162 
163     itemindex = m_ui.srcCbCountryList->findData(QVariant(int(country2)));
164     m_ui.srcCbCountryList->setCurrentIndex(itemindex == -1 ? 0 : itemindex);
165 }
166 
167 QT_END_NAMESPACE
168