diff --git a/src/controls/QskComboBox.cpp b/src/controls/QskComboBox.cpp index 17da7071..dda9e13d 100644 --- a/src/controls/QskComboBox.cpp +++ b/src/controls/QskComboBox.cpp @@ -40,6 +40,26 @@ static inline void qskTraverseOptions( QskComboBox* comboBox, int steps ) comboBox->setCurrentIndex( nextIndex ); } +static inline int qskFindOption( QskComboBox* comboBox, const QString& key ) +{ + if ( !key.isEmpty() ) + { + const int currentIndex = comboBox->currentIndex(); + const int count = comboBox->count(); + + for ( int i = 1; i < count; i++ ) + { + const int index = ( currentIndex + i ) % count; + const auto text = comboBox->textAt( index ); + + if ( text.startsWith( key ) ) + return index; + } + } + + return -1; +} + namespace { class Option @@ -206,15 +226,20 @@ void QskComboBox::setPlaceholderText( const QString& text ) Q_EMIT placeholderTextChanged( text ); } +QString QskComboBox::textAt( int index ) const +{ + if ( index >= 0 && index < m_data->options.count() ) + return m_data->options[ index ].text; + + return QString(); +} + QString QskComboBox::currentText() const { if( m_data->currentIndex >= 0 ) - { - const auto option = optionAt( m_data->currentIndex ); - return option.at( 1 ).toString(); - } + return m_data->options[ m_data->currentIndex ].text; - return placeholderText(); + return m_data->placeholderText; } void QskComboBox::openPopup() @@ -308,8 +333,14 @@ void QskComboBox::keyPressEvent( QKeyEvent* event ) } default: - // searching option by key TODO ... - break; + { + const int index = qskFindOption( this, event->text() ); + if ( index >= 0 ) + { + setCurrentIndex( index ); + return; + } + } } Inherited::keyPressEvent( event ); diff --git a/src/controls/QskComboBox.h b/src/controls/QskComboBox.h index fdda5036..92bf38e4 100644 --- a/src/controls/QskComboBox.h +++ b/src/controls/QskComboBox.h @@ -54,6 +54,7 @@ class QSK_EXPORT QskComboBox : public QskControl int count() const; QVariantList optionAt( int ) const; + QString textAt( int ) const; QString placeholderText() const; void setPlaceholderText( const QString& ); diff --git a/src/controls/QskMenu.cpp b/src/controls/QskMenu.cpp index 36d24307..9450868e 100644 --- a/src/controls/QskMenu.cpp +++ b/src/controls/QskMenu.cpp @@ -218,6 +218,19 @@ QVariantList QskMenu::optionAt( int index ) const return list; } +QString QskMenu::textAt( int index ) const +{ + if ( index >= 0 && index < m_data->options.count() ) + return m_data->options[ index ].text; + + return QString(); +} + +QString QskMenu::currentText() const +{ + return textAt( m_data->currentIndex ); +} + void QskMenu::setTextOptions( const QskTextOptions& textOptions ) { setTextOptionsHint( Text, textOptions ); diff --git a/src/controls/QskMenu.h b/src/controls/QskMenu.h index b1b5d470..317dd5d9 100644 --- a/src/controls/QskMenu.h +++ b/src/controls/QskMenu.h @@ -29,6 +29,8 @@ class QSK_EXPORT QskMenu : public QskPopup Q_PROPERTY( int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged ) + Q_PROPERTY( QString currentText READ currentText ) + using Inherited = QskPopup; public: @@ -55,6 +57,8 @@ class QSK_EXPORT QskMenu : public QskPopup void addOption( const QString& text ); QVariantList optionAt( int ) const; + QString textAt( int ) const; + int count() const; void addSeparator(); @@ -65,6 +69,7 @@ class QSK_EXPORT QskMenu : public QskPopup void clear(); int currentIndex() const; + QString currentText() const; QRectF focusIndicatorRect() const override;