semieditablecombobox.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2006 by Martin Mueller * 00003 * orvio@orvio.de * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 #include "semieditablecombobox.h" 00021 #include "semieditablecombobox.moc" 00022 00023 #include <QShowEvent> 00024 00025 class Blokkal::Ui::SemiEditableComboBox::Private 00026 { 00027 public: 00031 Private( void ) {} 00032 }; 00033 00034 Blokkal::Ui::SemiEditableComboBox::SemiEditableComboBox( QWidget * parent ): 00035 KComboBox( parent ), 00036 d( new Private() ) 00037 { 00038 setInsertPolicy( KComboBox::NoInsert ); 00039 connect( this, SIGNAL( activated( int ) ), 00040 this, SLOT( updateEditableState( int ) ) ); 00041 00042 connect( this, SIGNAL( editTextChanged ( const QString & ) ), 00043 this, SLOT( setCustomInfo( const QString & ) ) ); 00044 } 00045 00046 Blokkal::Ui::SemiEditableComboBox::~SemiEditableComboBox( void ) 00047 { 00048 delete d; 00049 } 00050 00051 void Blokkal::Ui::SemiEditableComboBox::setItems( QStringList items ) 00052 { 00053 QString oldText = currentText(); 00054 clear(); 00055 addItem( "" ); 00056 addItems( items ); 00057 setCurrentText( oldText ); 00058 } 00059 00060 void Blokkal::Ui::SemiEditableComboBox::setCurrentIndex( int index ) 00061 { 00062 KComboBox::setCurrentIndex( index ); 00063 updateEditableState( ); 00064 } 00065 00066 void Blokkal::Ui::SemiEditableComboBox::setCurrentText( const QString & _itemText ) 00067 { 00068 if( !_itemText.isNull() ) { 00069 int i; 00070 for( i = 0; i < count(); i++ ) { 00071 if( itemText( i ) == _itemText ) { 00072 setCurrentIndex( i ); 00073 break; 00074 } 00075 } 00076 if( i == count() ) { 00077 if( !count() ) { 00078 addItem( _itemText ); 00079 } 00080 setCurrentIndex( 0 ); 00081 setItemText( 0, _itemText ); 00082 } 00083 updateEditableState( ); 00084 } 00085 } 00086 00087 void Blokkal::Ui::SemiEditableComboBox::showEvent( QShowEvent * event ) 00088 { 00089 if( event && !event->spontaneous() ) { 00090 updateEditableState( ); 00091 } 00092 } 00093 00094 void Blokkal::Ui::SemiEditableComboBox::updateEditableState( int index ) 00095 { 00096 //box is empty 00097 if( currentIndex() < 0 ) { 00098 setEditable( TRUE ); 00099 return; 00100 } 00101 00102 if( index < 0 ) { 00103 updateEditableState( currentIndex() ); 00104 } 00105 else { 00106 setEditable( index == 0); 00107 } 00108 } 00109 00110 void Blokkal::Ui::SemiEditableComboBox::setCustomInfo( const QString & text ) 00111 { 00112 if( currentIndex() != 0 ) { 00113 return; 00114 } 00115 00116 setItemText( 0, text ); 00117 }