26 #define SEND_SELECTION_CHANGED_EVENT 0
30 #include <qcombobox.h>
31 #include <qlineedit.h>
33 #define YUILogComponent "qt-ui"
34 #include <yui/YUILog.h>
38 #include <yui/YEvent.h>
39 #include "QY2CharValidator.h"
40 #include "YQComboBox.h"
41 #include "YQSignalBlocker.h"
42 #include "YQWidgetCaption.h"
43 #include <QVBoxLayout>
47 const std::string & label,
49 : QFrame( (QWidget *) parent->widgetRep() )
50 , YComboBox( parent, label, editable )
53 QVBoxLayout* layout =
new QVBoxLayout(
this );
57 layout->setSpacing( YQWidgetSpacing );
58 layout->setMargin ( YQWidgetMargin );
61 YUI_CHECK_NEW( _caption );
62 layout->addWidget( _caption );
64 _qt_comboBox =
new QComboBox(
this);
65 _qt_comboBox->setEditable(editable);
66 YUI_CHECK_NEW( _caption );
67 layout->addWidget( _qt_comboBox );
69 _caption->setBuddy( _qt_comboBox );
71 #if SEND_SELECTION_CHANGED_EVENT
72 connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
76 connect( _qt_comboBox,
static_cast<void (QComboBox::*)(
const QString&)
>(&QComboBox::activated),
79 connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
92 return toUTF8( _qt_comboBox->currentText() );
98 QString
text = fromUTF8( newValue );
103 int index = _qt_comboBox->findText( text );
105 _qt_comboBox->setEditText( text );
107 _qt_comboBox->setCurrentIndex( index );
108 _qt_comboBox->setItemText(index, text );
113 yuiError() <<
this <<
": Rejecting invalid value \"" << newValue <<
"\"" << std::endl;
120 YComboBox::addItem( item );
123 if ( item->hasIconName() )
125 string iconName = iconFullPath( item );
126 icon = QIcon( iconName.c_str() );
129 yuiWarning() <<
"Can't load icon \"" << iconName <<
"\"" << std::endl;
133 _qt_comboBox->addItem( fromUTF8( item->label() ) );
135 _qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
137 if ( item->selected() )
149 _qt_comboBox->clear();
150 YComboBox::deleteAllItems();
157 YComboBox::setLabel( label );
163 if ( ! _qt_comboBox->isEditable() )
165 yuiWarning() <<
this <<
": Setting ValidChars is useless on a combo box that isn't editable!" << std::endl;
176 _qt_comboBox->setValidator( _validator );
182 if ( !
isValidText( _qt_comboBox->currentText() ) )
184 yuiError() <<
this <<
": Old value \"" << _qt_comboBox->currentText()
185 <<
" \" invalid according to new ValidChars \""<< newValidChars <<
"\" - deleting"
187 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(),
"");
190 YComboBox::setValidChars( newValidChars );
202 return _validator->
validate( text, pos ) == QValidator::Acceptable;
210 if ( !
YQUI::ui()->eventPendingFor(
this ) )
229 _qt_comboBox->lineEdit()->setMaxLength( len );
230 YComboBox::setInputMaxLength( len );
236 return sizeHint().width();
242 return sizeHint().height();
248 resize( newWidth, newHeight );
254 _caption->setEnabled( enabled );
255 _qt_comboBox->setEnabled( enabled );
256 YWidget::setEnabled( enabled );
262 _qt_comboBox->setFocus();
268 #include "YQComboBox.moc"
virtual void setValidChars(const std::string &validChars)
Change the valid input characters.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual std::string text()
Return this ComboBox's current value as text.
virtual void setLabel(const std::string &label)
Change the label text.
void textChanged(QString)
Tells the ui that the user has edited the text ( if the 'editable' option is set ).
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
virtual State validate(QString &input, int &pos) const
Check user input.
YQComboBox(YWidget *parent, const std::string &label, bool editable)
Constructor.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual void addItem(YItem *item)
Add one item.
virtual void deleteAllItems()
Delete all items.
void slotSelected(int i)
Tells the ui that an item has been selected.
bool isValidText(const QString &txt) const
Returns 'true' if the given text is valid according to the current setting of ValidChars.
void setValidChars(const QString &newValidChars)
Set the valid input characters.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual int preferredHeight()
Preferred height of the widget.
virtual void setInputMaxLength(int numberOfChars)
Specify the amount of characters which can be inserted.
virtual void setText(const std::string &newText)
Set this ComboBox's current value as text.
static YQUI * ui()
Access the global Qt-UI.