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

KDEUI

klanguagebutton.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
00003  *           (c) 2007      David Jarvie <software@astrojar.org.uk>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "klanguagebutton.moc"
00022 
00023 #include <QtGui/QMenu>
00024 #include <QtGui/QLayout>
00025 #include <QtGui/QPushButton>
00026 #include <QtGui/QMenuItem>
00027 
00028 #include <klocale.h>
00029 #include <kstandarddirs.h>
00030 #include <kdebug.h>
00031 #include <kconfiggroup.h>
00032 
00033 static void checkInsertPos( QMenu *popup, const QString &str, int &index )
00034 {
00035   if ( index == -1 )
00036     return;
00037 
00038   int a = 0;
00039   const QList<QAction*> actions = popup->actions();
00040   int b = actions.count();
00041 
00042   while ( a < b )
00043   {
00044     int w = ( a + b ) / 2;
00045     QAction *ac = actions[ w ];
00046     int j = str.localeAwareCompare( ac->text() );
00047     if ( j > 0 )
00048       a = w + 1;
00049     else
00050       b = w;
00051   }
00052 
00053   index = a; // it doesn't really matter ... a == b here.
00054 
00055   Q_ASSERT( a == b );
00056 }
00057 
00058 class KLanguageButtonPrivate
00059 {
00060 public:
00061   KLanguageButtonPrivate( KLanguageButton *parent);
00062   ~KLanguageButtonPrivate()  { delete button; delete popup; }
00063   void setCurrentItem( QAction* );
00064   void clear();
00065   QAction *findAction(const QString &data) const;
00066 
00067   QPushButton *button;
00068   QStringList ids;
00069   QMenu *popup;
00070   QString current;
00071   const KLocale *locale;
00072   bool staticText : 1;
00073   bool showCodes : 1;
00074 };
00075 
00076 KLanguageButton::KLanguageButton( QWidget * parent )
00077   : QWidget( parent ),
00078     d( new KLanguageButtonPrivate(this) )
00079 {
00080 }
00081 
00082 KLanguageButton::KLanguageButton( const QString & text, QWidget * parent )
00083   : QWidget( parent ),
00084     d( new KLanguageButtonPrivate(this) )
00085 {
00086   setText(text);
00087 }
00088 
00089 KLanguageButtonPrivate::KLanguageButtonPrivate( KLanguageButton *parent )
00090   : button(new QPushButton(parent)),
00091     popup(new QMenu(parent)),
00092     locale(0),
00093     staticText(false),
00094     showCodes(false)
00095 {
00096   QHBoxLayout *layout = new QHBoxLayout( parent );
00097   layout->setMargin(0);
00098   layout->setSpacing(0);
00099   layout->addWidget( button );
00100 
00101   parent->setFocusProxy( button );
00102   parent->setFocusPolicy( button->focusPolicy() );
00103 
00104   button->setMenu( popup );
00105 
00106   QObject::connect( popup, SIGNAL(triggered(QAction*)), parent, SLOT(slotTriggered(QAction*)) );
00107   QObject::connect( popup, SIGNAL(hovered(QAction*)), parent, SLOT(slotHovered(QAction*)) );
00108 }
00109 
00110 KLanguageButton::~KLanguageButton()
00111 {
00112   delete d;
00113 }
00114 
00115 void KLanguageButton::setText(const QString & text)
00116 {
00117   d->staticText = true;
00118   d->button->setText(text);
00119 }
00120 
00121 void KLanguageButton::setLocale( const KLocale *locale )
00122 {
00123   d->locale = locale;
00124 }
00125 
00126 void KLanguageButton::showLanguageCodes( bool show )
00127 {
00128   d->showCodes = show;
00129 }
00130 
00131 void KLanguageButton::insertLanguage( const QString &languageCode, const QString &name, int index )
00132 {
00133   QString text;
00134   bool showCodes = d->showCodes;
00135   if (name.isEmpty())
00136   {
00137     text = languageCode;
00138     const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
00139     if (locale)
00140       text = locale->languageCodeToName(languageCode);
00141     else
00142       showCodes = false;
00143   }
00144   else
00145     text = name;
00146   if (showCodes)
00147     text += QLatin1String( " (" ) + languageCode + QLatin1Char(')');
00148 
00149   checkInsertPos( d->popup, text, index );
00150   QAction *a = new QAction(QIcon(), text, this);
00151   a->setData(languageCode);
00152   if ( index >= 0 && index < d->popup->actions().count()-1)
00153     d->popup->insertAction(a, d->popup->actions()[index]);
00154   else
00155     d->popup->addAction(a);
00156   d->ids.append(languageCode);
00157 }
00158 
00159 void KLanguageButton::insertSeparator( int index )
00160 {
00161   if ( index >= 0 && index < d->popup->actions().count()-1)
00162     d->popup->insertSeparator(d->popup->actions()[index]);
00163   else
00164     d->popup->addSeparator();
00165 }
00166 
00167 void KLanguageButton::loadAllLanguages()
00168 {
00169   QStringList langlist = KGlobal::dirs()->findAllResources("locale",
00170                                   QString::fromLatin1("*/entry.desktop"));
00171   langlist.sort();
00172   for (int i = 0, count = langlist.count(); i < count; ++i)
00173   {
00174     QString fpath = langlist[i].left(langlist[i].length() - 14);
00175     QString code = fpath.mid(fpath.lastIndexOf('/') + 1);
00176     KConfig entry(langlist[i], KConfig::SimpleConfig);
00177     KConfigGroup group(&entry, "KCM Locale");
00178     QString name = group.readEntry("Name", i18n("without name"));
00179     insertLanguage(code, name);
00180   }
00181 
00182   const KLocale *locale = d->locale ? d->locale : KGlobal::locale();
00183   setCurrentItem(locale ? locale->language() : KLocale::defaultLanguage());
00184 }
00185 
00186 void KLanguageButton::slotTriggered( QAction *a )
00187 {
00188   //kDebug() << "slotTriggered" << index;
00189   if (!a)
00190     return;
00191 
00192   d->setCurrentItem( a );
00193 
00194   // Forward event from popup menu as if it was emitted from this widget:
00195   emit activated( d->current );
00196 }
00197 
00198 void KLanguageButton::slotHovered( QAction *a )
00199 {
00200   //kDebug() << "slotHovered" << index;
00201 
00202   emit highlighted(a->data().toString());
00203 }
00204 
00205 int KLanguageButton::count() const
00206 {
00207   return d->ids.count();
00208 }
00209 
00210 void KLanguageButton::clear()
00211 {
00212   d->clear();
00213 }
00214 
00215 void KLanguageButtonPrivate::clear()
00216 {
00217   ids.clear();
00218   popup->clear();
00219 
00220   if ( !staticText ) {
00221     button->setText( QString() );
00222   }
00223 }
00224 
00225 bool KLanguageButton::contains( const QString &languageCode ) const
00226 {
00227   return d->ids.contains( languageCode );
00228 }
00229 
00230 QString KLanguageButton::current() const
00231 {
00232   return d->current.isEmpty() ? QLatin1String("en") : d->current;
00233 }
00234 
00235 QAction *KLanguageButtonPrivate::findAction(const QString& data) const
00236 {
00237   foreach(QAction *a, popup->actions()) {
00238     if (!a->data().toString().compare(data))
00239       return a;
00240   }
00241   return 0;
00242 }
00243 
00244 void KLanguageButton::setCurrentItem( const QString & languageCode )
00245 {
00246   if (!d->ids.count())
00247     return;
00248   QAction *a;
00249   if (d->ids.indexOf(languageCode) < 0)
00250     a = d->findAction(d->ids[0]);
00251   else
00252     a = d->findAction(languageCode);
00253   if (a)
00254     d->setCurrentItem(a);
00255 }
00256 
00257 void KLanguageButtonPrivate::setCurrentItem( QAction *a )
00258 {
00259   if (!a->data().isValid())
00260     return;
00261   current = a->data().toString();
00262 
00263   if ( !staticText ) {
00264     button->setText( a->text() );
00265   }
00266 }

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal