• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
klanguagebutton.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
3 * (c) 2007 David Jarvie <software@astrojar.org.uk>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "klanguagebutton.moc"
22
23#include <QtGui/QMenu>
24#include <QtGui/QLayout>
25#include <QtGui/QPushButton>
26#include <QtGui/QMenuItem>
27
28#include <klocale.h>
29#include <kstandarddirs.h>
30#include <kdebug.h>
31#include <kconfiggroup.h>
32
33static void checkInsertPos( QMenu *popup, const QString &str, int &index )
34{
35 if ( index != -1 )
36 return;
37
38 int a = 0;
39 const QList<QAction*> actions = popup->actions();
40 int b = actions.count();
41
42 while ( a < b )
43 {
44 int w = ( a + b ) / 2;
45 QAction *ac = actions[ w ];
46 int j = str.localeAwareCompare( ac->text() );
47 if ( j > 0 )
48 a = w + 1;
49 else
50 b = w;
51 }
52
53 index = a; // it doesn't really matter ... a == b here.
54
55 Q_ASSERT( a == b );
56}
57
58class KLanguageButtonPrivate
59{
60public:
61 KLanguageButtonPrivate( KLanguageButton *parent);
62 ~KLanguageButtonPrivate() { delete button; delete popup; }
63 void setCurrentItem( QAction* );
64 void clear();
65 QAction *findAction(const QString &data) const;
66
67 QPushButton *button;
68 QStringList ids;
69 QMenu *popup;
70 QString current;
71 const KLocale *locale;
72 bool staticText : 1;
73 bool showCodes : 1;
74};
75
76KLanguageButton::KLanguageButton( QWidget * parent )
77 : QWidget( parent ),
78 d( new KLanguageButtonPrivate(this) )
79{
80}
81
82KLanguageButton::KLanguageButton( const QString & text, QWidget * parent )
83 : QWidget( parent ),
84 d( new KLanguageButtonPrivate(this) )
85{
86 setText(text);
87}
88
89KLanguageButtonPrivate::KLanguageButtonPrivate( KLanguageButton *parent )
90 : button(new QPushButton(parent)),
91 popup(new QMenu(parent)),
92 locale(0),
93 staticText(false),
94 showCodes(false)
95{
96 QHBoxLayout *layout = new QHBoxLayout( parent );
97 layout->setMargin(0);
98 layout->addWidget( button );
99
100 parent->setFocusProxy( button );
101 parent->setFocusPolicy( button->focusPolicy() );
102
103 button->setMenu( popup );
104
105 QObject::connect( popup, SIGNAL(triggered(QAction*)), parent, SLOT(slotTriggered(QAction*)) );
106 QObject::connect( popup, SIGNAL(hovered(QAction*)), parent, SLOT(slotHovered(QAction*)) );
107}
108
109KLanguageButton::~KLanguageButton()
110{
111 delete d;
112}
113
114void KLanguageButton::setText(const QString & text)
115{
116 d->staticText = true;
117 d->button->setText(text);
118}
119
120void KLanguageButton::setLocale( const KLocale *locale )
121{
122 d->locale = locale;
123}
124
125void KLanguageButton::showLanguageCodes( bool show )
126{
127 d->showCodes = show;
128}
129
130void KLanguageButton::insertLanguage( const QString &languageCode, const QString &name, int index )
131{
132 QString text;
133 bool showCodes = d->showCodes;
134 if (name.isEmpty())
135 {
136 text = languageCode;
137 const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
138 if (locale)
139 text = locale->languageCodeToName(languageCode);
140 else
141 showCodes = false;
142 }
143 else
144 text = name;
145 if (showCodes)
146 text += QLatin1String( " (" ) + languageCode + QLatin1Char(')');
147
148 checkInsertPos( d->popup, text, index );
149 QAction *a = new QAction(QIcon(), text, this);
150 a->setData(languageCode);
151 if ( index >= 0 && index < d->popup->actions().count()-1)
152 d->popup->insertAction(d->popup->actions()[index], a);
153 else
154 d->popup->addAction(a);
155 d->ids.append(languageCode);
156}
157
158void KLanguageButton::insertSeparator( int index )
159{
160 if ( index >= 0 && index < d->popup->actions().count()-1)
161 d->popup->insertSeparator(d->popup->actions()[index]);
162 else
163 d->popup->addSeparator();
164}
165
166void KLanguageButton::loadAllLanguages()
167{
168 QStringList langlist = KGlobal::dirs()->findAllResources("locale",
169 QString::fromLatin1("*/entry.desktop"));
170 langlist.sort();
171 for (int i = 0, count = langlist.count(); i < count; ++i)
172 {
173 QString fpath = langlist[i].left(langlist[i].length() - 14);
174 QString code = fpath.mid(fpath.lastIndexOf('/') + 1);
175 KConfig entry(langlist[i], KConfig::SimpleConfig);
176 KConfigGroup group(&entry, "KCM Locale");
177 QString name = group.readEntry("Name", i18n("without name"));
178 insertLanguage(code, name);
179 }
180
181 const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
182 setCurrentItem(locale ? locale->language() : KLocale::defaultLanguage());
183}
184
185void KLanguageButton::slotTriggered( QAction *a )
186{
187 //kDebug() << "slotTriggered" << index;
188 if (!a)
189 return;
190
191 d->setCurrentItem( a );
192
193 // Forward event from popup menu as if it was emitted from this widget:
194 emit activated( d->current );
195}
196
197void KLanguageButton::slotHovered( QAction *a )
198{
199 //kDebug() << "slotHovered" << index;
200
201 emit highlighted(a->data().toString());
202}
203
204int KLanguageButton::count() const
205{
206 return d->ids.count();
207}
208
209void KLanguageButton::clear()
210{
211 d->clear();
212}
213
214void KLanguageButtonPrivate::clear()
215{
216 ids.clear();
217 popup->clear();
218
219 if ( !staticText ) {
220 button->setText( QString() );
221 }
222}
223
224bool KLanguageButton::contains( const QString &languageCode ) const
225{
226 return d->ids.contains( languageCode );
227}
228
229QString KLanguageButton::current() const
230{
231 return d->current.isEmpty() ? QLatin1String("en") : d->current;
232}
233
234QAction *KLanguageButtonPrivate::findAction(const QString& data) const
235{
236 foreach(QAction *a, popup->actions()) {
237 if (!a->data().toString().compare(data))
238 return a;
239 }
240 return 0;
241}
242
243void KLanguageButton::setCurrentItem( const QString & languageCode )
244{
245 if (!d->ids.count())
246 return;
247 QAction *a;
248 if (d->ids.indexOf(languageCode) < 0)
249 a = d->findAction(d->ids[0]);
250 else
251 a = d->findAction(languageCode);
252 if (a)
253 d->setCurrentItem(a);
254}
255
256void KLanguageButtonPrivate::setCurrentItem( QAction *a )
257{
258 if (!a->data().isValid())
259 return;
260 current = a->data().toString();
261
262 if ( !staticText ) {
263 button->setText( a->text() );
264 }
265}
KConfigGroup
KConfig
KConfig::SimpleConfig
SimpleConfig
KLanguageButton
KLanguageButton is a pushbutton which allows a language to be selected from a popup list.
Definition: klanguagebutton.h:47
KLanguageButton::KLanguageButton
KLanguageButton(QWidget *parent=0)
Constructs a button whose text is determined by the current language in the popup list.
Definition: klanguagebutton.cpp:76
KLanguageButton::count
int count() const
Returns the number of items in the combo box.
Definition: klanguagebutton.cpp:204
KLanguageButton::current
QString current() const
Returns the language code of the combobox's current item.
Definition: klanguagebutton.cpp:229
KLanguageButton::insertSeparator
void insertSeparator(int index=-1)
Inserts a separator item into the combo box.
Definition: klanguagebutton.cpp:158
KLanguageButton::highlighted
void highlighted(const QString &languageCode)
This signal is emitted when a new item is highlighted.
KLanguageButton::~KLanguageButton
virtual ~KLanguageButton()
Deconstructor.
Definition: klanguagebutton.cpp:109
KLanguageButton::clear
void clear()
Removes all combobox items.
Definition: klanguagebutton.cpp:209
KLanguageButton::insertLanguage
void insertLanguage(const QString &languageCode, const QString &name=QString(), int index=-1)
Inserts a language into the combo box.
Definition: klanguagebutton.cpp:130
KLanguageButton::setCurrentItem
void setCurrentItem(const QString &languageCode)
Sets a given language to be the current item.
Definition: klanguagebutton.cpp:243
KLanguageButton::showLanguageCodes
void showLanguageCodes(bool show)
Specifies whether language codes should be shown alongside language names in the popup.
Definition: klanguagebutton.cpp:125
KLanguageButton::loadAllLanguages
void loadAllLanguages()
Load all known languages into the popup list.
Definition: klanguagebutton.cpp:166
KLanguageButton::setText
void setText(const QString &text)
Sets a static button text.
Definition: klanguagebutton.cpp:114
KLanguageButton::contains
bool contains(const QString &languageCode) const
Checks whether the specified language is in the popup list.
Definition: klanguagebutton.cpp:224
KLanguageButton::setLocale
void setLocale(const KLocale *locale)
Sets the locale to display language names.
Definition: klanguagebutton.cpp:120
KLocale
KLocale::languageCodeToName
QString languageCodeToName(const QString &language) const
KLocale::defaultLanguage
static QString defaultLanguage()
KLocale::language
QString language() const
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter, SearchOptions options, QStringList &relPaths) const
QAction
QList
QMenu
QPushButton
QWidget
kconfiggroup.h
kdebug.h
checkInsertPos
static void checkInsertPos(QMenu *popup, const QString &str, int &index)
Definition: klanguagebutton.cpp:33
klocale.h
i18n
QString i18n(const char *text)
kstandarddirs.h
KGlobal::dirs
KStandardDirs * dirs()
locale
KLocale * locale()
group
group
KStandardAction::clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Clear the content of the focus widget.
Definition: kstandardaction.cpp:314
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal