KDEUI
klanguagebutton.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
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
00189 if (!a)
00190 return;
00191
00192 d->setCurrentItem( a );
00193
00194
00195 emit activated( d->current );
00196 }
00197
00198 void KLanguageButton::slotHovered( QAction *a )
00199 {
00200
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 }