1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "languagechooser.h"
32
#include "mainwindow.h"
35
extern void qt_mac_set_menubar_merge(bool merge);
38
LanguageChooser::LanguageChooser(QWidget *parent)
39
: QDialog(parent, Qt::WindowStaysOnTopHint)
41
groupBox = new QGroupBox("Languages");
43
QGridLayout *groupBoxLayout = new QGridLayout;
45
QStringList qmFiles = findQmFiles();
46
for (int i = 0; i < qmFiles.size(); ++i) {
47
QCheckBox *checkBox = new QCheckBox(languageName(qmFiles[i]));
48
qmFileForCheckBoxMap.insert(checkBox, qmFiles[i]);
49
connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled()));
50
groupBoxLayout->addWidget(checkBox, i / 2, i % 2);
52
groupBox->setLayout(groupBoxLayout);
54
showAllButton = new QPushButton("Show All");
55
hideAllButton = new QPushButton("Hide All");
56
closeButton = new QPushButton("Close");
57
closeButton->setDefault(true);
59
connect(showAllButton, SIGNAL(clicked()), this, SLOT(showAll()));
60
connect(hideAllButton, SIGNAL(clicked()), this, SLOT(hideAll()));
61
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
63
QHBoxLayout *buttonLayout = new QHBoxLayout;
64
buttonLayout->addStretch(1);
65
buttonLayout->addWidget(showAllButton);
66
buttonLayout->addWidget(hideAllButton);
67
buttonLayout->addWidget(closeButton);
69
QVBoxLayout *mainLayout = new QVBoxLayout;
70
mainLayout->addWidget(groupBox);
71
mainLayout->addLayout(buttonLayout);
72
setLayout(mainLayout);
75
qt_mac_set_menubar_merge(false);
78
setWindowTitle("I18N");
81
bool LanguageChooser::eventFilter(QObject *object, QEvent *event)
83
if (event->type() == QEvent::Close) {
84
MainWindow *window = qobject_cast<MainWindow *>(object);
86
QCheckBox *checkBox = mainWindowForCheckBoxMap.key(window);
88
checkBox->setChecked(false);
91
return QWidget::eventFilter(object, event);
94
void LanguageChooser::closeEvent(QCloseEvent * /* event */)
99
void LanguageChooser::checkBoxToggled()
101
QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
102
MainWindow *window = mainWindowForCheckBoxMap[checkBox];
104
QTranslator translator;
105
translator.load(qmFileForCheckBoxMap[checkBox]);
106
qApp->installTranslator(&translator);
108
window = new MainWindow;
109
window->setPalette(colorForLanguage(checkBox->text()));
111
window->installEventFilter(this);
112
mainWindowForCheckBoxMap.insert(checkBox, window);
114
window->setVisible(checkBox->isChecked());
117
void LanguageChooser::showAll()
119
foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
120
checkBox->setChecked(true);
123
void LanguageChooser::hideAll()
125
foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
126
checkBox->setChecked(false);
129
QStringList LanguageChooser::findQmFiles()
131
QDir dir(":/translations");
132
QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files,
134
QMutableStringListIterator i(fileNames);
135
while (i.hasNext()) {
137
i.setValue(dir.filePath(i.value()));
142
QString LanguageChooser::languageName(const QString &qmFile)
144
QTranslator translator;
145
translator.load(qmFile);
147
return translator.translate("MainWindow", "English");
150
QColor LanguageChooser::colorForLanguage(const QString &language)
152
uint hashValue = qHash(language);
153
int red = 156 + (hashValue & 0x3F);
154
int green = 156 + ((hashValue >> 6) & 0x3F);
155
int blue = 156 + ((hashValue >> 12) & 0x3F);
156
return QColor(red, green, blue);