2019-02-26 21:52:02 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the 3-clause BSD License
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "MyToggleButton.h"
|
|
|
|
|
|
|
|
#include <QskAspect.h>
|
|
|
|
#include <QskGraphic.h>
|
|
|
|
#include <QskGraphicProvider.h>
|
|
|
|
#include <QskTextOptions.h>
|
|
|
|
|
|
|
|
QSK_SUBCONTROL( MyToggleButton, Panel )
|
|
|
|
QSK_SUBCONTROL( MyToggleButton, Cursor )
|
|
|
|
QSK_SUBCONTROL( MyToggleButton, CheckedPanel )
|
2021-12-27 18:47:46 +01:00
|
|
|
QSK_SUBCONTROL( MyToggleButton, CheckedText )
|
2019-02-26 21:52:02 +01:00
|
|
|
QSK_SUBCONTROL( MyToggleButton, UncheckedPanel )
|
2021-12-27 18:47:46 +01:00
|
|
|
QSK_SUBCONTROL( MyToggleButton, UncheckedText )
|
|
|
|
QSK_SUBCONTROL( MyToggleButton, CheckedGraphic )
|
|
|
|
QSK_SUBCONTROL( MyToggleButton, UncheckedGraphic )
|
2019-02-26 21:52:02 +01:00
|
|
|
|
|
|
|
class MyToggleButton::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
|
|
|
|
QString iconSource;
|
|
|
|
QskGraphic icon;
|
|
|
|
bool iconDirty = false;
|
|
|
|
|
|
|
|
} content[ 2 ];
|
|
|
|
|
|
|
|
QskTextOptions textOptions;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Normal: Checked: left, Unchecked : right
|
|
|
|
Inverted: Checked: right, Unchecked : left
|
|
|
|
*/
|
|
|
|
bool inverted = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
MyToggleButton::MyToggleButton( QQuickItem* parent )
|
|
|
|
: Inherited( parent )
|
|
|
|
, m_data( new PrivateData() )
|
|
|
|
{
|
|
|
|
setAcceptHoverEvents( false );
|
|
|
|
|
|
|
|
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
|
|
|
|
}
|
|
|
|
|
|
|
|
MyToggleButton::~MyToggleButton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-04 08:24:36 +02:00
|
|
|
bool MyToggleButton::isCheckable() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:52:02 +01:00
|
|
|
void MyToggleButton::setInverted( bool on )
|
|
|
|
{
|
|
|
|
if ( m_data->inverted != on )
|
|
|
|
{
|
|
|
|
m_data->inverted = on;
|
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT invertedChanged( on );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyToggleButton::isInverted() const
|
|
|
|
{
|
|
|
|
return m_data->inverted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyToggleButton::setTextOptions( const QskTextOptions& options )
|
|
|
|
{
|
|
|
|
if( options != m_data->textOptions )
|
|
|
|
{
|
|
|
|
m_data->textOptions = options;
|
|
|
|
|
|
|
|
if( !( m_data->content[ 0 ].text.isEmpty() && m_data->content[ 1 ].text.isEmpty() ) )
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QskTextOptions MyToggleButton::textOptions() const
|
|
|
|
{
|
|
|
|
return m_data->textOptions;
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:47:46 +01:00
|
|
|
void MyToggleButton::setText( bool isChecked, const QString& text )
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
auto& data = m_data->content[ isChecked ];
|
2019-02-26 21:52:02 +01:00
|
|
|
|
|
|
|
if( !data.icon.isNull() )
|
|
|
|
{
|
|
|
|
// we don't support text + graphic
|
|
|
|
data.icon.reset();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
data.iconDirty = false;
|
|
|
|
|
|
|
|
if( text != data.text )
|
|
|
|
{
|
|
|
|
data.text = text;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:47:46 +01:00
|
|
|
QString MyToggleButton::text( bool isChecked ) const
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
return m_data->content[ isChecked ].text;
|
2019-02-26 21:52:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-27 18:47:46 +01:00
|
|
|
void MyToggleButton::setIcon( bool isChecked, const QString& icon )
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
auto& data = m_data->content[ isChecked ];
|
2019-02-26 21:52:02 +01:00
|
|
|
|
|
|
|
if( !data.text.isEmpty() )
|
|
|
|
{
|
|
|
|
// we don't support text + graphic
|
|
|
|
data.text.clear();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( icon != data.iconSource )
|
|
|
|
{
|
|
|
|
data.icon.reset();
|
|
|
|
data.iconSource = icon;
|
|
|
|
data.iconDirty = true;
|
|
|
|
|
|
|
|
polish();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:47:46 +01:00
|
|
|
QString MyToggleButton::icon( bool isChecked ) const
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
return m_data->content[ isChecked ].iconSource;
|
2019-02-26 21:52:02 +01:00
|
|
|
}
|
|
|
|
|
2021-12-27 18:47:46 +01:00
|
|
|
QskGraphic MyToggleButton::graphic( bool isChecked ) const
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
auto& data = const_cast< MyToggleButton* >( this )->m_data->content[ isChecked ];
|
2019-02-26 21:52:02 +01:00
|
|
|
|
|
|
|
if( data.iconDirty )
|
|
|
|
{
|
|
|
|
data.icon = Qsk::loadGraphic( data.iconSource );
|
|
|
|
data.iconDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.icon;
|
|
|
|
}
|
|
|
|
|
2019-12-15 13:34:07 +01:00
|
|
|
void MyToggleButton::updateResources()
|
2019-02-26 21:52:02 +01:00
|
|
|
{
|
2021-12-27 18:47:46 +01:00
|
|
|
(void) graphic( false );
|
|
|
|
(void) graphic( true );
|
2019-02-26 21:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_MyToggleButton.cpp"
|