1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 | }
|
---|