kdeui Library API Documentation

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_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 ); // HPB don't touch this!! 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 // Nooooo ! Country != language 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 //kdDebug() << "slotActivated" << index << endl; 00189 00190 setCurrentItem( index ); 00191 00192 // Forward event from popup menu as if it was emitted from this widget: 00193 QString id = *m_ids->at( index ); 00194 emit activated( id ); 00195 } 00196 00197 void KLanguageButton::slotHighlighted( int index ) 00198 { 00199 //kdDebug() << "slotHighlighted" << index << endl; 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 }
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Sep 29 09:43:28 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003