QskComboBox key searching added

This commit is contained in:
Uwe Rathmann 2023-03-07 14:32:53 +01:00
parent fa998a9496
commit 82f9a72603
4 changed files with 57 additions and 7 deletions

View File

@ -40,6 +40,26 @@ static inline void qskTraverseOptions( QskComboBox* comboBox, int steps )
comboBox->setCurrentIndex( nextIndex ); 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 namespace
{ {
class Option class Option
@ -206,15 +226,20 @@ void QskComboBox::setPlaceholderText( const QString& text )
Q_EMIT placeholderTextChanged( 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 QString QskComboBox::currentText() const
{ {
if( m_data->currentIndex >= 0 ) if( m_data->currentIndex >= 0 )
{ return m_data->options[ m_data->currentIndex ].text;
const auto option = optionAt( m_data->currentIndex );
return option.at( 1 ).toString();
}
return placeholderText(); return m_data->placeholderText;
} }
void QskComboBox::openPopup() void QskComboBox::openPopup()
@ -308,8 +333,14 @@ void QskComboBox::keyPressEvent( QKeyEvent* event )
} }
default: default:
// searching option by key TODO ... {
break; const int index = qskFindOption( this, event->text() );
if ( index >= 0 )
{
setCurrentIndex( index );
return;
}
}
} }
Inherited::keyPressEvent( event ); Inherited::keyPressEvent( event );

View File

@ -54,6 +54,7 @@ class QSK_EXPORT QskComboBox : public QskControl
int count() const; int count() const;
QVariantList optionAt( int ) const; QVariantList optionAt( int ) const;
QString textAt( int ) const;
QString placeholderText() const; QString placeholderText() const;
void setPlaceholderText( const QString& ); void setPlaceholderText( const QString& );

View File

@ -218,6 +218,19 @@ QVariantList QskMenu::optionAt( int index ) const
return list; 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 ) void QskMenu::setTextOptions( const QskTextOptions& textOptions )
{ {
setTextOptionsHint( Text, textOptions ); setTextOptionsHint( Text, textOptions );

View File

@ -29,6 +29,8 @@ class QSK_EXPORT QskMenu : public QskPopup
Q_PROPERTY( int currentIndex READ currentIndex Q_PROPERTY( int currentIndex READ currentIndex
WRITE setCurrentIndex NOTIFY currentIndexChanged ) WRITE setCurrentIndex NOTIFY currentIndexChanged )
Q_PROPERTY( QString currentText READ currentText )
using Inherited = QskPopup; using Inherited = QskPopup;
public: public:
@ -55,6 +57,8 @@ class QSK_EXPORT QskMenu : public QskPopup
void addOption( const QString& text ); void addOption( const QString& text );
QVariantList optionAt( int ) const; QVariantList optionAt( int ) const;
QString textAt( int ) const;
int count() const; int count() const;
void addSeparator(); void addSeparator();
@ -65,6 +69,7 @@ class QSK_EXPORT QskMenu : public QskPopup
void clear(); void clear();
int currentIndex() const; int currentIndex() const;
QString currentText() const;
QRectF focusIndicatorRect() const override; QRectF focusIndicatorRect() const override;