2023-02-14 07:58:37 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2023 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskComboBox.h"
|
|
|
|
|
|
|
|
#include "QskGraphic.h"
|
|
|
|
#include "QskMenu.h"
|
|
|
|
#include "QskTextOptions.h"
|
2023-03-06 09:44:00 +00:00
|
|
|
#include "QskEvent.h"
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-06 06:47:49 +00:00
|
|
|
#include <qquickwindow.h>
|
|
|
|
|
2023-02-14 07:58:37 +00:00
|
|
|
QSK_SUBCONTROL( QskComboBox, Panel )
|
|
|
|
QSK_SUBCONTROL( QskComboBox, Graphic )
|
|
|
|
QSK_SUBCONTROL( QskComboBox, Text )
|
2023-03-06 09:44:00 +00:00
|
|
|
QSK_SUBCONTROL( QskComboBox, PopupIndicator )
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
QSK_SYSTEM_STATE( QskComboBox, PopupOpen, QskAspect::FirstSystemState << 1 )
|
|
|
|
|
2023-03-06 16:37:32 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2023-02-14 07:58:37 +00:00
|
|
|
class QskComboBox::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
2023-03-06 16:37:32 +00:00
|
|
|
PrivateData( QskComboBox* comboBox )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 06:47:49 +00:00
|
|
|
menu = new QskMenu();
|
2023-03-03 18:01:40 +00:00
|
|
|
|
|
|
|
/*
|
2023-03-06 06:47:49 +00:00
|
|
|
The popup menu is supposed to be modal for the window and
|
|
|
|
therefore needs the root item of the window as parent item.
|
|
|
|
So we set the box as QObject parent only, so that it gets
|
|
|
|
destroyed properly.
|
2023-03-03 18:01:40 +00:00
|
|
|
*/
|
2023-03-06 16:37:32 +00:00
|
|
|
menu->setParent( comboBox );
|
2023-03-06 06:47:49 +00:00
|
|
|
menu->setPopupFlag( QskPopup::DeleteOnClose, false );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:01:40 +00:00
|
|
|
QskMenu* menu;
|
|
|
|
QString placeholderText;
|
2023-03-06 16:37:32 +00:00
|
|
|
|
|
|
|
int currentIndex = -1;
|
2023-02-14 07:58:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
QskComboBox::QskComboBox( QQuickItem* parent )
|
|
|
|
: Inherited( parent )
|
|
|
|
, m_data( new PrivateData( this ) )
|
|
|
|
{
|
|
|
|
initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
|
|
|
|
|
|
|
|
setPolishOnResize( true );
|
|
|
|
|
|
|
|
setAcceptedMouseButtons( Qt::LeftButton );
|
|
|
|
setWheelEnabled( true );
|
|
|
|
setFocusPolicy( Qt::StrongFocus );
|
|
|
|
|
|
|
|
setAcceptHoverEvents( true );
|
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
connect( m_data->menu, &QskMenu::triggered,
|
2023-03-06 16:37:32 +00:00
|
|
|
this, &QskComboBox::setCurrentIndex );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-03 18:01:40 +00:00
|
|
|
connect( m_data->menu, &QskMenu::countChanged,
|
|
|
|
this, &QskComboBox::countChanged );
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-02-28 14:49:42 +00:00
|
|
|
connect( m_data->menu, &QskMenu::closed, this,
|
|
|
|
[ this ]() { setPopupOpen( false ); setFocus( true ); } );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QskComboBox::~QskComboBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::setPopupOpen( bool on )
|
|
|
|
{
|
|
|
|
if ( on == isPopupOpen() )
|
|
|
|
return;
|
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
setSkinStateFlag( PopupOpen, on );
|
|
|
|
|
2023-02-14 07:58:37 +00:00
|
|
|
if( on )
|
|
|
|
openPopup();
|
|
|
|
else
|
|
|
|
closePopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskComboBox::isPopupOpen() const
|
|
|
|
{
|
|
|
|
return hasSkinState( PopupOpen );
|
|
|
|
}
|
|
|
|
|
|
|
|
QskGraphic QskComboBox::graphic() const
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
if( m_data->currentIndex >= 0 )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
const auto option = optionAt( m_data->currentIndex );
|
2023-02-14 07:58:37 +00:00
|
|
|
return option.at( 0 ).value< QskGraphic >();
|
|
|
|
}
|
2023-03-03 18:01:40 +00:00
|
|
|
|
|
|
|
return QskGraphic();
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::setTextOptions( const QskTextOptions& textOptions )
|
|
|
|
{
|
|
|
|
setTextOptionsHint( Text, textOptions );
|
|
|
|
}
|
|
|
|
|
|
|
|
QskTextOptions QskComboBox::textOptions() const
|
|
|
|
{
|
|
|
|
return textOptionsHint( Text );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::addOption( const QUrl& graphicSource, const QString& text )
|
|
|
|
{
|
|
|
|
m_data->menu->addOption( graphicSource, text );
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList QskComboBox::optionAt( int index ) const
|
|
|
|
{
|
|
|
|
return m_data->menu->optionAt( index );
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:01:40 +00:00
|
|
|
QString QskComboBox::placeholderText() const
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-03 18:01:40 +00:00
|
|
|
return m_data->placeholderText;
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:01:40 +00:00
|
|
|
void QskComboBox::setPlaceholderText( const QString& text )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-03 18:01:40 +00:00
|
|
|
if ( m_data->placeholderText == text )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_data->placeholderText = text;
|
|
|
|
resetImplicitSize();
|
|
|
|
|
2023-03-06 16:37:32 +00:00
|
|
|
if ( m_data->currentIndex < 0 )
|
2023-03-03 18:01:40 +00:00
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT placeholderTextChanged( text );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
QString QskComboBox::currentText() const
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
if( m_data->currentIndex >= 0 )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
const auto option = optionAt( m_data->currentIndex );
|
2023-02-14 07:58:37 +00:00
|
|
|
return option.at( 1 ).toString();
|
|
|
|
}
|
2023-03-03 18:01:40 +00:00
|
|
|
|
|
|
|
return placeholderText();
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::openPopup()
|
|
|
|
{
|
2023-03-06 06:47:49 +00:00
|
|
|
const auto cr = contentsRect();
|
|
|
|
|
|
|
|
auto menu = m_data->menu;
|
|
|
|
|
|
|
|
menu->setParentItem( window()->contentItem() );
|
|
|
|
menu->setOrigin( mapToScene( cr.bottomLeft() ) );
|
|
|
|
menu->setFixedWidth( cr.width() );
|
|
|
|
|
|
|
|
menu->open();
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::closePopup()
|
|
|
|
{
|
|
|
|
m_data->menu->close();
|
|
|
|
}
|
|
|
|
|
2023-03-06 06:47:49 +00:00
|
|
|
void QskComboBox::mousePressEvent( QMouseEvent* )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 09:44:00 +00:00
|
|
|
setPopupOpen( true );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 18:01:40 +00:00
|
|
|
void QskComboBox::mouseReleaseEvent( QMouseEvent* )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::keyPressEvent( QKeyEvent* event )
|
|
|
|
{
|
2023-03-06 11:26:38 +00:00
|
|
|
if ( qskIsButtonPressKey( event ) )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 09:44:00 +00:00
|
|
|
if ( !event->isAutoRepeat() )
|
2023-02-14 07:58:37 +00:00
|
|
|
{
|
2023-03-06 09:44:00 +00:00
|
|
|
setPopupOpen( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 07:58:37 +00:00
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
switch( event->key() )
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
case Qt::Key_F4:
|
|
|
|
{
|
|
|
|
// QComboBox does this ???
|
|
|
|
setPopupOpen( true );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case Qt::Key_Up:
|
|
|
|
case Qt::Key_PageUp:
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
qskIncrement( this, -1 );
|
2023-03-06 09:44:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Qt::Key_Down:
|
|
|
|
case Qt::Key_PageDown:
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
qskIncrement( this, 1 );
|
2023-03-06 09:44:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Qt::Key_Home:
|
|
|
|
{
|
|
|
|
if ( count() > 0 )
|
|
|
|
setCurrentIndex( 0 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Qt::Key_End:
|
|
|
|
{
|
|
|
|
if ( count() > 0 )
|
|
|
|
setCurrentIndex( count() - 1 );
|
2023-02-14 07:58:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-03-06 09:44:00 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// searching option by key TODO ...
|
|
|
|
break;
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Inherited::keyPressEvent( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::keyReleaseEvent( QKeyEvent* event )
|
|
|
|
{
|
|
|
|
Inherited::keyReleaseEvent( event );
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:44:00 +00:00
|
|
|
void QskComboBox::wheelEvent( QWheelEvent* event )
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
if ( !isPopupOpen() )
|
|
|
|
{
|
|
|
|
const auto steps = -qRound( qskWheelSteps( event ) );
|
|
|
|
qskIncrement( this, steps );
|
|
|
|
}
|
2023-03-06 09:44:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 07:58:37 +00:00
|
|
|
void QskComboBox::clear()
|
|
|
|
{
|
|
|
|
m_data->menu->clear();
|
2023-03-06 16:37:32 +00:00
|
|
|
setCurrentIndex( -1 );
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskComboBox::setCurrentIndex( int index )
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
if ( m_data->currentIndex != index )
|
|
|
|
{
|
|
|
|
m_data->currentIndex = index;
|
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT currentIndexChanged( index );
|
|
|
|
}
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int QskComboBox::currentIndex() const
|
|
|
|
{
|
2023-03-06 16:37:32 +00:00
|
|
|
return m_data->currentIndex;
|
2023-02-14 07:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int QskComboBox::count() const
|
|
|
|
{
|
|
|
|
return m_data->menu->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskComboBox.cpp"
|