21 #include "klanguagebutton.moc"
23 #include <QtGui/QMenu>
24 #include <QtGui/QLayout>
25 #include <QtGui/QPushButton>
26 #include <QtGui/QMenuItem>
39 const QList<QAction*> actions = popup->actions();
40 int b = actions.count();
44 int w = ( a + b ) / 2;
46 int j = str.localeAwareCompare( ac->text() );
58 class KLanguageButtonPrivate
62 ~KLanguageButtonPrivate() {
delete button;
delete popup; }
63 void setCurrentItem(
QAction* );
78 d( new KLanguageButtonPrivate(this) )
84 d( new KLanguageButtonPrivate(this) )
89 KLanguageButtonPrivate::KLanguageButtonPrivate(
KLanguageButton *parent )
91 popup(new
QMenu(parent)),
96 QHBoxLayout *layout =
new QHBoxLayout( parent );
98 layout->addWidget( button );
100 parent->setFocusProxy( button );
101 parent->setFocusPolicy( button->focusPolicy() );
103 button->setMenu( popup );
105 QObject::connect( popup, SIGNAL(triggered(
QAction*)), parent, SLOT(slotTriggered(
QAction*)) );
106 QObject::connect( popup, SIGNAL(hovered(
QAction*)), parent, SLOT(slotHovered(
QAction*)) );
116 d->staticText =
true;
117 d->button->setText(text);
133 bool showCodes = d->showCodes;
146 text += QLatin1String(
" (" ) + languageCode + QLatin1Char(
')');
150 a->setData(languageCode);
151 if ( index >= 0 && index < d->popup->actions().count()-1)
152 d->popup->insertAction(d->popup->actions()[index], a);
154 d->popup->addAction(a);
155 d->ids.append(languageCode);
160 if ( index >= 0 && index < d->popup->actions().count()-1)
161 d->popup->insertSeparator(d->popup->actions()[index]);
163 d->popup->addSeparator();
169 QString::fromLatin1(
"*/entry.desktop"));
171 for (
int i = 0,
count = langlist.count(); i <
count; ++i)
173 QString fpath = langlist[i].left(langlist[i].length() - 14);
174 QString code = fpath.mid(fpath.lastIndexOf(
'/') + 1);
185 void KLanguageButton::slotTriggered(
QAction *a )
191 d->setCurrentItem( a );
197 void KLanguageButton::slotHovered(
QAction *a )
206 return d->ids.count();
226 return d->ids.contains( languageCode );
231 return d->current.isEmpty() ? QLatin1String(
"en") : d->current;
234 QAction *KLanguageButtonPrivate::findAction(
const QString& data)
const
236 foreach(
QAction *a, popup->actions()) {
237 if (!a->data().toString().compare(data))
248 if (d->ids.indexOf(languageCode) < 0)
249 a = d->findAction(d->ids[0]);
251 a = d->findAction(languageCode);
253 d->setCurrentItem(a);
256 void KLanguageButtonPrivate::setCurrentItem(
QAction *a )
258 if (!a->data().isValid())
260 current = a->data().toString();
263 button->setText( a->text() );