Add QskTristateCheckBox

This commit is contained in:
Clemens Manert 2022-04-18 12:36:17 +02:00
parent e70851dbc8
commit bba5ec1fa8
No known key found for this signature in database
GPG Key ID: 9197EAE8F85E3A18
8 changed files with 239 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <QskBox.h> #include <QskBox.h>
#include <QskCheckBox.h> #include <QskCheckBox.h>
#include <QskTristateCheckBox.h>
#include <QskDialogButton.h> #include <QskDialogButton.h>
#include <QskDialogButtonBox.h> #include <QskDialogButtonBox.h>
#include <QskFocusIndicator.h> #include <QskFocusIndicator.h>

View File

@ -9,6 +9,7 @@
#include <QskBox.h> #include <QskBox.h>
#include <QskCheckBox.h> #include <QskCheckBox.h>
#include <QskTristateCheckBox.h>
#include <QskDialogButton.h> #include <QskDialogButton.h>
#include <QskDialogButtonBox.h> #include <QskDialogButtonBox.h>
#include <QskFocusIndicator.h> #include <QskFocusIndicator.h>

View File

@ -32,6 +32,9 @@ QSK_QT_PRIVATE_END
#include "QskCheckBox.h" #include "QskCheckBox.h"
#include "QskCheckBoxSkinlet.h" #include "QskCheckBoxSkinlet.h"
#include "QskTristateCheckBox.h"
#include "QskTristateCheckBoxSkinlet.h"
#include "QskFocusIndicator.h" #include "QskFocusIndicator.h"
#include "QskFocusIndicatorSkinlet.h" #include "QskFocusIndicatorSkinlet.h"
@ -150,6 +153,7 @@ QskSkin::QskSkin( QObject* parent )
declareSkinlet< QskBox, QskBoxSkinlet >(); declareSkinlet< QskBox, QskBoxSkinlet >();
declareSkinlet< QskCheckBox, QskCheckBoxSkinlet >(); declareSkinlet< QskCheckBox, QskCheckBoxSkinlet >();
declareSkinlet< QskTristateCheckBox, QskTristateCheckBoxSkinlet >();
declareSkinlet< QskFocusIndicator, QskFocusIndicatorSkinlet >(); declareSkinlet< QskFocusIndicator, QskFocusIndicatorSkinlet >();
declareSkinlet< QskGraphicLabel, QskGraphicLabelSkinlet >(); declareSkinlet< QskGraphicLabel, QskGraphicLabelSkinlet >();
declareSkinlet< QskListView, QskListViewSkinlet >(); declareSkinlet< QskListView, QskListViewSkinlet >();

View File

@ -0,0 +1,66 @@
#include "QskTristateCheckBox.h"
#include "QskAspect.h"
#include <qset.h>
QSK_SYSTEM_STATE( QskTristateCheckBox, PartiallyChecked, QskAspect::LastUserState << 2 )
class QskTristateCheckBox::PrivateData
{
public:
PrivateData()
: checkState( Qt::Unchecked )
, checkStateChanging( false )
{
}
Qt::CheckState checkState : 2;
bool checkStateChanging : 1;
};
QskTristateCheckBox::QskTristateCheckBox( QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
{
setAcceptHoverEvents( true );
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
connect( this, &QskAbstractButton::checkedChanged, this,
[ this ]( bool on ) { setCheckStateInternal( on ? Qt::Checked : Qt::Unchecked ); } );
}
QskTristateCheckBox::~QskTristateCheckBox()
{
}
Qt::CheckState QskTristateCheckBox::checkState() const
{
return m_data->checkState;
}
void QskTristateCheckBox::setCheckStateInternal( Qt::CheckState checkState )
{
if( m_data->checkStateChanging )
return;
setSkinStateFlag( PartiallyChecked, checkState == Qt::PartiallyChecked );
m_data->checkState = checkState;
Q_EMIT checkStateChanged( checkState );
}
void QskTristateCheckBox::setCheckState( Qt::CheckState checkState )
{
if( checkState == m_data->checkState )
return;
m_data->checkStateChanging = true;
setChecked( checkState == Qt::PartiallyChecked
|| checkState == Qt::Checked );
m_data->checkStateChanging = false;
setCheckStateInternal( checkState );
}
#include "moc_QskTristateCheckBox.cpp"

View File

@ -0,0 +1,38 @@
#ifndef QSK_TRISTATE_CHECK_BOX_H
#define QSK_TRISTATE_CHECK_BOX_H
#include "QskCheckBox.h"
class QSK_EXPORT QskTristateCheckBox : public QskCheckBox
{
Q_OBJECT
Q_PROPERTY( Qt::CheckState checkState READ checkState
WRITE setCheckState NOTIFY checkStateChanged FINAL )
using Inherited = QskCheckBox;
public:
QSK_STATES( PartiallyChecked )
QskTristateCheckBox( QQuickItem* parent = nullptr );
~QskTristateCheckBox() override;
Qt::CheckState checkState() const;
public Q_SLOTS:
void setCheckState( Qt::CheckState );
Q_SIGNALS:
void checkStateChanged( Qt::CheckState );
void tristateChanged( bool );
private:
void setCheckStateInternal( Qt::CheckState );
void updated();
class PrivateData;
std::unique_ptr< PrivateData > m_data;
};
#endif

View File

@ -0,0 +1,103 @@
#include "QskTristateCheckBoxSkinlet.h"
#include "QskTristateCheckBox.h"
#include "QskSGNode.h"
#include <QSGFlatColorMaterial>
#include <qsgnode.h>
namespace
{
class IndicatorNode : public QSGGeometryNode
{
public:
IndicatorNode()
: m_geometry( QSGGeometry::defaultAttributes_Point2D(), 3 )
{
m_geometry.setDrawingMode( QSGGeometry::DrawLineStrip );
m_geometry.setLineWidth( 2 );
setGeometry( &m_geometry );
setMaterial( &m_material );
}
void update( bool isPartially, const QRectF& rect, const QColor& color )
{
if ( color != m_material.color() )
{
m_material.setColor( color );
markDirty( QSGNode::DirtyMaterial );
}
if ( rect != m_rect || isPartially != m_isPartially )
{
m_rect = rect;
m_isPartially = isPartially;
const auto x = rect.x();
const auto y = rect.y();
const auto w = rect.width();
const auto h = rect.height();
auto points = m_geometry.vertexDataAsPoint2D();
if ( isPartially )
{
points[0].set( x, y + h / 2 );
points[1] = points[0];
points[2].set( x + w, y + h / 2 );
}
else
{
points[0].set( x, y + h / 2 );
points[1].set( x + w / 3, y + h );
points[2].set( x + w, y );
}
markDirty( QSGNode::DirtyGeometry );
}
}
private:
QSGFlatColorMaterial m_material;
QSGGeometry m_geometry;
QRectF m_rect;
bool m_isPartially;
};
}
QskTristateCheckBoxSkinlet::QskTristateCheckBoxSkinlet( QskSkin* skin )
: Inherited( skin )
{
}
QskTristateCheckBoxSkinlet::~QskTristateCheckBoxSkinlet()
{
}
QSGNode* QskTristateCheckBoxSkinlet::updateIndicatorNode(
const QskCheckBox* checkBox, QSGNode* node ) const
{
using Q = QskCheckBox;
const auto tristate = qobject_cast< const QskTristateCheckBox* >( checkBox );
if ( checkBox->isChecked() == false )
return nullptr;
auto rect = checkBox->subControlRect(Q::IndicatorTic)
.marginsRemoved(checkBox->marginHint(Q::IndicatorTic) );
if ( rect.isEmpty() )
return nullptr;
auto indicatorNode = QskSGNode::ensureNode< IndicatorNode >( node );
indicatorNode->update(
tristate->checkState() == Qt::CheckState::PartiallyChecked, rect,
checkBox->color( Q::IndicatorTic ) );
return indicatorNode;
}
#include "moc_QskTristateCheckBoxSkinlet.cpp"

View File

@ -0,0 +1,22 @@
#ifndef QSK_TRISTATE_CHECK_BOX_SKINLET_H
#define QSK_TRISTATE_CHECK_BOX_SKINLET_H
#include "QskCheckBoxSkinlet.h"
class QskCheckBox;
class QSK_EXPORT QskTristateCheckBoxSkinlet : public QskCheckBoxSkinlet
{
Q_GADGET
using Inherited = QskCheckBoxSkinlet;
public:
Q_INVOKABLE QskTristateCheckBoxSkinlet( QskSkin* = nullptr );
~QskTristateCheckBoxSkinlet() override;
protected:
QSGNode* updateIndicatorNode( const QskCheckBox*, QSGNode* ) const override;
};
#endif

View File

@ -145,6 +145,8 @@ HEADERS += \
controls/QskBoxSkinlet.h \ controls/QskBoxSkinlet.h \
controls/QskCheckBox.h \ controls/QskCheckBox.h \
controls/QskCheckBoxSkinlet.h \ controls/QskCheckBoxSkinlet.h \
controls/QskTristateCheckBox.h \
controls/QskTristateCheckBoxSkinlet.h \
controls/QskControl.h \ controls/QskControl.h \
controls/QskControlPrivate.h \ controls/QskControlPrivate.h \
controls/QskDirtyItemFilter.h \ controls/QskDirtyItemFilter.h \
@ -229,6 +231,8 @@ SOURCES += \
controls/QskBoxSkinlet.cpp \ controls/QskBoxSkinlet.cpp \
controls/QskCheckBox.cpp \ controls/QskCheckBox.cpp \
controls/QskCheckBoxSkinlet.cpp \ controls/QskCheckBoxSkinlet.cpp \
controls/QskTristateCheckBox.cpp \
controls/QskTristateCheckBoxSkinlet.cpp \
controls/QskControl.cpp \ controls/QskControl.cpp \
controls/QskControlPrivate.cpp \ controls/QskControlPrivate.cpp \
controls/QskDirtyItemFilter.cpp \ controls/QskDirtyItemFilter.cpp \