mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 10:22:48 +00:00
kdeui: stub KSwitchLanguageDialog
to be rewritten. date, time, days, etc. translation and conversion work like a charm tho (when translated and supported by the locale classes ofcourse): https://ibb.co/hcW0dL3 Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
e5b48177a6
commit
b7e54f8e21
8 changed files with 90 additions and 820 deletions
|
@ -160,7 +160,6 @@ install(
|
|||
KJobTrackerInterface
|
||||
KJobUiDelegate
|
||||
KKeySequenceWidget
|
||||
KLanguageButton
|
||||
KLed
|
||||
KLineEdit
|
||||
KLinkItemSelectionModel
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
#include "../klanguagebutton.h"
|
|
@ -212,7 +212,6 @@ set(kdeui_LIB_SRCS
|
|||
widgets/khelpmenu.cpp
|
||||
widgets/khistorycombobox.cpp
|
||||
widgets/kkeysequencewidget.cpp
|
||||
widgets/klanguagebutton.cpp
|
||||
widgets/kled.cpp
|
||||
widgets/klineedit.cpp
|
||||
widgets/kmainwindow.cpp
|
||||
|
@ -517,7 +516,6 @@ install(
|
|||
widgets/khelpmenu.h
|
||||
widgets/khistorycombobox.h
|
||||
widgets/kkeysequencewidget.h
|
||||
widgets/klanguagebutton.h
|
||||
widgets/kled.h
|
||||
widgets/klineedit.h
|
||||
widgets/kmainwindow.h
|
||||
|
|
|
@ -1,221 +1,82 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
/* This file is part of the KDE libraries
|
||||
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
#include "moc_kswitchlanguagedialog_p.cpp"
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
#include <QtGui/QApplication>
|
||||
#include <QtGui/QLayout>
|
||||
#include <QtGui/QLabel>
|
||||
#include <QtCore/QEvent>
|
||||
#include <QtCore/QMap>
|
||||
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 "kswitchlanguagedialog_p.h"
|
||||
|
||||
#include <klanguagebutton.h>
|
||||
#include <kconfig.h>
|
||||
#include <klocale.h>
|
||||
#include <kmessagebox.h>
|
||||
#include <kdebug.h>
|
||||
#include <kpushbutton.h>
|
||||
#include <QApplication>
|
||||
#include <QEvent>
|
||||
|
||||
namespace KDEPrivate {
|
||||
|
||||
struct LanguageRowData
|
||||
static void languageChanged(KSwitchLanguageDialog *dialog)
|
||||
{
|
||||
LanguageRowData()
|
||||
{
|
||||
label = nullptr;
|
||||
languageButton = nullptr;
|
||||
removeButton = nullptr;
|
||||
}
|
||||
QLabel *label;
|
||||
KLanguageButton *languageButton;
|
||||
KPushButton *removeButton;
|
||||
KMessageBox::information(
|
||||
dialog,
|
||||
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
|
||||
);
|
||||
|
||||
void setRowWidgets(QLabel *label, KLanguageButton *languageButton, KPushButton *removeButton)
|
||||
{
|
||||
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);
|
||||
|
||||
/**
|
||||
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 **************************/
|
||||
QEvent ev(QEvent::LanguageChange);
|
||||
QApplication::sendEvent(qApp, &ev);
|
||||
}
|
||||
|
||||
KSwitchLanguageDialog::KSwitchLanguageDialog(QWidget *parent)
|
||||
: KDialog(parent),
|
||||
d(new KSwitchLanguageDialogPrivate(this))
|
||||
: KDialog(parent),
|
||||
m_dialogwidget(nullptr),
|
||||
m_dialoglayout(nullptr),
|
||||
m_languagelabel(nullptr),
|
||||
m_languageedit(nullptr)
|
||||
{
|
||||
setCaption(i18n("Switch Application Language"));
|
||||
setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Default);
|
||||
setDefaultButton(Ok);
|
||||
|
||||
connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
|
||||
connect(this, SIGNAL(defaultClicked()), SLOT(slotDefault()));
|
||||
|
||||
d->page = new QWidget(this);
|
||||
setMainWidget(d->page);
|
||||
QVBoxLayout *topLayout = new QVBoxLayout(d->page);
|
||||
topLayout->setMargin( 0 );
|
||||
QLabel *label = new QLabel(i18n("Please choose the language which should be used for this application:"), d->page);
|
||||
topLayout->addWidget(label);
|
||||
|
||||
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) {
|
||||
QString language = defaultLanguages[i];
|
||||
bool primaryLanguage = (i == 0);
|
||||
d->addLanguageButton(language, primaryLanguage);
|
||||
}
|
||||
|
||||
if (!count) {
|
||||
d->addLanguageButton(KLocale::defaultLanguage(), true);
|
||||
}
|
||||
|
||||
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.")
|
||||
);
|
||||
connect(addLangButton, SIGNAL(clicked()), this, SLOT(slotAddLanguageButton()));
|
||||
addButtonHorizontalLayout->addWidget(addLangButton);
|
||||
addButtonHorizontalLayout->addStretch();
|
||||
|
||||
topLayout->addStretch(10);
|
||||
m_dialogwidget = new QWidget(this);
|
||||
m_dialoglayout = new QVBoxLayout(m_dialogwidget);
|
||||
m_languagelabel = new QLabel(m_dialogwidget);
|
||||
m_languagelabel->setText(i18n("Please choose the language which should be used for this application:"));
|
||||
m_dialoglayout->addWidget(m_languagelabel);
|
||||
m_languageedit = new KEditListWidget(m_dialogwidget);
|
||||
m_dialoglayout->addWidget(m_languageedit);
|
||||
setMainWidget(m_dialogwidget);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
void KSwitchLanguageDialog::removeButtonClicked()
|
||||
{
|
||||
KPushButton *removeButton = qobject_cast<KPushButton*>(sender());
|
||||
if (!removeButton) {
|
||||
kError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton";
|
||||
return;
|
||||
}
|
||||
|
||||
QMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton);
|
||||
if (it == d->languageRows.end()) {
|
||||
kError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton";
|
||||
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)
|
||||
{
|
||||
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;
|
||||
KConfigGroup localegroup(KGlobal::config(), "Locale");
|
||||
const QStringList oldtranslations = localegroup.readEntry("Translations", QStringList());
|
||||
|
||||
for ( int i = 0, count = d->languageButtons.count(); i < count; ++i ) {
|
||||
KLanguageButton *languageButton = d->languageButtons[i];
|
||||
languages << languageButton->current();
|
||||
}
|
||||
const QStringList newtranslations = QStringList(); // TODO:
|
||||
if (oldtranslations != newtranslations) {
|
||||
localegroup.writeEntry("Translations", newtranslations);
|
||||
localegroup.sync();
|
||||
|
||||
if (d->applicationLanguageList() != languages) {
|
||||
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
|
||||
);
|
||||
|
||||
QEvent ev(QEvent::LanguageChange);
|
||||
QApplication::sendEvent(qApp, &ev);
|
||||
languageChanged(this);
|
||||
}
|
||||
|
||||
accept();
|
||||
|
@ -223,131 +84,17 @@ void KSwitchLanguageDialog::slotOk()
|
|||
|
||||
void KSwitchLanguageDialog::slotDefault()
|
||||
{
|
||||
const QStringList defaultLanguages = d->applicationLanguageList();
|
||||
KConfigGroup localegroup(KGlobal::config(), "Locale");
|
||||
const QStringList oldtranslations = localegroup.readEntry("Translations", QStringList());
|
||||
localegroup.revertToDefault("Translations");
|
||||
localegroup.sync();
|
||||
|
||||
KConfigGroup group(KGlobal::config(), "Locale");
|
||||
|
||||
group.revertToDefault("Language");
|
||||
group.sync();
|
||||
// read back the new default
|
||||
const QString language = group.readEntry("Language", KLocale::defaultLanguage());
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
QEvent ev(QEvent::LanguageChange);
|
||||
QApplication::sendEvent(qApp, &ev);
|
||||
const QStringList newtranslations = localegroup.readEntry("Translations", QStringList());
|
||||
if (oldtranslations != newtranslations) {
|
||||
languageChanged(this);
|
||||
}
|
||||
|
||||
accept();
|
||||
}
|
||||
|
||||
/************************ KSwitchLanguageDialogPrivate ***********************/
|
||||
|
||||
KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent)
|
||||
: p(parent)
|
||||
{
|
||||
// NOTE: do NOT use "p" in constructor, it is not fully constructed
|
||||
}
|
||||
|
||||
void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button)
|
||||
{
|
||||
QStringList allLanguages = KLocale::installedLanguages();
|
||||
allLanguages.prepend(KLocale::defaultLanguage());
|
||||
for (int i = 0, count = allLanguages.count(); i < count; ++i) {
|
||||
QString languageCode = allLanguages[i];
|
||||
if (KLocale::isApplicationTranslatedInto(languageCode)) {
|
||||
button->insertLanguage(languageCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QStringList KSwitchLanguageDialogPrivate::applicationLanguageList()
|
||||
{
|
||||
KSharedConfigPtr config = KGlobal::config();
|
||||
QStringList languagesList;
|
||||
|
||||
if (config->hasGroup("Locale")) {
|
||||
KConfigGroup group(config, "Locale");
|
||||
if (group.hasKey("Language")) {
|
||||
languagesList = group.readEntry("Language", QString()).split(':');
|
||||
}
|
||||
}
|
||||
if (languagesList.isEmpty()) {
|
||||
languagesList = KGlobal::locale()->languageList();
|
||||
}
|
||||
|
||||
KLocale *locale = KGlobal::locale();
|
||||
for (int i = 0; i < languagesList.count();) {
|
||||
if (!locale->isApplicationTranslatedInto(languagesList[i])) {
|
||||
languagesList.removeAt(i);
|
||||
} else {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
return languagesList;
|
||||
}
|
||||
|
||||
void KSwitchLanguageDialogPrivate::addLanguageButton(const QString &languageCode, bool primaryLanguage)
|
||||
{
|
||||
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))
|
||||
);
|
||||
|
||||
LanguageRowData languageRowData;
|
||||
KPushButton *removeButton = nullptr;
|
||||
|
||||
if (!primaryLanguage) {
|
||||
removeButton = new KPushButton(i18n("Remove"), page);
|
||||
|
||||
QObject::connect(
|
||||
removeButton,
|
||||
SIGNAL(clicked()),
|
||||
p,
|
||||
SLOT(removeButtonClicked())
|
||||
);
|
||||
}
|
||||
|
||||
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.")
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
if (!primaryLanguage) {
|
||||
languagesLayout->addWidget(removeButton, numRows + 1, 3, Qt::AlignLeft);
|
||||
languageRowData.setRowWidgets(languageLabel, languageButton, removeButton);
|
||||
removeButton->show();
|
||||
}
|
||||
|
||||
languageRows.insert(removeButton, languageRowData);
|
||||
|
||||
languageButtons.append(languageButton);
|
||||
languageButton->show();
|
||||
languageLabel->show();
|
||||
}
|
||||
|
||||
}
|
||||
#include "moc_kswitchlanguagedialog_p.cpp"
|
||||
|
|
|
@ -1,32 +1,29 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
/* This file is part of the KDE libraries
|
||||
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef _KSWITCHLANGUAGEDIALOG_H_
|
||||
#define _KSWITCHLANGUAGEDIALOG_H_
|
||||
#ifndef KSWITCHLANGUAGEDIALOG_H
|
||||
#define KSWITCHLANGUAGEDIALOG_H
|
||||
|
||||
#include <kdialog.h>
|
||||
#include "kdialog.h"
|
||||
#include "keditlistwidget.h"
|
||||
|
||||
namespace KDEPrivate {
|
||||
|
||||
class KSwitchLanguageDialogPrivate;
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
|
||||
/**
|
||||
* @short Standard "switch application language" dialog box.
|
||||
|
@ -34,48 +31,24 @@ class KSwitchLanguageDialogPrivate;
|
|||
* This class provides "switch application language" dialog box that is used
|
||||
* in KHelpMenu
|
||||
*
|
||||
* @author Krzysztof Lichota (lichota@mimuw.edu.pl)
|
||||
* @internal
|
||||
*/
|
||||
class KSwitchLanguageDialog : public KDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
* Constructor. Creates a fully featured "Switch application language" dialog box.
|
||||
* Note that this dialog is made modeless in the KHelpMenu class so
|
||||
* the users may expect a modeless dialog.
|
||||
*/
|
||||
KSwitchLanguageDialog(QWidget *parent = nullptr);
|
||||
virtual ~KSwitchLanguageDialog();
|
||||
~KSwitchLanguageDialog();
|
||||
|
||||
protected Q_SLOTS:
|
||||
/**
|
||||
* Activated when the Ok button has been clicked.
|
||||
*/
|
||||
virtual void slotOk();
|
||||
private Q_SLOTS:
|
||||
void slotOk();
|
||||
void slotDefault();
|
||||
|
||||
/**
|
||||
*Called when one of language buttons changes state.
|
||||
*/
|
||||
virtual void languageOnButtonChanged(const QString &);
|
||||
|
||||
/**
|
||||
*Called to add one language button to dialog.
|
||||
*/
|
||||
virtual void slotAddLanguageButton();
|
||||
|
||||
/**
|
||||
Called when "Remove" language button is clicked.
|
||||
*/
|
||||
virtual void removeButtonClicked();
|
||||
|
||||
|
||||
private:
|
||||
KSwitchLanguageDialogPrivate *const d;
|
||||
friend class KSwitchLanguageDialogPrivate;
|
||||
QWidget* m_dialogwidget;
|
||||
QVBoxLayout* m_dialoglayout;
|
||||
QLabel* m_languagelabel;
|
||||
KEditListWidget* m_languageedit;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // KSWITCHLANGUAGEDIALOG_H
|
||||
|
|
|
@ -1,264 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
|
||||
* (c) 2007 David Jarvie <software@astrojar.org.uk>
|
||||
*
|
||||
* 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_klanguagebutton.cpp"
|
||||
|
||||
#include <QtGui/QMenu>
|
||||
#include <QtGui/QLayout>
|
||||
#include <QtGui/QPushButton>
|
||||
|
||||
#include <klocale.h>
|
||||
#include <kstandarddirs.h>
|
||||
#include <kdebug.h>
|
||||
#include <kconfiggroup.h>
|
||||
|
||||
static void checkInsertPos( QMenu *popup, const QString &str, int &index )
|
||||
{
|
||||
if ( index != -1 )
|
||||
return;
|
||||
|
||||
int a = 0;
|
||||
const QList<QAction*> actions = popup->actions();
|
||||
int b = actions.count();
|
||||
|
||||
while ( a < b )
|
||||
{
|
||||
int w = ( a + b ) / 2;
|
||||
QAction *ac = actions[ w ];
|
||||
int j = str.localeAwareCompare( ac->text() );
|
||||
if ( j > 0 )
|
||||
a = w + 1;
|
||||
else
|
||||
b = w;
|
||||
}
|
||||
|
||||
index = a; // it doesn't really matter ... a == b here.
|
||||
|
||||
Q_ASSERT( a == b );
|
||||
}
|
||||
|
||||
class KLanguageButtonPrivate
|
||||
{
|
||||
public:
|
||||
KLanguageButtonPrivate( KLanguageButton *parent);
|
||||
~KLanguageButtonPrivate() { delete button; delete popup; }
|
||||
void setCurrentItem( QAction* );
|
||||
void clear();
|
||||
QAction *findAction(const QString &data) const;
|
||||
|
||||
QPushButton *button;
|
||||
QStringList ids;
|
||||
QMenu *popup;
|
||||
QString current;
|
||||
const KLocale *locale;
|
||||
bool staticText : 1;
|
||||
bool showCodes : 1;
|
||||
};
|
||||
|
||||
KLanguageButton::KLanguageButton( QWidget * parent )
|
||||
: QWidget( parent ),
|
||||
d( new KLanguageButtonPrivate(this) )
|
||||
{
|
||||
}
|
||||
|
||||
KLanguageButton::KLanguageButton( const QString & text, QWidget * parent )
|
||||
: QWidget( parent ),
|
||||
d( new KLanguageButtonPrivate(this) )
|
||||
{
|
||||
setText(text);
|
||||
}
|
||||
|
||||
KLanguageButtonPrivate::KLanguageButtonPrivate( KLanguageButton *parent )
|
||||
: button(new QPushButton(parent)),
|
||||
popup(new QMenu(parent)),
|
||||
locale(0),
|
||||
staticText(false),
|
||||
showCodes(false)
|
||||
{
|
||||
QHBoxLayout *layout = new QHBoxLayout( parent );
|
||||
layout->setMargin(0);
|
||||
layout->addWidget( button );
|
||||
|
||||
parent->setFocusProxy( button );
|
||||
parent->setFocusPolicy( button->focusPolicy() );
|
||||
|
||||
button->setMenu( popup );
|
||||
|
||||
QObject::connect( popup, SIGNAL(triggered(QAction*)), parent, SLOT(slotTriggered(QAction*)) );
|
||||
QObject::connect( popup, SIGNAL(hovered(QAction*)), parent, SLOT(slotHovered(QAction*)) );
|
||||
}
|
||||
|
||||
KLanguageButton::~KLanguageButton()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void KLanguageButton::setText(const QString & text)
|
||||
{
|
||||
d->staticText = true;
|
||||
d->button->setText(text);
|
||||
}
|
||||
|
||||
void KLanguageButton::setLocale( const KLocale *locale )
|
||||
{
|
||||
d->locale = locale;
|
||||
}
|
||||
|
||||
void KLanguageButton::showLanguageCodes( bool show )
|
||||
{
|
||||
d->showCodes = show;
|
||||
}
|
||||
|
||||
void KLanguageButton::insertLanguage( const QString &languageCode, const QString &name, int index )
|
||||
{
|
||||
QString text;
|
||||
bool showCodes = d->showCodes;
|
||||
if (name.isEmpty())
|
||||
{
|
||||
text = languageCode;
|
||||
const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
|
||||
if (locale)
|
||||
text = locale->languageCodeToName(languageCode);
|
||||
else
|
||||
showCodes = false;
|
||||
}
|
||||
else
|
||||
text = name;
|
||||
if (showCodes)
|
||||
text += QLatin1String( " (" ) + languageCode + QLatin1Char(')');
|
||||
|
||||
checkInsertPos( d->popup, text, index );
|
||||
QAction *a = new QAction(QIcon(), text, this);
|
||||
a->setData(languageCode);
|
||||
if ( index >= 0 && index < d->popup->actions().count()-1)
|
||||
d->popup->insertAction(d->popup->actions()[index], a);
|
||||
else
|
||||
d->popup->addAction(a);
|
||||
d->ids.append(languageCode);
|
||||
}
|
||||
|
||||
void KLanguageButton::insertSeparator( int index )
|
||||
{
|
||||
if ( index >= 0 && index < d->popup->actions().count()-1)
|
||||
d->popup->insertSeparator(d->popup->actions()[index]);
|
||||
else
|
||||
d->popup->addSeparator();
|
||||
}
|
||||
|
||||
void KLanguageButton::loadAllLanguages()
|
||||
{
|
||||
QStringList langlist = KGlobal::dirs()->findAllResources("locale",
|
||||
QString::fromLatin1("l10n/*/entry.desktop"));
|
||||
langlist.sort();
|
||||
for (int i = 0, count = langlist.count(); i < count; ++i)
|
||||
{
|
||||
QString fpath = langlist[i].left(langlist[i].length() - 14);
|
||||
QString code = fpath.mid(fpath.lastIndexOf('/') + 1);
|
||||
KConfig entry(langlist[i], KConfig::SimpleConfig);
|
||||
KConfigGroup group(&entry, "KCM Locale");
|
||||
QString name = group.readEntry("Name", i18n("without name"));
|
||||
insertLanguage(code, name, i);
|
||||
}
|
||||
|
||||
const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
|
||||
setCurrentItem(locale ? locale->language() : KLocale::defaultLanguage());
|
||||
}
|
||||
|
||||
void KLanguageButton::slotTriggered( QAction *a )
|
||||
{
|
||||
//kDebug() << "slotTriggered" << index;
|
||||
if (!a)
|
||||
return;
|
||||
|
||||
d->setCurrentItem( a );
|
||||
|
||||
// Forward event from popup menu as if it was emitted from this widget:
|
||||
emit activated( d->current );
|
||||
}
|
||||
|
||||
void KLanguageButton::slotHovered( QAction *a )
|
||||
{
|
||||
//kDebug() << "slotHovered" << index;
|
||||
|
||||
emit highlighted(a->data().toString());
|
||||
}
|
||||
|
||||
int KLanguageButton::count() const
|
||||
{
|
||||
return d->ids.count();
|
||||
}
|
||||
|
||||
void KLanguageButton::clear()
|
||||
{
|
||||
d->clear();
|
||||
}
|
||||
|
||||
void KLanguageButtonPrivate::clear()
|
||||
{
|
||||
ids.clear();
|
||||
popup->clear();
|
||||
|
||||
if ( !staticText ) {
|
||||
button->setText( QString() );
|
||||
}
|
||||
}
|
||||
|
||||
bool KLanguageButton::contains( const QString &languageCode ) const
|
||||
{
|
||||
return d->ids.contains( languageCode );
|
||||
}
|
||||
|
||||
QString KLanguageButton::current() const
|
||||
{
|
||||
return d->current.isEmpty() ? QLatin1String("en") : d->current;
|
||||
}
|
||||
|
||||
QAction *KLanguageButtonPrivate::findAction(const QString& data) const
|
||||
{
|
||||
foreach(QAction *a, popup->actions()) {
|
||||
if (!a->data().toString().compare(data))
|
||||
return a;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void KLanguageButton::setCurrentItem( const QString & languageCode )
|
||||
{
|
||||
if (!d->ids.count())
|
||||
return;
|
||||
QAction *a;
|
||||
if (d->ids.indexOf(languageCode) < 0)
|
||||
a = d->findAction(d->ids[0]);
|
||||
else
|
||||
a = d->findAction(languageCode);
|
||||
if (a)
|
||||
d->setCurrentItem(a);
|
||||
}
|
||||
|
||||
void KLanguageButtonPrivate::setCurrentItem( QAction *a )
|
||||
{
|
||||
if (!a->data().isValid())
|
||||
return;
|
||||
current = a->data().toString();
|
||||
|
||||
if ( !staticText ) {
|
||||
button->setText( a->text() );
|
||||
}
|
||||
}
|
|
@ -1,176 +0,0 @@
|
|||
/*
|
||||
* klangbutton.h - Button with language selection drop down menu.
|
||||
* Derived from the KLangCombo class by Hans Petter Bieker.
|
||||
*
|
||||
* Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
|
||||
* (c) 2001 Martijn Klingens <klingens@kde.org>
|
||||
* (c) 2007 David Jarvie <software@astrojar.org.uk>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef KLANGUAGEBUTTON_H
|
||||
#define KLANGUAGEBUTTON_H
|
||||
|
||||
#include <kdeui_export.h>
|
||||
#include <QtGui/QWidget>
|
||||
|
||||
#include <QAction>
|
||||
class KLocale;
|
||||
class KLanguageButtonPrivate;
|
||||
|
||||
/**
|
||||
* KLanguageButton is a pushbutton which allows a language to be selected from
|
||||
* a popup list.
|
||||
*
|
||||
* Languages are identified by their ISO 639-1 codes, e.g. en, pt_BR.
|
||||
*
|
||||
* \image html klanguagebutton.png "KDE Language Selection Widget"
|
||||
*
|
||||
* @author Hans Petter Bieker <bieker@kde.org>, Martijn Klingens <klingens@kde.org>,
|
||||
* David Jarvie <software@astrojar.org.uk>
|
||||
*/
|
||||
class KDEUI_EXPORT KLanguageButton : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs a button whose text is determined by the current language
|
||||
* in the popup list.
|
||||
*
|
||||
* @param parent the parent of the button
|
||||
*/
|
||||
explicit KLanguageButton(QWidget * parent = 0);
|
||||
|
||||
/**
|
||||
* Constructs a button with static text.
|
||||
*
|
||||
* @param text the text of the button
|
||||
* @param parent the parent of the button
|
||||
*/
|
||||
explicit KLanguageButton(const QString & text, QWidget * parent = 0 );
|
||||
|
||||
/**
|
||||
* Deconstructor
|
||||
*/
|
||||
virtual ~KLanguageButton();
|
||||
|
||||
/**
|
||||
* Sets the locale to display language names. By default, KGlobal::locale() is used.
|
||||
*
|
||||
* @param locale locale to use
|
||||
*/
|
||||
void setLocale( const KLocale *locale );
|
||||
|
||||
/**
|
||||
* Sets a static button text.
|
||||
*
|
||||
* @param text button text
|
||||
*/
|
||||
void setText( const QString &text );
|
||||
|
||||
/**
|
||||
* Specifies whether language codes should be shown alongside language names
|
||||
* in the popup. Calling this method does not affect any previously
|
||||
* inserted language texts, so it should normally be called before
|
||||
* populating the list.
|
||||
*
|
||||
* @param show true to show codes, false to hide codes
|
||||
*/
|
||||
void showLanguageCodes( bool show );
|
||||
|
||||
/**
|
||||
* Load all known languages into the popup list.
|
||||
* The current language in the list is set to the default language for the
|
||||
* current locale (as modified by setLocale()).
|
||||
*/
|
||||
void loadAllLanguages();
|
||||
|
||||
/**
|
||||
* Inserts a language into the combo box.
|
||||
* Normally the display name of the language is obtained automatically, but
|
||||
* if either the language code does not exist, or there are special display
|
||||
* requirements, the name of the language can be specified in @p name.
|
||||
*
|
||||
* @param languageCode the code for the language
|
||||
* @param name language name. If empty, the name is obtained automatically.
|
||||
* @param index the insertion position, or -1 to insert in alphabetical order
|
||||
*/
|
||||
void insertLanguage( const QString &languageCode, const QString &name = QString(), int index = -1 );
|
||||
|
||||
/**
|
||||
* Inserts a separator item into the combo box. A negative index will append the item.
|
||||
*
|
||||
* @param index the insertion position
|
||||
*/
|
||||
void insertSeparator( int index = -1 );
|
||||
|
||||
/**
|
||||
* Returns the number of items in the combo box.
|
||||
*/
|
||||
int count() const;
|
||||
|
||||
/**
|
||||
* Removes all combobox items.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Returns the language code of the combobox's current item.
|
||||
*
|
||||
* @return the current item's language code
|
||||
*/
|
||||
QString current() const;
|
||||
|
||||
/**
|
||||
* Checks whether the specified language is in the popup list.
|
||||
*
|
||||
* @param languageCode the language's code
|
||||
* @return true if in the list
|
||||
*/
|
||||
bool contains( const QString &languageCode ) const;
|
||||
|
||||
/**
|
||||
* Sets a given language to be the current item.
|
||||
*
|
||||
* @param languageCode the language's code
|
||||
*/
|
||||
void setCurrentItem( const QString &languageCode );
|
||||
|
||||
Q_SIGNALS:
|
||||
/**
|
||||
* This signal is emitted when a new item is activated.
|
||||
*
|
||||
* @param languageCode code of the activated language
|
||||
*/
|
||||
void activated( const QString &languageCode );
|
||||
/**
|
||||
* This signal is emitted when a new item is highlighted.
|
||||
*
|
||||
* @param languageCode code of the highlighted language
|
||||
*/
|
||||
void highlighted( const QString &languageCode );
|
||||
|
||||
private Q_SLOTS:
|
||||
void slotTriggered( QAction* );
|
||||
void slotHovered( QAction* );
|
||||
|
||||
private:
|
||||
KLanguageButtonPrivate * const d;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -151,12 +151,6 @@ WhatsThis=A widget to pick a QKeySequence.
|
|||
IncludeFile=kkeysequencewidget.h
|
||||
Group=Buttons (KDE)
|
||||
|
||||
[KLanguageButton]
|
||||
ToolTip=Language Button (KDE)
|
||||
WhatsThis=KDE's language button.
|
||||
IncludeFile=klanguagebutton.h
|
||||
Group=Buttons (KDE)
|
||||
|
||||
[KLed]
|
||||
ToolTip=LED Widget (KDE)
|
||||
WhatsThis=A widget showing a light emitter diode
|
||||
|
|
Loading…
Add table
Reference in a new issue