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> 46 const std::string & label,
48 : QFrame( (QWidget *) parent->widgetRep() )
49 , YComboBox( parent, label, editable )
52 QVBoxLayout* layout =
new QVBoxLayout(
this );
56 layout->setSpacing( YQWidgetSpacing );
57 layout->setMargin ( YQWidgetMargin );
60 YUI_CHECK_NEW( _caption );
61 layout->addWidget( _caption );
63 _qt_comboBox =
new QComboBox(
this);
64 _qt_comboBox->setEditable(editable);
65 YUI_CHECK_NEW( _caption );
66 layout->addWidget( _qt_comboBox );
68 _caption->setBuddy( _qt_comboBox );
70 #if SEND_SELECTION_CHANGED_EVENT 71 connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
75 connect( _qt_comboBox,
static_cast<void (QComboBox::*)(
const QString&)
>(&QComboBox::activated),
78 connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
91 return toUTF8( _qt_comboBox->currentText() );
97 QString
text = fromUTF8( newValue );
102 int index = _qt_comboBox->findText(
text );
104 _qt_comboBox->setEditText(
text );
106 _qt_comboBox->setCurrentIndex( index );
107 _qt_comboBox->setItemText(index,
text );
112 yuiError() <<
this <<
": Rejecting invalid value \"" << newValue <<
"\"" << std::endl;
119 YComboBox::addItem( item );
122 if ( item->hasIconName() )
124 icon =
YQUI::ui()->loadIcon( item->iconName() );
128 _qt_comboBox->addItem( fromUTF8( item->label() ) );
130 _qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
132 if ( item->selected() )
144 _qt_comboBox->clear();
145 YComboBox::deleteAllItems();
152 YComboBox::setLabel( label );
158 if ( ! _qt_comboBox->isEditable() )
160 yuiWarning() <<
this <<
": Setting ValidChars is useless on a combo box that isn't editable!" << std::endl;
171 _qt_comboBox->setValidator( _validator );
177 if ( !
isValidText( _qt_comboBox->currentText() ) )
179 yuiError() <<
this <<
": Old value \"" << _qt_comboBox->currentText()
180 <<
" \" invalid according to new ValidChars \""<< newValidChars <<
"\" - deleting" 182 _qt_comboBox->setItemText(_qt_comboBox->currentIndex(),
"");
185 YComboBox::setValidChars( newValidChars );
197 return _validator->
validate(
text, pos ) == QValidator::Acceptable;
205 if ( !
YQUI::ui()->eventPendingFor(
this ) )
224 _qt_comboBox->lineEdit()->setMaxLength( len );
225 YComboBox::setInputMaxLength( len );
231 return sizeHint().width();
237 return sizeHint().height();
243 resize( newWidth, newHeight );
249 _caption->setEnabled( enabled );
250 _qt_comboBox->setEnabled( enabled );
251 YWidget::setEnabled( enabled );
257 _qt_comboBox->setFocus();
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.
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.
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.
bool isValidText(const QString &txt) const
Returns 'true' if the given text is valid according to the current setting of ValidChars.
virtual State validate(QString &input, int &pos) const
Check user input.
static YQUI * ui()
Access the global Qt-UI.