index of the combo box changes on the final QskMenu::trigereed - not while

navigating in the menu
This commit is contained in:
Uwe Rathmann 2023-03-06 17:37:32 +01:00
parent afe3248a8b
commit 48853d38ef
3 changed files with 47 additions and 51 deletions

View File

@ -19,11 +19,29 @@ QSK_SUBCONTROL( QskComboBox, PopupIndicator )
QSK_SYSTEM_STATE( QskComboBox, PopupOpen, QskAspect::FirstSystemState << 1 ) QSK_SYSTEM_STATE( QskComboBox, PopupOpen, QskAspect::FirstSystemState << 1 )
static inline void qskIncrement( QskComboBox* comboBox, int steps )
{
const auto count = comboBox->count();
if ( count == 0 )
return;
const int index = comboBox->currentIndex();
if ( index == -1 && steps < 0 )
steps++;
int nextIndex = ( index + steps ) % count;
if ( nextIndex < 0 )
nextIndex += count;
if ( nextIndex != index )
comboBox->setCurrentIndex( nextIndex );
}
class QskComboBox::PrivateData class QskComboBox::PrivateData
{ {
public: public:
PrivateData( QskComboBox* const box ) PrivateData( QskComboBox* comboBox )
: menu( new QskMenu( box ) )
{ {
menu = new QskMenu(); menu = new QskMenu();
@ -33,12 +51,14 @@ class QskComboBox::PrivateData
So we set the box as QObject parent only, so that it gets So we set the box as QObject parent only, so that it gets
destroyed properly. destroyed properly.
*/ */
menu->setParent( box ); menu->setParent( comboBox );
menu->setPopupFlag( QskPopup::DeleteOnClose, false ); menu->setPopupFlag( QskPopup::DeleteOnClose, false );
} }
QskMenu* menu; QskMenu* menu;
QString placeholderText; QString placeholderText;
int currentIndex = -1;
}; };
QskComboBox::QskComboBox( QQuickItem* parent ) QskComboBox::QskComboBox( QQuickItem* parent )
@ -56,10 +76,7 @@ QskComboBox::QskComboBox( QQuickItem* parent )
setAcceptHoverEvents( true ); setAcceptHoverEvents( true );
connect( m_data->menu, &QskMenu::triggered, connect( m_data->menu, &QskMenu::triggered,
this, &QskComboBox::activated ); this, &QskComboBox::setCurrentIndex );
connect( m_data->menu, &QskMenu::currentIndexChanged,
this, &QskComboBox::showOption );
connect( m_data->menu, &QskMenu::countChanged, connect( m_data->menu, &QskMenu::countChanged,
this, &QskComboBox::countChanged ); this, &QskComboBox::countChanged );
@ -92,10 +109,9 @@ bool QskComboBox::isPopupOpen() const
QskGraphic QskComboBox::graphic() const QskGraphic QskComboBox::graphic() const
{ {
const int index = currentIndex(); if( m_data->currentIndex >= 0 )
if( index >= 0 )
{ {
const auto option = m_data->menu->optionAt( index ); const auto option = optionAt( m_data->currentIndex );
return option.at( 0 ).value< QskGraphic >(); return option.at( 0 ).value< QskGraphic >();
} }
@ -133,10 +149,9 @@ void QskComboBox::setPlaceholderText( const QString& text )
return; return;
m_data->placeholderText = text; m_data->placeholderText = text;
resetImplicitSize(); resetImplicitSize();
if ( currentIndex() < 0 ) if ( m_data->currentIndex < 0 )
update(); update();
Q_EMIT placeholderTextChanged( text ); Q_EMIT placeholderTextChanged( text );
@ -144,10 +159,9 @@ void QskComboBox::setPlaceholderText( const QString& text )
QString QskComboBox::currentText() const QString QskComboBox::currentText() const
{ {
const int index = currentIndex(); if( m_data->currentIndex >= 0 )
if( index >= 0 )
{ {
const auto option = optionAt( index ); const auto option = optionAt( m_data->currentIndex );
return option.at( 1 ).toString(); return option.at( 1 ).toString();
} }
@ -205,13 +219,13 @@ void QskComboBox::keyPressEvent( QKeyEvent* event )
case Qt::Key_Up: case Qt::Key_Up:
case Qt::Key_PageUp: case Qt::Key_PageUp:
{ {
increment( -1 ); qskIncrement( this, -1 );
return; return;
} }
case Qt::Key_Down: case Qt::Key_Down:
case Qt::Key_PageDown: case Qt::Key_PageDown:
{ {
increment( 1 ); qskIncrement( this, 1 );
return; return;
} }
case Qt::Key_Home: case Qt::Key_Home:
@ -242,23 +256,33 @@ void QskComboBox::keyReleaseEvent( QKeyEvent* event )
void QskComboBox::wheelEvent( QWheelEvent* event ) void QskComboBox::wheelEvent( QWheelEvent* event )
{ {
increment( -qRound( qskWheelSteps( event ) ) ); if ( !isPopupOpen() )
{
const auto steps = -qRound( qskWheelSteps( event ) );
qskIncrement( this, steps );
}
} }
void QskComboBox::clear() void QskComboBox::clear()
{ {
m_data->menu->clear(); m_data->menu->clear();
update(); setCurrentIndex( -1 );
} }
void QskComboBox::setCurrentIndex( int index ) void QskComboBox::setCurrentIndex( int index )
{ {
m_data->menu->setCurrentIndex( index ); if ( m_data->currentIndex != index )
{
m_data->currentIndex = index;
update();
Q_EMIT currentIndexChanged( index );
}
} }
int QskComboBox::currentIndex() const int QskComboBox::currentIndex() const
{ {
return m_data->menu->currentIndex(); return m_data->currentIndex;
} }
int QskComboBox::count() const int QskComboBox::count() const
@ -266,29 +290,4 @@ int QskComboBox::count() const
return m_data->menu->count(); return m_data->menu->count();
} }
void QskComboBox::showOption( int index )
{
update();
Q_EMIT currentIndexChanged( index );
}
void QskComboBox::increment( int steps )
{
if ( count() == 0 )
return;
if ( currentIndex() == -1 && steps < 0 )
steps++;
int nextIndex = ( currentIndex() + steps ) % count();
if ( nextIndex < 0 )
nextIndex += count();
if ( nextIndex != currentIndex() )
{
m_data->menu->setCurrentIndex( nextIndex );
Q_EMIT activated( nextIndex );
}
}
#include "moc_QskComboBox.cpp" #include "moc_QskComboBox.cpp"

View File

@ -59,7 +59,6 @@ class QSK_EXPORT QskComboBox : public QskControl
void setCurrentIndex( int ); void setCurrentIndex( int );
Q_SIGNALS: Q_SIGNALS:
void activated( int );
void currentIndexChanged( int ); void currentIndexChanged( int );
void countChanged(); void countChanged();
@ -78,10 +77,6 @@ class QSK_EXPORT QskComboBox : public QskControl
virtual void closePopup(); virtual void closePopup();
private: private:
void showOption( int );
void releaseButton();
void increment( int );
class PrivateData; class PrivateData;
std::unique_ptr< PrivateData > m_data; std::unique_ptr< PrivateData > m_data;
}; };

View File

@ -247,6 +247,8 @@ void QskMenu::keyPressEvent( QKeyEvent* event )
case Qt::Key_Select: case Qt::Key_Select:
case Qt::Key_Space: case Qt::Key_Space:
case Qt::Key_Return:
case Qt::Key_Enter:
{ {
m_data->isPressed = true; m_data->isPressed = true;
return; return;