From b77da049bc76ca809e7e2b58e0d554a3bf865b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20K=C3=B6hne?= Date: Fri, 28 Oct 2022 16:24:15 +0200 Subject: Move i18n example to qttools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ø --- examples/widgets/tools/i18n/languagechooser.cpp | 139 ------------------------ 1 file changed, 139 deletions(-) delete mode 100644 examples/widgets/tools/i18n/languagechooser.cpp (limited to 'examples/widgets/tools/i18n/languagechooser.cpp') 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 -#include -#include -#include -#include -#include -#include -#include - -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(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(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); -} -- cgit v1.2.3