QskSpinBox API improved

This commit is contained in:
Uwe Rathmann 2023-02-24 08:24:32 +01:00
parent 06ba85605b
commit a3e749157f
3 changed files with 53 additions and 4 deletions

View File

@ -7,6 +7,9 @@
#include "QskEvent.h" #include "QskEvent.h"
#include <qbasictimer.h> #include <qbasictimer.h>
#include <qlocale.h>
#include <cfloat>
QSK_SUBCONTROL( QskSpinBox, Panel ) QSK_SUBCONTROL( QskSpinBox, Panel )
@ -86,6 +89,8 @@ class QskSpinBox::PrivateData
this->repeatTimer.stop(); this->repeatTimer.stop();
} }
int decimals = 2;
int autoRepeatDelay = 300; int autoRepeatDelay = 300;
int autoRepeatInterval = 100; int autoRepeatInterval = 100;
@ -111,6 +116,8 @@ QskSpinBox::QskSpinBox( QQuickItem* parent )
setAcceptedMouseButtons( Qt::LeftButton ); setAcceptedMouseButtons( Qt::LeftButton );
setFocusPolicy( Qt::StrongFocus ); setFocusPolicy( Qt::StrongFocus );
connect( this, &QskSpinBox::valueChanged, this, &QskSpinBox::textChanged );
} }
QskSpinBox::~QskSpinBox() QskSpinBox::~QskSpinBox()
@ -173,6 +180,33 @@ bool QskSpinBox::isAccelerating() const
return m_data->accelerating; return m_data->accelerating;
} }
void QskSpinBox::setDecimals( int decimals )
{
decimals = qBound( 0, decimals, DBL_MAX_10_EXP + DBL_DIG );
if ( decimals != m_data->decimals )
{
m_data->decimals = decimals;
update();
resetImplicitSize();
}
}
int QskSpinBox::decimals() const
{
return m_data->decimals;
}
QString QskSpinBox::text() const
{
return textFromValue( value() );
}
QString QskSpinBox::textFromValue( qreal value ) const
{
return locale().toString( value, 'f', m_data->decimals );
}
void QskSpinBox::mousePressEvent( QMouseEvent* event ) void QskSpinBox::mousePressEvent( QMouseEvent* event )
{ {
if ( !isReadOnly() ) if ( !isReadOnly() )

View File

@ -25,6 +25,11 @@ class QSK_EXPORT QskSpinBox : public QskBoundedValueInput
Q_PROPERTY( bool buttons READ hasButtons Q_PROPERTY( bool buttons READ hasButtons
WRITE setButtons NOTIFY buttonsChanged ) WRITE setButtons NOTIFY buttonsChanged )
Q_PROPERTY( int decimals READ decimals
WRITE setDecimals NOTIFY decimalsChanged )
Q_PROPERTY( QString text READ text NOTIFY textChanged )
public: public:
QSK_SUBCONTROLS( Panel, TextPanel, Text, QSK_SUBCONTROLS( Panel, TextPanel, Text,
UpPanel, UpIndicator, DownPanel, DownIndicator ) UpPanel, UpIndicator, DownPanel, DownIndicator )
@ -46,11 +51,23 @@ class QSK_EXPORT QskSpinBox : public QskBoundedValueInput
void setAccelerating( bool ); void setAccelerating( bool );
bool isAccelerating() const; bool isAccelerating() const;
void setDecimals( int );
int decimals() const;
QString text() const;
Q_SIGNALS: Q_SIGNALS:
void buttonsChanged( bool );
void trackingChanged( bool ); void trackingChanged( bool );
void wrappingChanged( bool ); void wrappingChanged( bool );
void acceleratingChanged( bool ); void acceleratingChanged( bool );
void buttonsChanged( bool );
void decimalsChanged( int );
void textChanged();
protected:
virtual QString textFromValue( qreal ) const;
private: private:
void timerEvent( QTimerEvent* ) override; void timerEvent( QTimerEvent* ) override;

View File

@ -229,9 +229,7 @@ QSGNode* QskSpinBoxSkinlet::updateSubNode(
case Text: case Text:
{ {
const auto* const spinbox = static_cast< const QskSpinBox* >( skinnable ); const auto* const spinbox = static_cast< const QskSpinBox* >( skinnable );
return updateTextNode( skinnable, node, spinbox->text(), Q::Text );
return updateTextNode( skinnable, node,
QString::number( spinbox->value() ), Q::Text );
} }
} }