qskinny/src/controls/QskSpinBox.cpp

178 lines
4.1 KiB
C++
Raw Normal View History

2023-02-17 11:01:56 +00:00
/******************************************************************************
2023-02-17 13:05:05 +00:00
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
2023-02-17 11:01:56 +00:00
*****************************************************************************/
#include "QskSpinBox.h"
2023-02-19 10:29:13 +00:00
#include "QskIntervalF.h"
#include "QskEvent.h"
QSK_SUBCONTROL( QskSpinBox, Panel )
2023-02-17 13:05:05 +00:00
QSK_SUBCONTROL( QskSpinBox, TextPanel )
QSK_SUBCONTROL( QskSpinBox, Text )
2023-02-17 11:01:56 +00:00
2023-02-17 14:22:40 +00:00
QSK_SUBCONTROL( QskSpinBox, IncrementPanel )
QSK_SUBCONTROL( QskSpinBox, IncrementIndicator )
2023-02-17 14:22:40 +00:00
QSK_SUBCONTROL( QskSpinBox, DecrementPanel )
QSK_SUBCONTROL( QskSpinBox, DecrementIndicator )
2023-02-17 11:01:56 +00:00
2023-02-17 14:22:40 +00:00
QSK_SYSTEM_STATE( QskSpinBox, Pressed, ( QskAspect::QskAspect::FirstSystemState << 0 ) )
2023-02-17 11:01:56 +00:00
namespace
2023-02-17 12:30:39 +00:00
{
enum
2023-02-17 11:01:56 +00:00
{
ButtonDecrement = -1,
ButtonNone = 0,
ButtonIncrement = 1
};
2023-02-17 14:22:40 +00:00
inline int buttonAt( const QskSpinBox* spinBox, const QPointF& pos )
2023-02-17 14:22:40 +00:00
{
if ( spinBox->subControlRect( QskSpinBox::IncrementPanel ).contains( pos ) )
return ButtonIncrement;
2023-02-17 14:22:40 +00:00
if ( spinBox->subControlRect( QskSpinBox::DecrementPanel ).contains( pos ) )
return ButtonDecrement;
2023-02-17 14:22:40 +00:00
return ButtonNone;
2023-02-17 14:22:40 +00:00
}
}
2023-02-17 14:22:40 +00:00
class QskSpinBox::PrivateData
{
public:
inline void setPressed( QskSpinBox* spinBox, int button )
2023-02-17 14:22:40 +00:00
{
if ( pressedButton != button )
2023-02-17 14:22:40 +00:00
{
pressedButton = button;
spinBox->update();
2023-02-17 14:22:40 +00:00
}
}
inline void setHovered( QskSpinBox* spinBox, int button )
2023-02-17 14:22:40 +00:00
{
if ( hoveredButton != button )
2023-02-17 14:22:40 +00:00
{
hoveredButton = button;
spinBox->update();
2023-02-17 14:22:40 +00:00
}
}
int pressedButton = ButtonNone;
int hoveredButton = ButtonNone;
2023-02-17 14:22:40 +00:00
// int decimals; ???
2023-02-17 11:01:56 +00:00
};
2023-02-17 14:22:40 +00:00
QskSpinBox::QskSpinBox( QQuickItem* const parent )
: Inherited( parent )
, m_data( new PrivateData )
2023-02-17 11:01:56 +00:00
{
setBoundaries( 0.0, 99.99 ); // this is what QDoubleSpinBox does
2023-02-17 14:22:40 +00:00
setAcceptHoverEvents( true );
setAcceptedMouseButtons( Qt::LeftButton );
setFocusPolicy( Qt::StrongFocus );
2023-02-17 11:01:56 +00:00
}
QskSpinBox::~QskSpinBox()
2023-02-17 11:01:56 +00:00
{
}
void QskSpinBox::hoverEnterEvent( QHoverEvent* )
2023-02-17 11:01:56 +00:00
{
}
void QskSpinBox::hoverLeaveEvent( QHoverEvent* )
2023-02-17 11:01:56 +00:00
{
m_data->setHovered( this, ButtonNone );
2023-02-17 11:01:56 +00:00
}
void QskSpinBox::hoverMoveEvent( QHoverEvent* event )
2023-02-17 11:01:56 +00:00
{
const auto button = buttonAt( this, qskHoverPosition( event ) );
m_data->setHovered( this, button );
}
2023-02-17 11:01:56 +00:00
void QskSpinBox::mousePressEvent( QMouseEvent* event )
{
if ( const auto button = buttonAt( this, qskMousePosition( event ) ) )
2023-02-17 14:22:40 +00:00
{
m_data->setPressed( this, button );
increment( stepSize() * button );
2023-02-17 11:01:56 +00:00
2023-02-17 14:22:40 +00:00
return;
}
Inherited::mousePressEvent( event );
2023-02-17 11:01:56 +00:00
}
void QskSpinBox::mouseReleaseEvent( QMouseEvent* )
2023-02-17 11:01:56 +00:00
{
m_data->setPressed( this, ButtonNone );
2023-02-17 11:01:56 +00:00
}
2023-02-17 14:22:40 +00:00
void QskSpinBox::keyPressEvent( QKeyEvent* const event )
2023-02-17 11:01:56 +00:00
{
2023-02-17 14:22:40 +00:00
switch ( event->key() )
2023-02-17 11:01:56 +00:00
{
2023-02-17 14:22:40 +00:00
case Qt::Key_Minus:
case Qt::Key_Down:
case Qt::Key_Left:
{
2023-02-17 14:22:40 +00:00
increment( -stepSize() );
m_data->setPressed( this, ButtonDecrement );
2023-02-17 14:22:40 +00:00
return;
2023-02-17 11:01:56 +00:00
}
2023-02-17 13:37:10 +00:00
case Qt::Key_Plus:
case Qt::Key_Up:
case Qt::Key_Right:
2023-02-17 11:01:56 +00:00
{
increment( +stepSize() );
m_data->setPressed( this, ButtonIncrement );
return;
2023-02-17 11:01:56 +00:00
}
2023-02-17 13:37:10 +00:00
#if 1
case Qt::Key_Select:
case Qt::Key_Space:
/*
All keys to navigate along the focus tab chain are not valid
for a spin box, as it accepts number inputs only. Guess this is why
QSpinBox goes straight into edit mode when receiving the focus
So once setting values by keyboard is implemented we have to decide
how to do it here. TODO ...
*/
return;
#endif
default:
break;
2023-02-17 11:01:56 +00:00
}
2023-02-17 14:22:40 +00:00
Inherited::keyPressEvent( event );
2023-02-17 11:01:56 +00:00
}
2023-02-17 14:22:40 +00:00
void QskSpinBox::keyReleaseEvent( QKeyEvent* const event )
2023-02-17 11:01:56 +00:00
{
m_data->setPressed( this, ButtonNone );
2023-02-17 14:22:40 +00:00
Inherited::keyReleaseEvent( event );
2023-02-17 11:01:56 +00:00
}
int QskSpinBox::pressedButton() const
2023-02-17 11:01:56 +00:00
{
return m_data->pressedButton;
2023-02-17 11:01:56 +00:00
}
int QskSpinBox::hoveredButton() const
2023-02-17 12:30:39 +00:00
{
return m_data->hoveredButton;
2023-02-17 12:30:39 +00:00
}