qskinny/src/controls/QskSwitchButtonSkinlet.cpp

125 lines
3.8 KiB
C++
Raw Normal View History

2021-08-02 19:17:04 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
2021-08-02 13:22:37 +02:00
#include "QskSwitchButton.h"
#include "QskSwitchButtonSkinlet.h"
#include "QskSGNode.h"
2021-08-02 19:17:04 +02:00
QskSwitchButtonSkinlet::QskSwitchButtonSkinlet( QskSkin* skin )
2021-08-02 13:22:37 +02:00
: Inherited( skin )
{
2021-08-02 19:17:04 +02:00
setNodeRoles( { GrooveRole, HandleRole } );
2021-08-02 13:22:37 +02:00
}
2021-08-02 19:17:04 +02:00
QskSwitchButtonSkinlet::~QskSwitchButtonSkinlet()
{
}
2021-08-02 13:22:37 +02:00
QRectF QskSwitchButtonSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl) const
{
using Q = QskSwitchButton;
2021-08-02 19:17:04 +02:00
2021-08-02 13:22:37 +02:00
const auto switchButton = static_cast< const Q* >( skinnable );
2021-08-02 19:17:04 +02:00
if ( subControl == Q::Handle )
2021-08-02 13:22:37 +02:00
{
2021-08-02 19:17:04 +02:00
const auto diameter = 2 * skinnable->metric( Q::Handle | QskAspect::Size );
const auto grooveSize = skinnable->strutSizeHint( Q::Groove );
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
auto position = skinnable->metric( Q::Handle | QskAspect::Position );
if( switchButton->isInverted() )
position = 1.0 - position;
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
auto rect = QRectF( 0, 0, diameter, diameter );
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
if( switchButton->orientation() == Qt::Vertical )
2021-08-02 13:22:37 +02:00
{
2021-08-02 19:17:04 +02:00
if( diameter < grooveSize.height() )
rect.moveLeft( ( grooveSize.height() - diameter ) / 2 );
2021-08-02 13:22:37 +02:00
rect.moveTop( ( grooveSize.height() - diameter ) / 2
+ position * ( grooveSize.width() - diameter
- ( grooveSize.height() - diameter ) ) );
}
else
{
2021-08-02 19:17:04 +02:00
if( switchButton->layoutMirroring() )
position = 1.0 - position;
if( diameter < grooveSize.height() )
rect.moveTop( ( grooveSize.height() - diameter ) / 2 );
2021-08-02 13:22:37 +02:00
rect.moveLeft( ( grooveSize.height() - diameter ) / 2
+ position * ( grooveSize.width() - diameter
- ( grooveSize.height() - diameter ) ) );
}
return rect;
}
2021-08-02 19:17:04 +02:00
if ( subControl == Q::Groove )
2021-08-02 13:22:37 +02:00
{
2021-08-02 19:17:04 +02:00
auto diameter = 2 * skinnable->metric( Q::Handle | QskAspect::Size );
const auto grooveSize = skinnable->strutSizeHint( Q::Groove );
2021-08-02 13:22:37 +02:00
auto result = contentsRect;
2021-08-02 19:17:04 +02:00
result.setSize( grooveSize );
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
if( switchButton->orientation() == Qt::Vertical )
2021-08-02 13:22:37 +02:00
{
2021-08-02 19:17:04 +02:00
if( grooveSize.height() < diameter )
result.moveLeft( ( diameter - result.height() ) / 2 );
2021-08-02 13:22:37 +02:00
return result.transposed();
}
else
{
2021-08-02 19:17:04 +02:00
if( grooveSize.height() < diameter )
result.moveTop( ( diameter - result.height() ) / 2 );
2021-08-02 13:22:37 +02:00
return result;
}
}
return Inherited::subControlRect( skinnable, contentsRect, subControl );
}
QSizeF QskSwitchButtonSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint, const QSizeF&) const
{
2021-08-02 19:17:04 +02:00
using Q = QskSwitchButton;
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
const auto switchButton = static_cast< const Q* >( skinnable );
const auto diameter = 2 * skinnable->metric( Q::Handle | QskAspect::Size );
auto hint = skinnable->strutSizeHint( Q::Groove );
hint = hint.expandedTo( QSizeF( diameter, diameter ) );
if( switchButton->orientation() == Qt::Vertical )
hint.transpose();
2021-08-02 13:22:37 +02:00
2021-08-02 19:17:04 +02:00
return hint;
2021-08-02 13:22:37 +02:00
}
QSGNode* QskSwitchButtonSkinlet::updateSubNode( const QskSkinnable* skinnable,
quint8 nodeRole, QSGNode* node) const
{
2021-08-02 19:17:04 +02:00
using Q = QskSwitchButton;
2021-08-02 13:22:37 +02:00
switch ( nodeRole )
{
2021-08-02 19:17:04 +02:00
case HandleRole:
return updateBoxNode( skinnable, node, Q::Handle );
2021-08-02 13:22:37 +02:00
case GrooveRole:
2021-08-02 19:17:04 +02:00
return updateBoxNode( skinnable, node, Q::Groove );
2021-08-02 13:22:37 +02:00
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
2021-08-02 19:17:04 +02:00
#include "moc_QskSwitchButtonSkinlet.cpp"