QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskStatusIndicatorSkinlet.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskStatusIndicatorSkinlet.h"
7#include "QskStatusIndicator.h"
8
9#include "QskAspect.h"
10#include "QskColorFilter.h"
11#include "QskFunctions.h"
12#include "QskGraphic.h"
13
14QskStatusIndicatorSkinlet::QskStatusIndicatorSkinlet( QskSkin* skin )
15 : Inherited( skin )
16{
17 setNodeRoles( { GraphicRole } );
18}
19
20QskStatusIndicatorSkinlet::~QskStatusIndicatorSkinlet() = default;
21
22QRectF QskStatusIndicatorSkinlet::subControlRect( const QskSkinnable* skinnable,
23 const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
24{
25 const auto label = static_cast< const QskStatusIndicator* >( skinnable );
26
27 if ( subControl == QskStatusIndicator::Graphic )
28 {
29 return graphicRect( label, contentsRect );
30 }
31
32 return Inherited::subControlRect( skinnable, contentsRect, subControl );
33}
34
35QSGNode* QskStatusIndicatorSkinlet::updateSubNode(
36 const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
37{
38 const auto indicator = static_cast< const QskStatusIndicator* >( skinnable );
39
40 switch ( nodeRole )
41 {
42 case GraphicRole:
43 {
44 return updateGraphicNode( indicator, node );
45 }
46 }
47
48 return Inherited::updateSubNode( skinnable, nodeRole, node );
49}
50
51QRect QskStatusIndicatorSkinlet::graphicRect(
52 const QskStatusIndicator* indicator, const QRectF& contentsRect ) const
53{
54 // maybe we should have a common base class for
55 // QskStatusIndicator and QskGraphicLabel
56
57 // PreserveAspectFit/PreserveAspectCrop
58
59 const QRect graphicRect = contentsRect.toAlignedRect();
60
61 QSizeF sz = indicator->graphic( indicator->status() ).defaultSize();
62 sz.scale( graphicRect.size(), Qt::KeepAspectRatio );
63
64 return qskAlignedRect( graphicRect,
65 ( int ) sz.width(), ( int ) sz.height(), Qt::AlignCenter );
66}
67
68QSGNode* QskStatusIndicatorSkinlet::updateGraphicNode(
69 const QskStatusIndicator* indicator, QSGNode* node ) const
70{
71 const auto subControl = QskStatusIndicator::Graphic;
72
73 const auto rect = subControlRect( indicator, indicator->contentsRect(), subControl );
74 const auto alignment = indicator->alignmentHint( subControl, Qt::AlignCenter );
75 const int status = indicator->status();
76
77 node = QskSkinlet::updateGraphicNode( indicator, node,
78 indicator->graphic( status ), indicator->graphicFilter( status ),
79 rect, alignment );
80
81 return node;
82}
83
84QSizeF QskStatusIndicatorSkinlet::sizeHint( const QskSkinnable* skinnable,
85 Qt::SizeHint which, const QSizeF& constraint ) const
86{
87 if ( which != Qt::PreferredSize )
88 return QSizeF();
89
90 const auto indicator = static_cast< const QskStatusIndicator* >( skinnable );
91
92 QSizeF sz;
93
94 for ( const auto status : indicator->statusList() )
95 {
96 const auto graphic = indicator->graphic( status );
97
98 if ( !graphic.isEmpty() )
99 {
100 auto hint = graphic.defaultSize();
101
102 if ( !hint.isEmpty() )
103 {
104 if ( constraint.width() >= 0.0 )
105 {
106 hint.setHeight( sz.height() * constraint.width() / sz.width() );
107 }
108 else if ( constraint.height() >= 0.0 )
109 {
110 hint.setWidth( sz.width() * constraint.height() / sz.height() );
111 }
112 }
113
114 sz = sz.expandedTo( hint );
115 }
116 }
117
118 return sz;
119}
120
121#include "moc_QskStatusIndicatorSkinlet.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
QRectF contentsRect() const
Qt::Alignment alignmentHint(QskAspect, Qt::Alignment=Qt::Alignment()) const
Retrieves an alignment hint.