summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tools/i18n/languagechooser.cpp
diff options
context:
space:
mode:
authorKai Köhne <kai.koehne@qt.io>2022-10-28 16:24:15 +0200
committerKai Köhne <kai.koehne@qt.io>2022-11-01 15:52:12 +0100
commitb77da049bc76ca809e7e2b58e0d554a3bf865b56 (patch)
treea1c7fb30395dd0b272820aec8d2ed35969d09917 /examples/widgets/tools/i18n/languagechooser.cpp
parent7a6f43b7c2afe91dbbc23cc759555ce6fe789889 (diff)
Move i18n example to qttools
Move i18n example out of qtbase. In qttools, it can use lrelease, avoiding the need to store .qm files in the repository. Change-Id: I8ba36a1372c2a743b809e3f7ea95a67825558f41 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'examples/widgets/tools/i18n/languagechooser.cpp')
-rw-r--r--examples/widgets/tools/i18n/languagechooser.cpp139
1 files changed, 0 insertions, 139 deletions
diff --git a/examples/widgets/tools/i18n/languagechooser.cpp b/examples/widgets/tools/i18n/languagechooser.cpp
deleted file mode 100644
index ff8dddd2da..0000000000
--- a/examples/widgets/tools/i18n/languagechooser.cpp
+++ /dev/null
@@ -1,139 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "languagechooser.h"
-#include "mainwindow.h"
-
-#include <QCoreApplication>
-#include <QCheckBox>
-#include <QDialogButtonBox>
-#include <QDir>
-#include <QGridLayout>
-#include <QGroupBox>
-#include <QPushButton>
-#include <QTranslator>
-
-LanguageChooser::LanguageChooser(const QString &defaultLang, QWidget *parent)
- : QDialog(parent, Qt::WindowStaysOnTopHint)
-{
- groupBox = new QGroupBox("Languages");
-
- QGridLayout *groupBoxLayout = new QGridLayout;
-
- const QStringList qmFiles = findQmFiles();
- for (int i = 0; i < qmFiles.size(); ++i) {
- const QString &qmFile = qmFiles.at(i);
- QCheckBox *checkBox = new QCheckBox(languageName(qmFile));
- qmFileForCheckBoxMap.insert(checkBox, qmFile);
- connect(checkBox, &QCheckBox::toggled,
- this, &LanguageChooser::checkBoxToggled);
- if (languageMatch(defaultLang, qmFile))
- checkBox->setCheckState(Qt::Checked);
- groupBoxLayout->addWidget(checkBox, i / 2, i % 2);
- }
- groupBox->setLayout(groupBoxLayout);
-
- buttonBox = new QDialogButtonBox;
- showAllButton = buttonBox->addButton("Show All",
- QDialogButtonBox::ActionRole);
- hideAllButton = buttonBox->addButton("Hide All",
- QDialogButtonBox::ActionRole);
-
- connect(showAllButton, &QAbstractButton::clicked, this, &LanguageChooser::showAll);
- connect(hideAllButton, &QAbstractButton::clicked, this, &LanguageChooser::hideAll);
-
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(groupBox);
- mainLayout->addWidget(buttonBox);
- setLayout(mainLayout);
-
- setWindowTitle("I18N");
-}
-
-bool LanguageChooser::languageMatch(QStringView lang, QStringView qmFile)
-{
- //qmFile: i18n_xx.qm
- const QStringView prefix{ u"i18n_" };
- const int langTokenLength = 2; /*FIXME: is checking two chars enough?*/
- return qmFile.mid(qmFile.indexOf(prefix) + prefix.length(), langTokenLength) == lang.left(langTokenLength);
-}
-
-bool LanguageChooser::eventFilter(QObject *object, QEvent *event)
-{
- if (event->type() == QEvent::Close) {
- MainWindow *window = qobject_cast<MainWindow *>(object);
- if (window) {
- QCheckBox *checkBox = mainWindowForCheckBoxMap.key(window);
- if (checkBox)
- checkBox->setChecked(false);
- }
- }
- return QDialog::eventFilter(object, event);
-}
-
-void LanguageChooser::closeEvent(QCloseEvent * /* event */)
-{
- QCoreApplication::quit();
-}
-
-void LanguageChooser::checkBoxToggled()
-{
- QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
- MainWindow *window = mainWindowForCheckBoxMap.value(checkBox);
- if (!window) {
- QTranslator translator;
- const QString qmFile = qmFileForCheckBoxMap.value(checkBox);
- if (translator.load(qmFile))
- QCoreApplication::installTranslator(&translator);
- else
- qWarning("Unable to load %s", qPrintable(QDir::toNativeSeparators(qmFile)));
-
- window = new MainWindow;
- window->setPalette(colorForLanguage(checkBox->text()));
-
- window->installEventFilter(this);
- mainWindowForCheckBoxMap.insert(checkBox, window);
- }
- window->setVisible(checkBox->isChecked());
-}
-
-void LanguageChooser::showAll()
-{
- for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it)
- (*it)->setChecked(true);
-}
-
-void LanguageChooser::hideAll()
-{
- for (auto it = qmFileForCheckBoxMap.keyBegin(); it != qmFileForCheckBoxMap.keyEnd(); ++it)
- (*it)->setChecked(false);
-}
-
-QStringList LanguageChooser::findQmFiles()
-{
- QDir dir(":/translations");
- QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files,
- QDir::Name);
- for (QString &fileName : fileNames)
- fileName = dir.filePath(fileName);
- return fileNames;
-}
-
-QString LanguageChooser::languageName(const QString &qmFile)
-{
- QTranslator translator;
- if (!translator.load(qmFile)) {
- qWarning("Unable to load %s", qPrintable(QDir::toNativeSeparators(qmFile)));
- return {};
- }
- return translator.translate("MainWindow", "English");
-}
-
-QColor LanguageChooser::colorForLanguage(const QString &language)
-{
- size_t hashValue = qHash(language);
- int red = 156 + (hashValue & 0x3F);
- int green = 156 + ((hashValue >> 6) & 0x3F);
- int blue = 156 + ((hashValue >> 12) & 0x3F);
- return QColor(red, green, blue);
-}