| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "languagechooser.h"
|
|---|
| 45 | #include "mainwindow.h"
|
|---|
| 46 |
|
|---|
| 47 | #ifdef Q_WS_MAC
|
|---|
| 48 | QT_BEGIN_NAMESPACE
|
|---|
| 49 | extern void qt_mac_set_menubar_merge(bool merge);
|
|---|
| 50 | QT_END_NAMESPACE
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | LanguageChooser::LanguageChooser(QWidget *parent)
|
|---|
| 54 | : QDialog(parent, Qt::WindowStaysOnTopHint)
|
|---|
| 55 | {
|
|---|
| 56 | groupBox = new QGroupBox("Languages");
|
|---|
| 57 |
|
|---|
| 58 | QGridLayout *groupBoxLayout = new QGridLayout;
|
|---|
| 59 |
|
|---|
| 60 | QStringList qmFiles = findQmFiles();
|
|---|
| 61 | for (int i = 0; i < qmFiles.size(); ++i) {
|
|---|
| 62 | QCheckBox *checkBox = new QCheckBox(languageName(qmFiles[i]));
|
|---|
| 63 | qmFileForCheckBoxMap.insert(checkBox, qmFiles[i]);
|
|---|
| 64 | connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled()));
|
|---|
| 65 | groupBoxLayout->addWidget(checkBox, i / 2, i % 2);
|
|---|
| 66 | }
|
|---|
| 67 | groupBox->setLayout(groupBoxLayout);
|
|---|
| 68 |
|
|---|
| 69 | buttonBox = new QDialogButtonBox;
|
|---|
| 70 |
|
|---|
| 71 | showAllButton = buttonBox->addButton("Show All",
|
|---|
| 72 | QDialogButtonBox::ActionRole);
|
|---|
| 73 | hideAllButton = buttonBox->addButton("Hide All",
|
|---|
| 74 | QDialogButtonBox::ActionRole);
|
|---|
| 75 |
|
|---|
| 76 | connect(showAllButton, SIGNAL(clicked()), this, SLOT(showAll()));
|
|---|
| 77 | connect(hideAllButton, SIGNAL(clicked()), this, SLOT(hideAll()));
|
|---|
| 78 |
|
|---|
| 79 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
|---|
| 80 | mainLayout->addWidget(groupBox);
|
|---|
| 81 | mainLayout->addWidget(buttonBox);
|
|---|
| 82 | setLayout(mainLayout);
|
|---|
| 83 |
|
|---|
| 84 | #ifdef Q_WS_MAC
|
|---|
| 85 | qt_mac_set_menubar_merge(false);
|
|---|
| 86 | #endif
|
|---|
| 87 |
|
|---|
| 88 | setWindowTitle("I18N");
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | bool LanguageChooser::eventFilter(QObject *object, QEvent *event)
|
|---|
| 92 | {
|
|---|
| 93 | if (event->type() == QEvent::Close) {
|
|---|
| 94 | MainWindow *window = qobject_cast<MainWindow *>(object);
|
|---|
| 95 | if (window) {
|
|---|
| 96 | QCheckBox *checkBox = mainWindowForCheckBoxMap.key(window);
|
|---|
| 97 | if (checkBox)
|
|---|
| 98 | checkBox->setChecked(false);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | return QWidget::eventFilter(object, event);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void LanguageChooser::closeEvent(QCloseEvent * /* event */)
|
|---|
| 105 | {
|
|---|
| 106 | qApp->quit();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void LanguageChooser::checkBoxToggled()
|
|---|
| 110 | {
|
|---|
| 111 | QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
|
|---|
| 112 | MainWindow *window = mainWindowForCheckBoxMap[checkBox];
|
|---|
| 113 | if (!window) {
|
|---|
| 114 | QTranslator translator;
|
|---|
| 115 | translator.load(qmFileForCheckBoxMap[checkBox]);
|
|---|
| 116 | qApp->installTranslator(&translator);
|
|---|
| 117 |
|
|---|
| 118 | window = new MainWindow;
|
|---|
| 119 | window->setPalette(colorForLanguage(checkBox->text()));
|
|---|
| 120 |
|
|---|
| 121 | window->installEventFilter(this);
|
|---|
| 122 | mainWindowForCheckBoxMap.insert(checkBox, window);
|
|---|
| 123 | }
|
|---|
| 124 | window->setVisible(checkBox->isChecked());
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void LanguageChooser::showAll()
|
|---|
| 128 | {
|
|---|
| 129 | foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
|
|---|
| 130 | checkBox->setChecked(true);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void LanguageChooser::hideAll()
|
|---|
| 134 | {
|
|---|
| 135 | foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
|
|---|
| 136 | checkBox->setChecked(false);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | QStringList LanguageChooser::findQmFiles()
|
|---|
| 140 | {
|
|---|
| 141 | QDir dir(":/translations");
|
|---|
| 142 | QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files,
|
|---|
| 143 | QDir::Name);
|
|---|
| 144 | QMutableStringListIterator i(fileNames);
|
|---|
| 145 | while (i.hasNext()) {
|
|---|
| 146 | i.next();
|
|---|
| 147 | i.setValue(dir.filePath(i.value()));
|
|---|
| 148 | }
|
|---|
| 149 | return fileNames;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | QString LanguageChooser::languageName(const QString &qmFile)
|
|---|
| 153 | {
|
|---|
| 154 | QTranslator translator;
|
|---|
| 155 | translator.load(qmFile);
|
|---|
| 156 |
|
|---|
| 157 | return translator.translate("MainWindow", "English");
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | QColor LanguageChooser::colorForLanguage(const QString &language)
|
|---|
| 161 | {
|
|---|
| 162 | uint hashValue = qHash(language);
|
|---|
| 163 | int red = 156 + (hashValue & 0x3F);
|
|---|
| 164 | int green = 156 + ((hashValue >> 6) & 0x3F);
|
|---|
| 165 | int blue = 156 + ((hashValue >> 12) & 0x3F);
|
|---|
| 166 | return QColor(red, green, blue);
|
|---|
| 167 | }
|
|---|