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 examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "languagechooser.h" |
52 | #include "mainwindow.h" |
53 | |
54 | #include <QCoreApplication> |
55 | #include <QCheckBox> |
56 | #include <QDialogButtonBox> |
57 | #include <QDir> |
58 | #include <QGridLayout> |
59 | #include <QGroupBox> |
60 | #include <QPushButton> |
61 | #include <QTranslator> |
62 | |
63 | LanguageChooser::LanguageChooser(const QString &defaultLang, QWidget *parent) |
64 | : QDialog(parent, Qt::WindowStaysOnTopHint) |
65 | { |
66 | groupBox = new QGroupBox("Languages" ); |
67 | |
68 | QGridLayout *groupBoxLayout = new QGridLayout; |
69 | |
70 | const QStringList qmFiles = findQmFiles(); |
71 | for (int i = 0; i < qmFiles.size(); ++i) { |
72 | const QString &qmlFile = qmFiles.at(i); |
73 | QCheckBox *checkBox = new QCheckBox(languageName(qmFile: qmlFile)); |
74 | qmFileForCheckBoxMap.insert(akey: checkBox, avalue: qmlFile); |
75 | connect(sender: checkBox, signal: &QCheckBox::toggled, |
76 | receiver: this, slot: &LanguageChooser::checkBoxToggled); |
77 | if (languageMatch(lang: defaultLang, qmFile: qmlFile)) |
78 | checkBox->setCheckState(Qt::Checked); |
79 | groupBoxLayout->addWidget(checkBox, row: i / 2, column: i % 2); |
80 | } |
81 | groupBox->setLayout(groupBoxLayout); |
82 | |
83 | buttonBox = new QDialogButtonBox; |
84 | showAllButton = buttonBox->addButton(text: "Show All" , |
85 | role: QDialogButtonBox::ActionRole); |
86 | hideAllButton = buttonBox->addButton(text: "Hide All" , |
87 | role: QDialogButtonBox::ActionRole); |
88 | |
89 | connect(sender: showAllButton, signal: &QAbstractButton::clicked, receiver: this, slot: &LanguageChooser::showAll); |
90 | connect(sender: hideAllButton, signal: &QAbstractButton::clicked, receiver: this, slot: &LanguageChooser::hideAll); |
91 | |
92 | QVBoxLayout *mainLayout = new QVBoxLayout; |
93 | mainLayout->addWidget(groupBox); |
94 | mainLayout->addWidget(buttonBox); |
95 | setLayout(mainLayout); |
96 | |
97 | setWindowTitle("I18N" ); |
98 | } |
99 | |
100 | bool LanguageChooser::languageMatch(const QString &lang, const QString &qmFile) |
101 | { |
102 | //qmFile: i18n_xx.qm |
103 | const QString prefix = "i18n_" ; |
104 | const int langTokenLength = 2; /*FIXME: is checking two chars enough?*/ |
105 | return qmFile.midRef(position: qmFile.indexOf(s: prefix) + prefix.length(), n: langTokenLength) == lang.leftRef(n: langTokenLength); |
106 | } |
107 | |
108 | bool LanguageChooser::eventFilter(QObject *object, QEvent *event) |
109 | { |
110 | if (event->type() == QEvent::Close) { |
111 | MainWindow *window = qobject_cast<MainWindow *>(object); |
112 | if (window) { |
113 | QCheckBox *checkBox = mainWindowForCheckBoxMap.key(avalue: window); |
114 | if (checkBox) |
115 | checkBox->setChecked(false); |
116 | } |
117 | } |
118 | return QDialog::eventFilter(object, event); |
119 | } |
120 | |
121 | void LanguageChooser::closeEvent(QCloseEvent * /* event */) |
122 | { |
123 | QCoreApplication::quit(); |
124 | } |
125 | |
126 | void LanguageChooser::checkBoxToggled() |
127 | { |
128 | QCheckBox *checkBox = qobject_cast<QCheckBox *>(object: sender()); |
129 | MainWindow *window = mainWindowForCheckBoxMap.value(akey: checkBox); |
130 | if (!window) { |
131 | QTranslator translator; |
132 | translator.load(filename: qmFileForCheckBoxMap.value(akey: checkBox)); |
133 | qApp->installTranslator(messageFile: &translator); |
134 | |
135 | window = new MainWindow; |
136 | window->setPalette(colorForLanguage(language: checkBox->text())); |
137 | |
138 | window->installEventFilter(filterObj: this); |
139 | mainWindowForCheckBoxMap.insert(akey: checkBox, avalue: window); |
140 | } |
141 | window->setVisible(checkBox->isChecked()); |
142 | } |
143 | |
144 | void LanguageChooser::showAll() |
145 | { |
146 | for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) |
147 | (*it)->setChecked(true); |
148 | } |
149 | |
150 | void LanguageChooser::hideAll() |
151 | { |
152 | for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it) |
153 | (*it)->setChecked(false); |
154 | } |
155 | |
156 | QStringList LanguageChooser::findQmFiles() |
157 | { |
158 | QDir dir(":/translations" ); |
159 | QStringList fileNames = dir.entryList(nameFilters: QStringList("*.qm" ), filters: QDir::Files, |
160 | sort: QDir::Name); |
161 | for (QString &fileName : fileNames) |
162 | fileName = dir.filePath(fileName); |
163 | return fileNames; |
164 | } |
165 | |
166 | QString LanguageChooser::languageName(const QString &qmFile) |
167 | { |
168 | QTranslator translator; |
169 | translator.load(filename: qmFile); |
170 | |
171 | return translator.translate(context: "MainWindow" , sourceText: "English" ); |
172 | } |
173 | |
174 | QColor LanguageChooser::colorForLanguage(const QString &language) |
175 | { |
176 | uint hashValue = qHash(key: language); |
177 | int red = 156 + (hashValue & 0x3F); |
178 | int green = 156 + ((hashValue >> 6) & 0x3F); |
179 | int blue = 156 + ((hashValue >> 12) & 0x3F); |
180 | return QColor(red, green, blue); |
181 | } |
182 | |