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"
37 #include <QVBoxLayout>
40 YQComboBox::YQComboBox( YWidget * parent,
43 : QFrame( (QWidget *) parent->widgetRep() )
44 , YComboBox( parent, label, editable )
47 QVBoxLayout* layout = new QVBoxLayout( this );
51 layout->setSpacing( YQWidgetSpacing );
52 layout->setMargin ( YQWidgetMargin );
54 _caption = new YQWidgetCaption( this, label );
55 YUI_CHECK_NEW( _caption );
56 layout->addWidget( _caption );
58 _qt_comboBox = new QComboBox( editable, this );
59 YUI_CHECK_NEW( _caption );
60 layout->addWidget( _qt_comboBox );
62 _caption->setBuddy( _qt_comboBox );
64 #if SEND_SELECTION_CHANGED_EVENT
65 connect( _qt_comboBox, SIGNAL( highlighted (int) ),
66 this, SLOT ( slotSelected(int) ) );
69 connect( _qt_comboBox, SIGNAL( activated ( const QString & ) ),
70 this, SLOT ( textChanged( const QString & ) ) );
72 connect( _qt_comboBox, SIGNAL( textChanged( const QString & ) ),
73 this, SLOT ( textChanged( const QString & ) ) );
77 YQComboBox::~YQComboBox()
83 string YQComboBox::text()
85 return toUTF8( _qt_comboBox->currentText() );
89 void YQComboBox::setText( const string & newValue )
91 QString text = fromUTF8( newValue );
93 if ( isValidText( text ) )
95 YQSignalBlocker sigBlocker( _qt_comboBox );
96 _qt_comboBox->setCurrentText( text );
100 y2error( "%s \"%s\": Rejecting invalid value \"%s\"",
101 widgetClass(), debugLabel().c_str(), newValue.c_str() );
106 void YQComboBox::addItem( YItem * item )
108 YComboBox::addItem( item );
111 if ( item->hasIconName() )
113 string iconName = iconFullPath( item );
114 icon = QPixmap( iconName.c_str() );
117 y2warning( "Can't load icon %s", iconName.c_str() );
121 _qt_comboBox->insertItem( fromUTF8( item->label() ) );
123 _qt_comboBox->insertItem( icon, fromUTF8( item->label() ) );
125 if ( item->selected() )
127 YQSignalBlocker sigBlocker( _qt_comboBox );
128 setText( item->label() );
133 void YQComboBox::deleteAllItems()
135 YQSignalBlocker sigBlocker( _qt_comboBox );
137 _qt_comboBox->clear();
138 YComboBox::deleteAllItems();
142 void YQComboBox::setLabel( const string & label )
144 _caption->setText( label );
145 YComboBox::setLabel( label );
149 void YQComboBox::setValidChars( const string & newValidChars )
151 if ( ! _qt_comboBox->editable() )
153 y2warning( "Setting ValidChars is useless on a combo box that isn't editable! (%s)",
154 debugLabel().c_str() );
160 _validator->setValidChars( fromUTF8( newValidChars ) );
164 _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
165 _qt_comboBox->setValidator( _validator );
167 // No need to delete the validator in the destructor - Qt will take
168 // care of that since it's a QObject with a parent!
171 if ( ! isValidText( _qt_comboBox->currentText() ) )
173 y2error( "Old value \"%s\" of %s \"%s\" invalid according to ValidChars \"%s\" - deleting",
174 (const char *) _qt_comboBox->currentText(),
175 widgetClass(), debugLabel().c_str(),
176 newValidChars.c_str() );
177 _qt_comboBox->setCurrentText( "" );
180 YComboBox::setValidChars( newValidChars );
184 bool YQComboBox::isValidText( const QString & txt ) const
190 QString text( txt ); // need a non-const QString &
192 return _validator->validate( text, pos ) == QValidator::Acceptable;
196 void YQComboBox::slotSelected( int i )
200 if ( ! YQUI::ui()->eventPendingFor( this ) )
202 // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
204 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
210 void YQComboBox::textChanged( const QString & new_text )
213 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
217 void YQComboBox::setInputMaxLength( int len )
219 _qt_comboBox->lineEdit()->setMaxLength( len );
220 YComboBox::setInputMaxLength( len );
224 int YQComboBox::preferredWidth()
226 return sizeHint().width();
230 int YQComboBox::preferredHeight()
232 return sizeHint().height();
236 void YQComboBox::setSize( int newWidth, int newHeight )
238 resize( newWidth, newHeight );
242 void YQComboBox::setEnabled( bool enabled )
244 _caption->setEnabled( enabled );
245 _qt_comboBox->setEnabled( enabled );
246 YWidget::setEnabled( enabled );
250 bool YQComboBox::setKeyboardFocus()
252 _qt_comboBox->setFocus();
258 #include "YQComboBox.moc"