qskinny/src/controls/QskPushButton.cpp

322 lines
6.6 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskPushButton.h"
#include "QskAnimationHint.h"
2017-07-21 18:21:34 +02:00
#include "QskAspect.h"
#include "QskBoxShapeMetrics.h"
2017-07-21 18:21:34 +02:00
#include "QskGraphic.h"
#include "QskGraphicProvider.h"
#include "QskSetup.h"
2018-08-03 08:15:28 +02:00
#include "QskSkin.h"
2017-11-17 08:03:38 +01:00
#include "QskSkinlet.h"
2018-08-03 08:15:28 +02:00
#include "QskTextOptions.h"
#include "QskEvent.h"
2017-07-21 18:21:34 +02:00
QSK_SUBCONTROL( QskPushButton, Panel )
QSK_SUBCONTROL( QskPushButton, Splash )
2017-07-21 18:21:34 +02:00
QSK_SUBCONTROL( QskPushButton, Text )
QSK_SUBCONTROL( QskPushButton, Icon )
2017-07-21 18:21:34 +02:00
class QskPushButton::PrivateData
{
2018-08-03 08:15:28 +02:00
public:
PrivateData( const QString& txt )
: text( txt )
, isCheckable( false )
, isIconSourceDirty( false )
, emphasis( NoEmphasis )
2017-07-21 18:21:34 +02:00
{
}
void ensureIcon( const QskPushButton* button )
{
if ( isIconSourceDirty )
{
if ( !iconSource.isEmpty() )
icon = button->loadIcon( iconSource );
isIconSourceDirty = false;
}
}
2017-07-21 18:21:34 +02:00
QString text;
QUrl iconSource;
QskGraphic icon;
2017-07-21 18:21:34 +02:00
bool isCheckable : 1;
bool isIconSourceDirty : 1;
2023-02-20 10:30:24 +01:00
int emphasis : 4;
2017-07-21 18:21:34 +02:00
};
2018-08-03 08:15:28 +02:00
QskPushButton::QskPushButton( QQuickItem* parent )
: QskPushButton( QString(), parent )
2017-07-21 18:21:34 +02:00
{
}
2018-08-03 08:15:28 +02:00
QskPushButton::QskPushButton( const QString& text, QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData( text ) )
2017-07-21 18:21:34 +02:00
{
initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
2017-07-21 18:21:34 +02:00
}
QskPushButton::~QskPushButton()
{
}
void QskPushButton::setCheckable( bool on )
{
if ( on != m_data->isCheckable )
{
m_data->isCheckable = on;
Q_EMIT checkableChanged( on );
}
}
bool QskPushButton::isCheckable() const
{
return m_data->isCheckable;
}
2023-02-20 10:30:24 +01:00
void QskPushButton::setEmphasis( Emphasis emphasis )
{
if ( emphasis != m_data->emphasis )
{
m_data->emphasis = emphasis;
Q_EMIT emphasisChanged( emphasis );
}
}
QskPushButton::Emphasis QskPushButton::emphasis() const
{
return static_cast< Emphasis >( m_data->emphasis );
}
void QskPushButton::setShape( const QskBoxShapeMetrics& shape )
2017-07-21 18:21:34 +02:00
{
if ( setBoxShapeHint( Panel, shape ) )
Q_EMIT shapeChanged();
2017-07-21 18:21:34 +02:00
}
void QskPushButton::resetShape()
2017-07-21 18:21:34 +02:00
{
if ( resetBoxShapeHint( Panel ) )
Q_EMIT shapeChanged();
}
2017-07-21 18:21:34 +02:00
QskBoxShapeMetrics QskPushButton::shape() const
{
return boxShapeHint( Panel );
2017-07-21 18:21:34 +02:00
}
void QskPushButton::setText( const QString& text )
{
if ( text != m_data->text )
{
m_data->text = text;
resetImplicitSize();
update();
Q_EMIT textChanged();
}
}
QString QskPushButton::text() const
{
return m_data->text;
}
2022-08-25 09:39:33 +02:00
void QskPushButton::setTextOptions( const QskTextOptions& textOptions )
2017-07-21 18:21:34 +02:00
{
2022-08-25 09:39:33 +02:00
if ( setTextOptionsHint( Text, textOptions ) )
2017-07-21 18:21:34 +02:00
Q_EMIT textOptionsChanged();
}
QskTextOptions QskPushButton::textOptions() const
{
2022-08-25 09:39:33 +02:00
return textOptionsHint( Text );
}
void QskPushButton::resetTextOptions()
{
if ( resetTextOptionsHint( Text ) )
Q_EMIT textOptionsChanged();
2017-07-21 18:21:34 +02:00
}
QFont QskPushButton::font() const
{
return effectiveFont( Text );
2017-07-21 18:21:34 +02:00
}
void QskPushButton::resetIconStrutSize()
{
if ( resetStrutSizeHint( Icon ) )
Q_EMIT iconStrutSizeChanged();
}
void QskPushButton::setIconStrutSize( const QSizeF& size )
{
auto newSize = size;
if ( newSize.width() < 0.0 )
newSize.setWidth( -1.0 );
if ( newSize.height() < 0.0 )
newSize.setHeight( -1.0 );
if ( setStrutSizeHint( Icon, newSize ) )
Q_EMIT iconStrutSizeChanged();
}
QSizeF QskPushButton::iconStrutSize() const
{
return strutSizeHint( Icon );
}
void QskPushButton::setIconSource( const QUrl& url )
2017-07-21 18:21:34 +02:00
{
if ( m_data->iconSource == url )
2017-07-21 18:21:34 +02:00
return;
m_data->iconSource = url;
m_data->icon.reset();
2017-07-21 18:21:34 +02:00
m_data->isIconSourceDirty = true;
2017-07-21 18:21:34 +02:00
resetImplicitSize();
polish();
update();
Q_EMIT iconSourceChanged();
2017-07-21 18:21:34 +02:00
}
void QskPushButton::setIconSource( const QString& source )
{
setIconSource( QUrl( source ) );
}
QUrl QskPushButton::iconSource() const
2017-07-21 18:21:34 +02:00
{
return m_data->iconSource;
2017-07-21 18:21:34 +02:00
}
void QskPushButton::setIcon( const QskGraphic& icon )
2017-07-21 18:21:34 +02:00
{
if ( icon != m_data->icon )
2017-07-21 18:21:34 +02:00
{
m_data->icon = icon;
2017-07-21 18:21:34 +02:00
if ( !m_data->iconSource.isEmpty() )
2017-07-21 18:21:34 +02:00
{
m_data->iconSource = QString();
m_data->isIconSourceDirty = false;
2017-07-21 18:21:34 +02:00
Q_EMIT iconSourceChanged();
2017-07-21 18:21:34 +02:00
}
Q_EMIT iconChanged();
2017-07-21 18:21:34 +02:00
resetImplicitSize();
polish();
update();
}
}
QskGraphic QskPushButton::icon() const
2017-07-21 18:21:34 +02:00
{
m_data->ensureIcon( this );
return m_data->icon;
2017-07-21 18:21:34 +02:00
}
bool QskPushButton::hasIcon() const
2017-07-21 18:21:34 +02:00
{
return !( icon().isEmpty() && iconSource().isEmpty() );
2017-07-21 18:21:34 +02:00
}
void QskPushButton::updateResources()
2017-07-21 18:21:34 +02:00
{
m_data->ensureIcon( this );
2017-07-21 18:21:34 +02:00
}
QskAspect::Variation QskPushButton::effectiveVariation() const
{
2023-02-20 10:30:24 +01:00
switch( m_data->emphasis )
{
case VeryLowEmphasis:
2023-02-20 10:30:24 +01:00
return QskAspect::Tiny;
case LowEmphasis:
2023-02-20 10:30:24 +01:00
return QskAspect::Small;
case HighEmphasis:
2023-02-20 10:30:24 +01:00
return QskAspect::Large;
case VeryHighEmphasis:
2023-02-20 10:30:24 +01:00
return QskAspect::Huge;
default:
return QskAspect::NoVariation;
}
}
QRectF QskPushButton::layoutRectForSize( const QSizeF& size ) const
2017-11-17 08:03:38 +01:00
{
2020-12-29 12:57:03 +01:00
return subControlContentsRect( size, Panel );
2017-11-17 08:03:38 +01:00
}
2017-07-21 18:21:34 +02:00
void QskPushButton::changeEvent( QEvent* event )
{
2018-08-03 08:15:28 +02:00
switch ( event->type() )
2017-07-21 18:21:34 +02:00
{
case QEvent::StyleChange:
{
if ( !m_data->iconSource.isEmpty() &&
2018-08-03 08:15:28 +02:00
qskSetup->skin()->hasGraphicProvider() )
2017-07-21 18:21:34 +02:00
{
// we might need to reload from a different skin
m_data->isIconSourceDirty = true;
2017-07-21 18:21:34 +02:00
}
break;
}
case QEvent::LocaleChange:
{
if ( !m_data->text.isEmpty() )
{
// maybe QLocale::textDirection() has changed
update();
}
break;
}
default:
break;
}
Inherited::changeEvent( event );
}
void QskPushButton::mousePressEvent( QMouseEvent* event )
{
Inherited::mousePressEvent( event );
2022-07-17 16:01:46 +02:00
using A = QskAspect;
const auto hint = animationHint( Splash | A::Color );
if( hint.isValid() )
{
setPositionHint( Splash, qskMousePosition( event ).x() );
startTransition( Splash | A::Metric | A::Size, hint, 0.0, 1.0 );
}
}
QskGraphic QskPushButton::loadIcon( const QUrl& url ) const
2017-07-21 18:21:34 +02:00
{
return Qsk::loadGraphic( url );
}
#include "moc_QskPushButton.cpp"