qskinny/src/controls/QskBoundedInput.cpp

243 lines
5.0 KiB
C++
Raw Normal View History

2020-07-25 14:19:14 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Clause
2020-07-25 14:19:14 +02:00
*****************************************************************************/
#include "QskBoundedInput.h"
#include "QskFunctions.h"
#include "QskIntervalF.h"
2022-01-05 11:59:32 +01:00
#include "QskEvent.h"
2020-07-25 14:19:14 +02:00
2020-07-25 18:46:04 +02:00
#include <cmath>
2020-07-25 14:19:14 +02:00
QSK_SYSTEM_STATE( QskBoundedInput, ReadOnly, ( QskAspect::FirstSystemState << 1 ) )
QskBoundedInput::QskBoundedInput( QQuickItem* parent )
2020-07-31 07:41:25 +02:00
: Inherited( parent )
, m_stepSize( 0.1 )
, m_pageSize( 1 )
, m_snap( false )
2020-07-25 14:19:14 +02:00
{
setFocusPolicy( Qt::StrongFocus );
setAcceptedMouseButtons( Qt::LeftButton );
setWheelEnabled( true );
if ( isComponentComplete() )
{
2020-07-31 07:41:25 +02:00
connect( this, &QskBoundedControl::boundariesChanged,
this, &QskBoundedInput::alignInput );
2020-07-25 14:19:14 +02:00
}
}
2020-07-31 07:41:25 +02:00
QskBoundedInput::~QskBoundedInput()
2020-07-25 14:19:14 +02:00
{
}
void QskBoundedInput::setStepSize( qreal stepSize )
{
2020-07-25 18:46:04 +02:00
if ( qFuzzyIsNull( stepSize ) )
stepSize = 0.0;
2020-07-31 07:41:25 +02:00
if ( qFuzzyCompare( m_stepSize, stepSize ) )
2020-07-25 14:19:14 +02:00
return;
2020-07-31 07:41:25 +02:00
m_stepSize = stepSize;
2020-07-25 14:19:14 +02:00
Q_EMIT stepSizeChanged( stepSize );
2020-07-25 18:46:04 +02:00
if ( isComponentComplete() )
{
2020-07-31 07:41:25 +02:00
if ( m_snap && stepSize )
2020-07-25 18:46:04 +02:00
alignInput();
}
2020-07-25 14:19:14 +02:00
}
qreal QskBoundedInput::stepSize() const
{
2020-07-31 07:41:25 +02:00
return m_stepSize;
2020-07-25 14:19:14 +02:00
}
void QskBoundedInput::setPageSize( int pageSize )
{
2020-07-31 07:41:25 +02:00
if ( m_pageSize == pageSize )
2020-07-25 14:19:14 +02:00
return;
2020-07-31 07:41:25 +02:00
m_pageSize = pageSize;
2020-07-25 14:19:14 +02:00
Q_EMIT pageSizeChanged( pageSize );
}
int QskBoundedInput::pageSize() const
{
2020-07-31 07:41:25 +02:00
return m_pageSize;
2020-07-25 14:19:14 +02:00
}
void QskBoundedInput::stepUp()
{
2020-07-31 07:41:25 +02:00
increment( m_stepSize );
2020-08-09 11:50:34 +02:00
}
2020-07-25 14:19:14 +02:00
void QskBoundedInput::stepDown()
{
2020-07-31 07:41:25 +02:00
increment( -m_stepSize );
2020-08-09 11:50:34 +02:00
}
2020-07-25 14:19:14 +02:00
void QskBoundedInput::pageUp()
{
2020-07-31 07:41:25 +02:00
increment( m_pageSize * m_stepSize );
2020-08-09 11:50:34 +02:00
}
2020-07-25 14:19:14 +02:00
void QskBoundedInput::pageDown()
{
2020-07-31 07:41:25 +02:00
increment( -m_pageSize * m_stepSize );
2020-07-25 14:19:14 +02:00
}
void QskBoundedInput::setSnap( bool snap )
{
2020-07-31 07:41:25 +02:00
if ( m_snap == snap )
2020-07-25 14:19:14 +02:00
return;
2020-07-31 07:41:25 +02:00
m_snap = snap;
2020-07-25 14:19:14 +02:00
Q_EMIT snapChanged( snap );
2020-07-29 07:25:35 +02:00
if ( isComponentComplete() && snap )
2020-07-25 14:19:14 +02:00
alignInput();
}
bool QskBoundedInput::snap() const
{
2020-07-31 07:41:25 +02:00
return m_snap;
}
void QskBoundedInput::componentComplete()
{
if ( isComponentComplete() )
{
connect( this, &QskBoundedControl::boundariesChanged,
this, &QskBoundedInput::alignInput, Qt::UniqueConnection );
}
Inherited::componentComplete();
2020-07-25 14:19:14 +02:00
}
2020-07-29 07:25:35 +02:00
void QskBoundedInput::alignInput()
{
}
2020-07-25 18:46:04 +02:00
qreal QskBoundedInput::alignedValue( qreal value ) const
{
value = boundedValue( value );
if ( value > minimum() && value < maximum() )
2020-07-25 18:46:04 +02:00
{
if ( m_snap && m_stepSize )
{
value = qRound( value / m_stepSize ) * m_stepSize;
value = boundedValue( value );
}
2020-07-25 18:46:04 +02:00
}
2020-08-09 11:50:34 +02:00
return value;
2020-07-25 18:46:04 +02:00
}
QskIntervalF QskBoundedInput::alignedInterval( const QskIntervalF& interval ) const
{
2020-07-31 07:41:25 +02:00
if ( m_snap )
2020-07-25 18:46:04 +02:00
{
2020-07-31 07:41:25 +02:00
if ( const auto step = m_stepSize )
2020-07-25 18:46:04 +02:00
{
const qreal lower = std::floor( interval.lowerBound() / step ) * step;
const qreal upper = std::ceil( interval.upperBound() / step ) * step;
return QskIntervalF( lower, upper );
}
}
return interval;
}
2020-07-25 14:19:14 +02:00
void QskBoundedInput::setReadOnly( bool readOnly )
{
if ( readOnly == isReadOnly() )
return;
setSkinStateFlag( ReadOnly, readOnly );
// we are killing user settings here !!
setFocusPolicy( readOnly ? Qt::NoFocus : Qt::StrongFocus );
setAcceptedMouseButtons( readOnly ? Qt::NoButton : Qt::LeftButton );
setWheelEnabled( !readOnly );
Q_EMIT readOnlyChanged( readOnly );
2021-02-09 12:25:41 +01:00
QEvent event( QEvent::ReadOnlyChange );
QCoreApplication::sendEvent( this, &event );
2020-07-25 14:19:14 +02:00
}
bool QskBoundedInput::isReadOnly() const
{
2021-08-30 15:30:41 +02:00
return hasSkinState( ReadOnly );
2020-07-25 14:19:14 +02:00
}
qreal QskBoundedInput::incrementForKey( const QKeyEvent* event ) const
2020-07-25 14:19:14 +02:00
{
switch( event->key() )
2020-07-25 14:19:14 +02:00
{
case Qt::Key_Up:
return m_stepSize;
case Qt::Key_Down:
return -m_stepSize;
case Qt::Key_PageUp:
return m_pageSize;
case Qt::Key_PageDown:
return -m_pageSize;
default:
2020-07-25 14:19:14 +02:00
{
if ( qskIsStandardKeyInput( event, QKeySequence::MoveToNextChar ) )
return m_stepSize;
if ( qskIsStandardKeyInput( event, QKeySequence::MoveToPreviousChar ) )
return -m_stepSize;
2020-07-25 14:19:14 +02:00
}
}
return 0.0;
}
2020-07-25 14:19:14 +02:00
void QskBoundedInput::keyPressEvent( QKeyEvent* event )
{
if ( !isReadOnly() )
{
if ( const auto offset = incrementForKey( event ) )
2020-07-25 14:19:14 +02:00
{
increment( offset );
2020-07-25 14:19:14 +02:00
return;
}
}
Inherited::keyPressEvent( event );
}
#ifndef QT_NO_WHEELEVENT
void QskBoundedInput::wheelEvent( QWheelEvent* event )
{
2022-01-10 08:46:20 +01:00
if ( isReadOnly() )
2022-01-05 11:59:32 +01:00
{
2022-01-10 08:46:20 +01:00
Inherited::wheelEvent( event );
2020-07-25 14:19:14 +02:00
return;
2022-01-05 11:59:32 +01:00
}
2020-07-25 14:19:14 +02:00
2022-01-10 14:59:22 +01:00
auto offset = qskWheelIncrement( event ) * m_stepSize;
2022-01-10 08:46:20 +01:00
if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier ) )
offset *= m_pageSize;
increment( offset );
2020-07-25 14:19:14 +02:00
}
#endif
#include "moc_QskBoundedInput.cpp"