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 );
}
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 );

View File

@ -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& );

View File

@ -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 );

View File

@ -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;