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(this);
59 _qt_comboBox->setEditable(editable);
60 YUI_CHECK_NEW( _caption );
61 layout->addWidget( _qt_comboBox );
63 _caption->setBuddy( _qt_comboBox );
65 #if SEND_SELECTION_CHANGED_EVENT
66 connect( _qt_comboBox, SIGNAL( highlighted (int) ),
67 this, SLOT ( slotSelected(int) ) );
70 connect( _qt_comboBox, SIGNAL( activated ( const QString & ) ),
71 this, SLOT ( textChanged( const QString & ) ) );
73 connect( _qt_comboBox, SIGNAL( textChanged( const QString & ) ),
74 this, SLOT ( textChanged( const QString & ) ) );
78 YQComboBox::~YQComboBox()
84 string YQComboBox::text()
86 return toUTF8( _qt_comboBox->currentText() );
90 void YQComboBox::setText( const string & newValue )
92 QString text = fromUTF8( newValue );
94 if ( isValidText( text ) )
96 YQSignalBlocker sigBlocker( _qt_comboBox );
97 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(), text );
101 y2error( "%s \"%s\": Rejecting invalid value \"%s\"",
102 widgetClass(), debugLabel().c_str(), newValue.c_str() );
107 void YQComboBox::addItem( YItem * item )
109 YComboBox::addItem( item );
112 if ( item->hasIconName() )
114 string iconName = iconFullPath( item );
115 icon = QIcon( iconName.c_str() );
118 y2warning( "Can't load icon %s", iconName.c_str() );
122 _qt_comboBox->insertItem( -1, fromUTF8( item->label() ) );
124 _qt_comboBox->insertItem( -1, icon, fromUTF8( item->label() ) );
126 if ( item->selected() )
128 YQSignalBlocker sigBlocker( _qt_comboBox );
129 setText( item->label() );
134 void YQComboBox::deleteAllItems()
136 YQSignalBlocker sigBlocker( _qt_comboBox );
138 _qt_comboBox->clear();
139 YComboBox::deleteAllItems();
143 void YQComboBox::setLabel( const string & label )
145 _caption->setText( label );
146 YComboBox::setLabel( label );
150 void YQComboBox::setValidChars( const string & newValidChars )
152 if ( ! _qt_comboBox->isEditable() )
154 y2warning( "Setting ValidChars is useless on a combo box that isn't editable! (%s)",
155 debugLabel().c_str() );
161 _validator->setValidChars( fromUTF8( newValidChars ) );
165 _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
166 _qt_comboBox->setValidator( _validator );
168 // No need to delete the validator in the destructor - Qt will take
169 // care of that since it's a QObject with a parent!
172 if ( ! isValidText( _qt_comboBox->currentText() ) )
174 y2error( "Old value \"%s\" of %s \"%s\" invalid according to ValidChars \"%s\" - deleting",
175 qPrintable(_qt_comboBox->currentText()),
176 widgetClass(), debugLabel().c_str(),
177 newValidChars.c_str() );
178 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(), "");
181 YComboBox::setValidChars( newValidChars );
185 bool YQComboBox::isValidText( const QString & txt ) const
191 QString text( txt ); // need a non-const QString &
193 return _validator->validate( text, pos ) == QValidator::Acceptable;
197 void YQComboBox::slotSelected( int i )
201 if ( ! YQUI::ui()->eventPendingFor( this ) )
203 // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
205 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
211 void YQComboBox::textChanged( const QString & new_text )
214 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
218 void YQComboBox::setInputMaxLength( int len )
220 _qt_comboBox->lineEdit()->setMaxLength( len );
221 YComboBox::setInputMaxLength( len );
225 int YQComboBox::preferredWidth()
227 return sizeHint().width();
231 int YQComboBox::preferredHeight()
233 return sizeHint().height();
237 void YQComboBox::setSize( int newWidth, int newHeight )
239 resize( newWidth, newHeight );
243 void YQComboBox::setEnabled( bool enabled )
245 _caption->setEnabled( enabled );
246 _qt_comboBox->setEnabled( enabled );
247 YWidget::setEnabled( enabled );
251 bool YQComboBox::setKeyboardFocus()
253 _qt_comboBox->setFocus();
259 #include "YQComboBox.moc"