| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# klanguagebutton.cpp

    00001 /*
    00002  * klanguagebutton.cpp - Adds some methods for inserting languages.
    00003  *
    00004  * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
    00005  *
    00006  * Requires the Qt widget libraries, available at no cost at
    00007  * http://www.trolltech.com/
    00008  *
    00009  *  This program is free software; you can redistribute it and/or modify
    00010  *  it under the terms of the GNU General Public License as published by
    00011  *  the Free Software Foundation; either version 2 of the License, or
    00012  *  (at your option) any later version.
    00013  *
    00014  *  This program is distributed in the hope that it will be useful,
    00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  *  GNU General Public License for more details.
    00018  *
    00019  *  You should have received a copy of the GNU General Public License
    00020  *  along with this program; if not, write to the Free Software
    00021  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    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; // it doesn't really matter ... a == b here.
    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()); // remove the icon
    00103 }
    00104
    00105 void KLanguageButton::init(const char * name)
    00106 {
    00107   m_current = 0;
    00108   m_ids = new QStringList;
    00109   m_popup = 0;
    00110   m_oldPopup = 0;
    00111   d = new KLanguageButtonPrivate;
    00112
    00113   d->staticText = false;
    00114
    00115   QHBoxLayout *layout = new QHBoxLayout(this, 0, 0);
    00116   layout->setAutoAdd(true);
    00117   d->button = new QPushButton( this, name ); // HPB don't touch this!!
    00118
    00119   clear();
    00120 }
    00121
    00122 KLanguageButton::~KLanguageButton()
    00123 {
    00124   delete m_ids;
    00125
    00126   delete d->button;
    00127   delete d;
    00128 }
    00129
    00130
    00131 void KLanguageButton::insertLanguage( const QString& path, const QString& name,
    00132                         const QString&, const QString &submenu, int index )
    00133 {
    00134   QString output = name + QString::fromLatin1( " (" ) + path +
    00135                    QString::fromLatin1( ")" );
    00136 #if 0
    00137   // Nooooo ! Country != language
    00138   QPixmap flag( locate( "locale", sub + path +
    00139                 QString::fromLatin1( "/flag.png" ) ) );
    00140 #endif
    00141   insertItem( output, path, submenu, index );
    00142 }
    00143
    00144 void KLanguageButton::insertItem( const QIconSet& icon, const QString &text,
    00145                                   const QString & id, const QString &submenu, int index )
    00146 {
    00147   QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
    00148   checkInsertPos( pi, text, index );
    00149   pi->insertItem( icon, text, count(), index );
    00150   m_ids->append( id );
    00151 }
    00152
    00153 void KLanguageButton::insertItem( const QString &text, const QString & id,
    00154                                   const QString &submenu, int index )
    00155 {
    00156   insertItem( QIconSet(), text, id, submenu, index );
    00157 }
    00158
    00159 void KLanguageButton::insertSeparator( const QString &submenu, int index )
    00160 {
    00161   QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
    00162   pi->insertSeparator( index );
    00163   m_ids->append( QString::null );
    00164 }
    00165
    00166 void KLanguageButton::insertSubmenu( const QIconSet & icon,
    00167                                      const QString &text, const QString &id,
    00168                                      const QString &submenu, int index )
    00169 {
    00170   QPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
    00171   QPopupMenu *p = new QPopupMenu( pi );
    00172   checkInsertPos( pi, text, index );
    00173   pi->insertItem( icon, text, p, count(), index );
    00174   m_ids->append( id );
    00175   connect( p, SIGNAL( activated( int ) ),
    00176            SLOT( slotActivated( int ) ) );
    00177   connect( p, SIGNAL( highlighted( int ) ), this,
    00178            SLOT( slotHighlighted( int ) ) );
    00179 }
    00180
    00181 void KLanguageButton::insertSubmenu( const QString &text, const QString &id,
    00182                                      const QString &submenu, int index )
    00183 {
    00184   insertSubmenu(QIconSet(), text, id, submenu, index);
    00185 }
    00186
    00187 void KLanguageButton::slotActivated( int index )
    00188 {
    00189   //kdDebug() << "slotActivated" << index << endl;
    00190
    00191   setCurrentItem( index );
    00192
    00193   // Forward event from popup menu as if it was emitted from this widget:
    00194   QString id = *m_ids->at( index );
    00195   emit activated( id );
    00196 }
    00197
    00198 void KLanguageButton::slotHighlighted( int index )
    00199 {
    00200   //kdDebug() << "slotHighlighted" << index << endl;
    00201
    00202   QString id = *m_ids->at( index );
    00203   emit ( highlighted(id) );
    00204 }
    00205
    00206 int KLanguageButton::count() const
    00207 {
    00208   return m_ids->count();
    00209 }
    00210
    00211 void KLanguageButton::clear()
    00212 {
    00213   m_ids->clear();
    00214
    00215   delete m_oldPopup;
    00216   m_oldPopup = m_popup;
    00217   m_popup = new QPopupMenu( this );
    00218
    00219   d->button->setPopup( m_popup );
    00220
    00221   connect( m_popup, SIGNAL( activated( int ) ),
    00222            SLOT( slotActivated( int ) ) );
    00223   connect( m_popup, SIGNAL( highlighted( int ) ),
    00224            SLOT( slotHighlighted( int ) ) );
    00225
    00226   if ( !d->staticText )
    00227   {
    00228     d->button->setText( QString::null );
    00229     d->button->setIconSet( QIconSet() );
    00230   }
    00231 }
    00232
    00233 bool KLanguageButton::contains( const QString & id ) const
    00234 {
    00235   return m_ids->contains( id ) > 0;
    00236 }
    00237
    00238 QString KLanguageButton::current() const
    00239 {
    00240   return *m_ids->at( currentItem() );
    00241 }
    00242
    00243
    00244 QString KLanguageButton::id( int i ) const
    00245 {
    00246   if ( i < 0 || i >= count() )
    00247   {
    00248     kdDebug() << "KLanguageButton::tag(), unknown tag " << i << endl;
    00249     return QString::null;
    00250   }
    00251   return *m_ids->at( i );
    00252 }
    00253
    00254
    00255 int KLanguageButton::currentItem() const
    00256 {
    00257   return m_current;
    00258 }
    00259
    00260 void KLanguageButton::setCurrentItem( int i )
    00261 {
    00262   if ( i < 0 || i >= count() )
    00263     return;
    00264   m_current = i;
    00265
    00266   if ( !d->staticText )
    00267   {
    00268     d->button->setText( m_popup->text( m_current ) );
    00269     QIconSet *icon = m_popup->iconSet( m_current );
    00270     if ( icon )
    00271       d->button->setIconSet( *icon );
    00272     else
    00273       d->button->setIconSet( QIconSet() );
    00274   }
    00275 }
    00276
    00277 void KLanguageButton::setCurrentItem( const QString & id )
    00278 {
    00279   int i = m_ids->findIndex( id );
    00280   if ( id.isNull() )
    00281     i = 0;
    00282   if ( i != -1 )
    00283     setCurrentItem( i );
    00284 }

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:18 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003