Krita Source Code Documentation
Loading...
Searching...
No Matches
KLanguageButton Class Reference

#include <klanguagebutton.h>

+ Inheritance diagram for KLanguageButton:

Signals

void activated (const QString &languageCode)
 
void highlighted (const QString &languageCode)
 

Public Member Functions

void clear ()
 
bool contains (const QString &languageCode) const
 
int count () const
 
QString current () const
 
void insertLanguage (const QString &languageCode, const QString &name=QString(), int index=-1)
 
void insertSeparator (int index=-1)
 
 KLanguageButton (const QString &text, QWidget *parent=0)
 
 KLanguageButton (QWidget *parent=0)
 
void loadAllLanguages ()
 
void setCurrentItem (const QString &languageCode)
 
void setLocale (const QString &locale)
 
void setText (const QString &text)
 
void showLanguageCodes (bool show)
 
 ~KLanguageButton () override
 

Private Slots

void slotHovered (QAction *)
 
void slotTriggered (QAction *)
 

Private Attributes

KLanguageButtonPrivate *const d
 

Detailed Description

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.

KDE Language Selection Widget
Author
Hans Petter Bieker bieke.nosp@m.r@kd.nosp@m.e.org, Martijn Klingens kling.nosp@m.ens@.nosp@m.kde.o.nosp@m.rg, David Jarvie softw.nosp@m.are@.nosp@m.astro.nosp@m.jar..nosp@m.org.u.nosp@m.k

Definition at line 32 of file klanguagebutton.h.

Constructor & Destructor Documentation

◆ KLanguageButton() [1/2]

KLanguageButton::KLanguageButton ( QWidget * parent = 0)
explicit

Constructs a button whose text is determined by the current language in the popup list.

Parameters
parentthe parent of the button

Definition at line 69 of file klanguagebutton.cpp.

70 : QWidget(parent),
71 d(new KLanguageButtonPrivate(this))
72{
73}
KLanguageButtonPrivate *const d

◆ KLanguageButton() [2/2]

KLanguageButton::KLanguageButton ( const QString & text,
QWidget * parent = 0 )
explicit

Constructs a button with static text.

Parameters
textthe text of the button
parentthe parent of the button

Definition at line 75 of file klanguagebutton.cpp.

76 : QWidget(parent),
77 d(new KLanguageButtonPrivate(this))
78{
79 setText(text);
80}
void setText(const QString &text)

References setText().

◆ ~KLanguageButton()

KLanguageButton::~KLanguageButton ( )
override

Deconstructor

Definition at line 102 of file klanguagebutton.cpp.

103{
104 delete d;
105}

References d.

Member Function Documentation

◆ activated

void KLanguageButton::activated ( const QString & languageCode)
signal

This signal is emitted when a new item is activated.

Parameters
languageCodecode of the activated language

◆ clear()

void KLanguageButton::clear ( )

Removes all combobox items.

Definition at line 213 of file klanguagebutton.cpp.

214{
215 d->clear();
216}

References KLanguageButtonPrivate::clear(), and d.

◆ contains()

bool KLanguageButton::contains ( const QString & languageCode) const

Checks whether the specified language is in the popup list.

Parameters
languageCodethe language's code
Returns
true if in the list

Definition at line 228 of file klanguagebutton.cpp.

229{
230 return d->ids.contains(languageCode);
231}

References d, and KLanguageButtonPrivate::ids.

◆ count()

int KLanguageButton::count ( ) const

Returns the number of items in the combo box.

Definition at line 208 of file klanguagebutton.cpp.

209{
210 return d->ids.count();
211}

References d, and KLanguageButtonPrivate::ids.

◆ current()

QString KLanguageButton::current ( ) const

Returns the language code of the combobox's current item.

Returns
the current item's language code

Definition at line 233 of file klanguagebutton.cpp.

234{
235 return d->current.isEmpty() ? QLatin1String("en") : d->current;
236}
QString current() const

References KLanguageButtonPrivate::current, and d.

◆ highlighted

void KLanguageButton::highlighted ( const QString & languageCode)
signal

This signal is emitted when a new item is highlighted.

Parameters
languageCodecode of the highlighted language

◆ insertLanguage()

void KLanguageButton::insertLanguage ( const QString & languageCode,
const QString & name = QString(),
int index = -1 )

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 name.

Parameters
languageCodethe code for the language
namelanguage name. If empty, the name is obtained automatically.
indexthe insertion position, or -1 to insert in alphabetical order

Definition at line 123 of file klanguagebutton.cpp.

124{
125 QString text;
126 bool showCodes = d->showCodes;
127 if (name.isEmpty()) {
128 text = languageCode;
129 QLocale locale(languageCode);
130 if (locale != QLocale::c()) {
131 text = locale.nativeLanguageName();
132 } else {
133 showCodes = false;
134 }
135 } else {
136 text = name;
137 }
138 if (showCodes) {
139 text += QLatin1String(" (") + languageCode + QLatin1Char(')');
140 }
141
142 checkInsertPos(d->popup, text, index);
143 QAction *a = new QAction(QIcon(), text, this);
144 a->setData(languageCode);
145 if (index >= 0 && index < d->popup->actions().count() - 1) {
146 d->popup->insertAction(d->popup->actions()[index], a);
147 } else {
148 d->popup->addAction(a);
149 }
150 d->ids.append(languageCode);
151}
static void checkInsertPos(QMenu *popup, const QString &str, int &index)
const char * name(StandardAction id)

References checkInsertPos(), d, KLanguageButtonPrivate::ids, KLanguageButtonPrivate::popup, and KLanguageButtonPrivate::showCodes.

◆ insertSeparator()

void KLanguageButton::insertSeparator ( int index = -1)

Inserts a separator item into the combo box. A negative index will append the item.

Parameters
indexthe insertion position

Definition at line 153 of file klanguagebutton.cpp.

154{
155 if (index >= 0 && index < d->popup->actions().count() - 1) {
156 d->popup->insertSeparator(d->popup->actions()[index]);
157 } else {
158 d->popup->addSeparator();
159 }
160}

References d, and KLanguageButtonPrivate::popup.

◆ loadAllLanguages()

void KLanguageButton::loadAllLanguages ( )

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()).

Definition at line 162 of file klanguagebutton.cpp.

163{
164 QStringList langlist;
165 const QStringList localeDirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QString("locale"), QStandardPaths::LocateDirectory);
166 Q_FOREACH (const QString &localeDir, localeDirs) {
167 const QStringList entries = QDir(localeDir).entryList(QDir::Dirs);
168 Q_FOREACH (const QString &d, entries) {
169 const QString entryFile = localeDir + '/' + d + "/kf5_entry.desktop";
170 if (QFile::exists(entryFile)) {
171 langlist.append(entryFile);
172 }
173 }
174 }
175 langlist.sort();
176 for (int i = 0, count = langlist.count(); i < count; ++i) {
177 QString fpath = langlist[i].left(langlist[i].length() - 14);
178 QString code = fpath.mid(fpath.lastIndexOf('/') + 1);
179 KConfig entry(langlist[i], KConfig::SimpleConfig);
180 KConfigGroup group(&entry, "KCM Locale");
181 QString name = group.readEntry("Name", i18n("without name"));
182 insertLanguage(code, name);
183 }
184
186}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
void insertLanguage(const QString &languageCode, const QString &name=QString(), int index=-1)
void setCurrentItem(const QString &languageCode)

References count(), d, insertLanguage(), length(), KLanguageButtonPrivate::locale, and setCurrentItem().

◆ setCurrentItem()

void KLanguageButton::setCurrentItem ( const QString & languageCode)

Sets a given language to be the current item.

Parameters
languageCodethe language's code

Definition at line 248 of file klanguagebutton.cpp.

249{
250 if (!d->ids.count()) {
251 return;
252 }
253 QAction *a;
254 if (d->ids.indexOf(languageCode) < 0) {
255 a = d->findAction(d->ids[0]);
256 } else {
257 a = d->findAction(languageCode);
258 }
259 if (a) {
260 d->setCurrentItem(a);
261 }
262}
QAction * findAction(const QString &data) const
void setCurrentItem(QAction *)

References d, KLanguageButtonPrivate::findAction(), KLanguageButtonPrivate::ids, and KLanguageButtonPrivate::setCurrentItem().

◆ setLocale()

void KLanguageButton::setLocale ( const QString & locale)

Sets the locale to display language names. By default, QLocale::system().name() is used.

Parameters
localelocale to use

Definition at line 113 of file klanguagebutton.cpp.

114{
115 d->locale = locale;
116}

References d, and KLanguageButtonPrivate::locale.

◆ setText()

void KLanguageButton::setText ( const QString & text)

Sets a static button text.

Parameters
textbutton text

Definition at line 107 of file klanguagebutton.cpp.

108{
109 d->staticText = true;
110 d->button->setText(text);
111}

References KLanguageButtonPrivate::button, d, and KLanguageButtonPrivate::staticText.

◆ showLanguageCodes()

void KLanguageButton::showLanguageCodes ( bool show)

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.

Parameters
showtrue to show codes, false to hide codes

Definition at line 118 of file klanguagebutton.cpp.

119{
120 d->showCodes = show;
121}

References d, and KLanguageButtonPrivate::showCodes.

◆ slotHovered

void KLanguageButton::slotHovered ( QAction * a)
privateslot

Definition at line 201 of file klanguagebutton.cpp.

202{
203 //qDebug() << "slotHovered" << index;
204
205 Q_EMIT highlighted(a->data().toString());
206}
void highlighted(const QString &languageCode)

References highlighted().

◆ slotTriggered

void KLanguageButton::slotTriggered ( QAction * a)
privateslot

Definition at line 188 of file klanguagebutton.cpp.

189{
190 //qDebug() << "slotTriggered" << index;
191 if (!a) {
192 return;
193 }
194
195 d->setCurrentItem(a);
196
197 // Forward event from popup menu as if it was emitted from this widget:
198 Q_EMIT activated(d->current);
199}
void activated(const QString &languageCode)

References activated(), KLanguageButtonPrivate::current, d, and KLanguageButtonPrivate::setCurrentItem().

Member Data Documentation

◆ d

KLanguageButtonPrivate* const KLanguageButton::d
private

Definition at line 159 of file klanguagebutton.h.


The documentation for this class was generated from the following files: