26 #define SEND_SELECTION_CHANGED_EVENT 0
30 #include <qcombobox.h>
31 #include <qlineedit.h>
32 #define YUILogComponent "qt-ui"
33 #include <yui/YUILog.h>
37 #include <yui/YEvent.h>
38 #include "QY2CharValidator.h"
39 #include "YQComboBox.h"
40 #include "YQSignalBlocker.h"
41 #include "YQWidgetCaption.h"
42 #include <QVBoxLayout>
53 : QFrame( (QWidget *) parent->widgetRep() )
54 , YComboBox( parent, label, editable )
57 QVBoxLayout* layout =
new QVBoxLayout(
this );
61 layout->setSpacing( YQWidgetSpacing );
62 layout->setMargin ( YQWidgetMargin );
65 YUI_CHECK_NEW( _caption );
66 layout->addWidget( _caption );
68 _qt_comboBox =
new QComboBox(
this);
69 _qt_comboBox->setEditable(editable);
70 YUI_CHECK_NEW( _caption );
71 layout->addWidget( _qt_comboBox );
73 _caption->setBuddy( _qt_comboBox );
75 #if SEND_SELECTION_CHANGED_EVENT
76 connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
80 #if (QT_VERSION < QT_VERSION_CHECK( 5, 15, 0 ))
93 connect( _qt_comboBox,
static_cast<void (QComboBox::*)(
const QString&)
>(&QComboBox::activated),
96 connect( _qt_comboBox,
static_cast<void (QComboBox::*)(
const QString&)
>(&QComboBox::textActivated),
100 connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
113 return toUTF8( _qt_comboBox->currentText() );
119 QString
text = fromUTF8( newValue );
124 int index = _qt_comboBox->findText(
text );
126 _qt_comboBox->setEditText(
text );
129 _qt_comboBox->setCurrentIndex( index );
130 _qt_comboBox->setItemText(index,
text );
135 yuiError() <<
this <<
": Rejecting invalid value \"" << newValue <<
"\"" << endl;
142 YComboBox::addItem( item );
145 if ( item->hasIconName() )
151 _qt_comboBox->addItem( fromUTF8( item->label() ) );
153 _qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
155 if ( item->selected() )
167 _qt_comboBox->clear();
168 YComboBox::deleteAllItems();
175 YComboBox::setLabel( label );
181 if ( ! _qt_comboBox->isEditable() )
183 yuiWarning() <<
this <<
": Setting ValidChars is useless on a combo box that isn't editable!" << endl;
194 _qt_comboBox->setValidator( _validator );
200 if ( !
isValidText( _qt_comboBox->currentText() ) )
202 yuiError() <<
this <<
": Old value \"" << _qt_comboBox->currentText()
203 <<
" \" invalid according to new ValidChars \""<< newValidChars <<
"\" - deleting"
205 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(),
"");
208 YComboBox::setValidChars( newValidChars );
220 return _validator->
validate(
text, pos ) == QValidator::Acceptable;
228 if ( !
YQUI::ui()->eventPendingFor(
this ) )
247 _qt_comboBox->lineEdit()->setMaxLength( len );
248 YComboBox::setInputMaxLength( len );
254 return sizeHint().width();
260 return sizeHint().height();
266 resize( newWidth, newHeight );
272 _caption->setEnabled( enabled );
273 _qt_comboBox->setEnabled( enabled );
274 YWidget::setEnabled( enabled );
280 _qt_comboBox->setFocus();
virtual State validate(QString &input, int &pos) const
Check user input.
void setValidChars(const QString &newValidChars)
Set the valid input characters.
virtual void addItem(YItem *item)
Add one item.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setText(const std::string &newText)
Set this ComboBox's current value as text.
virtual void setLabel(const std::string &label)
Change the label text.
void slotSelected(int i)
Tells the ui that an item has been selected.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual int preferredHeight()
Preferred height of the widget.
virtual std::string text()
Return this ComboBox's current value as text.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
bool isValidText(const QString &txt) const
Returns 'true' if the given text is valid according to the current setting of ValidChars.
virtual void deleteAllItems()
Delete all items.
virtual void setInputMaxLength(int numberOfChars)
Specify the amount of characters which can be inserted.
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
void textChanged(QString)
Tells the ui that the user has edited the text ( if the 'editable' option is set ).
virtual void setValidChars(const std::string &validChars)
Change the valid input characters.
YQComboBox(YWidget *parent, const std::string &label, bool editable)
Constructor.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
static YQUI * ui()
Access the global Qt-UI.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
QIcon loadIcon(const string &iconName) const
Load an icon.