1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
20 #define SEND_SELECTION_CHANGED_EVENT 0
24 #include <qcombobox.h>
25 #include <qlineedit.h>
27 #define y2log_component "qt-ui"
28 #include <ycp/y2log.h>
33 #include "QY2CharValidator.h"
34 #include "YQComboBox.h"
35 #include "YQSignalBlocker.h"
36 #include "YQWidgetCaption.h"
40 YQComboBox::YQComboBox( YWidget * parent,
43 : QVBox( (QWidget *) parent->widgetRep() )
44 , YComboBox( parent, label, editable )
48 setSpacing( YQWidgetSpacing );
49 setMargin ( YQWidgetMargin );
51 _caption = new YQWidgetCaption( this, label );
52 YUI_CHECK_NEW( _caption );
54 _qt_comboBox = new QComboBox( editable, this );
55 YUI_CHECK_NEW( _caption );
57 _caption->setBuddy( _qt_comboBox );
59 #if SEND_SELECTION_CHANGED_EVENT
60 connect( _qt_comboBox, SIGNAL( highlighted (int) ),
61 this, SLOT ( slotSelected(int) ) );
64 connect( _qt_comboBox, SIGNAL( activated ( const QString & ) ),
65 this, SLOT ( textChanged( const QString & ) ) );
67 connect( _qt_comboBox, SIGNAL( textChanged( const QString & ) ),
68 this, SLOT ( textChanged( const QString & ) ) );
72 YQComboBox::~YQComboBox()
78 string YQComboBox::text()
80 return toUTF8( _qt_comboBox->currentText() );
84 void YQComboBox::setText( const string & newValue )
86 QString text = fromUTF8( newValue );
88 if ( isValidText( text ) )
90 YQSignalBlocker sigBlocker( _qt_comboBox );
91 _qt_comboBox->setCurrentText( text );
95 y2error( "%s \"%s\": Rejecting invalid value \"%s\"",
96 widgetClass(), debugLabel().c_str(), newValue.c_str() );
101 void YQComboBox::addItem( YItem * item )
103 YComboBox::addItem( item );
106 if ( item->hasIconName() )
108 string iconName = iconFullPath( item );
109 icon = QPixmap( iconName.c_str() );
112 y2warning( "Can't load icon %s", iconName.c_str() );
116 _qt_comboBox->insertItem( fromUTF8( item->label() ) );
118 _qt_comboBox->insertItem( icon, fromUTF8( item->label() ) );
120 if ( item->selected() )
122 YQSignalBlocker sigBlocker( _qt_comboBox );
123 setText( item->label() );
128 void YQComboBox::deleteAllItems()
130 YQSignalBlocker sigBlocker( _qt_comboBox );
132 _qt_comboBox->clear();
133 YComboBox::deleteAllItems();
137 void YQComboBox::setLabel( const string & label )
139 _caption->setText( label );
140 YComboBox::setLabel( label );
144 void YQComboBox::setValidChars( const string & newValidChars )
146 if ( ! _qt_comboBox->editable() )
148 y2warning( "Setting ValidChars is useless on a combo box that isn't editable! (%s)",
149 debugLabel().c_str() );
155 _validator->setValidChars( fromUTF8( newValidChars ) );
159 _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
160 _qt_comboBox->setValidator( _validator );
162 // No need to delete the validator in the destructor - Qt will take
163 // care of that since it's a QObject with a parent!
166 if ( ! isValidText( _qt_comboBox->currentText() ) )
168 y2error( "Old value \"%s\" of %s \"%s\" invalid according to ValidChars \"%s\" - deleting",
169 (const char *) _qt_comboBox->currentText(),
170 widgetClass(), debugLabel().c_str(),
171 newValidChars.c_str() );
172 _qt_comboBox->setCurrentText( "" );
175 YComboBox::setValidChars( newValidChars );
179 bool YQComboBox::isValidText( const QString & txt ) const
185 QString text( txt ); // need a non-const QString &
187 return _validator->validate( text, pos ) == QValidator::Acceptable;
191 void YQComboBox::slotSelected( int i )
195 if ( ! YQUI::ui()->eventPendingFor( this ) )
197 // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
199 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
205 void YQComboBox::textChanged( const QString & new_text )
208 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
212 void YQComboBox::setInputMaxLength( int len )
214 _qt_comboBox->lineEdit()->setMaxLength( len );
215 YComboBox::setInputMaxLength( len );
219 int YQComboBox::preferredWidth()
221 return sizeHint().width();
225 int YQComboBox::preferredHeight()
227 return sizeHint().height();
231 void YQComboBox::setSize( int newWidth, int newHeight )
233 resize( newWidth, newHeight );
237 void YQComboBox::setEnabled( bool enabled )
239 _caption->setEnabled( enabled );
240 _qt_comboBox->setEnabled( enabled );
241 YWidget::setEnabled( enabled );
245 bool YQComboBox::setKeyboardFocus()
247 _qt_comboBox->setFocus();
253 #include "YQComboBox.moc"