~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/tools/i18n/languagechooser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "languagechooser.h"
 
32
#include "mainwindow.h"
 
33
 
 
34
#ifdef Q_WS_MAC
 
35
extern void qt_mac_set_menubar_merge(bool merge);
 
36
#endif
 
37
 
 
38
LanguageChooser::LanguageChooser(QWidget *parent)
 
39
    : QDialog(parent, Qt::WindowStaysOnTopHint)
 
40
{
 
41
    groupBox = new QGroupBox("Languages");
 
42
 
 
43
    QGridLayout *groupBoxLayout = new QGridLayout;
 
44
 
 
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);
 
51
    }
 
52
    groupBox->setLayout(groupBoxLayout);
 
53
 
 
54
    showAllButton = new QPushButton("Show All");
 
55
    hideAllButton = new QPushButton("Hide All");
 
56
    closeButton = new QPushButton("Close");
 
57
    closeButton->setDefault(true);
 
58
 
 
59
    connect(showAllButton, SIGNAL(clicked()), this, SLOT(showAll()));
 
60
    connect(hideAllButton, SIGNAL(clicked()), this, SLOT(hideAll()));
 
61
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
 
62
 
 
63
    QHBoxLayout *buttonLayout = new QHBoxLayout;
 
64
    buttonLayout->addStretch(1);
 
65
    buttonLayout->addWidget(showAllButton);
 
66
    buttonLayout->addWidget(hideAllButton);
 
67
    buttonLayout->addWidget(closeButton);
 
68
 
 
69
    QVBoxLayout *mainLayout = new QVBoxLayout;
 
70
    mainLayout->addWidget(groupBox);
 
71
    mainLayout->addLayout(buttonLayout);
 
72
    setLayout(mainLayout);
 
73
 
 
74
#ifdef Q_WS_MAC
 
75
    qt_mac_set_menubar_merge(false);
 
76
#endif
 
77
 
 
78
    setWindowTitle("I18N");
 
79
}
 
80
 
 
81
bool LanguageChooser::eventFilter(QObject *object, QEvent *event)
 
82
{
 
83
    if (event->type() == QEvent::Close) {
 
84
        MainWindow *window = qobject_cast<MainWindow *>(object);
 
85
        if (window) {
 
86
            QCheckBox *checkBox = mainWindowForCheckBoxMap.key(window);
 
87
            if (checkBox)
 
88
                checkBox->setChecked(false);
 
89
        }
 
90
    }
 
91
    return QWidget::eventFilter(object, event);
 
92
}
 
93
 
 
94
void LanguageChooser::closeEvent(QCloseEvent * /* event */)
 
95
{
 
96
    qApp->quit();
 
97
}
 
98
 
 
99
void LanguageChooser::checkBoxToggled()
 
100
{
 
101
    QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
 
102
    MainWindow *window = mainWindowForCheckBoxMap[checkBox];
 
103
    if (!window) {
 
104
        QTranslator translator;
 
105
        translator.load(qmFileForCheckBoxMap[checkBox]);
 
106
        qApp->installTranslator(&translator);
 
107
 
 
108
        window = new MainWindow;
 
109
        window->setPalette(colorForLanguage(checkBox->text()));
 
110
 
 
111
        window->installEventFilter(this);
 
112
        mainWindowForCheckBoxMap.insert(checkBox, window);
 
113
    }
 
114
    window->setVisible(checkBox->isChecked());
 
115
}
 
116
 
 
117
void LanguageChooser::showAll()
 
118
{
 
119
    foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
 
120
        checkBox->setChecked(true);
 
121
}
 
122
 
 
123
void LanguageChooser::hideAll()
 
124
{
 
125
    foreach (QCheckBox *checkBox, qmFileForCheckBoxMap.keys())
 
126
        checkBox->setChecked(false);
 
127
}
 
128
 
 
129
QStringList LanguageChooser::findQmFiles()
 
130
{
 
131
    QDir dir(":/translations");
 
132
    QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files,
 
133
                                          QDir::Name);
 
134
    QMutableStringListIterator i(fileNames);
 
135
    while (i.hasNext()) {
 
136
        i.next();
 
137
        i.setValue(dir.filePath(i.value()));
 
138
    }
 
139
    return fileNames;
 
140
}
 
141
 
 
142
QString LanguageChooser::languageName(const QString &qmFile)
 
143
{
 
144
    QTranslator translator;
 
145
    translator.load(qmFile);
 
146
 
 
147
    return translator.translate("MainWindow", "English");
 
148
}
 
149
 
 
150
QColor LanguageChooser::colorForLanguage(const QString &language)
 
151
{
 
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);
 
157
}