QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskCheckBoxSkinlet.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskCheckBoxSkinlet.h"
7#include "QskCheckBox.h"
8#include "QskTextOptions.h"
9#include "QskFunctions.h"
10
11QskCheckBoxSkinlet::QskCheckBoxSkinlet( QskSkin* skin )
12 : QskSkinlet( skin )
13{
14 setNodeRoles( { BoxRole, IndicatorRole, TextRole } );
15}
16
17QskCheckBoxSkinlet::~QskCheckBoxSkinlet()
18{
19}
20
21QRectF QskCheckBoxSkinlet::subControlRect( const QskSkinnable* skinnable,
22 const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
23{
24 using Q = QskCheckBox;
25
26 const auto checkBox = static_cast< const QskCheckBox* >( skinnable );
27
28 if ( subControl == Q::Panel )
29 return contentsRect;
30
31 if ( subControl == Q::Box )
32 return boxRect( checkBox, contentsRect );
33
34 if ( subControl == Q::Indicator )
35 {
36 const auto boxRect = subControlRect( skinnable, contentsRect, Q::Box );
37 return skinnable->innerBox( Q::Box, boxRect );
38 }
39
40 if ( subControl == Q::Text )
41 return textRect( checkBox, contentsRect );
42
43 return contentsRect;
44}
45
46QRectF QskCheckBoxSkinlet::textRect(
47 const QskCheckBox* checkBox, const QRectF& contentsRect ) const
48{
49 using Q = QskCheckBox;
50
51 const auto boxRect = subControlRect( checkBox, contentsRect, Q::Box );
52 const qreal spacing = checkBox->spacingHint( Q::Panel );
53
54 auto r = subControlRect( checkBox, contentsRect, Q::Panel );
55 r = checkBox->innerBox( Q::Panel, r );
56
57 if ( checkBox->layoutMirroring() )
58 r.setRight( boxRect.left() - spacing );
59 else
60 r.setLeft( boxRect.right() + spacing );
61
62 return r;
63}
64
65QRectF QskCheckBoxSkinlet::boxRect(
66 const QskCheckBox* checkBox, const QRectF& contentsRect ) const
67{
68 const auto size = checkBox->strutSizeHint( QskCheckBox::Box );
69
70 auto r = checkBox->innerBox( QskCheckBox::Panel, contentsRect );
71
72 if ( checkBox->layoutMirroring() )
73 r.setLeft( r.right() - size.width() );
74 else
75 r.setWidth( size.width() );
76
77 r.setTop( r.top() + 0.5 * ( r.height() - size.height() ) );
78 r.setHeight( size.height() );
79
80 return r;
81}
82
83QSGNode* QskCheckBoxSkinlet::updateSubNode(
84 const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
85{
86 using Q = QskCheckBox;
87
88 auto checkBox = static_cast< const QskCheckBox* >( skinnable );
89
90 switch( nodeRole )
91 {
92 case PanelRole:
93 return updateBoxNode( skinnable, node, Q::Panel );
94
95 case BoxRole:
96 return updateBoxNode( skinnable, node, Q::Box );
97
98 case IndicatorRole:
99 return updateSymbolNode( checkBox, node, Q::Indicator );
100
101 case TextRole:
102 return updateTextNode( checkBox, node );
103 }
104
105 return Inherited::updateSubNode( skinnable, nodeRole, node );
106}
107
108QSGNode* QskCheckBoxSkinlet::updateTextNode(
109 const QskCheckBox* checkBox, QSGNode* node ) const
110{
111 using Q = QskCheckBox;
112
113 const auto rect = checkBox->subControlRect( Q::Text );
114 const auto alignH = checkBox->layoutMirroring()
115 ? Qt::AlignRight : Qt::AlignLeft;
116
117 return QskSkinlet::updateTextNode( checkBox, node,
118 rect, alignH | Qt::AlignVCenter, checkBox->text(), Q::Text );
119}
120
121QSizeF QskCheckBoxSkinlet::sizeHint( const QskSkinnable* skinnable,
122 Qt::SizeHint which, const QSizeF& ) const
123{
124 using Q = QskCheckBox;
125
126 if ( which != Qt::PreferredSize )
127 return QSizeF();
128
129 auto checkBox = static_cast< const QskCheckBox* >( skinnable );
130
131 auto size = skinnable->strutSizeHint( QskCheckBox::Box );
132 size = skinnable->outerBoxSize( Q::Panel, size );
133
134 auto text = checkBox->text();
135 if ( !text.isEmpty() )
136 {
137 qreal extra = skinnable->spacingHint( Q::Panel );
138
139 if ( which == Qt::MinimumSize )
140 text = 'W';
141
142 const auto font = skinnable->effectiveFont( Q::Text );
143 extra += qskHorizontalAdvance( font, text );
144
145 size.setWidth( size.width() + extra );
146 }
147
148 return size.expandedTo( skinnable->strutSizeHint( Q::Panel ) );
149}
150
151#include "moc_QskCheckBoxSkinlet.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
QRectF subControlRect(QskAspect::Subcontrol) const
bool layoutMirroring() const
Definition QskItem.cpp:511
Describes the rendering interface of a QskControl. Change the skinlet to change the appearance of the...
Definition QskSkinlet.h:34
qreal spacingHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a spacing hint.
QFont effectiveFont(QskAspect) const
QSizeF strutSizeHint(QskAspect, QskSkinHintStatus *=nullptr) const
Retrieves a strut size hint.
QSizeF outerBoxSize(QskAspect, const QSizeF &innerBoxSize) const
Calculate the size, when being expanded by paddings, indentations.
QRectF innerBox(QskAspect, const QRectF &outerBox) const
Calculate the rectangle, whith paddings, indentations being subtracted.