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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
33 static 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 
58 class KLanguageButtonPrivate
59 {
60 public:
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 
76 KLanguageButton::KLanguageButton( QWidget * parent )
77  : QWidget( parent ),
78  d( new KLanguageButtonPrivate(this) )
79 {
80 }
81 
82 KLanguageButton::KLanguageButton( const QString & text, QWidget * parent )
83  : QWidget( parent ),
84  d( new KLanguageButtonPrivate(this) )
85 {
86  setText(text);
87 }
88 
89 KLanguageButtonPrivate::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 
109 KLanguageButton::~KLanguageButton()
110 {
111  delete d;
112 }
113 
114 void KLanguageButton::setText(const QString & text)
115 {
116  d->staticText = true;
117  d->button->setText(text);
118 }
119 
120 void KLanguageButton::setLocale( const KLocale *locale )
121 {
122  d->locale = locale;
123 }
124 
125 void KLanguageButton::showLanguageCodes( bool show )
126 {
127  d->showCodes = show;
128 }
129 
130 void 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 
158 void 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 
166 void 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 
185 void 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 
197 void KLanguageButton::slotHovered( QAction *a )
198 {
199  //kDebug() << "slotHovered" << index;
200 
201  emit highlighted(a->data().toString());
202 }
203 
204 int KLanguageButton::count() const
205 {
206  return d->ids.count();
207 }
208 
209 void KLanguageButton::clear()
210 {
211  d->clear();
212 }
213 
214 void KLanguageButtonPrivate::clear()
215 {
216  ids.clear();
217  popup->clear();
218 
219  if ( !staticText ) {
220  button->setText( QString() );
221  }
222 }
223 
224 bool KLanguageButton::contains( const QString &languageCode ) const
225 {
226  return d->ids.contains( languageCode );
227 }
228 
229 QString KLanguageButton::current() const
230 {
231  return d->current.isEmpty() ? QLatin1String("en") : d->current;
232 }
233 
234 QAction *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 
243 void 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 
256 void 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 }
QWidget::layout
QLayout * layout() const
QAction::text
text
i18n
QString i18n(const char *text)
QWidget
kdebug.h
QString::localeAwareCompare
int localeAwareCompare(const QString &other) const
group
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
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
QAction::data
QVariant data() const
KLanguageButton::loadAllLanguages
void loadAllLanguages()
Load all known languages into the popup list.
Definition: klanguagebutton.cpp:166
KGlobal::dirs
KStandardDirs * dirs()
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
QHBoxLayout
KConfig::SimpleConfig
KLanguageButton::setLocale
void setLocale(const KLocale *locale)
Sets the locale to display language names.
Definition: klanguagebutton.cpp:120
KLanguageButton::setCurrentItem
void setCurrentItem(const QString &languageCode)
Sets a given language to be the current item.
Definition: klanguagebutton.cpp:243
klocale.h
KLocale::languageCodeToName
QString languageCodeToName(const QString &language) const
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
KLanguageButton::highlighted
void highlighted(const QString &languageCode)
This signal is emitted when a new item is highlighted.
QObject::name
const char * name() const
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
KLanguageButton::current
QString current() const
Returns the language code of the combobox's current item.
Definition: klanguagebutton.cpp:229
KLanguageButton::clear
void clear()
Removes all combobox items.
Definition: klanguagebutton.cpp:209
QWidget::setFocusProxy
void setFocusProxy(QWidget *w)
QString::isEmpty
bool isEmpty() const
KLanguageButton::~KLanguageButton
virtual ~KLanguageButton()
Deconstructor.
Definition: klanguagebutton.cpp:109
KStandardAction::clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Clear the content of the focus widget.
Definition: kstandardaction.cpp:314
KLocale::language
QString language() const
QString
QList< QAction * >
QLayout::setMargin
void setMargin(int margin)
KLanguageButton::setText
void setText(const QString &text)
Sets a static button text.
Definition: klanguagebutton.cpp:114
QStringList
QWidget::locale
QLocale locale() const
KLanguageButton::contains
bool contains(const QString &languageCode) const
Checks whether the specified language is in the popup list.
Definition: klanguagebutton.cpp:224
QAction::setData
void setData(const QVariant &userData)
QMenu
QLatin1Char
KLanguageButton::activated
void activated(const QString &languageCode)
This signal is emitted when a new item is activated.
KLanguageButton::insertSeparator
void insertSeparator(int index=-1)
Inserts a separator item into the combo box.
Definition: klanguagebutton.cpp:158
locale
KLocale * locale()
KConfigGroup
KConfig
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
QString::mid
QString mid(int position, int n) const
KLocale
QLatin1String
kstandarddirs.h
QAction
checkInsertPos
static void checkInsertPos(QMenu *popup, const QString &str, int &index)
Definition: klanguagebutton.cpp:33
KLanguageButton::count
int count() const
Returns the number of items in the combo box.
Definition: klanguagebutton.cpp:204
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QVariant::isValid
bool isValid() const
QStringList::sort
void sort()
QPushButton
QWidget::show
void show()
KLanguageButton::showLanguageCodes
void showLanguageCodes(bool show)
Specifies whether language codes should be shown alongside language names in the popup.
Definition: klanguagebutton.cpp:125
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget::actions
QList< QAction * > actions() const
QString::compare
int compare(const QString &other) const
QVariant::toString
QString toString() const
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KLocale::defaultLanguage
static QString defaultLanguage()
kconfiggroup.h
QIcon
KLanguageButton
KLanguageButton is a pushbutton which allows a language to be selected from a popup list...
Definition: klanguagebutton.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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