kdelibs/kdeui/dialogs/kswitchlanguagedialog_p.cpp

354 lines
11 KiB
C++
Raw Normal View History

2014-11-13 01:04:59 +02:00
/*
* This file is part of the KDE Libraries
* Copyright (C) 2007 Krzysztof Lichota (lichota@mimuw.edu.pl)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "moc_kswitchlanguagedialog_p.cpp"
2014-11-13 01:04:59 +02:00
#include <QtGui/QApplication>
#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtCore/QEvent>
#include <QtCore/QMap>
#include <klanguagebutton.h>
#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kpushbutton.h>
namespace KDEPrivate {
struct LanguageRowData
{
LanguageRowData()
{
label = nullptr;
languageButton = nullptr;
removeButton = nullptr;
2014-11-13 01:04:59 +02:00
}
QLabel *label;
KLanguageButton *languageButton;
KPushButton *removeButton;
void setRowWidgets(QLabel *label, KLanguageButton *languageButton, KPushButton *removeButton)
2014-11-13 01:04:59 +02:00
{
this->label = label;
this->languageButton = languageButton;
this->removeButton = removeButton;
}
};
class KSwitchLanguageDialogPrivate
{
public:
KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent);
KSwitchLanguageDialog *p; //parent class
/**
Fills language button with names of languages for which given application has translation.
*/
void fillApplicationLanguages(KLanguageButton *button);
/**
Adds one button with language to widget.
*/
void addLanguageButton(const QString &languageCode, bool primaryLanguage);
2014-11-13 01:04:59 +02:00
/**
Returns list of languages chosen for application or default languages is they are not set.
*/
QStringList applicationLanguageList();
QMap<KPushButton*, LanguageRowData> languageRows;
QList<KLanguageButton*> languageButtons;
QGridLayout *languagesLayout;
QWidget *page;
};
/*************************** KSwitchLanguageDialog **************************/
KSwitchLanguageDialog::KSwitchLanguageDialog(QWidget *parent)
2014-11-13 01:04:59 +02:00
: KDialog(parent),
d(new KSwitchLanguageDialogPrivate(this))
{
setCaption(i18n("Switch Application Language"));
setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Default);
2014-11-13 01:04:59 +02:00
setDefaultButton(Ok);
connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
connect(this, SIGNAL(defaultClicked()), SLOT(slotDefault()));
d->page = new QWidget(this);
2014-11-13 01:04:59 +02:00
setMainWidget(d->page);
QVBoxLayout *topLayout = new QVBoxLayout(d->page);
2014-11-13 01:04:59 +02:00
topLayout->setMargin( 0 );
QLabel *label = new QLabel(i18n("Please choose the language which should be used for this application:"), d->page);
topLayout->addWidget(label);
2014-11-13 01:04:59 +02:00
QHBoxLayout *languageHorizontalLayout = new QHBoxLayout();
topLayout->addLayout(languageHorizontalLayout);
d->languagesLayout = new QGridLayout();
languageHorizontalLayout->addLayout(d->languagesLayout);
languageHorizontalLayout->addStretch();
const QStringList defaultLanguages = d->applicationLanguageList();
int count = defaultLanguages.count();
for (int i = 0; i < count; ++i) {
2014-11-13 01:04:59 +02:00
QString language = defaultLanguages[i];
bool primaryLanguage = (i == 0);
d->addLanguageButton(language, primaryLanguage);
}
if (!count) {
d->addLanguageButton(KLocale::defaultLanguage(), true);
2014-11-13 01:04:59 +02:00
}
QHBoxLayout *addButtonHorizontalLayout = new QHBoxLayout();
topLayout->addLayout(addButtonHorizontalLayout);
KPushButton *addLangButton = new KPushButton(i18n("Add Fallback Language"), d->page);
addLangButton->setToolTip(
i18n("Adds one more language which will be used if other translations do not contain a proper translation.")
);
2014-11-13 01:04:59 +02:00
connect(addLangButton, SIGNAL(clicked()), this, SLOT(slotAddLanguageButton()));
addButtonHorizontalLayout->addWidget(addLangButton);
addButtonHorizontalLayout->addStretch();
topLayout->addStretch(10);
}
KSwitchLanguageDialog::~KSwitchLanguageDialog()
{
delete d;
}
void KSwitchLanguageDialog::slotAddLanguageButton()
{
// adding new button with en_US as it should always be present
d->addLanguageButton(KLocale::defaultLanguage(), d->languageButtons.isEmpty());
2014-11-13 01:04:59 +02:00
}
void KSwitchLanguageDialog::removeButtonClicked()
{
KPushButton *removeButton = qobject_cast<KPushButton*>(sender());
if (!removeButton) {
kError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton";
2014-11-13 01:04:59 +02:00
return;
}
QMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton);
if (it == d->languageRows.end()) {
kError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton";
2014-11-13 01:04:59 +02:00
return;
}
LanguageRowData languageRowData = it.value();
d->languageButtons.removeAll(languageRowData.languageButton);
languageRowData.label->deleteLater();
languageRowData.languageButton->deleteLater();
languageRowData.removeButton->deleteLater();
d->languageRows.erase(it);
}
void KSwitchLanguageDialog::languageOnButtonChanged(const QString &languageCode)
2014-11-13 01:04:59 +02:00
{
Q_UNUSED(languageCode);
#if 0
for ( int i = 0, count = d->languageButtons.count(); i < count; ++i )
{
KLanguageButton *languageButton = d->languageButtons[i];
if (languageButton->current() == languageCode)
{
//update all buttons which have matching id
//might update buttons which were not changed, but well...
languageButton->setText(KGlobal::locale()->languageCodeToName(languageCode));
}
}
#endif
}
void KSwitchLanguageDialog::slotOk()
{
QStringList languages;
for ( int i = 0, count = d->languageButtons.count(); i < count; ++i ) {
2014-11-13 01:04:59 +02:00
KLanguageButton *languageButton = d->languageButtons[i];
languages << languageButton->current();
2014-11-13 01:04:59 +02:00
}
if (d->applicationLanguageList() != languages) {
2014-11-13 01:04:59 +02:00
QString languageString = languages.join(":");
//list is different from defaults or saved languages list
KConfigGroup group(KGlobal::config(), "Locale");
group.writeEntry("Language", languageString);
group.sync();
KMessageBox::information(
this,
i18n("The language for this application has been changed. The change will take effect the next time the application is started."), //text
i18n("Application Language Changed"), //caption
"ApplicationLanguageChangedWarning" //dontShowAgainName
);
2014-11-13 01:04:59 +02:00
QEvent ev(QEvent::LanguageChange);
QApplication::sendEvent(qApp, &ev);
}
accept();
}
void KSwitchLanguageDialog::slotDefault()
{
const QStringList defaultLanguages = d->applicationLanguageList();
KConfigGroup group(KGlobal::config(), "Locale");
group.revertToDefault("Language");
group.sync();
// read back the new default
const QString language = group.readEntry("Language", KLocale::defaultLanguage());
2014-11-13 01:04:59 +02:00
if (defaultLanguages != (QStringList() << language)) {
KMessageBox::information(
this,
i18n("The language for this application has been changed. The change will take effect the next time the application is started."), //text
i18n("Application Language Changed"), // caption
"ApplicationLanguageChangedWarning" // dontShowAgainName
);
2014-11-13 01:04:59 +02:00
QEvent ev(QEvent::LanguageChange);
QApplication::sendEvent(qApp, &ev);
}
accept();
}
/************************ KSwitchLanguageDialogPrivate ***********************/
KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent)
: p(parent)
2014-11-13 01:04:59 +02:00
{
// NOTE: do NOT use "p" in constructor, it is not fully constructed
2014-11-13 01:04:59 +02:00
}
void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button)
{
QStringList allLanguages = KLocale::installedLanguages();
allLanguages.prepend(KLocale::defaultLanguage());
for (int i = 0, count = allLanguages.count(); i < count; ++i) {
2014-11-13 01:04:59 +02:00
QString languageCode = allLanguages[i];
if (KLocale::isApplicationTranslatedInto(languageCode)) {
2014-11-13 01:04:59 +02:00
button->insertLanguage(languageCode);
}
}
}
QStringList KSwitchLanguageDialogPrivate::applicationLanguageList()
{
KSharedConfigPtr config = KGlobal::config();
QStringList languagesList;
if (config->hasGroup("Locale")) {
2014-11-13 01:04:59 +02:00
KConfigGroup group(config, "Locale");
if (group.hasKey("Language")) {
2014-11-13 01:04:59 +02:00
languagesList = group.readEntry("Language", QString()).split(':');
}
}
if (languagesList.isEmpty()) {
languagesList = KGlobal::locale()->languageList();
2014-11-13 01:04:59 +02:00
}
KLocale *locale = KGlobal::locale();
for (int i = 0; i < languagesList.count();) {
if (!locale->isApplicationTranslatedInto(languagesList[i])) {
languagesList.removeAt(i);
} else {
++i;
}
2014-11-13 01:04:59 +02:00
}
return languagesList;
}
void KSwitchLanguageDialogPrivate::addLanguageButton(const QString &languageCode, bool primaryLanguage)
2014-11-13 01:04:59 +02:00
{
QString labelText = primaryLanguage ? i18n("Primary language:") : i18n("Fallback language:");
KLanguageButton *languageButton = new KLanguageButton(page);
fillApplicationLanguages(languageButton);
languageButton->setCurrentItem(languageCode);
QObject::connect(
languageButton,
SIGNAL(activated(QString)),
p,
SLOT(languageOnButtonChanged(QString))
);
2014-11-13 01:04:59 +02:00
LanguageRowData languageRowData;
KPushButton *removeButton = nullptr;
2014-11-13 01:04:59 +02:00
if (!primaryLanguage) {
2014-11-13 01:04:59 +02:00
removeButton = new KPushButton(i18n("Remove"), page);
QObject::connect(
removeButton,
SIGNAL(clicked()),
p,
SLOT(removeButtonClicked())
);
2014-11-13 01:04:59 +02:00
}
languageButton->setToolTip(
primaryLanguage
? i18n("This is the main application language which will be used first, before any other languages.")
: i18n("This is the language which will be used if any previous languages do not contain a proper translation.")
);
2014-11-13 01:04:59 +02:00
int numRows = languagesLayout->rowCount();
QLabel *languageLabel = new QLabel(labelText, page);
languagesLayout->addWidget(languageLabel, numRows + 1, 1, Qt::AlignLeft);
languagesLayout->addWidget(languageButton, numRows + 1, 2, Qt::AlignLeft);
2014-11-13 01:04:59 +02:00
if (!primaryLanguage) {
languagesLayout->addWidget(removeButton, numRows + 1, 3, Qt::AlignLeft);
languageRowData.setRowWidgets(languageLabel, languageButton, removeButton);
2014-11-13 01:04:59 +02:00
removeButton->show();
}
languageRows.insert(removeButton, languageRowData);
languageButtons.append(languageButton);
languageButton->show();
languageLabel->show();
}
}