Krita Source Code Documentation
Loading...
Searching...
No Matches
klanguagebutton.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 1999-2003 Hans Petter Bieker <bieker@kde.org>
3 * SPDX-FileCopyrightText: 2007 David Jarvie <software@astrojar.org.uk>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "klanguagebutton.h"
9
10#include <QMenu>
11#include <QLayout>
12#include <QPushButton>
13#include <QDir>
14#include <QFile>
15#include <QLocale>
16
17#include <klocalizedstring.h>
18#include <kconfig.h>
19#include <kconfiggroup.h>
20
21static void checkInsertPos(QMenu *popup, const QString &str, int &index)
22{
23 if (index != -1) {
24 return;
25 }
26
27 int a = 0;
28 const QList<QAction *> actions = popup->actions();
29 int b = actions.count();
30
31 while (a < b) {
32 int w = (a + b) / 2;
33 QAction *ac = actions[ w ];
34 int j = str.localeAwareCompare(ac->text());
35 if (j > 0) {
36 a = w + 1;
37 } else {
38 b = w;
39 }
40 }
41
42 index = a; // it doesn't really matter ... a == b here.
43
44 Q_ASSERT(a == b);
45}
46
48{
49public:
52 {
53 delete button;
54 delete popup;
55 }
56 void setCurrentItem(QAction *);
57 void clear();
58 QAction *findAction(const QString &data) const;
59
60 QPushButton *button;
62 QMenu *popup;
63 QString current;
64 QString locale;
65 bool staticText : 1;
66 bool showCodes : 1;
67};
68
70 : QWidget(parent),
71 d(new KLanguageButtonPrivate(this))
72{
73}
74
75KLanguageButton::KLanguageButton(const QString &text, QWidget *parent)
76 : QWidget(parent),
77 d(new KLanguageButtonPrivate(this))
78{
79 setText(text);
80}
81
83 : button(new QPushButton(parent)),
84 popup(new QMenu(parent)),
85 locale(QLocale::system().name()),
86 staticText(false),
87 showCodes(false)
88{
89 QHBoxLayout *layout = new QHBoxLayout(parent);
90 layout->setContentsMargins(0, 0, 0, 0);
91 layout->addWidget(button);
92
93 parent->setFocusProxy(button);
94 parent->setFocusPolicy(button->focusPolicy());
95
96 button->setMenu(popup);
97
98 QObject::connect(popup, SIGNAL(triggered(QAction*)), parent, SLOT(slotTriggered(QAction*)));
99 QObject::connect(popup, SIGNAL(hovered(QAction*)), parent, SLOT(slotHovered(QAction*)));
100}
101
103{
104 delete d;
105}
106
107void KLanguageButton::setText(const QString &text)
108{
109 d->staticText = true;
110 d->button->setText(text);
111}
112
113void KLanguageButton::setLocale(const QString &locale)
114{
115 d->locale = locale;
116}
117
119{
120 d->showCodes = show;
121}
122
123void KLanguageButton::insertLanguage(const QString &languageCode, const QString &name, int index)
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}
152
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}
161
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}
187
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}
200
202{
203 //qDebug() << "slotHovered" << index;
204
205 Q_EMIT highlighted(a->data().toString());
206}
207
209{
210 return d->ids.count();
211}
212
214{
215 d->clear();
216}
217
219{
220 ids.clear();
221 popup->clear();
222
223 if (!staticText) {
224 button->setText(QString());
225 }
226}
227
228bool KLanguageButton::contains(const QString &languageCode) const
229{
230 return d->ids.contains(languageCode);
231}
232
234{
235 return d->current.isEmpty() ? QLatin1String("en") : d->current;
236}
237
238QAction *KLanguageButtonPrivate::findAction(const QString &data) const
239{
240 Q_FOREACH (QAction *a, popup->actions()) {
241 if (!a->data().toString().compare(data)) {
242 return a;
243 }
244 }
245 return 0;
246}
247
248void KLanguageButton::setCurrentItem(const QString &languageCode)
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}
263
265{
266 if (!a->data().isValid()) {
267 return;
268 }
269 current = a->data().toString();
270
271 if (!staticText) {
272 button->setText(a->text());
273 }
274}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
KLanguageButtonPrivate(KLanguageButton *parent)
QAction * findAction(const QString &data) const
void setCurrentItem(QAction *)
KLanguageButton(QWidget *parent=0)
QString current() const
void insertSeparator(int index=-1)
void highlighted(const QString &languageCode)
void activated(const QString &languageCode)
void insertLanguage(const QString &languageCode, const QString &name=QString(), int index=-1)
void setCurrentItem(const QString &languageCode)
void slotHovered(QAction *)
void showLanguageCodes(bool show)
~KLanguageButton() override
void setText(const QString &text)
bool contains(const QString &languageCode) const
void setLocale(const QString &locale)
KLanguageButtonPrivate *const d
void slotTriggered(QAction *)
QString button(const QWheelEvent &ev)
static void checkInsertPos(QMenu *popup, const QString &str, int &index)