libyui-qt
2.43.5
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YQComboBox.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 00026 #define SEND_SELECTION_CHANGED_EVENT 0 00027 00028 #include <qstring.h> 00029 #include <qlabel.h> 00030 #include <qcombobox.h> 00031 #include <qlineedit.h> 00032 #include <qpixmap.h> 00033 #define YUILogComponent "qt-ui" 00034 #include <yui/YUILog.h> 00035 00036 #include "utf8.h" 00037 #include "YQUI.h" 00038 #include <yui/YEvent.h> 00039 #include "QY2CharValidator.h" 00040 #include "YQComboBox.h" 00041 #include "YQSignalBlocker.h" 00042 #include "YQWidgetCaption.h" 00043 #include <QVBoxLayout> 00044 #include <QDebug> 00045 00046 YQComboBox::YQComboBox( YWidget * parent, 00047 const std::string & label, 00048 bool editable ) 00049 : QFrame( (QWidget *) parent->widgetRep() ) 00050 , YComboBox( parent, label, editable ) 00051 , _validator(0) 00052 { 00053 QVBoxLayout* layout = new QVBoxLayout( this ); 00054 setLayout( layout ); 00055 00056 setWidgetRep( this ); 00057 layout->setSpacing( YQWidgetSpacing ); 00058 layout->setMargin ( YQWidgetMargin ); 00059 00060 _caption = new YQWidgetCaption( this, label ); 00061 YUI_CHECK_NEW( _caption ); 00062 layout->addWidget( _caption ); 00063 00064 _qt_comboBox = new QComboBox(this); 00065 _qt_comboBox->setEditable(editable); 00066 YUI_CHECK_NEW( _caption ); 00067 layout->addWidget( _qt_comboBox ); 00068 00069 _caption->setBuddy( _qt_comboBox ); 00070 00071 #if SEND_SELECTION_CHANGED_EVENT 00072 connect( _qt_comboBox, SIGNAL( highlighted (int) ), 00073 this, SLOT ( slotSelected(int) ) ); 00074 #endif 00075 00076 connect( _qt_comboBox, SIGNAL( activated ( QString ) ), 00077 this, SLOT ( textChanged( QString ) ) ); 00078 00079 connect( _qt_comboBox, SIGNAL( editTextChanged( QString ) ), 00080 this, SLOT ( textChanged( QString ) ) ); 00081 } 00082 00083 00084 YQComboBox::~YQComboBox() 00085 { 00086 // NOP 00087 } 00088 00089 00090 string YQComboBox::text() 00091 { 00092 return toUTF8( _qt_comboBox->currentText() ); 00093 } 00094 00095 00096 void YQComboBox::setText( const std::string & newValue ) 00097 { 00098 QString text = fromUTF8( newValue ); 00099 00100 if ( isValidText( text ) ) 00101 { 00102 YQSignalBlocker sigBlocker( _qt_comboBox ); 00103 int index = _qt_comboBox->findText( text ); 00104 if ( index < 0 ) 00105 _qt_comboBox->setEditText( text ); 00106 else { 00107 _qt_comboBox->setCurrentIndex( index ); 00108 _qt_comboBox->setItemText(index, text ); 00109 } 00110 } 00111 else 00112 { 00113 yuiError() << this << ": Rejecting invalid value \"" << newValue << "\"" << std::endl; 00114 } 00115 } 00116 00117 00118 void YQComboBox::addItem( YItem * item ) 00119 { 00120 YComboBox::addItem( item ); 00121 QIcon icon; 00122 00123 if ( item->hasIconName() ) 00124 { 00125 string iconName = iconFullPath( item ); 00126 icon = QIcon( iconName.c_str() ); 00127 00128 if ( icon.isNull() ) 00129 yuiWarning() << "Can't load icon \"" << iconName << "\"" << std::endl; 00130 } 00131 00132 if ( icon.isNull() ) 00133 _qt_comboBox->addItem( fromUTF8( item->label() ) ); 00134 else 00135 _qt_comboBox->addItem( icon, fromUTF8( item->label() ) ); 00136 00137 if ( item->selected() ) 00138 { 00139 YQSignalBlocker sigBlocker( _qt_comboBox ); 00140 setText( item->label() ); 00141 } 00142 } 00143 00144 00145 void YQComboBox::deleteAllItems() 00146 { 00147 YQSignalBlocker sigBlocker( _qt_comboBox ); 00148 00149 _qt_comboBox->clear(); 00150 YComboBox::deleteAllItems(); 00151 } 00152 00153 00154 void YQComboBox::setLabel( const std::string & label ) 00155 { 00156 _caption->setText( label ); 00157 YComboBox::setLabel( label ); 00158 } 00159 00160 00161 void YQComboBox::setValidChars( const std::string & newValidChars ) 00162 { 00163 if ( ! _qt_comboBox->isEditable() ) 00164 { 00165 yuiWarning() << this << ": Setting ValidChars is useless on a combo box that isn't editable!" << std::endl; 00166 return; 00167 } 00168 00169 if ( _validator ) 00170 { 00171 _validator->setValidChars( fromUTF8( newValidChars ) ); 00172 } 00173 else 00174 { 00175 _validator = new QY2CharValidator( fromUTF8( newValidChars ), this ); 00176 _qt_comboBox->setValidator( _validator ); 00177 00178 // No need to delete the validator in the destructor - Qt will take 00179 // care of that since it's a QObject with a parent! 00180 } 00181 00182 if ( ! isValidText( _qt_comboBox->currentText() ) ) 00183 { 00184 yuiError() << this << ": Old value \"" << _qt_comboBox->currentText() 00185 << " \" invalid according to new ValidChars \""<< newValidChars << "\" - deleting" 00186 << std::endl; 00187 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(), ""); 00188 } 00189 00190 YComboBox::setValidChars( newValidChars ); 00191 } 00192 00193 00194 bool YQComboBox::isValidText( const QString & txt ) const 00195 { 00196 if ( ! _validator ) 00197 return true; 00198 00199 int pos = 0; 00200 QString text( txt ); // need a non-const QString & 00201 00202 return _validator->validate( text, pos ) == QValidator::Acceptable; 00203 } 00204 00205 00206 void YQComboBox::slotSelected( int i ) 00207 { 00208 if ( notify() ) 00209 { 00210 if ( ! YQUI::ui()->eventPendingFor( this ) ) 00211 { 00212 // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event 00213 00214 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) ); 00215 } 00216 } 00217 } 00218 00219 00220 void YQComboBox::textChanged( QString ) 00221 { 00222 if ( notify() ) 00223 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) ); 00224 } 00225 00226 00227 void YQComboBox::setInputMaxLength( int len ) 00228 { 00229 _qt_comboBox->lineEdit()->setMaxLength( len ); 00230 YComboBox::setInputMaxLength( len ); 00231 } 00232 00233 00234 int YQComboBox::preferredWidth() 00235 { 00236 return sizeHint().width(); 00237 } 00238 00239 00240 int YQComboBox::preferredHeight() 00241 { 00242 return sizeHint().height(); 00243 } 00244 00245 00246 void YQComboBox::setSize( int newWidth, int newHeight ) 00247 { 00248 resize( newWidth, newHeight ); 00249 } 00250 00251 00252 void YQComboBox::setEnabled( bool enabled ) 00253 { 00254 _caption->setEnabled( enabled ); 00255 _qt_comboBox->setEnabled( enabled ); 00256 YWidget::setEnabled( enabled ); 00257 } 00258 00259 00260 bool YQComboBox::setKeyboardFocus() 00261 { 00262 _qt_comboBox->setFocus(); 00263 00264 return true; 00265 } 00266 00267 00268 #include "YQComboBox.moc"