00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#define INCLUDE_MENUITEM_DEF
00025
#include <qpopupmenu.h>
00026
#include <qlayout.h>
00027
#include <qpushbutton.h>
00028
00029
#include "klanguagebutton.h"
00030
#include "klanguagebutton.moc"
00031
00032
#include <kdebug.h>
00033
00034
static void checkInsertPos(
QPopupMenu *popup,
const QString & str,
00035
int &index )
00036 {
00037
if ( index == -1 )
00038
return;
00039
00040
int a = 0;
00041
int b = popup->count();
00042
while ( a < b )
00043 {
00044
int w = ( a + b ) / 2;
00045
00046
int id = popup->idAt( w );
00047
int j = str.localeAwareCompare( popup->text(
id ) );
00048
00049
if ( j > 0 )
00050 a = w + 1;
00051
else
00052 b = w;
00053 }
00054
00055 index = a;
00056
00057 Q_ASSERT( a == b );
00058 }
00059
00060
static QPopupMenu * checkInsertIndex(
QPopupMenu *popup,
00061
const QStringList *tags,
const QString &submenu )
00062 {
00063
int pos = tags->findIndex( submenu );
00064
00065
QPopupMenu *pi = 0;
00066
if ( pos != -1 )
00067 {
00068 QMenuItem *p = popup->findItem( pos );
00069 pi = p ? p->popup() : 0;
00070 }
00071
if ( !pi )
00072 pi = popup;
00073
00074
return pi;
00075 }
00076
00077
class KLanguageButtonPrivate
00078 {
00079
public:
00080
QPushButton * button;
00081
bool staticText;
00082 };
00083
00084 KLanguageButton::KLanguageButton(
QWidget * parent,
const char *name )
00085 :
QWidget( parent, name )
00086 {
00087 init(name);
00088 }
00089
00090 KLanguageButton::KLanguageButton(
const QString & text,
QWidget * parent,
const char *name )
00091 :
QWidget( parent, name )
00092 {
00093 init(name);
00094
00095
setText(text);
00096 }
00097
00098 void KLanguageButton::setText(
const QString & text)
00099 {
00100 d->staticText =
true;
00101 d->button->setText(text);
00102 d->button->setIconSet(
QIconSet());
00103 }
00104
00105
void KLanguageButton::init(
const char * name)
00106 {
00107 m_ids =
new QStringList;
00108 m_popup = 0;
00109 m_oldPopup = 0;
00110 d =
new KLanguageButtonPrivate;
00111
00112 d->staticText =
false;
00113
00114
QHBoxLayout *layout =
new QHBoxLayout(
this, 0, 0);
00115 layout->setAutoAdd(
true);
00116 d->button =
new QPushButton(
this, name );
00117
00118
clear();
00119 }
00120
00121 KLanguageButton::~KLanguageButton()
00122 {
00123
delete m_ids;
00124
00125
delete d->button;
00126
delete d;
00127 }
00128
00129
00130
void KLanguageButton::insertLanguage(
const QString& path,
const QString& name,
00131
const QString&,
const QString &submenu,
int index )
00132 {
00133
QString output = name + QString::fromLatin1(
" (" ) + path +
00134 QString::fromLatin1(
")" );
00135
#if 0
00136
00137
QPixmap flag(
locate(
"locale", sub + path +
00138 QString::fromLatin1(
"/flag.png" ) ) );
00139
#endif
00140
insertItem( output, path, submenu, index );
00141 }
00142
00143 void KLanguageButton::insertItem(
const QIconSet& icon,
const QString &text,
00144
const QString &
id,
const QString &submenu,
int index )
00145 {
00146
QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00147 checkInsertPos( pi, text, index );
00148 pi->insertItem( icon, text, count(), index );
00149 m_ids->append(
id );
00150 }
00151
00152 void KLanguageButton::insertItem(
const QString &text,
const QString &
id,
00153
const QString &submenu,
int index )
00154 {
00155
insertItem(
QIconSet(), text,
id, submenu, index );
00156 }
00157
00158 void KLanguageButton::insertSeparator(
const QString &submenu,
int index )
00159 {
00160
QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00161 pi->insertSeparator( index );
00162 m_ids->append( QString::null );
00163 }
00164
00165 void KLanguageButton::insertSubmenu(
const QIconSet & icon,
00166
const QString &text,
const QString &
id,
00167
const QString &submenu,
int index )
00168 {
00169
QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00170
QPopupMenu *p =
new QPopupMenu( pi );
00171 checkInsertPos( pi, text, index );
00172 pi->insertItem( icon, text, p, count(), index );
00173 m_ids->append(
id );
00174 connect( p, SIGNAL(
activated(
int ) ),
00175 SLOT( slotActivated(
int ) ) );
00176 connect( p, SIGNAL( highlighted(
int ) ),
this,
00177 SLOT( slotHighlighted(
int ) ) );
00178 }
00179
00180 void KLanguageButton::insertSubmenu(
const QString &text,
const QString &
id,
00181
const QString &submenu,
int index )
00182 {
00183
insertSubmenu(
QIconSet(), text,
id, submenu, index);
00184 }
00185
00186
void KLanguageButton::slotActivated(
int index )
00187 {
00188
00189
00190
setCurrentItem( index );
00191
00192
00193
QString id = *m_ids->at( index );
00194 emit
activated(
id );
00195 }
00196
00197
void KLanguageButton::slotHighlighted(
int index )
00198 {
00199
00200
00201
QString id = *m_ids->at( index );
00202 emit ( highlighted(
id) );
00203 }
00204
00205
int KLanguageButton::count()
const
00206
{
00207
return m_ids->count();
00208 }
00209
00210 void KLanguageButton::clear()
00211 {
00212 m_ids->clear();
00213
00214
delete m_oldPopup;
00215 m_oldPopup = m_popup;
00216 m_popup =
new QPopupMenu(
this );
00217
00218 d->button->setPopup( m_popup );
00219
00220 connect( m_popup, SIGNAL(
activated(
int ) ),
00221 SLOT( slotActivated(
int ) ) );
00222 connect( m_popup, SIGNAL( highlighted(
int ) ),
00223 SLOT( slotHighlighted(
int ) ) );
00224
00225
if ( !d->staticText )
00226 {
00227 d->button->setText( QString::null );
00228 d->button->setIconSet(
QIconSet() );
00229 }
00230 }
00231
00232 bool KLanguageButton::contains(
const QString &
id )
const
00233
{
00234
return m_ids->contains(
id ) > 0;
00235 }
00236
00237 QString KLanguageButton::current()
const
00238
{
00239
return *m_ids->at( currentItem() );
00240 }
00241
00242
00243
QString KLanguageButton::id(
int i )
const
00244
{
00245
if ( i < 0 || i >= count() )
00246 {
00247
kdDebug() <<
"KLanguageButton::tag(), unknown tag " << i <<
endl;
00248
return QString::null;
00249 }
00250
return *m_ids->at( i );
00251 }
00252
00253
00254
int KLanguageButton::currentItem()
const
00255
{
00256
return m_current;
00257 }
00258
00259
void KLanguageButton::setCurrentItem(
int i )
00260 {
00261
if ( i < 0 || i >= count() )
00262
return;
00263 m_current = i;
00264
00265
if ( !d->staticText )
00266 {
00267 d->button->setText( m_popup->text( m_current ) );
00268
QIconSet *icon = m_popup->iconSet( m_current );
00269
if ( icon )
00270 d->button->setIconSet( *icon );
00271
else
00272 d->button->setIconSet(
QIconSet() );
00273 }
00274 }
00275
00276 void KLanguageButton::setCurrentItem(
const QString &
id )
00277 {
00278
int i = m_ids->findIndex(
id );
00279
if (
id.isNull() )
00280 i = 0;
00281
if ( i != -1 )
00282
setCurrentItem( i );
00283 }